2.9
[glibc/nacl-glibc.git] / nptl / tst-atfork2.c
blobf162f80e6b1730edfc27d204ce641c9954493b34
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <mcheck.h>
23 #include <pthread.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/wait.h>
30 /* Must be exported. */
31 int val;
33 static void
34 prepare (void)
36 val *= 2;
39 static void
40 parent (void)
42 val += 4;
45 static void
46 child (void)
48 val += 8;
52 static int
53 do_test (void)
55 mtrace ();
57 if (pthread_atfork (prepare, parent, child) != 0)
59 puts ("do_test: atfork failed");
60 exit (1);
63 void *h = dlopen ("tst-atfork2mod.so", RTLD_LAZY);
64 if (h == NULL)
66 printf ("dlopen failed: %s\n", dlerror ());
67 exit (1);
70 /* First trial of fork. */
71 pid_t pid = fork ();
72 if (pid == -1)
74 puts ("1st fork failed");
75 exit (1);
78 if (pid == 0)
80 /* Child. */
81 if (val != 80)
83 printf ("1st: expected val=%d, got %d\n", 80, val);
84 exit (2);
87 exit (0);
90 /* Parent. */
91 if (val != 24)
93 printf ("1st: expected val=%d, got %d\n", 24, val);
94 exit (1);
97 int status;
98 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
100 puts ("1st waitpid failed");
101 exit (1);
104 if (status != 0)
105 exit (status);
107 puts ("unloading now");
109 /* Unload the module. */
110 if (dlclose (h) != 0)
112 puts ("dlclose failed");
113 exit (1);
116 puts ("2nd fork");
118 /* Second fork trial. */
119 val = 1;
120 pid = fork ();
121 if (pid == -1)
123 puts ("2nd fork failed");
124 exit (1);
127 if (pid == 0)
129 /* Child. */
130 if (val != 10)
132 printf ("2nd: expected val=%d, got %d\n", 10, val);
133 exit (3);
136 exit (0);
139 /* Parent. */
140 if (val != 6)
142 printf ("2nd: expected val=%d, got %d\n", 6, val);
143 exit (1);
146 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
148 puts ("2nd waitpid failed");
149 exit (1);
152 if (status != 0)
153 exit (status);
155 return 0;
158 #define TEST_FUNCTION do_test ()
159 #include "../test-skeleton.c"