diff options
Diffstat (limited to 'busybox/libbb/run_parts.c')
-rw-r--r-- | busybox/libbb/run_parts.c | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/busybox/libbb/run_parts.c b/busybox/libbb/run_parts.c new file mode 100644 index 000000000..4c8841fe5 --- /dev/null +++ b/busybox/libbb/run_parts.c | |||
@@ -0,0 +1,126 @@ | |||
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 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation; either version 2 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * | ||
16 | */ | ||
17 | |||
18 | |||
19 | #include <sys/types.h> | ||
20 | #include <sys/wait.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <dirent.h> | ||
23 | #include <unistd.h> | ||
24 | #include <ctype.h> | ||
25 | #include <errno.h> | ||
26 | |||
27 | #include "libbb.h" | ||
28 | |||
29 | /* valid_name */ | ||
30 | /* True or false? Is this a valid filename (upper/lower alpha, digits, | ||
31 | * underscores, and hyphens only?) | ||
32 | */ | ||
33 | static int valid_name(const struct dirent *d) | ||
34 | { | ||
35 | char *c = d->d_name; | ||
36 | |||
37 | while (*c) { | ||
38 | if (!isalnum(*c) && (*c != '_') && (*c != '-')) { | ||
39 | return 0; | ||
40 | } | ||
41 | ++c; | ||
42 | } | ||
43 | return 1; | ||
44 | } | ||
45 | |||
46 | /* test mode = 1 is the same as official run_parts | ||
47 | * test_mode = 2 means to fail silently on missing directories | ||
48 | */ | ||
49 | |||
50 | extern int run_parts(char **args, const unsigned char test_mode, char **env) | ||
51 | { | ||
52 | struct dirent **namelist = 0; | ||
53 | struct stat st; | ||
54 | char *filename; | ||
55 | char *arg0 = args[0]; | ||
56 | int entries; | ||
57 | int i; | ||
58 | int exitstatus = 0; | ||
59 | |||
60 | #if __GNUC__ | ||
61 | /* Avoid longjmp clobbering */ | ||
62 | (void) &i; | ||
63 | (void) &exitstatus; | ||
64 | #endif | ||
65 | /* scandir() isn't POSIX, but it makes things easy. */ | ||
66 | entries = scandir(arg0, &namelist, valid_name, alphasort); | ||
67 | |||
68 | if (entries == -1) { | ||
69 | if (test_mode & 2) { | ||
70 | return(2); | ||
71 | } | ||
72 | bb_perror_msg_and_die("failed to open directory %s", arg0); | ||
73 | } | ||
74 | |||
75 | for (i = 0; i < entries; i++) { | ||
76 | |||
77 | filename = concat_path_file(arg0, namelist[i]->d_name); | ||
78 | |||
79 | if (stat(filename, &st) < 0) { | ||
80 | bb_perror_msg_and_die("failed to stat component %s", filename); | ||
81 | } | ||
82 | if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { | ||
83 | if (test_mode) { | ||
84 | puts(filename); | ||
85 | } else { | ||
86 | /* exec_errno is common vfork variable */ | ||
87 | volatile int exec_errno = 0; | ||
88 | int result; | ||
89 | int pid; | ||
90 | |||
91 | if ((pid = vfork()) < 0) { | ||
92 | bb_perror_msg_and_die("failed to fork"); | ||
93 | } else if (!pid) { | ||
94 | args[0] = filename; | ||
95 | execve(filename, args, env); | ||
96 | exec_errno = errno; | ||
97 | _exit(1); | ||
98 | } | ||
99 | |||
100 | waitpid(pid, &result, 0); | ||
101 | if(exec_errno) { | ||
102 | errno = exec_errno; | ||
103 | bb_perror_msg("failed to exec %s", filename); | ||
104 | exitstatus = 1; | ||
105 | } | ||
106 | if (WIFEXITED(result) && WEXITSTATUS(result)) { | ||
107 | bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); | ||
108 | exitstatus = 1; | ||
109 | } else if (WIFSIGNALED(result)) { | ||
110 | bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); | ||
111 | exitstatus = 1; | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | else if (!S_ISDIR(st.st_mode)) { | ||
116 | bb_error_msg("component %s is not an executable plain file", filename); | ||
117 | exitstatus = 1; | ||
118 | } | ||
119 | |||
120 | free(namelist[i]); | ||
121 | free(filename); | ||
122 | } | ||
123 | free(namelist); | ||
124 | |||
125 | return(exitstatus); | ||
126 | } | ||