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
  • 22,306
  • 30
  • 110
  • 141
  • 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
  • 224,562
  • 31
  • 268
  • 324
0

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

CrazyCoder
  • 2,465
  • 8
  • 36
  • 57
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
  • 208,859
  • 35
  • 376
  • 711