4

Is it possible to mark a specific file descriptor as not inheritable, or close it, in the child process when fork() is invoked?

zer0stimulus
  • 20,576
  • 30
  • 105
  • 139
  • 4
    Possible duplicate of [Prevent file descriptors inheritance during Linux fork](https://stackoverflow.com/q/5713242/608639) – jww Apr 18 '19 at 18:06

3 Answers3

10

No. All file descriptors are inherited in fork. You can set a fd to be closed on exec, however, by using fcntl(fd, F_SETFD, FD_CLOEXEC).

bdonlan
  • 214,833
  • 29
  • 259
  • 321
0

No its not possible. By default child processes with inherit file table from parent process.

CrazyCoder
  • 2,305
  • 8
  • 32
  • 51
0

If you really want close-on-fork, something like this could work:

static void fd_to_close;
static void closer()
{
    close(fd_to_close);
}

pthread_atfork(0, 0, closer);

Normally close-on-exec is the desired behavior anyway, though.

R.. GitHub STOP HELPING ICE
  • 201,833
  • 32
  • 354
  • 689