annotate tests/test-posix_spawn2.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents cd549182ef0e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of posix_spawn() function.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 39883
diff changeset
2 Copyright (C) 2008-2019 Free Software Foundation, Inc.
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <spawn.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <errno.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <fcntl.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <signal.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdbool.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdio.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <stdlib.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <string.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <unistd.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <sys/types.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include <sys/wait.h>
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #define CHILD_PROGRAM_FILENAME "test-posix_spawn2.sh"
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 static int
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 fd_safer (int fd)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 if (0 <= fd && fd <= 2)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 int f = fd_safer (dup (fd));
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 int e = errno;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 close (fd);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 errno = e;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 fd = f;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 return fd;
10548
8a87d2aae670 Remove trailing spaces.
Bruno Haible <bruno@clisp.org>
parents: 10547
diff changeset
49 }
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 int
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 main ()
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
39883
cd549182ef0e sh-filename: New module.
Bruno Haible <bruno@clisp.org>
parents: 39848
diff changeset
54 char *argv[3] = { (char *) BOURNE_SHELL, (char *) CHILD_PROGRAM_FILENAME, NULL };
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 int ofd[2];
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 sigset_t blocked_signals;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 sigset_t fatal_signal_set;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 posix_spawn_file_actions_t actions;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 bool actions_allocated;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 posix_spawnattr_t attrs;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 bool attrs_allocated;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 int err;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 pid_t child;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 int fd;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 FILE *fp;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 int written;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 int status;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 int exitstatus;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 perror ("cannot create pipe");
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 exit (1);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 sigemptyset (&fatal_signal_set);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 sigaddset (&fatal_signal_set, SIGINT);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 sigaddset (&fatal_signal_set, SIGTERM);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 sigaddset (&fatal_signal_set, SIGHUP);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 sigaddset (&fatal_signal_set, SIGPIPE);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 actions_allocated = false;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 attrs_allocated = false;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 if ((err = posix_spawn_file_actions_init (&actions)) != 0
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 || (actions_allocated = true,
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 || (err = posix_spawnattr_init (&attrs)) != 0
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 || (attrs_allocated = true,
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
39883
cd549182ef0e sh-filename: New module.
Bruno Haible <bruno@clisp.org>
parents: 39848
diff changeset
93 || (err = posix_spawnp (&child, BOURNE_SHELL, &actions, &attrs, argv, environ)) != 0))
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (actions_allocated)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
96 posix_spawn_file_actions_destroy (&actions);
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 if (attrs_allocated)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
98 posix_spawnattr_destroy (&attrs);
10547
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 errno = err;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 perror ("subprocess failed");
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 exit (1);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 posix_spawn_file_actions_destroy (&actions);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 posix_spawnattr_destroy (&attrs);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 close (ofd[0]);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 fd = ofd[1];
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 fp = fdopen (fd, "w");
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 if (fp == NULL)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 fprintf (stderr, "fdopen() failed\n");
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 exit (1);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 written = fwrite ("Halle Potta\n", 1, 12, fp);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 if (written < 12)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 fprintf (stderr, "could not write input\n");
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 exit (1);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 fclose (fp);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 status = 0;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 while (waitpid (child, &status, 0) != child)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 ;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 if (!WIFEXITED (status))
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 exit (1);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 exitstatus = WEXITSTATUS (status);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 if (exitstatus != 0)
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 {
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 exit (1);
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 }
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 return 0;
f8cfd84a2220 A new test for posix_spawn.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 }