>>108059024
I'm guessing it's because CLOEXEC didn't use to exist and making it the default would have broken a lot of existing programs.
It's stupid because random inherited file descriptors were a problem from the very beginning before Unix even had fork(). When a command exited it exec'd the shell (sorta) and then the shell had to manually clean up all the files that the command had opened. And then it still took them until V7 to add FIOCLEX/CLOEXEC. (Though I guess it wouldn't have helped with that particular issue.)
On the topic of old Unix, this code is funny:
https://www.tuhs.org/cgi-bin/utree.pl?file=PWB1/sys/source/s1/fd2.c
close(2);
if (dup(fd) != 2) {
write(open("/dev/tty",1),"bad file descriptor?!?\n",23);
exit(1);
}
close(fd);
No dup2() yet so you close() and dup() and then do an extra check to see if that did what you hoped.