view sources/patches/alt-busybox-pgrep.patch @ 775:f43389fc2452

Update patch because busybox changed upstream.
author Rob Landley <rob@landley.net>
date Thu, 02 Jul 2009 00:54:08 -0500
parents 7cb6ef2cdf10
children
line wrap: on
line source

diff --git a/include/usage.h b/include/usage.h
index f654062..ce8a7e5 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -3224,7 +3224,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:" \
@@ -3234,6 +3234,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 \
@@ -3326,7 +3328,7 @@
        "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:" \
@@ -3336,6 +3338,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;