News for hackers. Stuff that matters.
Re:Fork() is evil; vfork() is goodness; afork() would be bet... by cryptonector on Tuesday March 01, 2022 @04:41PM (#30520403)
Solaris/Illumos has an extension[0] for that.

  [0] http://src.illumos.org/source/search?project=illumos-gate&full=posix_spawn_file_actions_addclosefrom_np&defs=&refs=&path=&hist=&type=&xrd=&nn=1
  [1] https://docs.oracle.com/cd/E36784_01/html/E36874/posix-spawn-file-actions-addclosefrom-np-3c.html
Re:Fork() is evil; vfork() is goodness; afork() would be bet... by kazinator on Tuesday March 01, 2022 @08:00PM (#30522282)
For implementations which don't have it, you can stuff, into the file_actions, say, 4093 close action entries into the file_actions, targeting descriptors 3 to 4095. This big file_actions object can be cached and re-used for multiple calls to posix_spawn.

It won't close descriptor 4096, but that's probably beyond giving a darn in most cases. If you have an application that opens high descriptor numbers, you probably know.

Re:Fork() is evil; vfork() is goodness; afork() would be bet... by cryptonector on Tuesday March 01, 2022 @11:11PM (#30523589)
A better approach is to exec an intermediate helper program that will do it and then exec the actual intended program. One can also use this approach to do things like reset signal dispositions to SIG_IGN.
Re:Fork() is evil; vfork() is goodness; afork() would be bet... by kazinator on Tuesday March 01, 2022 @11:46PM (#30523845)
... add another option to /usr/bin/env and you got it!