Print error rather than abort
[userspace-rcu.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 /* We generate children 3 levels deep */
48 #define FORK_DEPTH 3
49 /* Each generation spawns 10 children */
50 #define NR_FORK 10
51
52 static int fork_generation;
53
54 struct test_node {
55 int somedata;
56 struct rcu_head head;
57 };
58
59 static void cb(struct rcu_head *head)
60 {
61 struct test_node *node;
62
63 fprintf(stderr, "rcu callback invoked in pid: %d\n",
64 (int) getpid());
65 node = caa_container_of(head, struct test_node, head);
66 free(node);
67 }
68
69 static void test_rcu(void)
70 {
71 struct test_node *node;
72
73 rcu_register_thread();
74
75 synchronize_rcu();
76
77 rcu_read_lock();
78 rcu_read_unlock();
79
80 node = malloc(sizeof(*node));
81 assert(node);
82
83 call_rcu(&node->head, cb);
84
85 synchronize_rcu();
86
87 rcu_unregister_thread();
88 }
89
90 /*
91 * Return 0 if child, > 0 if parent, < 0 on error.
92 */
93 static int do_fork(const char *execname)
94 {
95 pid_t pid;
96
97 fprintf(stderr, "%s parent pid: %d, before fork\n",
98 execname, (int) getpid());
99
100 call_rcu_before_fork();
101 pid = fork();
102 if (pid == 0) {
103 /* child */
104 fork_generation++;
105
106 call_rcu_after_fork_child();
107 fprintf(stderr, "%s child pid: %d, after fork\n",
108 execname, (int) getpid());
109 test_rcu();
110 fprintf(stderr, "%s child pid: %d, after rcu test\n",
111 execname, (int) getpid());
112 if (fork_generation >= FORK_DEPTH)
113 exit(EXIT_SUCCESS);
114 return 0;
115 } else if (pid > 0) {
116 int status;
117
118 /* parent */
119 call_rcu_after_fork_parent();
120 fprintf(stderr, "%s parent pid: %d, after fork\n",
121 execname, (int) getpid());
122 test_rcu();
123 fprintf(stderr, "%s parent pid: %d, after rcu test\n",
124 execname, (int) getpid());
125 for (;;) {
126 pid = wait(&status);
127 if (pid < 0) {
128 perror("wait");
129 return -1;
130 }
131 if (WIFEXITED(status)) {
132 fprintf(stderr, "child %u exited normally with status %u\n",
133 pid, WEXITSTATUS(status));
134 if (WEXITSTATUS(status))
135 return -1;
136 break;
137 } else if (WIFSIGNALED(status)) {
138 fprintf(stderr, "child %u was terminated by signal %u\n",
139 pid, WTERMSIG(status));
140 return -1;
141 } else {
142 continue;
143 }
144 }
145 return 1;
146 } else {
147 perror("fork");
148 return -1;
149 }
150 }
151
152 int main(int argc, char **argv)
153 {
154 unsigned int i;
155
156 #if 0
157 /* pthread_atfork does not work with malloc/free in callbacks */
158 ret = pthread_atfork(call_rcu_before_fork,
159 call_rcu_after_fork_parent,
160 call_rcu_after_fork_child);
161 if (ret) {
162 errno = ret;
163 perror("pthread_atfork");
164 exit(EXIT_FAILURE);
165 }
166 #endif
167
168 restart:
169 for (i = 0; i < NR_FORK; i++) {
170 int ret;
171
172 test_rcu();
173 synchronize_rcu();
174 ret = do_fork(argv[0]);
175 if (ret == 0) /* child */
176 goto restart;
177 else if (ret < 0)
178 goto error;
179 /* else parent, continue. */
180 }
181 exit(EXIT_SUCCESS);
182
183 error:
184 exit(EXIT_FAILURE);
185 }
This page took 0.04161 seconds and 4 git commands to generate.