1b465884fSJonathan Anderson /*-
2b465884fSJonathan Anderson * Copyright (c) 2009-2011 Robert N. M. Watson
3b465884fSJonathan Anderson * Copyright (c) 2011 Jonathan Anderson
4b465884fSJonathan Anderson * All rights reserved.
5b465884fSJonathan Anderson *
6b465884fSJonathan Anderson * Redistribution and use in source and binary forms, with or without
7b465884fSJonathan Anderson * modification, are permitted provided that the following conditions
8b465884fSJonathan Anderson * are met:
9b465884fSJonathan Anderson * 1. Redistributions of source code must retain the above copyright
10b465884fSJonathan Anderson * notice, this list of conditions and the following disclaimer.
11b465884fSJonathan Anderson * 2. Redistributions in binary form must reproduce the above copyright
12b465884fSJonathan Anderson * notice, this list of conditions and the following disclaimer in the
13b465884fSJonathan Anderson * documentation and/or other materials provided with the distribution.
14b465884fSJonathan Anderson *
15b465884fSJonathan Anderson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b465884fSJonathan Anderson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b465884fSJonathan Anderson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b465884fSJonathan Anderson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b465884fSJonathan Anderson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b465884fSJonathan Anderson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b465884fSJonathan Anderson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b465884fSJonathan Anderson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b465884fSJonathan Anderson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b465884fSJonathan Anderson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b465884fSJonathan Anderson * SUCH DAMAGE.
26b465884fSJonathan Anderson */
27b465884fSJonathan Anderson
28b465884fSJonathan Anderson /*
29b465884fSJonathan Anderson * Test routines to make sure a variety of system calls are or are not
30b465884fSJonathan Anderson * available in capability mode. The goal is not to see if they work, just
31b465884fSJonathan Anderson * whether or not they return the expected ECAPMODE.
32b465884fSJonathan Anderson */
33b465884fSJonathan Anderson
34b465884fSJonathan Anderson #include <sys/types.h>
35b465884fSJonathan Anderson
36*b881b8beSRobert Watson #include <sys/capsium.h>
37b465884fSJonathan Anderson #include <sys/errno.h>
38b465884fSJonathan Anderson #include <sys/procdesc.h>
39b465884fSJonathan Anderson #include <sys/resource.h>
40b465884fSJonathan Anderson #include <sys/stat.h>
41b465884fSJonathan Anderson #include <sys/wait.h>
42b465884fSJonathan Anderson
43b465884fSJonathan Anderson #include <err.h>
44b465884fSJonathan Anderson #include <stdlib.h>
45b465884fSJonathan Anderson #include <string.h>
46b465884fSJonathan Anderson #include <unistd.h>
47b465884fSJonathan Anderson
48b465884fSJonathan Anderson #include <stdio.h>
49b465884fSJonathan Anderson #include <time.h>
50b465884fSJonathan Anderson
51b465884fSJonathan Anderson #include "cap_test.h"
52b465884fSJonathan Anderson
53b465884fSJonathan Anderson int
test_pdfork(void)54b465884fSJonathan Anderson test_pdfork(void)
55b465884fSJonathan Anderson {
56b465884fSJonathan Anderson struct stat stat;
57b465884fSJonathan Anderson int success = PASSED;
58b465884fSJonathan Anderson int pd, error;
59b465884fSJonathan Anderson pid_t pid;
60b465884fSJonathan Anderson time_t now;
61b465884fSJonathan Anderson
62b465884fSJonathan Anderson //cap_enter();
63b465884fSJonathan Anderson
64b465884fSJonathan Anderson pid = pdfork(&pd, 0);
65b465884fSJonathan Anderson if (pid < 0)
66b465884fSJonathan Anderson err(-1, "pdfork");
67b465884fSJonathan Anderson
68b465884fSJonathan Anderson else if (pid == 0) {
69b465884fSJonathan Anderson /*
70b465884fSJonathan Anderson * Child process.
71b465884fSJonathan Anderson *
72b465884fSJonathan Anderson * pd should not be a valid process descriptor.
73b465884fSJonathan Anderson */
74b465884fSJonathan Anderson error = pdgetpid(pd, &pid);
75b465884fSJonathan Anderson if (error != -1)
76b465884fSJonathan Anderson FAILX("pdgetpid succeeded");
77b465884fSJonathan Anderson else if (errno != EBADF)
78b465884fSJonathan Anderson FAIL("pdgetpid failed, but errno != EBADF");
79b465884fSJonathan Anderson
80b465884fSJonathan Anderson exit(success);
81b465884fSJonathan Anderson }
82b465884fSJonathan Anderson
83b465884fSJonathan Anderson /* Parent process. Ensure that [acm]times have been set correctly. */
84b465884fSJonathan Anderson REQUIRE(fstat(pd, &stat));
85b465884fSJonathan Anderson
86b465884fSJonathan Anderson now = time(NULL);
87b465884fSJonathan Anderson CHECK(now != (time_t)-1);
88b465884fSJonathan Anderson
89b465884fSJonathan Anderson CHECK(now >= stat.st_birthtime);
90b465884fSJonathan Anderson CHECK((now - stat.st_birthtime) < 2);
91b465884fSJonathan Anderson CHECK(stat.st_birthtime == stat.st_atime);
92b465884fSJonathan Anderson CHECK(stat.st_atime == stat.st_ctime);
93b465884fSJonathan Anderson CHECK(stat.st_ctime == stat.st_mtime);
94b465884fSJonathan Anderson
95b465884fSJonathan Anderson /* Wait for the child to finish. */
96b465884fSJonathan Anderson error = pdgetpid(pd, &pid);
97b465884fSJonathan Anderson CHECK(error == 0);
98b465884fSJonathan Anderson CHECK(pid > 0);
99b465884fSJonathan Anderson
100b465884fSJonathan Anderson int status;
101b465884fSJonathan Anderson while (waitpid(pid, &status, 0) != pid) {}
102b465884fSJonathan Anderson if ((success == PASSED) && WIFEXITED(status))
103b465884fSJonathan Anderson success = WEXITSTATUS(status);
104b465884fSJonathan Anderson else
105b465884fSJonathan Anderson success = FAILED;
106b465884fSJonathan Anderson
107b465884fSJonathan Anderson return (success);
108b465884fSJonathan Anderson }
109