Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
Daemonize on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
lutoma committed May 10, 2012
1 parent c74fce3 commit fee02fa
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ int main(int argc, char* argv[], char* envp[])
openlog("fastresize", LOG_CONS, LOG_USER);
#endif

// Change the working directory
if((chdir(root)) < 0)
error("Couldn't change working directory\n", EXIT_FAILURE);

// Initialize FastCGI
syslog(LOG_INFO, "Initializing FastCGI\n");
if(FCGX_Init())
Expand All @@ -244,15 +248,28 @@ int main(int argc, char* argv[], char* envp[])
syslog(LOG_INFO, "Initializing ImageMagick\n");
MagickWandGenesis();

// Fork worker processes
// Fork worker processes and exit main process (to daemonize)
bool worker = false;
syslog(LOG_INFO, "Forking workers\n");
for(int i = 1; i < num_workers; i++)
for(int i = 1; i <= num_workers; i++)
if((worker = fork() == 0)) break;

if(!worker)
{
syslog(LOG_INFO, "Now listening for requests on 127.0.0.1:9000\n");

printf("Successfull startup, see syslog for details\n");

// Change the filemode mask
umask(0);

// Close stdin, -out, -err
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

exit(EXIT_SUCCESS);
}

while(FCGX_Accept_r(&request) == 0)
handle_request(&request, root, http_uri);

Expand Down

0 comments on commit fee02fa

Please sign in to comment.