Subject: bin/3719: /usr/sbin/xntpd fails to remove its pid file
To: None <>
From: Paul Goyette <>
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
>Originator:     Paul Goyette
>Release:        24 May 1997
	System: NetBSD 1.2E NetBSD 1.2E (PGOYETTE) #60: 
		Sun May 25 14:15:18 PDT 1997 
		(current as of Sat May 24 04:53:29 1997) mac68k

	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.
	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)
	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().

> static char * saved_pid_file = NULL;
> static pid_t  saved_pid = 0;
> void remove_pid_file(void);
> 		if ((saved_pid_file = malloc(strlen(value)+1)))
> 			strcpy(saved_pid_file, value);
< 		fprintf(fp, "%d", (int) getpid());
> 		saved_pid = getpid();
> 		atexit(remove_pid_file);
> 		fprintf(fp, "%d\n", (int) saved_pid);
> void
> remove_pid_file(void)
> {
> 	if (saved_pid_file != NULL && saved_pid == getpid())
> 		unlink(saved_pid_file);	
> }