1//===-- Unittests for execv -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/sys/wait/waitpid.h"
10#include "src/unistd/execv.h"
11#include "src/unistd/fork.h"
12
13#include "test/IntegrationTest/test.h"
14
15#include <signal.h>
16#include <sys/wait.h>
17
18void fork_and_execv_normal_exit() {
19 pid_t pid = LIBC_NAMESPACE::fork();
20 if (pid == 0) {
21 const char *path = "libc_execv_test_normal_exit";
22 char *const argv[] = {
23 const_cast<char *>("execv_test_normal_exit"),
24 nullptr,
25 };
26 LIBC_NAMESPACE::execv(path, argv);
27 }
28 ASSERT_TRUE(pid > 0);
29 int status;
30 pid_t cpid = LIBC_NAMESPACE::waitpid(pid, waitstatus: &status, options: 0);
31 ASSERT_TRUE(cpid > 0);
32 ASSERT_EQ(cpid, pid);
33 ASSERT_TRUE(WIFEXITED(status));
34}
35
36void fork_and_execv_signal_exit() {
37 pid_t pid = LIBC_NAMESPACE::fork();
38 if (pid == 0) {
39 const char *path = "libc_execv_test_signal_exit";
40 char *const argv[] = {
41 const_cast<char *>("execv_test_normal_exit"),
42 nullptr,
43 };
44 LIBC_NAMESPACE::execv(path, argv);
45 }
46 ASSERT_TRUE(pid > 0);
47 int status;
48 pid_t cpid = LIBC_NAMESPACE::waitpid(pid, waitstatus: &status, options: 0);
49 ASSERT_TRUE(cpid > 0);
50 ASSERT_EQ(cpid, pid);
51 ASSERT_FALSE(WIFEXITED(status));
52 ASSERT_TRUE(WTERMSIG(status) == SIGUSR1);
53}
54
55TEST_MAIN(int argc, char **argv, char **envp) {
56 fork_and_execv_normal_exit();
57 fork_and_execv_signal_exit();
58 return 0;
59}
60

source code of libc/test/integration/src/unistd/execv_test.cpp