2013-08-29 10:11:26 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/types.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2013-08-29 10:11:26 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <libgen.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <limits.h>
|
2013-08-29 10:11:26 +00:00
|
|
|
#include <stdio.h>
|
2013-09-02 13:14:20 +00:00
|
|
|
#include <stdlib.h>
|
2013-08-29 10:11:26 +00:00
|
|
|
#include <string.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-08-29 10:11:26 +00:00
|
|
|
#include "proc.h"
|
2014-06-30 11:58:24 +00:00
|
|
|
#include "queue.h"
|
2013-08-29 10:11:26 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-06-30 11:58:24 +00:00
|
|
|
struct pidentry {
|
2013-09-02 13:14:20 +00:00
|
|
|
pid_t pid;
|
2015-10-07 12:53:41 +00:00
|
|
|
SLIST_ENTRY(pidentry) entry;
|
2014-06-30 11:58:24 +00:00
|
|
|
};
|
|
|
|
|
2015-10-07 12:53:41 +00:00
|
|
|
static SLIST_HEAD(, pidentry) omitpid_head;
|
2013-09-02 13:14:20 +00:00
|
|
|
|
2015-02-06 14:19:33 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
2013-08-29 10:11:26 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *entry;
|
|
|
|
pid_t pid;
|
|
|
|
struct procstat ps;
|
2014-07-05 12:50:18 +00:00
|
|
|
char cmdline[BUFSIZ], *cmd, *cmdbase = NULL, *p, *arg = NULL;
|
2013-08-29 10:11:26 +00:00
|
|
|
int i, found = 0;
|
2013-09-02 13:14:20 +00:00
|
|
|
int sflag = 0, oflag = 0;
|
2015-10-07 12:53:41 +00:00
|
|
|
struct pidentry *pe;
|
2013-08-29 10:11:26 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-08-31 16:33:45 +00:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
2013-09-02 13:14:20 +00:00
|
|
|
case 'o':
|
|
|
|
oflag = 1;
|
|
|
|
arg = EARGF(usage());
|
|
|
|
break;
|
2013-08-29 10:11:26 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (!argc)
|
2014-10-02 22:45:25 +00:00
|
|
|
return 1;
|
2013-08-29 10:11:26 +00:00
|
|
|
|
2015-10-07 12:53:41 +00:00
|
|
|
SLIST_INIT(&omitpid_head);
|
2014-06-30 11:58:24 +00:00
|
|
|
|
2013-09-02 13:14:20 +00:00
|
|
|
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
|
2014-06-30 11:58:24 +00:00
|
|
|
pe = emalloc(sizeof(*pe));
|
2013-09-02 13:14:20 +00:00
|
|
|
if (strcmp(p, "%PPID") == 0)
|
2014-06-30 11:58:24 +00:00
|
|
|
pe->pid = getppid();
|
2013-09-02 13:14:20 +00:00
|
|
|
else
|
2014-06-30 11:58:24 +00:00
|
|
|
pe->pid = estrtol(p, 10);
|
2015-10-07 12:53:41 +00:00
|
|
|
SLIST_INSERT_HEAD(&omitpid_head, pe, entry);
|
2013-09-02 13:14:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 10:11:26 +00:00
|
|
|
if (!(dp = opendir("/proc")))
|
|
|
|
eprintf("opendir /proc:");
|
|
|
|
|
|
|
|
while ((entry = readdir(dp))) {
|
|
|
|
if (!pidfile(entry->d_name))
|
|
|
|
continue;
|
2013-09-02 13:14:20 +00:00
|
|
|
pid = estrtol(entry->d_name, 10);
|
|
|
|
if (oflag) {
|
2015-10-07 12:53:41 +00:00
|
|
|
SLIST_FOREACH(pe, &omitpid_head, entry)
|
2014-06-30 11:58:24 +00:00
|
|
|
if (pe->pid == pid)
|
2013-09-02 13:14:20 +00:00
|
|
|
break;
|
2014-06-30 11:58:24 +00:00
|
|
|
if (pe)
|
2013-09-02 13:14:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-02-13 12:25:29 +00:00
|
|
|
if (parsestat(pid, &ps) < 0)
|
|
|
|
continue;
|
2013-09-02 13:14:20 +00:00
|
|
|
if (parsecmdline(ps.pid, cmdline,
|
|
|
|
sizeof(cmdline)) < 0) {
|
|
|
|
cmd = ps.comm;
|
2014-07-05 12:50:18 +00:00
|
|
|
cmdbase = cmd;
|
2013-09-02 13:14:20 +00:00
|
|
|
} else {
|
|
|
|
if ((p = strchr(cmdline, ' ')))
|
|
|
|
*p = '\0';
|
2014-07-05 12:50:18 +00:00
|
|
|
cmd = cmdline;
|
|
|
|
cmdbase = basename(cmdline);
|
2013-09-02 13:14:20 +00:00
|
|
|
}
|
2013-09-04 12:13:44 +00:00
|
|
|
/* Workaround for login shells */
|
2013-09-02 14:00:59 +00:00
|
|
|
if (cmd[0] == '-')
|
|
|
|
cmd++;
|
2013-08-29 10:11:26 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
2014-07-05 12:50:18 +00:00
|
|
|
if (strcmp(cmd, argv[i]) == 0 ||
|
2014-07-05 15:47:30 +00:00
|
|
|
strcmp(cmdbase, argv[i]) == 0) {
|
2013-08-29 10:11:26 +00:00
|
|
|
putword(entry->d_name);
|
|
|
|
found++;
|
2013-08-31 16:33:45 +00:00
|
|
|
if (sflag)
|
|
|
|
goto out;
|
2013-08-29 10:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 16:33:45 +00:00
|
|
|
out:
|
2013-08-29 10:11:26 +00:00
|
|
|
if (found)
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
closedir(dp);
|
|
|
|
|
2014-10-02 22:45:25 +00:00
|
|
|
return 0;
|
2013-08-29 10:11:26 +00:00
|
|
|
}
|