Add parsestatus()

This commit is contained in:
sin 2013-08-16 16:36:52 +01:00
parent f462025156
commit e33f5bed23
3 changed files with 47 additions and 55 deletions

8
proc.h
View File

@ -27,8 +27,16 @@ struct procstat {
long rsslim;
};
struct procstatus {
uid_t uid;
uid_t euid;
gid_t gid;
gid_t egid;
};
int parsecmdline(pid_t pid, char *buf, size_t siz);
int parsestat(pid_t pid, struct procstat *ps);
int parsestatus(pid_t pid, struct procstatus *pstatus);
int proceuid(pid_t pid, uid_t *euid);
int procuid(pid_t pid, uid_t *euid);
int validps(const char *path);

13
ps.c
View File

@ -64,11 +64,12 @@ usage(void)
static void
psout(struct procstat *ps)
{
struct procstatus pstatus;
char cmdline[BUFSIZ], *cmd;
char stimestr[6];
char *ttystr, *myttystr;
int tty_maj, tty_min;
uid_t myeuid, peuid, puid;
uid_t myeuid;
unsigned sutime;
time_t start;
struct sysinfo info;
@ -91,6 +92,8 @@ psout(struct procstat *ps)
}
}
parsestatus(ps->pid, &pstatus);
/* This is the default case, only print processes that have
* the same controlling terminal as the invoker and the same
* euid as the current user */
@ -107,9 +110,8 @@ psout(struct procstat *ps)
ttystr[0] = '?';
ttystr[1] = '\0';
}
proceuid(ps->pid, &peuid);
myeuid = geteuid();
if (myeuid != peuid) {
if (myeuid != pstatus.euid) {
free(ttystr);
return;
}
@ -122,11 +124,10 @@ psout(struct procstat *ps)
sutime / 3600, (sutime % 3600) / 60, sutime % 60,
ps->comm);
} else {
procuid(ps->pid, &puid);
errno = 0;
pw = getpwuid(puid);
pw = getpwuid(pstatus.uid);
if (errno || !pw)
eprintf("getpwuid %d:", puid);
eprintf("getpwuid %d:", pstatus.uid);
if (sysinfo(&info) < 0)
eprintf("sysinfo:");

View File

@ -10,55 +10,6 @@
#include "../proc.h"
#include "../util.h"
/* TODO: Unify proc{euid,uid}() into parsestatus() */
int
proceuid(pid_t pid, uid_t *euid)
{
FILE *fp;
char buf[BUFSIZ], *p;
char path[PATH_MAX];
uid_t procuid, proceuid;
snprintf(path, sizeof(path), "/proc/%d/status", pid);
if (!(fp = fopen(path, "r")))
eprintf("%s fopen:", path);
while (fgets(buf, sizeof(buf), fp)) {
if (!strncmp(buf, "Uid:", 4)) {
p = buf + strlen("Uid:");
sscanf(p, "%u %u", &procuid, &proceuid);
*euid = proceuid;
fclose(fp);
return 0;
}
}
fclose(fp);
return -1;
}
int
procuid(pid_t pid, uid_t *uid)
{
FILE *fp;
char buf[BUFSIZ], *p;
char path[PATH_MAX];
uid_t procuid;
snprintf(path, sizeof(path), "/proc/%d/status", pid);
if (!(fp = fopen(path, "r")))
eprintf("%s fopen:", path);
while (fgets(buf, sizeof(buf), fp)) {
if (!strncmp(buf, "Uid:", 4)) {
p = buf + strlen("Uid:");
sscanf(p, "%u", &procuid);
*uid = procuid;
fclose(fp);
return 0;
}
}
fclose(fp);
return -1;
}
int
parsecmdline(pid_t pid, char *buf, size_t siz)
{
@ -111,6 +62,38 @@ parsestat(pid_t pid, struct procstat *ps)
return 0;
}
int
parsestatus(pid_t pid, struct procstatus *pstatus)
{
char path[PATH_MAX];
char buf[BUFSIZ], *off;
int fd;
ssize_t n;
snprintf(path, sizeof(path), "/proc/%d/status", pid);
fd = open(path, O_RDONLY);
if (fd < 0)
eprintf("open %s:", path);
n = read(fd, buf, sizeof(buf) - 1);
if (n < 0)
eprintf("%s: read error:", path);
if (!n) {
close(fd);
return -1;
}
buf[n] = '\0';
close(fd);
off = strstr(buf, "Uid:");
if (!off)
return -1;
sscanf(off, "Uid: %u %u", &pstatus->uid, &pstatus->euid);
off = strstr(buf, "Gid:");
if (!off)
return -1;
sscanf(off, "Gid: %u %u", &pstatus->gid, &pstatus->egid);
return 0;
}
int
validps(const char *path)
{