1 /* Test of posix_spawn() function with 'open' action and O_APPEND flag.
2 Copyright (C) 2008-2021 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 posix_spawn_file_actions_addopen supports the O_APPEND flag. */
32 #include <sys/types.h>
35 #define CHILD_PROGRAM_FILENAME "test-posix_spawn-open2"
36 #define DATA_FILENAME "test-posix_spawn-open2-data.tmp"
42 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
43 posix_spawn_file_actions_t actions;
44 bool actions_allocated;
50 /* Create a data file with specific contents. */
51 fp = fopen (DATA_FILENAME, "wb");
54 perror ("cannot create data file");
57 fwrite ("Halle ", 1, 6, fp);
58 if (fflush (fp) || fclose (fp))
60 perror ("cannot prepare data file");
64 /* Test whether posix_spawn_file_actions_addopen with O_APPEND flag causes
65 the child to append to this file. */
66 actions_allocated = false;
67 if ((err = posix_spawn_file_actions_init (&actions)) != 0
68 || (actions_allocated = true,
69 (err = posix_spawn_file_actions_addopen (&actions, STDOUT_FILENO, DATA_FILENAME, O_RDWR | O_APPEND, 0600)) != 0
70 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
72 if (actions_allocated)
73 posix_spawn_file_actions_destroy (&actions);
75 perror ("subprocess failed");
78 posix_spawn_file_actions_destroy (&actions);
80 while (waitpid (child, &status, 0) != child)
82 if (!WIFEXITED (status))
84 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
87 exitstatus = WEXITSTATUS (status);
90 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
94 /* Check the contents of the data file. */
95 fp = fopen (DATA_FILENAME, "rb");
98 perror ("cannot open data file");
102 int nread = fread (buf, 1, sizeof (buf), fp);
103 if (!(nread == 11 && memcmp (buf, "Halle Potta", 11) == 0))
105 fprintf (stderr, "data file wrong: has %d bytes, expected %d bytes\n", nread, 11);
110 perror ("cannot close data file");
114 /* Clean up data file. */
115 unlink (DATA_FILENAME);
123 /* Write to STDOUT_FILENO. */
124 fwrite ("Potta", 1, 5, stdout);
125 /* No 'fflush (stdout);' is needed. It is implicit when the child process
132 cleanup_then_die (int sig)
134 /* Clean up data file. */
135 unlink (DATA_FILENAME);
137 /* Re-raise the signal and die from it. */
138 signal (sig, SIG_DFL);
143 main (int argc, char *argv[])
147 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
149 /* This is the parent process. */
150 signal (SIGINT, cleanup_then_die);
151 signal (SIGTERM, cleanup_then_die);
153 signal (SIGHUP, cleanup_then_die);
156 exitstatus = parent_main ();
160 /* This is the child process. */
161 exitstatus = child_main ();