etc/services - sync with NetBSD-8
[minix.git] / external / bsd / blacklist / port / pidfile.c
bloba2138f1ae4bcb30efb7262c754f8f0fec7ef79ba
1 /* $NetBSD: pidfile.c,v 1.1 2015/01/22 16:19:53 christos Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe, Matthias Scheler and Julio Merino.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 __RCSID("$NetBSD: pidfile.c,v 1.1 2015/01/22 16:19:53 christos Exp $");
38 #endif
40 #include <sys/param.h>
42 #include <paths.h>
43 #include <stdbool.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #ifdef HAVE_UTIL_H
49 #include <util.h>
50 #endif
52 static pid_t pidfile_pid;
53 static char *pidfile_path;
55 /* Deletes an existent pidfile iff it was created by this process. */
56 static void
57 pidfile_cleanup(void)
60 if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
61 (void) unlink(pidfile_path);
64 /* Registers an atexit(3) handler to delete the pidfile we have generated.
65 * We only register the handler when we create a pidfile, so we can assume
66 * that the pidfile exists.
68 * Returns 0 on success or -1 if the handler could not be registered. */
69 static int
70 register_atexit_handler(void)
72 static bool done = false;
74 if (!done) {
75 if (atexit(pidfile_cleanup) < 0)
76 return -1;
77 done = true;
80 return 0;
83 /* Given a new pidfile name in 'path', deletes any previously-created pidfile
84 * if the previous file differs to the new one.
86 * If a previous file is deleted, returns 1, which means that a new pidfile
87 * must be created. Otherwise, this returns 0, which means that the existing
88 * file does not need to be touched. */
89 static int
90 cleanup_old_pidfile(const char* path)
92 if (pidfile_path != NULL) {
93 if (strcmp(pidfile_path, path) != 0) {
94 pidfile_cleanup();
96 free(pidfile_path);
97 pidfile_path = NULL;
99 return 1;
100 } else
101 return 0;
102 } else
103 return 1;
106 /* Constructs a name for a pidfile in the default location (/var/run). If
107 * 'basename' is NULL, uses the name of the current program for the name of
108 * the pidfile.
110 * Returns a pointer to a dynamically-allocatd string containing the absolute
111 * path to the pidfile; NULL on failure. */
112 static char *
113 generate_varrun_path(const char *bname)
115 char *path;
117 if (bname == NULL)
118 bname = getprogname();
120 /* _PATH_VARRUN includes trailing / */
121 if (asprintf(&path, "%s%s.pid", _PATH_VARRUN, bname) == -1)
122 return NULL;
123 return path;
126 /* Creates a pidfile with the provided name. The new pidfile is "registered"
127 * in the global variables pidfile_path and pidfile_pid so that any further
128 * call to pidfile(3) can check if we are recreating the same file or a new
129 * one.
131 * Returns 0 on success or -1 if there is any error. */
132 static int
133 create_pidfile(const char* path)
135 FILE *f;
137 if (register_atexit_handler() == -1)
138 return -1;
140 if (cleanup_old_pidfile(path) == 0)
141 return 0;
143 pidfile_path = strdup(path);
144 if (pidfile_path == NULL)
145 return -1;
147 if ((f = fopen(path, "w")) == NULL) {
148 free(pidfile_path);
149 pidfile_path = NULL;
150 return -1;
153 pidfile_pid = getpid();
155 (void) fprintf(f, "%d\n", pidfile_pid);
156 (void) fclose(f);
158 return 0;
162 pidfile(const char *path)
165 if (path == NULL || strchr(path, '/') == NULL) {
166 char *default_path;
168 if ((default_path = generate_varrun_path(path)) == NULL)
169 return -1;
171 if (create_pidfile(default_path) == -1) {
172 free(default_path);
173 return -1;
176 free(default_path);
177 return 0;
178 } else
179 return create_pidfile(path);