Skip to content

Commit

Permalink
Backport stderr/stdout fixes from v3.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Feb 20, 2014
1 parent 687726e commit e46e822
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/main/radiusd.c
Expand Up @@ -99,6 +99,8 @@ int main(int argc, char *argv[])
int flag = 0;
int from_child[2] = {-1, -1};

int devnull;

/*
* If the server was built with debugging enabled always install
* the basic fatal signal handlers.
Expand Down Expand Up @@ -293,12 +295,27 @@ int main(int argc, char *argv[])
}

#ifndef __MINGW32__

devnull = open("/dev/null", O_RDWR);
if (devnull < 0) {
radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n", strerror(errno));
exit(1);
}

/*
* Disconnect from session
*/
if (dont_fork == FALSE) {
pid_t pid;

/*
* Really weird things happen if we leave stdin open and call
* things like system() later.
*/
if (dont_fork == 0) {
dup2(devnull, STDIN_FILENO);
}

if (pipe(from_child) != 0) {
radlog(L_ERR, "Couldn't open pipe for child status: %s", strerror(errno));
exit(1);
Expand Down Expand Up @@ -355,38 +372,28 @@ int main(int argc, char *argv[])
*/
radius_pid = getpid();

/*
* If we're running as a daemon, close the default file
* descriptors, AFTER forking.
*/
if (!debug_flag) {
int devnull;

devnull = open("/dev/null", O_RDWR);
if (devnull < 0) {
radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
strerror(errno));
exit(1);
}
dup2(devnull, STDIN_FILENO);
if (mainconfig.radlog_dest == RADLOG_STDOUT) {
setlinebuf(stdout);
mainconfig.radlog_fd = STDOUT_FILENO;
} else {
dup2(devnull, STDOUT_FILENO);
}
if (mainconfig.radlog_dest == RADLOG_STDERR) {
setlinebuf(stderr);
mainconfig.radlog_fd = STDERR_FILENO;
} else {
dup2(devnull, STDERR_FILENO);
}
close(devnull);
if (mainconfig.radlog_dest == RADLOG_STDOUT) {
setlinebuf(stdout);
mainconfig.radlog_fd = STDOUT_FILENO;
} else if (debug_flag) {
dup2(devnull, STDOUT_FILENO);
}

if (mainconfig.radlog_dest == RADLOG_STDERR) {
setlinebuf(stdout);
mainconfig.radlog_fd = STDERR_FILENO;
} else {
setlinebuf(stdout); /* unbuffered output */
dup2(devnull, STDERR_FILENO);
}

/* Libraries may write messages to stderr or stdout */
if (debug_flag) {
dup2(mainconfig.radlog_fd, STDOUT_FILENO);
dup2(mainconfig.radlog_fd, STDERR_FILENO);
}

close(devnull);

/*
* Now we have logging check that the OpenSSL
*/
Expand Down

0 comments on commit e46e822

Please sign in to comment.