Skip to content

Commit

Permalink
refactor close stdin,out,err to own function and clarify the comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yrutschle committed Sep 8, 2024
1 parent e7a9a37 commit 686d1f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
33 changes: 19 additions & 14 deletions sslh-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ void config_sanity_check(struct sslhcfg_item* cfg)
}
}

/* Connect stdin, stdout, stderr to /dev/null. It is better to keep them around
* so they do not get re-used by socket descriptors, and accidently used by
* some library code.
*/
void close_std(void)
{
int newfd;

if ((newfd = open("/dev/null", O_RDWR))) {
dup2 (newfd, STDIN_FILENO);
dup2 (newfd, STDOUT_FILENO);
dup2 (newfd, STDERR_FILENO);
/* close the helper handle, as this is now unnecessary */
close(newfd);
} else {
print_message(msg_system_error, "Error closing standard filehandles for background daemon\n");
}
}

int main(int argc, char *argv[], char* envp[])
{
Expand Down Expand Up @@ -287,20 +305,7 @@ int main(int argc, char *argv[], char* envp[])

if (!cfg.foreground) {
if (fork() > 0) exit(0); /* Detach */
// close stdin, stderr, stdout
int newfd;
// duplicating a handle connected to /dev/null to stdin, stdout and stderr
// so we don't run in any problems, when a control-job or whats-o-ever will
// grab the those handles.
if ((newfd = open("/dev/null", O_RDWR))) {
dup2 (newfd, STDIN_FILENO);
dup2 (newfd, STDOUT_FILENO);
dup2 (newfd, STDERR_FILENO);
// close the helper handle, as this is now unnecessary
close(newfd);
} else {
print_message(msg_system_error, "Error closing standard filehandles for background daemon\n");
}
close_std();

/* New session -- become group leader */
if (getuid() == 0) {
Expand Down
6 changes: 5 additions & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
// this is a placeholder for version.h, to make code-checking editors happy
#ifndef VERSION_H
#define VERSION_H

#define VERSION "v2.1.1-49-ge7a9a37-dirty"
#endif

0 comments on commit 686d1f7

Please sign in to comment.