lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2002 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Roland McGrath <roland@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 <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | |
| 28 | static pid_t initial_pid; |
| 29 | |
| 30 | |
| 31 | static void * |
| 32 | tf (void *arg) |
| 33 | { |
| 34 | if (getppid () != initial_pid) |
| 35 | { |
| 36 | printf ("getppid in thread returned %ld, expected %ld\n", |
| 37 | (long int) getppid (), (long int) initial_pid); |
| 38 | return (void *) -1; |
| 39 | } |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | int |
| 46 | main (void) |
| 47 | { |
| 48 | initial_pid = getpid (); |
| 49 | |
| 50 | pid_t child = fork (); |
| 51 | if (child == 0) |
| 52 | { |
| 53 | if (getppid () != initial_pid) |
| 54 | { |
| 55 | printf ("first getppid returned %ld, expected %ld\n", |
| 56 | (long int) getppid (), (long int) initial_pid); |
| 57 | exit (1); |
| 58 | } |
| 59 | |
| 60 | pthread_t th; |
| 61 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 62 | { |
| 63 | puts ("pthread_create failed"); |
| 64 | exit (1); |
| 65 | } |
| 66 | |
| 67 | void *result; |
| 68 | if (pthread_join (th, &result) != 0) |
| 69 | { |
| 70 | puts ("pthread_join failed"); |
| 71 | exit (1); |
| 72 | } |
| 73 | |
| 74 | exit (result == NULL ? 0 : 1); |
| 75 | } |
| 76 | else if (child == -1) |
| 77 | { |
| 78 | puts ("initial fork failed"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | int status; |
| 83 | if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child) |
| 84 | { |
| 85 | printf ("waitpid failed: %m\n"); |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | return status; |
| 90 | } |