aboutsummaryrefslogtreecommitdiff
path: root/findutils
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-09 21:32:30 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-09 21:32:30 +0000
commitcd7001f7055c3fc2d6298ab9e3befe91e951c652 (patch)
treeb9509ed21e0a7af26128b796a66a3294ae4dc5b0 /findutils
parent1b4b2cb20e5291c319ce0c7721e64445e2749b10 (diff)
downloadbusybox-w32-cd7001f7055c3fc2d6298ab9e3befe91e951c652.tar.gz
busybox-w32-cd7001f7055c3fc2d6298ab9e3befe91e951c652.tar.bz2
busybox-w32-cd7001f7055c3fc2d6298ab9e3befe91e951c652.zip
factor out NOFORK/NOEXEC code from find. Use it for xargs too.
Diffstat (limited to 'findutils')
-rw-r--r--findutils/find.c32
-rw-r--r--findutils/xargs.c35
2 files changed, 17 insertions, 50 deletions
diff --git a/findutils/find.c b/findutils/find.c
index 7b5a09d56..1a1301b38 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -238,37 +238,19 @@ ACTF(inum)
238ACTF(exec) 238ACTF(exec)
239{ 239{
240 int i, rc; 240 int i, rc;
241 char *argv[ap->exec_argc+1]; 241 char *argv[ap->exec_argc + 1];
242 for (i = 0; i < ap->exec_argc; i++) 242 for (i = 0; i < ap->exec_argc; i++)
243 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); 243 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
244 argv[i] = NULL; /* terminate the list */ 244 argv[i] = NULL; /* terminate the list */
245 245
246 if (ENABLE_FEATURE_EXEC_PREFER_APPLETS) { 246 rc = spawn_and_wait(argv);
247 const struct BB_applet *a = find_applet_by_name(argv[0]);
248 if (a) {
249 if (a->nofork) {
250 rc = a->main(ap->exec_argc, argv);
251 goto f;
252 }
253#ifndef BB_NOMMU
254 if (a->noexec) {
255 rc = fork();
256 if (rc) goto w;
257 current_applet = a;
258 run_current_applet_and_exit(ap->exec_argc, argv);
259 }
260#endif
261 }
262 }
263 rc = spawn(argv);
264 w:
265 rc = wait4pid(rc);
266 if (rc < 0) 247 if (rc < 0)
267 bb_perror_msg("%s", argv[0]); 248 bb_perror_msg("%s", argv[0]);
268 f: 249
269 for (i = 0; i < ap->exec_argc; i++) 250 i = 0;
270 free(argv[i]); 251 while (argv[i])
271 return rc == 0; /* return 1 if success */ 252 free(argv[i++]);
253 return rc == 0; /* return 1 if exitcode 0 */
272} 254}
273#endif 255#endif
274 256
diff --git a/findutils/xargs.c b/findutils/xargs.c
index ea7c22060..b4dd9f876 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -48,47 +48,32 @@
48 This function has special algorithm. 48 This function has special algorithm.
49 Don't use fork and include to main! 49 Don't use fork and include to main!
50*/ 50*/
51static int xargs_exec(char *const *args) 51static int xargs_exec(char **args)
52{ 52{
53 pid_t p;
54 volatile int exec_errno = 0; /* shared vfork stack */
55 int status; 53 int status;
56 54
57 p = vfork(); 55 status = spawn_and_wait(args);
58 if (p < 0) 56 if (status < 0) {
59 bb_perror_msg_and_die("vfork");
60
61 if (p == 0) {
62 /* vfork -- child */
63 BB_EXECVP(args[0], args);
64 exec_errno = errno; /* set error to shared stack */
65 _exit(1);
66 }
67
68 /* vfork -- parent */
69 while (wait(&status) == (pid_t) -1)
70 if (errno != EINTR)
71 break;
72 if (exec_errno) {
73 errno = exec_errno;
74 bb_perror_msg("%s", args[0]); 57 bb_perror_msg("%s", args[0]);
75 return exec_errno == ENOENT ? 127 : 126; 58 return errno == ENOENT ? 127 : 126;
76 } 59 }
77 if (WEXITSTATUS(status) == 255) { 60 if (status == 255) {
78 bb_error_msg("%s: exited with status 255; aborting", args[0]); 61 bb_error_msg("%s: exited with status 255; aborting", args[0]);
79 return 124; 62 return 124;
80 } 63 }
64/* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
81 if (WIFSTOPPED(status)) { 65 if (WIFSTOPPED(status)) {
82 bb_error_msg("%s: stopped by signal %d", 66 bb_error_msg("%s: stopped by signal %d",
83 args[0], WSTOPSIG(status)); 67 args[0], WSTOPSIG(status));
84 return 125; 68 return 125;
85 } 69 }
86 if (WIFSIGNALED(status)) { 70*/
71 if (status >= 1000) {
87 bb_error_msg("%s: terminated by signal %d", 72 bb_error_msg("%s: terminated by signal %d",
88 args[0], WTERMSIG(status)); 73 args[0], status - 1000);
89 return 125; 74 return 125;
90 } 75 }
91 if (WEXITSTATUS(status)) 76 if (status)
92 return 123; 77 return 123;
93 return 0; 78 return 0;
94} 79}