Re: exec command - closing file descriptor
- From: Barry Margolin <barmar@xxxxxxxxxxxx>
- Date: Thu, 19 Feb 2009 20:53:51 -0500
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 ***
.
- References:
- exec command - closing file descriptor
- From: Norman Bullen
- exec command - closing file descriptor
- Prev by Date: Re: rsync with custom fixing of symlinks
- Next by Date: exec command - closing file descriptor
- Previous by thread: exec command - closing file descriptor
- Index(es):
Relevant Pages
|
Loading