Skip to content

reopen STDIN and STDOUT as dev/null#28

Open
Maxime2 wants to merge 1 commit into
rustyrussell:masterfrom
Maxime2:daemonize-reopen
Open

reopen STDIN and STDOUT as dev/null#28
Maxime2 wants to merge 1 commit into
rustyrussell:masterfrom
Maxime2:daemonize-reopen

Conversation

@Maxime2

@Maxime2 Maxime2 commented Jul 30, 2015

Copy link
Copy Markdown
Contributor

It is better to reopen STDIN and STDOUT as well as they may be used in a third-party application using ccan.

@Maxime2

Maxime2 commented Jul 31, 2015

Copy link
Copy Markdown
Contributor Author

But STDIN is read only while STDOUT and STDERR are write only.

@rustyrussell

Copy link
Copy Markdown
Owner

Good point! A couple of typos to fix is all it needs then!

Comment thread ccan/daemonize/daemonize.c Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "if (fd == STDIN_FILENO)" right?

@Maxime2

Maxime2 commented Aug 3, 2015

Copy link
Copy Markdown
Contributor Author

Nope. If the new fd is already open, dup2() would close it before reusing. If the old fd is equal to the new fd dup2() does nothing. So there is no point to close fd if it is below or equal to STDERR_FILENO.

@rustyrussell

Copy link
Copy Markdown
Owner

Maxim Zakharov notifications@github.com writes:

Nope. If the new fd is already open, dup2() would close it before reusing. If the old fd is equal to the new fd dup2() does nothing. So there is no point to close fd if it is below or equal to STDERR_FILENO.

Well, if you're assuming the continuity of stdin, stdout, and stderr,
then how about this:

    if (open("/dev/null", O_RDONLY) != STDIN_FILENO)
            return false;
    /* Save some kernel memory by dup() for stdout & stderr */
    if (dup(open("/dev/null", O_WRONLY)) != STDERR_FILENO)
            return false;

Cheers,
Rusty.

@Maxime2

Maxime2 commented Aug 3, 2015

Copy link
Copy Markdown
Contributor Author

It looks very good, but perhaps we need to add a comment that this code is rely on unix's property to always use lowest unused file descriptor. As I remember, Windows do not state such guaranty.

@rustyrussell

Copy link
Copy Markdown
Owner

Maxim Zakharov notifications@github.com writes:

It looks very good, but perhaps we need to add a comment that this code is rely on unix's property to always use lowest unused file descriptor. As I remember, Windows do not state such guaranty.

Hmm, does this code even work on Windows at all? If so, might be nice
to do it the long way, but it'll need a comment explaining the reason
for the > test instead of == :)

Thanks,
Rusty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants