+ return ((count >= 1) ? count : 1);
+} /* int *ps_read_tasks */
+
+int ps_read_process (int pid, procstat_t *ps, char *state)
+{
+ char filename[64];
+ char buffer[1024];
+
+ char *fields[64];
+ char fields_len;
+
+ int i;
+
+ int ppid;
+ int name_len;
+
+ long long unsigned cpu_user_counter;
+ long long unsigned cpu_system_counter;
+ long long unsigned vmem_size;
+ long long unsigned vmem_rss;
+ long long unsigned stack_size;
+
+ memset (ps, 0, sizeof (procstat_t));
+
+ ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
+
+ i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
+ if (i <= 0)
+ return (-1);
+ buffer[i] = 0;
+
+ 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 ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
+ {
+ /* returns -1 => kernel 2.4 */
+ ps->num_lwp = 1;
+ }
+ ps->num_proc = 1;
+ }
+
+ /* 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_size = atoll (fields[22]);
+ vmem_rss = atoll (fields[23]);
+ ps->vmem_minflt_counter = atol (fields[9]);
+ ps->vmem_majflt_counter = atol (fields[11]);
+
+ {
+ unsigned long long stack_start = atoll (fields[27]);
+ unsigned long long stack_ptr = atoll (fields[28]);
+
+ stack_size = (stack_start > stack_ptr)
+ ? stack_start - stack_ptr
+ : stack_ptr - stack_start;
+ }
+
+ /* 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_size = (unsigned long) vmem_size;
+ ps->vmem_rss = (unsigned long) vmem_rss;
+ ps->stack_size = (unsigned long) stack_size;
+
+ /* success */
+ return (0);
+} /* int ps_read_process (...) */
+
+static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
+{
+ char *buf_ptr;
+ size_t len;
+
+ char file[PATH_MAX];
+ int fd;
+
+ size_t n;
+
+ if ((pid < 1) || (NULL == buf) || (buf_len < 2))
+ return NULL;
+
+ ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
+
+ fd = open (file, O_RDONLY);
+ if (fd < 0) {
+ char errbuf[4096];
+ WARNING ("processes plugin: Failed to open `%s': %s.", file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return NULL;
+ }
+
+ buf_ptr = buf;
+ len = buf_len;
+
+ n = 0;
+
+ while (42) {
+ ssize_t status;
+
+ status = read (fd, (void *)buf_ptr, len);
+
+ if (status < 0) {
+ char errbuf[4096];
+
+ if ((EAGAIN == errno) || (EINTR == errno))
+ continue;
+
+ WARNING ("processes plugin: Failed to read from `%s': %s.", file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ close (fd);
+ return NULL;
+ }
+
+ n += status;
+
+ if (status == 0)
+ break;
+
+ buf_ptr += status;
+ len -= status;
+
+ if (len <= 0)
+ break;
+ }
+
+ close (fd);
+
+ if (0 == n) {
+ /* cmdline not available; e.g. kernel thread, zombie */
+ if (NULL == name)
+ return NULL;
+
+ ssnprintf (buf, buf_len, "[%s]", name);
+ return buf;
+ }
+
+ assert (n <= buf_len);
+
+ if (n == buf_len)
+ --n;
+ buf[n] = '\0';
+
+ --n;
+ /* remove trailing whitespace */
+ while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
+ buf[n] = '\0';
+ --n;
+ }
+
+ /* arguments are separated by '\0' in /proc/<pid>/cmdline */
+ while (n > 0) {
+ if ('\0' == buf[n])
+ buf[n] = ' ';
+ --n;
+ }
+ return buf;
+} /* char *ps_get_cmdline (...) */
+#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);
+
+ if (name_max_len > (MAXCOMLEN + 1))
+ name_max_len = MAXCOMLEN + 1;
+
+ strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
+ name[name_max_len - 1] = '\0';
+
+ DEBUG ("pid = %i; name = %s;", *pid, name);
+
+ /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
+ * `top' does it, because it is a lot of work and only used when
+ * debugging. -octo */
+
+ return (0);