Browse code

added port number in pid filename

Jiri Kuthan authored on 26/05/2002 13:50:06
Showing 1 changed files
... ...
@@ -239,7 +239,8 @@ extern int yyparse();
239 239
 
240 240
 static int is_main=0; /* flag = is this the  "main" process? */
241 241
 
242
-char* pid_file = 0;
242
+char* pid_file = 0; /* filename as asked by use */
243
+char *pid_fn = 0; /* and with port number appended */
243 244
 
244 245
 /* daemon init, return 0 on success, -1 on error */
245 246
 int daemonize(char*  name)
... ...
@@ -248,6 +249,8 @@ int daemonize(char*  name)
248 249
 	pid_t pid;
249 250
 	int r, p;
250 251
 
252
+	int pid_fn_len;
253
+
251 254
 	p=-1;
252 255
 
253 256
 	if (log_stderr==0)
... ...
@@ -300,25 +303,41 @@ int daemonize(char*  name)
300 303
 
301 304
 	/* added by noh: create a pid file for the main process */
302 305
 	if (pid_file!=0){
303
-		if ((pid_stream=fopen(pid_file, "r"))!=NULL){
306
+
307
+		/* added port number; -jiri */
308
+		pid_fn_len = strlen(pid_file) + 5 /* long port number */ 
309
+			+ 1 /* dot */ + 1 /* ZT */ ;
310
+		pid_fn = malloc( pid_fn_len );
311
+		if (!pid_fn) {
312
+			LOG(L_ERR, "ERROR: There is really no memory for ser\n");
313
+			goto error;
314
+		}
315
+		if (snprintf(pid_fn, pid_fn_len, "%s.%d", pid_file, port_no )==-1) {
316
+			LOG(L_ERR, "ERROR: pidfile printig failed -- perhaps too high port?\n");
317
+			goto error;
318
+		}
319
+		
320
+			
321
+		if ((pid_stream=fopen(pid_fn, "r"))!=NULL){
304 322
 			fscanf(pid_stream, "%d", &p);
305 323
 			fclose(pid_stream);
306 324
 			if (p==-1){
307 325
 				LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
308
-					" pid number\n", pid_file);
326
+					" pid number\n", pid_fn);
309 327
 				goto error;
310 328
 			}
311 329
 			if (kill((pid_t)p, 0)==0 || errno==EPERM){
312 330
 				LOG(L_CRIT, "running process found in the pid file %s\n",
313
-					pid_file);
331
+					pid_fn);
314 332
 				goto error;
315 333
 			}else{
316 334
 				LOG(L_WARN, "pid file contains old pid, replacing pid\n");
317 335
 			}
318 336
 		}
319 337
 		pid=getpid();
320
-		if ((pid_stream=fopen(pid_file, "w"))==NULL){
321
-			LOG(L_WARN, "unable to create pid file: %s\n", strerror(errno));
338
+		if ((pid_stream=fopen(pid_fn, "w"))==NULL){
339
+			LOG(L_WARN, "unable to create pid file %s: %s\n", 
340
+				pid_fn, strerror(errno));
322 341
 			goto error;
323 342
 		}else{
324 343
 			fprintf(pid_stream, "%i\n", (int)pid);
... ...
@@ -490,7 +509,10 @@ static void sig_usr(int signo)
490 509
 #ifdef STATS
491 510
 			dump_all_statistic();
492 511
 #endif
493
-			if (pid_file) unlink(pid_file);
512
+			if (pid_fn) {
513
+				unlink(pid_fn);
514
+				free(pid_fn);
515
+			}
494 516
 		}
495 517
 		exit(0);
496 518
 	} else if (signo==SIGUSR1) { /* statistic */