tags:

views:

659

answers:

1

I'm implementing a library to run commands. The library is C, on Linux.

It currently does a popen() call to run a command and get output. The problem is that the command inherits all currently open file handlers.

If I did a fork/exec I could close the handlers in child explicitly. But that means re-implementing popen().

Can I set close-on-exec on all handlers without looping through them one by one?

Can I set close-on-exec as default for the process?

Thanks!

+2  A: 
ephemient
being a library, I don't control how my users open files, so using flags in open() is not an option
n-alexander
Too bad, you're out of luck -- UNIX (and Linux) will not let you change the underlying semantics of `open` et al. You'll just have to live with it, like everybody else.
ephemient
See http://lkml.org/lkml/2004/3/29/191 for an example of a cloexec-by-default-switch proposal being shot down. This has been proposed many times and been declined just as many times.
ephemient
just backward compatibility again. Too badI've seen lots of cases when inherited files got stuck because a loose child didn't exit, and since it's loose there isn't much you can do about it. Not even kill easily. Is manual fork/exec with close really the only way to build something reliable?
n-alexander