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. */
31 #include <sys/types.h>
34 #define CHILD_PROGRAM_FILENAME "test-posix_spawn-inherit1" EXEEXT
35 #define DATA_FILENAME "test-posix_spawn-inh1-data.tmp"
41 char *argv
[3] = { CHILD_PROGRAM_FILENAME
, "-child", NULL
};
47 /* Create a data file with specific contents. */
48 fp
= freopen (DATA_FILENAME
, "wb", stdout
);
51 perror ("cannot create data file");
54 fwrite ("Halle X", 1, 7, fp
);
55 if (fflush (fp
) || fseek (fp
, 6, SEEK_SET
))
57 perror ("cannot prepare data file");
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)
65 perror ("subprocess failed");
69 while (waitpid (child
, &status
, 0) != child
)
71 if (!WIFEXITED (status
))
73 fprintf (stderr
, "subprocess terminated with unexpected wait status %d\n", status
);
76 exitstatus
= WEXITSTATUS (status
);
79 fprintf (stderr
, "subprocess terminated with unexpected exit status %d\n", exitstatus
);
85 perror ("cannot close data file");
89 /* Check the contents of the data file. */
90 fp
= fopen (DATA_FILENAME
, "rb");
93 perror ("cannot open data file");
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);
105 perror ("cannot close data file");
109 /* Clean up data file. */
110 unlink (DATA_FILENAME
);
118 /* Write to STDOUT_FILENO. */
119 fwrite ("Potta", 1, 5, stdout
);
120 /* No 'fflush (stdout);' is needed. It is implicit when the child process
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
);
138 main (int argc
, char *argv
[])
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
);
148 signal (SIGHUP
, cleanup_then_die
);
151 exitstatus
= parent_main ();
155 /* This is the child process. */
156 exitstatus
= child_main ();