From 2d5f4350eb3a5683ab0fb8b8c4f615dab0870744 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Thu, 18 Mar 2010 13:07:40 +0000 Subject: [PATCH] + Add -deamon mode to kiwix-serve (ID: 2961214) --- src/server/kiwix-serve.cpp | 67 ++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index aa19d95..f4c6e6f 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -256,7 +256,7 @@ int main(int argc, char **argv) { string indexPath = ""; int serverPort = 80; - int daemonFlag = 0; + int daemonFlag = false; /* Argument parsing */ while (42) { @@ -272,31 +272,32 @@ int main(int argc, char **argv) { int option_index = 0; int c = getopt_long(argc, argv, "dvi:p:", long_options, &option_index); - if (c == -1) - break; + if (c != -1) { - switch (c) { - - case 'd': - daemonFlag = 1; - break; - - case 'v': - verboseFlag = true; - break; - - case 'i': - indexPath = optarg; - break; - - case 'p': - serverPort = atoi(optarg); - break; - - } - - if (optind < argc) { - zimPath = argv[optind++]; + switch (c) { + + case 'd': + daemonFlag = true; + break; + + case 'v': + verboseFlag = true; + break; + + case 'i': + indexPath = optarg; + break; + + case 'p': + serverPort = atoi(optarg); + break; + + } + } else { + if (optind < argc) { + zimPath = argv[optind++]; + break; + } } } @@ -330,6 +331,22 @@ int main(int argc, char **argv) { hasSearchIndex = false; } + /* Fork if necessary */ + if (daemonFlag) { + pid_t pid; + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + exit(1); + } + /* If we got a good PID, then + we can exit the parent process. */ + if (pid > 0) { + exit(0); + } + } + /* Mutex init */ pthread_mutex_init(&readerLock, NULL); pthread_mutex_init(&searcherLock, NULL);