1208554Sglebius/*-
2208554Sglebius * Copyright (c) 2009-2011 Robert N. M. Watson
3208554Sglebius * Copyright (c) 2011 Jonathan Anderson
4208554Sglebius * All rights reserved.
5208554Sglebius *
6208554Sglebius * Redistribution and use in source and binary forms, with or without
7208554Sglebius * modification, are permitted provided that the following conditions
8208554Sglebius * are met:
9208554Sglebius * 1. Redistributions of source code must retain the above copyright
10208554Sglebius *    notice, this list of conditions and the following disclaimer.
11208554Sglebius * 2. Redistributions in binary form must reproduce the above copyright
12208554Sglebius *    notice, this list of conditions and the following disclaimer in the
13208554Sglebius *    documentation and/or other materials provided with the distribution.
14208554Sglebius *
15208554Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16208554Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17208554Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18208554Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19208554Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20208554Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21208554Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22208554Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23208554Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24208554Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25208554Sglebius * SUCH DAMAGE.
26208554Sglebius *
27208554Sglebius * $FreeBSD$
28208554Sglebius */
29208554Sglebius
30208554Sglebius/*
31208554Sglebius * Test routines to make sure a variety of system calls are or are not
32208554Sglebius * available in capability mode.  The goal is not to see if they work, just
33208554Sglebius * whether or not they return the expected ECAPMODE.
34208554Sglebius */
35208554Sglebius
36208554Sglebius#include <sys/cdefs.h>
37208554Sglebius__FBSDID("$FreeBSD$");
38208554Sglebius
39208554Sglebius#include <sys/types.h>
40208554Sglebius
41208554Sglebius#include <sys/capability.h>
42208554Sglebius#include <sys/errno.h>
43208554Sglebius#include <sys/procdesc.h>
44208554Sglebius#include <sys/resource.h>
45208554Sglebius#include <sys/wait.h>
46208554Sglebius
47208554Sglebius#include <err.h>
48208554Sglebius#include <signal.h>
49208554Sglebius#include <stdlib.h>
50208554Sglebius#include <string.h>
51208554Sglebius#include <unistd.h>
52208554Sglebius
53208554Sglebius#include <stdio.h>
54210676Sjoel
55208554Sglebius#include "cap_test.h"
56208554Sglebius
57208554Sglebiusvoid handle_signal(int);
58208554Sglebiusvoid handle_signal(int sig) {
59208554Sglebius	exit(PASSED);
60208554Sglebius}
61208554Sglebius
62208554Sglebiusint
63208554Sglebiustest_pdkill(void)
64208554Sglebius{
65208554Sglebius	int success = PASSED;
66208554Sglebius	int pd, error;
67208554Sglebius	pid_t pid;
68208554Sglebius
69208554Sglebius	//cap_enter();
70208554Sglebius
71208554Sglebius	error = pdfork(&pd, 0);
72208554Sglebius	if (error < 0)
73208554Sglebius		err(-1, "pdfork");
74208554Sglebius
75222600Suqs	else if (error == 0) {
76222600Suqs		signal(SIGINT, handle_signal);
77222600Suqs		sleep(3600);
78222600Suqs		exit(FAILED);
79222600Suqs	}
80
81	/* Parent process; find the child's PID (we'll need it later). */
82	error = pdgetpid(pd, &pid);
83	if (error != 0)
84		FAIL("pdgetpid");
85
86	/* Kill the child! */
87	usleep(100);
88	error = pdkill(pd, SIGINT);
89	if (error != 0)
90		FAIL("pdkill");
91
92	/* Make sure the child finished properly. */
93	int status;
94	while (waitpid(pid, &status, 0) != pid) {}
95	if ((success == PASSED) && WIFEXITED(status))
96		success = WEXITSTATUS(status);
97	else
98		success = FAILED;
99
100	return (success);
101}
102