diff options
author | JozanLeClerc <JozanLeClerc@noemail.net> | 2020-11-26 22:49:44 +0000 |
---|---|---|
committer | JozanLeClerc <JozanLeClerc@noemail.net> | 2020-11-26 22:49:44 +0000 |
commit | 61a0d117878ace757d62b4af35fcc8de5b981a34 (patch) | |
tree | fe5bb7142ddce9131e2bd39c5c69c7363a371697 | |
parent | In progress (diff) | |
download | bsdsetsid-61a0d117878ace757d62b4af35fcc8de5b981a34.tar.gz bsdsetsid-61a0d117878ace757d62b4af35fcc8de5b981a34.tar.bz2 bsdsetsid-61a0d117878ace757d62b4af35fcc8de5b981a34.tar.xz bsdsetsid-61a0d117878ace757d62b4af35fcc8de5b981a34.tar.zst bsdsetsid-61a0d117878ace757d62b4af35fcc8de5b981a34.zip |
In progress
FossilOrigin-Name: c044b82560a01287a9c768217a04c79b04f39e77
-rw-r--r-- | src/c_fork.c | 100 | ||||
-rw-r--r-- | src/c_fork.h | 9 |
2 files changed, 80 insertions, 29 deletions
diff --git a/src/c_fork.c b/src/c_fork.c index 7709bd6..b74d8bc 100644 --- a/src/c_fork.c +++ b/src/c_fork.c @@ -39,12 +39,17 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * bsdsetsid: src/c_fork.c - * Thu Nov 26 22:12:40 CET 2020 + * Thu Nov 26 23:40:15 CET 2020 * Joe * * The program's main fork(2). */ +#include <sys/types.h> +#include <sys/wait.h> +#include <sys/time.h> +#include <sys/resource.h> + #include <errno.h> #include <stdlib.h> #include <stdio.h> @@ -52,54 +57,93 @@ #include <unistd.h> #include "c_bsdsetsid.h" +#include "c_fork.h" -void -c_fork +static void +c_fork_child (const char* argv[], const char* envp[], bool_t wopt) { - int sets_ret; - int exec_ret; - pid_t pid; + union ret_u u; - pid = fork(); - if (pid == -1) { + u.pid = setsid(); + if (u.pid == -1) { dprintf( STDERR_FILENO, - "%s: fork: %s\n", + "%s: setsid: %s\n", C_PROGNAME, strerror(errno) ); exit(EXIT_FAILURE); } - else if (pid == 0) { - sets_ret = setsid(); - if (sets_ret == -1) { - dprintf( - STDERR_FILENO, - "%s: setsid: %s\n", - C_PROGNAME, - strerror(errno) - ); - exit(EXIT_FAILURE); - } - exec_ret = execve( - argv[1 + wopt], - (char* const*)argv + (1 + wopt), - (char* const*)envp + u.ret = execve( + argv[1 + wopt], + (char* const*)argv + (1 + wopt), + (char* const*)envp + ); + if (u.ret == -1) { + dprintf( + STDERR_FILENO, + "%s: execve: %s\n", + C_PROGNAME, + strerror(errno) ); - if (exec_ret == -1) { + exit(EXIT_FAILURE); + } +} + +static void +c_fork_parent +(bool_t wopt, + pid_t pid) +{ + union ret_u u; + int status; + + if (wopt == TRUE) { + u.pid = wait4(pid, &status, 0, NULL); + if (u.pid < 0) { dprintf( STDERR_FILENO, - "%s: execve: %s\n", + "%s: wait4: %s\n", C_PROGNAME, strerror(errno) ); - exit(EXIT_FAILURE); } + exit(WEXITSTATUS(status)); + } + exit(EXIT_SUCCESS); +} + +static void +c_fork_error(void) +{ + dprintf( + STDERR_FILENO, + "%s: fork: %s\n", + C_PROGNAME, + strerror(errno) + ); + exit(EXIT_FAILURE); +} + +void +c_fork +(const char* argv[], + const char* envp[], + bool_t wopt) +{ + pid_t pid; + + pid = fork(); + if (pid == -1) { + c_fork_error(); + } + else if (pid == 0) { + c_fork_child(argv, envp, wopt); } else { - exit(EXIT_SUCCESS); + c_fork_parent(wopt, pid); } } diff --git a/src/c_fork.h b/src/c_fork.h index dfc154c..18e059b 100644 --- a/src/c_fork.h +++ b/src/c_fork.h @@ -39,15 +39,22 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * bsdsetsid: src/c_fork.h - * Thu Nov 26 22:09:56 CET 2020 + * Thu Nov 26 23:39:54 CET 2020 * Joe */ #ifndef __C_FORK_H__ #define __C_FORK_H__ +#include <sys/types.h> + #include "c_bsdsetsid.h" +union ret_u { + pid_t pid; + int ret; +}; + void c_fork(const char*[], const char*[], bool_t); #endif |