2 * Copyright (c) 2007-2009 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
46 * We need a signal handler so kill(2) will interrupt our child's
47 * select(2) instead of killing it.
50 signal_handler(int sig)
56 * Test that pidfile_open() can create a pidfile and that pidfile_write()
60 test_pidfile_uncontested(void)
62 const char *fn = "test_pidfile_uncontested";
67 pf = pidfile_open(fn, 0600, &other);
68 if (pf == NULL && other != 0)
69 return ("pidfile exists and is locked");
71 return (strerror(errno));
72 if (pidfile_write(pf) != 0) {
75 return ("failed to write PID");
83 * Test that pidfile_open() locks against self.
86 test_pidfile_self(void)
88 const char *fn = "test_pidfile_self";
89 struct pidfh *pf1, *pf2;
94 pf1 = pidfile_open(fn, 0600, &other);
95 if (pf1 == NULL && other != 0)
96 return ("pidfile exists and is locked");
98 return (strerror(errno));
99 if (pidfile_write(pf1) != 0) {
103 return (strerror(serrno));
105 // second open should fail
106 pf2 = pidfile_open(fn, 0600, &other);
111 return ("managed to opened pidfile twice");
113 if (other != getpid()) {
116 return ("pidfile contained wrong PID");
124 * Common code for test_pidfile_{contested,inherited}.
127 common_test_pidfile_child(const char *fn, int parent_open)
129 struct pidfh *pf = NULL;
130 pid_t other = 0, pid = 0;
131 int fd[2], serrno, status;
136 return (strerror(errno));
139 pf = pidfile_open(fn, 0600, &other);
140 if (pf == NULL && other != 0)
141 return ("pidfile exists and is locked");
143 return (strerror(errno));
148 return (strerror(errno));
152 signal(SIGINT, signal_handler);
154 pf = pidfile_open(fn, 0600, &other);
155 if (pf == NULL && other != 0)
156 return ("pidfile exists and is locked");
158 return (strerror(errno));
160 if (pidfile_write(pf) != 0) {
164 return (strerror(serrno));
168 if (pidfile_write(pf) != 0)
170 if (write(fd[1], "*", 1) != 1)
172 select(0, 0, 0, 0, 0);
180 // wait for the child to signal us
181 if (read(fd[0], &ch, 1) != 1) {
186 return (strerror(errno));
189 // We shouldn't be able to lock the same pidfile as our child
190 pf = pidfile_open(fn, 0600, &other);
194 return ("managed to lock contested pidfile");
197 // Failed to lock, but not because it was contested
200 return (strerror(errno));
203 // Locked by the wrong process
206 return ("pidfile contained wrong PID");
209 // check our child's fate
213 if (kill(pid, SIGINT) != 0)
214 return (strerror(errno));
215 if (waitpid(pid, &status, 0) == -1)
216 return (strerror(errno));
217 if (WIFSIGNALED(status))
218 return ("child caught signal");
219 if (WEXITSTATUS(status) != 0)
220 return ("child returned non-zero status");
227 * Test that pidfile_open() fails when attempting to open a pidfile that
228 * is already locked, and that it returns the correct PID.
231 test_pidfile_contested(void)
233 const char *fn = "test_pidfile_contested";
236 result = common_test_pidfile_child(fn, 0);
241 * Test that the pidfile lock is inherited.
244 test_pidfile_inherited(void)
246 const char *fn = "test_pidfile_inherited";
249 result = common_test_pidfile_child(fn, 1);
255 const char *(*func)(void);
257 { "pidfile_uncontested", test_pidfile_uncontested },
258 { "pidfile_self", test_pidfile_self },
259 { "pidfile_contested", test_pidfile_contested },
260 { "pidfile_inherited", test_pidfile_inherited },
269 nt = sizeof(t) / sizeof(*t);
270 printf("1..%d\n", nt);
271 for (i = 0; i < nt; ++i) {
272 if ((result = t[i].func()) != NULL)
273 printf("not ok %d - %s # %s\n", i + 1,
276 printf("ok %d - %s\n", i + 1,