| 1 | /* Copyright (C) 2003 Free Software Foundation, Inc.
|
|---|
| 2 | This file is part of the GNU C Library.
|
|---|
| 3 | Contributed by Ulrich Drepper <[email protected]>, 2003.
|
|---|
| 4 |
|
|---|
| 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.
|
|---|
| 9 |
|
|---|
| 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.
|
|---|
| 14 |
|
|---|
| 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. */
|
|---|
| 19 |
|
|---|
| 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>
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | /* Must be exported. */
|
|---|
| 31 | int val;
|
|---|
| 32 |
|
|---|
| 33 | static void
|
|---|
| 34 | prepare (void)
|
|---|
| 35 | {
|
|---|
| 36 | val *= 2;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | static void
|
|---|
| 40 | parent (void)
|
|---|
| 41 | {
|
|---|
| 42 | val += 4;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | static void
|
|---|
| 46 | child (void)
|
|---|
| 47 | {
|
|---|
| 48 | val += 8;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | static int
|
|---|
| 53 | do_test (void)
|
|---|
| 54 | {
|
|---|
| 55 | mtrace ();
|
|---|
| 56 |
|
|---|
| 57 | if (pthread_atfork (prepare, parent, child) != 0)
|
|---|
| 58 | {
|
|---|
| 59 | puts ("do_test: atfork failed");
|
|---|
| 60 | exit (1);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void *h = dlopen ("tst-atfork2mod.so", RTLD_LAZY);
|
|---|
| 64 | if (h == NULL)
|
|---|
| 65 | {
|
|---|
| 66 | printf ("dlopen failed: %s\n", dlerror ());
|
|---|
| 67 | exit (1);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /* First trial of fork. */
|
|---|
| 71 | pid_t pid = fork ();
|
|---|
| 72 | if (pid == -1)
|
|---|
| 73 | {
|
|---|
| 74 | puts ("1st fork failed");
|
|---|
| 75 | exit (1);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | if (pid == 0)
|
|---|
| 79 | {
|
|---|
| 80 | /* Child. */
|
|---|
| 81 | if (val != 80)
|
|---|
| 82 | {
|
|---|
| 83 | printf ("1st: expected val=%d, got %d\n", 80, val);
|
|---|
| 84 | exit (2);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | exit (0);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /* Parent. */
|
|---|
| 91 | if (val != 24)
|
|---|
| 92 | {
|
|---|
| 93 | printf ("1st: expected val=%d, got %d\n", 24, val);
|
|---|
| 94 | exit (1);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | int status;
|
|---|
| 98 | if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
|
|---|
| 99 | {
|
|---|
| 100 | puts ("1st waitpid failed");
|
|---|
| 101 | exit (1);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if (status != 0)
|
|---|
| 105 | exit (status);
|
|---|
| 106 |
|
|---|
| 107 | puts ("unloading now");
|
|---|
| 108 |
|
|---|
| 109 | /* Unload the module. */
|
|---|
| 110 | if (dlclose (h) != 0)
|
|---|
| 111 | {
|
|---|
| 112 | puts ("dlclose failed");
|
|---|
| 113 | exit (1);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | puts ("2nd fork");
|
|---|
| 117 |
|
|---|
| 118 | /* Second fork trial. */
|
|---|
| 119 | val = 1;
|
|---|
| 120 | pid = fork ();
|
|---|
| 121 | if (pid == -1)
|
|---|
| 122 | {
|
|---|
| 123 | puts ("2nd fork failed");
|
|---|
| 124 | exit (1);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | if (pid == 0)
|
|---|
| 128 | {
|
|---|
| 129 | /* Child. */
|
|---|
| 130 | if (val != 10)
|
|---|
| 131 | {
|
|---|
| 132 | printf ("2nd: expected val=%d, got %d\n", 10, val);
|
|---|
| 133 | exit (3);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | exit (0);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Parent. */
|
|---|
| 140 | if (val != 6)
|
|---|
| 141 | {
|
|---|
| 142 | printf ("2nd: expected val=%d, got %d\n", 6, val);
|
|---|
| 143 | exit (1);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
|
|---|
| 147 | {
|
|---|
| 148 | puts ("2nd waitpid failed");
|
|---|
| 149 | exit (1);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (status != 0)
|
|---|
| 153 | exit (status);
|
|---|
| 154 |
|
|---|
| 155 | return 0;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | #define TEST_FUNCTION do_test ()
|
|---|
| 159 | #include "../test-skeleton.c"
|
|---|