Re: Re: [here string] uncompatible change on here string function

2017-11-27 Thread Greg Wooledge
On Fri, Nov 24, 2017 at 11:22:56AM +0800, 尹剑虹 wrote:
> It's not the point. the problem is bash-4.4 auto add quote around  $var or 
> $(cmd)  after '<<<'
> 
> On bash-4.2 and before  following command always works fine
>   read ignore fstype <<<$(df --output=fstype $mountpoint)
> 
> But after Update bash-4.4, our customer's script get fail, do you understand 
> what I mean?

The script is broken.  It happened to work in bash 4.2 because of a bug
in bash 4.2 that cancelled out the bug in the script.

wooledg:~$ df --output=fstype /
Type
ext4

Here is how the script should be written:

{ read; read -r fstype; } < <(df --output=fstype "$mountpoint")

And testing it:

wooledg:~$ mountpoint=/
wooledg:~$ { read; read -r fstype; } < <(df --output=fstype "$mountpoint")
wooledg:~$ declare -p fstype
declare -- fstype="ext4"



Bash: sleep execution issue with bash loadable builtins

2017-11-27 Thread Thiruvadi Rajaraman
Hi,

Found a 'sleep' execution issue with bash loadable builtins and has
performed
the below sleep test on bash-4.4-rc1.

Bash version: 4.4-rc1
Web link: https://ftp.gnu.org/gnu/bash/bash-4.4-rc1.tar.gz

Reproducible test case and Console logs:


bash-4.4-rc1#./bash
bash-4.4-rc1# cd examples/loadables/
bash-4.4-rc1/examples/loadables# enable -f ./sleep sleep

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]+  Donesleep 1


bash-4.4-rc1/examples/loadables#
bash-4.4-rc1/examples/loadables# date; sleep 4 & date; sleep 1; date
Mon Nov 27 17:00:16 IST 2017
[1] 5386
Mon Nov 27 17:00:16 IST 2017
Mon Nov 27 17:00:17 IST 2017


bash-4.4-rc1/examples/loadables#
bash-4.4-rc1/examples/loadables# date; sleep 1 & date; sleep 10; date
Mon Nov 27 17:00:29 IST 2017
[1]+  Donesleep 4
[1] 5390
Mon Nov 27 17:00:29 IST 2017
Mon Nov 27 17:00:30 IST 2017 -> Took 1 Sec only instead of
10 Sec's.
[1]+  Donesleep 1
root@cavium-Vostro-3446
:~/Desktop/bugzilla-CGX/77504/bash-4.4-rc1/examples/loadables#

Fix patch:
==
# Attached the fix patch -
Fix_for_bash_loadable_builtin_sleep_execution_issue.patch


Please kindly review and suggest your comments.

Thanks,
Thiruvadi Rajaraman

Fix for bash loadable builtins 'sleep' execution failure issue

The rootcause of the failure was found with select() execution 
with time information in fsleep() function in lib/sh/ufunc.c file.

The fix patch content resolves the issue.

Test logs (After applying patch)
=
 
# date; sleep 1 & date; sleep 10; date

Mon Nov 27 17:25:31 IST 2017
[1] 13173
Mon Nov 27 17:25:31 IST 2017
Mon Nov 27 17:25:41 IST 2017-> Took 10 Sec's sleep 
[1]+  Donesleep 1

#

Signed-off-by: Thiruvadi Rajaraman  

Index: bash-4.4-rc1/lib/sh/ufuncs.c
===
--- bash-4.4-rc1.orig/lib/sh/ufuncs.c	2017-11-27 13:57:18.172796871 +0530
+++ bash-4.4-rc1/lib/sh/ufuncs.c	2017-11-27 17:21:13.622624417 +0530
@@ -90,11 +90,16 @@
  unsigned int sec, usec;
 {
   struct timeval tv;
+  int ret = 0;
 
   tv.tv_sec = sec;
   tv.tv_usec = usec;
 
-  return select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
+  do {
+	ret = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
+  } while ( (ret == -1));
+  
+  return ret;
 }
 #else /* !HAVE_TIMEVAL || !HAVE_SELECT */
 int


Feature idea

2017-11-27 Thread PePa
Recently I wanted to put the content of the stdout and the stderr of a 
command into variables (without needing to write to a file), something 
like this:


sterr=$(cmd >>>stout)

Or:

stout=$(cmd 2>>>sterr)

Or even:

cmd >>>stout 2>>>sterr

Obviously the idea is to introduce an operator >>> that functions as a 
"reverse here-document" that stores the content of a file stream into a 
variable. There would be less need to store things in a file on a 
filesystem.


I think that would be very useful extension, easy to comprehend in the 
light of current syntax, and not clashing with anything as far as I can see.


Thanks for considering this,
Peter



Re: [here string] uncompatible change on here string function

2017-11-27 Thread Vladimir Marek
> > It's not the point. the problem is bash-4.4 auto add quote around  $var or 
> > $(cmd)  after '<<<'
> > 
> > On bash-4.2 and before  following command always works fine
> >   read ignore fstype <<<$(df --output=fstype $mountpoint)
> > 
> > But after Update bash-4.4, our customer's script get fail, do you 
> > understand what I mean?
> 
> The script is broken.  It happened to work in bash 4.2 because of a bug
> in bash 4.2 that cancelled out the bug in the script.
> 
> wooledg:~$ df --output=fstype /
> Type
> ext4
> 
> Here is how the script should be written:
> 
> { read; read -r fstype; } < <(df --output=fstype "$mountpoint")
> 
> And testing it:
> 
> wooledg:~$ mountpoint=/
> wooledg:~$ { read; read -r fstype; } < <(df --output=fstype "$mountpoint")
> wooledg:~$ declare -p fstype
> declare -- fstype="ext4"

Or even
fstype=$( stat --file-system --print "%T\n" "$mountpoint" )

-- 
Vlad



Re: Bash: sleep execution issue with bash loadable builtins

2017-11-27 Thread Chet Ramey
On 11/27/17 4:17 AM, Thiruvadi Rajaraman wrote:
> Hi,
> 
> Found a 'sleep' execution issue with bash loadable builtins and has
> performed
> the below sleep test on bash-4.4-rc1.

That's an interesting one. It looks like the SIGCHLD interrupts the select
loop, even though bash supplies SA_RESTART when installing its SIGCHLD
handler.  It's probably too hard to restart it in general, since select
doesn't necessarily modify its timeval argument when it returns early
(Linux does; many other OSs do not).

There is a problem with your fix in that, in most cases, you've just made
everything that uses this function non-interruptible, especially in an
interactive shell. I think a better fix would be to change fsleep() to cope
with select(2) being interrupted using the bash primitives that deal with
signal and trap handling.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: Bash: sleep execution issue with bash loadable builtins

2017-11-27 Thread Ángel
On 2017-11-27 at 17:47 +0530, Thiruvadi Rajaraman wrote:
> Reproducible test case and Console logs:
> 

Simpler test case:
bash-4.4-rc1# cd examples/loadables/
bash-4.4-rc1/examples/loadables# enable -f ./sleep sleep
bash-4.4-rc1/examples/loadables# sleep 1 & time sleep 10
[1] 8892

real0m1.005s
user0m0.001s
sys 0m0.004s
[1]+  Donesleep 1




> Fix patch:
> ==
> # Attached the fix patch -
> Fix_for_bash_loadable_builtin_sleep_execution_issue.patch
> 
> 
> Please kindly review and suggest your comments.
> 
> Thanks,
> Thiruvadi Rajaraman

I guess the line 
+  } while ( (ret == -1));
should actually be while ( (ret == -1) && (errno == EINTR) );  ?


Also there's the issue that select() _may_ modify the object pointed to
by the timeout argument [POSIX]. But it may not, in which case this
would end up oversleeping.

On such system, doing eg.
 sleep 9 & time sleep 10

would end up sleeping 19 seconds.   

The solution is probably to change that select() into a pselect() that
masks SIGCHLD (as well as _some_ other signals, but not SIGINT).

Best regards




Re: [PATCH] Fix hang if $OLDPWD points to inaccessible directory

2017-11-27 Thread Mikulas Patocka


On Wed, 4 Oct 2017, Eduardo A. Bustamante López wrote:

> On Tue, Oct 03, 2017 at 10:29:08PM +0200, Mikulas Patocka wrote:
> > If $OLDPWD points to a non-existing directory, 'cd -' will fail.
> > But if we clear $OLDPWD, 'cd -' will fail too (with just different message).
> [...]
> 
> I performed the following tests:
> 
> 
> dualbus@ubuntu:~$ for sh in mksh ksh93 dash zsh posh bash; do echo $sh \| 
> $(OLDPWD=/invalid $sh -c 'echo "$OLDPWD | "; cd -; echo " | $?"' 2>&1); done
> mksh | /invalid | mksh: cd: /invalid: No such file or directory | 2
> ksh93 | /invalid | ksh93: cd: /invalid: [No such file or directory] | 1
> dash | /invalid | dash: 1: cd: can't cd to /invalid | 2
> zsh | /home/dualbus | | 0
> posh | /invalid | posh: cd: /invalid - No such file or directory | 1
> bash | | bash: line 0: cd: OLDPWD not set | 1
> 
> dualbus@ubuntu:~$ for sh in mksh ksh93 dash zsh posh bash; do echo $sh \| 
> $(OLDPWD=/tmp $sh -c 'echo "$OLDPWD | "; cd -; echo " | $?"' 2>&1); done
> mksh | /tmp | /tmp | 0
> ksh93 | /tmp | /tmp | 0
> dash | /tmp | /tmp | 0
> zsh | /home/dualbus | | 0
> posh | /tmp | /tmp | 0
> bash | /tmp | /tmp | 0
> 
> 
> So:
> 
> - Bash is the only major shell that performs stat() on OLDPWD to see if it's
>   going to import it from the environment or initialize it to an empty
>   value.
> 
> - Zsh doesn't import OLDPWD from the environment (bash's old behavior)
> 
> - The rest of the shells (mksh, ATT ksh, dash, posh) import OLDPWD from
>   the environment unconditionally.
> 
> - Mikulas already made the argument above of why this change wouldn't
>   break existing scripts that rely on bash performing the stat().
> 
> - I don't see a way around this behavior using existing bash features.
>   The value of OLDPWD can't be reset via BASH_ENV or other
>   initialization mechanisms. Sure, `unset OLDPWD; program' might work,
>   but I wouldn't call that a solution.
> 
> 
> Given the above, I've changed my mind, and I think this patch should be
> fine :-)

Hi

Will you commit my patch? - if you agree with it.

Mikulas