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 238
 
238 239
 static int is_main=0; /* flag = is this the  "main" process? */
239 240
 
241
+char* pid_file = 0;
242
+
240 243
 /* daemon init, return 0 on success, -1 on error */
241 244
 int daemonize(char*  name)
242 245
 {
246
+	FILE *pid_stream;
243 247
 	pid_t pid;
244
-	int r;
245
-	
248
+	int r, p;
249
+
250
+	p=-1;
251
+
246 252
 	if (log_stderr==0)
247 253
 		openlog(name, LOG_PID|LOG_CONS, LOG_LOCAL1 /*LOG_DAEMON*/);
248 254
 		/* LOG_CONS, LOG_PERRROR ? */
... ...
@@ -290,6 +296,34 @@ int daemonize(char*  name)
290 296
 		/*parent process => exit */
291 297
 		exit(0);
292 298
 	}
299
+
300
+	/* added by noh: create a pid file for the main process */
301
+	if (pid_file!=0){
302
+		if ((pid_stream=fopen(pid_file, "r"))!=NULL){
303
+			fscanf(pid_stream, "%d", &p);
304
+			fclose(pid_stream);
305
+			if (p==-1){
306
+				LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
307
+					" pid number\n", pid_file);
308
+				goto error;
309
+			}
310
+			if (kill((pid_t)p, 0)==0 || errno==EPERM){
311
+				LOG(L_CRIT, "running process found in the pid file %s\n",
312
+					pid_file);
313
+				goto error;
314
+			}else{
315
+				LOG(L_WARN, "pid file contains old pid, replacing pid\n");
316
+			}
317
+		}
318
+		pid=getpid();
319
+		if ((pid_stream=fopen(pid_file, "w"))==NULL){
320
+			LOG(L_WARN, "unable to create pid file: %s\n", strerror(errno));
321
+			goto error;
322
+		}else{
323
+			fprintf(pid_stream, "%i\n", (int)pid);
324
+			fclose(pid_stream);
325
+		}
326
+	}
293 327
 	
294 328
 	/* close any open file descriptors */
295 329
 	if (log_stderr==0)
... ...
@@ -449,6 +483,15 @@ static void sig_usr(int signo)
449 483
 #endif
450 484
 		dprint("Thank you for flying " NAME "\n");
451 485
 		exit(0);
486
+	} else if (signo==SIGTERM) { /* exit gracefully as daemon */
487
+		DPrint("TERM received, program terminates\n");
488
+		if (is_main){
489
+#ifdef STATS
490
+			dump_all_statistic();
491
+#endif
492
+			if (pif_file) unlink(pid_file);
493
+		}
494
+		exit(0);
452 495
 	} else if (signo==SIGUSR1) { /* statistic */
453 496
 #ifdef STATS
454 497
 		dump_all_statistic();
... ...
@@ -512,6 +555,10 @@ int main(int argc, char** argv)
512 555
 		DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
513 556
 		goto error;
514 557
 	}
558
+	if (signal(SIGTERM , sig_usr)  == SIG_ERR ) {
559
+		DPrint("ERROR: no SIGTERM signal handler can be installed\n");
560
+		goto error;
561
+	}
515 562
 
516 563
 	//memtest();
517 564
 	//hashtest();
... ...
@@ -522,7 +569,7 @@ int main(int argc, char** argv)
522 569
 #ifdef STATS
523 570
 	"s:"
524 571
 #endif
525
-	"f:p:m:b:l:n:rRvcdDEVhw:t:u:g:";
572
+	"f:p:m:b:l:n:rRvcdDEVhw:t:u:g:P:";
526 573
 	
527 574
 	while((c=getopt(argc,argv,options))!=-1){
528 575
 		switch(c){
... ...
@@ -638,6 +685,9 @@ int main(int argc, char** argv)
638 685
 						goto error;
639 686
 					}
640 687
 					break;
688
+			case 'P':
689
+					pid_file=optarg;
690
+					break;
641 691
 			case '?':
642 692
 					if (isprint(optopt))
643 693
 						fprintf(stderr, "Unknown option `-%c�.\n", optopt);