gettext: Sync with gettext 0.23.
[gnulib.git] / tests / test-posix_spawn-inherit1.c
blob6c6ac268d3619c9ccdd7352df0276059f63ccdad
1 /* Test of posix_spawn() function with an inherited file descriptor 1.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
19 /* Test whether passing a file descriptor open for writing, including the
20 current file position, works. */
22 #include <config.h>
24 #include <spawn.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
34 #define CHILD_PROGRAM_FILENAME "test-posix_spawn-inherit1" EXEEXT
35 #define DATA_FILENAME "test-posix_spawn-inh1-data.tmp"
37 static int
38 parent_main (void)
40 FILE *fp;
41 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
42 int err;
43 pid_t child;
44 int status;
45 int exitstatus;
47 /* Create a data file with specific contents. */
48 fp = freopen (DATA_FILENAME, "wb", stdout);
49 if (fp == NULL)
51 perror ("cannot create data file");
52 return 1;
54 fwrite ("Halle X", 1, 7, fp);
55 if (fflush (fp) || fseek (fp, 6, SEEK_SET))
57 perror ("cannot prepare data file");
58 return 1;
61 /* Test whether the child writes to fd 1 at the current file position. */
62 if ((err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, NULL, NULL, argv, environ)) != 0)
64 errno = err;
65 perror ("subprocess failed");
66 return 1;
68 status = 0;
69 while (waitpid (child, &status, 0) != child)
71 if (!WIFEXITED (status))
73 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
74 return 1;
76 exitstatus = WEXITSTATUS (status);
77 if (exitstatus != 0)
79 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
80 return 1;
83 if (fclose (fp))
85 perror ("cannot close data file");
86 return 1;
89 /* Check the contents of the data file. */
90 fp = fopen (DATA_FILENAME, "rb");
91 if (fp == NULL)
93 perror ("cannot open data file");
94 return 1;
96 char buf[1024];
97 int nread = fread (buf, 1, sizeof (buf), fp);
98 if (!(nread == 11 && memcmp (buf, "Halle Potta", 11) == 0))
100 fprintf (stderr, "data file wrong: has %d bytes, expected %d bytes\n", nread, 11);
101 return 1;
103 if (fclose (fp))
105 perror ("cannot close data file");
106 return 1;
109 /* Clean up data file. */
110 unlink (DATA_FILENAME);
112 return 0;
115 static int
116 child_main (void)
118 /* Write to STDOUT_FILENO. */
119 fwrite ("Potta", 1, 5, stdout);
120 /* No 'fflush (stdout);' is needed. It is implicit when the child process
121 exits. */
123 return 0;
126 static void
127 cleanup_then_die (int sig)
129 /* Clean up data file. */
130 unlink (DATA_FILENAME);
132 /* Re-raise the signal and die from it. */
133 signal (sig, SIG_DFL);
134 raise (sig);
138 main (int argc, char *argv[])
140 int exitstatus;
142 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
144 /* This is the parent process. */
145 signal (SIGINT, cleanup_then_die);
146 signal (SIGTERM, cleanup_then_die);
147 #ifdef SIGHUP
148 signal (SIGHUP, cleanup_then_die);
149 #endif
151 exitstatus = parent_main ();
153 else
155 /* This is the child process. */
156 exitstatus = child_main ();
158 return exitstatus;