why does dash save, dup, and restore redirected descriptor in the parent, rather than redirect in the child?
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hello,
a dash script like:
date > foobar.txt
date
is (as an SSCCE) handled like this:
int fd;
int saved;
fd = open64("foobar.txt", O_WRONLY|O_CREAT);
saved = fcntl(1, F_DUPFD, 10);dup2(fd, 1);
if (!fork()) {
execl("/bin/date", "date", (char *)NULL);
} dup2(saved, 1);
if (!fork()) {
execl("/bin/date", "date", (char *)NULL);
} This is strange. Why save, dup and dup again to restore, descriptors in the parent, when it would be much simpler to just dup in the child, and not have to save and restore. This is simpler and I checked it works the same:
int fd;
if (!fork()) {
fd = open64("foobar.txt", O_WRONLY|O_CREAT);
dup2(fd, 1);
execl("/bin/date", "date", (char *)NULL);
} if (!fork()) {
execl("/bin/date", "date", (char *)NULL);
} I am sure there must be a good reason and I am not understanding something deeper. What is it?
Thank you,
Mark
Quoted 1 line · Mark Galeck
Why save, dup and dup again to restore, descriptors in the parent, when it would be much simpler to just dup in the child, and not have to save and restore.
This assumes there's a child in the first place. Depending on the command, that might not be the case, think of built-in commands such as echo for instance. Code is needed to handle that case, and I suspect that if that code is there anyway, then having one place to set up redirections is simpler than having two places to set up redirections.
Cheers,
Harald van Dijk
Quoted 3 lines · Mark Galeck
This is strange. Why save, dup and dup again to restore, descriptors in the parent, when it would be much simpler to just dup in the child, and not have to save and restore. This is simpler and I checked it works the same:
I am sure there must be a good reason and I am not understanding something deeper. What is it?
I am not a dash developer, but one reason to make system calls in the parent is that it is much simpler to handle errors in the parent.
In your example:
Quoted 5 lines
if (!fork()) {
fd = open64("foobar.txt", O_WRONLY|O_CREAT);
dup2(fd, 1);
execl("/bin/date", "date", (char *)NULL);
}What happens if open64 fails? How should the child inform the parent of this specific error?
-Parke
Quoted 14 lines · Parke
Quoted 7 lines · Mark Galeck
This is strange. Why save, dup and dup again to restore, descriptors in the parent, when it would be much simpler to just dup in the child, and not have to save and restore. This is simpler and I checked it works the same:
I am sure there must be a good reason and I am not understanding something deeper. What is it?
I am not a dash developer, but one reason to make system calls in the parent is that it is much simpler to handle errors in the parent.
In your example:
Quoted 8 lines
Quoted 5 lines
if (!fork()) { fd = open64("foobar.txt", O_WRONLY|O_CREAT); dup2(fd, 1); execl("/bin/date", "date", (char *)NULL); }What happens if open64 fails? How should the child inform the parent of this specific error?
In general, you are right, but in the shell's case there is no difficulty. The error is reported via an error message to stderr and a non-zero exit status of the command, and the child process can easily do those things.
Signature · 1 line
Jilles Tjoelker
Thank you Harald and Parke for your very informative comments, I appreciate!
Mark