+
+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",
+ (unsigned int) pid);
+
+ errno = 0;
+ fd = open (file, O_RDONLY);
+ if (fd < 0) {
+ char errbuf[4096];
+ /* ENOENT means the process exited while we were handling it.
+ * Don't complain about this, it only fills the logs. */
+ if (errno != ENOENT)
+ 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[1024];
+
+ 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 (...) */
+
+static unsigned long read_fork_rate ()
+{
+ FILE *proc_stat;
+ char buf[1024];
+ unsigned long result = 0;
+ int numfields;
+ char *fields[3];
+
+ proc_stat = fopen("/proc/stat", "r");
+ if (proc_stat == NULL) {
+ char errbuf[1024];
+ ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return ULONG_MAX;
+ }
+
+ while (fgets (buf, sizeof(buf), proc_stat) != NULL)
+ {
+ char *endptr;
+
+ numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
+ if (numfields != 2)
+ continue;
+
+ if (strcmp ("processes", fields[0]) != 0)
+ continue;
+
+ errno = 0;
+ endptr = NULL;
+ result = strtoul(fields[1], &endptr, /* base = */ 10);
+ if ((endptr == fields[1]) || (errno != 0)) {
+ ERROR ("processes plugin: Cannot parse fork rate: %s",
+ fields[1]);
+ result = ULONG_MAX;
+ break;
+ }
+
+ break;
+ }
+
+ fclose(proc_stat);
+
+ return result;
+}
+
+static void ps_submit_fork_rate (unsigned long value)
+{
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
+
+ values[0].derive = (derive_t) value;
+
+ vl.values = values;
+ vl.values_len = 1;
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
+ sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
+
+ plugin_dispatch_values (&vl);
+}
+