commit e33f5bed237e860dc16381ae95cdd68f7fc341e7
parent f462025156a80bdc508a857cee87a617640d68e9
Author: sin <sin@2f30.org>
Date: Fri, 16 Aug 2013 16:36:52 +0100
Add parsestatus()
Diffstat:
M | proc.h | | | 8 | ++++++++ |
M | ps.c | | | 13 | +++++++------ |
M | util/proc.c | | | 81 | +++++++++++++++++++++++++++++++------------------------------------------------ |
3 files changed, 47 insertions(+), 55 deletions(-)
diff --git a/proc.h b/proc.h
@@ -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);
diff --git a/ps.c b/ps.c
@@ -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:");
diff --git a/util/proc.c b/util/proc.c
@@ -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)
{
@@ -112,6 +63,38 @@ parsestat(pid_t pid, struct procstat *ps)
}
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)
{
char *end;