I didn't exactly find something about the following in the man-page. How is the supposed behavior in subprocesses spawned by a process which was itself spawned by stdbuf
?
E.g.:
stdbuf -oL myprog
From the code, I get that it sets LD_PRELOAD
, and as far as I know, all environment variables are inherited in any subprocesses.
I'm interested in both fork();
and fork(); execv();
subprocesses. (Not sure if that would make a difference.)
fork();
should not change the behavior at all. execv()
would use the same LD_PRELOAD
(as well as the stdbuf settings which also stored in env) and thus apply the same behavior (from the example: stdout is line-buffered).
Right?
Best Answer
strace
ing theexecve
(with environ) andwrite
system calls can help see what's going on:Here with the
stdbuf
of GNU coreutils 8.25. I beleive FreeBSD'sstdbuf
works similarly:exec and no fork:
LD_PRELOAD
and the config in_STDBUF_O
is passed to bothenv
commands. The twowrite()
system calls even though the output doesn't go to a terminal confirms the output is not buffered.fork and exec:
Same situation.
So yes
stdbuf
applies to the command it runs and all of its descendants (provided they don't clean their environment like the dynamic linker or libc do withLD_PRELOAD
for setuid/setgid... applications).