lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2002, 2003 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, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | #include <signal.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | |
| 31 | static sigset_t ss; |
| 32 | static pthread_barrier_t *b; |
| 33 | |
| 34 | |
| 35 | static void * |
| 36 | tf (void *arg) |
| 37 | { |
| 38 | pthread_barrier_wait (b); |
| 39 | |
| 40 | puts ("child: calling sigwait now"); |
| 41 | |
| 42 | int sig; |
| 43 | int err; |
| 44 | err = sigwait (&ss, &sig); |
| 45 | if (err != 0) |
| 46 | { |
| 47 | printf ("sigwait returned unsuccessfully: %s (%d)\n", |
| 48 | strerror (err), err); |
| 49 | _exit (1); |
| 50 | } |
| 51 | |
| 52 | puts ("sigwait returned"); |
| 53 | |
| 54 | if (sig != SIGINT) |
| 55 | { |
| 56 | printf ("caught signal %d, expected %d (SIGINT)\n", sig, SIGINT); |
| 57 | _exit (1); |
| 58 | } |
| 59 | |
| 60 | puts ("child thread terminating now"); |
| 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static void |
| 67 | receiver (void) |
| 68 | { |
| 69 | pthread_t th; |
| 70 | |
| 71 | /* Make sure the process doesn't run forever. */ |
| 72 | alarm (10); |
| 73 | |
| 74 | sigfillset (&ss); |
| 75 | |
| 76 | if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0) |
| 77 | { |
| 78 | puts ("1st pthread_sigmask failed"); |
| 79 | _exit (1); |
| 80 | } |
| 81 | |
| 82 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 83 | { |
| 84 | puts ("pthread_create failed"); |
| 85 | _exit (1); |
| 86 | } |
| 87 | |
| 88 | if (pthread_join (th, NULL) != 0) |
| 89 | { |
| 90 | puts ("thread didn't join"); |
| 91 | _exit (1); |
| 92 | } |
| 93 | |
| 94 | puts ("join succeeded"); |
| 95 | |
| 96 | _exit (0); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | static int |
| 101 | do_test (void) |
| 102 | { |
| 103 | char tmp[] = "/tmp/tst-signal1-XXXXXX"; |
| 104 | |
| 105 | int fd = mkstemp (tmp); |
| 106 | if (fd == -1) |
| 107 | { |
| 108 | puts ("mkstemp failed"); |
| 109 | exit (1); |
| 110 | } |
| 111 | |
| 112 | unlink (tmp); |
| 113 | |
| 114 | int i; |
| 115 | for (i = 0; i < 20; ++i) |
| 116 | write (fd, "foobar xyzzy", 12); |
| 117 | |
| 118 | b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE, |
| 119 | MAP_SHARED, fd, 0); |
| 120 | if (b == MAP_FAILED) |
| 121 | { |
| 122 | puts ("mmap failed"); |
| 123 | exit (1); |
| 124 | } |
| 125 | |
| 126 | pthread_barrierattr_t ba; |
| 127 | if (pthread_barrierattr_init (&ba) != 0) |
| 128 | { |
| 129 | puts ("barrierattr_init failed"); |
| 130 | exit (1); |
| 131 | } |
| 132 | |
| 133 | if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0) |
| 134 | { |
| 135 | puts ("barrierattr_setpshared failed"); |
| 136 | exit (1); |
| 137 | } |
| 138 | |
| 139 | if (pthread_barrier_init (b, &ba, 2) != 0) |
| 140 | { |
| 141 | puts ("barrier_init failed"); |
| 142 | exit (1); |
| 143 | } |
| 144 | |
| 145 | if (pthread_barrierattr_destroy (&ba) != 0) |
| 146 | { |
| 147 | puts ("barrierattr_destroy failed"); |
| 148 | exit (1); |
| 149 | } |
| 150 | |
| 151 | pid_t pid = fork (); |
| 152 | if (pid == -1) |
| 153 | { |
| 154 | puts ("fork failed"); |
| 155 | exit (1); |
| 156 | } |
| 157 | |
| 158 | if (pid == 0) |
| 159 | receiver (); |
| 160 | |
| 161 | pthread_barrier_wait (b); |
| 162 | |
| 163 | /* Wait a bit more. */ |
| 164 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; |
| 165 | nanosleep (&ts, NULL); |
| 166 | |
| 167 | /* Send the signal. */ |
| 168 | puts ("sending the signal now"); |
| 169 | kill (pid, SIGINT); |
| 170 | |
| 171 | /* Wait for the process to terminate. */ |
| 172 | int status; |
| 173 | if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) |
| 174 | { |
| 175 | puts ("wrong child reported terminated"); |
| 176 | exit (1); |
| 177 | } |
| 178 | |
| 179 | if (!WIFEXITED (status)) |
| 180 | { |
| 181 | if (WIFSIGNALED (status)) |
| 182 | printf ("child exited with signal %d\n", WTERMSIG (status)); |
| 183 | else |
| 184 | puts ("child didn't exit normally"); |
| 185 | exit (1); |
| 186 | } |
| 187 | |
| 188 | if (WEXITSTATUS (status) != 0) |
| 189 | { |
| 190 | printf ("exit status %d != 0\n", WEXITSTATUS (status)); |
| 191 | exit (1); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | #define TEST_FUNCTION do_test () |
| 198 | #include "../test-skeleton.c" |