Hello, the shell script

    #!/bin/bash
    #
    
    sh -c "sleep 2; echo before; exit 7; echo after" &
    while true; do
        echo waiting ...
        wait -n
        st=$?
        echo $st
        [ "$st" == 127 ]  &&  break
    done

prints with bash

    $ bash test.sh | head -10
    waiting ...
    before
    7
    waiting ...
    127

but busybox-1.37 ash (from github) delivers

    $ busybox ash test.sh | head -10
    waiting ...
    before
    129
    waiting ...
    0
    waiting ...
    0
    waiting ...
    0
    waiting ...

There are two bugs here:

 1. `129` is not the return code from the exited child.
 2. `wait -n` should return `127` when no child/job is
    running.

The following patch should fix both issues.


diff --git a/a/shell/ash.c b/b/shell/ash.c
index bbd7307..38fcec8 100644
--- a/a/shell/ash.c
+++ b/b/shell/ash.c
@@ -4464,6 +4464,11 @@ dowait(int block, struct job *jp)
        do {
                pid = waitone(block, jp);
                rpid &= !!pid;
+#if BASH_WAIT_N                /* Patch 1: Make `wait -n` return exit status. 
*/
+       if (pid >= 0 && block & DOWAIT_JOBSTATUS)
+               /* Return the status when waiting for one job/child. */
+               return (pid);
+#endif
 
                if (!pid || (jp && jp->state != JOBRUNNING))
                        block = DOWAIT_NONBLOCK;
@@ -4652,14 +4657,16 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
                /* wait for all jobs / one job if -n */
                for (;;) {
                        jp = curjob;
+                       while (1) {
+                               /* Patch 2: Make `wait -n` return 127 is no job 
are running. */
+                               if (!jp) { /* no running procs */
 #if BASH_WAIT_N
-                       if (one && !jp)
-                               /* exitcode of "wait -n" with nothing to wait 
for is 127, not 0 */
-                               retval = 127;
+                                       if (one)
+                                               /* exitcode of "wait -n" with 
nothing to wait for is 127, not 0 */
+                                               retval = 127;
 #endif
-                       while (1) {
-                               if (!jp) /* no running procs */
                                        goto ret;
+                               }
                                if (jp->state == JOBRUNNING)
                                        break;
                                jp->waited = 1;
--
_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to