148411Swes/*-
248411Swes * Copyright (c) 2009-2011 Robert N. M. Watson
348411Swes * Copyright (c) 2011 Jonathan Anderson
448411Swes * All rights reserved.
548411Swes *
648411Swes * Redistribution and use in source and binary forms, with or without
748411Swes * modification, are permitted provided that the following conditions
848411Swes * are met:
948411Swes * 1. Redistributions of source code must retain the above copyright
1048411Swes *    notice, this list of conditions and the following disclaimer.
1148411Swes * 2. Redistributions in binary form must reproduce the above copyright
1248411Swes *    notice, this list of conditions and the following disclaimer in the
1348411Swes *    documentation and/or other materials provided with the distribution.
1448411Swes *
1548411Swes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1648411Swes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1748411Swes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1848411Swes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1948411Swes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2048411Swes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2148411Swes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2248411Swes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2348411Swes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2448411Swes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2550476Speter * SUCH DAMAGE.
2648411Swes *
2748411Swes * $FreeBSD$
2848411Swes */
2948411Swes
3048411Swes/*
3148411Swes * Test routines to make sure a variety of system calls are or are not
3248421Smpp * available in capability mode.  The goal is not to see if they work, just
3359460Sphantom * whether or not they return the expected ECAPMODE.
3459460Sphantom */
3548411Swes
3684306Sru#include <sys/cdefs.h>
3748411Swes__FBSDID("$FreeBSD$");
38177870Skevlo
3948411Swes#include <sys/types.h>
4048411Swes
4148411Swes#include <sys/capsium.h>
42108028Sru#include <sys/errno.h>
4351470Swes#include <sys/procdesc.h>
4451470Swes#include <sys/resource.h>
4561988Schris#include <sys/stat.h>
4648411Swes#include <sys/wait.h>
4751470Swes
48108087Sru#include <err.h>
4961988Schris#include <stdlib.h>
50108087Sru#include <string.h>
5179754Sdd#include <unistd.h>
5261988Schris
53131504Sru#include <stdio.h>
54131504Sru#include <time.h>
55139253Skeramida
5652041Swes#include "cap_test.h"
5752041Swes
5861988Schrisint
59139253Skeramidatest_pdfork(void)
6052041Swes{
6161988Schris	struct stat stat;
62139253Skeramida	int success = PASSED;
63131504Sru	int pd, error;
6452041Swes	pid_t pid;
6561988Schris	time_t now;
6652041Swes
6748411Swes	//cap_enter();
6851470Swes
6951470Swes	pid = pdfork(&pd, 0);
7048411Swes	if (pid < 0)
71131504Sru		err(-1, "pdfork");
72131504Sru
7351470Swes	else if (pid == 0) {
7451470Swes		/*
7548411Swes		 * Child process.
7648411Swes		 *
7748411Swes		 * pd should not be a valid process descriptor.
78108028Sru		 */
7948411Swes		error = pdgetpid(pd, &pid);
8051470Swes		if (error != -1)
8179754Sdd			FAILX("pdgetpid succeeded");
8261988Schris		else if (errno != EBADF)
8351470Swes			FAIL("pdgetpid failed, but errno != EBADF");
8448411Swes
85108087Sru		exit(success);
8661988Schris	}
87108087Sru
8848411Swes	/* Parent process. Ensure that [acm]times have been set correctly. */
8951509Swes	REQUIRE(fstat(pd, &stat));
9051509Swes
9151509Swes	now = time(NULL);
9248411Swes	CHECK(now != (time_t)-1);
9348411Swes
9448411Swes	CHECK(now >= stat.st_birthtime);
95105720Salfred	CHECK((now - stat.st_birthtime) < 2);
96105720Salfred	CHECK(stat.st_birthtime == stat.st_atime);
97105720Salfred	CHECK(stat.st_atime == stat.st_ctime);
98105720Salfred	CHECK(stat.st_ctime == stat.st_mtime);
99105720Salfred
100105720Salfred	/* Wait for the child to finish. */
101105864Ssheldonh	error = pdgetpid(pd, &pid);
10248411Swes	CHECK(error == 0);
103108028Sru	CHECK(pid > 0);
10448411Swes
105108028Sru	int status;
10648411Swes	while (waitpid(pid, &status, 0) != pid) {}
107109174Stjr	if ((success == PASSED) && WIFEXITED(status))
10848411Swes		success = WEXITSTATUS(status);
10948411Swes	else
11048411Swes		success = FAILED;
111108028Sru
112108028Sru	return (success);
11348411Swes}
11449828Smpp