From 2ddd12fcff38fb94f030f09d1408d5e30fe8ee3a Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 19 Mar 2012 08:43:22 +0100 Subject: [PATCH] processes plugin: Use stack memory when reading the command line. Also remove the call to the non-threadsafe strtok(3) call and fix the size passed to sstrncpy(). --- src/processes.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/processes.c b/src/processes.c index a1aa17c2..a09eb501 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1195,25 +1195,30 @@ static int read_fork_rate () #endif /*KERNEL_LINUX */ #if KERNEL_SOLARIS -static char *ps_get_cmdline(pid_t pid) +static const char *ps_get_cmdline (pid_t pid, /* {{{ */ + char *buffer, size_t buffer_size) { - char f_psinfo[64]; - char cmdline[80]; - char *buffer = NULL; - psinfo_t *myInfo; + char path[PATH_MAX]; + psinfo_t info; + int status; - snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%i/psinfo", pid); + snprintf(path, sizeof (path), "/proc/%i/psinfo", pid); - buffer = (char *)malloc(sizeof (psinfo_t)); - memset(buffer, 0, sizeof(psinfo_t)); - read_file_contents(f_psinfo, buffer, sizeof (psinfo_t)); - myInfo = (psinfo_t *) buffer; + status = read_file_contents (path, (void *) &info, sizeof (info)); + if (status != ((int) buffer_size)) + { + ERROR ("processes plugin: Unexpected return value " + "while reading \"%s\": " + "Returned %i but expected %zu.", + path, status, buffer_size); + return (NULL); + } - sstrncpy(cmdline, myInfo->pr_psargs, sizeof (myInfo->pr_psargs)); + info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0; + sstrncpy (buffer, info.pr_psargs, buffer_size); - sfree(myInfo); - return strtok(cmdline, " "); -} + return (buffer); +} /* }}} int ps_get_cmdline */ /* * Reads process information on the Solaris OS. The information comes mainly from @@ -2077,8 +2082,9 @@ static int ps_read (void) procstat_t *ps_ptr; char state; - ps_list_reset(); + char cmdline[ARG_MAX]; + ps_list_reset (); proc = opendir("/proc"); if (proc == NULL) { @@ -2142,9 +2148,12 @@ static int ps_read (void) break; } - ps_list_add(ps.name, ps_get_cmdline(pid), &pse); - } // while() + ps_list_add (ps.name, + ps_get_cmdline ((pid_t) pid, + cmdline, sizeof (cmdline)), + &pse); + } /* while(readdir) */ closedir (proc); ps_submit_state ("running", running); -- 2.30.2