hppa: allocate membarrier system call number
[urcu.git] / tests / regression / test_urcu_fork.c
CommitLineData
390f0a26
MD
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
f5ab766e 24#include "config.h"
390f0a26
MD
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
ad460058
MD
47#include "tap.h"
48
d6aefcd1
MD
49/* We generate children 3 levels deep */
50#define FORK_DEPTH 3
51/* Each generation spawns 10 children */
52#define NR_FORK 10
53
ad460058
MD
54#define NR_TESTS NR_FORK
55
d6aefcd1
MD
56static int fork_generation;
57
ad460058
MD
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
390f0a26
MD
68struct test_node {
69 int somedata;
70 struct rcu_head head;
71};
72
73static void cb(struct rcu_head *head)
74{
75 struct test_node *node;
76
ad460058 77 diag_gen0("rcu callback invoked in pid: %d", (int) getpid());
390f0a26
MD
78 node = caa_container_of(head, struct test_node, head);
79 free(node);
80}
81
82static 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
d6aefcd1
MD
103/*
104 * Return 0 if child, > 0 if parent, < 0 on error.
105 */
106static int do_fork(const char *execname)
390f0a26
MD
107{
108 pid_t pid;
390f0a26 109
ad460058 110 diag_gen0("%s parent pid: %d, before fork",
d6aefcd1 111 execname, (int) getpid());
390f0a26
MD
112
113 call_rcu_before_fork();
114 pid = fork();
390f0a26
MD
115 if (pid == 0) {
116 /* child */
d6aefcd1 117 fork_generation++;
ad460058 118 tap_disable();
d6aefcd1 119
390f0a26 120 call_rcu_after_fork_child();
ad460058 121 diag_gen0("%s child pid: %d, after fork",
d6aefcd1 122 execname, (int) getpid());
390f0a26 123 test_rcu();
ad460058 124 diag_gen0("%s child pid: %d, after rcu test",
d6aefcd1
MD
125 execname, (int) getpid());
126 if (fork_generation >= FORK_DEPTH)
127 exit(EXIT_SUCCESS);
128 return 0;
390f0a26
MD
129 } else if (pid > 0) {
130 int status;
131
132 /* parent */
133 call_rcu_after_fork_parent();
ad460058 134 diag_gen0("%s parent pid: %d, after fork",
d6aefcd1 135 execname, (int) getpid());
390f0a26 136 test_rcu();
ad460058 137 diag_gen0("%s parent pid: %d, after rcu test",
d6aefcd1 138 execname, (int) getpid());
390f0a26
MD
139 for (;;) {
140 pid = wait(&status);
d6aefcd1 141 if (pid < 0) {
ad460058
MD
142 if (!fork_generation)
143 perror("wait");
d6aefcd1
MD
144 return -1;
145 }
390f0a26 146 if (WIFEXITED(status)) {
ad460058 147 diag_gen0("child %u exited normally with status %u",
390f0a26 148 pid, WEXITSTATUS(status));
d6aefcd1
MD
149 if (WEXITSTATUS(status))
150 return -1;
390f0a26
MD
151 break;
152 } else if (WIFSIGNALED(status)) {
ad460058 153 diag_gen0("child %u was terminated by signal %u",
390f0a26 154 pid, WTERMSIG(status));
d6aefcd1 155 return -1;
390f0a26
MD
156 } else {
157 continue;
158 }
159 }
d6aefcd1 160 return 1;
390f0a26 161 } else {
ad460058
MD
162 if (!fork_generation)
163 perror("fork");
d6aefcd1
MD
164 return -1;
165 }
166}
167
168int main(int argc, char **argv)
169{
170 unsigned int i;
171
ad460058
MD
172 plan_tests(NR_TESTS);
173
d6aefcd1
MD
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");
390f0a26
MD
182 exit(EXIT_FAILURE);
183 }
d6aefcd1
MD
184#endif
185
186restart:
187 for (i = 0; i < NR_FORK; i++) {
188 int ret;
189
190 test_rcu();
191 synchronize_rcu();
192 ret = do_fork(argv[0]);
ad460058
MD
193 if (!fork_generation) {
194 ok(ret >= 0, "child status %d", ret);
195 }
196 if (ret == 0) { /* child */
d6aefcd1 197 goto restart;
ad460058 198 } else if (ret < 0) {
d6aefcd1 199 goto error;
ad460058
MD
200 } else {
201 /* else parent, continue. */
202 }
203 }
204 if (!fork_generation) {
205 return exit_status();
206 } else {
207 exit(EXIT_SUCCESS);
d6aefcd1 208 }
d6aefcd1
MD
209
210error:
ad460058
MD
211 if (!fork_generation) {
212 return exit_status();
213 } else {
214 exit(EXIT_FAILURE);
215 }
390f0a26 216}
This page took 0.038015 seconds and 4 git commands to generate.