X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fprocesses.c;h=aa47f3328f86355ba020ab2730ce237503f9aa4e;hb=7cea19815ba24735e91dde1c08a889960b299b62;hp=a1aa17c21c0dfe745b2bb9585956055c8cad07f7;hpb=5fb55d066515132942bfb8f7b28fdc42f19a8bf9;p=collectd.git diff --git a/src/processes.c b/src/processes.c index a1aa17c2..aa47f332 100644 --- a/src/processes.c +++ b/src/processes.c @@ -25,7 +25,7 @@ * * Authors: * Lyonel Vincent - * Florian octo Forster + * Florian octo Forster * Oleg King * Sebastian Harl * Andrés J. Díaz @@ -128,8 +128,12 @@ # include #endif -#ifndef ARG_MAX -# define ARG_MAX 4096 +#ifndef CMDLINE_BUFFER_SIZE +# if defined(ARG_MAX) && (ARG_MAX < 4096) +# define CMDLINE_BUFFER_SIZE ARG_MAX +# else +# define CMDLINE_BUFFER_SIZE 4096 +# endif #endif typedef struct procstat_entry_s @@ -223,12 +227,12 @@ static int pagesize; int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count); int getthrds64( pid_t, void *, int, tid64_t *, int ); #endif -int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen); +int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen); #endif /* HAVE_PROCINFO_H */ /* put name of process from config to list_head_g tree - list_head_g is a list of 'procstat_t' structs with - processes names we want to watch */ + * list_head_g is a list of 'procstat_t' structs with + * processes names we want to watch */ static void ps_list_register (const char *name, const char *regexp) { procstat_t *new; @@ -723,7 +727,7 @@ static void ps_submit_proc_list (procstat_t *ps) } DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; " - "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; " + "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; " "vmem_code = %lu; " "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; " "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; " @@ -811,7 +815,7 @@ static procstat_t *ps_read_vmem (int pid, procstat_t *ps) continue; numfields = strsplit (buffer, fields, - STATIC_ARRAY_SIZE (fields)); + STATIC_ARRAY_SIZE (fields)); if (numfields < 2) continue; @@ -1195,25 +1199,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 @@ -1225,10 +1234,8 @@ static int ps_read_process(int pid, procstat_t *ps, char *state) { char filename[64]; char f_psinfo[64], f_usage[64]; - int i; char *buffer; - pstatus_t *myStatus; psinfo_t *myInfo; prusage_t *myUsage; @@ -1683,7 +1690,7 @@ static int ps_read (void) DIR *proc; int pid; - char cmdline[ARG_MAX]; + char cmdline[CMDLINE_BUFFER_SIZE]; int status; procstat_t ps; @@ -1785,9 +1792,9 @@ static int ps_read (void) kvm_t *kd; char errbuf[1024]; - struct kinfo_proc *procs; /* array of processes */ + struct kinfo_proc *procs; /* array of processes */ struct kinfo_proc *proc_ptr = NULL; - int count; /* returns number of processes */ + int count; /* returns number of processes */ int i; procstat_t *ps_ptr; @@ -1821,7 +1828,7 @@ static int ps_read (void) * filter out threads (duplicate PID entries). */ if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) { - char cmdline[ARG_MAX] = ""; + char cmdline[CMDLINE_BUFFER_SIZE] = ""; _Bool have_cmdline = 0; proc_ptr = &(procs[i]); @@ -1958,7 +1965,7 @@ static int ps_read (void) if (procentry[i].pi_pid == 0) cmdline = "swapper"; cargs = cmdline; - } + } else { if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0) @@ -2054,10 +2061,10 @@ static int ps_read (void) #elif KERNEL_SOLARIS /* - * The Solaris section adds a few more process states and removes some - * process states compared to linux. Most notably there is no "PAGING" - * and "BLOCKED" state for a process. The rest is similar to the linux - * code. + * The Solaris section adds a few more process states and removes some + * process states compared to linux. Most notably there is no "PAGING" + * and "BLOCKED" state for a process. The rest is similar to the linux + * code. */ int running = 0; int sleeping = 0; @@ -2067,45 +2074,50 @@ static int ps_read (void) int daemon = 0; int system = 0; int orphan = 0; + struct dirent *ent; DIR *proc; - int pid; int status; - struct procstat ps; - procstat_entry_t pse; procstat_t *ps_ptr; char state; - ps_list_reset(); + char cmdline[PRARGSZ]; + ps_list_reset (); - proc = opendir("/proc"); - if (proc == NULL) { + proc = opendir ("/proc"); + if (proc == NULL) return (-1); - } - while ((ent = readdir(proc)) != NULL) { - if (!isdigit(ent->d_name[0])) + while ((ent = readdir(proc)) != NULL) + { + int pid; + struct procstat ps; + procstat_entry_t pse; + + if (!isdigit ((int) ent->d_name[0])) continue; - if ((pid = atoi(ent->d_name)) < 1) + if ((pid = atoi (ent->d_name)) < 1) continue; - status = ps_read_process(pid, &ps, &state); - if (status != 0) { + status = ps_read_process (pid, &ps, &state); + if (status != 0) + { DEBUG("ps_read_process failed: %i", status); continue; } + pse.id = pid; pse.age = 0; - pse.num_proc = ps.num_proc; - pse.num_lwp = ps.num_lwp; - pse.vmem_size = ps.vmem_size; - pse.vmem_rss = ps.vmem_rss; - pse.vmem_data = ps.vmem_data; - pse.vmem_code = ps.vmem_code; + pse.num_proc = ps.num_proc; + pse.num_lwp = ps.num_lwp; + pse.vmem_size = ps.vmem_size; + pse.vmem_rss = ps.vmem_rss; + pse.vmem_data = ps.vmem_data; + pse.vmem_code = ps.vmem_code; pse.stack_size = ps.stack_size; pse.vmem_minflt = 0; @@ -2123,28 +2135,24 @@ static int ps_read (void) pse.io_syscr = ps.io_syscr; pse.io_syscw = ps.io_syscw; - switch (state) { - case 'R': running++; - break; - case 'S': sleeping++; - break; - case 'E': detached++; - break; - case 'Z': zombies++; - break; - case 'T': stopped++; - break; - case 'A': daemon++; - break; - case 'Y': system++; - break; - case 'O': orphan++; - break; + switch (state) + { + case 'R': running++; break; + case 'S': sleeping++; break; + case 'E': detached++; break; + case 'Z': zombies++; break; + case 'T': stopped++; break; + case 'A': daemon++; break; + case 'Y': system++; break; + case 'O': orphan++; 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);