waitproc: unobfuscate and explain what the code does
PATCH 1/2 waitproc: unobfuscate and explain what the code does
Also, the #ifdef'ed out waitpid() has extra argument, fix that.
Signed-off-by Denys Vlasenko <[email protected]>
--- src/jobs.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
src/jobs.c +14 -6
@@ -1175,8 +1175,8 @@ static int dowait(int block, struct job *jp) }/* -* Do a wait system call. If block is zero, we return -1 rather than -* blocking. If block is DOWAIT_WAITCMD, we return 0 when a signal +* Do a wait system call. If block is DOWAIT_NONBLOCK, we return -1 rather +* than blocking. If block is DOWAIT_WAITCMD, we return 0 when a signal * other than SIGCHLD interrupted the wait. * * We use sigsuspend in conjunction with a non-blocking wait3 in
@@ -1189,7 +1189,6 @@ static int dowait(int block, struct job *jp)* children have been reaped. Otherwise zombies may linger. */ - STATIC intwaitproc (int block,int *status) {
@@ -1208,12 +1207,15 @@ waitproc(int block, int *status)#ifdef HAVE_WAIT3 err =wait3 (status, flags,NULL );#else - err =waitpid ((pid_t)-1 , status, flags,NULL ); + err =waitpid ((pid_t)-1 , status, flags);#endif while (err <0 && errno ==EINTR ); -if (err || (err = -!block)) -break ; +if (err)/* wait3() returned PID or error? */ +break ;/* return it */ + err = -!block; +if (err)/* block == DOWAIT_NONBLOCK (0)? */ +break ;/* return -1 */ sigblockall (&oldmask);
@@ -1223,6 +1225,12 @@ waitproc(int block, int *status)sigclearmask (); }while (gotsigchld); +/* Possible returns: +* PID of successfully waited-for child. +* -1 on wait3() error (can get ECHILD: no one to wait for) +* -1 if DOWAIT_NONBLOCK +* 0 if there were no waitable children and got a signal (not SIGCHLD) +*/ return err; }
Signature · 1 line
2.52.0
PATCH 2/2 Reorder wait-related functions. Pure block move, no code changes
Palcing them in the "callees first" order:
waitproc() {...}waitone() {...} // calls waitprocdowait() {...} // calls waitone
instead of current semi-random one:
waitone() {...}dowait() {...}waitproc() {...}
Signed-off-by Denys Vlasenko <[email protected]>
--- src/jobs.c | 121 ++++++++++++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 61 deletions(-)
src/jobs.c +60 -61
@@ -116,7 +116,6 @@ STATIC int dowait(int, struct job *);#ifdef SYSV STATIC int onsigchild(void);#endif -STATIC int waitproc(int, int *); STATIC char *commandtext(union node *); STATIC void cmdtxt(union node *); STATIC void cmdlist(union node *, int);
@@ -1013,6 +1012,66 @@ struct job *vforkexec(union node *n, char **argv, const char *path, int idx)return jp; } +/* +* Do a wait system call. If block is DOWAIT_NONBLOCK, we return -1 rather +* than blocking. If block is DOWAIT_WAITCMD, we return 0 when a signal +* other than SIGCHLD interrupted the wait. +* +* We use sigsuspend in conjunction with a non-blocking wait3 in +* order to ensure that waitcmd exits promptly upon the reception +* of a signal. +* +* For code paths other than waitcmd we either use a blocking wait3 +* or a non-blocking wait3. For the latter case the caller of dowait +* must ensure that it is called over and over again until all dead +* children have been reaped. Otherwise zombies may linger. +*/ + +STATIC int +waitproc (int block,int *status) +{ +sigset_t oldmask; +int flags = block ==DOWAIT_BLOCK ?0 :WNOHANG ; +int err; + +#if JOBS +if (jobctl) + flags |=WUNTRACED ; +#endif + +do { + gotsigchld =0 ; + do +#ifdef HAVE_WAIT3 + err =wait3 (status, flags,NULL ); +#else + err =waitpid ((pid_t)-1 , status, flags); +#endif +while (err <0 && errno ==EINTR ); + +if (err)/* wait3() returned PID or error? */ +break ;/* return it */ + err = -!block; +if (err)/* block == DOWAIT_NONBLOCK (0)? */ +break ;/* return -1 */ + +sigblockall (&oldmask); + +while (!gotsigchld && !pending_sig) +sigsuspend (&oldmask); + +sigclearmask (); + }while (gotsigchld); + +/* Possible returns: +* PID of successfully waited-for child. +* -1 on wait3() error (can get ECHILD: no one to wait for) +* -1 if DOWAIT_NONBLOCK +* 0 if there were no waitable children and got a signal (not SIGCHLD) +*/ +return err; +} +/* * Wait for job to finish. *
@@ -1174,66 +1233,6 @@ static int dowait(int block, struct job *jp)return rpid; } -/* -* Do a wait system call. If block is DOWAIT_NONBLOCK, we return -1 rather -* than blocking. If block is DOWAIT_WAITCMD, we return 0 when a signal -* other than SIGCHLD interrupted the wait. -* -* We use sigsuspend in conjunction with a non-blocking wait3 in -* order to ensure that waitcmd exits promptly upon the reception -* of a signal. -* -* For code paths other than waitcmd we either use a blocking wait3 -* or a non-blocking wait3. For the latter case the caller of dowait -* must ensure that it is called over and over again until all dead -* children have been reaped. Otherwise zombies may linger. -*/ - -STATIC int -waitproc (int block,int *status) -{ -sigset_t oldmask; -int flags = block ==DOWAIT_BLOCK ?0 :WNOHANG ; -int err; - -#if JOBS -if (jobctl) - flags |=WUNTRACED ; -#endif - -do { - gotsigchld =0 ; - do -#ifdef HAVE_WAIT3 - err =wait3 (status, flags,NULL ); -#else - err =waitpid ((pid_t)-1 , status, flags); -#endif -while (err <0 && errno ==EINTR ); - -if (err)/* wait3() returned PID or error? */ -break ;/* return it */ - err = -!block; -if (err)/* block == DOWAIT_NONBLOCK (0)? */ -break ;/* return -1 */ - -sigblockall (&oldmask); - -while (!gotsigchld && !pending_sig) -sigsuspend (&oldmask); - -sigclearmask (); - }while (gotsigchld); - -/* Possible returns: -* PID of successfully waited-for child. -* -1 on wait3() error (can get ECHILD: no one to wait for) -* -1 if DOWAIT_NONBLOCK -* 0 if there were no waitable children and got a signal (not SIGCHLD) -*/ -return err; -} -/* * return 1 if there are stopped jobs, otherwise 0 */
Signature · 1 line
2.52.0