Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close std-filehandles when daemonize #468

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sslh-fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
wait(NULL);
}

/* The actual main is in common.c: it's the same for both version of
/* The actual main() is in sslh_main.c: it's the same for all versions of
* the server
*/

10 changes: 10 additions & 0 deletions sslh-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ int main(int argc, char *argv[], char* envp[])

if (!cfg.foreground) {
if (fork() > 0) exit(0); /* Detach */
// close stdin, stderr, stdout
int newfd;
if (newfd = open("/dev/null", O_RDWR)) {
dup2 (newfd, STDIN_FILENO);
Copy link
Owner

Choose a reason for hiding this comment

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

I am curious of why you're doing this. Intuitively I would have simply

close(fileno(stdin));
close(fileno(stdout));
close(fileno(stderr));

(which is how we do it to close stderr when using inetd)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remembered this construct from Stevens Network Programming, but I have to reread to give more arguments.
Just copied from some older simple programs, I wrote decades ago

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I bet, the other way is also working.

dup2 (newfd, STDOUT_FILENO);
dup2 (newfd, STDERR_FILENO);
close(newfd);
} else {
print_message(msg_config, "Error closing standard filehandles for background daemon\n");
Copy link
Owner

Choose a reason for hiding this comment

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

I think this more of a msg_system_error, I don't see how this could fail...

}

/* New session -- become group leader */
if (getuid() == 0) {
Expand Down
Loading