xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2002-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 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, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/wait.h> |
| 25 | |
| 26 | |
| 27 | static int val; |
| 28 | |
| 29 | |
| 30 | static void |
| 31 | prepare1 (void) |
| 32 | { |
| 33 | val *= 2; |
| 34 | } |
| 35 | |
| 36 | static void |
| 37 | prepare2 (void) |
| 38 | { |
| 39 | ++val; |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | parent1 (void) |
| 44 | { |
| 45 | val += 4; |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | parent2 (void) |
| 50 | { |
| 51 | val *= 4; |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | child1 (void) |
| 56 | { |
| 57 | val += 8; |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | child2 (void) |
| 62 | { |
| 63 | val *= 8; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static int |
| 68 | do_test (void) |
| 69 | { |
| 70 | pid_t pid; |
| 71 | int status = 0; |
| 72 | |
| 73 | if (pthread_atfork (prepare1, parent1, child1) != 0) |
| 74 | { |
| 75 | puts ("1st atfork failed"); |
| 76 | exit (1); |
| 77 | } |
| 78 | if (pthread_atfork (prepare2, parent2, child2) != 0) |
| 79 | { |
| 80 | puts ("2nd atfork failed"); |
| 81 | exit (1); |
| 82 | } |
| 83 | |
| 84 | pid = fork (); |
| 85 | if (pid == -1) |
| 86 | { |
| 87 | puts ("fork failed"); |
| 88 | exit (1); |
| 89 | } |
| 90 | |
| 91 | if (pid != 0) |
| 92 | { |
| 93 | /* Parent. */ |
| 94 | if (val != 24) |
| 95 | { |
| 96 | printf ("expected val=%d, got %d\n", 24, val); |
| 97 | exit (1); |
| 98 | } |
| 99 | |
| 100 | if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) |
| 101 | { |
| 102 | puts ("waitpid failed"); |
| 103 | exit (1); |
| 104 | } |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | /* Child. */ |
| 109 | if (val != 80) |
| 110 | { |
| 111 | printf ("expected val=%d, got %d\n", 80, val); |
| 112 | exit (2); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return status; |
| 117 | } |
| 118 | |
| 119 | #define TEST_FUNCTION do_test () |
| 120 | #include "../test-skeleton.c" |