1224989Sjonathan/*-
2224989Sjonathan * Copyright (c) 2009-2011 Robert N. M. Watson
3224989Sjonathan * Copyright (c) 2011 Jonathan Anderson
4224989Sjonathan * All rights reserved.
5224989Sjonathan *
6224989Sjonathan * Redistribution and use in source and binary forms, with or without
7224989Sjonathan * modification, are permitted provided that the following conditions
8224989Sjonathan * are met:
9224989Sjonathan * 1. Redistributions of source code must retain the above copyright
10224989Sjonathan *    notice, this list of conditions and the following disclaimer.
11224989Sjonathan * 2. Redistributions in binary form must reproduce the above copyright
12224989Sjonathan *    notice, this list of conditions and the following disclaimer in the
13224989Sjonathan *    documentation and/or other materials provided with the distribution.
14224989Sjonathan *
15224989Sjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16224989Sjonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17224989Sjonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18224989Sjonathan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19224989Sjonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20224989Sjonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21224989Sjonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22224989Sjonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23224989Sjonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24224989Sjonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25224989Sjonathan * SUCH DAMAGE.
26224989Sjonathan *
27224989Sjonathan * $FreeBSD$
28224989Sjonathan */
29224989Sjonathan
30224989Sjonathan/*
31224989Sjonathan * Test routines to make sure a variety of system calls are or are not
32224989Sjonathan * available in capability mode.  The goal is not to see if they work, just
33224989Sjonathan * whether or not they return the expected ECAPMODE.
34224989Sjonathan */
35224989Sjonathan
36224989Sjonathan#include <sys/cdefs.h>
37224989Sjonathan__FBSDID("$FreeBSD$");
38224989Sjonathan
39224989Sjonathan#include <sys/types.h>
40224989Sjonathan
41263234Srwatson#include <sys/capsium.h>
42224989Sjonathan#include <sys/errno.h>
43224989Sjonathan#include <sys/procdesc.h>
44224989Sjonathan#include <sys/resource.h>
45224989Sjonathan#include <sys/stat.h>
46224989Sjonathan#include <sys/wait.h>
47224989Sjonathan
48224989Sjonathan#include <err.h>
49224989Sjonathan#include <stdlib.h>
50224989Sjonathan#include <string.h>
51224989Sjonathan#include <unistd.h>
52224989Sjonathan
53224989Sjonathan#include <stdio.h>
54224989Sjonathan#include <time.h>
55224989Sjonathan
56224989Sjonathan#include "cap_test.h"
57224989Sjonathan
58224989Sjonathanint
59224989Sjonathantest_pdfork(void)
60224989Sjonathan{
61224989Sjonathan	struct stat stat;
62224989Sjonathan	int success = PASSED;
63224989Sjonathan	int pd, error;
64224989Sjonathan	pid_t pid;
65224989Sjonathan	time_t now;
66224989Sjonathan
67224989Sjonathan	//cap_enter();
68224989Sjonathan
69224989Sjonathan	pid = pdfork(&pd, 0);
70224989Sjonathan	if (pid < 0)
71224989Sjonathan		err(-1, "pdfork");
72224989Sjonathan
73224989Sjonathan	else if (pid == 0) {
74224989Sjonathan		/*
75224989Sjonathan		 * Child process.
76224989Sjonathan		 *
77224989Sjonathan		 * pd should not be a valid process descriptor.
78224989Sjonathan		 */
79224989Sjonathan		error = pdgetpid(pd, &pid);
80224989Sjonathan		if (error != -1)
81224989Sjonathan			FAILX("pdgetpid succeeded");
82224989Sjonathan		else if (errno != EBADF)
83224989Sjonathan			FAIL("pdgetpid failed, but errno != EBADF");
84224989Sjonathan
85224989Sjonathan		exit(success);
86224989Sjonathan	}
87224989Sjonathan
88224989Sjonathan	/* Parent process. Ensure that [acm]times have been set correctly. */
89224989Sjonathan	REQUIRE(fstat(pd, &stat));
90224989Sjonathan
91224989Sjonathan	now = time(NULL);
92224989Sjonathan	CHECK(now != (time_t)-1);
93224989Sjonathan
94224989Sjonathan	CHECK(now >= stat.st_birthtime);
95224989Sjonathan	CHECK((now - stat.st_birthtime) < 2);
96224989Sjonathan	CHECK(stat.st_birthtime == stat.st_atime);
97224989Sjonathan	CHECK(stat.st_atime == stat.st_ctime);
98224989Sjonathan	CHECK(stat.st_ctime == stat.st_mtime);
99224989Sjonathan
100224989Sjonathan	/* Wait for the child to finish. */
101224989Sjonathan	error = pdgetpid(pd, &pid);
102224989Sjonathan	CHECK(error == 0);
103224989Sjonathan	CHECK(pid > 0);
104224989Sjonathan
105224989Sjonathan	int status;
106224989Sjonathan	while (waitpid(pid, &status, 0) != pid) {}
107224989Sjonathan	if ((success == PASSED) && WIFEXITED(status))
108224989Sjonathan		success = WEXITSTATUS(status);
109224989Sjonathan	else
110224989Sjonathan		success = FAILED;
111224989Sjonathan
112224989Sjonathan	return (success);
113224989Sjonathan}
114