Browse code

Added parameter -P for creating PID file by noh

Nils Ohlmeier authored on 11/05/2002 18:02:16
Showing 1 changed files
... ...
@@ -146,7 +146,8 @@ Options:\n\
146 146
     -w  dir      change the working directory to \"dir\" (default \"/\")\n\
147 147
     -t  dir      chroot to \"dir\"\n\
148 148
     -u uid       change uid \n\
149
-    -g gid       change gid \n"
149
+    -g gid       change gid \n\
150
+    -P file      create a pid file\n"
150 151
 #ifdef STATS
151 152
 "    -s file	 File to which statistics is dumped (disabled otherwise)\n"
152 153
 #endif
... ...
@@ -237,12 +238,17 @@ extern int yyparse();
237 237
 
238 238
 static int is_main=0; /* flag = is this the  "main" process? */
239 239
 
240
+char* pid_file = 0;
241
+
240 242
 /* daemon init, return 0 on success, -1 on error */
241 243
 int daemonize(char*  name)
242 244
 {
245
+	FILE *pid_stream;
243 246
 	pid_t pid;
244
-	int r;
245
-	
247
+	int r, p;
248
+
249
+	p=-1;
250
+
246 251
 	if (log_stderr==0)
247 252
 		openlog(name, LOG_PID|LOG_CONS, LOG_LOCAL1 /*LOG_DAEMON*/);
248 253
 		/* LOG_CONS, LOG_PERRROR ? */
... ...
@@ -290,6 +296,34 @@ int daemonize(char*  name)
290 290
 		/*parent process => exit */
291 291
 		exit(0);
292 292
 	}
293
+
294
+	/* added by noh: create a pid file for the main process */
295
+	if (pid_file!=0){
296
+		if ((pid_stream=fopen(pid_file, "r"))!=NULL){
297
+			fscanf(pid_stream, "%d", &p);
298
+			fclose(pid_stream);
299
+			if (p==-1){
300
+				LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
301
+					" pid number\n", pid_file);
302
+				goto error;
303
+			}
304
+			if (kill((pid_t)p, 0)==0 || errno==EPERM){
305
+				LOG(L_CRIT, "running process found in the pid file %s\n",
306
+					pid_file);
307
+				goto error;
308
+			}else{
309
+				LOG(L_WARN, "pid file contains old pid, replacing pid\n");
310
+			}
311
+		}
312
+		pid=getpid();
313
+		if ((pid_stream=fopen(pid_file, "w"))==NULL){
314
+			LOG(L_WARN, "unable to create pid file: %s\n", strerror(errno));
315
+			goto error;
316
+		}else{
317
+			fprintf(pid_stream, "%i\n", (int)pid);
318
+			fclose(pid_stream);
319
+		}
320
+	}
293 321
 	
294 322
 	/* close any open file descriptors */
295 323
 	if (log_stderr==0)
... ...
@@ -449,6 +483,15 @@ static void sig_usr(int signo)
449 449
 #endif
450 450
 		dprint("Thank you for flying " NAME "\n");
451 451
 		exit(0);
452
+	} else if (signo==SIGTERM) { /* exit gracefully as daemon */
453
+		DPrint("TERM received, program terminates\n");
454
+		if (is_main){
455
+#ifdef STATS
456
+			dump_all_statistic();
457
+#endif
458
+			if (pif_file) unlink(pid_file);
459
+		}
460
+		exit(0);
452 461
 	} else if (signo==SIGUSR1) { /* statistic */
453 462
 #ifdef STATS
454 463
 		dump_all_statistic();
... ...
@@ -512,6 +555,10 @@ int main(int argc, char** argv)
512 512
 		DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
513 513
 		goto error;
514 514
 	}
515
+	if (signal(SIGTERM , sig_usr)  == SIG_ERR ) {
516
+		DPrint("ERROR: no SIGTERM signal handler can be installed\n");
517
+		goto error;
518
+	}
515 519
 
516 520
 	//memtest();
517 521
 	//hashtest();
... ...
@@ -522,7 +569,7 @@ int main(int argc, char** argv)
522 522
 #ifdef STATS
523 523
 	"s:"
524 524
 #endif
525
-	"f:p:m:b:l:n:rRvcdDEVhw:t:u:g:";
525
+	"f:p:m:b:l:n:rRvcdDEVhw:t:u:g:P:";
526 526
 	
527 527
 	while((c=getopt(argc,argv,options))!=-1){
528 528
 		switch(c){
... ...
@@ -638,6 +685,9 @@ int main(int argc, char** argv)
638 638
 						goto error;
639 639
 					}
640 640
 					break;
641
+			case 'P':
642
+					pid_file=optarg;
643
+					break;
641 644
 			case '?':
642 645
 					if (isprint(optopt))
643 646
 						fprintf(stderr, "Unknown option `-%c�.\n", optopt);