[PATCH v2] inotifyd: -x: new option

Bartosz Golaszewski bartekgola at gmail.com
Tue Aug 25 13:39:02 UTC 2015


Add -x option which allows to specify the exit status of PROG for which
inotifyd should exit.

An example use case for this change is writing parallel system startup
scripts with busybox' runit: inotifyd can be used to wait for a specific
pid-file to appear in /var/run and then exit, allowing the blocked script
to proceed.

function                                             old     new   delta
inotifyd_main                                        653     742     +89
.rodata                                           157197  157261     +64
packed_usage                                       30460   30516     +56
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 209/0)             Total: 209 bytes

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
v2:
- don't call xstrtol_range() if -x is not specified

v1:
http://lists.busybox.net/pipermail/busybox/2015-August/083219.html

 miscutils/inotifyd.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/miscutils/inotifyd.c b/miscutils/inotifyd.c
index 7a1a6a2..e142668 100644
--- a/miscutils/inotifyd.c
+++ b/miscutils/inotifyd.c
@@ -28,7 +28,7 @@
  */
 
 //usage:#define inotifyd_trivial_usage
-//usage:	"PROG FILE1[:MASK]..."
+//usage:	"[OPTS] PROG FILE1[:MASK]..."
 //usage:#define inotifyd_full_usage "\n\n"
 //usage:       "Run PROG on filesystem changes."
 //usage:     "\nWhen a filesystem event matching MASK occurs on FILEn,"
@@ -52,12 +52,17 @@
 //usage:     "\n	n	Subfile is created"
 //usage:     "\n	d	Subfile is deleted"
 //usage:     "\n"
+//usage:     "\nOptions:"
+//usage:     "\n	-x STATUS	Exit if PROG returns STATUS"
+//usage:     "\n"
 //usage:     "\ninotifyd waits for PROG to exit."
 //usage:     "\nWhen x event happens for all FILEs, inotifyd exits."
 
 #include "libbb.h"
 #include <sys/inotify.h>
 
+#define OPT_x (1 << 0)
+
 static const char mask_names[] ALIGN1 =
 	"a"	// 0x00000001	File was accessed
 	"c"	// 0x00000002	File was modified
@@ -84,17 +89,21 @@ enum {
 int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int inotifyd_main(int argc, char **argv)
 {
-	int n;
-	unsigned mask;
+	int n, exp_st, st;
+	unsigned mask, opts;
 	struct pollfd pfd;
 	char **watches; // names of files being watched
 	const char *args[5];
+	char *opt_x_str;
+
+	opts = getopt32(argv, "x:", &opt_x_str);
+	argc -= optind;
+	argv += optind;
 
 	// sanity check: agent and at least one watch must be given
-	if (!argv[1] || !argv[2])
+	if (!argv[0] || !argv[1])
 		bb_show_usage();
 
-	argv++;
 	// inotify_add_watch will number watched files
 	// starting from 1, thus watches[0] is unimportant,
 	// and 1st file name is watches[1].
@@ -190,7 +199,12 @@ int inotifyd_main(int argc, char **argv)
 					args[1] = events;
 					args[2] = watches[ie->wd];
 					args[3] = ie->len ? ie->name : NULL;
-					spawn_and_wait((char **)args);
+					st = spawn_and_wait((char **)args);
+					if (opts & OPT_x) {
+						exp_st = xstrtol_range(opt_x_str, 10, 0, 255);
+						if (st == exp_st)
+							goto done;
+					}
 				}
 				// we are done if all files got final x event
 				if (ie->mask & 0x8000) {
-- 
2.1.4



More information about the busybox mailing list