Re: [PATCH] fs: add FD_CLOFORK and O_CLOFORK

From: Changli Gao
Date: Sat May 07 2011 - 06:10:12 EST


On Sat, May 7, 2011 at 1:25 PM, Eric Dumazet <eric.dumazet@xxxxxxxxx> wrote:
> Le samedi 07 mai 2011 à 12:49 +0800, Changli Gao a écrit :
>> If FD_CLOFORK is 1, when a fork occurs, the corresponding file descriptor
>> will be closed for the child process. IOW, the file descriptor isn't
>> inheritable.
>>
>> FD_CLOFORK is used as IBM does.
>
> Is it part of a standard, and what could be the use for such thing ?
> Why had we wait 2011 to add it in linux ?
>

It isn't part of any standard. It can be used in multi-process
programs, which don't want the child processes inherit some file
descriptors. Here is a basic server program.

serv_sock = socket(...);
bind(serv_sock, ...);
listen(serv_sock, ...);
fcntl(serv_sock, F_SETFD, FD_CLOFORK);
while (1) {
clnt_sock = accept(serv_sock);
switch (fork()) {
case 0:
exit(do_serv(clnt_sock));
default:
break;
}
}

This flag can also been used instead of FD_CLOEXEC in the exec(2)
after fork(2) environment. As no such file descriptors is duplicated
between fork(2) and exec(2), the later close(2) in kernel won't be
needed when exec(2). It can improve the performance.

Thanks.

--
Regards,
Changli Gao(xiaosuo@xxxxxxxxx)
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/