1*92841acfSchristos /* $NetBSD: t_fexecve.c,v 1.3 2017/01/10 15:15:09 christos Exp $ */
2a76c1cc1Smanu
3a76c1cc1Smanu /*-
4a76c1cc1Smanu * Copyright (c) 2012 The NetBSD Foundation, Inc.
5a76c1cc1Smanu * All rights reserved.
6a76c1cc1Smanu *
7a76c1cc1Smanu * This code is derived from software contributed to The NetBSD Foundation
8a76c1cc1Smanu * by Emmanuel Dreyfus.
9a76c1cc1Smanu *
10a76c1cc1Smanu * Redistribution and use in source and binary forms, with or without
11a76c1cc1Smanu * modification, are permitted provided that the following conditions
12a76c1cc1Smanu * are met:
13a76c1cc1Smanu * 1. Redistributions of source code must retain the above copyright
14a76c1cc1Smanu * notice, this list of conditions and the following disclaimer.
15a76c1cc1Smanu * 2. Redistributions in binary form must reproduce the above copyright
16a76c1cc1Smanu * notice, this list of conditions and the following disclaimer in the
17a76c1cc1Smanu * documentation and/or other materials provided with the distribution.
18a76c1cc1Smanu *
19a76c1cc1Smanu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a76c1cc1Smanu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a76c1cc1Smanu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a76c1cc1Smanu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a76c1cc1Smanu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a76c1cc1Smanu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a76c1cc1Smanu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a76c1cc1Smanu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a76c1cc1Smanu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a76c1cc1Smanu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a76c1cc1Smanu * POSSIBILITY OF SUCH DAMAGE.
30a76c1cc1Smanu */
31a76c1cc1Smanu #include <sys/cdefs.h>
32*92841acfSchristos __RCSID("$NetBSD: t_fexecve.c,v 1.3 2017/01/10 15:15:09 christos Exp $");
33042bde11Sjmmv
34042bde11Sjmmv #include <sys/wait.h>
35a76c1cc1Smanu
36a76c1cc1Smanu #include <atf-c.h>
37042bde11Sjmmv #include <err.h>
38a76c1cc1Smanu #include <errno.h>
39a76c1cc1Smanu #include <fcntl.h>
40a76c1cc1Smanu #include <limits.h>
41a76c1cc1Smanu #include <paths.h>
42a76c1cc1Smanu #include <stdio.h>
43042bde11Sjmmv #include <stdlib.h>
44a76c1cc1Smanu #include <string.h>
45a76c1cc1Smanu #include <unistd.h>
46a76c1cc1Smanu #include <sys/param.h>
47a76c1cc1Smanu
48042bde11Sjmmv ATF_TC(fexecve);
ATF_TC_HEAD(fexecve,tc)49a76c1cc1Smanu ATF_TC_HEAD(fexecve, tc)
50a76c1cc1Smanu {
51a76c1cc1Smanu atf_tc_set_md_var(tc, "descr", "See that fexecve works");
52a76c1cc1Smanu }
ATF_TC_BODY(fexecve,tc)53a76c1cc1Smanu ATF_TC_BODY(fexecve, tc)
54a76c1cc1Smanu {
55042bde11Sjmmv int status;
56a76c1cc1Smanu pid_t pid;
57042bde11Sjmmv const char *const argv[] = { "touch", "test", NULL };
58a76c1cc1Smanu const char *const envp[] = { NULL };
59a76c1cc1Smanu
60a76c1cc1Smanu ATF_REQUIRE((pid = fork()) != -1);
61042bde11Sjmmv if (pid == 0) {
62042bde11Sjmmv int fd;
63042bde11Sjmmv
64042bde11Sjmmv if ((fd = open("/usr/bin/touch", O_RDONLY, 0)) == -1)
65042bde11Sjmmv err(EXIT_FAILURE, "open /usr/bin/touch");
66042bde11Sjmmv
67042bde11Sjmmv if (fexecve(fd, __UNCONST(argv), __UNCONST(envp)) == -1) {
68042bde11Sjmmv int error;
69042bde11Sjmmv if (errno == ENOSYS)
70042bde11Sjmmv error = 76;
71042bde11Sjmmv else
72042bde11Sjmmv error = EXIT_FAILURE;
73*92841acfSchristos (void)close(fd);
74042bde11Sjmmv err(error, "fexecve");
75a76c1cc1Smanu }
76a76c1cc1Smanu }
77a76c1cc1Smanu
78042bde11Sjmmv ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
79042bde11Sjmmv if (!WIFEXITED(status))
80042bde11Sjmmv atf_tc_fail("child process did not exit cleanly");
81042bde11Sjmmv if (WEXITSTATUS(status) == 76)
82042bde11Sjmmv atf_tc_expect_fail("fexecve not implemented");
83042bde11Sjmmv else
84042bde11Sjmmv ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);
85a76c1cc1Smanu
86042bde11Sjmmv ATF_REQUIRE(access("test", F_OK) == 0);
87042bde11Sjmmv }
88a76c1cc1Smanu
ATF_TP_ADD_TCS(tp)89a76c1cc1Smanu ATF_TP_ADD_TCS(tp)
90a76c1cc1Smanu {
91a76c1cc1Smanu
92a76c1cc1Smanu ATF_TP_ADD_TC(tp, fexecve);
93a76c1cc1Smanu
94a76c1cc1Smanu return atf_no_error();
95a76c1cc1Smanu }
96