diff options
author | Rob Landley <rob@landley.net> | 2006-05-26 03:02:40 +0400 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-05-26 03:02:40 +0400 |
commit | 399d2b5c24aaa059104a89058c7d4c8b97f41629 (patch) | |
tree | 1c6aed143c5dd2ff927190caf83b683c013028cb /libbb | |
parent | 69d863b6c609ac0523c546e408cc5d2063073425 (diff) |
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...)
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xfuncs.c | 34 |
1 files changed, 34 insertions, 0 deletions
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 |