procps/proc/readproc.c

607 lines
19 KiB
C
Raw Normal View History

2002-02-02 04:17:29 +05:30
/*
* New Interface to Process Table -- PROCTAB Stream (a la Directory streams)
* Copyright (C) 1996 Charles L. Blake.
* Copyright (C) 1998 Michael K. Johnson
* May be distributed under the conditions of the
* GNU Library General Public License; a copy is in COPYING
*/
2002-09-27 19:18:00 +05:30
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "version.h"
#include "readproc.h"
#include "devname.h"
#include "procps.h"
2002-02-02 04:17:29 +05:30
#include <stdio.h>
#include <stdlib.h>
2002-12-03 14:37:59 +05:30
#include <errno.h>
2002-02-02 04:17:29 +05:30
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/dir.h>
#include <sys/types.h>
#include <sys/stat.h>
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
#include <fs_secure.h>
#endif
2002-02-02 04:17:29 +05:30
/* initiate a process table scan
*/
PROCTAB* openproc(int flags, ...) {
va_list ap;
PROCTAB* PT = xmalloc(sizeof(PROCTAB));
2002-10-22 11:42:12 +05:30
if (flags & PROC_PID)
2002-02-02 04:17:29 +05:30
PT->procfs = NULL;
else if (!(PT->procfs = opendir("/proc")))
return NULL;
PT->flags = flags;
va_start(ap, flags); /* Init args list */
2002-10-22 11:42:12 +05:30
if (flags & PROC_PID)
2002-02-02 04:17:29 +05:30
PT->pids = va_arg(ap, pid_t*);
2002-10-22 11:42:12 +05:30
else if (flags & PROC_UID) {
2002-02-02 04:17:29 +05:30
PT->uids = va_arg(ap, uid_t*);
PT->nuid = va_arg(ap, int);
2002-09-30 12:41:30 +05:30
}
2002-02-02 04:17:29 +05:30
va_end(ap); /* Clean up args list */
return PT;
}
/* terminate a process table scan
*/
void closeproc(PROCTAB* PT) {
if (PT){
if (PT->procfs) closedir(PT->procfs);
free(PT);
}
}
/* deallocate the space allocated by readproc if the passed rbuf was NULL
*/
void freeproc(proc_t* p) {
if (!p) /* in case p is NULL */
return;
/* ptrs are after strings to avoid copying memory when building them. */
/* so free is called on the address of the address of strvec[0]. */
if (p->cmdline)
free((void*)*p->cmdline);
if (p->environ)
free((void*)*p->environ);
free(p);
}
2002-12-03 14:37:59 +05:30
static void status2proc(const char *S, proc_t *restrict P){
2002-02-02 04:17:29 +05:30
char* tmp;
2002-12-03 14:48:27 +05:30
unsigned i;
2002-12-03 14:37:59 +05:30
// The cmd is escaped, with \\ and \n for backslash and newline.
// It certainly may contain "VmSize:" and similar crap.
if(unlikely(strncmp("Name:\t",S,6))) fprintf(stderr, "Internal error!\n");
S += 6;
i = 0;
while(i < sizeof P->cmd - 1){
int c = *S++;
if(unlikely(c=='\n')) break;
if(unlikely(c=='\0')) return; // should never happen
if(unlikely(c=='\\')){
c = *S++;
if(c=='\n') break; // should never happen
if(!c) break; // should never happen
if(c=='n') c='\n'; // else we assume it is '\\'
}
P->cmd[i++] = c;
2002-02-02 04:17:29 +05:30
}
2002-12-03 14:37:59 +05:30
P->cmd[i] = '\0';
tmp = strstr (S,"State:\t");
2002-12-04 09:34:19 +05:30
if(likely(tmp)) P->state = tmp[7];
2002-12-03 14:37:59 +05:30
else fprintf(stderr, "Internal error!\n");
2002-02-02 04:17:29 +05:30
tmp = strstr (S,"Pid:");
2002-12-04 09:34:19 +05:30
if(likely(tmp)) sscanf (tmp,
2002-02-02 04:17:29 +05:30
"Pid:\t%d\n"
"PPid:\t%d\n",
&P->pid,
&P->ppid
);
else fprintf(stderr, "Internal error!\n");
tmp = strstr (S,"Uid:");
2002-12-04 09:34:19 +05:30
if(likely(tmp)) sscanf (tmp,
2002-02-02 04:17:29 +05:30
"Uid:\t%d\t%d\t%d\t%d",
&P->ruid, &P->euid, &P->suid, &P->fuid
);
else fprintf(stderr, "Internal error!\n");
tmp = strstr (S,"Gid:");
2002-12-04 09:34:19 +05:30
if(likely(tmp)) sscanf (tmp,
2002-02-02 04:17:29 +05:30
"Gid:\t%d\t%d\t%d\t%d",
&P->rgid, &P->egid, &P->sgid, &P->fgid
);
else fprintf(stderr, "Internal error!\n");
tmp = strstr (S,"VmSize:");
2002-12-04 09:34:19 +05:30
if(likely(tmp)) sscanf (tmp,
2002-02-02 04:17:29 +05:30
"VmSize: %lu kB\n"
"VmLck: %lu kB\n"
"VmRSS: %lu kB\n"
"VmData: %lu kB\n"
"VmStk: %lu kB\n"
"VmExe: %lu kB\n"
"VmLib: %lu kB\n",
&P->vm_size, &P->vm_lock, &P->vm_rss, &P->vm_data,
&P->vm_stack, &P->vm_exe, &P->vm_lib
);
else /* looks like an annoying kernel thread */
{
P->vm_size = 0;
P->vm_lock = 0;
P->vm_rss = 0;
P->vm_data = 0;
P->vm_stack = 0;
P->vm_exe = 0;
P->vm_lib = 0;
}
tmp = strstr (S,"SigPnd:");
2002-12-04 09:34:19 +05:30
if(likely(tmp)) sscanf (tmp,
2002-02-02 04:17:29 +05:30
#ifdef SIGNAL_STRING
"SigPnd: %s SigBlk: %s SigIgn: %s %*s %s",
P->signal, P->blocked, P->sigignore, P->sigcatch
#else
"SigPnd: %Lx SigBlk: %Lx SigIgn: %Lx %*s %Lx",
&P->signal, &P->blocked, &P->sigignore, &P->sigcatch
#endif
);
else fprintf(stderr, "Internal error!\n");
}
/* stat2proc() makes sure it can handle arbitrary executable file basenames
* for `cmd', i.e. those with embedded whitespace or embedded ')'s.
* Such names confuse %s (see scanf(3)), so the string is split and %39c
* is used instead. (except for embedded ')' "(%[^)]c)" would work.
*/
2002-11-25 15:46:33 +05:30
static void stat2proc(const char* S, proc_t *restrict P) {
2002-12-03 14:48:27 +05:30
unsigned num;
2002-12-03 14:37:59 +05:30
char* tmp;
2002-02-02 04:17:29 +05:30
/* fill in default values for older kernels */
P->exit_signal = SIGCHLD;
P->processor = 0;
2002-10-03 15:11:57 +05:30
P->rtprio = -1;
P->sched = -1;
2002-12-03 14:37:59 +05:30
P->pid = strtol(S, &tmp, 10);
S = tmp + 2;
tmp = strrchr(S, ')'); // split into "PID (cmd" and "<rest>"
num = tmp - S;
2002-12-07 14:04:03 +05:30
if(unlikely(num >= sizeof P->cmd)) num = sizeof P->cmd - 1;
2002-12-03 14:37:59 +05:30
memcpy(P->cmd, S, num);
P->cmd[num] = '\0';
S = tmp + 2; // skip ") "
num = sscanf(S,
2002-02-02 04:17:29 +05:30
"%c "
"%d %d %d %d %d "
2002-05-28 09:48:55 +05:30
"%lu %lu %lu %lu %lu "
"%Lu %Lu %Lu %Lu " /* utime stime cutime cstime */
"%ld %ld %ld %ld "
"%Lu " /* start_time */
"%lu "
2002-02-02 04:17:29 +05:30
"%ld "
"%lu %lu %lu %lu %lu %lu "
"%*s %*s %*s %*s " /* discard, no RT signals & Linux 2.1 used hex */
"%lu %lu %lu "
2002-10-03 15:11:57 +05:30
"%d %d "
"%lu %lu",
2002-02-02 04:17:29 +05:30
&P->state,
&P->ppid, &P->pgrp, &P->session, &P->tty, &P->tpgid,
2002-05-28 09:48:55 +05:30
&P->flags, &P->min_flt, &P->cmin_flt, &P->maj_flt, &P->cmaj_flt,
&P->utime, &P->stime, &P->cutime, &P->cstime,
&P->priority, &P->nice, &P->timeout, &P->it_real_value,
&P->start_time,
&P->vsize,
2002-02-02 04:17:29 +05:30
&P->rss,
&P->rss_rlim, &P->start_code, &P->end_code, &P->start_stack, &P->kstk_esp, &P->kstk_eip,
/* P->signal, P->blocked, P->sigignore, P->sigcatch, */ /* can't use */
&P->wchan, &P->nswap, &P->cnswap,
/* -- Linux 2.0.35 ends here -- */
2002-10-03 15:11:57 +05:30
&P->exit_signal, &P->processor, /* 2.2.1 ends with "exit_signal" */
/* -- Linux 2.2.8 to 2.5.17 end here -- */
&P->rtprio, &P->sched /* both added to 2.5.18 */
2002-02-02 04:17:29 +05:30
);
}
2002-11-25 15:46:33 +05:30
static void statm2proc(const char* s, proc_t *restrict P) {
2002-02-02 04:17:29 +05:30
int num;
num = sscanf(s, "%ld %ld %ld %ld %ld %ld %ld",
&P->size, &P->resident, &P->share,
&P->trs, &P->lrs, &P->drs, &P->dt);
/* fprintf(stderr, "statm2proc converted %d fields.\n",num); */
}
2002-10-12 09:55:57 +05:30
static int file2str(const char *directory, const char *what, char *ret, int cap) {
2002-02-02 04:17:29 +05:30
static char filename[80];
int fd, num_read;
sprintf(filename, "%s/%s", directory, what);
2002-12-03 14:37:59 +05:30
fd = open(filename, O_RDONLY, 0);
if(unlikely(fd==-1)) return -1;
num_read = read(fd, ret, cap - 1);
if(unlikely(num_read<=0)) num_read = -1;
2002-02-02 04:17:29 +05:30
else ret[num_read] = 0;
close(fd);
return num_read;
}
2002-10-12 09:55:57 +05:30
static char** file2strvec(const char* directory, const char* what) {
2002-02-02 04:17:29 +05:30
char buf[2048]; /* read buf bytes at a time */
char *p, *rbuf = 0, *endbuf, **q, **ret;
int fd, tot = 0, n, c, end_of_file = 0;
int align;
sprintf(buf, "%s/%s", directory, what);
2002-12-03 14:37:59 +05:30
fd = open(buf, O_RDONLY, 0);
if(fd==-1) return NULL;
2002-02-02 04:17:29 +05:30
/* read whole file into a memory buffer, allocating as we go */
while ((n = read(fd, buf, sizeof buf - 1)) > 0) {
2002-10-14 02:02:09 +05:30
if (n < (int)(sizeof buf - 1))
2002-02-02 04:17:29 +05:30
end_of_file = 1;
if (n == 0 && rbuf == 0)
return NULL; /* process died between our open and read */
if (n < 0) {
if (rbuf)
free(rbuf);
return NULL; /* read error */
}
if (end_of_file && buf[n-1]) /* last read char not null */
buf[n++] = '\0'; /* so append null-terminator */
rbuf = xrealloc(rbuf, tot + n); /* allocate more memory */
memcpy(rbuf + tot, buf, n); /* copy buffer into it */
tot += n; /* increment total byte ctr */
if (end_of_file)
break;
}
close(fd);
if (n <= 0 && !end_of_file) {
if (rbuf) free(rbuf);
return NULL; /* read error */
}
endbuf = rbuf + tot; /* count space for pointers */
align = (sizeof(char*)-1) - ((tot + sizeof(char*)-1) & (sizeof(char*)-1));
for (c = 0, p = rbuf; p < endbuf; p++)
if (!*p)
c += sizeof(char*);
c += sizeof(char*); /* one extra for NULL term */
rbuf = xrealloc(rbuf, tot + c + align); /* make room for ptrs AT END */
endbuf = rbuf + tot; /* addr just past data buf */
q = ret = (char**) (endbuf+align); /* ==> free(*ret) to dealloc */
*q++ = p = rbuf; /* point ptrs to the strings */
endbuf--; /* do not traverse final NUL */
while (++p < endbuf)
if (!*p) /* NUL char implies that */
*q++ = p+1; /* next string -> next char */
*q = 0; /* null ptr list terminator */
return ret;
}
2002-12-03 14:37:59 +05:30
// warning: interface may change
int read_cmdline(char *restrict const dst, unsigned sz, unsigned pid){
char name[32];
int fd;
2002-12-03 14:48:27 +05:30
unsigned n = 0;
dst[0] = '\0';
2002-12-03 14:37:59 +05:30
snprintf(name, sizeof name, "/proc/%u/cmdline", pid);
fd = open(name, O_RDONLY);
2002-12-03 14:48:27 +05:30
if(fd==-1) return 0;
2002-12-03 14:37:59 +05:30
for(;;){
ssize_t r = read(fd,dst+n,sz-n);
if(r==-1){
if(errno==EINTR) continue;
break;
}
n += r;
if(n==sz) break; // filled the buffer
if(r==0) break; // EOF
}
if(n){
int i;
if(n==sz) n--;
dst[n] = '\0';
i=n;
while(i--){
int c = dst[i];
if(c<' ' || c>'~') dst[i]=' ';
}
}
return n;
}
2002-02-02 04:17:29 +05:30
/* These are some nice GNU C expression subscope "inline" functions.
* The can be used with arbitrary types and evaluate their arguments
* exactly once.
*/
/* Test if item X of type T is present in the 0 terminated list L */
# define XinL(T, X, L) ( { \
T x = (X), *l = (L); \
while (*l && *l != x) l++; \
*l == x; \
} )
/* Test if item X of type T is present in the list L of length N */
# define XinLN(T, X, L, N) ( { \
T x = (X), *l = (L); \
int i = 0, n = (N); \
while (i < n && l[i] != x) i++; \
i < n && l[i] == x; \
} )
/* readproc: return a pointer to a proc_t filled with requested info about the
* next process available matching the restriction set. If no more such
* processes are available, return a null pointer (boolean false). Use the
* passed buffer instead of allocating space if it is non-NULL. */
/* This is optimized so that if a PID list is given, only those files are
* searched for in /proc. If other lists are given in addition to the PID list,
* the same logic can follow through as for the no-PID list case. This is
* fairly complex, but it does try to not to do any unnecessary work.
*/
2002-05-28 07:46:24 +05:30
proc_t* readproc(PROCTAB* PT, proc_t* p) {
2002-02-02 04:17:29 +05:30
static struct direct *ent; /* dirent handle */
static struct stat sb; /* stat buffer */
static char path[32], sbuf[1024]; /* bufs for stat,statm */
2002-05-28 07:46:24 +05:30
int matched = 0; /* flags */
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
2002-09-30 12:41:30 +05:30
security_id_t secsid;
2002-09-27 19:18:00 +05:30
#endif
2002-02-02 04:17:29 +05:30
/* loop until a proc matching restrictions is found or no more processes */
/* I know this could be a while loop -- this way is easier to indent ;-) */
next_proc: /* get next PID for consideration */
/*printf("PT->flags is 0x%08x\n", PT->flags);*/
#define flags (PT->flags)
2002-10-22 11:42:12 +05:30
if (flags & PROC_PID) {
2002-02-02 04:17:29 +05:30
if (!*PT->pids) /* set to next item in pids */
return NULL;
sprintf(path, "/proc/%d", *(PT->pids)++);
matched = 1;
} else { /* get next numeric /proc ent */
while ((ent = readdir(PT->procfs)) &&
(*ent->d_name < '0' || *ent->d_name > '9'))
;
if (!ent || !ent->d_name)
return NULL;
sprintf(path, "/proc/%s", ent->d_name);
}
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
2002-09-30 12:41:30 +05:30
if ( stat_secure(path, &sb, &secsid) == -1 ) /* no such dirent (anymore) */
2002-09-27 19:18:00 +05:30
#else
2002-02-02 04:17:29 +05:30
if (stat(path, &sb) == -1) /* no such dirent (anymore) */
2002-09-27 19:18:00 +05:30
#endif
2002-02-02 04:17:29 +05:30
goto next_proc;
2002-09-27 19:18:00 +05:30
2002-10-22 11:42:12 +05:30
if ((flags & PROC_UID) && !XinLN(uid_t, sb.st_uid, PT->uids, PT->nuid))
2002-02-02 04:17:29 +05:30
goto next_proc; /* not one of the requested uids */
2002-05-28 07:46:24 +05:30
if (!p)
p = xcalloc(p, sizeof *p); /* passed buf or alloced mem */
2002-02-02 04:17:29 +05:30
p->euid = sb.st_uid; /* need a way to get real uid */
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
2002-09-30 12:41:30 +05:30
p->secsid = secsid;
2002-09-27 19:18:00 +05:30
#endif
2002-12-09 00:21:56 +05:30
if (flags & PROC_FILLSTAT) { /* read, parse /proc/#/stat */
if ((file2str(path, "stat", sbuf, sizeof sbuf)) == -1)
goto next_proc; /* error reading /proc/#/stat */
stat2proc(sbuf, p); /* parse /proc/#/stat */
}
2002-02-02 04:17:29 +05:30
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLMEM) { /* read, parse /proc/#/statm */
2002-02-02 04:17:29 +05:30
if ((file2str(path, "statm", sbuf, sizeof sbuf)) != -1 )
statm2proc(sbuf, p); /* ignore statm errors here */
} /* statm fields just zero */
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLSTATUS) { /* read, parse /proc/#/status */
2002-02-02 04:17:29 +05:30
if ((file2str(path, "status", sbuf, sizeof sbuf)) != -1 ){
2002-12-03 14:37:59 +05:30
status2proc(sbuf, p);
2002-02-02 04:17:29 +05:30
}
}
/* some number->text resolving which is time consuming */
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLUSR){
2002-02-02 04:17:29 +05:30
strncpy(p->euser, user_from_uid(p->euid), sizeof p->euser);
2002-10-22 11:42:12 +05:30
if(flags & PROC_FILLSTATUS) {
2002-02-02 04:17:29 +05:30
strncpy(p->ruser, user_from_uid(p->ruid), sizeof p->ruser);
strncpy(p->suser, user_from_uid(p->suid), sizeof p->suser);
strncpy(p->fuser, user_from_uid(p->fuid), sizeof p->fuser);
}
}
/* some number->text resolving which is time consuming */
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLGRP){
strncpy(p->egroup, group_from_gid(p->egid), sizeof p->egroup);
2002-10-22 11:42:12 +05:30
if(flags & PROC_FILLSTATUS) {
strncpy(p->rgroup, group_from_gid(p->rgid), sizeof p->rgroup);
strncpy(p->sgroup, group_from_gid(p->sgid), sizeof p->sgroup);
2002-02-02 04:17:29 +05:30
strncpy(p->fgroup, group_from_gid(p->fgid), sizeof p->fgroup);
}
}
2002-10-22 11:42:12 +05:30
if ((flags & PROC_FILLCOM) || (flags & PROC_FILLARG)) /* read+parse /proc/#/cmdline */
2002-02-02 04:17:29 +05:30
p->cmdline = file2strvec(path, "cmdline");
else
p->cmdline = NULL;
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLENV) /* read+parse /proc/#/environ */
2002-02-02 04:17:29 +05:30
p->environ = file2strvec(path, "environ");
else
p->environ = NULL;
2002-02-02 04:17:29 +05:30
2002-10-09 09:58:48 +05:30
if (p->state == 'Z') /* fixup cmd for zombies */
2002-02-02 04:17:29 +05:30
strncat(p->cmd," <defunct>", sizeof p->cmd);
return p;
}
#undef flags
/* ps_readproc: return a pointer to a proc_t filled with requested info about the
* next process available matching the restriction set. If no more such
* processes are available, return a null pointer (boolean false). Use the
* passed buffer instead of allocating space if it is non-NULL. */
/* This is optimized so that if a PID list is given, only those files are
* searched for in /proc. If other lists are given in addition to the PID list,
* the same logic can follow through as for the no-PID list case. This is
* fairly complex, but it does try to not to do any unnecessary work.
*/
2002-05-28 07:46:24 +05:30
proc_t* ps_readproc(PROCTAB* PT, proc_t* p) {
2002-02-02 04:17:29 +05:30
static struct direct *ent; /* dirent handle */
static struct stat sb; /* stat buffer */
static char path[32], sbuf[1024]; /* bufs for stat,statm */
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
2002-09-30 12:41:30 +05:30
security_id_t secsid;
2002-09-27 19:18:00 +05:30
#endif
2002-02-02 04:17:29 +05:30
/* loop until a proc matching restrictions is found or no more processes */
/* I know this could be a while loop -- this way is easier to indent ;-) */
next_proc: /* get next PID for consideration */
/*printf("PT->flags is 0x%08x\n", PT->flags);*/
#define flags (PT->flags)
while ((ent = readdir(PT->procfs)) &&
(*ent->d_name < '0' || *ent->d_name > '9'))
;
if (!ent || !ent->d_name)
return NULL;
sprintf(path, "/proc/%s", ent->d_name);
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
2002-09-30 12:41:30 +05:30
if (stat_secure(path, &sb, &secsid) == -1) /* no such dirent (anymore) */
2002-09-27 19:18:00 +05:30
#else
2002-02-02 04:17:29 +05:30
if (stat(path, &sb) == -1) /* no such dirent (anymore) */
2002-09-27 19:18:00 +05:30
#endif
2002-02-02 04:17:29 +05:30
goto next_proc;
2002-05-28 07:46:24 +05:30
if (!p)
p = xcalloc(p, sizeof *p); /* passed buf or alloced mem */
2002-02-02 04:17:29 +05:30
p->euid = sb.st_uid; /* need a way to get real uid */
2002-09-27 19:18:00 +05:30
#ifdef FLASK_LINUX
2002-09-30 12:41:30 +05:30
p->secsid = secsid;
2002-09-27 19:18:00 +05:30
#endif
2002-02-02 04:17:29 +05:30
if ((file2str(path, "stat", sbuf, sizeof sbuf)) == -1)
goto next_proc; /* error reading /proc/#/stat */
stat2proc(sbuf, p); /* parse /proc/#/stat */
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLMEM) { /* read, parse /proc/#/statm */
2002-02-02 04:17:29 +05:30
if ((file2str(path, "statm", sbuf, sizeof sbuf)) != -1 )
statm2proc(sbuf, p); /* ignore statm errors here */
} /* statm fields just zero */
2002-02-02 04:17:29 +05:30
2002-10-22 11:42:12 +05:30
/* if (flags & PROC_FILLSTATUS) { */ /* read, parse /proc/#/status */
2002-02-02 04:17:29 +05:30
if ((file2str(path, "status", sbuf, sizeof sbuf)) != -1 ){
2002-12-03 14:37:59 +05:30
status2proc(sbuf, p);
2002-02-02 04:17:29 +05:30
}
/* }*/
/* some number->text resolving which is time consuming */
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLUSR){
2002-02-02 04:17:29 +05:30
strncpy(p->euser, user_from_uid(p->euid), sizeof p->euser);
2002-10-22 11:42:12 +05:30
/* if(flags & PROC_FILLSTATUS) { */
2002-02-02 04:17:29 +05:30
strncpy(p->ruser, user_from_uid(p->ruid), sizeof p->ruser);
strncpy(p->suser, user_from_uid(p->suid), sizeof p->suser);
strncpy(p->fuser, user_from_uid(p->fuid), sizeof p->fuser);
/* }*/
}
/* some number->text resolving which is time consuming */
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLGRP){
strncpy(p->egroup, group_from_gid(p->egid), sizeof p->egroup);
2002-10-22 11:42:12 +05:30
/* if(flags & PROC_FILLSTATUS) { */
strncpy(p->rgroup, group_from_gid(p->rgid), sizeof p->rgroup);
strncpy(p->sgroup, group_from_gid(p->sgid), sizeof p->sgroup);
2002-02-02 04:17:29 +05:30
strncpy(p->fgroup, group_from_gid(p->fgid), sizeof p->fgroup);
/* }*/
}
2002-02-02 04:17:29 +05:30
2002-10-22 11:42:12 +05:30
if ((flags & PROC_FILLCOM) || (flags & PROC_FILLARG)) /* read+parse /proc/#/cmdline */
2002-02-02 04:17:29 +05:30
p->cmdline = file2strvec(path, "cmdline");
else
p->cmdline = NULL;
2002-10-22 11:42:12 +05:30
if (flags & PROC_FILLENV) /* read+parse /proc/#/environ */
2002-02-02 04:17:29 +05:30
p->environ = file2strvec(path, "environ");
else
p->environ = NULL;
2002-02-02 04:17:29 +05:30
2002-10-09 09:58:48 +05:30
if (p->state == 'Z') /* fixup cmd for zombies */
2002-02-02 04:17:29 +05:30
strncat(p->cmd," <defunct>", sizeof p->cmd);
return p;
}
#undef flags
void look_up_our_self(proc_t *p) {
static char path[32], sbuf[1024]; /* bufs for stat,statm */
2002-02-02 04:17:29 +05:30
sprintf(path, "/proc/%d", getpid());
file2str(path, "stat", sbuf, sizeof sbuf);
stat2proc(sbuf, p); /* parse /proc/#/stat */
file2str(path, "statm", sbuf, sizeof sbuf);
statm2proc(sbuf, p); /* ignore statm errors here */
file2str(path, "status", sbuf, sizeof sbuf);
2002-12-03 14:37:59 +05:30
status2proc(sbuf, p);
2002-02-02 04:17:29 +05:30
}
/* Convenient wrapper around openproc and readproc to slurp in the whole process
* table subset satisfying the constraints of flags and the optional PID list.
* Free allocated memory with freeproctab(). Access via tab[N]->member. The
* pointer list is NULL terminated.
*/
proc_t** readproctab(int flags, ...) {
PROCTAB* PT = NULL;
proc_t** tab = NULL;
int n = 0;
va_list ap;
va_start(ap, flags); /* pass through args to openproc */
2002-10-22 11:42:12 +05:30
if (flags & PROC_UID) {
2002-02-02 04:17:29 +05:30
/* temporary variables to ensure that va_arg() instances
* are called in the right order
*/
uid_t* u;
int i;
u = va_arg(ap, uid_t*);
i = va_arg(ap, int);
PT = openproc(flags, u, i);
}
2002-10-22 11:42:12 +05:30
else if (flags & PROC_PID)
2002-02-02 04:17:29 +05:30
PT = openproc(flags, va_arg(ap, void*)); /* assume ptr sizes same */
else
PT = openproc(flags);
va_end(ap);
do { /* read table: */
tab = xrealloc(tab, (n+1)*sizeof(proc_t*));/* realloc as we go, using */
tab[n] = readproc(PT, NULL); /* final null to terminate */
} while (tab[n++]); /* stop when NULL reached */
closeproc(PT);
return tab;
}