1 /* Test of posix_spawn() function.
2 Copyright (C) 2008-2017 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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (posix_spawnp
, int, (pid_t
*, char const *,
25 posix_spawn_file_actions_t
const *,
26 posix_spawnattr_t
const *,
27 char *const[], char *const[]));
28 SIGNATURE_CHECK (posix_spawnattr_init
, int, (posix_spawnattr_t
*));
29 SIGNATURE_CHECK (posix_spawnattr_destroy
, int, (posix_spawnattr_t
*));
30 SIGNATURE_CHECK (posix_spawnattr_setsigmask
, int, (posix_spawnattr_t
*,
32 SIGNATURE_CHECK (posix_spawnattr_setflags
, int, (posix_spawnattr_t
*, short));
33 SIGNATURE_CHECK (posix_spawn_file_actions_init
, int,
34 (posix_spawn_file_actions_t
*));
35 SIGNATURE_CHECK (posix_spawn_file_actions_destroy
, int,
36 (posix_spawn_file_actions_t
*));
37 SIGNATURE_CHECK (posix_spawn_file_actions_addclose
, int,
38 (posix_spawn_file_actions_t
*, int));
39 SIGNATURE_CHECK (posix_spawn_file_actions_addopen
, int,
40 (posix_spawn_file_actions_t
*, int, char const *, int,
42 SIGNATURE_CHECK (posix_spawn_file_actions_adddup2
, int,
43 (posix_spawn_file_actions_t
*, int, int));
53 #include <sys/types.h>
56 extern char **environ
;
58 #define CHILD_PROGRAM_FILENAME "test-posix_spawn1.sh"
63 if (0 <= fd
&& fd
<= 2)
65 int f
= fd_safer (dup (fd
));
78 char *argv
[3] = { (char *) "/bin/sh", (char *) CHILD_PROGRAM_FILENAME
, NULL
};
80 sigset_t blocked_signals
;
81 sigset_t fatal_signal_set
;
82 posix_spawn_file_actions_t actions
;
83 bool actions_allocated
;
84 posix_spawnattr_t attrs
;
94 if (pipe (ifd
) < 0 || (ifd
[0] = fd_safer (ifd
[0])) < 0)
96 perror ("cannot create pipe");
99 sigprocmask (SIG_SETMASK
, NULL
, &blocked_signals
);
100 sigemptyset (&fatal_signal_set
);
101 sigaddset (&fatal_signal_set
, SIGINT
);
102 sigaddset (&fatal_signal_set
, SIGTERM
);
103 sigaddset (&fatal_signal_set
, SIGHUP
);
104 sigaddset (&fatal_signal_set
, SIGPIPE
);
105 sigprocmask (SIG_BLOCK
, &fatal_signal_set
, NULL
);
106 actions_allocated
= false;
107 attrs_allocated
= false;
108 if ((err
= posix_spawn_file_actions_init (&actions
)) != 0
109 || (actions_allocated
= true,
110 (err
= posix_spawn_file_actions_adddup2 (&actions
, ifd
[1], STDOUT_FILENO
)) != 0
111 || (err
= posix_spawn_file_actions_addclose (&actions
, ifd
[1])) != 0
112 || (err
= posix_spawn_file_actions_addclose (&actions
, ifd
[0])) != 0
113 || (err
= posix_spawn_file_actions_addopen (&actions
, STDIN_FILENO
, "/dev/null", O_RDONLY
, 0)) != 0
114 || (err
= posix_spawnattr_init (&attrs
)) != 0
115 || (attrs_allocated
= true,
116 (err
= posix_spawnattr_setsigmask (&attrs
, &blocked_signals
)) != 0
117 || (err
= posix_spawnattr_setflags (&attrs
, POSIX_SPAWN_SETSIGMASK
)) != 0)
118 || (err
= posix_spawnp (&child
, "/bin/sh", &actions
, &attrs
, argv
, environ
)) != 0))
120 if (actions_allocated
)
121 posix_spawn_file_actions_destroy (&actions
);
123 posix_spawnattr_destroy (&attrs
);
124 sigprocmask (SIG_UNBLOCK
, &fatal_signal_set
, NULL
);
126 perror ("subprocess failed");
129 posix_spawn_file_actions_destroy (&actions
);
130 posix_spawnattr_destroy (&attrs
);
131 sigprocmask (SIG_UNBLOCK
, &fatal_signal_set
, NULL
);
134 fp
= fdopen (fd
, "r");
137 fprintf (stderr
, "fdopen() failed\n");
140 if (fread (line
, 1, 80, fp
) < 12)
142 fprintf (stderr
, "could not read expected output\n");
145 if (memcmp (line
, "Halle Potta", 11) != 0)
147 fprintf (stderr
, "read output is not the expected output");
152 while (waitpid (child
, &status
, 0) != child
)
154 if (!WIFEXITED (status
))
156 fprintf (stderr
, "subprocess terminated with unexpected wait status %d\n", status
);
159 exitstatus
= WEXITSTATUS (status
);
162 fprintf (stderr
, "subprocess terminated with unexpected exit status %d\n", exitstatus
);