1 /*
2 * Copyright © 2019 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include <errno.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include "igt_core.h"
33 #include "drmtest.h"
34
35 #include "igt_tests_common.h"
36
37 char test[] = "test";
38 char *argv_run[] = { test };
39
igt_fork_vs_skip(void)40 static void igt_fork_vs_skip(void)
41 {
42 igt_fork(i, 1) {
43 igt_skip("skipping");
44 }
45
46 igt_waitchildren();
47 }
48
igt_fork_vs_assert(void)49 static void igt_fork_vs_assert(void)
50 {
51 igt_fork(i, 1) {
52 igt_assert(0);
53 }
54
55 igt_waitchildren();
56 }
57
igt_fork_leak(void)58 static void igt_fork_leak(void)
59 {
60 igt_fork(i, 1) {
61 sleep(10);
62 }
63 }
64
plain_fork_leak(void)65 static void plain_fork_leak(void)
66 {
67 int pid;
68
69 switch (pid = fork()) {
70 case -1:
71 internal_assert(0);
72 case 0:
73 sleep(1);
74 default:
75 exit(0);
76 }
77 }
78
igt_fork_timeout_leak(void)79 static void igt_fork_timeout_leak(void)
80 {
81 igt_fork(i, 1) {
82 sleep(10);
83 }
84
85 igt_waitchildren_timeout(1, "library test");
86 }
87
do_fork(void (* test_to_run)(void))88 static int do_fork(void (*test_to_run)(void))
89 {
90 int pid, status;
91 int argc;
92
93 switch (pid = fork()) {
94 case -1:
95 internal_assert(0);
96 case 0:
97 argc = ARRAY_SIZE(argv_run);
98 igt_simple_init(argc, argv_run);
99 test_to_run();
100 igt_exit();
101 default:
102 while (waitpid(pid, &status, 0) == -1 &&
103 errno == EINTR)
104 ;
105
106 return status;
107 }
108 }
109
110
main(int argc,char ** argv)111 int main(int argc, char **argv)
112 {
113 int ret;
114
115 /* check that igt_assert is forwarded */
116 ret = do_fork(igt_fork_vs_assert);
117 internal_assert_wexited(ret, IGT_EXIT_FAILURE);
118
119 /* check that igt_skip within a fork blows up */
120 ret = do_fork(igt_fork_vs_skip);
121 internal_assert_wexited(ret, SIGABRT + 128);
122
123 /* check that failure to clean up fails */
124 ret = do_fork(igt_fork_leak);
125 internal_assert_wsignaled(ret, SIGABRT);
126
127 /* check that igt_waitchildren_timeout cleans up*/
128 ret = do_fork(igt_fork_timeout_leak);
129 internal_assert_wexited(ret, SIGKILL + 128);
130
131 /* check that any other process leaks are caught*/
132 ret = do_fork(plain_fork_leak);
133 internal_assert_wsignaled(ret, SIGABRT);
134 }
135