1/* $NetBSD: t_pidfile.c,v 1.3 2011/03/29 13:55:37 jmmv Exp $ */
2
3/*
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * This file implements tests for the pidfile(3) functions.
31 *
32 * The tests here are tricky because we need to validate that the atexit(3)
33 * handler registered by pidfile(3) correctly deletes the generated pidfile.
34 * To do so:
35 * 1) We spawn a subprocess in every test case,
36 * 2) Run our test code in such subprocesses.  We cannot call any of the ATF
37 *    primitives from inside these.
38 * 3) Wait for the subprocess to terminate and ensure it exited successfully.
39 * 4) Check that the pidfile(s) created in the subprocess are gone.
40 *
41 * Additionally, pidfile(3) hardcodes a path to a directory writable only by
42 * root (/var/run).  This makes us require root privileges to execute these
43 * tests.
44 */
45
46#include <sys/cdefs.h>
47__COPYRIGHT("@(#) Copyright (c) 2011\
48 The NetBSD Foundation, inc. All rights reserved.");
49__RCSID("$NetBSD: t_pidfile.c,v 1.3 2011/03/29 13:55:37 jmmv Exp $");
50
51#include <sys/stat.h>
52#include <sys/wait.h>
53
54#include <assert.h>
55#include <err.h>
56#include <stdbool.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <paths.h>
60#include <unistd.h>
61#include <util.h>
62
63#include <atf-c.h>
64
65/* Used by routines that can be called both from the parent and the child
66 * code to implement proper error reporting. */
67static bool in_child = false;
68
69/* Callable from the test case child code. */
70static void
71check_pidfile(const char *path)
72{
73	FILE *file;
74	int pid;
75
76	printf("Validating contents of pidfile '%s'\n", path);
77
78	if ((file = fopen(path, "r")) == NULL)
79		errx(EXIT_FAILURE, "Cannot open expected pidfile '%s'", path);
80
81	if (fscanf(file, "%d", &pid) == -1)
82		errx(EXIT_FAILURE, "Failed to read pid from pidfile '%s'",
83		    path);
84
85	printf("Read pid %d, current pid %d\n", pid, getpid());
86	if (pid != getpid())
87		errx(EXIT_FAILURE, "Pid in pidfile (%d) does not match "
88		    "current pid (%d)", pid, getpid());
89}
90
91/* Callable from the test case parent/child code. */
92static void
93ensure_deleted(const char *path)
94{
95	printf("Ensuring pidfile %s does not exist any more\n", path);
96	if (access(path, R_OK) != -1) {
97		unlink(path);
98		if (in_child)
99			errx(EXIT_FAILURE, "The pidfile %s was not deleted",
100			    path);
101		else
102			atf_tc_fail("The pidfile %s was not deleted", path);
103	}
104}
105
106/* Callable from the test case parent code. */
107static void
108run_child(void (*child)(const char *), const char *cookie)
109{
110	pid_t pid;
111
112	pid = fork();
113	ATF_REQUIRE(pid != -1);
114	if (pid == 0) {
115		in_child = true;
116		child(cookie);
117		assert(false);
118		/* UNREACHABLE */
119	} else {
120		int status;
121
122		ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
123		if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS)
124			atf_tc_fail("See stderr for details");
125	}
126}
127
128/* Callable from the test case parent/child code. */
129static char *
130generate_varrun_pidfile(const char *basename)
131{
132	char *path;
133
134	if (asprintf(&path, "%s%s.pid", _PATH_VARRUN,
135	    basename == NULL ? getprogname() : basename) == -1) {
136		if (in_child)
137			errx(EXIT_FAILURE, "Cannot allocate memory for path");
138		else
139			atf_tc_fail("Cannot allocate memory for path");
140	}
141
142	return path;
143}
144
145static void
146helper_default_path(const char *path)
147{
148
149	if (pidfile(NULL) == -1)
150		errx(EXIT_FAILURE, "Failed to create pidfile with default "
151		    "basename");
152
153	check_pidfile(path);
154	exit(EXIT_SUCCESS);
155}
156
157ATF_TC(default_path);
158ATF_TC_HEAD(default_path, tc)
159{
160	atf_tc_set_md_var(tc, "require.user", "root");
161}
162ATF_TC_BODY(default_path, tc)
163{
164	char *path;
165
166	path = generate_varrun_pidfile(NULL);
167	run_child(helper_default_path, path);
168	ensure_deleted(path);
169	free(path);
170}
171
172static void
173helper_custom_basename(const char *path)
174{
175
176	if (pidfile("custom-basename") == -1)
177		errx(EXIT_FAILURE, "Failed to create pidfile with custom "
178		    "basename");
179
180	check_pidfile(path);
181	exit(EXIT_SUCCESS);
182}
183
184ATF_TC(custom_basename);
185ATF_TC_HEAD(custom_basename, tc)
186{
187	atf_tc_set_md_var(tc, "require.user", "root");
188}
189ATF_TC_BODY(custom_basename, tc)
190{
191	char *path;
192
193	path = generate_varrun_pidfile("custom-basename");
194	run_child(helper_custom_basename, path);
195	ensure_deleted(path);
196	free(path);
197}
198
199static void
200helper_custom_path(const char *path)
201{
202
203	if (pidfile(path) == -1)
204		errx(EXIT_FAILURE, "Failed to create pidfile '%s'", path);
205	check_pidfile(path);
206	exit(EXIT_SUCCESS);
207}
208
209ATF_TC_WITHOUT_HEAD(custom_path);
210ATF_TC_BODY(custom_path, tc)
211{
212
213	ATF_REQUIRE(mkdir("var", 0777) != -1);
214	ATF_REQUIRE(mkdir("var/run", 0777) != -1);
215
216	run_child(helper_custom_path, "./var/run/my-pidfile.pid");
217
218	ensure_deleted("./var/run/my-pidfile.pid");
219}
220
221static void
222helper_change_basenames(const char *unused_cookie)
223{
224	char *default_path;
225	char *custom_path;
226
227	default_path = generate_varrun_pidfile(NULL);
228	if (pidfile(NULL) == -1)
229		errx(EXIT_FAILURE, "Failed to create pidfile with default "
230		    "basename");
231	check_pidfile(default_path);
232	if (pidfile(NULL) == -1)
233		errx(EXIT_FAILURE, "Failed to recreate pidfile with default "
234		    "basename");
235	check_pidfile(default_path);
236
237	custom_path = generate_varrun_pidfile("custom-basename");
238	if (pidfile("custom-basename") == -1)
239		errx(EXIT_FAILURE, "Failed to create pidfile with custom "
240		    "basename");
241	ensure_deleted(default_path);
242	check_pidfile(custom_path);
243	if (pidfile("custom-basename") == -1)
244		errx(EXIT_FAILURE, "Failed to recreate pidfile with custom "
245		    "basename");
246	check_pidfile(custom_path);
247
248	free(custom_path);
249	free(default_path);
250	exit(EXIT_SUCCESS);
251}
252
253ATF_TC(change_basenames);
254ATF_TC_HEAD(change_basenames, tc)
255{
256	atf_tc_set_md_var(tc, "require.user", "root");
257}
258ATF_TC_BODY(change_basenames, tc)
259{
260	char *default_path;
261	char *custom_path;
262
263	run_child(helper_change_basenames, NULL);
264
265	default_path = generate_varrun_pidfile(NULL);
266	custom_path = generate_varrun_pidfile("custom-basename");
267
268	ensure_deleted(default_path);
269	ensure_deleted(custom_path);
270
271	free(custom_path);
272	free(default_path);
273}
274
275static void
276helper_change_paths(const char *unused_cookie)
277{
278
279	if (pidfile("./var/run/first.pid") == -1)
280		errx(EXIT_FAILURE, "Failed to create pidfile "
281		    "'./var/run/first.pid'");
282	check_pidfile("./var/run/first.pid");
283
284	if (pidfile("./second.pid") == -1)
285		errx(EXIT_FAILURE, "Failed to create pidfile 'second.pid'");
286	ensure_deleted("./var/run/first.pid");
287	check_pidfile("./second.pid");
288
289	exit(EXIT_SUCCESS);
290}
291
292ATF_TC_WITHOUT_HEAD(change_paths);
293ATF_TC_BODY(change_paths, tc)
294{
295
296	ATF_REQUIRE(mkdir("var", 0777) != -1);
297	ATF_REQUIRE(mkdir("var/run", 0777) != -1);
298
299	run_child(helper_change_paths, NULL);
300
301	ensure_deleted("./var/run/my-pidfile.pid");
302	ensure_deleted("second.pid");
303}
304
305static void
306helper_mix(const char *unused_cookie)
307{
308	char *default_path;
309	char *custom_path;
310
311	default_path = generate_varrun_pidfile(NULL);
312	custom_path = generate_varrun_pidfile("custom-basename");
313
314	if (pidfile(NULL) == -1)
315		errx(EXIT_FAILURE, "Failed to create default pidfile");
316	check_pidfile(default_path);
317
318	if (pidfile("./second.pid") == -1)
319		errx(EXIT_FAILURE, "Failed to create pidfile 'second.pid'");
320	ensure_deleted(default_path);
321	check_pidfile("./second.pid");
322
323	if (pidfile("custom-basename") == -1)
324		errx(EXIT_FAILURE, "Failed to create pidfile 'second.pid'");
325	ensure_deleted(default_path);
326	ensure_deleted("./second.pid");
327	ensure_deleted("./custom-basename");
328	check_pidfile(custom_path);
329
330	free(custom_path);
331	free(default_path);
332	exit(EXIT_SUCCESS);
333}
334
335ATF_TC(change_mix);
336ATF_TC_HEAD(change_mix, tc)
337{
338	atf_tc_set_md_var(tc, "require.user", "root");
339}
340ATF_TC_BODY(change_mix, tc)
341{
342	char *default_path;
343
344	run_child(helper_mix, NULL);
345
346	default_path = generate_varrun_pidfile(NULL);
347	ensure_deleted(default_path);
348	ensure_deleted("second.pid");
349	free(default_path);
350}
351
352ATF_TP_ADD_TCS(tp)
353{
354
355	ATF_TP_ADD_TC(tp, default_path);
356	ATF_TP_ADD_TC(tp, custom_basename);
357	ATF_TP_ADD_TC(tp, custom_path);
358	ATF_TP_ADD_TC(tp, change_basenames);
359	ATF_TP_ADD_TC(tp, change_paths);
360	ATF_TP_ADD_TC(tp, change_mix);
361
362	return atf_no_error();
363}
364