1 /* Test of posix_spawn() function.
2 Copyright (C) 2008 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
31 #include <sys/types.h>
34 extern char **environ;
37 # define STDIN_FILENO 0
40 # define STDOUT_FILENO 1
43 # define STDERR_FILENO 2
46 #define CHILD_PROGRAM_FILENAME "test-posix_spawn2.sh"
51 if (0 <= fd && fd <= 2)
53 int f = fd_safer (dup (fd));
66 char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
68 sigset_t blocked_signals;
69 sigset_t fatal_signal_set;
70 posix_spawn_file_actions_t actions;
71 bool actions_allocated;
72 posix_spawnattr_t attrs;
82 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
84 perror ("cannot create pipe");
87 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
88 sigemptyset (&fatal_signal_set);
89 sigaddset (&fatal_signal_set, SIGINT);
90 sigaddset (&fatal_signal_set, SIGTERM);
91 sigaddset (&fatal_signal_set, SIGHUP);
92 sigaddset (&fatal_signal_set, SIGPIPE);
93 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
94 actions_allocated = false;
95 attrs_allocated = false;
96 if ((err = posix_spawn_file_actions_init (&actions)) != 0
97 || (actions_allocated = true,
98 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
99 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
100 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
101 || (err = posix_spawnattr_init (&attrs)) != 0
102 || (attrs_allocated = true,
103 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
104 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
105 || (err = posix_spawnp (&child, "/bin/sh", &actions, &attrs, argv, environ)) != 0))
107 if (actions_allocated)
108 posix_spawn_file_actions_destroy (&actions);
110 posix_spawnattr_destroy (&attrs);
111 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
113 perror ("subprocess failed");
116 posix_spawn_file_actions_destroy (&actions);
117 posix_spawnattr_destroy (&attrs);
118 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
121 fp = fdopen (fd, "w");
124 fprintf (stderr, "fdopen() failed\n");
127 written = fwrite ("Halle Potta\n", 1, 12, fp);
130 fprintf (stderr, "could not write input\n");
135 while (waitpid (child, &status, 0) != child)
137 if (!WIFEXITED (status))
139 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
142 exitstatus = WEXITSTATUS (status);
145 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);