"Do" cleanup

This commit is contained in:
albert 2002-10-22 06:12:12 +00:00
parent 9cae81a619
commit 9e098634f0
3 changed files with 33 additions and 45 deletions

View File

@ -1746,8 +1746,6 @@ static void do_key(char c)
*####### table entries. #####
*#####################################################################
*/
#define Do(x) (flags & PROC_ ## x)
static proc_t** readproctab2(int flags, proc_t** tab, ...) {
PROCTAB* PT = NULL;
static proc_t *buff;
@ -1756,7 +1754,7 @@ static proc_t** readproctab2(int flags, proc_t** tab, ...) {
va_list ap;
va_start(ap, tab); /* pass through args to openproc */
if (Do(UID)) {
if (flags & PROC_UID) {
/* temporary variables to ensure that va_arg() instances
* are called in the right order
*/
@ -1767,14 +1765,14 @@ static proc_t** readproctab2(int flags, proc_t** tab, ...) {
i = va_arg(ap, int);
PT = openproc(flags, u, i);
}
else if (Do(PID)) {
else if (flags & PROC_PID) {
PT = openproc(flags, va_arg(ap, void*)); /* assume ptr sizes same */
/* work around a bug in openproc() */
PT->procfs = NULL;
/* share some process time, since we skipped opendir("/proc") */
usleep (50*1000);
}
else if (Do(TTY) /*|| Do(STAT) */)
else if (flags & PROC_TTY)
PT = openproc(flags, va_arg(ap, void*)); /* assume ptr sizes same */
else
PT = openproc(flags);

View File

@ -27,25 +27,21 @@
#include <fs_secure.h>
#endif
#define Do(x) (flags & PROC_ ## x) /* convenient shorthand */
/* initiate a process table scan
*/
PROCTAB* openproc(int flags, ...) {
va_list ap;
PROCTAB* PT = xmalloc(sizeof(PROCTAB));
if (Do(PID))
if (flags & PROC_PID)
PT->procfs = NULL;
else if (!(PT->procfs = opendir("/proc")))
return NULL;
PT->flags = flags;
va_start(ap, flags); /* Init args list */
if (Do(PID))
if (flags & PROC_PID)
PT->pids = va_arg(ap, pid_t*);
else if (Do(TTY))
PT->ttys = va_arg(ap, dev_t*);
else if (Do(UID)) {
else if (flags & PROC_UID) {
PT->uids = va_arg(ap, uid_t*);
PT->nuid = va_arg(ap, int);
}
@ -325,7 +321,7 @@ next_proc: /* get next PID for consideration */
/*printf("PT->flags is 0x%08x\n", PT->flags);*/
#define flags (PT->flags)
if (Do(PID)) {
if (flags & PROC_PID) {
if (!*PT->pids) /* set to next item in pids */
return NULL;
sprintf(path, "/proc/%d", *(PT->pids)++);
@ -345,7 +341,7 @@ next_proc: /* get next PID for consideration */
#endif
goto next_proc;
if (Do(UID) && !XinLN(uid_t, sb.st_uid, PT->uids, PT->nuid))
if ((flags & PROC_UID) && !XinLN(uid_t, sb.st_uid, PT->uids, PT->nuid))
goto next_proc; /* not one of the requested uids */
if (!p)
@ -360,24 +356,21 @@ next_proc: /* get next PID for consideration */
goto next_proc; /* error reading /proc/#/stat */
stat2proc(sbuf, p); /* parse /proc/#/stat */
if (!matched && Do(TTY) && !XinL(dev_t, p->tty, PT->ttys))
goto next_proc; /* not one of the requested ttys */
if (Do(FILLMEM)) { /* read, parse /proc/#/statm */
if (flags & PROC_FILLMEM) { /* read, parse /proc/#/statm */
if ((file2str(path, "statm", sbuf, sizeof sbuf)) != -1 )
statm2proc(sbuf, p); /* ignore statm errors here */
} /* statm fields just zero */
if (Do(FILLSTATUS)) { /* read, parse /proc/#/status */
if (flags & PROC_FILLSTATUS) { /* read, parse /proc/#/status */
if ((file2str(path, "status", sbuf, sizeof sbuf)) != -1 ){
status2proc(sbuf, p, 0 /*FIXME*/);
}
}
/* some number->text resolving which is time consuming */
if (Do(FILLUSR)){
if (flags & PROC_FILLUSR){
strncpy(p->euser, user_from_uid(p->euid), sizeof p->euser);
if(Do(FILLSTATUS)) {
if(flags & PROC_FILLSTATUS) {
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);
@ -385,21 +378,21 @@ next_proc: /* get next PID for consideration */
}
/* some number->text resolving which is time consuming */
if (Do(FILLGRP)){
if (flags & PROC_FILLGRP){
strncpy(p->egroup, group_from_gid(p->egid), sizeof p->egroup);
if(Do(FILLSTATUS)) {
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);
strncpy(p->fgroup, group_from_gid(p->fgid), sizeof p->fgroup);
}
}
if (Do(FILLCOM) || Do(FILLARG)) /* read+parse /proc/#/cmdline */
if ((flags & PROC_FILLCOM) || (flags & PROC_FILLARG)) /* read+parse /proc/#/cmdline */
p->cmdline = file2strvec(path, "cmdline");
else
p->cmdline = NULL;
if (Do(FILLENV)) /* read+parse /proc/#/environ */
if (flags & PROC_FILLENV) /* read+parse /proc/#/environ */
p->environ = file2strvec(path, "environ");
else
p->environ = NULL;
@ -463,21 +456,21 @@ next_proc: /* get next PID for consideration */
goto next_proc; /* error reading /proc/#/stat */
stat2proc(sbuf, p); /* parse /proc/#/stat */
if (Do(FILLMEM)) { /* read, parse /proc/#/statm */
if (flags & PROC_FILLMEM) { /* read, parse /proc/#/statm */
if ((file2str(path, "statm", sbuf, sizeof sbuf)) != -1 )
statm2proc(sbuf, p); /* ignore statm errors here */
} /* statm fields just zero */
/* if (Do(FILLSTATUS)) { */ /* read, parse /proc/#/status */
/* if (flags & PROC_FILLSTATUS) { */ /* read, parse /proc/#/status */
if ((file2str(path, "status", sbuf, sizeof sbuf)) != -1 ){
status2proc(sbuf, p, 0 /*FIXME*/);
}
/* }*/
/* some number->text resolving which is time consuming */
if (Do(FILLUSR)){
if (flags & PROC_FILLUSR){
strncpy(p->euser, user_from_uid(p->euid), sizeof p->euser);
/* if(Do(FILLSTATUS)) { */
/* if(flags & PROC_FILLSTATUS) { */
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);
@ -485,21 +478,21 @@ next_proc: /* get next PID for consideration */
}
/* some number->text resolving which is time consuming */
if (Do(FILLGRP)){
if (flags & PROC_FILLGRP){
strncpy(p->egroup, group_from_gid(p->egid), sizeof p->egroup);
/* if(Do(FILLSTATUS)) { */
/* 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);
strncpy(p->fgroup, group_from_gid(p->fgid), sizeof p->fgroup);
/* }*/
}
if (Do(FILLCOM) || Do(FILLARG)) /* read+parse /proc/#/cmdline */
if ((flags & PROC_FILLCOM) || (flags & PROC_FILLARG)) /* read+parse /proc/#/cmdline */
p->cmdline = file2strvec(path, "cmdline");
else
p->cmdline = NULL;
if (Do(FILLENV)) /* read+parse /proc/#/environ */
if (flags & PROC_FILLENV) /* read+parse /proc/#/environ */
p->environ = file2strvec(path, "environ");
else
p->environ = NULL;
@ -536,7 +529,7 @@ proc_t** readproctab(int flags, ...) {
va_list ap;
va_start(ap, flags); /* pass through args to openproc */
if (Do(UID)) {
if (flags & PROC_UID) {
/* temporary variables to ensure that va_arg() instances
* are called in the right order
*/
@ -547,7 +540,7 @@ proc_t** readproctab(int flags, ...) {
i = va_arg(ap, int);
PT = openproc(flags, u, i);
}
else if (Do(PID) || Do(TTY))
else if (flags & PROC_PID)
PT = openproc(flags, va_arg(ap, void*)); /* assume ptr sizes same */
else
PT = openproc(flags);

View File

@ -146,12 +146,10 @@ typedef struct PROCTAB {
DIR* procfs;
int flags;
pid_t* pids; /* pids of the procs */
dev_t* ttys; /* devnos of the cttys */
uid_t* uids; /* uids of procs */
int nuid; /* cannot really sentinel-terminate unsigned short[] */
char* stats; /* status chars (actually output into /proc//stat) */
#ifdef FLASK_LINUX
security_id_t* sids; /* SIDs of the procs */
security_id_t* sids; /* SIDs of the procs */
#endif
} PROCTAB;
@ -192,9 +190,9 @@ extern void freeproc(proc_t* p);
* `flags' (a bitwise-or of PROC_* below) modifies the default behavior. The
* "fill" options will cause more of the proc_t to be filled in. The "filter"
* options all use the second argument as the pointer to a list of objects:
* process status', process id's, user id's, and tty device numbers. The third
* process status', process id's, user id's. The third
* argument is the length of the list (currently only used for lists of user
* id's since unsigned short[] supports no convenient termination sentinel.)
* id's since uid_t supports no convenient termination sentinel.)
*/
#define PROC_FILLMEM 0x0001 /* read statm */
#define PROC_FILLCOM 0x0002 /* alloc and fill in `cmdline' */
@ -204,14 +202,13 @@ extern void freeproc(proc_t* p);
#define PROC_FILLSTATUS 0x0020 /* read status -- currently unconditional */
#define PROC_FILLSTAT 0x0040 /* read stat -- currently unconditional */
#define PROC_FILLWCHAN 0x0080 /* look up WCHAN name */
#define PROC_FILLARG 0x1000 /* alloc and fill in `cmdline' */
#define PROC_FILLARG 0x0100 /* alloc and fill in `cmdline' */
#define PROC_FILLBUG 0xf0ff /* No idea what we need */
#define PROC_FILLBUG 0x0fff /* No idea what we need */
#define PROC_FILLANY 0x0000 /* either stat or status will do */
/* Obsolete, consider only processes with one of the passed: */
#define PROC_PID 0x0100 /* process id numbers ( 0 terminated) */
#define PROC_TTY 0x0200 /* ctty device nos. ( 0 terminated) */
#define PROC_UID 0x0400 /* user id numbers ( length needed ) */
#define PROC_PID 0x1000 /* process id numbers ( 0 terminated) */
#define PROC_UID 0x4000 /* user id numbers ( length needed ) */
#endif