coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Tadeus Prastowo
Hi!

To quote 
https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html#Coprocesses

"The file descriptors are not available in subshells."

So, that is my expectation.  However, the following Bash fails me:
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu'
-DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/share/locale'
-DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include
-I./lib   -g -O2 -Wno-parentheses -Wno-format-security
uname output: Linux 0x657573 4.4.0-111-generic #134-Ubuntu SMP Mon Jan
15 14:53:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-unknown-linux-gnu

Bash Version: 4.4
Patch Level: 12
Release Status: release

Description:

First, to quote
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution

"Bash performs the expansion by executing command in a subshell environment"

However, the following works: coproc cat >/tmp/test.txt; echo x $(echo
hello777 >&${COPROC[1]}) y; exec {COPROC[1]}<&- ; cat /tmp/test.txt

Specifically, I use command substitution to feed the coproc with
hello777.  And, the coproc receives that string storing it in
/tmp/test.txt.  But, command substitution creates a subshell.  So,
coproc file descriptors are available in subshells, contradicting
coproc documentation.

Second, to quote
https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html#Process-Substitution

"The process list is run asynchronously"

However, the following works: coproc cat >/tmp/test.txt; cat <(echo
hello888 >&${COPROC[1]}); exec {COPROC[1]}<&- ; cat /tmp/test.txt

Specifically, I use process substitution to feed the coproc with
hello888.  And, the coproc receives that string storing it in
/tmp/test.txt.  But, process substitution creates a subshell because
it runs asynchronously.  So, coproc file descriptors are available in
subshells, contradicting coproc documentation.

How should the contradiction be resolved?

Thank you.

--
Best regards,
Tadeus



Re: coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Chet Ramey
On 1/24/18 3:38 AM, Tadeus Prastowo wrote:
> Hi!
> 
> To quote 
> https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html#Coprocesses
> 
> "The file descriptors are not available in subshells."

The file descriptors are set to close-on-exec, so they're not available
to new processes, and closed explicitly when executing subshell commands
such as ( ... ) and asynchronous commands run with `&'.  Command and
process substitutions are created as exact copies of their parent, and
inherit the file descriptors.

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



Re: coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Tadeus Prastowo
On Wed, Jan 24, 2018 at 3:16 PM, Chet Ramey  wrote:
> On 1/24/18 3:38 AM, Tadeus Prastowo wrote:
>> Hi!
>>
>> To quote 
>> https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html#Coprocesses
>>
>> "The file descriptors are not available in subshells."
>
> The file descriptors are set to close-on-exec, so they're not available
> to new processes, and closed explicitly when executing subshell commands
> such as ( ... ) and asynchronous commands run with `&'.  Command and
> process substitutions are created as exact copies of their parent, and
> inherit the file descriptors.

So, the documentation should be fixed, no?

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

--
Best regards,
Tadeus



Re: coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Chet Ramey
On 1/24/18 9:50 AM, Tadeus Prastowo wrote:
> On Wed, Jan 24, 2018 at 3:16 PM, Chet Ramey  wrote:
>> On 1/24/18 3:38 AM, Tadeus Prastowo wrote:
>>> Hi!
>>>
>>> To quote 
>>> https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html#Coprocesses
>>>
>>> "The file descriptors are not available in subshells."
>>
>> The file descriptors are set to close-on-exec, so they're not available
>> to new processes, and closed explicitly when executing subshell commands
>> such as ( ... ) and asynchronous commands run with `&'.  Command and
>> process substitutions are created as exact copies of their parent, and
>> inherit the file descriptors.
> 
> So, the documentation should be fixed, no?

Sure, it can be clarified.


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



Re: coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Tadeus Prastowo
On Wed, Jan 24, 2018 at 3:55 PM, Tadeus Prastowo
 wrote:
> On Wed, Jan 24, 2018 at 3:53 PM, Chet Ramey  wrote:
>> On 1/24/18 9:50 AM, Tadeus Prastowo wrote:
>>> On Wed, Jan 24, 2018 at 3:16 PM, Chet Ramey  wrote:
 On 1/24/18 3:38 AM, Tadeus Prastowo wrote:
> Hi!
>
> To quote 
> https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html#Coprocesses
>
> "The file descriptors are not available in subshells."

 The file descriptors are set to close-on-exec, so they're not available
 to new processes, and closed explicitly when executing subshell commands
 such as ( ... ) and asynchronous commands run with `&'.  Command and
 process substitutions are created as exact copies of their parent, and
 inherit the file descriptors.
>>>
>>> So, the documentation should be fixed, no?
>>
>> Sure, it can be clarified.
>
> Okay, I will propose a patch.

diff --git a/doc/bashref.texi b/doc/bashref.texi
index c0f4a2f..fee8f0e 100644
--- a/doc/bashref.texi
+++ b/doc/bashref.texi
@@ -1197,7 +1197,10 @@ This pipe is established before any
redirections specified by the
 command (@pxref{Redirections}).
 The file descriptors can be utilized as arguments to shell commands
 and redirections using standard word expansions.
-The file descriptors are not available in subshells.
+The file descriptors are not available in subshells other than those
+created by command and process substitutions (see the descriptions
+the substitutions in @ref{Command Substitution} and
+@ref{Process Substitution}, respectively).

 The process ID of the shell spawned to execute the coprocess is
 available as the value of the variable @env{NAME}_PID.
@@ -3025,6 +3028,12 @@ shell's parent, and traps ignored by the shell
are ignored
 A command invoked in this separate environment cannot affect the
 shell's execution environment.

+Command substitution and process substitution are invoked in a
+separate execution environment that starts as an exact duplicate of
+the execution environment of the shell.  As before, any command within
+the separate execution environment cannot affect the shell's execution
+environment.
+
 Command substitution, commands grouped with parentheses,
 and asynchronous commands are invoked in a
 subshell environment that is a duplicate of the shell environment,

What do you think?

> Meanwhile, I observe another problem
> that I will send in the next e-mail.

Sorry, it is not a problem after I read your e-mail almost 7 years
ago.  To quote 
https://lists.gnu.org/archive/html/bug-bash/2011-04/msg00056.html,

the man page says under BUGS:

There may be only one active coprocess at a time.

(The shell will forget about earlier coprocs and not mark them as
completed, for instance.)

End quote.

By forgetting, I see that it also means that the FDs of the forgotten
coproc(s) have to be closed manually by subshells that do not need the
FDs and do not perform execve, such as builtin commands and shell
functions (e.g., coproc pA (cat); coproc pB (cat); echo xxx | read
myinput; will cause the subshells in the pipe to inherit the FDs of pA
but not pB).

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

--
Best regards,
Tadeus



Re: coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Tadeus Prastowo
On Wed, Jan 24, 2018 at 5:51 PM, Tadeus Prastowo
 wrote:
> On Wed, Jan 24, 2018 at 3:55 PM, Tadeus Prastowo
>  wrote:
>> On Wed, Jan 24, 2018 at 3:53 PM, Chet Ramey  wrote:
>>> On 1/24/18 9:50 AM, Tadeus Prastowo wrote:
 On Wed, Jan 24, 2018 at 3:16 PM, Chet Ramey  wrote:
> On 1/24/18 3:38 AM, Tadeus Prastowo wrote:
>> Hi!
>>
>> To quote 
>> https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html#Coprocesses
>>
>> "The file descriptors are not available in subshells."
>
> The file descriptors are set to close-on-exec, so they're not available
> to new processes, and closed explicitly when executing subshell commands
> such as ( ... ) and asynchronous commands run with `&'.  Command and
> process substitutions are created as exact copies of their parent, and
> inherit the file descriptors.

 So, the documentation should be fixed, no?
>>>
>>> Sure, it can be clarified.
>>
>> Okay, I will propose a patch.
>
> diff --git a/doc/bashref.texi b/doc/bashref.texi
> index c0f4a2f..fee8f0e 100644
> --- a/doc/bashref.texi
> +++ b/doc/bashref.texi
> @@ -1197,7 +1197,10 @@ This pipe is established before any
> redirections specified by the
>  command (@pxref{Redirections}).
>  The file descriptors can be utilized as arguments to shell commands
>  and redirections using standard word expansions.
> -The file descriptors are not available in subshells.
> +The file descriptors are not available in subshells other than those
> +created by command and process substitutions (see the descriptions
> +the substitutions in @ref{Command Substitution} and
> +@ref{Process Substitution}, respectively).
>
>  The process ID of the shell spawned to execute the coprocess is
>  available as the value of the variable @env{NAME}_PID.
> @@ -3025,6 +3028,12 @@ shell's parent, and traps ignored by the shell
> are ignored
>  A command invoked in this separate environment cannot affect the
>  shell's execution environment.
>
> +Command substitution and process substitution are invoked in a
> +separate execution environment that starts as an exact duplicate of
> +the execution environment of the shell.  As before, any command within
> +the separate execution environment cannot affect the shell's execution
> +environment.
> +
>  Command substitution, commands grouped with parentheses,
>  and asynchronous commands are invoked in a
>  subshell environment that is a duplicate of the shell environment,

I think the patch misses some information.  Please find attached the
new patch that I propose.  What do you think?

Thanks.

--
Best regards,
Tadeus


patch
Description: Binary data


Re: coproc FDs (file descriptors) available to subshells

2018-01-24 Thread Tadeus Prastowo
On Wed, Jan 24, 2018 at 7:32 PM, Tadeus Prastowo
 wrote:
> On Wed, Jan 24, 2018 at 5:51 PM, Tadeus Prastowo
>  wrote:
>> On Wed, Jan 24, 2018 at 3:55 PM, Tadeus Prastowo
>>  wrote:

[...]

>>> Okay, I will propose a patch.
>>
>> diff --git a/doc/bashref.texi b/doc/bashref.texi
>> index c0f4a2f..fee8f0e 100644
>> --- a/doc/bashref.texi
>> +++ b/doc/bashref.texi
>> @@ -1197,7 +1197,10 @@ This pipe is established before any
>> redirections specified by the
>>  command (@pxref{Redirections}).
>>  The file descriptors can be utilized as arguments to shell commands
>>  and redirections using standard word expansions.
>> -The file descriptors are not available in subshells.
>> +The file descriptors are not available in subshells other than those
>> +created by command and process substitutions (see the descriptions
>> +the substitutions in @ref{Command Substitution} and
>> +@ref{Process Substitution}, respectively).
>>
>>  The process ID of the shell spawned to execute the coprocess is
>>  available as the value of the variable @env{NAME}_PID.
>> @@ -3025,6 +3028,12 @@ shell's parent, and traps ignored by the shell
>> are ignored
>>  A command invoked in this separate environment cannot affect the
>>  shell's execution environment.
>>
>> +Command substitution and process substitution are invoked in a
>> +separate execution environment that starts as an exact duplicate of
>> +the execution environment of the shell.  As before, any command within
>> +the separate execution environment cannot affect the shell's execution
>> +environment.
>> +
>>  Command substitution, commands grouped with parentheses,
>>  and asynchronous commands are invoked in a
>>  subshell environment that is a duplicate of the shell environment,
>
> I think the patch misses some information.  Please find attached the
> new patch that I propose.  What do you think?

Sorry, the patch still misses some information as per
https://lists.gnu.org/archive/html/bug-bash/2011-04/msg00056.html.  I
attach the updated patch.  What do you think?

Thank you.

--
Best regards,
Tadeus


patch
Description: Binary data


Re: coredump from C-c-ed function

2018-01-24 Thread Chet Ramey
On 1/23/18 12:42 PM, xftroxgpx wrote:

> I had no idea that the latest bash(from master) is almost 1 year old:
> commit bc007799f0e1362100375bb95d952d28de4c62fb (origin/master, origin/HEAD, 
> master)
> Date:   Fri Jan 27 11:25:44 2017 -0500
> 
> Bash-4.4 patch 12

That's the stable version.

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