Patch from vodz:
[oweals/busybox.git] / libbb / run_parts.c
index 8399a6afb6790a06b88ceb5674d8e45b77f2ad3d..171d93793d16fffe313f7f9c9848593eeab3584f 100644 (file)
@@ -43,13 +43,16 @@ static int valid_name(const struct dirent *d)
        return 1;
 }
 
-/* run_parts */
-/* Find the parts to run & call run_part() */
-extern int run_parts(char **args, const unsigned char test_mode)
+/* test mode = 1 is the same as offical run_parts
+ * test_mode = 2 means to fail siliently on missing directories
+ */
+
+extern int run_parts(char **args, const unsigned char test_mode, char **env)
 {
        struct dirent **namelist = 0;
        struct stat st;
        char *filename;
+       char *arg0 = args[0];
        int entries;
        int i;
        int exitstatus = 0;
@@ -60,33 +63,36 @@ extern int run_parts(char **args, const unsigned char test_mode)
        (void) &exitstatus;
 #endif
        /* scandir() isn't POSIX, but it makes things easy. */
-       entries = scandir(args[0], &namelist, valid_name, alphasort);
+       entries = scandir(arg0, &namelist, valid_name, alphasort);
 
        if (entries == -1) {
-               perror_msg_and_die("failed to open directory %s", args[0]);
+               if (test_mode & 2) {
+                       return(2);
+               }
+               bb_perror_msg_and_die("failed to open directory %s", arg0);
        }
 
        for (i = 0; i < entries; i++) {
 
-               filename = concat_path_file(args[0], namelist[i]->d_name);
+               filename = concat_path_file(arg0, namelist[i]->d_name);
 
                if (stat(filename, &st) < 0) {
-                       perror_msg_and_die("failed to stat component %s", filename);
+                       bb_perror_msg_and_die("failed to stat component %s", filename);
                }
                if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
-                       if (test_mode)
-                               printf("run-parts would run %s\n", filename);
-                       else {
+                       if (test_mode & 1) {
+                               puts(filename);
+                       else {
                                /* exec_errno is common vfork variable */
                                volatile int exec_errno = 0;
                                int result;
                                int pid;
 
                                if ((pid = vfork()) < 0) {
-                                       perror_msg_and_die("failed to fork");
+                                       bb_perror_msg_and_die("failed to fork");
                                } else if (!pid) {
                                        args[0] = filename;
-                                       execv(filename, args);
+                                       execve(filename, args, env);
                                        exec_errno = errno;
                                        _exit(1);
                                }
@@ -94,19 +100,19 @@ extern int run_parts(char **args, const unsigned char test_mode)
                                waitpid(pid, &result, 0);
                                if(exec_errno) {
                                        errno = exec_errno;
-                                       perror_msg_and_die("failed to exec %s", filename);
+                                       bb_perror_msg_and_die("failed to exec %s", filename);
                                }
                                if (WIFEXITED(result) && WEXITSTATUS(result)) {
-                                       perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
+                                       bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
                                        exitstatus = 1;
                                } else if (WIFSIGNALED(result)) {
-                                       perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
+                                       bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
                                        exitstatus = 1;
                                }
                        }
                } 
                else if (!S_ISDIR(st.st_mode)) {
-                       error_msg("component %s is not an executable plain file", filename);
+                       bb_error_msg("component %s is not an executable plain file", filename);
                        exitstatus = 1;
                }