expl: Work around inaccurate implementation on NetBSD.
[gnulib.git] / tests / test-posix_spawn4.c
blob6456160a18c7c55d589e50a7686e4b66f1acd5cd
1 /* Test of posix_spawn() function with 'chdir' action.
2 Copyright (C) 2008-2019 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>, 2018. */
19 #include <config.h>
21 #include <spawn.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
34 static int
35 fd_safer (int fd)
37 if (0 <= fd && fd <= 2)
39 int f = fd_safer (dup (fd));
40 int e = errno;
41 close (fd);
42 errno = e;
43 fd = f;
46 return fd;
49 int
50 main ()
52 char *argv[2] = { (char *) "pwd", NULL };
53 int ifd[2];
54 sigset_t blocked_signals;
55 sigset_t fatal_signal_set;
56 posix_spawn_file_actions_t actions;
57 bool actions_allocated;
58 posix_spawnattr_t attrs;
59 bool attrs_allocated;
60 int err;
61 pid_t child;
62 int fd;
63 FILE *fp;
64 char line[80];
65 int status;
66 int exitstatus;
68 if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
70 perror ("cannot create pipe");
71 exit (1);
73 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
74 sigemptyset (&fatal_signal_set);
75 sigaddset (&fatal_signal_set, SIGINT);
76 sigaddset (&fatal_signal_set, SIGTERM);
77 sigaddset (&fatal_signal_set, SIGHUP);
78 sigaddset (&fatal_signal_set, SIGPIPE);
79 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
80 actions_allocated = false;
81 attrs_allocated = false;
82 if ((err = posix_spawn_file_actions_init (&actions)) != 0
83 || (actions_allocated = true,
84 (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
85 || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
86 || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
87 || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
88 || (err = posix_spawn_file_actions_addchdir (&actions, "/")) != 0
89 || (err = posix_spawnattr_init (&attrs)) != 0
90 || (attrs_allocated = true,
91 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
92 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
93 || (err = posix_spawnp (&child, "pwd", &actions, &attrs, argv, environ)) != 0))
95 if (actions_allocated)
96 posix_spawn_file_actions_destroy (&actions);
97 if (attrs_allocated)
98 posix_spawnattr_destroy (&attrs);
99 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
100 errno = err;
101 perror ("subprocess failed");
102 exit (1);
104 posix_spawn_file_actions_destroy (&actions);
105 posix_spawnattr_destroy (&attrs);
106 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
107 close (ifd[1]);
108 fd = ifd[0];
109 fp = fdopen (fd, "r");
110 if (fp == NULL)
112 fprintf (stderr, "fdopen() failed\n");
113 exit (1);
115 if (fread (line, 1, 80, fp) < 2)
117 fprintf (stderr, "could not read expected output\n");
118 exit (1);
120 if (memcmp (line, "/\n", 2) != 0)
122 fprintf (stderr, "read output is not the expected output");
123 exit (1);
125 fclose (fp);
126 status = 0;
127 while (waitpid (child, &status, 0) != child)
129 if (!WIFEXITED (status))
131 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
132 exit (1);
134 exitstatus = WEXITSTATUS (status);
135 if (exitstatus != 0)
137 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
138 exit (1);
140 return 0;