Refactor tests
[urcu.git] / tests / regression / test_urcu_fork.c
1 /*
2 * test_urcu_fork.c
3 *
4 * Userspace RCU library - test program (fork)
5 *
6 * Copyright February 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #define _GNU_SOURCE
24 #include "config.h"
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <assert.h>
34 #include <sched.h>
35 #include <errno.h>
36
37 #include <urcu/arch.h>
38 #include <urcu/tls-compat.h>
39
40 #ifndef DYNAMIC_LINK_TEST
41 #define _LGPL_SOURCE
42 #else
43 #define rcu_debug_yield_read()
44 #endif
45 #include <urcu.h>
46
47 #include "tap.h"
48
49 /* We generate children 3 levels deep */
50 #define FORK_DEPTH 3
51 /* Each generation spawns 10 children */
52 #define NR_FORK 10
53
54 #define NR_TESTS NR_FORK
55
56 static int fork_generation;
57
58 /*
59 * Only print diagnostic for top level parent process, else the console
60 * has trouble formatting the tap output.
61 */
62 #define diag_gen0(...) \
63 do { \
64 if (!fork_generation) \
65 diag(__VA_ARGS__); \
66 } while (0)
67
68 struct test_node {
69 int somedata;
70 struct rcu_head head;
71 };
72
73 static void cb(struct rcu_head *head)
74 {
75 struct test_node *node;
76
77 diag_gen0("rcu callback invoked in pid: %d", (int) getpid());
78 node = caa_container_of(head, struct test_node, head);
79 free(node);
80 }
81
82 static void test_rcu(void)
83 {
84 struct test_node *node;
85
86 rcu_register_thread();
87
88 synchronize_rcu();
89
90 rcu_read_lock();
91 rcu_read_unlock();
92
93 node = malloc(sizeof(*node));
94 assert(node);
95
96 call_rcu(&node->head, cb);
97
98 synchronize_rcu();
99
100 rcu_unregister_thread();
101 }
102
103 /*
104 * Return 0 if child, > 0 if parent, < 0 on error.
105 */
106 static int do_fork(const char *execname)
107 {
108 pid_t pid;
109
110 diag_gen0("%s parent pid: %d, before fork",
111 execname, (int) getpid());
112
113 call_rcu_before_fork();
114 pid = fork();
115 if (pid == 0) {
116 /* child */
117 fork_generation++;
118 tap_disable();
119
120 call_rcu_after_fork_child();
121 diag_gen0("%s child pid: %d, after fork",
122 execname, (int) getpid());
123 test_rcu();
124 diag_gen0("%s child pid: %d, after rcu test",
125 execname, (int) getpid());
126 if (fork_generation >= FORK_DEPTH)
127 exit(EXIT_SUCCESS);
128 return 0;
129 } else if (pid > 0) {
130 int status;
131
132 /* parent */
133 call_rcu_after_fork_parent();
134 diag_gen0("%s parent pid: %d, after fork",
135 execname, (int) getpid());
136 test_rcu();
137 diag_gen0("%s parent pid: %d, after rcu test",
138 execname, (int) getpid());
139 for (;;) {
140 pid = wait(&status);
141 if (pid < 0) {
142 if (!fork_generation)
143 perror("wait");
144 return -1;
145 }
146 if (WIFEXITED(status)) {
147 diag_gen0("child %u exited normally with status %u",
148 pid, WEXITSTATUS(status));
149 if (WEXITSTATUS(status))
150 return -1;
151 break;
152 } else if (WIFSIGNALED(status)) {
153 diag_gen0("child %u was terminated by signal %u",
154 pid, WTERMSIG(status));
155 return -1;
156 } else {
157 continue;
158 }
159 }
160 return 1;
161 } else {
162 if (!fork_generation)
163 perror("fork");
164 return -1;
165 }
166 }
167
168 int main(int argc, char **argv)
169 {
170 unsigned int i;
171
172 plan_tests(NR_TESTS);
173
174 #if 0
175 /* pthread_atfork does not work with malloc/free in callbacks */
176 ret = pthread_atfork(call_rcu_before_fork,
177 call_rcu_after_fork_parent,
178 call_rcu_after_fork_child);
179 if (ret) {
180 errno = ret;
181 perror("pthread_atfork");
182 exit(EXIT_FAILURE);
183 }
184 #endif
185
186 restart:
187 for (i = 0; i < NR_FORK; i++) {
188 int ret;
189
190 test_rcu();
191 synchronize_rcu();
192 ret = do_fork(argv[0]);
193 if (!fork_generation) {
194 ok(ret >= 0, "child status %d", ret);
195 }
196 if (ret == 0) { /* child */
197 goto restart;
198 } else if (ret < 0) {
199 goto error;
200 } else {
201 /* else parent, continue. */
202 }
203 }
204 if (!fork_generation) {
205 return exit_status();
206 } else {
207 exit(EXIT_SUCCESS);
208 }
209
210 error:
211 if (!fork_generation) {
212 return exit_status();
213 } else {
214 exit(EXIT_FAILURE);
215 }
216 }
This page took 0.046801 seconds and 4 git commands to generate.