From 399d2b5c24aaa059104a89058c7d4c8b97f41629 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 25 May 2006 23:02:40 +0000 Subject: Rich Felker suggested removing dprintf() from watch, and one thing led to another... This adds bb_xspawn() support, which does vfork/exec. (I don't know why using a static instead of a local adds ~40 bytes, but using the local doesn't work...) --- libbb/xfuncs.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'libbb/xfuncs.c') diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index d3c9e41e1..2cfafb01a 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -182,3 +182,37 @@ void bb_xfflush_stdout(void) } } #endif + +#ifdef L_spawn +// This does a fork/exec in one call, using vfork(). +pid_t bb_spawn(char **argv) +{ + static int failed; + pid_t pid; + + // Be nice to nommu machines. + failed = 0; + pid = vfork(); + if (pid < 0) return pid; + if (!pid) { + execvp(*argv, argv); + + // We're sharing a stack with blocked parent, let parent know we failed + // and then exit to unblock parent (but don't run atexit() stuff, which + // would screw up parent.) + + failed = -1; + _exit(0); + } + return failed ? failed : pid; +} +#endif + +#ifdef L_xspawn +pid_t bb_xspawn(char **argv) +{ + pid_t pid = bb_spawn(argv); + if (pid < 0) bb_perror_msg_and_die("%s", *argv); + return pid; +} +#endif -- cgit v1.2.3