diff options
author | Rob Landley <rob@landley.net> | 2014-06-03 06:27:24 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2014-06-03 06:27:24 -0500 |
commit | 44e68a1bec9b342c01e2f4e6ccf9f1e7a3ee8081 (patch) | |
tree | ca7b2ec935cba0625fe1dfe331aa0375864bb8e3 /lib/xwrap.c | |
parent | 314f19e4d2099eb19824df49d71953946486f78f (diff) | |
download | toybox-44e68a1bec9b342c01e2f4e6ccf9f1e7a3ee8081.tar.gz toybox-44e68a1bec9b342c01e2f4e6ccf9f1e7a3ee8081.tar.bz2 toybox-44e68a1bec9b342c01e2f4e6ccf9f1e7a3ee8081.zip |
mount: start on option parsing, implement loopback and bind mount autodetection.
Diffstat (limited to 'lib/xwrap.c')
-rw-r--r-- | lib/xwrap.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/xwrap.c b/lib/xwrap.c index 3e146fb..7e495ca 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -143,6 +143,67 @@ void xexec(char **argv) perror_exit("exec %s", argv[0]); } +// Spawn child process, capturing stdin/stdout. +// argv[]: command to exec. If null, child returns to original program. +// pipes[]: stdin, stdout of new process. If null, block and wait for child. +// return: pid of child process +pid_t xpopen(char **argv, int *pipes) +{ + int cestnepasun[4], pid; + + // Make the pipes? + if (pipes) { + if (pipe(cestnepasun) || pipe(cestnepasun+2)) perror_exit("pipe"); + pipes[0] = cestnepasun[1]; + pipes[1] = cestnepasun[2]; + } + + // Child process + if (!(pid = xfork())) { + // Dance of the stdin/stdout redirection. + if (pipes) { + close(cestnepasun[1]); + close(cestnepasun[2]); + // if we had no stdin/out, pipe handles could overlap, so test for that + if (cestnepasun[0]) { + dup2(cestnepasun[0], 0); + close(cestnepasun[0]); + } + dup2(cestnepasun[3], 1); + dup2(cestnepasun[3], 2); + if (cestnepasun[3] > 2) close(cestnepasun[3]); + } + if (argv) { + if (CFG_TOYBOX) toy_exec(argv); + execvp(argv[0], argv); + _exit(127); + } + return 0; + + // Parent process + } else { + if (pipes) { + close(cestnepasun[0]); + close(cestnepasun[3]); + } + + return pid; + } +} + +int xpclose(pid_t pid, int *pipes) +{ + int rc = 127; + + if (pipes) { + close(pipes[0]); + close(pipes[1]); + } + waitpid(pid, &rc, 0); + + return WIFEXITED(rc) ? WEXITSTATUS(rc) : WTERMSIG(rc) + 127; +} + void xaccess(char *path, int flags) { if (access(path, flags)) perror_exit("Can't access '%s'", path); |