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 extern pid_t __wait4_time64(pid_t, int *, int, struct rusage *);
28
29 /**
30 * @tc.name : wait4_0100
31 * @tc.desc : Test the wait4 function to wait4 for the child process
32 * @tc.level : Level 0
33 */
wait4_0100(void)34 void wait4_0100(void)
35 {
36 pid_t pid = fork();
37 if (pid > 0) {
38 int status = 0;
39 int options = 0;
40 struct rusage ru;
41 pid_t wait4_for_pind = wait4(pid, &status, options, &ru);
42 if (wait4_for_pind != pid) {
43 t_error("%s wait4 get pid is %d are not want %d\n", __func__, wait4_for_pind, pid);
44 }
45 if (status != 0) {
46 t_error("%s wait4 get status is %d are not 0\n", __func__, status);
47 }
48 } else if (pid == 0) {
49 sleep(1);
50 exit(0);
51 } else {
52 t_error("%s wait4 fork error\n");
53 }
54 }
55
56 /**
57 * @tc.name : wait4_0200
58 * @tc.desc : Test the result of the wait4 function when the exit code of the child process is not 0
59 * @tc.level : Level 1
60 */
wait4_0200(void)61 void wait4_0200(void)
62 {
63 pid_t pid = fork();
64 if (pid > 0) {
65 int status = 0;
66 int options = 0;
67 struct rusage ru;
68 pid_t wait4_for_pind = wait4(pid, &status, options, &ru);
69 if (wait4_for_pind != pid) {
70 t_error("%s wait4 get pid is %d are not want %d\n", __func__, wait4_for_pind, pid);
71 }
72 if (status == 0) {
73 t_error("%s wait4 get status is 0\n", __func__);
74 }
75 } else if (pid == 0) {
76 sleep(1);
77 exit(EXIT_CODE);
78 } else {
79 t_error("%s wait4 fork error\n");
80 }
81 }
82
83 /**
84 * @tc.name : wait4_time64_0100
85 * @tc.desc : Test the __wait4_time64 function to __wait4_time64 for the child process
86 * @tc.level : Level 0
87 */
wait4_time64_0100(void)88 void wait4_time64_0100(void)
89 {
90 pid_t pid = fork();
91 if (pid > 0) {
92 int status = 0;
93 int options = 0;
94 struct rusage ru;
95 pid_t wait4_for_pind = __wait4_time64(pid, &status, options, &ru);
96 if (wait4_for_pind != pid) {
97 t_error("%s __wait4_time64 get pid is %d are not want %d\n", __func__, wait4_for_pind, pid);
98 }
99 if (status != 0) {
100 t_error("%s __wait4_time64 get status is %d are not 0\n", __func__, status);
101 }
102 } else if (pid == 0) {
103 sleep(1);
104 exit(0);
105 } else {
106 t_error("%s __wait4_time64 fork error\n");
107 }
108 }
109
main(int argc,char * argv[])110 int main(int argc, char *argv[])
111 {
112 wait4_0100();
113 wait4_0200();
114 wait4_time64_0100();
115 return t_status;
116 }