[BusyBox] [patch] pidof, make -s optional and add optional -o

Bernhard Fischer rep.nop at aon.at
Tue Feb 8 14:49:39 UTC 2005


Hi,

include/usage.h: add strings for -o and make -s optional.
libbb/find_pid_by_name.c: reverse returned pidlist to mimic GNU's big
	pidof.
procps/Config.in: add config option for -o, -s. Both default to off.
procps/pidof.c: remove var for single_flag and use variable opt instead.
	remove direct usage of getopt and reuse bb_getulflags().
	add (optional) -o for omitting pids.

Comments?

-------------- next part --------------
diff -X ../excl -rdup upstream/busybox/include/usage.h busybox/include/usage.h
--- upstream/busybox/include/usage.h	2005-01-24 08:00:01.000000000 +0100
+++ busybox/include/usage.h	2005-02-07 18:30:53.000000000 +0100
@@ -1865,12 +1865,31 @@
 	"$ patch -p1 <example.diff"
 
 #define pidof_trivial_usage \
-	"process-name [OPTION] [process-name ...]"
+	"[OPTION] [process-name ...]"
+#ifdef CONFIG_FEATURE_PIDOF_SINGLE
+#define USAGE_PIDOF_SINGLE(a) a
+#else
+#define USAGE_PIDOF_SINGLE(a)
+#endif
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+#define USAGE_PIDOF_OMIT(a) a
+#else
+#define USAGE_PIDOF_OMIT(a)
+#endif
+#if defined(CONFIG_FEATURE_PIDOF_SINGLE) || defined(CONFIG_FEATURE_PIDOF_OMIT)
+#define USAGE_PIDOF "\nOptions:"
+#else
+#define USAGE_PIDOF "\n\tThis version of pidof accepts no options."
+#endif
+
 #define pidof_full_usage \
 	"Lists the PIDs of all processes with names that match the\n" \
 	"names on the command line.\n" \
-	"Options:\n" \
-	"\t-s\t\tdisplay only a single PID."
+	USAGE_PIDOF \
+	USAGE_PIDOF_SINGLE("\n\t-s\t\tdisplay only a single PID.") \
+	USAGE_PIDOF_OMIT("\n\t-o\t\tomit PID.") \
+	USAGE_PIDOF_OMIT("\n\t\t\tThe special string %PPID refers to") \
+	USAGE_PIDOF_OMIT("\n\t\t\tpidof's parent process.")
 #define pidof_example_usage \
 	"$ pidof init\n" \
 	"1\n"
diff -X ../excl -rdup upstream/busybox/libbb/find_pid_by_name.c busybox/libbb/find_pid_by_name.c
--- upstream/busybox/libbb/find_pid_by_name.c	2004-03-15 09:28:42.000000000 +0100
+++ busybox/libbb/find_pid_by_name.c	2005-02-07 14:35:43.000000000 +0100
@@ -57,6 +57,16 @@ extern long* find_pid_by_name( const cha
 	}
 
 	pidList[i] = i==0 ? -1 : 0;
+	/* reverse list to mimic GNU pidof */
+	if (i-- > 0) {
+		long k;
+		int j;
+		for (j = 0; i > j; i--, j++) {
+			k = pidList[i];
+			pidList[i] = pidList[j];
+			pidList[j] = k;
+		}
+	}
 	return pidList;
 }
 
diff -X ../excl -rdup upstream/busybox/procps/Config.in busybox/procps/Config.in
--- upstream/busybox/procps/Config.in	2003-12-24 07:02:11.000000000 +0100
+++ busybox/procps/Config.in	2005-02-08 09:45:42.000000000 +0100
@@ -37,6 +37,22 @@ config CONFIG_PIDOF
 	  Pidof finds the process id's (pids) of the named programs. It prints
 	  those id's on the standard output.
 
+config CONFIG_FEATURE_PIDOF_SINGLE
+	bool "  Enable argument for single shot (-s)"
+	default n
+	depends on CONFIG_PIDOF
+	help
+	  Support argument '-s' for returning only the first pid found.
+
+config CONFIG_FEATURE_PIDOF_OMIT
+	bool "  Enable argument for omitting pids (-o)"
+	default n
+	depends on CONFIG_PIDOF
+	help
+	  Support argument '-o' for omitting the given pids in output.
+	  The special pid %PPID can be used to name the parent process
+	  of the pidof, in other words the calling shell or shell script.
+
 config CONFIG_PS
 	bool "ps"
 	default n
diff -X ../excl -rdup upstream/busybox/procps/pidof.c busybox/procps/pidof.c
--- upstream/busybox/procps/pidof.c	2004-03-15 09:29:03.000000000 +0100
+++ busybox/procps/pidof.c	2005-02-07 18:17:24.000000000 +0100
@@ -31,24 +31,61 @@
 #include <unistd.h>
 #include "busybox.h"
 
+#ifdef CONFIG_FEATURE_PIDOF_SINGLE
+#define _SINGLE(a) a
+#define SINGLE (1<<0)
+#else
+#define _SINGLE(a)
+#endif
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+#define _OMIT(a, b) a , b
+static llist_t *omits; /* list of pids to omit */
+#ifdef CONFIG_FEATURE_PIDOF_SINGLE
+#define OMIT (1<<1)
+#else
+#define OMIT (1<<0)
+#endif
+#else
+#define _OMIT(a)
+#endif
 
 extern int pidof_main(int argc, char **argv)
 {
-	int opt, n = 0;
-	int single_flag = 0;
+	int n = 0;
 	int fail = 1;
+	unsigned long int opt;
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+	bb_opt_complementaly = "o*";
+#endif
 
-	/* do normal option parsing */
-	while ((opt = getopt(argc, argv, "s")) > 0) {
-		switch (opt) {
-			case 's':
-				single_flag = 1;
-				break;
-			default:
-				bb_show_usage();
-		}
+	/* do option parsing */
+	if ((opt = bb_getopt_ulflags(argc, argv, _SINGLE("s") _OMIT("o:", &omits)))
+			> 0) {
+#ifdef CONFIG_FEATURE_PIDOF_SINGLE
+		if (!(opt & SINGLE))
+#endif
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+		if (!(opt & OMIT))
+#endif
+		bb_show_usage();
 	}
 
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+	/* fill omit list */
+	{
+		llist_t * omits_p = omits;
+		while (omits_p) {
+			if (omits_p->data[0] == '%') {
+				if (!strncmp(omits_p->data, "%PPID", 5))
+					omits_p->data = getppid();
+				/* else { fprintf(stderr, "illegal omit pid value (%s)!\n",
+								omits_p->data); bb_show_usage(); } */
+			} else
+				omits_p->data = strtol(omits_p->data, NULL, 10);
+			omits_p = omits_p->link;
+		}
+	}
+#endif
 	/* Looks like everything is set to go. */
 	while(optind < argc) {
 		long *pidList;
@@ -56,16 +93,38 @@ extern int pidof_main(int argc, char **a
 
 		pidList = find_pid_by_name(argv[optind]);
 		for(pl = pidList; *pl > 0; pl++) {
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+			short omitted = 0;
+			if (opt & OMIT) {
+				llist_t *omits_p = omits;
+				while ((opt & OMIT) && omits_p)
+					if (omits_p->data == *pl) { omitted = 1; break; }
+					else omits_p = omits_p->link;
+			}
+			if (!omitted)
+#endif
 			printf("%s%ld", (n++ ? " " : ""), *pl);
+#ifdef CONFIG_FEATURE_PIDOF_OMIT
+			fail = omitted;
+#else
 			fail = 0;
-			if (single_flag)
+#endif
+#ifdef CONFIG_FEATURE_PIDOF_SINGLE
+			if (opt & SINGLE)
 				break;
+#endif
 		}
 		free(pidList);
 		optind++;
 
 	}
 	printf("\n");
-
+#ifdef CONFIG_FEATURE_CLEAN_UP
+	while (omits) {
+		llist_t *omits_p = omits;
+		omits = omits->link;
+		free(omits_p);
+	}
+#endif
 	return fail ? EXIT_FAILURE : EXIT_SUCCESS;
 }


More information about the busybox mailing list