spawn: Use special invocation for <spawn.h> on OS/2 kLIBC.
[gnulib.git] / tests / test-posix_spawn-fchdir.c
blob0f71374460ed710807c510fe0524d338212061a2
1 /* Test of posix_spawn() function with 'fchdir' action.
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>, 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 #include "findprog.h"
36 static int
37 fd_safer (int fd)
39 if (0 <= fd && fd <= 2)
41 int f = fd_safer (dup (fd));
42 int e = errno;
43 close (fd);
44 errno = e;
45 fd = f;
48 return fd;
51 static void
52 test (const char *pwd_prog)
54 char *argv[2] = { (char *) "pwd", NULL };
55 int rootfd;
56 int ifd[2];
57 sigset_t blocked_signals;
58 sigset_t fatal_signal_set;
59 posix_spawn_file_actions_t actions;
60 bool actions_allocated;
61 posix_spawnattr_t attrs;
62 bool attrs_allocated;
63 int err;
64 pid_t child;
65 int fd;
66 FILE *fp;
67 char line[80];
68 int status;
69 int exitstatus;
71 rootfd = open ("/", O_RDONLY);
72 if (rootfd < 0)
74 perror ("cannot open directory");
75 exit (1);
77 if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
79 perror ("cannot create pipe");
80 exit (1);
82 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
83 sigemptyset (&fatal_signal_set);
84 sigaddset (&fatal_signal_set, SIGINT);
85 sigaddset (&fatal_signal_set, SIGTERM);
86 #ifdef SIGHUP
87 sigaddset (&fatal_signal_set, SIGHUP);
88 #endif
89 #ifdef SIGPIPE
90 sigaddset (&fatal_signal_set, SIGPIPE);
91 #endif
92 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
93 actions_allocated = false;
94 attrs_allocated = false;
95 if ((err = posix_spawn_file_actions_init (&actions)) != 0
96 || (actions_allocated = true,
97 (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
98 || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
99 || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
100 || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
101 || (err = posix_spawn_file_actions_addfchdir (&actions, rootfd)) != 0
102 || (err = posix_spawnattr_init (&attrs)) != 0
103 || (attrs_allocated = true,
104 #if defined _WIN32 && !defined __CYGWIN__
106 #else
107 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
108 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0
109 #endif
111 || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0))
113 if (actions_allocated)
114 posix_spawn_file_actions_destroy (&actions);
115 if (attrs_allocated)
116 posix_spawnattr_destroy (&attrs);
117 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
118 errno = err;
119 perror ("subprocess failed");
120 exit (1);
122 posix_spawn_file_actions_destroy (&actions);
123 posix_spawnattr_destroy (&attrs);
124 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
125 close (ifd[1]);
126 fd = ifd[0];
127 fp = fdopen (fd, "r");
128 if (fp == NULL)
130 fprintf (stderr, "fdopen() failed\n");
131 exit (1);
133 if (fread (line, 1, 80, fp) < 2)
135 fprintf (stderr, "could not read expected output\n");
136 exit (1);
138 if (memcmp (line, "/\n", 2) != 0)
140 fprintf (stderr, "read output is not the expected output\n");
141 exit (1);
143 fclose (fp);
144 status = 0;
145 while (waitpid (child, &status, 0) != child)
147 if (!WIFEXITED (status))
149 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
150 exit (1);
152 exitstatus = WEXITSTATUS (status);
153 if (exitstatus != 0)
155 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
156 exit (1);
161 main ()
163 test ("pwd");
165 /* Verify that if a program is given as a relative file name with at least one
166 slash, it is interpreted w.r.t. the current directory after fchdir has been
167 executed. */
169 const char *abs_pwd_prog = find_in_path ("pwd");
171 if (abs_pwd_prog != NULL
172 && abs_pwd_prog[0] == '/'
173 && abs_pwd_prog[1] != '0' && abs_pwd_prog[1] != '/')
174 test (&abs_pwd_prog[1]);
177 return 0;