1171176Smlaier/*	$FreeBSD$ */
2171176Smlaier/*	$OpenBSD: pidfile.c,v 1.5 2002/05/26 09:29:02 deraadt Exp $	*/
3171176Smlaier/*	$NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $	*/
4171176Smlaier
5171176Smlaier/*-
6171176Smlaier * Copyright (c) 1999 The NetBSD Foundation, Inc.
7171176Smlaier * All rights reserved.
8171176Smlaier *
9171176Smlaier * This code is derived from software contributed to The NetBSD Foundation
10171176Smlaier * by Jason R. Thorpe.
11171176Smlaier *
12171176Smlaier * Redistribution and use in source and binary forms, with or without
13171176Smlaier * modification, are permitted provided that the following conditions
14171176Smlaier * are met:
15171176Smlaier * 1. Redistributions of source code must retain the above copyright
16171176Smlaier *    notice, this list of conditions and the following disclaimer.
17171176Smlaier * 2. Redistributions in binary form must reproduce the above copyright
18171176Smlaier *    notice, this list of conditions and the following disclaimer in the
19171176Smlaier *    documentation and/or other materials provided with the distribution.
20171176Smlaier *
21171176Smlaier * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22171176Smlaier * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23171176Smlaier * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24171176Smlaier * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25171176Smlaier * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26171176Smlaier * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27171176Smlaier * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28171176Smlaier * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29171176Smlaier * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30171176Smlaier * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31171176Smlaier * POSSIBILITY OF SUCH DAMAGE.
32171176Smlaier */
33171176Smlaier
34171176Smlaier#if defined(LIBC_SCCS) && !defined(lint)
35171176Smlaierstatic const char rcsid[] = "$OpenBSD: pidfile.c,v 1.5 2002/05/26 09:29:02 deraadt Exp $";
36171176Smlaier#endif /* LIBC_SCCS and not lint */
37171176Smlaier
38171176Smlaier#include <sys/param.h>
39171176Smlaier#include <errno.h>
40171176Smlaier#include <paths.h>
41171176Smlaier#include <stdio.h>
42171176Smlaier#include <stdlib.h>
43171176Smlaier#include <unistd.h>
44171176Smlaier#ifdef __FreeBSD__
45171176Smlaier#include "pidfile.h"
46171176Smlaier#else
47171176Smlaier#include <util.h>
48171176Smlaier#endif
49171176Smlaier
50171176Smlaierstatic char *pidfile_path;
51171176Smlaierstatic pid_t pidfile_pid;
52171176Smlaier
53171176Smlaierstatic void pidfile_cleanup(void);
54171176Smlaier
55171176Smlaierextern char *__progname;
56171176Smlaier
57171176Smlaierint
58171176Smlaierpidfile(const char *basename)
59171176Smlaier{
60171176Smlaier	FILE *f;
61171176Smlaier	int save_errno;
62171176Smlaier	pid_t pid;
63171176Smlaier
64171176Smlaier	if (basename == NULL)
65171176Smlaier		basename = __progname;
66171176Smlaier
67171176Smlaier	if (pidfile_path != NULL) {
68171176Smlaier		free(pidfile_path);
69171176Smlaier		pidfile_path = NULL;
70171176Smlaier	}
71171176Smlaier
72171176Smlaier	/* _PATH_VARRUN includes trailing / */
73171176Smlaier	(void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
74171176Smlaier	if (pidfile_path == NULL)
75171176Smlaier		return (-1);
76171176Smlaier
77171176Smlaier	if ((f = fopen(pidfile_path, "w")) == NULL) {
78171176Smlaier		save_errno = errno;
79171176Smlaier		free(pidfile_path);
80171176Smlaier		pidfile_path = NULL;
81171176Smlaier		errno = save_errno;
82171176Smlaier		return (-1);
83171176Smlaier	}
84171176Smlaier
85171176Smlaier	pid = getpid();
86171176Smlaier	if (fprintf(f, "%ld\n", (long)pid) <= 0 || fclose(f) != 0) {
87171176Smlaier		save_errno = errno;
88171176Smlaier		(void) unlink(pidfile_path);
89171176Smlaier		free(pidfile_path);
90171176Smlaier		pidfile_path = NULL;
91171176Smlaier		errno = save_errno;
92171176Smlaier		return (-1);
93171176Smlaier	}
94171176Smlaier
95171176Smlaier	pidfile_pid = pid;
96171176Smlaier	if (atexit(pidfile_cleanup) < 0) {
97171176Smlaier		save_errno = errno;
98171176Smlaier		(void) unlink(pidfile_path);
99171176Smlaier		free(pidfile_path);
100171176Smlaier		pidfile_path = NULL;
101171176Smlaier		pidfile_pid = 0;
102171176Smlaier		errno = save_errno;
103171176Smlaier		return (-1);
104171176Smlaier	}
105171176Smlaier
106171176Smlaier	return (0);
107171176Smlaier}
108171176Smlaier
109171176Smlaierstatic void
110171176Smlaierpidfile_cleanup(void)
111171176Smlaier{
112171176Smlaier
113171176Smlaier	if (pidfile_path != NULL && pidfile_pid == getpid())
114171176Smlaier		(void) unlink(pidfile_path);
115171176Smlaier}
116