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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 #include "test.h"
23
24 const int EXIT_CODE = 66;
25 const int SLEEP_TIME = 2;
26
27 /**
28 * @tc.name : wait3_0100
29 * @tc.desc : Test the wait3 function to wait3 for the child process
30 * @tc.level : Level 0
31 */
wait3_0100(void)32 void wait3_0100(void)
33 {
34 pid_t pid = fork();
35 if (pid > 0) {
36 int status = 0;
37 int options = 0;
38 struct rusage ru;
39 pid_t wait3_for_pind = wait3(&status, options, &ru);
40 if (wait3_for_pind != pid) {
41 t_error("%s wait3 get pid is %d are not want %d\n", __func__, wait3_for_pind, pid);
42 }
43 if (status != 0) {
44 t_error("%s wait3 get status is %d are not 0\n", __func__, status);
45 }
46 } else if (pid == 0) {
47 sleep(1);
48 exit(0);
49 } else {
50 t_error("%s wait3 fork error\n");
51 }
52 }
53
54 /**
55 * @tc.name : wait3_0200
56 * @tc.desc : Test the result of the wait3 function when the exit code of the child process is not 0
57 * @tc.level : Level 1
58 */
wait3_0200(void)59 void wait3_0200(void)
60 {
61 pid_t pid = fork();
62 if (pid > 0) {
63 int status = 0;
64 int options = 0;
65 struct rusage ru;
66 pid_t wait3_for_pind = wait3(&status, options, &ru);
67 if (wait3_for_pind != pid) {
68 t_error("%s wait3 get pid is %d are not want %d\n", __func__, wait3_for_pind, pid);
69 }
70 if (status == 0) {
71 t_error("%s wait3 get status is 0\n", __func__);
72 }
73 } else if (pid == 0) {
74 sleep(1);
75 exit(EXIT_CODE);
76 } else {
77 t_error("%s wait3 fork error\n");
78 }
79 }
80
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 wait3_0100();
84 sleep(SLEEP_TIME);
85 wait3_0200();
86 return t_status;
87 }