Implement -o support for pidof(8)

This commit is contained in:
sin 2013-09-02 16:14:20 +03:00
parent 1003ebad26
commit 755a96237c

64
pidof.c
View File

@ -1,8 +1,10 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <libgen.h> #include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "proc.h" #include "proc.h"
#include "util.h" #include "util.h"
@ -10,9 +12,14 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-s] [program...]\n", argv0); eprintf("usage: %s [-os] [program...]\n", argv0);
} }
static struct omit {
pid_t pid;
struct omit *next;
} *omithead;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -20,14 +27,19 @@ main(int argc, char *argv[])
struct dirent *entry; struct dirent *entry;
pid_t pid; pid_t pid;
struct procstat ps; struct procstat ps;
char cmdline[BUFSIZ], *cmd, *p; char cmdline[BUFSIZ], *cmd, *p, *arg;
int i, found = 0; int i, found = 0;
int sflag = 0; int sflag = 0, oflag = 0;
struct omit *onode, *tmp;
ARGBEGIN { ARGBEGIN {
case 's': case 's':
sflag = 1; sflag = 1;
break; break;
case 'o':
oflag = 1;
arg = EARGF(usage());
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -35,23 +47,42 @@ main(int argc, char *argv[])
if (!argc) if (!argc)
return 1; return 1;
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
onode = malloc(sizeof(*onode));
if (!onode)
eprintf("malloc:");
if (strcmp(p, "%PPID") == 0)
onode->pid = getppid();
else
onode->pid = estrtol(p, 10);
onode->next = omithead;
omithead = onode;
}
if (!(dp = opendir("/proc"))) if (!(dp = opendir("/proc")))
eprintf("opendir /proc:"); eprintf("opendir /proc:");
while ((entry = readdir(dp))) { while ((entry = readdir(dp))) {
if (!pidfile(entry->d_name)) if (!pidfile(entry->d_name))
continue; continue;
pid = estrtol(entry->d_name, 10);
if (oflag) {
for (onode = omithead; onode; onode = onode->next)
if (onode->pid == pid)
break;
if (onode)
continue;
}
parsestat(pid, &ps);
if (parsecmdline(ps.pid, cmdline,
sizeof(cmdline)) < 0) {
cmd = ps.comm;
} else {
if ((p = strchr(cmdline, ' ')))
*p = '\0';
cmd = basename(cmdline);
}
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
pid = estrtol(entry->d_name, 10);
parsestat(pid, &ps);
if (parsecmdline(ps.pid, cmdline,
sizeof(cmdline)) < 0) {
cmd = ps.comm;
} else {
if ((p = strchr(cmdline, ' ')))
*p = '\0';
cmd = basename(cmdline);
}
if (strcmp(cmd, argv[i]) == 0) { if (strcmp(cmd, argv[i]) == 0) {
putword(entry->d_name); putword(entry->d_name);
found++; found++;
@ -67,5 +98,12 @@ out:
closedir(dp); closedir(dp);
onode = omithead;
while (onode) {
tmp = onode->next;
free(onode);
onode = tmp;
}
return 0; return 0;
} }