daemon: daemonize_close_stdin() optimised.
Changed function to first close standard input (this may fail but we don't care) and then try to open /dev/null (this may fail but it shouldn't on Unix platforms plus we don't know what to do in such case anyways). Since standard input has the "zeroth" descriptor number next "open" will use it. Since there is no "/dev/null" on Windows (It's not even a valid path!) the second step is skipped if WIN32 is defined. As a final touch, since the function consists of merely two function calls it has been moved to header file and declared static inline. [mk: un-inline daemonize_close_stdin()]
This commit is contained in:
parent
bdb1965b50
commit
bfed1c04cc
14
src/daemon.c
14
src/daemon.c
@ -85,23 +85,13 @@ daemonize_kill(void)
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void
|
||||
daemonize_close_stdin(void)
|
||||
{
|
||||
int fd = open("/dev/null", O_RDONLY);
|
||||
|
||||
if (fd < 0)
|
||||
close(STDIN_FILENO);
|
||||
else if (fd != STDIN_FILENO) {
|
||||
dup2(fd, STDIN_FILENO);
|
||||
close(fd);
|
||||
}
|
||||
close(STDIN_FILENO);
|
||||
open("/dev/null", O_RDONLY);
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
|
||||
void
|
||||
daemonize_set_user(void)
|
||||
{
|
||||
|
@ -56,8 +56,13 @@ daemonize_kill(void)
|
||||
/**
|
||||
* Close stdin (fd 0) and re-open it as /dev/null.
|
||||
*/
|
||||
#ifndef WIN32
|
||||
void
|
||||
daemonize_close_stdin(void);
|
||||
#else
|
||||
static inline void
|
||||
daemonize_close_stdin(void) {}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Change to the configured Unix user.
|
||||
|
Loading…
Reference in New Issue
Block a user