Home | History | Annotate | Line # | Download | only in execve
t_execve.c revision 1.3
      1 /*	$NetBSD: t_execve.c,v 1.3 2025/03/13 01:27:27 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: t_execve.c,v 1.3 2025/03/13 01:27:27 riastradh Exp $");
     31 
     32 #include <sys/wait.h>
     33 
     34 #include <atf-c.h>
     35 
     36 #include <errno.h>
     37 #include <limits.h>
     38 #include <signal.h>
     39 #include <stddef.h>
     40 #include <stdio.h>
     41 #include <unistd.h>
     42 #include <time.h>
     43 
     44 #include "h_macros.h"
     45 
     46 ATF_TC(t_execve_null);
     47 ATF_TC_HEAD(t_execve_null, tc)
     48 {
     49 	atf_tc_set_md_var(tc, "descr",
     50 	    "Tests an empty execve(2) executing");
     51 }
     52 ATF_TC_BODY(t_execve_null, tc)
     53 {
     54 	int err;
     55 
     56 	err = execve(NULL, NULL, NULL);
     57 	ATF_REQUIRE(err == -1);
     58 	ATF_REQUIRE_MSG(errno == EFAULT,
     59 	    "wrong error returned %d instead of %d", errno, EFAULT);
     60 }
     61 
     62 ATF_TC(t_execve_sig);
     63 ATF_TC_HEAD(t_execve_sig, tc)
     64 {
     65 	atf_tc_set_md_var(tc, "descr",
     66 	    "Checks that execve does not drop pending signals");
     67 }
     68 ATF_TC_BODY(t_execve_sig, tc)
     69 {
     70 	const char *srcdir = atf_tc_get_config_var(tc, "srcdir");
     71 	char h_execsig[PATH_MAX];
     72 	time_t start;
     73 
     74 	snprintf(h_execsig, sizeof(h_execsig), "%s/../h_execsig", srcdir);
     75 	REQUIRE_LIBC(signal(SIGPIPE, SIG_IGN), SIG_ERR);
     76 
     77 	atf_tc_expect_fail("PR kern/580911: after fork/execve or posix_spawn,"
     78 	    " parent kill(child, SIGTERM) has race condition"
     79 	    " making it unreliable");
     80 
     81 	for (start = time(NULL); time(NULL) - start <= 10;) {
     82 		int fd[2];
     83 		char *const argv[] = {h_execsig, NULL};
     84 		pid_t pid;
     85 		int status;
     86 
     87 		RL(pipe(fd));
     88 		RL(pid = vfork());
     89 		if (pid == 0) {
     90 			if (dup2(fd[0], STDIN_FILENO) == -1)
     91 				_exit(1);
     92 			(void)execve(argv[0], argv, NULL);
     93 			_exit(2);
     94 		}
     95 		RL(close(fd[0]));
     96 		RL(kill(pid, SIGTERM));
     97 		if (write(fd[1], (char[]){0}, 1) == -1 && errno != EPIPE)
     98 			atf_tc_fail("write failed: %s", strerror(errno));
     99 		RL(waitpid(pid, &status, 0));
    100 		ATF_REQUIRE_MSG(WIFSIGNALED(status),
    101 		    "child exited with status 0x%x", status);
    102 		ATF_REQUIRE_EQ_MSG(WTERMSIG(status), SIGTERM,
    103 		    "child exited on signal %d (%s)",
    104 		    WTERMSIG(status), strsignal(WTERMSIG(status)));
    105 		RL(close(fd[1]));
    106 	}
    107 }
    108 
    109 ATF_TP_ADD_TCS(tp)
    110 {
    111 
    112 	ATF_TP_ADD_TC(tp, t_execve_null);
    113 	ATF_TP_ADD_TC(tp, t_execve_sig);
    114 
    115 	return atf_no_error();
    116 }
    117