view sources/patches/busybox-pgrep.patch @ 772:7cb6ef2cdf10

Busybox unstable currently needs this patch too.
author Rob Landley <rob@landley.net>
date Wed, 01 Jul 2009 18:02:44 -0500
parents d0157468f73d
children
line wrap: on
line source

diff --git a/include/usage.h b/include/usage.h
index e606925..20fd909 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -3207,7 +3207,7 @@
        "$ patch -p0 -i example.diff"
 
 #define pgrep_trivial_usage \
-       "[-flnovx] pattern"
+       "[-flnovx] [-s SID|-P ppid|pattern]"
 #define pgrep_full_usage "\n\n" \
        "Display process(es) selected by regex pattern\n" \
      "\nOptions:" \
@@ -3217,6 +3217,8 @@
      "\n	-o	Show the oldest process only" \
      "\n	-v	Negate the matching" \
      "\n	-x	Match whole name (not substring)" \
+     "\n	-s	Match session ID (0 for this one)" \
+     "\n	-P	Match parent process ID" \
 
 #if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
 #define pidof_trivial_usage \
@@ -3309,7 +3311,8 @@
        "the new root file system"
 
 #define pkill_trivial_usage \
-       "[-l] | [-fnovx] [-signal] pattern"
+       "[-l|-SIGNAL] [-fnovx] [-s SID|-P ppid|pattern]"
+
 #define pkill_full_usage "\n\n" \
        "Send a signal to process(es) selected by regex pattern\n" \
      "\nOptions:" \
@@ -3319,6 +3322,10 @@
      "\n	-o	Signal the oldest process only" \
      "\n	-v	Negate the matching" \
      "\n	-x	Match whole name (not substring)" \
+     "\n	-s	Match session ID (0 for this one)" \
+     "\n	-P	Match parent process ID" \
+     "\n\nSignal, if present, must be first option."
+
 
 #define popmaildir_trivial_usage \
        "[OPTIONS] Maildir [connection-helper ...]"
diff --git a/procps/pgrep.c b/procps/pgrep.c
index 0e8e529..a336bdf 100644
--- a/procps/pgrep.c
+++ b/procps/pgrep.c
@@ -49,10 +49,8 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
 	int signo = SIGTERM;
 	unsigned opt;
 	int scan_mask = PSSCAN_COMM;
-	char *first_arg;
-	int first_arg_idx;
-	int matched_pid;
-	char *cmd_last;
+	int matched_pid, matched_sid = 0, matched_ppid = 0;
+	char *cmd_last, *which_sid = NULL, *which_ppid = NULL;
 	procps_status_t *proc;
 	/* These are initialized to 0 */
 	struct {
@@ -64,45 +62,39 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
 
 	memset(&Z, 0, sizeof(Z));
 
-	/* We must avoid interpreting -NUM (signal num) as an option */
-	first_arg_idx = 1;
-	while (1) {
-		first_arg = argv[first_arg_idx];
-		if (!first_arg)
-			break;
-		/* not "-<small_letter>..."? */
-		if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 'z') {
-			argv[first_arg_idx] = NULL; /* terminate argv here */
-			break;
+	/* Parse -SIGNAL for pkill.  Must be first option, if present. */
+	if (pkill && argv[1] && argv[1][0]=='-') {
+		int temp = get_signum(argv[1]+1);
+		if (temp != -1) {
+			signo = temp;
+			argv++;
 		}
-		first_arg_idx++;
 	}
-	opt = getopt32(argv, "vlfxon");
-	argv[first_arg_idx] = first_arg;
+
+	/* Parse remaining options */
+	opt = getopt32(argv, "vlfxons:P:", &which_sid, &which_ppid);
+
+	if (pkill && OPT_LIST) { /* -l: print the whole signal list */
+		print_signames();
+		return 0;
+	}
+
+	if (which_sid) {
+		matched_sid = xatol(which_sid);
+		if (!matched_sid) matched_sid = getsid(pid);
+	}
+	if (which_ppid) matched_ppid = xatol(which_ppid);
 
 	argv += optind;
 	//argc -= optind; - unused anyway
 	if (OPT_FULL)
 		scan_mask |= PSSCAN_ARGVN;
 
-	if (pkill) {
-		if (OPT_LIST) { /* -l: print the whole signal list */
-			print_signames();
-			return 0;
-		}
-		if (first_arg && first_arg[0] == '-') {
-			signo = get_signum(&first_arg[1]);
-			if (signo < 0) /* || signo > MAX_SIGNUM ? */
-				bb_error_msg_and_die("bad signal name '%s'", &first_arg[1]);
-			argv++;
-		}
-	}
-
 	/* One pattern is required */
-	if (!argv[0] || argv[1])
+	if (!which_sid && !which_ppid && (!argv[0] || argv[1]))
 		bb_show_usage();
 
-	xregcomp(&re_buffer, argv[0], 0);
+	if (argv[0]) xregcomp(&re_buffer, argv[0], 0);
 	matched_pid = 0;
 	cmd_last = NULL;
 	proc = NULL;
@@ -120,8 +112,13 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
 				i--;
 			}
 		}
+
+		if (matched_ppid && matched_ppid != proc->ppid) continue;
+		if (matched_sid && matched_sid != proc->sid) continue;
+
 		/* NB: OPT_INVERT is always 0 or 1 */
-		if ((regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
+		if (!argv[0] ||
+			(regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
 		     && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT
 		) {
 			matched_pid = proc->pid;