Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / processes.c
1 /**
2  * collectd - src/processes.c
3  * Copyright (C) 2005       Lyonel Vincent
4  * Copyright (C) 2006-2017  Florian octo Forster
5  * Copyright (C) 2008       Oleg King
6  * Copyright (C) 2009       Sebastian Harl
7  * Copyright (C) 2009       Andrés J. Díaz
8  * Copyright (C) 2009       Manuel Sanmartin
9  * Copyright (C) 2010       Clément Stenac
10  * Copyright (C) 2012       Cosmin Ioiart
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation; either version 2 of the License, or (at your
15  * option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
25  *
26  * Authors:
27  *   Lyonel Vincent <lyonel at ezix.org>
28  *   Florian octo Forster <octo at collectd.org>
29  *   Oleg King <king2 at kaluga.ru>
30  *   Sebastian Harl <sh at tokkee.org>
31  *   Andrés J. Díaz <ajdiaz at connectical.com>
32  *   Manuel Sanmartin
33  *   Clément Stenac <clement.stenac at diwi.org>
34  *   Cosmin Ioiart <cioiart at gmail.com>
35  *   Pavel Rochnyack <pavel2000 at ngs.ru>
36  *   Wilfried Goesgens <dothebart at citadel.org>
37  **/
38
39 #include "collectd.h"
40
41 #include "plugin.h"
42 #include "utils/common/common.h"
43
44 #if HAVE_LIBTASKSTATS
45 #include "utils/taskstats/taskstats.h"
46 #include "utils_complain.h"
47 #endif
48
49 /* Include header files for the mach system, if they exist.. */
50 #if HAVE_THREAD_INFO
51 #if HAVE_MACH_MACH_INIT_H
52 #include <mach/mach_init.h>
53 #endif
54 #if HAVE_MACH_HOST_PRIV_H
55 #include <mach/host_priv.h>
56 #endif
57 #if HAVE_MACH_MACH_ERROR_H
58 #include <mach/mach_error.h>
59 #endif
60 #if HAVE_MACH_MACH_HOST_H
61 #include <mach/mach_host.h>
62 #endif
63 #if HAVE_MACH_MACH_PORT_H
64 #include <mach/mach_port.h>
65 #endif
66 #if HAVE_MACH_MACH_TYPES_H
67 #include <mach/mach_types.h>
68 #endif
69 #if HAVE_MACH_MESSAGE_H
70 #include <mach/message.h>
71 #endif
72 #if HAVE_MACH_PROCESSOR_SET_H
73 #include <mach/processor_set.h>
74 #endif
75 #if HAVE_MACH_TASK_H
76 #include <mach/task.h>
77 #endif
78 #if HAVE_MACH_THREAD_ACT_H
79 #include <mach/thread_act.h>
80 #endif
81 #if HAVE_MACH_VM_REGION_H
82 #include <mach/vm_region.h>
83 #endif
84 #if HAVE_MACH_VM_MAP_H
85 #include <mach/vm_map.h>
86 #endif
87 #if HAVE_MACH_VM_PROT_H
88 #include <mach/vm_prot.h>
89 #endif
90 #if HAVE_SYS_SYSCTL_H
91 #include <sys/sysctl.h>
92 #endif
93 /* #endif HAVE_THREAD_INFO */
94
95 #elif KERNEL_LINUX
96 #if HAVE_LINUX_CONFIG_H
97 #include <linux/config.h>
98 #endif
99 #ifndef CONFIG_HZ
100 #define CONFIG_HZ 100
101 #endif
102 /* #endif KERNEL_LINUX */
103
104 #elif HAVE_LIBKVM_GETPROCS &&                                                  \
105     (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
106 #include <kvm.h>
107 #include <sys/param.h>
108 #include <sys/proc.h>
109 #include <sys/sysctl.h>
110 #include <sys/user.h>
111 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
112  * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
113
114 #elif HAVE_PROCINFO_H
115 #include <procinfo.h>
116 #include <sys/types.h>
117
118 #define MAXPROCENTRY 32
119 #define MAXTHRDENTRY 16
120 #define MAXARGLN 1024
121 /* #endif HAVE_PROCINFO_H */
122
123 #elif KERNEL_SOLARIS
124 /* Hack: Avoid #error when building a 32-bit binary with
125  * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
126  * of the structures in <sys/procfs.h> uses an off_t, but that
127  * isn't relevant to our usage of procfs. */
128 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
129 #define SAVE_FOB_64
130 #undef _FILE_OFFSET_BITS
131 #endif
132
133 #include <procfs.h>
134
135 #ifdef SAVE_FOB_64
136 #define _FILE_OFFSET_BITS 64
137 #undef SAVE_FOB_64
138 #endif
139
140 #include <dirent.h>
141 #include <sys/user.h>
142
143 #ifndef MAXCOMLEN
144 #define MAXCOMLEN 16
145 #endif
146
147 /* #endif KERNEL_SOLARIS */
148
149 #else
150 #error "No applicable input method."
151 #endif
152
153 #if HAVE_REGEX_H
154 #include <regex.h>
155 #endif
156
157 #if HAVE_KSTAT_H
158 #include <kstat.h>
159 #endif
160
161 #ifdef HAVE_SYS_CAPABILITY_H
162 #include <sys/capability.h>
163 #endif
164
165 #ifndef CMDLINE_BUFFER_SIZE
166 #if defined(ARG_MAX) && (ARG_MAX < 4096)
167 #define CMDLINE_BUFFER_SIZE ARG_MAX
168 #else
169 #define CMDLINE_BUFFER_SIZE 4096
170 #endif
171 #endif
172
173 #define PROCSTAT_NAME_LEN 256
174 typedef struct process_entry_s {
175   unsigned long id;
176   char name[PROCSTAT_NAME_LEN];
177
178   unsigned long num_proc;
179   unsigned long num_lwp;
180   unsigned long num_fd;
181   unsigned long num_maps;
182   unsigned long vmem_size;
183   unsigned long vmem_rss;
184   unsigned long vmem_data;
185   unsigned long vmem_code;
186   unsigned long stack_size;
187
188   derive_t vmem_minflt_counter;
189   derive_t vmem_majflt_counter;
190
191   derive_t cpu_user_counter;
192   derive_t cpu_system_counter;
193
194   /* io data */
195   derive_t io_rchar;
196   derive_t io_wchar;
197   derive_t io_syscr;
198   derive_t io_syscw;
199   derive_t io_diskr;
200   derive_t io_diskw;
201   bool has_io;
202
203   derive_t cswitch_vol;
204   derive_t cswitch_invol;
205   bool has_cswitch;
206
207 #if HAVE_LIBTASKSTATS
208   ts_delay_t delay;
209 #endif
210   bool has_delay;
211
212   bool has_fd;
213
214   bool has_maps;
215 } process_entry_t;
216
217 typedef struct procstat_entry_s {
218   unsigned long id;
219   unsigned char age;
220
221   derive_t vmem_minflt_counter;
222   derive_t vmem_majflt_counter;
223
224   derive_t cpu_user_counter;
225   derive_t cpu_system_counter;
226
227   /* io data */
228   derive_t io_rchar;
229   derive_t io_wchar;
230   derive_t io_syscr;
231   derive_t io_syscw;
232   derive_t io_diskr;
233   derive_t io_diskw;
234
235   derive_t cswitch_vol;
236   derive_t cswitch_invol;
237
238 #if HAVE_LIBTASKSTATS
239   value_to_rate_state_t delay_cpu;
240   value_to_rate_state_t delay_blkio;
241   value_to_rate_state_t delay_swapin;
242   value_to_rate_state_t delay_freepages;
243 #endif
244
245   struct procstat_entry_s *next;
246 } procstat_entry_t;
247
248 typedef struct procstat {
249   char name[PROCSTAT_NAME_LEN];
250 #if HAVE_REGEX_H
251   regex_t *re;
252 #endif
253
254   unsigned long num_proc;
255   unsigned long num_lwp;
256   unsigned long num_fd;
257   unsigned long num_maps;
258   unsigned long vmem_size;
259   unsigned long vmem_rss;
260   unsigned long vmem_data;
261   unsigned long vmem_code;
262   unsigned long stack_size;
263
264   derive_t vmem_minflt_counter;
265   derive_t vmem_majflt_counter;
266
267   derive_t cpu_user_counter;
268   derive_t cpu_system_counter;
269
270   /* io data */
271   derive_t io_rchar;
272   derive_t io_wchar;
273   derive_t io_syscr;
274   derive_t io_syscw;
275   derive_t io_diskr;
276   derive_t io_diskw;
277
278   derive_t cswitch_vol;
279   derive_t cswitch_invol;
280
281   /* Linux Delay Accounting. Unit is ns/s. */
282   gauge_t delay_cpu;
283   gauge_t delay_blkio;
284   gauge_t delay_swapin;
285   gauge_t delay_freepages;
286
287   bool report_fd_num;
288   bool report_maps_num;
289   bool report_ctx_switch;
290   bool report_delay;
291
292   struct procstat *next;
293   struct procstat_entry_s *instances;
294 } procstat_t;
295
296 static procstat_t *list_head_g;
297
298 static bool want_init = true;
299 static bool report_ctx_switch;
300 static bool report_fd_num;
301 static bool report_maps_num;
302 static bool report_delay;
303
304 #if HAVE_THREAD_INFO
305 static mach_port_t port_host_self;
306 static mach_port_t port_task_self;
307
308 static processor_set_name_array_t pset_list;
309 static mach_msg_type_number_t pset_list_len;
310 /* #endif HAVE_THREAD_INFO */
311
312 #elif KERNEL_LINUX
313 static long pagesize_g;
314 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
315 /* #endif KERNEL_LINUX */
316
317 #elif HAVE_LIBKVM_GETPROCS &&                                                  \
318     (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
319 static int pagesize;
320 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
321  * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
322
323 #elif HAVE_PROCINFO_H
324 static struct procentry64 procentry[MAXPROCENTRY];
325 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
326 static int pagesize;
327
328 #ifndef _AIXVERSION_610
329 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
330                pid_t *index, int count);
331 int getthrds64(pid_t, void *, int, tid64_t *, int);
332 #endif
333 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
334 #endif /* HAVE_PROCINFO_H */
335
336 #if HAVE_LIBTASKSTATS
337 static ts_t *taskstats_handle;
338 #endif
339
340 /* put name of process from config to list_head_g tree
341  * list_head_g is a list of 'procstat_t' structs with
342  * processes names we want to watch */
343 static procstat_t *ps_list_register(const char *name, const char *regexp) {
344   procstat_t *new;
345   procstat_t *ptr;
346   int status;
347
348   new = calloc(1, sizeof(*new));
349   if (new == NULL) {
350     ERROR("processes plugin: ps_list_register: calloc failed.");
351     return NULL;
352   }
353   sstrncpy(new->name, name, sizeof(new->name));
354
355   new->io_rchar = -1;
356   new->io_wchar = -1;
357   new->io_syscr = -1;
358   new->io_syscw = -1;
359   new->io_diskr = -1;
360   new->io_diskw = -1;
361   new->cswitch_vol = -1;
362   new->cswitch_invol = -1;
363
364   new->report_fd_num = report_fd_num;
365   new->report_maps_num = report_maps_num;
366   new->report_ctx_switch = report_ctx_switch;
367   new->report_delay = report_delay;
368
369 #if HAVE_REGEX_H
370   if (regexp != NULL) {
371     DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
372           name);
373     new->re = malloc(sizeof(*new->re));
374     if (new->re == NULL) {
375       ERROR("processes plugin: ps_list_register: malloc failed.");
376       sfree(new);
377       return NULL;
378     }
379
380     status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
381     if (status != 0) {
382       DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
383             regexp);
384       sfree(new->re);
385       sfree(new);
386       return NULL;
387     }
388   }
389 #else
390   if (regexp != NULL) {
391     ERROR("processes plugin: ps_list_register: "
392           "Regular expression \"%s\" found in config "
393           "file, but support for regular expressions "
394           "has been disabled at compile time.",
395           regexp);
396     sfree(new);
397     return NULL;
398   }
399 #endif
400
401   for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
402     if (strcmp(ptr->name, name) == 0) {
403       WARNING("processes plugin: You have configured more "
404               "than one `Process' or "
405               "`ProcessMatch' with the same name. "
406               "All but the first setting will be "
407               "ignored.");
408 #if HAVE_REGEX_H
409       sfree(new->re);
410 #endif
411       sfree(new);
412       return NULL;
413     }
414
415     if (ptr->next == NULL)
416       break;
417   }
418
419   if (ptr == NULL)
420     list_head_g = new;
421   else
422     ptr->next = new;
423
424   return new;
425 } /* void ps_list_register */
426
427 /* try to match name against entry, returns 1 if success */
428 static int ps_list_match(const char *name, const char *cmdline,
429                          procstat_t *ps) {
430 #if HAVE_REGEX_H
431   if (ps->re != NULL) {
432     int status;
433     const char *str;
434
435     str = cmdline;
436     if ((str == NULL) || (str[0] == 0))
437       str = name;
438
439     assert(str != NULL);
440
441     status = regexec(ps->re, str,
442                      /* nmatch = */ 0,
443                      /* pmatch = */ NULL,
444                      /* eflags = */ 0);
445     if (status == 0)
446       return 1;
447   } else
448 #endif
449       if (strcmp(ps->name, name) == 0)
450     return 1;
451
452   return 0;
453 } /* int ps_list_match */
454
455 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
456                               derive_t new_counter) {
457   unsigned long curr_value;
458
459   if (want_init) {
460     *curr_counter = new_counter;
461     return;
462   }
463
464   if (new_counter < *curr_counter)
465     curr_value = new_counter + (ULONG_MAX - *curr_counter);
466   else
467     curr_value = new_counter - *curr_counter;
468
469   if (*group_counter == -1)
470     *group_counter = 0;
471
472   *curr_counter = new_counter;
473   *group_counter += curr_value;
474 }
475
476 #if HAVE_LIBTASKSTATS
477 static void ps_update_delay_one(gauge_t *out_rate_sum,
478                                 value_to_rate_state_t *state, uint64_t cnt,
479                                 cdtime_t t) {
480   gauge_t rate = NAN;
481   int status = value_to_rate(&rate, (value_t){.counter = (counter_t)cnt},
482                              DS_TYPE_COUNTER, t, state);
483   if ((status != 0) || isnan(rate)) {
484     return;
485   }
486
487   if (isnan(*out_rate_sum)) {
488     *out_rate_sum = rate;
489   } else {
490     *out_rate_sum += rate;
491   }
492 }
493
494 static void ps_update_delay(procstat_t *out, procstat_entry_t *prev,
495                             process_entry_t *curr) {
496   cdtime_t now = cdtime();
497
498   ps_update_delay_one(&out->delay_cpu, &prev->delay_cpu, curr->delay.cpu_ns,
499                       now);
500   ps_update_delay_one(&out->delay_blkio, &prev->delay_blkio,
501                       curr->delay.blkio_ns, now);
502   ps_update_delay_one(&out->delay_swapin, &prev->delay_swapin,
503                       curr->delay.swapin_ns, now);
504   ps_update_delay_one(&out->delay_freepages, &prev->delay_freepages,
505                       curr->delay.freepages_ns, now);
506 }
507 #endif
508
509 /* add process entry to 'instances' of process 'name' (or refresh it) */
510 static void ps_list_add(const char *name, const char *cmdline,
511                         process_entry_t *entry) {
512   procstat_entry_t *pse;
513
514   if (entry->id == 0)
515     return;
516
517   for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
518     if ((ps_list_match(name, cmdline, ps)) == 0)
519       continue;
520
521 #if KERNEL_LINUX
522     ps_fill_details(ps, entry);
523 #endif
524
525     for (pse = ps->instances; pse != NULL; pse = pse->next)
526       if ((pse->id == entry->id) || (pse->next == NULL))
527         break;
528
529     if ((pse == NULL) || (pse->id != entry->id)) {
530       procstat_entry_t *new;
531
532       new = calloc(1, sizeof(*new));
533       if (new == NULL)
534         return;
535       new->id = entry->id;
536
537       if (pse == NULL)
538         ps->instances = new;
539       else
540         pse->next = new;
541
542       pse = new;
543     }
544
545     pse->age = 0;
546
547     ps->num_proc += entry->num_proc;
548     ps->num_lwp += entry->num_lwp;
549     ps->num_fd += entry->num_fd;
550     ps->num_maps += entry->num_maps;
551     ps->vmem_size += entry->vmem_size;
552     ps->vmem_rss += entry->vmem_rss;
553     ps->vmem_data += entry->vmem_data;
554     ps->vmem_code += entry->vmem_code;
555     ps->stack_size += entry->stack_size;
556
557     if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
558       ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
559       ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
560     }
561
562     if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
563       ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
564       ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
565     }
566
567     if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
568       ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
569       ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
570     }
571
572     if ((entry->cswitch_vol != -1) && (entry->cswitch_invol != -1)) {
573       ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
574                         entry->cswitch_vol);
575       ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
576                         entry->cswitch_invol);
577     }
578
579     ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
580                       entry->vmem_minflt_counter);
581     ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
582                       entry->vmem_majflt_counter);
583
584     ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
585                       entry->cpu_user_counter);
586     ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
587                       entry->cpu_system_counter);
588
589 #if HAVE_LIBTASKSTATS
590     if (entry->has_delay)
591       ps_update_delay(ps, pse, entry);
592 #endif
593   }
594 }
595
596 /* remove old entries from instances of processes in list_head_g */
597 static void ps_list_reset(void) {
598   procstat_entry_t *pse;
599   procstat_entry_t *pse_prev;
600
601   for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
602     ps->num_proc = 0;
603     ps->num_lwp = 0;
604     ps->num_fd = 0;
605     ps->num_maps = 0;
606     ps->vmem_size = 0;
607     ps->vmem_rss = 0;
608     ps->vmem_data = 0;
609     ps->vmem_code = 0;
610     ps->stack_size = 0;
611
612     ps->delay_cpu = NAN;
613     ps->delay_blkio = NAN;
614     ps->delay_swapin = NAN;
615     ps->delay_freepages = NAN;
616
617     pse_prev = NULL;
618     pse = ps->instances;
619     while (pse != NULL) {
620       if (pse->age > 0) {
621         DEBUG("Removing this procstat entry cause it's too old: "
622               "id = %lu; name = %s;",
623               pse->id, ps->name);
624
625         if (pse_prev == NULL) {
626           ps->instances = pse->next;
627           free(pse);
628           pse = ps->instances;
629         } else {
630           pse_prev->next = pse->next;
631           free(pse);
632           pse = pse_prev->next;
633         }
634       } else {
635         pse->age = 1;
636         pse_prev = pse;
637         pse = pse->next;
638       }
639     } /* while (pse != NULL) */
640   }   /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
641 }
642
643 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
644   for (int i = 0; i < ci->children_num; i++) {
645     oconfig_item_t *c = ci->children + i;
646
647     if (strcasecmp(c->key, "CollectContextSwitch") == 0)
648       cf_util_get_boolean(c, &ps->report_ctx_switch);
649     else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
650       cf_util_get_boolean(c, &ps->report_fd_num);
651     else if (strcasecmp(c->key, "CollectMemoryMaps") == 0)
652       cf_util_get_boolean(c, &ps->report_maps_num);
653     else if (strcasecmp(c->key, "CollectDelayAccounting") == 0) {
654 #if HAVE_LIBTASKSTATS
655       cf_util_get_boolean(c, &ps->report_delay);
656 #else
657       WARNING("processes plugin: The plugin has been compiled without support "
658               "for the \"CollectDelayAccounting\" option.");
659 #endif
660     } else {
661       ERROR("processes plugin: Option \"%s\" not allowed here.", c->key);
662     }
663   } /* for (ci->children) */
664 } /* void ps_tune_instance */
665
666 /* put all pre-defined 'Process' names from config to list_head_g tree */
667 static int ps_config(oconfig_item_t *ci) {
668 #if KERNEL_LINUX
669   const size_t max_procname_len = 15;
670 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
671   const size_t max_procname_len = MAXCOMLEN - 1;
672 #endif
673
674   procstat_t *ps;
675
676   for (int i = 0; i < ci->children_num; ++i) {
677     oconfig_item_t *c = ci->children + i;
678
679     if (strcasecmp(c->key, "Process") == 0) {
680       if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
681         ERROR("processes plugin: `Process' expects exactly "
682               "one string argument (got %i).",
683               c->values_num);
684         continue;
685       }
686
687 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
688       if (strlen(c->values[0].value.string) > max_procname_len) {
689         WARNING("processes plugin: this platform has a %" PRIsz
690                 " character limit "
691                 "to process names. The `Process \"%s\"' option will "
692                 "not work as expected.",
693                 max_procname_len, c->values[0].value.string);
694       }
695 #endif
696
697       ps = ps_list_register(c->values[0].value.string, NULL);
698
699       if (c->children_num != 0 && ps != NULL)
700         ps_tune_instance(c, ps);
701     } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
702       if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
703           (OCONFIG_TYPE_STRING != c->values[1].type)) {
704         ERROR("processes plugin: `ProcessMatch' needs exactly "
705               "two string arguments (got %i).",
706               c->values_num);
707         continue;
708       }
709
710       ps = ps_list_register(c->values[0].value.string,
711                             c->values[1].value.string);
712
713       if (c->children_num != 0 && ps != NULL)
714         ps_tune_instance(c, ps);
715     } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
716       cf_util_get_boolean(c, &report_ctx_switch);
717     } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
718       cf_util_get_boolean(c, &report_fd_num);
719     } else if (strcasecmp(c->key, "CollectMemoryMaps") == 0) {
720       cf_util_get_boolean(c, &report_maps_num);
721     } else if (strcasecmp(c->key, "CollectDelayAccounting") == 0) {
722 #if HAVE_LIBTASKSTATS
723       cf_util_get_boolean(c, &report_delay);
724 #else
725       WARNING("processes plugin: The plugin has been compiled without support "
726               "for the \"CollectDelayAccounting\" option.");
727 #endif
728     } else {
729       ERROR("processes plugin: The `%s' configuration option is not "
730             "understood and will be ignored.",
731             c->key);
732       continue;
733     }
734   }
735
736   return 0;
737 }
738
739 static int ps_init(void) {
740 #if HAVE_THREAD_INFO
741   kern_return_t status;
742
743   port_host_self = mach_host_self();
744   port_task_self = mach_task_self();
745
746   if (pset_list != NULL) {
747     vm_deallocate(port_task_self, (vm_address_t)pset_list,
748                   pset_list_len * sizeof(processor_set_t));
749     pset_list = NULL;
750     pset_list_len = 0;
751   }
752
753   if ((status = host_processor_sets(port_host_self, &pset_list,
754                                     &pset_list_len)) != KERN_SUCCESS) {
755     ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
756     pset_list = NULL;
757     pset_list_len = 0;
758     return -1;
759   }
760     /* #endif HAVE_THREAD_INFO */
761
762 #elif KERNEL_LINUX
763   pagesize_g = sysconf(_SC_PAGESIZE);
764   DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
765
766 #if HAVE_LIBTASKSTATS
767   if (taskstats_handle == NULL) {
768     taskstats_handle = ts_create();
769     if (taskstats_handle == NULL) {
770       WARNING("processes plugin: Creating taskstats handle failed.");
771     }
772   }
773 #endif
774     /* #endif KERNEL_LINUX */
775
776 #elif HAVE_LIBKVM_GETPROCS &&                                                  \
777     (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
778   pagesize = getpagesize();
779   /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
780    * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
781
782 #elif HAVE_PROCINFO_H
783   pagesize = getpagesize();
784 #endif /* HAVE_PROCINFO_H */
785
786   return 0;
787 } /* int ps_init */
788
789 /* submit global state (e.g.: qty of zombies, running, etc..) */
790 static void ps_submit_state(const char *state, double value) {
791   value_list_t vl = VALUE_LIST_INIT;
792
793   vl.values = &(value_t){.gauge = value};
794   vl.values_len = 1;
795   sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
796   sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
797   sstrncpy(vl.type, "ps_state", sizeof(vl.type));
798   sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
799
800   plugin_dispatch_values(&vl);
801 }
802
803 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
804 static void ps_submit_proc_list(procstat_t *ps) {
805   value_list_t vl = VALUE_LIST_INIT;
806   value_t values[2];
807
808   vl.values = values;
809   sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
810   sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
811
812   sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
813   vl.values[0].gauge = ps->vmem_size;
814   vl.values_len = 1;
815   plugin_dispatch_values(&vl);
816
817   sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
818   vl.values[0].gauge = ps->vmem_rss;
819   vl.values_len = 1;
820   plugin_dispatch_values(&vl);
821
822   sstrncpy(vl.type, "ps_data", sizeof(vl.type));
823   vl.values[0].gauge = ps->vmem_data;
824   vl.values_len = 1;
825   plugin_dispatch_values(&vl);
826
827   sstrncpy(vl.type, "ps_code", sizeof(vl.type));
828   vl.values[0].gauge = ps->vmem_code;
829   vl.values_len = 1;
830   plugin_dispatch_values(&vl);
831
832   sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
833   vl.values[0].gauge = ps->stack_size;
834   vl.values_len = 1;
835   plugin_dispatch_values(&vl);
836
837   sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
838   vl.values[0].derive = ps->cpu_user_counter;
839   vl.values[1].derive = ps->cpu_system_counter;
840   vl.values_len = 2;
841   plugin_dispatch_values(&vl);
842
843   sstrncpy(vl.type, "ps_count", sizeof(vl.type));
844   vl.values[0].gauge = ps->num_proc;
845   vl.values[1].gauge = ps->num_lwp;
846   vl.values_len = 2;
847   plugin_dispatch_values(&vl);
848
849   sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
850   vl.values[0].derive = ps->vmem_minflt_counter;
851   vl.values[1].derive = ps->vmem_majflt_counter;
852   vl.values_len = 2;
853   plugin_dispatch_values(&vl);
854
855   if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
856     sstrncpy(vl.type, "io_octets", sizeof(vl.type));
857     vl.values[0].derive = ps->io_rchar;
858     vl.values[1].derive = ps->io_wchar;
859     vl.values_len = 2;
860     plugin_dispatch_values(&vl);
861   }
862
863   if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
864     sstrncpy(vl.type, "io_ops", sizeof(vl.type));
865     vl.values[0].derive = ps->io_syscr;
866     vl.values[1].derive = ps->io_syscw;
867     vl.values_len = 2;
868     plugin_dispatch_values(&vl);
869   }
870
871   if ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
872     sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
873     vl.values[0].derive = ps->io_diskr;
874     vl.values[1].derive = ps->io_diskw;
875     vl.values_len = 2;
876     plugin_dispatch_values(&vl);
877   }
878
879   if (ps->num_fd > 0) {
880     sstrncpy(vl.type, "file_handles", sizeof(vl.type));
881     vl.values[0].gauge = ps->num_fd;
882     vl.values_len = 1;
883     plugin_dispatch_values(&vl);
884   }
885
886   if (ps->num_maps > 0) {
887     sstrncpy(vl.type, "file_handles", sizeof(vl.type));
888     sstrncpy(vl.type_instance, "mapped", sizeof(vl.type_instance));
889     vl.values[0].gauge = ps->num_maps;
890     vl.values_len = 1;
891     plugin_dispatch_values(&vl);
892   }
893
894   if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
895     sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
896     sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
897     vl.values[0].derive = ps->cswitch_vol;
898     vl.values_len = 1;
899     plugin_dispatch_values(&vl);
900
901     sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
902     sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
903     vl.values[0].derive = ps->cswitch_invol;
904     vl.values_len = 1;
905     plugin_dispatch_values(&vl);
906   }
907
908   /* The ps->delay_* metrics are in nanoseconds per second. Convert to seconds
909    * per second. */
910   gauge_t const delay_factor = 1000000000.0;
911
912   struct {
913     const char *type_instance;
914     gauge_t rate_ns;
915   } delay_metrics[] = {
916       {"delay-cpu", ps->delay_cpu},
917       {"delay-blkio", ps->delay_blkio},
918       {"delay-swapin", ps->delay_swapin},
919       {"delay-freepages", ps->delay_freepages},
920   };
921   for (size_t i = 0; i < STATIC_ARRAY_SIZE(delay_metrics); i++) {
922     if (isnan(delay_metrics[i].rate_ns)) {
923       continue;
924     }
925     sstrncpy(vl.type, "delay_rate", sizeof(vl.type));
926     sstrncpy(vl.type_instance, delay_metrics[i].type_instance,
927              sizeof(vl.type_instance));
928     vl.values[0].gauge = delay_metrics[i].rate_ns / delay_factor;
929     vl.values_len = 1;
930     plugin_dispatch_values(&vl);
931   }
932
933   DEBUG(
934       "name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; num_maps = %lu; "
935       "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
936       "vmem_code = %lu; "
937       "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
938       "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
939       "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
940       "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
941       "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
942       "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 "; "
943       "delay_cpu = %g; delay_blkio = %g; "
944       "delay_swapin = %g; delay_freepages = %g;",
945       ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->num_maps,
946       ps->vmem_size, ps->vmem_rss, ps->vmem_data, ps->vmem_code,
947       ps->vmem_minflt_counter, ps->vmem_majflt_counter, ps->cpu_user_counter,
948       ps->cpu_system_counter, ps->io_rchar, ps->io_wchar, ps->io_syscr,
949       ps->io_syscw, ps->io_diskr, ps->io_diskw, ps->cswitch_vol,
950       ps->cswitch_invol, ps->delay_cpu, ps->delay_blkio, ps->delay_swapin,
951       ps->delay_freepages);
952
953 } /* void ps_submit_proc_list */
954
955 #if KERNEL_LINUX || KERNEL_SOLARIS
956 static void ps_submit_fork_rate(derive_t value) {
957   value_list_t vl = VALUE_LIST_INIT;
958
959   vl.values = &(value_t){.derive = value};
960   vl.values_len = 1;
961   sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
962   sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
963   sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
964   sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
965
966   plugin_dispatch_values(&vl);
967 }
968 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
969
970 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
971 #if KERNEL_LINUX
972 static int ps_read_tasks_status(process_entry_t *ps) {
973   char dirname[64];
974   DIR *dh;
975   char filename[64];
976   FILE *fh;
977   struct dirent *ent;
978   derive_t cswitch_vol = 0;
979   derive_t cswitch_invol = 0;
980   char buffer[1024];
981   char *fields[8];
982   int numfields;
983
984   snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
985
986   if ((dh = opendir(dirname)) == NULL) {
987     DEBUG("Failed to open directory `%s'", dirname);
988     return -1;
989   }
990
991   while ((ent = readdir(dh)) != NULL) {
992     char *tpid;
993
994     if (!isdigit((int)ent->d_name[0]))
995       continue;
996
997     tpid = ent->d_name;
998
999     int r = snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status",
1000                      ps->id, tpid);
1001     if ((size_t)r >= sizeof(filename)) {
1002       DEBUG("Filename too long: `%s'", filename);
1003       continue;
1004     }
1005
1006     if ((fh = fopen(filename, "r")) == NULL) {
1007       DEBUG("Failed to open file `%s'", filename);
1008       continue;
1009     }
1010
1011     while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1012       derive_t tmp;
1013       char *endptr;
1014
1015       if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
1016           strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
1017         continue;
1018
1019       numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1020
1021       if (numfields < 2)
1022         continue;
1023
1024       errno = 0;
1025       endptr = NULL;
1026       tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
1027       if ((errno == 0) && (endptr != fields[1])) {
1028         if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
1029           cswitch_vol += tmp;
1030         } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
1031           cswitch_invol += tmp;
1032         }
1033       }
1034     } /* while (fgets) */
1035
1036     if (fclose(fh)) {
1037       WARNING("processes: fclose: %s", STRERRNO);
1038     }
1039   }
1040   closedir(dh);
1041
1042   ps->cswitch_vol = cswitch_vol;
1043   ps->cswitch_invol = cswitch_invol;
1044
1045   return 0;
1046 } /* int *ps_read_tasks_status */
1047
1048 /* Read data from /proc/pid/status */
1049 static int ps_read_status(long pid, process_entry_t *ps) {
1050   FILE *fh;
1051   char buffer[1024];
1052   char filename[64];
1053   unsigned long lib = 0;
1054   unsigned long exe = 0;
1055   unsigned long data = 0;
1056   unsigned long threads = 0;
1057   char *fields[8];
1058   int numfields;
1059
1060   snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1061   if ((fh = fopen(filename, "r")) == NULL)
1062     return -1;
1063
1064   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1065     unsigned long tmp;
1066     char *endptr;
1067
1068     if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
1069       continue;
1070
1071     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1072
1073     if (numfields < 2)
1074       continue;
1075
1076     errno = 0;
1077     endptr = NULL;
1078     tmp = strtoul(fields[1], &endptr, /* base = */ 10);
1079     if ((errno == 0) && (endptr != fields[1])) {
1080       if (strncmp(buffer, "VmData", 6) == 0) {
1081         data = tmp;
1082       } else if (strncmp(buffer, "VmLib", 5) == 0) {
1083         lib = tmp;
1084       } else if (strncmp(buffer, "VmExe", 5) == 0) {
1085         exe = tmp;
1086       } else if (strncmp(buffer, "Threads", 7) == 0) {
1087         threads = tmp;
1088       }
1089     }
1090   } /* while (fgets) */
1091
1092   if (fclose(fh)) {
1093     WARNING("processes: fclose: %s", STRERRNO);
1094   }
1095
1096   ps->vmem_data = data * 1024;
1097   ps->vmem_code = (exe + lib) * 1024;
1098   if (threads != 0)
1099     ps->num_lwp = threads;
1100
1101   return 0;
1102 } /* int *ps_read_status */
1103
1104 static int ps_read_io(process_entry_t *ps) {
1105   FILE *fh;
1106   char buffer[1024];
1107   char filename[64];
1108
1109   char *fields[8];
1110   int numfields;
1111
1112   snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
1113   if ((fh = fopen(filename, "r")) == NULL) {
1114     DEBUG("ps_read_io: Failed to open file `%s'", filename);
1115     return -1;
1116   }
1117
1118   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1119     derive_t *val = NULL;
1120     long long tmp;
1121     char *endptr;
1122
1123     if (strncasecmp(buffer, "rchar:", 6) == 0)
1124       val = &(ps->io_rchar);
1125     else if (strncasecmp(buffer, "wchar:", 6) == 0)
1126       val = &(ps->io_wchar);
1127     else if (strncasecmp(buffer, "syscr:", 6) == 0)
1128       val = &(ps->io_syscr);
1129     else if (strncasecmp(buffer, "syscw:", 6) == 0)
1130       val = &(ps->io_syscw);
1131     else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
1132       val = &(ps->io_diskr);
1133     else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
1134       val = &(ps->io_diskw);
1135     else
1136       continue;
1137
1138     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1139
1140     if (numfields < 2)
1141       continue;
1142
1143     errno = 0;
1144     endptr = NULL;
1145     tmp = strtoll(fields[1], &endptr, /* base = */ 10);
1146     if ((errno != 0) || (endptr == fields[1]))
1147       *val = -1;
1148     else
1149       *val = (derive_t)tmp;
1150   } /* while (fgets) */
1151
1152   if (fclose(fh)) {
1153     WARNING("processes: fclose: %s", STRERRNO);
1154   }
1155   return 0;
1156 } /* int ps_read_io (...) */
1157
1158 static int ps_count_maps(pid_t pid) {
1159   FILE *fh;
1160   char buffer[1024];
1161   char filename[64];
1162   int count = 0;
1163
1164   snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
1165   if ((fh = fopen(filename, "r")) == NULL) {
1166     DEBUG("ps_count_maps: Failed to open file `%s'", filename);
1167     return -1;
1168   }
1169
1170   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1171     if (strchr(buffer, '\n')) {
1172       count++;
1173     }
1174   } /* while (fgets) */
1175
1176   if (fclose(fh)) {
1177     WARNING("processes: fclose: %s", STRERRNO);
1178   }
1179   return count;
1180 } /* int ps_count_maps (...) */
1181
1182 static int ps_count_fd(int pid) {
1183   char dirname[64];
1184   DIR *dh;
1185   struct dirent *ent;
1186   int count = 0;
1187
1188   snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1189
1190   if ((dh = opendir(dirname)) == NULL) {
1191     DEBUG("Failed to open directory `%s'", dirname);
1192     return -1;
1193   }
1194   while ((ent = readdir(dh)) != NULL) {
1195     if (!isdigit((int)ent->d_name[0]))
1196       continue;
1197     else
1198       count++;
1199   }
1200   closedir(dh);
1201
1202   return (count >= 1) ? count : 1;
1203 } /* int ps_count_fd (pid) */
1204
1205 #if HAVE_LIBTASKSTATS
1206 static int ps_delay(process_entry_t *ps) {
1207   if (taskstats_handle == NULL) {
1208     return ENOTCONN;
1209   }
1210
1211   int status = ts_delay_by_tgid(taskstats_handle, (uint32_t)ps->id, &ps->delay);
1212   if (status == EPERM) {
1213     static c_complain_t c;
1214 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
1215     if (check_capability(CAP_NET_ADMIN) != 0) {
1216       if (getuid() == 0) {
1217         c_complain(
1218             LOG_ERR, &c,
1219             "processes plugin: Reading Delay Accounting metric failed: %s. "
1220             "collectd is running as root, but missing the CAP_NET_ADMIN "
1221             "capability. The most common cause for this is that the init "
1222             "system is dropping capabilities.",
1223             STRERROR(status));
1224       } else {
1225         c_complain(
1226             LOG_ERR, &c,
1227             "processes plugin: Reading Delay Accounting metric failed: %s. "
1228             "collectd is not running as root and missing the CAP_NET_ADMIN "
1229             "capability. Either run collectd as root or grant it the "
1230             "CAP_NET_ADMIN capability using \"setcap cap_net_admin=ep " PREFIX
1231             "/sbin/collectd\".",
1232             STRERROR(status));
1233       }
1234     } else {
1235       ERROR("processes plugin: ts_delay_by_tgid failed: %s. The CAP_NET_ADMIN "
1236             "capability is available (I checked), so this error is utterly "
1237             "unexpected.",
1238             STRERROR(status));
1239     }
1240 #else
1241     c_complain(LOG_ERR, &c,
1242                "processes plugin: Reading Delay Accounting metric failed: %s. "
1243                "Reading Delay Accounting metrics requires root privileges.",
1244                STRERROR(status));
1245 #endif
1246     return status;
1247   } else if (status != 0) {
1248     ERROR("processes plugin: ts_delay_by_tgid failed: %s", STRERROR(status));
1249     return status;
1250   }
1251
1252   return 0;
1253 }
1254 #endif
1255
1256 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1257   if (entry->has_io == false) {
1258     ps_read_io(entry);
1259     entry->has_io = true;
1260   }
1261
1262   if (ps->report_ctx_switch) {
1263     if (entry->has_cswitch == false) {
1264       ps_read_tasks_status(entry);
1265       entry->has_cswitch = true;
1266     }
1267   }
1268
1269   if (ps->report_maps_num) {
1270     int num_maps;
1271     if (entry->has_maps == false && (num_maps = ps_count_maps(entry->id)) > 0) {
1272       entry->num_maps = num_maps;
1273     }
1274     entry->has_maps = true;
1275   }
1276
1277   if (ps->report_fd_num) {
1278     int num_fd;
1279     if (entry->has_fd == false && (num_fd = ps_count_fd(entry->id)) > 0) {
1280       entry->num_fd = num_fd;
1281     }
1282     entry->has_fd = true;
1283   }
1284
1285 #if HAVE_LIBTASKSTATS
1286   if (ps->report_delay && !entry->has_delay) {
1287     if (ps_delay(entry) == 0) {
1288       entry->has_delay = true;
1289     }
1290   }
1291 #endif
1292 } /* void ps_fill_details (...) */
1293
1294 /* ps_read_process reads process counters on Linux. */
1295 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1296   char filename[64];
1297   char buffer[1024];
1298
1299   char *fields[64];
1300   char fields_len;
1301
1302   size_t buffer_len;
1303
1304   char *buffer_ptr;
1305   size_t name_start_pos;
1306   size_t name_end_pos;
1307   size_t name_len;
1308
1309   derive_t cpu_user_counter;
1310   derive_t cpu_system_counter;
1311   long long unsigned vmem_size;
1312   long long unsigned vmem_rss;
1313   long long unsigned stack_size;
1314
1315   ssize_t status;
1316
1317   snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1318
1319   status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1320   if (status <= 0)
1321     return -1;
1322   buffer_len = (size_t)status;
1323   buffer[buffer_len] = 0;
1324
1325   /* The name of the process is enclosed in parens. Since the name can
1326    * contain parens itself, spaces, numbers and pretty much everything
1327    * else, use these to determine the process name. We don't use
1328    * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1329    * otherwise be required to determine name_len. */
1330   name_start_pos = 0;
1331   while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1332     name_start_pos++;
1333
1334   name_end_pos = buffer_len;
1335   while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1336     name_end_pos--;
1337
1338   /* Either '(' or ')' is not found or they are in the wrong order.
1339    * Anyway, something weird that shouldn't happen ever. */
1340   if (name_start_pos >= name_end_pos) {
1341     ERROR("processes plugin: name_start_pos = %" PRIsz
1342           " >= name_end_pos = %" PRIsz,
1343           name_start_pos, name_end_pos);
1344     return -1;
1345   }
1346
1347   name_len = (name_end_pos - name_start_pos) - 1;
1348   if (name_len >= sizeof(ps->name))
1349     name_len = sizeof(ps->name) - 1;
1350
1351   sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1352
1353   if ((buffer_len - name_end_pos) < 2)
1354     return -1;
1355   buffer_ptr = &buffer[name_end_pos + 2];
1356
1357   fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1358   if (fields_len < 22) {
1359     DEBUG("processes plugin: ps_read_process (pid = %li):"
1360           " `%s' has only %i fields..",
1361           pid, filename, fields_len);
1362     return -1;
1363   }
1364
1365   *state = fields[0][0];
1366
1367   if (*state == 'Z') {
1368     ps->num_lwp = 0;
1369     ps->num_proc = 0;
1370   } else {
1371     ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1372     if ((ps_read_status(pid, ps)) != 0) {
1373       /* No VMem data */
1374       ps->vmem_data = -1;
1375       ps->vmem_code = -1;
1376       DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1377     }
1378     if (ps->num_lwp == 0)
1379       ps->num_lwp = 1;
1380     ps->num_proc = 1;
1381   }
1382
1383   /* Leave the rest at zero if this is only a zombi */
1384   if (ps->num_proc == 0) {
1385     DEBUG("processes plugin: This is only a zombie: pid = %li; "
1386           "name = %s;",
1387           pid, ps->name);
1388     return 0;
1389   }
1390
1391   cpu_user_counter = atoll(fields[11]);
1392   cpu_system_counter = atoll(fields[12]);
1393   vmem_size = atoll(fields[20]);
1394   vmem_rss = atoll(fields[21]);
1395   ps->vmem_minflt_counter = atol(fields[7]);
1396   ps->vmem_majflt_counter = atol(fields[9]);
1397
1398   {
1399     unsigned long long stack_start = atoll(fields[25]);
1400     unsigned long long stack_ptr = atoll(fields[26]);
1401
1402     stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1403                                            : stack_ptr - stack_start;
1404   }
1405
1406   /* Convert jiffies to useconds */
1407   cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1408   cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1409   vmem_rss = vmem_rss * pagesize_g;
1410
1411   ps->cpu_user_counter = cpu_user_counter;
1412   ps->cpu_system_counter = cpu_system_counter;
1413   ps->vmem_size = (unsigned long)vmem_size;
1414   ps->vmem_rss = (unsigned long)vmem_rss;
1415   ps->stack_size = (unsigned long)stack_size;
1416
1417   /* no data by default. May be filled by ps_fill_details () */
1418   ps->io_rchar = -1;
1419   ps->io_wchar = -1;
1420   ps->io_syscr = -1;
1421   ps->io_syscw = -1;
1422   ps->io_diskr = -1;
1423   ps->io_diskw = -1;
1424
1425   ps->cswitch_vol = -1;
1426   ps->cswitch_invol = -1;
1427
1428   /* success */
1429   return 0;
1430 } /* int ps_read_process (...) */
1431
1432 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1433   char *buf_ptr;
1434   size_t len;
1435
1436   char file[PATH_MAX];
1437   int fd;
1438
1439   size_t n;
1440
1441   if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1442     return NULL;
1443
1444   snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1445
1446   errno = 0;
1447   fd = open(file, O_RDONLY);
1448   if (fd < 0) {
1449     /* ENOENT means the process exited while we were handling it.
1450      * Don't complain about this, it only fills the logs. */
1451     if (errno != ENOENT)
1452       WARNING("processes plugin: Failed to open `%s': %s.", file, STRERRNO);
1453     return NULL;
1454   }
1455
1456   buf_ptr = buf;
1457   len = buf_len;
1458
1459   n = 0;
1460
1461   while (42) {
1462     ssize_t status;
1463
1464     status = read(fd, (void *)buf_ptr, len);
1465
1466     if (status < 0) {
1467
1468       if ((EAGAIN == errno) || (EINTR == errno))
1469         continue;
1470
1471       WARNING("processes plugin: Failed to read from `%s': %s.", file,
1472               STRERRNO);
1473       close(fd);
1474       return NULL;
1475     }
1476
1477     n += status;
1478
1479     if (status == 0)
1480       break;
1481
1482     buf_ptr += status;
1483     len -= status;
1484
1485     if (len == 0)
1486       break;
1487   }
1488
1489   close(fd);
1490
1491   if (0 == n) {
1492     /* cmdline not available; e.g. kernel thread, zombie */
1493     if (NULL == name)
1494       return NULL;
1495
1496     snprintf(buf, buf_len, "[%s]", name);
1497     return buf;
1498   }
1499
1500   assert(n <= buf_len);
1501
1502   if (n == buf_len)
1503     --n;
1504   buf[n] = '\0';
1505
1506   --n;
1507   /* remove trailing whitespace */
1508   while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1509     buf[n] = '\0';
1510     --n;
1511   }
1512
1513   /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1514   while (n > 0) {
1515     if ('\0' == buf[n])
1516       buf[n] = ' ';
1517     --n;
1518   }
1519   return buf;
1520 } /* char *ps_get_cmdline (...) */
1521
1522 static int read_fork_rate(void) {
1523   FILE *proc_stat;
1524   char buffer[1024];
1525   value_t value;
1526   bool value_valid = 0;
1527
1528   proc_stat = fopen("/proc/stat", "r");
1529   if (proc_stat == NULL) {
1530     ERROR("processes plugin: fopen (/proc/stat) failed: %s", STRERRNO);
1531     return -1;
1532   }
1533
1534   while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1535     int status;
1536     char *fields[3];
1537     int fields_num;
1538
1539     fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1540     if (fields_num != 2)
1541       continue;
1542
1543     if (strcmp("processes", fields[0]) != 0)
1544       continue;
1545
1546     status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1547     if (status == 0)
1548       value_valid = 1;
1549
1550     break;
1551   }
1552   fclose(proc_stat);
1553
1554   if (!value_valid)
1555     return -1;
1556
1557   ps_submit_fork_rate(value.derive);
1558   return 0;
1559 }
1560 #endif /*KERNEL_LINUX */
1561
1562 #if KERNEL_SOLARIS
1563 static char *ps_get_cmdline(long pid,
1564                             char *name __attribute__((unused)), /* {{{ */
1565                             char *buffer, size_t buffer_size) {
1566   char path[PATH_MAX];
1567   psinfo_t info;
1568   ssize_t status;
1569
1570   snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1571
1572   status = read_file_contents(path, (void *)&info, sizeof(info));
1573   if ((status < 0) || (((size_t)status) != sizeof(info))) {
1574     ERROR("processes plugin: Unexpected return value "
1575           "while reading \"%s\": "
1576           "Returned %zd but expected %" PRIsz ".",
1577           path, status, buffer_size);
1578     return NULL;
1579   }
1580
1581   sstrncpy(buffer, info.pr_psargs, buffer_size);
1582
1583   return buffer;
1584 } /* }}} int ps_get_cmdline */
1585
1586 /*
1587  * Reads process information on the Solaris OS. The information comes mainly
1588  * from
1589  * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1590  * The values for input and ouput chars are calculated "by hand"
1591  * Added a few "solaris" specific process states as well
1592  */
1593 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1594   char filename[64];
1595   char f_psinfo[64], f_usage[64];
1596   char *buffer;
1597
1598   pstatus_t *myStatus;
1599   psinfo_t *myInfo;
1600   prusage_t *myUsage;
1601
1602   snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1603   snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1604   snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1605
1606   buffer = calloc(1, sizeof(pstatus_t));
1607   read_file_contents(filename, buffer, sizeof(pstatus_t));
1608   myStatus = (pstatus_t *)buffer;
1609
1610   buffer = calloc(1, sizeof(psinfo_t));
1611   read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1612   myInfo = (psinfo_t *)buffer;
1613
1614   buffer = calloc(1, sizeof(prusage_t));
1615   read_file_contents(f_usage, buffer, sizeof(prusage_t));
1616   myUsage = (prusage_t *)buffer;
1617
1618   sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1619   ps->num_lwp = myStatus->pr_nlwp;
1620   if (myInfo->pr_wstat != 0) {
1621     ps->num_proc = 0;
1622     ps->num_lwp = 0;
1623     *state = (char)'Z';
1624
1625     sfree(myStatus);
1626     sfree(myInfo);
1627     sfree(myUsage);
1628     return 0;
1629   } else {
1630     ps->num_proc = 1;
1631     ps->num_lwp = myInfo->pr_nlwp;
1632   }
1633
1634   /*
1635    * Convert system time and user time from nanoseconds to microseconds
1636    * for compatibility with the linux module
1637    */
1638   ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1639   ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1640
1641   /*
1642    * Convert rssize from KB to bytes to be consistent w/ the linux module
1643    */
1644   ps->vmem_rss = myInfo->pr_rssize * 1024;
1645   ps->vmem_size = myInfo->pr_size * 1024;
1646   ps->vmem_minflt_counter = myUsage->pr_minf;
1647   ps->vmem_majflt_counter = myUsage->pr_majf;
1648
1649   /*
1650    * TODO: Data and code segment calculations for Solaris
1651    */
1652
1653   ps->vmem_data = -1;
1654   ps->vmem_code = -1;
1655   ps->stack_size = myStatus->pr_stksize;
1656
1657   /*
1658    * TODO: File descriptor count for Solaris
1659    */
1660   ps->num_fd = 0;
1661
1662   /* Number of memory mappings */
1663   ps->num_maps = 0;
1664
1665   /*
1666    * Calculating input/ouput chars
1667    * Formula used is total chars / total blocks => chars/block
1668    * then convert input/output blocks to chars
1669    */
1670   ulong_t tot_chars = myUsage->pr_ioch;
1671   ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1672   ulong_t chars_per_block = 1;
1673   if (tot_blocks != 0)
1674     chars_per_block = tot_chars / tot_blocks;
1675   ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1676   ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1677   ps->io_syscr = myUsage->pr_sysc;
1678   ps->io_syscw = myUsage->pr_sysc;
1679   ps->io_diskr = -1;
1680   ps->io_diskw = -1;
1681
1682   /*
1683    * TODO: context switch counters for Solaris
1684    */
1685   ps->cswitch_vol = -1;
1686   ps->cswitch_invol = -1;
1687
1688   /*
1689    * TODO: Find way of setting BLOCKED and PAGING status
1690    */
1691
1692   *state = (char)'R';
1693   if (myStatus->pr_flags & PR_ASLEEP)
1694     *state = (char)'S';
1695   else if (myStatus->pr_flags & PR_STOPPED)
1696     *state = (char)'T';
1697   else if (myStatus->pr_flags & PR_DETACH)
1698     *state = (char)'E';
1699   else if (myStatus->pr_flags & PR_DAEMON)
1700     *state = (char)'A';
1701   else if (myStatus->pr_flags & PR_ISSYS)
1702     *state = (char)'Y';
1703   else if (myStatus->pr_flags & PR_ORPHAN)
1704     *state = (char)'O';
1705
1706   sfree(myStatus);
1707   sfree(myInfo);
1708   sfree(myUsage);
1709
1710   return 0;
1711 }
1712
1713 /*
1714  * Reads the number of threads created since the last reboot. On Solaris these
1715  * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1716  * The result is the sum for all the threads created on each cpu
1717  */
1718 static int read_fork_rate(void) {
1719   extern kstat_ctl_t *kc;
1720   derive_t result = 0;
1721
1722   if (kc == NULL)
1723     return -1;
1724
1725   for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1726        ksp_chain = ksp_chain->ks_next) {
1727     if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1728         (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1729         (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1730       long long tmp;
1731
1732       kstat_read(kc, ksp_chain, NULL);
1733
1734       tmp = get_kstat_value(ksp_chain, "nthreads");
1735       if (tmp != -1LL)
1736         result += tmp;
1737     }
1738   }
1739
1740   ps_submit_fork_rate(result);
1741   return 0;
1742 }
1743 #endif /* KERNEL_SOLARIS */
1744
1745 #if HAVE_THREAD_INFO
1746 static int mach_get_task_name(task_t t, int *pid, char *name,
1747                               size_t name_max_len) {
1748   int mib[4];
1749
1750   struct kinfo_proc kp;
1751   size_t kp_size;
1752
1753   mib[0] = CTL_KERN;
1754   mib[1] = KERN_PROC;
1755   mib[2] = KERN_PROC_PID;
1756
1757   if (pid_for_task(t, pid) != KERN_SUCCESS)
1758     return -1;
1759   mib[3] = *pid;
1760
1761   kp_size = sizeof(kp);
1762   if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1763     return -1;
1764
1765   if (name_max_len > (MAXCOMLEN + 1))
1766     name_max_len = MAXCOMLEN + 1;
1767
1768   strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1769   name[name_max_len - 1] = '\0';
1770
1771   DEBUG("pid = %i; name = %s;", *pid, name);
1772
1773   /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1774    * `top' does it, because it is a lot of work and only used when
1775    * debugging. -octo */
1776
1777   return 0;
1778 }
1779 #endif /* HAVE_THREAD_INFO */
1780 /* end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO */
1781
1782 /* do actual readings from kernel */
1783 static int ps_read(void) {
1784 #if HAVE_THREAD_INFO
1785   kern_return_t status;
1786
1787   processor_set_t port_pset_priv;
1788
1789   task_array_t task_list;
1790   mach_msg_type_number_t task_list_len;
1791
1792   int task_pid;
1793   char task_name[MAXCOMLEN + 1];
1794
1795   thread_act_array_t thread_list;
1796   mach_msg_type_number_t thread_list_len;
1797   thread_basic_info_data_t thread_data;
1798   mach_msg_type_number_t thread_data_len;
1799
1800   int running = 0;
1801   int sleeping = 0;
1802   int zombies = 0;
1803   int stopped = 0;
1804   int blocked = 0;
1805
1806   procstat_t *ps;
1807   process_entry_t pse;
1808
1809   ps_list_reset();
1810
1811   /*
1812    * The Mach-concept is a little different from the traditional UNIX
1813    * concept: All the work is done in threads. Threads are contained in
1814    * `tasks'. Therefore, `task status' doesn't make much sense, since
1815    * it's actually a `thread status'.
1816    * Tasks are assigned to sets of processors, so that's where you go to
1817    * get a list.
1818    */
1819   for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1820     if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1821                                           &port_pset_priv)) != KERN_SUCCESS) {
1822       ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1823       continue;
1824     }
1825
1826     if ((status = processor_set_tasks(port_pset_priv, &task_list,
1827                                       &task_list_len)) != KERN_SUCCESS) {
1828       ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1829       mach_port_deallocate(port_task_self, port_pset_priv);
1830       continue;
1831     }
1832
1833     for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1834       ps = NULL;
1835       if (mach_get_task_name(task_list[task], &task_pid, task_name,
1836                              PROCSTAT_NAME_LEN) == 0) {
1837         /* search for at least one match */
1838         for (ps = list_head_g; ps != NULL; ps = ps->next)
1839           /* FIXME: cmdline should be here instead of NULL */
1840           if (ps_list_match(task_name, NULL, ps) == 1)
1841             break;
1842       }
1843
1844       /* Collect more detailed statistics for this process */
1845       if (ps != NULL) {
1846         task_basic_info_data_t task_basic_info;
1847         mach_msg_type_number_t task_basic_info_len;
1848         task_events_info_data_t task_events_info;
1849         mach_msg_type_number_t task_events_info_len;
1850         task_absolutetime_info_data_t task_absolutetime_info;
1851         mach_msg_type_number_t task_absolutetime_info_len;
1852
1853         memset(&pse, '\0', sizeof(pse));
1854         pse.id = task_pid;
1855
1856         task_basic_info_len = TASK_BASIC_INFO_COUNT;
1857         status = task_info(task_list[task], TASK_BASIC_INFO,
1858                            (task_info_t)&task_basic_info, &task_basic_info_len);
1859         if (status != KERN_SUCCESS) {
1860           ERROR("task_info failed: %s", mach_error_string(status));
1861           continue; /* with next thread_list */
1862         }
1863
1864         task_events_info_len = TASK_EVENTS_INFO_COUNT;
1865         status =
1866             task_info(task_list[task], TASK_EVENTS_INFO,
1867                       (task_info_t)&task_events_info, &task_events_info_len);
1868         if (status != KERN_SUCCESS) {
1869           ERROR("task_info failed: %s", mach_error_string(status));
1870           continue; /* with next thread_list */
1871         }
1872
1873         task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1874         status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1875                            (task_info_t)&task_absolutetime_info,
1876                            &task_absolutetime_info_len);
1877         if (status != KERN_SUCCESS) {
1878           ERROR("task_info failed: %s", mach_error_string(status));
1879           continue; /* with next thread_list */
1880         }
1881
1882         pse.num_proc++;
1883         pse.vmem_size = task_basic_info.virtual_size;
1884         pse.vmem_rss = task_basic_info.resident_size;
1885         /* Does not seem to be easily exposed */
1886         pse.vmem_data = 0;
1887         pse.vmem_code = 0;
1888
1889         pse.io_rchar = -1;
1890         pse.io_wchar = -1;
1891         pse.io_syscr = -1;
1892         pse.io_syscw = -1;
1893         pse.io_diskr = -1;
1894         pse.io_diskw = -1;
1895
1896         /* File descriptor count not implemented */
1897         pse.num_fd = 0;
1898
1899         /* Number of memory mappings */
1900         pse.num_maps = 0;
1901
1902         pse.vmem_minflt_counter = task_events_info.cow_faults;
1903         pse.vmem_majflt_counter = task_events_info.faults;
1904
1905         pse.cpu_user_counter = task_absolutetime_info.total_user;
1906         pse.cpu_system_counter = task_absolutetime_info.total_system;
1907
1908         /* context switch counters not implemented */
1909         pse.cswitch_vol = -1;
1910         pse.cswitch_invol = -1;
1911       }
1912
1913       status = task_threads(task_list[task], &thread_list, &thread_list_len);
1914       if (status != KERN_SUCCESS) {
1915         /* Apple's `top' treats this case a zombie. It
1916          * makes sense to some extend: A `zombie'
1917          * thread is nonsense, since the task/process
1918          * is dead. */
1919         zombies++;
1920         DEBUG("task_threads failed: %s", mach_error_string(status));
1921         if (task_list[task] != port_task_self)
1922           mach_port_deallocate(port_task_self, task_list[task]);
1923         continue; /* with next task_list */
1924       }
1925
1926       for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1927            thread++) {
1928         thread_data_len = THREAD_BASIC_INFO_COUNT;
1929         status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1930                              (thread_info_t)&thread_data, &thread_data_len);
1931         if (status != KERN_SUCCESS) {
1932           ERROR("thread_info failed: %s", mach_error_string(status));
1933           if (task_list[task] != port_task_self)
1934             mach_port_deallocate(port_task_self, thread_list[thread]);
1935           continue; /* with next thread_list */
1936         }
1937
1938         if (ps != NULL)
1939           pse.num_lwp++;
1940
1941         switch (thread_data.run_state) {
1942         case TH_STATE_RUNNING:
1943           running++;
1944           break;
1945         case TH_STATE_STOPPED:
1946         /* What exactly is `halted'? */
1947         case TH_STATE_HALTED:
1948           stopped++;
1949           break;
1950         case TH_STATE_WAITING:
1951           sleeping++;
1952           break;
1953         case TH_STATE_UNINTERRUPTIBLE:
1954           blocked++;
1955           break;
1956         /* There is no `zombie' case here,
1957          * since there are no zombie-threads.
1958          * There's only zombie tasks, which are
1959          * handled above. */
1960         default:
1961           WARNING("Unknown thread status: %i", thread_data.run_state);
1962           break;
1963         } /* switch (thread_data.run_state) */
1964
1965         if (task_list[task] != port_task_self) {
1966           status = mach_port_deallocate(port_task_self, thread_list[thread]);
1967           if (status != KERN_SUCCESS)
1968             ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1969         }
1970       } /* for (thread_list) */
1971
1972       if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1973                                   thread_list_len * sizeof(thread_act_t))) !=
1974           KERN_SUCCESS) {
1975         ERROR("vm_deallocate failed: %s", mach_error_string(status));
1976       }
1977       thread_list = NULL;
1978       thread_list_len = 0;
1979
1980       /* Only deallocate the task port, if it isn't our own.
1981        * Don't know what would happen in that case, but this
1982        * is what Apple's top does.. ;) */
1983       if (task_list[task] != port_task_self) {
1984         status = mach_port_deallocate(port_task_self, task_list[task]);
1985         if (status != KERN_SUCCESS)
1986           ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1987       }
1988
1989       if (ps != NULL)
1990         /* FIXME: cmdline should be here instead of NULL */
1991         ps_list_add(task_name, NULL, &pse);
1992     } /* for (task_list) */
1993
1994     if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1995                                 task_list_len * sizeof(task_t))) !=
1996         KERN_SUCCESS) {
1997       ERROR("vm_deallocate failed: %s", mach_error_string(status));
1998     }
1999     task_list = NULL;
2000     task_list_len = 0;
2001
2002     if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
2003         KERN_SUCCESS) {
2004       ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
2005     }
2006   } /* for (pset_list) */
2007
2008   ps_submit_state("running", running);
2009   ps_submit_state("sleeping", sleeping);
2010   ps_submit_state("zombies", zombies);
2011   ps_submit_state("stopped", stopped);
2012   ps_submit_state("blocked", blocked);
2013
2014   for (ps = list_head_g; ps != NULL; ps = ps->next)
2015     ps_submit_proc_list(ps);
2016     /* #endif HAVE_THREAD_INFO */
2017
2018 #elif KERNEL_LINUX
2019   int running = 0;
2020   int sleeping = 0;
2021   int zombies = 0;
2022   int stopped = 0;
2023   int paging = 0;
2024   int blocked = 0;
2025
2026   struct dirent *ent;
2027   DIR *proc;
2028   long pid;
2029
2030   char cmdline[CMDLINE_BUFFER_SIZE];
2031
2032   int status;
2033   process_entry_t pse;
2034   char state;
2035
2036   running = sleeping = zombies = stopped = paging = blocked = 0;
2037   ps_list_reset();
2038
2039   if ((proc = opendir("/proc")) == NULL) {
2040     ERROR("Cannot open `/proc': %s", STRERRNO);
2041     return -1;
2042   }
2043
2044   while ((ent = readdir(proc)) != NULL) {
2045     if (!isdigit(ent->d_name[0]))
2046       continue;
2047
2048     if ((pid = atol(ent->d_name)) < 1)
2049       continue;
2050
2051     memset(&pse, 0, sizeof(pse));
2052     pse.id = pid;
2053
2054     status = ps_read_process(pid, &pse, &state);
2055     if (status != 0) {
2056       DEBUG("ps_read_process failed: %i", status);
2057       continue;
2058     }
2059
2060     switch (state) {
2061     case 'R':
2062       running++;
2063       break;
2064     case 'S':
2065       sleeping++;
2066       break;
2067     case 'D':
2068       blocked++;
2069       break;
2070     case 'Z':
2071       zombies++;
2072       break;
2073     case 'T':
2074       stopped++;
2075       break;
2076     case 'W':
2077       paging++;
2078       break;
2079     }
2080
2081     ps_list_add(pse.name,
2082                 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2083   }
2084
2085   closedir(proc);
2086
2087   ps_submit_state("running", running);
2088   ps_submit_state("sleeping", sleeping);
2089   ps_submit_state("zombies", zombies);
2090   ps_submit_state("stopped", stopped);
2091   ps_submit_state("paging", paging);
2092   ps_submit_state("blocked", blocked);
2093
2094   for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2095     ps_submit_proc_list(ps_ptr);
2096
2097   read_fork_rate();
2098   /* #endif KERNEL_LINUX */
2099
2100 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
2101   int running = 0;
2102   int sleeping = 0;
2103   int zombies = 0;
2104   int stopped = 0;
2105   int blocked = 0;
2106   int idle = 0;
2107   int wait = 0;
2108
2109   kvm_t *kd;
2110   char errbuf[_POSIX2_LINE_MAX];
2111   struct kinfo_proc *procs; /* array of processes */
2112   struct kinfo_proc *proc_ptr = NULL;
2113   int count; /* returns number of processes */
2114
2115   process_entry_t pse;
2116
2117   ps_list_reset();
2118
2119   /* Open the kvm interface, get a descriptor */
2120   kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
2121   if (kd == NULL) {
2122     ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2123     return 0;
2124   }
2125
2126   /* Get the list of processes. */
2127   procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
2128   if (procs == NULL) {
2129     ERROR("processes plugin: Cannot get kvm processes list: %s",
2130           kvm_geterr(kd));
2131     kvm_close(kd);
2132     return 0;
2133   }
2134
2135   /* Iterate through the processes in kinfo_proc */
2136   for (int i = 0; i < count; i++) {
2137     /* Create only one process list entry per _process_, i.e.
2138      * filter out threads (duplicate PID entries). */
2139     if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
2140       char cmdline[CMDLINE_BUFFER_SIZE] = "";
2141       bool have_cmdline = 0;
2142
2143       proc_ptr = &(procs[i]);
2144       /* Don't probe system processes and processes without arguments */
2145       if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
2146         char **argv;
2147         int argc;
2148         int status;
2149
2150         /* retrieve the arguments */
2151         argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2152         argc = 0;
2153         if ((argv != NULL) && (argv[0] != NULL)) {
2154           while (argv[argc] != NULL)
2155             argc++;
2156
2157           status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2158           if (status < 0)
2159             WARNING("processes plugin: Command line did not fit into buffer.");
2160           else
2161             have_cmdline = 1;
2162         }
2163       } /* if (process has argument list) */
2164
2165       memset(&pse, 0, sizeof(pse));
2166       pse.id = procs[i].ki_pid;
2167
2168       pse.num_proc = 1;
2169       pse.num_lwp = procs[i].ki_numthreads;
2170
2171       pse.vmem_size = procs[i].ki_size;
2172       pse.vmem_rss = procs[i].ki_rssize * pagesize;
2173       pse.vmem_data = procs[i].ki_dsize * pagesize;
2174       pse.vmem_code = procs[i].ki_tsize * pagesize;
2175       pse.stack_size = procs[i].ki_ssize * pagesize;
2176       pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
2177       pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
2178
2179       pse.cpu_user_counter = 0;
2180       pse.cpu_system_counter = 0;
2181       /*
2182        * The u-area might be swapped out, and we can't get
2183        * at it because we have a crashdump and no swap.
2184        * If it's here fill in these fields, otherwise, just
2185        * leave them 0.
2186        */
2187       if (procs[i].ki_flag & P_INMEM) {
2188         pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
2189                                (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2190         pse.cpu_system_counter =
2191             procs[i].ki_rusage.ru_stime.tv_usec +
2192             (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2193       }
2194
2195       /* no I/O data */
2196       pse.io_rchar = -1;
2197       pse.io_wchar = -1;
2198       pse.io_syscr = -1;
2199       pse.io_syscw = -1;
2200       pse.io_diskr = -1;
2201       pse.io_diskw = -1;
2202
2203       /* file descriptor count not implemented */
2204       pse.num_fd = 0;
2205
2206       /* Number of memory mappings */
2207       pse.num_maps = 0;
2208
2209       /* context switch counters not implemented */
2210       pse.cswitch_vol = -1;
2211       pse.cswitch_invol = -1;
2212
2213       ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2214
2215       switch (procs[i].ki_stat) {
2216       case SSTOP:
2217         stopped++;
2218         break;
2219       case SSLEEP:
2220         sleeping++;
2221         break;
2222       case SRUN:
2223         running++;
2224         break;
2225       case SIDL:
2226         idle++;
2227         break;
2228       case SWAIT:
2229         wait++;
2230         break;
2231       case SLOCK:
2232         blocked++;
2233         break;
2234       case SZOMB:
2235         zombies++;
2236         break;
2237       }
2238     } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2239   }
2240
2241   kvm_close(kd);
2242
2243   ps_submit_state("running", running);
2244   ps_submit_state("sleeping", sleeping);
2245   ps_submit_state("zombies", zombies);
2246   ps_submit_state("stopped", stopped);
2247   ps_submit_state("blocked", blocked);
2248   ps_submit_state("idle", idle);
2249   ps_submit_state("wait", wait);
2250
2251   for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2252     ps_submit_proc_list(ps_ptr);
2253     /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2254
2255 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2256   int running = 0;
2257   int sleeping = 0;
2258   int zombies = 0;
2259   int stopped = 0;
2260   int onproc = 0;
2261   int idle = 0;
2262   int dead = 0;
2263
2264   kvm_t *kd;
2265   char errbuf[1024];
2266   struct kinfo_proc *procs; /* array of processes */
2267   struct kinfo_proc *proc_ptr = NULL;
2268   int count; /* returns number of processes */
2269
2270   process_entry_t pse;
2271
2272   ps_list_reset();
2273
2274   /* Open the kvm interface, get a descriptor */
2275   kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2276   if (kd == NULL) {
2277     ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2278     return 0;
2279   }
2280
2281   /* Get the list of processes. */
2282   procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2283   if (procs == NULL) {
2284     ERROR("processes plugin: Cannot get kvm processes list: %s",
2285           kvm_geterr(kd));
2286     kvm_close(kd);
2287     return 0;
2288   }
2289
2290   /* Iterate through the processes in kinfo_proc */
2291   for (int i = 0; i < count; i++) {
2292     /* Create only one process list entry per _process_, i.e.
2293      * filter out threads (duplicate PID entries). */
2294     if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2295       char cmdline[CMDLINE_BUFFER_SIZE] = "";
2296       bool have_cmdline = 0;
2297
2298       proc_ptr = &(procs[i]);
2299       /* Don't probe zombie processes  */
2300       if (!P_ZOMBIE(proc_ptr)) {
2301         char **argv;
2302         int argc;
2303         int status;
2304
2305         /* retrieve the arguments */
2306         argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2307         argc = 0;
2308         if ((argv != NULL) && (argv[0] != NULL)) {
2309           while (argv[argc] != NULL)
2310             argc++;
2311
2312           status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2313           if (status < 0)
2314             WARNING("processes plugin: Command line did not fit into buffer.");
2315           else
2316             have_cmdline = 1;
2317         }
2318       } /* if (process has argument list) */
2319
2320       memset(&pse, 0, sizeof(pse));
2321       pse.id = procs[i].p_pid;
2322
2323       pse.num_proc = 1;
2324       pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2325
2326       pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2327       pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2328       pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2329       pse.stack_size = procs[i].p_vm_ssize * pagesize;
2330       pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2331       pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2332       pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2333
2334       pse.cpu_user_counter =
2335           procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2336       pse.cpu_system_counter =
2337           procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2338
2339       /* no I/O data */
2340       pse.io_rchar = -1;
2341       pse.io_wchar = -1;
2342       pse.io_syscr = -1;
2343       pse.io_syscw = -1;
2344       pse.io_diskr = -1;
2345       pse.io_diskw = -1;
2346
2347       /* file descriptor count not implemented */
2348       pse.num_fd = 0;
2349
2350       /* Number of memory mappings */
2351       pse.num_maps = 0;
2352
2353       /* context switch counters not implemented */
2354       pse.cswitch_vol = -1;
2355       pse.cswitch_invol = -1;
2356
2357       ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2358
2359       switch (procs[i].p_stat) {
2360       case SSTOP:
2361         stopped++;
2362         break;
2363       case SSLEEP:
2364         sleeping++;
2365         break;
2366       case SRUN:
2367         running++;
2368         break;
2369       case SIDL:
2370         idle++;
2371         break;
2372       case SONPROC:
2373         onproc++;
2374         break;
2375       case SDEAD:
2376         dead++;
2377         break;
2378       case SZOMB:
2379         zombies++;
2380         break;
2381       }
2382     } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2383   }
2384
2385   kvm_close(kd);
2386
2387   ps_submit_state("running", running);
2388   ps_submit_state("sleeping", sleeping);
2389   ps_submit_state("zombies", zombies);
2390   ps_submit_state("stopped", stopped);
2391   ps_submit_state("onproc", onproc);
2392   ps_submit_state("idle", idle);
2393   ps_submit_state("dead", dead);
2394
2395   for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2396     ps_submit_proc_list(ps_ptr);
2397     /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2398
2399 #elif HAVE_PROCINFO_H
2400   /* AIX */
2401   int running = 0;
2402   int sleeping = 0;
2403   int zombies = 0;
2404   int stopped = 0;
2405   int paging = 0;
2406   int blocked = 0;
2407
2408   pid_t pindex = 0;
2409   int nprocs;
2410
2411   process_entry_t pse;
2412
2413   ps_list_reset();
2414   while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2415                               /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2416                               &pindex, MAXPROCENTRY)) > 0) {
2417     for (int i = 0; i < nprocs; i++) {
2418       tid64_t thindex;
2419       int nthreads;
2420       char arglist[MAXARGLN + 1];
2421       char *cargs;
2422       char *cmdline;
2423
2424       if (procentry[i].pi_state == SNONE)
2425         continue;
2426       /* if (procentry[i].pi_state == SZOMB)  FIXME */
2427
2428       cmdline = procentry[i].pi_comm;
2429       cargs = procentry[i].pi_comm;
2430       if (procentry[i].pi_flags & SKPROC) {
2431         if (procentry[i].pi_pid == 0)
2432           cmdline = "swapper";
2433         cargs = cmdline;
2434       } else {
2435         if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2436                     MAXARGLN) >= 0) {
2437           int n;
2438
2439           n = -1;
2440           while (++n < MAXARGLN) {
2441             if (arglist[n] == '\0') {
2442               if (arglist[n + 1] == '\0')
2443                 break;
2444               arglist[n] = ' ';
2445             }
2446           }
2447           cargs = arglist;
2448         }
2449       }
2450
2451       memset(&pse, 0, sizeof(pse));
2452
2453       pse.id = procentry[i].pi_pid;
2454       pse.num_lwp = procentry[i].pi_thcount;
2455       pse.num_proc = 1;
2456
2457       thindex = 0;
2458       while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2459                                     sizeof(struct thrdentry64), &thindex,
2460                                     MAXTHRDENTRY)) > 0) {
2461         int j;
2462
2463         for (j = 0; j < nthreads; j++) {
2464           switch (thrdentry[j].ti_state) {
2465           /* case TSNONE: break; */
2466           case TSIDL:
2467             blocked++;
2468             break; /* FIXME is really blocked */
2469           case TSRUN:
2470             running++;
2471             break;
2472           case TSSLEEP:
2473             sleeping++;
2474             break;
2475           case TSSWAP:
2476             paging++;
2477             break;
2478           case TSSTOP:
2479             stopped++;
2480             break;
2481           case TSZOMB:
2482             zombies++;
2483             break;
2484           }
2485         }
2486         if (nthreads < MAXTHRDENTRY)
2487           break;
2488       }
2489
2490       /* tv_usec is nanosec ??? */
2491       pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2492                              procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2493
2494       /* tv_usec is nanosec ??? */
2495       pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2496                                procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2497
2498       pse.vmem_minflt_counter = procentry[i].pi_minflt;
2499       pse.vmem_majflt_counter = procentry[i].pi_majflt;
2500
2501       pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2502       pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2503       /* Not supported/implemented */
2504       pse.vmem_data = 0;
2505       pse.vmem_code = 0;
2506       pse.stack_size = 0;
2507
2508       pse.io_rchar = -1;
2509       pse.io_wchar = -1;
2510       pse.io_syscr = -1;
2511       pse.io_syscw = -1;
2512       pse.io_diskr = -1;
2513       pse.io_diskw = -1;
2514
2515       pse.num_fd = 0;
2516       pse.num_maps = 0;
2517
2518       pse.cswitch_vol = -1;
2519       pse.cswitch_invol = -1;
2520
2521       ps_list_add(cmdline, cargs, &pse);
2522     } /* for (i = 0 .. nprocs) */
2523
2524     if (nprocs < MAXPROCENTRY)
2525       break;
2526   } /* while (getprocs64() > 0) */
2527   ps_submit_state("running", running);
2528   ps_submit_state("sleeping", sleeping);
2529   ps_submit_state("zombies", zombies);
2530   ps_submit_state("stopped", stopped);
2531   ps_submit_state("paging", paging);
2532   ps_submit_state("blocked", blocked);
2533
2534   for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2535     ps_submit_proc_list(ps);
2536     /* #endif HAVE_PROCINFO_H */
2537
2538 #elif KERNEL_SOLARIS
2539   /*
2540    * The Solaris section adds a few more process states and removes some
2541    * process states compared to linux. Most notably there is no "PAGING"
2542    * and "BLOCKED" state for a process.  The rest is similar to the linux
2543    * code.
2544    */
2545   int running = 0;
2546   int sleeping = 0;
2547   int zombies = 0;
2548   int stopped = 0;
2549   int detached = 0;
2550   int daemon = 0;
2551   int system = 0;
2552   int orphan = 0;
2553
2554   struct dirent *ent;
2555   DIR *proc;
2556
2557   int status;
2558   char state;
2559
2560   char cmdline[PRARGSZ];
2561
2562   ps_list_reset();
2563
2564   proc = opendir("/proc");
2565   if (proc == NULL)
2566     return -1;
2567
2568   while ((ent = readdir(proc)) != NULL) {
2569     long pid;
2570     process_entry_t pse;
2571     char *endptr;
2572
2573     if (!isdigit((int)ent->d_name[0]))
2574       continue;
2575
2576     pid = strtol(ent->d_name, &endptr, 10);
2577     if (*endptr != 0) /* value didn't completely parse as a number */
2578       continue;
2579
2580     memset(&pse, 0, sizeof(pse));
2581     pse.id = pid;
2582
2583     status = ps_read_process(pid, &pse, &state);
2584     if (status != 0) {
2585       DEBUG("ps_read_process failed: %i", status);
2586       continue;
2587     }
2588
2589     switch (state) {
2590     case 'R':
2591       running++;
2592       break;
2593     case 'S':
2594       sleeping++;
2595       break;
2596     case 'E':
2597       detached++;
2598       break;
2599     case 'Z':
2600       zombies++;
2601       break;
2602     case 'T':
2603       stopped++;
2604       break;
2605     case 'A':
2606       daemon++;
2607       break;
2608     case 'Y':
2609       system++;
2610       break;
2611     case 'O':
2612       orphan++;
2613       break;
2614     }
2615
2616     ps_list_add(pse.name,
2617                 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2618   } /* while(readdir) */
2619   closedir(proc);
2620
2621   ps_submit_state("running", running);
2622   ps_submit_state("sleeping", sleeping);
2623   ps_submit_state("zombies", zombies);
2624   ps_submit_state("stopped", stopped);
2625   ps_submit_state("detached", detached);
2626   ps_submit_state("daemon", daemon);
2627   ps_submit_state("system", system);
2628   ps_submit_state("orphan", orphan);
2629
2630   for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2631     ps_submit_proc_list(ps_ptr);
2632
2633   read_fork_rate();
2634 #endif /* KERNEL_SOLARIS */
2635
2636   want_init = false;
2637
2638   return 0;
2639 } /* int ps_read */
2640
2641 void module_register(void) {
2642   plugin_register_complex_config("processes", ps_config);
2643   plugin_register_init("processes", ps_init);
2644   plugin_register_read("processes", ps_read);
2645 } /* void module_register */