Hi,

Found the issue with sleep execution with bash loadable builtins which
failed to sleep for specified amount of time. The rootcause for the sleep
execution failure found with select() function (in  lib/sh/ufunc.c) which
is interrupted by SIGCHLD signal.

Followings are host, reproduction steps, logs and fix patch.


Bash version:  4.2, 4.4 ( Found in latest bash version)

Host machine: Ubuntu-14.04,  x86_64

Reproduction steps:

# cd bash-4.4;
# ./configure; make; cd examples/loadables/; make;
# cd ../../
# ./bash
(bash shell)

# enable -f examples/loadables/sleep sleep
# date; sleep 1 & date; sleep 4; date

logs: (failure log)
------
bash-4.4-rc1/examples/loadables# date; sleep 1 & date; sleep 4; date
Mon Nov 27 17:00:01 IST 2017
[1] 5355
Mon Nov 27 17:00:01 IST 2017
Mon Nov 27 17:00:02 IST 2017    --------------> Took 1 Sec only instead of
4 Sec's.
[1]+  Done                    sleep 1


Fix patch:
========
An issue fixed by blocking the SIGCHLD signal during sleep process.

Attached the fix patch for your kind reference.


logs: ( with fix patch)
------

bash-4.2#
bash-4.2# date; sleep 1 & date; sleep 6 & date; sleep 4; date
Mon Dec  4 16:04:00 IST 2017
[1] 14674
Mon Dec  4 16:04:00 IST 2017
[2] 14676
Mon Dec  4 16:04:00 IST 2017
Mon Dec  4 16:04:04 IST 2017
[1]-  Done                    sleep 1

bash-4.2#
[2]+  Done                    sleep 6
bash-4.2#

bash-4.2# date; sleep 1 & date; sleep 4; date
Mon Dec  4 16:04:13 IST 2017
[1] 14680
Mon Dec  4 16:04:13 IST 2017
Mon Dec  4 16:04:17 IST 2017
[1]+  Done                    sleep 1
bash-4.2#


Please kindly review the patch and suggest comments.

Thanks,
Thiruvadi Rajaraman
Fix for sleep issue in bash loadable builtins

Replaced select() with pselect() and Blocked the 
SIGCHLD signal with sigprocmask() during the sleep process.

Signed-off-by: Thiruvadi Rajaraman <trajara...@mvista.com>

  
Index: bash-4.2/lib/sh/ufuncs.c
===================================================================
--- bash-4.2.orig/lib/sh/ufuncs.c	2009-01-08 19:30:47.000000000 +0530
+++ bash-4.2/lib/sh/ufuncs.c	2017-12-04 15:53:09.576891443 +0530
@@ -19,7 +19,7 @@
 */
 
 #include "config.h"
-
+#include <signal.h>
 #include "bashtypes.h"
 
 #if defined (TIME_WITH_SYS_TIME)
@@ -86,11 +86,20 @@
      unsigned int sec, usec;
 {
   struct timeval tv;
+  sigset_t nmask, omask;
+  int ret;
+
+  sigemptyset(&nmask);
+  sigaddset(&nmask, SIGCHLD);
+  sigprocmask(SIG_BLOCK, &nmask, &omask);
 
   tv.tv_sec = sec;
   tv.tv_usec = usec;
 
-  return select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
+  ret = pselect(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv, &nmask);
+  sigprocmask(SIG_SETMASK, &omask, NULL);
+
+  return ret;
 }
 #else /* !HAVE_TIMEVAL || !HAVE_SELECT */
 int

Reply via email to