+ Add -deamon mode to kiwix-serve (ID: 2961214)

This commit is contained in:
kelson42 2010-03-18 13:07:40 +00:00
parent a5646c3c44
commit 2d5f4350eb

View File

@ -256,7 +256,7 @@ int main(int argc, char **argv) {
string indexPath = ""; string indexPath = "";
int serverPort = 80; int serverPort = 80;
int daemonFlag = 0; int daemonFlag = false;
/* Argument parsing */ /* Argument parsing */
while (42) { while (42) {
@ -272,31 +272,32 @@ int main(int argc, char **argv) {
int option_index = 0; int option_index = 0;
int c = getopt_long(argc, argv, "dvi:p:", long_options, &option_index); int c = getopt_long(argc, argv, "dvi:p:", long_options, &option_index);
if (c == -1) if (c != -1) {
break;
switch (c) { switch (c) {
case 'd': case 'd':
daemonFlag = 1; daemonFlag = true;
break; break;
case 'v': case 'v':
verboseFlag = true; verboseFlag = true;
break; break;
case 'i': case 'i':
indexPath = optarg; indexPath = optarg;
break; break;
case 'p': case 'p':
serverPort = atoi(optarg); serverPort = atoi(optarg);
break; break;
} }
} else {
if (optind < argc) { if (optind < argc) {
zimPath = argv[optind++]; zimPath = argv[optind++];
break;
}
} }
} }
@ -330,6 +331,22 @@ int main(int argc, char **argv) {
hasSearchIndex = false; 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 */ /* Mutex init */
pthread_mutex_init(&readerLock, NULL); pthread_mutex_init(&readerLock, NULL);
pthread_mutex_init(&searcherLock, NULL); pthread_mutex_init(&searcherLock, NULL);