xref: /aosp_15_r20/external/llvm-libc/test/integration/src/spawn/posix_spawn_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for posix_spawn -----------------------------------------===//
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 "test_binary_properties.h"
10 
11 #include "src/spawn/posix_spawn.h"
12 #include "src/spawn/posix_spawn_file_actions_addopen.h"
13 #include "src/spawn/posix_spawn_file_actions_destroy.h"
14 #include "src/spawn/posix_spawn_file_actions_init.h"
15 #include "src/sys/wait/waitpid.h"
16 #include "test/IntegrationTest/test.h"
17 
18 #include <fcntl.h>
19 #include <spawn.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <sys/wait.h>
23 
24 char arg0[] = "libc_posix_spawn_test_binary";
25 char *argv[] = {
26     arg0,
27     nullptr,
28 };
29 
spawn_and_wait_for_normal_exit(char ** envp)30 void spawn_and_wait_for_normal_exit(char **envp) {
31   pid_t cpid;
32   posix_spawn_file_actions_t file_actions;
33   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_init(&file_actions), 0);
34   LIBC_NAMESPACE::posix_spawn_file_actions_addopen(
35       &file_actions, CHILD_FD, "testdata/posix_spawn.test", O_RDONLY, 0);
36   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn(&cpid, arg0, &file_actions, nullptr,
37                                         argv, envp),
38             0);
39   ASSERT_TRUE(cpid > 0);
40   int status;
41   ASSERT_EQ(LIBC_NAMESPACE::waitpid(cpid, &status, 0), cpid);
42   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(&file_actions), 0);
43   ASSERT_TRUE(WIFEXITED(status));
44   int exit_status = WEXITSTATUS(status);
45   ASSERT_EQ(exit_status, 0);
46 }
47 
TEST_MAIN(int argc,char ** argv,char ** envp)48 TEST_MAIN(int argc, char **argv, char **envp) {
49   spawn_and_wait_for_normal_exit(envp);
50   return 0;
51 }
52