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 248
 	pid_t pid;
249 249
 	int r, p;
250 250
 
251
+	int pid_fn_len;
252
+
251 253
 	p=-1;
252 254
 
253 255
 	if (log_stderr==0)
... ...
@@ -300,25 +303,41 @@ int daemonize(char*  name)
300 300
 
301 301
 	/* added by noh: create a pid file for the main process */
302 302
 	if (pid_file!=0){
303
-		if ((pid_stream=fopen(pid_file, "r"))!=NULL){
303
+
304
+		/* added port number; -jiri */
305
+		pid_fn_len = strlen(pid_file) + 5 /* long port number */ 
306
+			+ 1 /* dot */ + 1 /* ZT */ ;
307
+		pid_fn = malloc( pid_fn_len );
308
+		if (!pid_fn) {
309
+			LOG(L_ERR, "ERROR: There is really no memory for ser\n");
310
+			goto error;
311
+		}
312
+		if (snprintf(pid_fn, pid_fn_len, "%s.%d", pid_file, port_no )==-1) {
313
+			LOG(L_ERR, "ERROR: pidfile printig failed -- perhaps too high port?\n");
314
+			goto error;
315
+		}
316
+		
317
+			
318
+		if ((pid_stream=fopen(pid_fn, "r"))!=NULL){
304 319
 			fscanf(pid_stream, "%d", &p);
305 320
 			fclose(pid_stream);
306 321
 			if (p==-1){
307 322
 				LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
308
-					" pid number\n", pid_file);
323
+					" pid number\n", pid_fn);
309 324
 				goto error;
310 325
 			}
311 326
 			if (kill((pid_t)p, 0)==0 || errno==EPERM){
312 327
 				LOG(L_CRIT, "running process found in the pid file %s\n",
313
-					pid_file);
328
+					pid_fn);
314 329
 				goto error;
315 330
 			}else{
316 331
 				LOG(L_WARN, "pid file contains old pid, replacing pid\n");
317 332
 			}
318 333
 		}
319 334
 		pid=getpid();
320
-		if ((pid_stream=fopen(pid_file, "w"))==NULL){
321
-			LOG(L_WARN, "unable to create pid file: %s\n", strerror(errno));
335
+		if ((pid_stream=fopen(pid_fn, "w"))==NULL){
336
+			LOG(L_WARN, "unable to create pid file %s: %s\n", 
337
+				pid_fn, strerror(errno));
322 338
 			goto error;
323 339
 		}else{
324 340
 			fprintf(pid_stream, "%i\n", (int)pid);
... ...
@@ -490,7 +509,10 @@ static void sig_usr(int signo)
490 490
 #ifdef STATS
491 491
 			dump_all_statistic();
492 492
 #endif
493
-			if (pid_file) unlink(pid_file);
493
+			if (pid_fn) {
494
+				unlink(pid_fn);
495
+				free(pid_fn);
496
+			}
494 497
 		}
495 498
 		exit(0);
496 499
 	} else if (signo==SIGUSR1) { /* statistic */