ubase/pidof.c

121 lines
2.1 KiB
C
Raw Normal View History

2013-08-29 06:11:26 -04:00
/* See LICENSE file for copyright and license details. */
#include <sys/types.h>
2014-06-30 14:03:41 -04:00
2013-08-29 06:11:26 -04:00
#include <dirent.h>
#include <libgen.h>
2014-06-30 14:03:41 -04:00
#include <limits.h>
2013-08-29 06:11:26 -04:00
#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>
2014-06-30 14:03:41 -04:00
#include <unistd.h>
2013-08-29 06:11:26 -04:00
#include "proc.h"
#include "queue.h"
2013-08-29 06:11:26 -04:00
#include "util.h"
struct pidentry {
2013-09-02 09:14:20 -04:00
pid_t pid;
TAILQ_ENTRY(pidentry) entry;
};
static TAILQ_HEAD(omitpid_head, pidentry) omitpid_head;
2013-09-02 09:14:20 -04:00
static void
usage(void)
{
eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0);
}
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;
char cmdline[BUFSIZ], *cmd, *cmdbase = NULL, *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 pidentry *pe, *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)
2014-10-02 18:45:25 -04:00
return 1;
2013-08-29 06:11:26 -04:00
TAILQ_INIT(&omitpid_head);
2013-09-02 09:14:20 -04:00
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
pe = emalloc(sizeof(*pe));
2013-09-02 09:14:20 -04:00
if (strcmp(p, "%PPID") == 0)
pe->pid = getppid();
2013-09-02 09:14:20 -04:00
else
pe->pid = estrtol(p, 10);
TAILQ_INSERT_TAIL(&omitpid_head, pe, entry);
2013-09-02 09:14:20 -04:00
}
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) {
TAILQ_FOREACH(pe, &omitpid_head, entry)
if (pe->pid == pid)
2013-09-02 09:14:20 -04:00
break;
if (pe)
2013-09-02 09:14:20 -04:00
continue;
}
if (parsestat(pid, &ps) < 0)
continue;
2013-09-02 09:14:20 -04:00
if (parsecmdline(ps.pid, cmdline,
sizeof(cmdline)) < 0) {
cmd = ps.comm;
cmdbase = cmd;
2013-09-02 09:14:20 -04:00
} else {
if ((p = strchr(cmdline, ' ')))
*p = '\0';
cmd = cmdline;
cmdbase = basename(cmdline);
2013-09-02 09:14:20 -04:00
}
/* Workaround for login shells */
if (cmd[0] == '-')
cmd++;
2013-08-29 06:11:26 -04:00
for (i = 0; i < argc; i++) {
if (strcmp(cmd, argv[i]) == 0 ||
2014-07-05 11:47:30 -04:00
strcmp(cmdbase, argv[i]) == 0) {
2013-08-29 06:11:26 -04:00
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);
for (pe = TAILQ_FIRST(&omitpid_head); pe; pe = tmp) {
tmp = TAILQ_NEXT(pe, entry);
TAILQ_REMOVE(&omitpid_head, pe, entry);
free(pe);
2013-09-02 09:14:20 -04:00
}
2014-10-02 18:45:25 -04:00
return 0;
2013-08-29 06:11:26 -04:00
}