Re: exec command - closing file descriptor



In article <xLWdndVE-sJvlQPUnZ2dnUVZ_rTinZ2d@xxxxxxxxxxxxx>,
Norman Bullen <norm@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

On my Solaris system, the man page for "exec" says the following:

ksh
With the exec built-in, if arg is given, the command speci-
fied by the arguments is executed in place of this shell
without creating a new process. Input/output arguments may
appear and affect the current process. If no arguments are
given the effect of this command is to modify file descrip-
tors as prescribed by the input/output redirection list. In
this case, any file descriptor numbers greater than 2 that
are opened with this mechanism are closed when invoking
another program.

In this context, what does "closed when invoking another program" mean?

My script executes several external programs (cat, grep, sed, etc.)
after "exec 4> logfile" and I don't see any effect on file descriptor 4.
However, descriptor 4 does get closed when the script executes ksh as a
separate process in the background. In a small test script, even that
does not affect the file descriptor.

What's going on here?

What they mean is that it's closed in the child process where the
external program runs, not the original shell process. I.e. it's opened
with the close-on-exec flag.

I wrote the following script:

exec 4>/dev/null
/bin/sleep 100 # Need /bin/ because sleep is builtin

imac:barmar $ ./test.sh &
[1] 7738
imac:barmar $ ps
PID TTY TIME CMD
251 ttyp0 0:00.05 /bin/bash --noediting -i
7738 ttyp0 0:00.00 /bin/ksh ./test.sh
7741 ttyp0 0:00.00 /bin/sleep 100
imac:barmar $ lsof -p 7738
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ksh 7738 barmar cwd DIR 14,2 3264 654666 /Users/barmar
ksh 7738 barmar txt REG 14,2 2103664 21949 /bin/ksh
ksh 7738 barmar txt REG 14,2 1059792 2428551 /usr/lib/dyld
ksh 7738 barmar txt REG 14,2 136196096 2666323
/private/var/db/dyld/dyld_shared_cache_i386
ksh 7738 barmar 0u CHR 4,0 0t8475 46380036 /dev/ttyp0
ksh 7738 barmar 1u CHR 4,0 0t8475 46380036 /dev/ttyp0
ksh 7738 barmar 2u CHR 4,0 0t8475 46380036 /dev/ttyp0
ksh 7738 barmar 3r REG 14,2 5425538 11508
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolb
ox.framework/Versions/A/Resources/Extras2.rsrc
ksh 7738 barmar 4w CHR 3,2 0t0 46380420 /dev/null
ksh 7738 barmar 5r REG 14,2 490410 2425071
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolb
ox.framework/Versions/A/Resources/English.lproj/Localized.rsrc
ksh 7738 barmar 10r REG 14,2 51 2760393
/Users/barmar/test.sh
imac:barmar $ lsof -p 7741
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sleep 7741 barmar cwd DIR 14,2 3264 654666 /Users/barmar
sleep 7741 barmar txt REG 14,2 38464 22464 /bin/sleep
sleep 7741 barmar txt REG 14,2 1059792 2428551 /usr/lib/dyld
sleep 7741 barmar txt REG 14,2 136196096 2666323
/private/var/db/dyld/dyld_shared_cache_i386
sleep 7741 barmar 0u CHR 4,0 0t9605 46380036 /dev/ttyp0
sleep 7741 barmar 1u CHR 4,0 0t9605 46380036 /dev/ttyp0
sleep 7741 barmar 2u CHR 4,0 0t9605 46380036 /dev/ttyp0
sleep 7741 barmar 3r REG 14,2 5425538 11508
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolb
ox.framework/Versions/A/Resources/Extras2.rsrc
sleep 7741 barmar 5r REG 14,2 490410 2425071
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolb
ox.framework/Versions/A/Resources/English.lproj/Localized.rsrc

Notice that FD 4 is not open in the child process running sleep.

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Re: xp_cmdshell default path (system32) problem
    ... exec xp_cmdshell @cmd1 ... - specify the full path in the del command ... ensure that xp_cmdshell ALWAYS executes under the security context of ... I haven't executed the actual erase statements yet but rather have been ...
    (microsoft.public.sqlserver.programming)
  • Re: Problems with exec
    ... On Nov 28, 5:37 am, Erwin Moller ... Ok, the $ReturnValue says 1, so I expect that the command DID run ... The passthrufunction is similar to the exec() function in that it ... executes a command. ...
    (comp.lang.php)
  • Re: basic perl query
    ... You have multiple mistakes here. ... First of all, you are using the exec() function, ... given command IN THIS PROCESS and DOES NOT RETURN. ... want either, as that forks off a different process, executes your cd ...
    (perl.beginners)
  • Re: wScript.Shell Problems
    ... the completion of the underlying process. ... However, the Exec procedure is ... Here is why i went to the RUN command, it is my understanding that the ... EXEC command does not allow for the BOOLEAN indicating wether the ...
    (microsoft.public.scripting.vbscript)
  • Re: Problem in calling c programs and compiling them in tcl/tk
    ... Are you using Tcl's glob command to do this? ... using exec gcc filename.c and later exec ./a.out filename.c. ... the execution of a command that might generate a Tcl error. ... i can call a c program and compile it? ...
    (comp.lang.tcl)

Loading