From: John Hodge Date: Tue, 15 Mar 2011 05:24:57 +0000 (+0800) Subject: Fixed up daemonising implementation (now to test it) X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=1b1b4f396d34f08e2e2932e7a69229dcc51a1da2;p=tpg%2Fopendispense2.git Fixed up daemonising implementation (now to test it) --- diff --git a/src/server/main.c b/src/server/main.c index 83c8e06..d6fefbc 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -25,6 +25,7 @@ extern void Init_Handlers(void); extern void Load_Itemlist(void); extern void Server_Start(void); +extern int gbServer_RunInBackground; extern int giServer_Port; extern char *gsItemListFile; extern char *gsCoke_SerialPort; @@ -85,6 +86,9 @@ int main(int argc, char *argv[]) if( i + 1 >= argc ) return -1; giDebugLevel = atoi(argv[++i]); break; + case 'D': + gbServer_RunInBackground = 1; + return -1; default: // Usage Error? PrintUsage(argv[0]); @@ -116,12 +120,18 @@ int main(int argc, char *argv[]) } fgets(buf, sizeof buf, fp); fclose(fp); - gsDoor_Password = strdup(buf);; + gsDoor_Password = strdup(buf); } else if( strcmp(arg, "--cokebank") == 0 ) { if( i + 1 >= argc ) return -1; gsCokebankPath = argv[++i]; } + else if( strcmp(arg, "--daemonise") == 0 ) { + gbServer_RunInBackground = 1; + } + else if( strcmp(arg, "--dont-daemonise") == 0 ) { + gbServer_RunInBackground = 1; + } else { // Usage error? PrintUsage(argv[0]); diff --git a/src/server/server.c b/src/server/server.c index dd8a294..4dec980 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -14,6 +14,7 @@ #include #include #include +#include // O_* #include #include #include @@ -147,9 +148,10 @@ void Server_Start(void) return ; } -#if 0 + // if( gbServer_RunInBackground ) { + int newin, newout, newerr; int pid = fork(); if( pid == -1 ) { fprintf(stderr, "ERROR: Unable to fork\n"); @@ -160,12 +162,15 @@ void Server_Start(void) // Parent, quit exit(0); } - // In child, sort out stdin/stdout - reopen(0, "/dev/null", O_READ); - reopen(1, gsServer_LogFile, O_CREAT|O_APPEND); - reopen(2, gsServer_ErrorLog, O_CREAT|O_APPEND); + // In child + // - Sort out stdin/stdout + newin = open("/dev/null", O_RDONLY); + newout = open(gsServer_LogFile, O_CREAT|O_APPEND, 0644); + newerr = open(gsServer_ErrorLog, O_CREAT|O_APPEND, 0644); + dup2(newin, 0); + dup2(newout, 1); + dup2(newerr, 2); } -#endif // Listen if( listen(giServer_Socket, MAX_CONNECTION_QUEUE) < 0 ) {