Subject: bin/3719: /usr/sbin/xntpd fails to remove its pid file
To: None <gnats-bugs@gnats.netbsd.org>
From: Paul Goyette <paul@whooppee.com>
List: netbsd-bugs
Date: 06/07/1997 09:21:37
>Number:         3719
>Category:       bin
>Synopsis:       /usr/sbin/xntpd fails to remove its pid file
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Jun  7 09:35:02 1997
>Last-Modified:
>Originator:     Paul Goyette
>Organization:
>Release:        24 May 1997
>Environment:
	System: NetBSD mac1.whooppee.com 1.2E NetBSD 1.2E (PGOYETTE) #60: 
		Sun May 25 14:15:18 PDT 1997 
		paul@mac1.whooppee.com:/home/paul/src/sys/arch/mac68k/compile/PGOYETTE 
		(current as of Sat May 24 04:53:29 1997) mac68k


>Description:
	xntpd can exit for a variety of reasons, including various signals
	as well as if it finds out that network time differs greatly from
	your system's clock.  When xntpd is run with the -p option, it
	creates a pid file;  this file is never deleted when xntpd exits.
	Since the presence or absence of this file is useful to determine
	if xntpd is running (ie, so a cron job can restart it if it's not
	running!), the program should delete the file at exit.
>How-To-Repeat:
	run the xntpd daemon with the -p <file> option, and then force
	it to exit (via `kill -TERM` works, as does setting your system
	time to be a couple hours off from "real" time)
>Fix:
	The following patch implements an atexit() routine that will delete
	the file.  A copy of the file name is saved by the mainline code,
	for use by the atexit() routine.  The pid of the file creator is 
	also saved, since the atexit() is called by both the parent and 
	child processes resulting from a subsequent call to daemon().

	
33a34,38
> static char * saved_pid_file = NULL;
> static pid_t  saved_pid = 0;
> 
> void remove_pid_file(void);
> 
400a406,407
> 		if ((saved_pid_file = malloc(strlen(value)+1)))
> 			strcpy(saved_pid_file, value);
405c412,414
< 		fprintf(fp, "%d", (int) getpid());
---
> 		saved_pid = getpid();
> 		atexit(remove_pid_file);
> 		fprintf(fp, "%d\n", (int) saved_pid);
413a423,429
> 
> void
> remove_pid_file(void)
> {
> 	if (saved_pid_file != NULL && saved_pid == getpid())
> 		unlink(saved_pid_file);	
> }
>Audit-Trail:
>Unformatted: