hush: fix exec builtin in a better way (+ "glob in exec" bug fixed)
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 19 Oct 2009 21:09:06 +0000 (23:09 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 19 Oct 2009 21:09:06 +0000 (23:09 +0200)
function                                             old     new   delta
execvp_or_die                                          -      50     +50
static.pseudo_null_str                                 3       -      -3
builtin_exec                                          83      57     -26
pseudo_exec_argv                                     200     163     -37
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/2 up/down: 50/-66)            Total: -16 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c
shell/hush_test/hush-vars/param_glob.tests

index ae08c73269d14c385523f1416e6b074780408bcd..5edcdb8445976485401704266b4ecd7beb284495 100644 (file)
@@ -3322,6 +3322,16 @@ static void exec_builtin(char ***to_free,
 }
 
 
+static void execvp_or_die(char **argv) NORETURN;
+static void execvp_or_die(char **argv)
+{
+       debug_printf_exec("execing '%s'\n", argv[0]);
+       sigprocmask(SIG_SETMASK, &G.inherited_set, NULL);
+       execvp(argv[0], argv);
+       bb_perror_msg("can't execute '%s'", argv[0]);
+       _exit(127); /* bash compat */
+}
+
 #if BB_MMU
 #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \
        pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
@@ -3337,7 +3347,7 @@ static void exec_builtin(char ***to_free,
 static void pseudo_exec_argv(nommu_save_t *nommu_save,
                char **argv, int assignment_cnt,
                char **argv_expanded) NORETURN;
-static void pseudo_exec_argv(nommu_save_t *nommu_save,
+static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save,
                char **argv, int assignment_cnt,
                char **argv_expanded)
 {
@@ -3421,11 +3431,7 @@ static void pseudo_exec_argv(nommu_save_t *nommu_save,
 #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU
  skip:
 #endif
-       debug_printf_exec("execing '%s'\n", argv[0]);
-       sigprocmask(SIG_SETMASK, &G.inherited_set, NULL);
-       execvp(argv[0], argv);
-       bb_perror_msg("can't execute '%s'", argv[0]);
-       _exit(127); /* bash compat */
+       execvp_or_die(argv);
 }
 
 /* Called after [v]fork() in run_pipe
@@ -6985,32 +6991,19 @@ static int FAST_FUNC builtin_cd(char **argv)
 
 static int FAST_FUNC builtin_exec(char **argv)
 {
-       static const char pseudo_null_str[] = { SPECIAL_VAR_SYMBOL, SPECIAL_VAR_SYMBOL, '\0' };
-       char **pp;
-#if !BB_MMU
-       nommu_save_t dummy;
-#endif
-
        if (*++argv == NULL)
                return EXIT_SUCCESS; /* bash does this */
 
-       /* Make sure empty arguments aren't ignored */
-       /* Example: exec ls '' */
-       pp = argv;
-       while (*pp) {
-               if ((*pp)[0] == '\0')
-                       *pp = (char*)pseudo_null_str;
-               pp++;
-       }
-
        /* Careful: we can end up here after [v]fork. Do not restore
         * tty pgrp then, only top-level shell process does that */
        if (G_saved_tty_pgrp && getpid() == G.root_pid)
                tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp);
 
-       /* TODO: if exec fails, bash does NOT exit! We do... */
-       pseudo_exec_argv(&dummy, argv, 0, NULL);
-       /* never returns */
+       /* TODO: if exec fails, bash does NOT exit! We do.
+        * We'll need to undo sigprocmask (it's inside execvp_or_die)
+        * and tcsetpgrp, and this is inherently racy.
+        */
+       execvp_or_die(argv);
 }
 
 static int FAST_FUNC builtin_exit(char **argv)
index 0173fd771a97a126ae59e88f81b68c27688bcde6..4d74fee029556216ca5914d7ed90f21e04971ab0 100755 (executable)
@@ -1,7 +1,6 @@
 if test $# = 0; then
-    # UNFIXED BUG in builtin_exec! will glob param!
-    #exec "$THIS_SH" "$0" 'param_glob.t*'
-    "$THIS_SH" "$0" 'param_glob.t*'
+    exec "$THIS_SH" "$0" 'param_glob.t*'
+    echo NOT SHOWN
     exit
 fi
 echo $*