Skip to content

Commit

Permalink
Split up daemonMainline() into subroutines.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Oct 6, 2008
1 parent 3545580 commit eaf5292
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions nph-offload.c
Expand Up @@ -1781,34 +1781,34 @@ static inline void daemonChild(const int fd, const struct sockaddr *addr,
} // daemonChild


static inline int daemonMainline(int argc, char **argv, char **envp)
#if !GLISTENDAEMONIZE
#define daemonToBackground()
#else
static void daemonToBackground(void)
{
signal(SIGCHLD, SIG_IGN);
const pid_t backpid = fork();
if (backpid > 0) // parent.
exit(0);

// !!! FIXME: move to own function.
#if GLISTENDAEMONIZE
else if (backpid == -1)
{
const pid_t backpid = fork();
if (backpid > 0) // parent.
return 0; // done.
fprintf(stderr, "Failed to fork(): %s\n", strerror(errno));
exit(1);
} // if

else if (backpid == -1)
{
fprintf(stderr, "Failed to fork(): %s\n", strerror(errno));
return 5;
} // if
// we're the child. Welcome to the background.
fclose(stdin);
fclose(stdout);
fclose(stderr);
stdin = stderr = stdout = NULL;
chdir("/");
setsid();
}
#endif

// we're the child. Welcome to the background.
fclose(stdin);
fclose(stdout);
fclose(stderr);
stdin = stderr = stdout = NULL;
chdir("/");
setsid();
}
#endif

// !!! FIXME: move to own function.
static int daemonListenSocket(void)
{
struct addrinfo hints;
memset(&hints, '\0', sizeof (hints));
hints.ai_family = GLISTENFAMILY;
Expand All @@ -1821,7 +1821,7 @@ static inline int daemonMainline(int argc, char **argv, char **envp)
{
if (stderr != NULL)
fprintf(stderr, "getaddrinfo failure: %s\n", gai_strerror(rc));
return 1;
return -1;
} // if

int fd = -1;
Expand Down Expand Up @@ -1849,9 +1849,21 @@ static inline int daemonMainline(int argc, char **argv, char **envp)
{
if (stderr != NULL)
fprintf(stderr, "Failed to bind socket.\n");
return 2;
} // if

return fd;
} // daemonListenSocket


static inline int daemonMainline(int argc, char **argv, char **envp)
{
signal(SIGCHLD, SIG_IGN);
daemonToBackground();

const int fd = daemonListenSocket();
if (fd == -1)
return 2;

while (1) // loop forever.
{
struct sockaddr addr;
Expand Down

0 comments on commit eaf5292

Please sign in to comment.