xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Test for vfork functions. |
| 2 | Copyright (C) 2004-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2004. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | /* This test relies on non-POSIX functionality since the child |
| 28 | processes call write and getpid. */ |
| 29 | static int |
| 30 | do_test (void) |
| 31 | { |
| 32 | int result = 0; |
| 33 | int fd[2]; |
| 34 | |
| 35 | if (pipe (fd) == -1) |
| 36 | { |
| 37 | puts ("pipe failed"); |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | /* First vfork() without previous getpid(). */ |
| 42 | pid_t p1; |
| 43 | if ((p1 = vfork ()) == 0) |
| 44 | { |
| 45 | pid_t p = getpid (); |
| 46 | _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p)); |
| 47 | } |
| 48 | else if (p1 == -1) |
| 49 | { |
| 50 | puts ("1st vfork failed"); |
| 51 | result = 1; |
| 52 | } |
| 53 | |
| 54 | pid_t p2 = 0; |
| 55 | if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t)) |
| 56 | { |
| 57 | puts ("1st read failed"); |
| 58 | result = 1; |
| 59 | } |
| 60 | int r; |
| 61 | if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1) |
| 62 | { |
| 63 | puts ("1st waitpid failed"); |
| 64 | result = 1; |
| 65 | } |
| 66 | else if (r != 0) |
| 67 | { |
| 68 | puts ("write in 1st child failed"); |
| 69 | result = 1; |
| 70 | } |
| 71 | |
| 72 | /* Main process' ID. */ |
| 73 | pid_t p0 = getpid (); |
| 74 | |
| 75 | /* vfork() again, but after a getpid() in the main process. */ |
| 76 | pid_t p3; |
| 77 | if ((p3 = vfork ()) == 0) |
| 78 | { |
| 79 | pid_t p = getpid (); |
| 80 | _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p)); |
| 81 | } |
| 82 | else if (p1 == -1) |
| 83 | { |
| 84 | puts ("2nd vfork failed"); |
| 85 | result = 1; |
| 86 | } |
| 87 | |
| 88 | pid_t p4; |
| 89 | if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t)) |
| 90 | { |
| 91 | puts ("2nd read failed"); |
| 92 | result = 1; |
| 93 | } |
| 94 | if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3) |
| 95 | { |
| 96 | puts ("2nd waitpid failed"); |
| 97 | result = 1; |
| 98 | } |
| 99 | else if (r != 0) |
| 100 | { |
| 101 | puts ("write in 2nd child failed"); |
| 102 | result = 1; |
| 103 | } |
| 104 | |
| 105 | /* And getpid in the main process again. */ |
| 106 | pid_t p5 = getpid (); |
| 107 | |
| 108 | /* Analysis of the results. */ |
| 109 | if (p0 != p5) |
| 110 | { |
| 111 | printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5); |
| 112 | result = 1; |
| 113 | } |
| 114 | |
| 115 | if (p0 == p1) |
| 116 | { |
| 117 | printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1); |
| 118 | result = 1; |
| 119 | } |
| 120 | |
| 121 | if (p1 != p2) |
| 122 | { |
| 123 | printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2); |
| 124 | result = 1; |
| 125 | } |
| 126 | |
| 127 | if (p0 == p3) |
| 128 | { |
| 129 | printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3); |
| 130 | result = 1; |
| 131 | } |
| 132 | |
| 133 | if (p3 != p4) |
| 134 | { |
| 135 | printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4); |
| 136 | result = 1; |
| 137 | } |
| 138 | |
| 139 | close (fd[0]); |
| 140 | close (fd[1]); |
| 141 | |
| 142 | if (result == 0) |
| 143 | puts ("All OK"); |
| 144 | |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | #define TEST_FUNCTION do_test () |
| 149 | #include "../test-skeleton.c" |