Jan 6, 2008

close-on-exec

Not until I noticed the FD_CLOEXEC flag of file descriptor, I thought all file handlers will be closed on calling exec(), and stdin, stdout, stderr will be reopened too.

But it seems not, After some testing, I think that the file handlers might be all untouched on calling exec(), opened files remained opened, closed stdio remained closed.

But if the file descriptor have set with FD_CLOEXEC flag, it'll be closed when calling exec().

1 comment:

cooldavid said...

A peace of code from a program like inetd:

dup2(sock, 0);
close(sock);
dup2(0, 1);
dup2(0, 2);
execl(sl->server_path, sl->server_path, NULL);

It works only when exec() won't touch the opened file handlers.