pidof: new option to show cmdline-less PIDs (kernel workers), empty input fixed

This commit is contained in:
Jan Rybar 2020-12-22 00:14:41 +00:00 committed by Craig Small
parent 518547742b
commit b568f9f2b7
2 changed files with 15 additions and 2 deletions

View File

@ -45,6 +45,9 @@ the current root directory of processes they do not own.
.IP \-x .IP \-x
Scripts too - this causes the program to also return process id's of Scripts too - this causes the program to also return process id's of
shells running the named scripts. shells running the named scripts.
.IP \-w
Show also processes that do not have visible command line (e.g. kernel
worker threads).
.IP "-o \fIomitpid\fP" .IP "-o \fIomitpid\fP"
Tells \fIpidof\fP to omit processes with that process id. The special Tells \fIpidof\fP to omit processes with that process id. The special
pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP

14
pidof.c
View File

@ -57,6 +57,8 @@ static char *program = NULL;
static int opt_single_shot = 0; /* -s */ static int opt_single_shot = 0; /* -s */
static int opt_scripts_too = 0; /* -x */ static int opt_scripts_too = 0; /* -x */
static int opt_rootdir_check = 0; /* -c */ static int opt_rootdir_check = 0; /* -c */
static int opt_with_workers = 0; /* -w */
static char *pidof_root = NULL; static char *pidof_root = NULL;
@ -71,6 +73,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
fputs(_(" -s, --single-shot return one PID only\n"), fp); fputs(_(" -s, --single-shot return one PID only\n"), fp);
fputs(_(" -c, --check-root omit processes with different root\n"), fp); fputs(_(" -c, --check-root omit processes with different root\n"), fp);
fputs(_(" -x also find shells running the named scripts\n"), fp); fputs(_(" -x also find shells running the named scripts\n"), fp);
fputs(_(" -w, --with-workers show kernel workers too\n"), fp);
fputs(_(" -o, --omit-pid <PID,...> omit processes with PID\n"), fp); fputs(_(" -o, --omit-pid <PID,...> omit processes with PID\n"), fp);
fputs(_(" -S, --separator SEP use SEP as separator put between PIDs"), fp); fputs(_(" -S, --separator SEP use SEP as separator put between PIDs"), fp);
fputs(USAGE_SEPARATOR, fp); fputs(USAGE_SEPARATOR, fp);
@ -173,7 +176,7 @@ static void select_procs (void)
} }
} }
if (!is_omitted(tid)) { if (!is_omitted(tid) && ((p_cmdline && *p_cmdline) || opt_with_workers)) {
cmd_arg0 = (p_cmdline && *p_cmdline) ? *p_cmdline : "\0"; cmd_arg0 = (p_cmdline && *p_cmdline) ? *p_cmdline : "\0";
@ -196,6 +199,7 @@ static void select_procs (void)
if (!strcmp(program, cmd_arg0base) || if (!strcmp(program, cmd_arg0base) ||
!strcmp(program_base, cmd_arg0) || !strcmp(program_base, cmd_arg0) ||
!strcmp(program, cmd_arg0) || !strcmp(program, cmd_arg0) ||
(opt_with_workers && !strcmp(program, p_cmd)) ||
!strcmp(program, exe_link_base) || !strcmp(program, exe_link_base) ||
!strcmp(program, exe_link)) !strcmp(program, exe_link))
{ {
@ -295,13 +299,14 @@ int main (int argc, char **argv)
int first_pid = 1; int first_pid = 1;
const char *separator = " "; const char *separator = " ";
const char *opts = "scdnxmo:S:?Vh"; const char *opts = "scnxwmo:S:?Vh";
static const struct option longopts[] = { static const struct option longopts[] = {
{"check-root", no_argument, NULL, 'c'}, {"check-root", no_argument, NULL, 'c'},
{"single-shot", no_argument, NULL, 's'}, {"single-shot", no_argument, NULL, 's'},
{"omit-pid", required_argument, NULL, 'o'}, {"omit-pid", required_argument, NULL, 'o'},
{"separator", required_argument, NULL, 'S'}, {"separator", required_argument, NULL, 'S'},
{"with-workers", no_argument, NULL, 'w'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'}, {"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
@ -327,6 +332,9 @@ int main (int argc, char **argv)
case 'x': case 'x':
opt_scripts_too = 1; opt_scripts_too = 1;
break; break;
case 'w':
opt_with_workers = 1;
break;
case 'c': case 'c':
if (geteuid() == 0) { if (geteuid() == 0) {
opt_rootdir_check = 1; opt_rootdir_check = 1;
@ -361,6 +369,8 @@ int main (int argc, char **argv)
program = argv[optind++]; program = argv[optind++];
if (*program == '\0') continue;
select_procs(); /* get the list of matching processes */ select_procs(); /* get the list of matching processes */
if (proc_count) { if (proc_count) {