1313227Sngie/*	$NetBSD: t_fexecve.c,v 1.3 2017/01/10 15:15:09 christos Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Emmanuel Dreyfus.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie#include <sys/cdefs.h>
32313227Sngie__RCSID("$NetBSD: t_fexecve.c,v 1.3 2017/01/10 15:15:09 christos Exp $");
33272343Sngie
34272343Sngie#include <sys/wait.h>
35272343Sngie
36272343Sngie#include <atf-c.h>
37272343Sngie#include <err.h>
38272343Sngie#include <errno.h>
39272343Sngie#include <fcntl.h>
40272343Sngie#include <limits.h>
41272343Sngie#include <paths.h>
42272343Sngie#include <stdio.h>
43272343Sngie#include <stdlib.h>
44272343Sngie#include <string.h>
45272343Sngie#include <unistd.h>
46272343Sngie#include <sys/param.h>
47272343Sngie
48272343SngieATF_TC(fexecve);
49272343SngieATF_TC_HEAD(fexecve, tc)
50272343Sngie{
51272343Sngie	atf_tc_set_md_var(tc, "descr", "See that fexecve works");
52272343Sngie}
53272343SngieATF_TC_BODY(fexecve, tc)
54272343Sngie{
55272343Sngie	int status;
56272343Sngie	pid_t pid;
57272343Sngie	const char *const argv[] = { "touch", "test", NULL };
58272343Sngie	const char *const envp[] = { NULL };
59272343Sngie
60272343Sngie	ATF_REQUIRE((pid = fork()) != -1);
61272343Sngie	if (pid == 0) {
62272343Sngie		int fd;
63272343Sngie
64272343Sngie		if ((fd = open("/usr/bin/touch", O_RDONLY, 0)) == -1)
65272343Sngie			err(EXIT_FAILURE, "open /usr/bin/touch");
66272343Sngie
67272343Sngie		if (fexecve(fd, __UNCONST(argv), __UNCONST(envp)) == -1) {
68272343Sngie			int error;
69272343Sngie			if (errno == ENOSYS)
70272343Sngie				error = 76;
71272343Sngie			else
72272343Sngie				error = EXIT_FAILURE;
73311607Sngie			(void)close(fd);
74272343Sngie			err(error, "fexecve");
75272343Sngie		}
76272343Sngie	}
77272343Sngie
78272343Sngie	ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
79272343Sngie	if (!WIFEXITED(status))
80272343Sngie		atf_tc_fail("child process did not exit cleanly");
81272343Sngie	if (WEXITSTATUS(status) == 76)
82272343Sngie		atf_tc_expect_fail("fexecve not implemented");
83272343Sngie	else
84272343Sngie		ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);
85272343Sngie
86272343Sngie	ATF_REQUIRE(access("test", F_OK) == 0);
87272343Sngie}
88272343Sngie
89272343SngieATF_TP_ADD_TCS(tp)
90272343Sngie{
91272343Sngie
92272343Sngie	ATF_TP_ADD_TC(tp, fexecve);
93272343Sngie
94272343Sngie	return atf_no_error();
95272343Sngie}
96