1*ddba0402SEnji Cooper /* $NetBSD: t_fexecve.c,v 1.3 2017/01/10 15:15:09 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2012 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Emmanuel Dreyfus.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32*ddba0402SEnji Cooper __RCSID("$NetBSD: t_fexecve.c,v 1.3 2017/01/10 15:15:09 christos Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <sys/wait.h>
3557718be8SEnji Cooper
3657718be8SEnji Cooper #include <atf-c.h>
3757718be8SEnji Cooper #include <err.h>
3857718be8SEnji Cooper #include <errno.h>
3957718be8SEnji Cooper #include <fcntl.h>
4057718be8SEnji Cooper #include <limits.h>
4157718be8SEnji Cooper #include <paths.h>
4257718be8SEnji Cooper #include <stdio.h>
4357718be8SEnji Cooper #include <stdlib.h>
4457718be8SEnji Cooper #include <string.h>
4557718be8SEnji Cooper #include <unistd.h>
4657718be8SEnji Cooper #include <sys/param.h>
4757718be8SEnji Cooper
4857718be8SEnji Cooper ATF_TC(fexecve);
ATF_TC_HEAD(fexecve,tc)4957718be8SEnji Cooper ATF_TC_HEAD(fexecve, tc)
5057718be8SEnji Cooper {
5157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "See that fexecve works");
5257718be8SEnji Cooper }
ATF_TC_BODY(fexecve,tc)5357718be8SEnji Cooper ATF_TC_BODY(fexecve, tc)
5457718be8SEnji Cooper {
5557718be8SEnji Cooper int status;
5657718be8SEnji Cooper pid_t pid;
5757718be8SEnji Cooper const char *const argv[] = { "touch", "test", NULL };
5857718be8SEnji Cooper const char *const envp[] = { NULL };
5957718be8SEnji Cooper
6057718be8SEnji Cooper ATF_REQUIRE((pid = fork()) != -1);
6157718be8SEnji Cooper if (pid == 0) {
6257718be8SEnji Cooper int fd;
6357718be8SEnji Cooper
6457718be8SEnji Cooper if ((fd = open("/usr/bin/touch", O_RDONLY, 0)) == -1)
6557718be8SEnji Cooper err(EXIT_FAILURE, "open /usr/bin/touch");
6657718be8SEnji Cooper
6757718be8SEnji Cooper if (fexecve(fd, __UNCONST(argv), __UNCONST(envp)) == -1) {
6857718be8SEnji Cooper int error;
6957718be8SEnji Cooper if (errno == ENOSYS)
7057718be8SEnji Cooper error = 76;
7157718be8SEnji Cooper else
7257718be8SEnji Cooper error = EXIT_FAILURE;
735641f109SEnji Cooper (void)close(fd);
7457718be8SEnji Cooper err(error, "fexecve");
7557718be8SEnji Cooper }
7657718be8SEnji Cooper }
7757718be8SEnji Cooper
7857718be8SEnji Cooper ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
7957718be8SEnji Cooper if (!WIFEXITED(status))
8057718be8SEnji Cooper atf_tc_fail("child process did not exit cleanly");
8157718be8SEnji Cooper if (WEXITSTATUS(status) == 76)
8257718be8SEnji Cooper atf_tc_expect_fail("fexecve not implemented");
8357718be8SEnji Cooper else
8457718be8SEnji Cooper ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);
8557718be8SEnji Cooper
8657718be8SEnji Cooper ATF_REQUIRE(access("test", F_OK) == 0);
8757718be8SEnji Cooper }
8857718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)8957718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
9057718be8SEnji Cooper {
9157718be8SEnji Cooper
9257718be8SEnji Cooper ATF_TP_ADD_TC(tp, fexecve);
9357718be8SEnji Cooper
9457718be8SEnji Cooper return atf_no_error();
9557718be8SEnji Cooper }
96