From d8ca87028a9e8b189b244b6fe7c6b981e240bbb4 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Mon, 16 Apr 2012 15:10:27 +0000 Subject: [PATCH] NEW: kiwix-serve command line option --attachToProcess --- src/server/kiwix-serve.cpp | 54 ++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 53948d3..563c991 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -17,7 +17,14 @@ * MA 02110-1301, USA. */ +#ifdef __APPLE__ +#import +#import +#define MIBSIZE 4 +#endif + #ifdef _WIN32 +#include #include #include #include @@ -351,6 +358,8 @@ int main(int argc, char **argv) { int serverPort = 80; int daemonFlag = false; int libraryFlag = false; + string PPIDString; + unsigned int PPID = 0; kiwix::Manager libraryManager; /* Argument parsing */ @@ -361,12 +370,13 @@ int main(int argc, char **argv) { {"verbose", no_argument, 0, 'v'}, {"library", no_argument, 0, 'l'}, {"index", required_argument, 0, 'i'}, + {"attachToProcess", required_argument, 0, 'a'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0} }; int option_index = 0; - int c = getopt_long(argc, argv, "dvli:p:", long_options, &option_index); + int c = getopt_long(argc, argv, "dvli:a:p:", long_options, &option_index); if (c != -1) { @@ -390,6 +400,11 @@ int main(int argc, char **argv) { case 'p': serverPort = atoi(optarg); break; + + case 'a': + PPIDString = string(optarg); + PPID = atoi(optarg); + break; } } else { if (optind < argc) { @@ -405,8 +420,8 @@ int main(int argc, char **argv) { /* Print usage)) if necessary */ if (zimPath.empty() && libraryPath.empty()) { - cerr << "Usage: kiwix-serve [--index=INDEX_PATH] [--port=PORT] [--verbose] [--daemon] ZIM_PATH" << endl; - cerr << " kiwix-serve --library [--port=PORT] [--verbose] [--daemon] LIBRARY_PATH" << endl; + cerr << "Usage: kiwix-serve [--index=INDEX_PATH] [--port=PORT] [--verbose] [--daemon] [--attachToProcess=PID] ZIM_PATH" << endl; + cerr << " kiwix-serve --library [--port=PORT] [--verbose] [--daemon] [--attachToProcess=PID] LIBRARY_PATH" << endl; exit(1); } @@ -556,13 +571,42 @@ int main(int argc, char **argv) { } /* Run endless */ - while (42) { + bool waiting = true; + do { + + if (PPID > 0) { +#ifdef _WIN32 + HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, PPID); + DWORD ret = WaitForSingleObject(process, 0); + CloseHandle(process); + if (ret == WAIT_TIMEOUT) { +#elif __APPLE__ + int mib[MIBSIZE]; + struct kinfo_proc kp; + size_t len = sizeof(kp); + + mib[0]=CTL_KERN; + mib[1]=KERN_PROC; + mib[2]=KERN_PROC_PID; + mib[3]=PPID; + + int ret = sysctl(mib, MIBSIZE, &kp, &len, NULL, 0); + if (ret != -1 && len > 0) { +#else /* Linux & co */ + string procPath = "/proc/" + string(PPIDString); + if (access(procPath.c_str(), F_OK) != -1) { +#endif + } else { + waiting = false; + } + } + #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif - } + } while (waiting); /* Stop the daemon */ MHD_stop_daemon(daemon);