pidfile.c revision 171176
1/*	$FreeBSD: head/contrib/pf/pflogd/pidfile.c 171176 2007-07-03 14:08:49Z mlaier $ */
2/*	$OpenBSD: pidfile.c,v 1.5 2002/05/26 09:29:02 deraadt Exp $	*/
3/*	$NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $	*/
4
5/*-
6 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Jason R. Thorpe.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *        This product includes software developed by the NetBSD
23 *        Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 *    contributors may be used to endorse or promote products derived
26 *    from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#if defined(LIBC_SCCS) && !defined(lint)
42static const char rcsid[] = "$OpenBSD: pidfile.c,v 1.5 2002/05/26 09:29:02 deraadt Exp $";
43#endif /* LIBC_SCCS and not lint */
44
45#include <sys/param.h>
46#include <errno.h>
47#include <paths.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <unistd.h>
51#ifdef __FreeBSD__
52#include "pidfile.h"
53#else
54#include <util.h>
55#endif
56
57static char *pidfile_path;
58static pid_t pidfile_pid;
59
60static void pidfile_cleanup(void);
61
62extern char *__progname;
63
64int
65pidfile(const char *basename)
66{
67	FILE *f;
68	int save_errno;
69	pid_t pid;
70
71	if (basename == NULL)
72		basename = __progname;
73
74	if (pidfile_path != NULL) {
75		free(pidfile_path);
76		pidfile_path = NULL;
77	}
78
79	/* _PATH_VARRUN includes trailing / */
80	(void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
81	if (pidfile_path == NULL)
82		return (-1);
83
84	if ((f = fopen(pidfile_path, "w")) == NULL) {
85		save_errno = errno;
86		free(pidfile_path);
87		pidfile_path = NULL;
88		errno = save_errno;
89		return (-1);
90	}
91
92	pid = getpid();
93	if (fprintf(f, "%ld\n", (long)pid) <= 0 || fclose(f) != 0) {
94		save_errno = errno;
95		(void) unlink(pidfile_path);
96		free(pidfile_path);
97		pidfile_path = NULL;
98		errno = save_errno;
99		return (-1);
100	}
101
102	pidfile_pid = pid;
103	if (atexit(pidfile_cleanup) < 0) {
104		save_errno = errno;
105		(void) unlink(pidfile_path);
106		free(pidfile_path);
107		pidfile_path = NULL;
108		pidfile_pid = 0;
109		errno = save_errno;
110		return (-1);
111	}
112
113	return (0);
114}
115
116static void
117pidfile_cleanup(void)
118{
119
120	if (pidfile_path != NULL && pidfile_pid == getpid())
121		(void) unlink(pidfile_path);
122}
123