| 1 | /* Verify that the parent pid is unchanged by __clone_internal. |
| 2 | Copyright (C) 2021-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 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 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <sched.h> |
| 20 | #include <signal.h> |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <clone_internal.h> |
| 27 | #include <support/xunistd.h> |
| 28 | |
| 29 | #ifndef TEST_CLONE_FLAGS |
| 30 | #define TEST_CLONE_FLAGS 0 |
| 31 | #endif |
| 32 | |
| 33 | static int sig; |
| 34 | |
| 35 | static int |
| 36 | f (void *a) |
| 37 | { |
| 38 | puts (s: "in f" ); |
| 39 | union sigval sival; |
| 40 | sival.sival_int = getpid (); |
| 41 | printf (format: "pid = %d\n" , sival.sival_int); |
| 42 | if (sigqueue (pid: getppid (), sig: sig, val: sival) != 0) |
| 43 | return 1; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | static int |
| 49 | do_test (void) |
| 50 | { |
| 51 | int mypid = getpid (); |
| 52 | |
| 53 | sig = SIGRTMIN; |
| 54 | sigset_t ss; |
| 55 | sigemptyset (&ss); |
| 56 | sigaddset (&ss, sig); |
| 57 | if (sigprocmask (SIG_BLOCK, set: &ss, NULL) != 0) |
| 58 | { |
| 59 | printf (format: "sigprocmask failed: %m\n" ); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | #define STACK_SIZE 128 * 1024 |
| 64 | char st[STACK_SIZE] __attribute__ ((aligned)); |
| 65 | struct clone_args clone_args = |
| 66 | { |
| 67 | .flags = TEST_CLONE_FLAGS & ~CSIGNAL, |
| 68 | .exit_signal = TEST_CLONE_FLAGS & CSIGNAL, |
| 69 | .stack = (uintptr_t) st, |
| 70 | .stack_size = sizeof (st), |
| 71 | }; |
| 72 | pid_t p = __clone_internal (&clone_args, f, 0); |
| 73 | if (p == -1) |
| 74 | { |
| 75 | printf(format: "clone failed: %m\n" ); |
| 76 | return 1; |
| 77 | } |
| 78 | printf (format: "new thread: %d\n" , (int) p); |
| 79 | |
| 80 | siginfo_t si; |
| 81 | do |
| 82 | if (sigwaitinfo (set: &ss, info: &si) < 0) |
| 83 | { |
| 84 | printf(format: "sigwaitinfo failed: %m\n" ); |
| 85 | kill (pid: p, SIGKILL); |
| 86 | return 1; |
| 87 | } |
| 88 | while (si.si_signo != sig || si.si_code != SI_QUEUE); |
| 89 | |
| 90 | int e; |
| 91 | xwaitpid (p, status: &e, __WCLONE); |
| 92 | if (!WIFEXITED (e)) |
| 93 | { |
| 94 | if (WIFSIGNALED (e)) |
| 95 | printf (format: "died from signal %s\n" , strsignal (WTERMSIG (e))); |
| 96 | else |
| 97 | puts (s: "did not terminate correctly" ); |
| 98 | return 1; |
| 99 | } |
| 100 | if (WEXITSTATUS (e) != 0) |
| 101 | { |
| 102 | printf (format: "exit code %d\n" , WEXITSTATUS (e)); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | if (si.si_int != (int) p) |
| 107 | { |
| 108 | printf (format: "expected PID %d, got si_int %d\n" , (int) p, si.si_int); |
| 109 | kill (pid: p, SIGKILL); |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | if (si.si_pid != p) |
| 114 | { |
| 115 | printf (format: "expected PID %d, got si_pid %d\n" , (int) p, (int) si.si_pid); |
| 116 | kill (pid: p, SIGKILL); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | if (getpid () != mypid) |
| 121 | { |
| 122 | puts (s: "my PID changed" ); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | #include <support/test-driver.c> |
| 130 | |