etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libutil / t_pidfile.c
bloba2aff833df380abc13c08d79e53a1889d752dec6
1 /* $NetBSD: t_pidfile.c,v 1.3 2011/03/29 13:55:37 jmmv Exp $ */
3 /*
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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.
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.
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 $");
51 #include <sys/stat.h>
52 #include <sys/wait.h>
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>
63 #include <atf-c.h>
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. */
70 static void
71 check_pidfile(const char *path)
73 FILE *file;
74 int pid;
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'",
83 path);
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());
91 /* Callable from the test case parent/child code. */
92 static void
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) {
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);
106 /* Callable from the test case parent code. */
107 static void
108 run_child(void (*child)(const char *), const char *cookie)
110 pid_t pid;
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;
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. */
129 static char *
130 generate_varrun_pidfile(const char *basename)
132 char *path;
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");
142 return path;
145 static void
146 helper_default_path(const char *path)
149 if (pidfile(NULL) == -1)
150 errx(EXIT_FAILURE, "Failed to create pidfile with default "
151 "basename");
153 check_pidfile(path);
154 exit(EXIT_SUCCESS);
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)
164 char *path;
166 path = generate_varrun_pidfile(NULL);
167 run_child(helper_default_path, path);
168 ensure_deleted(path);
169 free(path);
172 static void
173 helper_custom_basename(const char *path)
176 if (pidfile("custom-basename") == -1)
177 errx(EXIT_FAILURE, "Failed to create pidfile with custom "
178 "basename");
180 check_pidfile(path);
181 exit(EXIT_SUCCESS);
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)
191 char *path;
193 path = generate_varrun_pidfile("custom-basename");
194 run_child(helper_custom_basename, path);
195 ensure_deleted(path);
196 free(path);
199 static void
200 helper_custom_path(const char *path)
203 if (pidfile(path) == -1)
204 errx(EXIT_FAILURE, "Failed to create pidfile '%s'", path);
205 check_pidfile(path);
206 exit(EXIT_SUCCESS);
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");
221 static void
222 helper_change_basenames(const char *unused_cookie)
224 char *default_path;
225 char *custom_path;
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);
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);
248 free(custom_path);
249 free(default_path);
250 exit(EXIT_SUCCESS);
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)
260 char *default_path;
261 char *custom_path;
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);
271 free(custom_path);
272 free(default_path);
275 static void
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");
289 exit(EXIT_SUCCESS);
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");
305 static void
306 helper_mix(const char *unused_cookie)
308 char *default_path;
309 char *custom_path;
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);
330 free(custom_path);
331 free(default_path);
332 exit(EXIT_SUCCESS);
335 ATF_TC(change_mix);
336 ATF_TC_HEAD(change_mix, tc)
338 atf_tc_set_md_var(tc, "require.user", "root");
340 ATF_TC_BODY(change_mix, tc)
342 char *default_path;
344 run_child(helper_mix, NULL);
346 default_path = generate_varrun_pidfile(NULL);
347 ensure_deleted(default_path);
348 ensure_deleted("second.pid");
349 free(default_path);
352 ATF_TP_ADD_TCS(tp)
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();