diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-26 20:13:39 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-26 20:13:39 +0000 |
| commit | e5667c1cfe339b2f84abaabb90259ec29b91cf89 (patch) | |
| tree | 385d96cede38de88bcc55cf2e02c36deb86f25a5 | |
| parent | ec0c920a78813d1c047924e024017189dedeec93 (diff) | |
| download | busybox-w32-e5667c1cfe339b2f84abaabb90259ec29b91cf89.tar.gz busybox-w32-e5667c1cfe339b2f84abaabb90259ec29b91cf89.tar.bz2 busybox-w32-e5667c1cfe339b2f84abaabb90259ec29b91cf89.zip | |
Closing bug 730. libbb run_parts is using scandir (a GNUism),
and it is used only by run_parts applet, so move it there.
Also saved ~30 bytes (prolly gcc autoinlining...).
| -rw-r--r-- | debianutils/run_parts.c | 107 | ||||
| -rw-r--r-- | include/libbb.h | 1 | ||||
| -rw-r--r-- | libbb/Kbuild | 1 | ||||
| -rw-r--r-- | libbb/run_parts.c | 119 |
4 files changed, 99 insertions, 129 deletions
diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index f2d90b64d..448fe80c1 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c | |||
| @@ -37,17 +37,108 @@ | |||
| 37 | 37 | ||
| 38 | #include "busybox.h" | 38 | #include "busybox.h" |
| 39 | #include <getopt.h> | 39 | #include <getopt.h> |
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | 40 | ||
| 43 | static const struct option runparts_long_options[] = { | 41 | static const struct option runparts_long_options[] = { |
| 44 | { "test", 0, NULL, 't' }, | 42 | { "test", 0, NULL, 't' }, |
| 45 | { "umask", 1, NULL, 'u' }, | 43 | { "umask", 1, NULL, 'u' }, |
| 46 | { "arg", 1, NULL, 'a' }, | 44 | { "arg", 1, NULL, 'a' }, |
| 47 | { 0, 0, 0, 0 } | 45 | { 0, 0, 0, 0 } |
| 48 | }; | 46 | }; |
| 49 | 47 | ||
| 50 | extern char **environ; | 48 | /* valid_name */ |
| 49 | /* True or false? Is this a valid filename (upper/lower alpha, digits, | ||
| 50 | * underscores, and hyphens only?) | ||
| 51 | */ | ||
| 52 | static int valid_name(const struct dirent *d) | ||
| 53 | { | ||
| 54 | const char *c = d->d_name; | ||
| 55 | |||
| 56 | while (*c) { | ||
| 57 | if (!isalnum(*c) && (*c != '_') && (*c != '-')) { | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | ++c; | ||
| 61 | } | ||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | |||
| 65 | /* test mode = 1 is the same as official run_parts | ||
| 66 | * test_mode = 2 means to fail silently on missing directories | ||
| 67 | */ | ||
| 68 | static int run_parts(char **args, const unsigned char test_mode) | ||
| 69 | { | ||
| 70 | struct dirent **namelist = 0; | ||
| 71 | struct stat st; | ||
| 72 | char *filename; | ||
| 73 | char *arg0 = args[0]; | ||
| 74 | int entries; | ||
| 75 | int i; | ||
| 76 | int exitstatus = 0; | ||
| 77 | |||
| 78 | #if __GNUC__ | ||
| 79 | /* Avoid longjmp clobbering */ | ||
| 80 | (void) &i; | ||
| 81 | (void) &exitstatus; | ||
| 82 | #endif | ||
| 83 | /* scandir() isn't POSIX, but it makes things easy. */ | ||
| 84 | entries = scandir(arg0, &namelist, valid_name, alphasort); | ||
| 85 | |||
| 86 | if (entries == -1) { | ||
| 87 | if (test_mode & 2) { | ||
| 88 | return(2); | ||
| 89 | } | ||
| 90 | bb_perror_msg_and_die("cannot open '%s'", arg0); | ||
| 91 | } | ||
| 92 | |||
| 93 | for (i = 0; i < entries; i++) { | ||
| 94 | filename = concat_path_file(arg0, namelist[i]->d_name); | ||
| 95 | |||
| 96 | xstat(filename, &st); | ||
| 97 | if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { | ||
| 98 | if (test_mode) { | ||
| 99 | puts(filename); | ||
| 100 | } else { | ||
| 101 | /* exec_errno is common vfork variable */ | ||
| 102 | volatile int exec_errno = 0; | ||
| 103 | int result; | ||
| 104 | int pid; | ||
| 105 | |||
| 106 | if ((pid = vfork()) < 0) { | ||
| 107 | bb_perror_msg_and_die("failed to fork"); | ||
| 108 | } else if (!pid) { | ||
| 109 | args[0] = filename; | ||
| 110 | execve(filename, args, environ); | ||
| 111 | exec_errno = errno; | ||
| 112 | _exit(1); | ||
| 113 | } | ||
| 114 | |||
| 115 | waitpid(pid, &result, 0); | ||
| 116 | if (exec_errno) { | ||
| 117 | errno = exec_errno; | ||
| 118 | bb_perror_msg("failed to exec %s", filename); | ||
| 119 | exitstatus = 1; | ||
| 120 | } | ||
| 121 | if (WIFEXITED(result) && WEXITSTATUS(result)) { | ||
| 122 | bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); | ||
| 123 | exitstatus = 1; | ||
| 124 | } else if (WIFSIGNALED(result)) { | ||
| 125 | bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); | ||
| 126 | exitstatus = 1; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } else if (!S_ISDIR(st.st_mode)) { | ||
| 130 | bb_error_msg("component %s is not an executable plain file", filename); | ||
| 131 | exitstatus = 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | free(namelist[i]); | ||
| 135 | free(filename); | ||
| 136 | } | ||
| 137 | free(namelist); | ||
| 138 | |||
| 139 | return exitstatus; | ||
| 140 | } | ||
| 141 | |||
| 51 | 142 | ||
| 52 | /* run_parts_main */ | 143 | /* run_parts_main */ |
| 53 | /* Process options */ | 144 | /* Process options */ |
| @@ -96,5 +187,5 @@ int run_parts_main(int argc, char **argv) | |||
| 96 | args[0] = argv[optind]; | 187 | args[0] = argv[optind]; |
| 97 | args[argcount] = 0; | 188 | args[argcount] = 0; |
| 98 | 189 | ||
| 99 | return(run_parts(args, test_mode, environ)); | 190 | return run_parts(args, test_mode); |
| 100 | } | 191 | } |
diff --git a/include/libbb.h b/include/libbb.h index 61e379423..e93031231 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -449,7 +449,6 @@ extern void run_shell(const char *shell, int loginshell, const char *command, co | |||
| 449 | extern void renew_current_security_context(void); | 449 | extern void renew_current_security_context(void); |
| 450 | extern void set_current_security_context(security_context_t sid); | 450 | extern void set_current_security_context(security_context_t sid); |
| 451 | #endif | 451 | #endif |
| 452 | extern int run_parts(char **args, const unsigned char test_mode, char **env); | ||
| 453 | extern int restricted_shell(const char *shell); | 452 | extern int restricted_shell(const char *shell); |
| 454 | extern void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw); | 453 | extern void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw); |
| 455 | extern int correct_password(const struct passwd *pw); | 454 | extern int correct_password(const struct passwd *pw); |
diff --git a/libbb/Kbuild b/libbb/Kbuild index 1ddec9a8d..550723cea 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
| @@ -65,7 +65,6 @@ lib-y += read.o | |||
| 65 | lib-y += recursive_action.o | 65 | lib-y += recursive_action.o |
| 66 | lib-y += remove_file.o | 66 | lib-y += remove_file.o |
| 67 | lib-y += restricted_shell.o | 67 | lib-y += restricted_shell.o |
| 68 | lib-y += run_parts.o | ||
| 69 | lib-y += run_shell.o | 68 | lib-y += run_shell.o |
| 70 | lib-y += safe_strncpy.o | 69 | lib-y += safe_strncpy.o |
| 71 | lib-y += safe_strtol.o | 70 | lib-y += safe_strtol.o |
diff --git a/libbb/run_parts.c b/libbb/run_parts.c deleted file mode 100644 index fae91813c..000000000 --- a/libbb/run_parts.c +++ /dev/null | |||
| @@ -1,119 +0,0 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * run command from specified directory | ||
| 4 | * | ||
| 5 | * | ||
| 6 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> | ||
| 7 | * rewrite to vfork usage by | ||
| 8 | * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> | ||
| 9 | * | ||
| 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
| 11 | */ | ||
| 12 | |||
| 13 | |||
| 14 | #include <sys/types.h> | ||
| 15 | #include <sys/wait.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | #include <dirent.h> | ||
| 18 | #include <unistd.h> | ||
| 19 | #include <ctype.h> | ||
| 20 | #include <errno.h> | ||
| 21 | |||
| 22 | #include "libbb.h" | ||
| 23 | |||
| 24 | /* valid_name */ | ||
| 25 | /* True or false? Is this a valid filename (upper/lower alpha, digits, | ||
| 26 | * underscores, and hyphens only?) | ||
| 27 | */ | ||
| 28 | static int valid_name(const struct dirent *d) | ||
| 29 | { | ||
| 30 | const char *c = d->d_name; | ||
| 31 | |||
| 32 | while (*c) { | ||
| 33 | if (!isalnum(*c) && (*c != '_') && (*c != '-')) { | ||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | ++c; | ||
| 37 | } | ||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | /* test mode = 1 is the same as official run_parts | ||
| 42 | * test_mode = 2 means to fail silently on missing directories | ||
| 43 | */ | ||
| 44 | |||
| 45 | int run_parts(char **args, const unsigned char test_mode, char **env) | ||
| 46 | { | ||
| 47 | struct dirent **namelist = 0; | ||
| 48 | struct stat st; | ||
| 49 | char *filename; | ||
| 50 | char *arg0 = args[0]; | ||
| 51 | int entries; | ||
| 52 | int i; | ||
| 53 | int exitstatus = 0; | ||
| 54 | |||
| 55 | #if __GNUC__ | ||
| 56 | /* Avoid longjmp clobbering */ | ||
| 57 | (void) &i; | ||
| 58 | (void) &exitstatus; | ||
| 59 | #endif | ||
| 60 | /* scandir() isn't POSIX, but it makes things easy. */ | ||
| 61 | entries = scandir(arg0, &namelist, valid_name, alphasort); | ||
| 62 | |||
| 63 | if (entries == -1) { | ||
| 64 | if (test_mode & 2) { | ||
| 65 | return(2); | ||
| 66 | } | ||
| 67 | bb_perror_msg_and_die("cannot open '%s'", arg0); | ||
| 68 | } | ||
| 69 | |||
| 70 | for (i = 0; i < entries; i++) { | ||
| 71 | |||
| 72 | filename = concat_path_file(arg0, namelist[i]->d_name); | ||
| 73 | |||
| 74 | xstat(filename, &st); | ||
| 75 | if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { | ||
| 76 | if (test_mode) { | ||
| 77 | puts(filename); | ||
| 78 | } else { | ||
| 79 | /* exec_errno is common vfork variable */ | ||
| 80 | volatile int exec_errno = 0; | ||
| 81 | int result; | ||
| 82 | int pid; | ||
| 83 | |||
| 84 | if ((pid = vfork()) < 0) { | ||
| 85 | bb_perror_msg_and_die("failed to fork"); | ||
| 86 | } else if (!pid) { | ||
| 87 | args[0] = filename; | ||
| 88 | execve(filename, args, env); | ||
| 89 | exec_errno = errno; | ||
| 90 | _exit(1); | ||
| 91 | } | ||
| 92 | |||
| 93 | waitpid(pid, &result, 0); | ||
| 94 | if(exec_errno) { | ||
| 95 | errno = exec_errno; | ||
| 96 | bb_perror_msg("failed to exec %s", filename); | ||
| 97 | exitstatus = 1; | ||
| 98 | } | ||
| 99 | if (WIFEXITED(result) && WEXITSTATUS(result)) { | ||
| 100 | bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); | ||
| 101 | exitstatus = 1; | ||
| 102 | } else if (WIFSIGNALED(result)) { | ||
| 103 | bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); | ||
| 104 | exitstatus = 1; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | else if (!S_ISDIR(st.st_mode)) { | ||
| 109 | bb_error_msg("component %s is not an executable plain file", filename); | ||
| 110 | exitstatus = 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | free(namelist[i]); | ||
| 114 | free(filename); | ||
| 115 | } | ||
| 116 | free(namelist); | ||
| 117 | |||
| 118 | return(exitstatus); | ||
| 119 | } | ||
