summaryrefslogtreecommitdiff
path: root/libbb/vfork_daemon_rexec.c
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 /libbb/vfork_daemon_rexec.c
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 'libbb/vfork_daemon_rexec.c')
-rw-r--r--libbb/vfork_daemon_rexec.c42
1 files changed, 39 insertions, 3 deletions
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index ff2b0bceb..d25693917 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -16,7 +16,7 @@
16 */ 16 */
17 17
18#include <paths.h> 18#include <paths.h>
19#include "libbb.h" 19#include "busybox.h" /* for struct BB_applet */
20 20
21/* This does a fork/exec in one call, using vfork(). Returns PID of new child, 21/* This does a fork/exec in one call, using vfork(). Returns PID of new child,
22 * -1 for failure. Runs argv[0], searching path if that has no / in it. */ 22 * -1 for failure. Runs argv[0], searching path if that has no / in it. */
@@ -72,7 +72,8 @@ int wait4pid(int pid)
72 int status; 72 int status;
73 73
74 if (pid <= 0) { 74 if (pid <= 0) {
75 /*errno = ECHILD; -- wrong. we expect errno to be set from failed exec */ 75 /*errno = ECHILD; -- wrong. */
76 /* we expect errno to be already set from failed [v]fork/exec */
76 return -1; 77 return -1;
77 } 78 }
78 if (waitpid(pid, &status, 0) == -1) 79 if (waitpid(pid, &status, 0) == -1)
@@ -80,7 +81,7 @@ int wait4pid(int pid)
80 if (WIFEXITED(status)) 81 if (WIFEXITED(status))
81 return WEXITSTATUS(status); 82 return WEXITSTATUS(status);
82 if (WIFSIGNALED(status)) 83 if (WIFSIGNALED(status))
83 return WTERMSIG(status) + 10000; 84 return WTERMSIG(status) + 1000;
84 return 0; 85 return 0;
85} 86}
86 87
@@ -99,6 +100,41 @@ int wait_pid(int *wstat, int pid)
99 return r; 100 return r;
100} 101}
101 102
103int spawn_and_wait(char **argv)
104{
105 int rc;
106
107 if (ENABLE_FEATURE_EXEC_PREFER_APPLETS) {
108 const struct BB_applet *a = find_applet_by_name(argv[0]);
109 if (a && (a->nofork
110#ifndef BB_NOMMU
111 || a->noexec /* NOEXEC cannot be used on NOMMU */
112#endif
113 )) {
114 int argc = 1;
115 char **pp = argv;
116 while (*++pp)
117 argc++;
118#ifdef BB_NOMMU
119 return a->main(argc, argv);
120#else
121 if (a->nofork)
122 return a->main(argc, argv);
123 /* a->noexec is true */
124 rc = fork();
125 if (rc)
126 goto w;
127 /* child */
128 current_applet = a;
129 run_current_applet_and_exit(argc, argv);
130#endif
131 }
132 }
133 rc = spawn(argv);
134 w:
135 return wait4pid(rc);
136}
137
102#if 0 //ndef BB_NOMMU 138#if 0 //ndef BB_NOMMU
103// Die with an error message if we can't daemonize. 139// Die with an error message if we can't daemonize.
104void xdaemon(int nochdir, int noclose) 140void xdaemon(int nochdir, int noclose)