+ char dirname[64];
+ DIR *dh;
+ struct dirent *ent;
+
+ snprintf (dirname, 64, "/proc/%i/task", pid);
+ dirname[63] = '\0';
+
+ if ((dh = opendir (dirname)) == NULL)
+ {
+ DEBUG ("Failed to open directory `%s'", dirname);
+ return (NULL);
+ }
+
+ while ((ent = readdir (dh)) != NULL)
+ {
+ if (!isdigit (ent->d_name[0]))
+ continue;
+
+ if ((list_len + 1) >= list_size)
+ {
+ int *new_ptr;
+ int new_size = 2 * list_size;
+ /* Comes in sizes: 2, 4, 8, 16, ... */
+
+ new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
+ if (new_ptr == NULL)
+ {
+ if (list != NULL)
+ free (list);
+ ERROR ("processes plugin: "
+ "Failed to allocate more memory.");
+ return (NULL);
+ }
+
+ list = new_ptr;
+ list_size = new_size;
+
+ memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
+ }
+
+ list[list_len] = atoi (ent->d_name);
+ if (list[list_len] != 0)
+ list_len++;
+ }
+
+ closedir (dh);
+
+ if (list_len == 0)
+ return (NULL);
+
+ assert (list_len < list_size);
+ assert (list[list_len] == 0);
+
+ return (list);
+} /* int *ps_read_tasks */
+
+int ps_read_process (int pid, procstat_t *ps, char *state)
+{
+ char filename[64];
+ char buffer[1024];
+ FILE *fh;
+
+ char *fields[64];
+ char fields_len;
+
+ int *tasks;
+ int i;
+
+ int ppid;
+ int name_len;
+
+ long long unsigned cpu_user_counter;
+ long long unsigned cpu_system_counter;
+ long long unsigned vmem_rss;
+
+ memset (ps, 0, sizeof (procstat_t));
+
+ snprintf (filename, 64, "/proc/%i/stat", pid);
+ filename[63] = '\0';
+
+ if ((fh = fopen (filename, "r")) == NULL)
+ return (-1);
+
+ if (fgets (buffer, 1024, fh) == NULL)
+ {
+ fclose (fh);
+ return (-1);
+ }
+
+ fclose (fh);
+
+ fields_len = strsplit (buffer, fields, 64);
+ if (fields_len < 24)
+ {
+ DEBUG ("processes plugin: ps_read_process (pid = %i):"
+ " `%s' has only %i fields..",
+ (int) pid, filename, fields_len);
+ return (-1);
+ }
+
+ /* copy the name, strip brackets in the process */
+ name_len = strlen (fields[1]) - 2;
+ if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
+ {
+ DEBUG ("No brackets found in process name: `%s'", fields[1]);
+ return (-1);
+ }
+ fields[1] = fields[1] + 1;
+ fields[1][name_len] = '\0';
+ strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
+
+ ppid = atoi (fields[3]);
+
+ *state = fields[2][0];
+
+ if (*state == 'Z')
+ {
+ ps->num_lwp = 0;
+ ps->num_proc = 0;
+ }
+ else if ((tasks = ps_read_tasks (pid)) == NULL)
+ {
+ /* Kernel 2.4 or so */
+ ps->num_lwp = 1;
+ ps->num_proc = 1;
+ }
+ else
+ {
+ ps->num_lwp = 0;
+ ps->num_proc = 1;
+ for (i = 0; tasks[i] != 0; i++)
+ ps->num_lwp++;
+
+ free (tasks);
+ tasks = NULL;
+ }
+
+ /* Leave the rest at zero if this is only a zombi */
+ if (ps->num_proc == 0)
+ {
+ DEBUG ("processes plugin: This is only a zombi: pid = %i; "
+ "name = %s;", pid, ps->name);
+ return (0);
+ }
+
+ cpu_user_counter = atoll (fields[13]);
+ cpu_system_counter = atoll (fields[14]);
+ vmem_rss = atoll (fields[23]);
+ ps->vmem_minflt_counter = atol (fields[9]);
+ ps->vmem_majflt_counter = atol (fields[11]);
+
+ /* Convert jiffies to useconds */
+ cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
+ cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
+ vmem_rss = vmem_rss * pagesize_g;
+
+ ps->cpu_user_counter = (unsigned long) cpu_user_counter;
+ ps->cpu_system_counter = (unsigned long) cpu_system_counter;
+ ps->vmem_rss = (unsigned long) vmem_rss;
+
+ /* success */
+ return (0);
+} /* int ps_read_process (...) */
+#endif /* KERNEL_LINUX */
+
+#if HAVE_THREAD_INFO
+static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
+{
+ int mib[4];
+
+ struct kinfo_proc kp;
+ size_t kp_size;
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = KERN_PROC_PID;
+
+ if (pid_for_task (t, pid) != KERN_SUCCESS)
+ return (-1);
+ mib[3] = *pid;
+
+ kp_size = sizeof (kp);
+ if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
+ return (-1);