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     741     +88
.rodata                                           157453  157517     +64
packed_usage                                       30506   30556     +50
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 202/0)             Total: 202 bytes
   text    data     bss     dec     hex filename
 828021    4196    9584  841801   cd849 busybox_old
 828165    4196    9584  841945   cd8d9 busybox_unstripped

Signed-off-by: Bartosz Golaszewski <[email protected]>
---
 miscutils/inotifyd.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/miscutils/inotifyd.c b/miscutils/inotifyd.c
index 7a1a6a2..97f67ab 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,22 @@ 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;
+       exp_st = xstrtol_range(opt_x_str, 10, 0, 255);
 
        // 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 +200,9 @@ 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) && st == exp_st)
+                                               goto done;
                                }
                                // we are done if all files got final x event
                                if (ie->mask & 0x8000) {
-- 
2.1.4

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to