*: code shrink and better "died from signal" reporting from wait4pid
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 18 Mar 2010 21:44:00 +0000 (22:44 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 18 Mar 2010 21:44:00 +0000 (22:44 +0100)
function                                             old     new   delta
parse                                                964     967      +3
udhcp_run_script                                     670     665      -5
singlemount                                          911     906      -5
mount_it_now                                         360     355      -5
inotifyd_main                                        521     516      -5
xspawn                                                21       -     -21
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/4 up/down: 3/-41)             Total: -38 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
12 files changed:
debianutils/run_parts.c
findutils/xargs.c
include/libbb.h
libbb/vfork_daemon_rexec.c
mailutils/mime.c
miscutils/devfsd.c
miscutils/inotifyd.c
networking/ifplugd.c
networking/ntpd.c
networking/udhcp/script.c
networking/zcip.c
util-linux/mount.c

index 2854a539cfa331b985148d81bfad6ccd95ded7d5..37e8487e18117e44ca2907cdde21d764b3227841 100644 (file)
@@ -159,14 +159,14 @@ int run_parts_main(int argc UNUSED_PARAM, char **argv)
                        continue;
                }
                cmd[0] = name;
-               ret = wait4pid(spawn(cmd));
+               ret = spawn_and_wait(cmd);
                if (ret == 0)
                        continue;
                n = 1;
                if (ret < 0)
                        bb_perror_msg("can't execute '%s'", name);
                else /* ret > 0 */
-                       bb_error_msg("%s exited with code %d", name, ret);
+                       bb_error_msg("%s exited with code %d", name, ret & 0xff);
        }
 
        return n;
index c7117771cbb4e34a52cb157ad12e12e0b53a3c2f..f5dbc7825c2dc4f4fea616e0ae5f0f9eb94933f9 100644 (file)
@@ -64,16 +64,9 @@ static int xargs_exec(char **args)
                bb_error_msg("%s: exited with status 255; aborting", args[0]);
                return 124;
        }
-/* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
-       if (WIFSTOPPED(status)) {
-               bb_error_msg("%s: stopped by signal %d",
-                       args[0], WSTOPSIG(status));
-               return 125;
-       }
-*/
-       if (status >= 1000) {
+       if (status >= 0x180) {
                bb_error_msg("%s: terminated by signal %d",
-                       args[0], status - 1000);
+                       args[0], status - 0x180);
                return 125;
        }
        if (status)
index 044d09047a607a725d00ae688b59e7acda4dd4e9..72d6c7dc84907b7947e8e92562c34aa1e544bbdc 100644 (file)
@@ -812,21 +812,22 @@ int bb_execvp(const char *file, char *const argv[]) FAST_FUNC;
 #define BB_EXECLP(prog,cmd,...) execlp(prog,cmd, __VA_ARGS__)
 #endif
 
-/* NOMMU friendy fork+exec */
+/* NOMMU friendy fork+exec: */
 pid_t spawn(char **argv) FAST_FUNC;
 pid_t xspawn(char **argv) FAST_FUNC;
 
 pid_t safe_waitpid(pid_t pid, int *wstat, int options) FAST_FUNC;
-/* Unlike waitpid, waits ONLY for one process.
+pid_t wait_any_nohang(int *wstat) FAST_FUNC;
+/* wait4pid: unlike waitpid, waits ONLY for one process.
+ * Returns sig + 0x180 if child is killed by signal.
  * It's safe to pass negative 'pids' from failed [v]fork -
  * wait4pid will return -1 (and will not clobber [v]fork's errno).
  * IOW: rc = wait4pid(spawn(argv));
  *      if (rc < 0) bb_perror_msg("%s", argv[0]);
- *      if (rc > 0) bb_error_msg("exit code: %d", rc);
+ *      if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff);
  */
 int wait4pid(pid_t pid) FAST_FUNC;
-pid_t wait_any_nohang(int *wstat) FAST_FUNC;
-/* wait4pid(spawn(argv)) + NOFORK/NOEXEC (if configured) */
+/* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */
 int spawn_and_wait(char **argv) FAST_FUNC;
 struct nofork_save_area {
        jmp_buf die_jmp;
index 9be90172290ef67bfd611c64371befa24c2e56b8..07024f5f07758ea0a1dd02ae7f7d26c16a08dc27 100644 (file)
@@ -97,7 +97,7 @@ int FAST_FUNC wait4pid(pid_t pid)
        if (WIFEXITED(status))
                return WEXITSTATUS(status);
        if (WIFSIGNALED(status))
-               return WTERMSIG(status) + 1000;
+               return WTERMSIG(status) + 0x180;
        return 0;
 }
 
index 125ca01ddff1ce9a8298bbe331c4424f209e6c0d..ee147802e2b0343b9c9250b843ebdd6b7b71a5ad 100644 (file)
@@ -357,7 +357,7 @@ static int parse(const char *boundary, char **argv)
                        if (opts & OPT_X) {
                                signal(SIGPIPE, SIG_DFL);
                                // exit if helper exited >0
-                               rc = wait4pid(pid);
+                               rc = (wait4pid(pid) & 0xff);
                                if (rc)
                                        return rc+20;
                        }
index 62f5de8b6011884039451847fdc9610cf0e35ff4..4ccb76d9544f621c4397069e34d1ea9f4d394af2 100644 (file)
@@ -751,7 +751,7 @@ static void action_modload(const struct devfsd_notify_struct *info,
        argv[4] = concat_path_file("/dev", info->devname); /* device */
        argv[5] = NULL;
 
-       wait4pid(xspawn(argv));
+       spawn_and_wait(argv);
        free(argv[4]);
 }  /*  End Function action_modload  */
 
@@ -783,7 +783,7 @@ static void action_execute(const struct devfsd_notify_struct *info,
                argv[count] = largv[count];
        }
        argv[count] = NULL;
-       wait4pid(spawn(argv));
+       spawn_and_wait(argv);
 }   /*  End Function action_execute  */
 
 
index 999b5e309f168b9b9eb370642acfdc82fdd79dd9..271f3ade17667237e9d874a88e2fe0173d74cf28 100644 (file)
@@ -155,7 +155,7 @@ int inotifyd_main(int argc, char **argv)
                                args[1] = events;
                                args[2] = watches[ie->wd];
                                args[3] = ie->len ? ie->name : NULL;
-                               wait4pid(xspawn((char **)args));
+                               spawn_and_wait((char **)args);
                                // we are done if all files got final x event
                                if (ie->mask & 0x8000) {
                                        if (--argc <= 0)
index ac6607c9155a6b9740a632cb592473a665f76927..62b1355246986532f2968d934fc54c2a2b5cc436 100644 (file)
@@ -132,10 +132,10 @@ static int run_script(const char *action)
        argv[3] = (char*) G.extra_arg;
        argv[4] = NULL;
 
-       /* r < 0 - can't exec, 0 <= r < 1000 - exited, >1000 - killed by sig (r-1000) */
-       r = wait4pid(spawn(argv));
+       /* r < 0 - can't exec, 0 <= r < 0x180 - exited, >=0x180 - killed by sig (r-0x180) */
+       r = spawn_and_wait(argv);
 
-       bb_error_msg("exit code: %d", r);
+       bb_error_msg("exit code: %d", r & 0xff);
        return (option_mask32 & FLAG_IGNORE_RETVAL) ? 0 : r;
 
 #else /* insanity */
index 04df3fa7fa060d91e8b35458b811a3287fff34ee..6d9183a4bfa04264d665d12f1ad79f10e16c8dd2 100644 (file)
@@ -772,7 +772,7 @@ static void run_script(const char *action, double offset)
 
        /* Don't want to wait: it may run hwclock --systohc, and that
         * may take some time (seconds): */
-       /*wait4pid(spawn(argv));*/
+       /*spawn_and_wait(argv);*/
        spawn(argv);
 
        unsetenv("stratum");
index 574c74f45c1dc0b4ed0f645c4ddfd26690924ad0..a74697c90ba1261a78931edcc39e0e2106646c44 100644 (file)
@@ -271,7 +271,7 @@ void FAST_FUNC udhcp_run_script(struct dhcp_packet *packet, const char *name)
        argv[0] = (char*) client_config.script;
        argv[1] = (char*) name;
        argv[2] = NULL;
-       wait4pid(spawn(argv));
+       spawn_and_wait(argv);
 
        for (curr = envp; *curr; curr++) {
                log2(" %s", *curr);
index db10d0a26c7d11cbcc912fb17cddd6126b8870df..6b0b1c4917784182f8549207a7914fcdb6a0273c 100644 (file)
@@ -160,13 +160,13 @@ static int run(char *argv[3], const char *param, struct in_addr *ip)
        }
        bb_info_msg(fmt, argv[2], argv[0], addr);
 
-       status = wait4pid(spawn(argv + 1));
+       status = spawn_and_wait(argv + 1);
        if (status < 0) {
                bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
                return -errno;
        }
        if (status != 0)
-               bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status);
+               bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
        return status;
 }
 
index 620b14667b9be4c5c963855bf57b7b056074681d..e24fc40245b8a00c8de08a22b733b41012fa9f09 100644 (file)
@@ -456,7 +456,7 @@ static int mount_it_now(struct mntent *mp, long vfsflags, char *filteropts)
                                args[rc++] = filteropts;
                        }
                        args[rc] = NULL;
-                       rc = wait4pid(spawn(args));
+                       rc = spawn_and_wait(args);
                        free(args[0]);
                        if (!rc)
                                break;
@@ -1633,7 +1633,7 @@ static int singlemount(struct mntent *mp, int ignore_busy)
                }
                args[n++] = mp->mnt_dir;
                args[n] = NULL;
-               rc = wait4pid(xspawn(args));
+               rc = spawn_and_wait(args);
                goto report_error;
        }