lzaoral / rpms / procps-ng

Forked from rpms/procps-ng 11 months ago
Clone
53ec625
diff --git a/pidof.1 b/pidof.1
53ec625
index 8ef4abf..5f95b85 100644
53ec625
--- a/pidof.1
53ec625
+++ b/pidof.1
53ec625
@@ -45,6 +45,9 @@ the current root directory of processes they do not own.
53ec625
 .IP \-x
53ec625
 Scripts too - this causes the program to also return process id's of
53ec625
 shells running the named scripts.
53ec625
+.IP \-w
53ec625
+Show also processes that do not have visible command line (e.g. kernel
53ec625
+worker threads).
53ec625
 .IP "-o \fIomitpid\fP"
53ec625
 Tells \fIpidof\fP to omit processes with that process id. The special
53ec625
 pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP
53ec625
diff --git a/pidof.c b/pidof.c
53ec625
index 90ecb13..0754754 100644
53ec625
--- a/pidof.c
53ec625
+++ b/pidof.c
53ec625
@@ -55,6 +55,8 @@ static char *program = NULL;
53ec625
 static int opt_single_shot    = 0;  /* -s */
53ec625
 static int opt_scripts_too    = 0;  /* -x */
53ec625
 static int opt_rootdir_check  = 0;  /* -c */
53ec625
+static int opt_with_workers   = 0;  /* -w */
53ec625
+
53ec625
 
53ec625
 static char *pidof_root = NULL;
53ec625
 
53ec625
@@ -69,6 +71,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
53ec625
 	fputs(_(" -s, --single-shot         return one PID only\n"), fp);
53ec625
 	fputs(_(" -c, --check-root          omit processes with different root\n"), fp);
53ec625
 	fputs(_(" -x                        also find shells running the named scripts\n"), fp);
53ec625
+	fputs(_(" -w, --with-workers        show kernel workers too\n"), fp);
53ec625
 	fputs(_(" -o, --omit-pid <PID,...>  omit processes with PID\n"), fp);
53ec625
 	fputs(_(" -S, --separator SEP       use SEP as separator put between PIDs"), fp);
53ec625
 	fputs(USAGE_SEPARATOR, fp);
53ec625
@@ -142,7 +145,6 @@ static void select_procs (void)
53ec625
 	static int size = 0;
53ec625
 	char *cmd_arg0, *cmd_arg0base;
53ec625
 	char *cmd_arg1, *cmd_arg1base;
53ec625
-	char *stat_cmd;
53ec625
 	char *program_base;
53ec625
 	char *root_link;
53ec625
 	char *exe_link;
53ec625
@@ -168,10 +170,9 @@ static void select_procs (void)
53ec625
 			}
53ec625
 		}
53ec625
 
53ec625
-		if (!is_omitted(task.XXXID)) {
53ec625
+		if (!is_omitted(task.XXXID) && ((task.cmdline && *task.cmdline) || opt_with_workers)) {
53ec625
 
53ec625
 			cmd_arg0 = (task.cmdline && *task.cmdline) ? *task.cmdline : "\0";
53ec625
-			stat_cmd = task.cmd ? task.cmd : "\0";
53ec625
 
53ec625
 			/* processes starting with '-' are login shells */
53ec625
 			if (*cmd_arg0 == '-') {
53ec625
@@ -193,7 +194,7 @@ static void select_procs (void)
53ec625
 			    !strcmp(program_base, cmd_arg0) ||
53ec625
 			    !strcmp(program, cmd_arg0) ||
53ec625
 
53ec625
-			    !strcmp(program, stat_cmd) ||
53ec625
+			    (opt_with_workers && !strcmp(program, task.cmd)) ||
53ec625
 
53ec625
 			    !strcmp(program, exe_link_base) ||
53ec625
 			    !strcmp(program, exe_link))
53ec625
@@ -293,13 +294,14 @@ int main (int argc, char **argv)
53ec625
 	int first_pid = 1;
53ec625
 
53ec625
 	const char *separator = " ";
53ec625
-	const char *opts = "scdnxmo:S:?Vh";
53ec625
+	const char *opts = "scdnxwmo:S:?Vh";
53ec625
 
53ec625
 	static const struct option longopts[] = {
53ec625
 		{"check-root", no_argument, NULL, 'c'},
53ec625
 		{"single-shot", no_argument, NULL, 's'},
53ec625
 		{"omit-pid", required_argument, NULL, 'o'},
53ec625
 		{"separator", required_argument, NULL, 'S'},
53ec625
+		{"with-workers", no_argument, NULL, 'w'},
53ec625
 		{"help", no_argument, NULL, 'h'},
53ec625
 		{"version", no_argument, NULL, 'V'},
53ec625
 		{NULL, 0, NULL, 0}
53ec625
@@ -325,6 +327,9 @@ int main (int argc, char **argv)
53ec625
 		case 'x':
53ec625
 			opt_scripts_too = 1;
53ec625
 			break;
53ec625
+		case 'w':
53ec625
+			opt_with_workers = 1;
53ec625
+			break;
53ec625
 		case 'c':
53ec625
 			if (geteuid() == 0) {
53ec625
 				opt_rootdir_check = 1;
53ec625
@@ -359,6 +364,8 @@ int main (int argc, char **argv)
53ec625
 
53ec625
 		program = argv[optind++];
53ec625
 
53ec625
+		if (*program == '\0') continue;
53ec625
+
53ec625
 		select_procs();	/* get the list of matching processes */
53ec625
 
53ec625
 		if (proc_count) {