1 /* Test of posix_spawn() function: writing to a subprocess.
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>, 2008. */
31 #include <sys/types.h>
34 #define CHILD_PROGRAM_FILENAME "test-posix_spawn-dup2-stdin.sh"
39 if (0 <= fd && fd <= 2)
41 int f = fd_safer (dup (fd));
54 char *argv[3] = { (char *) BOURNE_SHELL, (char *) CHILD_PROGRAM_FILENAME, NULL };
56 sigset_t blocked_signals;
57 sigset_t fatal_signal_set;
58 posix_spawn_file_actions_t actions;
59 bool actions_allocated;
60 posix_spawnattr_t attrs;
70 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
72 perror ("cannot create pipe");
75 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
76 sigemptyset (&fatal_signal_set);
77 sigaddset (&fatal_signal_set, SIGINT);
78 sigaddset (&fatal_signal_set, SIGTERM);
80 sigaddset (&fatal_signal_set, SIGHUP);
83 sigaddset (&fatal_signal_set, SIGPIPE);
85 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
86 actions_allocated = false;
87 attrs_allocated = false;
88 if ((err = posix_spawn_file_actions_init (&actions)) != 0
89 || (actions_allocated = true,
90 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
91 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
92 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
93 || (err = posix_spawnattr_init (&attrs)) != 0
94 || (attrs_allocated = true,
95 #if defined _WIN32 && !defined __CYGWIN__
98 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
99 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0
102 || (err = posix_spawnp (&child, BOURNE_SHELL, &actions, &attrs, argv, environ)) != 0))
104 if (actions_allocated)
105 posix_spawn_file_actions_destroy (&actions);
107 posix_spawnattr_destroy (&attrs);
108 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
110 perror ("subprocess failed");
113 posix_spawn_file_actions_destroy (&actions);
114 posix_spawnattr_destroy (&attrs);
115 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
118 fp = fdopen (fd, "w");
121 fprintf (stderr, "fdopen() failed\n");
124 written = fwrite ("Halle Potta\n", 1, 12, fp);
127 fprintf (stderr, "could not write input\n");
132 while (waitpid (child, &status, 0) != child)
134 if (!WIFEXITED (status))
136 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
139 exitstatus = WEXITSTATUS (status);
142 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);