2013-08-29 06:11:26 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/types.h>
|
2013-09-02 09:14:20 -04:00
|
|
|
#include <unistd.h>
|
2013-08-29 06:11:26 -04:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdio.h>
|
2013-09-02 09:14:20 -04:00
|
|
|
#include <stdlib.h>
|
2013-08-29 06:11:26 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include "proc.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-09-02 09:56:35 -04:00
|
|
|
eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0);
|
2013-08-29 06:11:26 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 09:14:20 -04:00
|
|
|
static struct omit {
|
|
|
|
pid_t pid;
|
|
|
|
struct omit *next;
|
|
|
|
} *omithead;
|
|
|
|
|
2013-08-29 06:11:26 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *entry;
|
|
|
|
pid_t pid;
|
|
|
|
struct procstat ps;
|
2013-09-02 09:58:40 -04:00
|
|
|
char cmdline[BUFSIZ], *cmd, *p, *arg = NULL;
|
2013-08-29 06:11:26 -04:00
|
|
|
int i, found = 0;
|
2013-09-02 09:14:20 -04:00
|
|
|
int sflag = 0, oflag = 0;
|
|
|
|
struct omit *onode, *tmp;
|
2013-08-29 06:11:26 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-08-31 12:33:45 -04:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
2013-09-02 09:14:20 -04:00
|
|
|
case 'o':
|
|
|
|
oflag = 1;
|
|
|
|
arg = EARGF(usage());
|
|
|
|
break;
|
2013-08-29 06:11:26 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (!argc)
|
|
|
|
return 1;
|
|
|
|
|
2013-09-02 09:14:20 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-29 06:11:26 -04:00
|
|
|
if (!(dp = opendir("/proc")))
|
|
|
|
eprintf("opendir /proc:");
|
|
|
|
|
|
|
|
while ((entry = readdir(dp))) {
|
|
|
|
if (!pidfile(entry->d_name))
|
|
|
|
continue;
|
2013-09-02 09:14:20 -04:00
|
|
|
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);
|
|
|
|
}
|
2013-09-02 10:00:59 -04:00
|
|
|
if (cmd[0] == '-')
|
|
|
|
cmd++;
|
2013-08-29 06:11:26 -04:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (strcmp(cmd, argv[i]) == 0) {
|
|
|
|
putword(entry->d_name);
|
|
|
|
found++;
|
2013-08-31 12:33:45 -04:00
|
|
|
if (sflag)
|
|
|
|
goto out;
|
2013-08-29 06:11:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:33:45 -04:00
|
|
|
out:
|
2013-08-29 06:11:26 -04:00
|
|
|
if (found)
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
closedir(dp);
|
|
|
|
|
2013-09-02 09:14:20 -04:00
|
|
|
onode = omithead;
|
|
|
|
while (onode) {
|
|
|
|
tmp = onode->next;
|
|
|
|
free(onode);
|
|
|
|
onode = tmp;
|
|
|
|
}
|
|
|
|
|
2013-08-29 06:11:26 -04:00
|
|
|
return 0;
|
|
|
|
}
|