1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <spawn.h>
17 #include <sys/wait.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include "functionalext.h"
22
23 /**
24 * @tc.name : posix_spawn_0100
25 * @tc.desc : Use the default process attributes, create a subprocess and
26 * execute shell commands, which are input from outside
27 * @tc.level : Level 0
28 */
posix_spawn_0100(char * cmd)29 void posix_spawn_0100(char *cmd)
30 {
31 pid_t pid;
32 char *argv[] = {"sh", "-c", cmd, NULL};
33 int ret = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, NULL);
34 EXPECT_EQ("posix_spawn_0100", ret, CMPFLAG);
35 if (ret != 0) {
36 t_error("posix_spawn create failed\n");
37 return;
38 }
39 wait(NULL);
40 }
41
42 /**
43 * @tc.name : posix_spawn_0200
44 * @tc.desc : Add SIGCHLD to the signal set, create a subprocess and
45 * execute shell commands, which are input from outside
46 * @tc.level : Level 0
47 */
posix_spawn_0200(char * cmd)48 void posix_spawn_0200(char *cmd)
49 {
50 pid_t pid;
51 sigset_t def;
52 posix_spawnattr_t attr;
53 char *argv[] = {"sh", "-c", cmd, NULL};
54 int ret = posix_spawnattr_init(&attr);
55
56 ret = sigemptyset(&def);
57 ret = sigaddset(&def, SIGCHLD);
58 if (ret == ERREXPECT) {
59 EXPECT_NE("posix_spawn_0200", ret, ERREXPECT);
60 return;
61 }
62
63 ret = posix_spawnattr_setsigdefault(&attr, &def);
64 posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
65
66 ret = posix_spawn(&pid, "/bin/sh", NULL, &attr, argv, NULL);
67 EXPECT_EQ("posix_spawn_0200", ret, CMPFLAG);
68 if (ret != 0) {
69 t_error("posix_spawn create failed\n");
70 return;
71 }
72
73 wait(NULL);
74 posix_spawnattr_destroy(&attr);
75 }
76
77 /**
78 * @tc.name : posix_spawn_0300
79 * @tc.desc : Set process attributes, create subprocesses and execute shell
80 * commands, which are input externally
81 * @tc.level : Level 0
82 */
posix_spawn_0300(char * cmd)83 void posix_spawn_0300(char *cmd)
84 {
85 pid_t pid;
86 sigset_t def;
87 posix_spawnattr_t attr;
88 char *argv[] = {"sh", "-c", cmd, NULL};
89 int ret = posix_spawnattr_init(&attr);
90
91 posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF);
92
93 ret = posix_spawn(&pid, "/bin/sh", NULL, &attr, argv, NULL);
94 EXPECT_EQ("posix_spawn_0300", ret, CMPFLAG);
95 if (ret != 0) {
96 t_error("posix_spawn create failed\n");
97 return;
98 }
99
100 wait(NULL);
101 posix_spawnattr_destroy(&attr);
102 }
103
104 /**
105 * @tc.name : posix_spawn_0400
106 * @tc.desc : Create a child process and execute a non-existing file in the current
107 * directory, the process creation fails
108 * @tc.level : Level 2
109 */
posix_spawn_0400(void)110 void posix_spawn_0400(void)
111 {
112 pid_t pid;
113
114 int ret = posix_spawn(&pid, "unexitfile", NULL, NULL, NULL, NULL);
115 EXPECT_NE("posix_spawn_0400", ret, CMPFLAG);
116 if (ret != 0) {
117 return;
118 }
119 waitpid(pid, NULL, WNOHANG);
120 }
121
main(int argc,char * argv[])122 int main(int argc, char *argv[])
123 {
124 if (argc > 1) {
125 posix_spawn_0100(argv[1]);
126 posix_spawn_0200(argv[1]);
127 posix_spawn_0300(argv[1]);
128 posix_spawn_0400();
129 }
130 return t_status;
131 }
132