1 /* $NetBSD: t_pidfile.c,v 1.3 2011/03/29 13:55:37 jmmv Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 * This file implements tests for the pidfile(3) functions.
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.
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.
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
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 $");
65 /* Used by routines that can be called both from the parent and the child
66 * code to implement proper error reporting. */
67 static bool in_child
= false;
69 /* Callable from the test case child code. */
71 check_pidfile(const char *path
)
76 printf("Validating contents of pidfile '%s'\n", path
);
78 if ((file
= fopen(path
, "r")) == NULL
)
79 errx(EXIT_FAILURE
, "Cannot open expected pidfile '%s'", path
);
81 if (fscanf(file
, "%d", &pid
) == -1)
82 errx(EXIT_FAILURE
, "Failed to read pid from pidfile '%s'",
85 printf("Read pid %d, current pid %d\n", pid
, getpid());
87 errx(EXIT_FAILURE
, "Pid in pidfile (%d) does not match "
88 "current pid (%d)", pid
, getpid());
91 /* Callable from the test case parent/child code. */
93 ensure_deleted(const char *path
)
95 printf("Ensuring pidfile %s does not exist any more\n", path
);
96 if (access(path
, R_OK
) != -1) {
99 errx(EXIT_FAILURE
, "The pidfile %s was not deleted",
102 atf_tc_fail("The pidfile %s was not deleted", path
);
106 /* Callable from the test case parent code. */
108 run_child(void (*child
)(const char *), const char *cookie
)
113 ATF_REQUIRE(pid
!= -1);
122 ATF_REQUIRE(waitpid(pid
, &status
, 0) != -1);
123 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != EXIT_SUCCESS
)
124 atf_tc_fail("See stderr for details");
128 /* Callable from the test case parent/child code. */
130 generate_varrun_pidfile(const char *basename
)
134 if (asprintf(&path
, "%s%s.pid", _PATH_VARRUN
,
135 basename
== NULL
? getprogname() : basename
) == -1) {
137 errx(EXIT_FAILURE
, "Cannot allocate memory for path");
139 atf_tc_fail("Cannot allocate memory for path");
146 helper_default_path(const char *path
)
149 if (pidfile(NULL
) == -1)
150 errx(EXIT_FAILURE
, "Failed to create pidfile with default "
157 ATF_TC(default_path
);
158 ATF_TC_HEAD(default_path
, tc
)
160 atf_tc_set_md_var(tc
, "require.user", "root");
162 ATF_TC_BODY(default_path
, tc
)
166 path
= generate_varrun_pidfile(NULL
);
167 run_child(helper_default_path
, path
);
168 ensure_deleted(path
);
173 helper_custom_basename(const char *path
)
176 if (pidfile("custom-basename") == -1)
177 errx(EXIT_FAILURE
, "Failed to create pidfile with custom "
184 ATF_TC(custom_basename
);
185 ATF_TC_HEAD(custom_basename
, tc
)
187 atf_tc_set_md_var(tc
, "require.user", "root");
189 ATF_TC_BODY(custom_basename
, tc
)
193 path
= generate_varrun_pidfile("custom-basename");
194 run_child(helper_custom_basename
, path
);
195 ensure_deleted(path
);
200 helper_custom_path(const char *path
)
203 if (pidfile(path
) == -1)
204 errx(EXIT_FAILURE
, "Failed to create pidfile '%s'", path
);
209 ATF_TC_WITHOUT_HEAD(custom_path
);
210 ATF_TC_BODY(custom_path
, tc
)
213 ATF_REQUIRE(mkdir("var", 0777) != -1);
214 ATF_REQUIRE(mkdir("var/run", 0777) != -1);
216 run_child(helper_custom_path
, "./var/run/my-pidfile.pid");
218 ensure_deleted("./var/run/my-pidfile.pid");
222 helper_change_basenames(const char *unused_cookie
)
227 default_path
= generate_varrun_pidfile(NULL
);
228 if (pidfile(NULL
) == -1)
229 errx(EXIT_FAILURE
, "Failed to create pidfile with default "
231 check_pidfile(default_path
);
232 if (pidfile(NULL
) == -1)
233 errx(EXIT_FAILURE
, "Failed to recreate pidfile with default "
235 check_pidfile(default_path
);
237 custom_path
= generate_varrun_pidfile("custom-basename");
238 if (pidfile("custom-basename") == -1)
239 errx(EXIT_FAILURE
, "Failed to create pidfile with custom "
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 "
246 check_pidfile(custom_path
);
253 ATF_TC(change_basenames
);
254 ATF_TC_HEAD(change_basenames
, tc
)
256 atf_tc_set_md_var(tc
, "require.user", "root");
258 ATF_TC_BODY(change_basenames
, tc
)
263 run_child(helper_change_basenames
, NULL
);
265 default_path
= generate_varrun_pidfile(NULL
);
266 custom_path
= generate_varrun_pidfile("custom-basename");
268 ensure_deleted(default_path
);
269 ensure_deleted(custom_path
);
276 helper_change_paths(const char *unused_cookie
)
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");
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");
292 ATF_TC_WITHOUT_HEAD(change_paths
);
293 ATF_TC_BODY(change_paths
, tc
)
296 ATF_REQUIRE(mkdir("var", 0777) != -1);
297 ATF_REQUIRE(mkdir("var/run", 0777) != -1);
299 run_child(helper_change_paths
, NULL
);
301 ensure_deleted("./var/run/my-pidfile.pid");
302 ensure_deleted("second.pid");
306 helper_mix(const char *unused_cookie
)
311 default_path
= generate_varrun_pidfile(NULL
);
312 custom_path
= generate_varrun_pidfile("custom-basename");
314 if (pidfile(NULL
) == -1)
315 errx(EXIT_FAILURE
, "Failed to create default pidfile");
316 check_pidfile(default_path
);
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");
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
);
336 ATF_TC_HEAD(change_mix
, tc
)
338 atf_tc_set_md_var(tc
, "require.user", "root");
340 ATF_TC_BODY(change_mix
, tc
)
344 run_child(helper_mix
, NULL
);
346 default_path
= generate_varrun_pidfile(NULL
);
347 ensure_deleted(default_path
);
348 ensure_deleted("second.pid");
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
);
362 return atf_no_error();