X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fprocesses.c;h=f4ffef8f67ed49a2ddb459e95cce8ad154aa49f9;hp=8cf2562e8886b04b2dd7aa27ee681cc5dbb6c5b2;hb=ef7fec0c4e0bbbabb356e6a570ac6297ee06eb80;hpb=074b4980bc75bea6826e6a38dcc6e193a721b2a8 diff --git a/src/processes.c b/src/processes.c index 8cf2562e..f4ffef8f 100644 --- a/src/processes.c +++ b/src/processes.c @@ -22,6 +22,7 @@ * Lyonel Vincent * Florian octo Forster * Oleg King + * Sebastian Harl **/ #include "collectd.h" @@ -458,12 +459,18 @@ static int ps_config (const char *key, const char *value) int fields_num; new_val = strdup (value); - if (new_val == NULL) + if (new_val == NULL) { + ERROR ("processes plugin: strdup failed when processing " + "`ProcessMatch %s'.", value); return (1); + } + fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields)); if (fields_num != 2) { + ERROR ("processes plugin: `ProcessMatch' needs exactly " + "two string arguments."); sfree (new_val); return (1); } @@ -472,6 +479,8 @@ static int ps_config (const char *key, const char *value) } else { + ERROR ("processes plugin: The `%s' configuration option is not " + "understood and will be ignored.", key); return (-1); } @@ -746,6 +755,96 @@ int ps_read_process (int pid, procstat_t *ps, char *state) /* 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) { + size_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//cmdline */ + while (n > 0) { + if ('\0' == buf[n]) + buf[n] = ' '; + --n; + } + return buf; +} /* char *ps_get_cmdline (...) */ #endif /* KERNEL_LINUX */ #if HAVE_THREAD_INFO @@ -1061,6 +1160,8 @@ static int ps_read (void) DIR *proc; int pid; + char cmdline[ARG_MAX]; + int status; procstat_t ps; procstat_entry_t pse; @@ -1121,8 +1222,9 @@ static int ps_read (void) case 'W': paging++; break; } - /* FIXME: cmdline should be here instead of NULL */ - ps_list_add (ps.name, NULL, &pse); + ps_list_add (ps.name, + ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)), + &pse); } closedir (proc);