changeset 1327:85f297591693

Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
author Rob Landley <rob@landley.net>
date Sat, 31 May 2014 12:33:24 -0500
parents 78a3eaf5555f
children a498d143be72
files lib/lib.h lib/xwrap.c toys/other/netcat.c toys/other/timeout.c toys/pending/bootchartd.c toys/pending/dhcp.c toys/pending/find.c toys/pending/inotifyd.c toys/pending/nbd_client.c toys/pending/openvt.c toys/pending/tcpsvd.c toys/pending/useradd.c toys/posix/time.c toys/posix/xargs.c
diffstat 14 files changed, 35 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.h	Thu May 29 08:21:48 2014 -0500
+++ b/lib/lib.h	Sat May 31 12:33:24 2014 -0500
@@ -91,6 +91,7 @@
 void xputs(char *s);
 void xputc(char c);
 void xflush(void);
+pid_t xfork(void);
 void xexec_optargs(int skip);
 void xexec(char **argv);
 void xaccess(char *path, int flags);
--- a/lib/xwrap.c	Thu May 29 08:21:48 2014 -0500
+++ b/lib/xwrap.c	Sat May 31 12:33:24 2014 -0500
@@ -113,6 +113,15 @@
   if (fflush(stdout) || ferror(stdout)) perror_exit("write");;
 }
 
+pid_t xfork(void)
+{
+  pid_t pid = fork();
+
+  if (pid < 0) perror_exit("fork");
+
+  return pid;
+}
+
 // Call xexec with a chunk of optargs, starting at skip. (You can't just
 // call xexec() directly because toy_init() frees optargs.)
 void xexec_optargs(int skip)
--- a/toys/other/netcat.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/other/netcat.c	Sat May 31 12:33:24 2014 -0500
@@ -140,7 +140,7 @@
       // Do we need to return immediately because -l has arguments?
 
       if ((toys.optflags & FLAG_l) && toys.optc) {
-        if (fork()) goto cleanup;
+        if (xfork()) goto cleanup;
         close(0);
         close(1);
         close(2);
@@ -149,7 +149,7 @@
       for (;;) {
         pid_t child = 0;
 
-        // For -l, call accept from the _new_ thread.
+        // For -l, call accept from the _new_ process.
 
         pollfds[0].fd = accept(sockfd, (struct sockaddr *)&address, &len);
         if (pollfds[0].fd<0) perror_exit("accept");
--- a/toys/other/timeout.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/other/timeout.c	Sat May 31 12:33:24 2014 -0500
@@ -60,15 +60,14 @@
   if (TT.s_signal && -1 == (TT.nextsig = sig_to_num(TT.s_signal)))
     error_exit("bad -s: '%s'", TT.s_signal);
 
-  if (!(TT.pid = fork())) xexec_optargs(1);
+  if (!(TT.pid = xfork())) xexec_optargs(1);
   else {
     int status;
 
     signal(SIGALRM, handler);
     setitimer(ITIMER_REAL, &TT.itv, (void *)&toybuf);
     while (-1 == waitpid(TT.pid, &status, 0) && errno == EINTR);
-    if (WIFEXITED(status)) toys.exitval = WEXITSTATUS(status);
-    else if (WIFSIGNALED(status)) toys.exitval = WTERMSIG(status);
-    else toys.exitval = 1;
+    toys.exitval = WIFEXITED(status)
+      ? WEXITSTATUS(status) : WTERMSIG(status) + 127;
   }
 }
--- a/toys/pending/bootchartd.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/bootchartd.c	Sat May 31 12:33:24 2014 -0500
@@ -281,7 +281,7 @@
     parse_config_file("/etc/bootchartd.conf");
 
   memset(toybuf, 0, sizeof(toybuf));
-  if (!(lgr_pid = fork())) {
+  if (!(lgr_pid = xfork())) {
     char *tmp_dir = create_tmp_dir();
 
     sigatexit(generic_signal);
@@ -306,7 +306,7 @@
   if (bchartd_opt == 1 && toys.optargs[1]) { 
     pid_t prog_pid;
 
-    if (!(prog_pid = fork())) xexec_optargs(1);
+    if (!(prog_pid = xfork())) xexec_optargs(1);
     waitpid(prog_pid, NULL, 0);
     kill(lgr_pid, SIGUSR1);
   }
--- a/toys/pending/dhcp.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/dhcp.c	Sat May 31 12:33:24 2014 -0500
@@ -318,10 +318,8 @@
 {
   int fd = open("/dev/null", O_RDWR);
   if (fd < 0) fd = xcreate("/", O_RDONLY, 0666);
-  pid_t pid = fork();
 
-  if (pid < 0) perror_exit("DAEMON: failed to fork");
-  if (pid) exit(EXIT_SUCCESS);
+  if (xfork()) exit(0);
 
   setsid();
   dup2(fd, 0);
--- a/toys/pending/find.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/find.c	Sat May 31 12:33:24 2014 -0500
@@ -94,7 +94,7 @@
     arg_array[filter->data.e.arg_path_index] = path;
   }
 
-  if (!(pid = fork())) xexec(arg_array);
+  if (!(pid = xfork())) xexec(arg_array);
   free(path);
   waitpid(pid, &status, 0);
 
--- a/toys/pending/inotifyd.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/inotifyd.c	Sat May 31 12:33:24 2014 -0500
@@ -55,11 +55,10 @@
 static int exec_wait(char **args)
 {
   int status = 0;
-  pid_t pid = fork();
+  pid_t pid = xfork();
 
   if (!pid) xexec(args);
-  else if (pid > 0) waitpid(pid, &status, 0);
-  else perror_exit("fork");
+  else waitpid(pid, &status, 0);
   return WEXITSTATUS(status);
 }
 
--- a/toys/pending/nbd_client.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/nbd_client.c	Sat May 31 12:33:24 2014 -0500
@@ -100,7 +100,7 @@
     if (toys.optflags & FLAG_s) mlockall(MCL_CURRENT|MCL_FUTURE);
 
     // Open the device to force reread of the partition table.
-    if ((toys.optflags & FLAG_n) || !fork()) {
+    if ((toys.optflags & FLAG_n) || !xfork()) {
       char *s = strrchr(device, '/');
       int i;
 
--- a/toys/pending/openvt.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/openvt.c	Sat May 31 12:33:24 2014 -0500
@@ -110,9 +110,8 @@
   while (vt_fd > 2)
     close(vt_fd--);
 
-  pid = vfork();
-  if (pid < 0)  perror_exit("Fork failed");
-  else if (!pid) {
+  pid = xfork();
+  if (!pid) {
     setsid();
     ioctl(vt_fd, TIOCSCTTY, 0);
     xexec(toys.optargs);
--- a/toys/pending/tcpsvd.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/tcpsvd.c	Sat May 31 12:33:24 2014 -0500
@@ -14,7 +14,7 @@
   default n
   help
     usage: tcpsvd [-hEv] [-c N] [-C N[:MSG]] [-b N] [-u User] [-l Name] IP Port Prog
-    udpsvd [-hEv] [-c N] [-u User] [-l Name] IP Port Prog
+    usage: udpsvd [-hEv] [-c N] [-u User] [-l Name] IP Port Prog
     
     Create TCP/UDP socket, bind to IP:PORT and listen for incoming connection. 
     Run PROG for each connection.
@@ -350,7 +350,7 @@
       h[hash].head->count++;
     }
 
-    if (!(pid = fork())) {
+    if (!(pid = xfork())) {
       char *serv = NULL, *clie = NULL;
       char *client = sock_to_address((struct sockaddr*)buf, NI_NUMERICHOST | NI_NUMERICSERV);
       if (toys.optflags & FLAG_h) { //lookup name
@@ -393,11 +393,11 @@
       dup2(newfd, 0);
       dup2(newfd, 1);
       xexec_optargs(2); //skip IP PORT
-    } else if(pid > 0) {
+    } else {
       insert(&pids, pid, addr);
       xclose(newfd); //close and reopen for next client.
       if (TT.udp) fd = create_bind_sock(toys.optargs[0],
           (struct sockaddr*)&haddr);
-    } else error_msg(" fork failed");
+    }
   } //while(1)
 }
--- a/toys/pending/useradd.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/pending/useradd.c	Sat May 31 12:33:24 2014 -0500
@@ -58,12 +58,12 @@
 static int exec_wait(char **args)
 {
   int status = 0;
-  pid_t pid = fork();
+  pid_t pid = xfork();
 
   if (!pid) xexec(args);
-  else if (pid > 0) waitpid(pid, &status, 0);
-  else perror_exit("fork failed");
-  return WEXITSTATUS(status);
+  else if waitpid(pid, &status, 0);
+
+  return WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
 }
 
 /* create_copy_skel(), This function will create the home directory of the
--- a/toys/posix/time.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/posix/time.c	Sat May 31 12:33:24 2014 -0500
@@ -27,7 +27,7 @@
   struct timeval tv, tv2;
 
   gettimeofday(&tv, NULL);
-  if (!(pid = fork())) xexec_optargs(0);
+  if (!(pid = xfork())) xexec_optargs(0);
   else {
     int stat;
     struct rusage ru;
--- a/toys/posix/xargs.c	Thu May 29 08:21:48 2014 -0500
+++ b/toys/posix/xargs.c	Sat May 31 12:33:24 2014 -0500
@@ -166,14 +166,14 @@
     for (dtemp = dlist; dtemp; dtemp = dtemp->next)
       handle_entries(dtemp->data, out+entries);
 
-    pid_t pid=fork();
+    pid_t pid=xfork();
     if (!pid) {
       xclose(0);
       open("/dev/null", O_RDONLY);
       xexec(out);
     }
     waitpid(pid, &status, 0);
-    status = WEXITSTATUS(status);
+    status = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)+127;
 
     // Abritrary number of execs, can't just leak memory each time...
     while (dlist) {