usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-posix_spawn-fchdir.c
blob13081b76974b4aa5786f6a481433b9057aea7e79
1 /* Test of posix_spawn() function with 'fchdir' action.
2 Copyright (C) 2008-2024 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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
33 #include "findprog.h"
34 #include "qemu.h"
35 #include "xvasprintf.h"
37 static bool is_qemu;
39 static int
40 fd_safer (int fd)
42 if (0 <= fd && fd <= 2)
44 int f = fd_safer (dup (fd));
45 int e = errno;
46 close (fd);
47 errno = e;
48 fd = f;
51 return fd;
54 static void
55 test (const char *pwd_prog)
57 char *argv[2] = { (char *) "pwd", NULL };
58 /* The name of a directory that most likely is accessible. */
59 #if defined __ANDROID__
60 #define KNOWNDIR "/proc"
61 #else
62 #define KNOWNDIR "/"
63 #endif
64 int knownfd;
65 int ifd[2];
66 sigset_t blocked_signals;
67 sigset_t fatal_signal_set;
68 posix_spawn_file_actions_t actions;
69 bool actions_allocated;
70 posix_spawnattr_t attrs;
71 bool attrs_allocated;
72 int err;
73 pid_t child;
74 int fd;
75 FILE *fp;
76 char line[80];
77 int status;
78 int exitstatus;
80 knownfd = open (KNOWNDIR, O_RDONLY);
81 if (knownfd < 0)
83 perror ("cannot open directory");
84 exit (1);
86 if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
88 perror ("cannot create pipe");
89 exit (1);
91 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
92 sigemptyset (&fatal_signal_set);
93 sigaddset (&fatal_signal_set, SIGINT);
94 sigaddset (&fatal_signal_set, SIGTERM);
95 #ifdef SIGHUP
96 sigaddset (&fatal_signal_set, SIGHUP);
97 #endif
98 #ifdef SIGPIPE
99 sigaddset (&fatal_signal_set, SIGPIPE);
100 #endif
101 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
102 actions_allocated = false;
103 attrs_allocated = false;
104 if ((err = posix_spawn_file_actions_init (&actions)) != 0
105 || (actions_allocated = true,
106 (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
107 || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
108 || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
109 || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
110 || (err = posix_spawn_file_actions_addfchdir (&actions, knownfd)) != 0
111 || (err = posix_spawnattr_init (&attrs)) != 0
112 || (attrs_allocated = true,
113 #if defined _WIN32 && !defined __CYGWIN__
115 #else
116 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
117 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0
118 #endif
120 || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0))
122 if (actions_allocated)
123 posix_spawn_file_actions_destroy (&actions);
124 if (attrs_allocated)
125 posix_spawnattr_destroy (&attrs);
126 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
127 errno = err;
128 perror ("subprocess failed");
129 exit (1);
131 posix_spawn_file_actions_destroy (&actions);
132 posix_spawnattr_destroy (&attrs);
133 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
134 close (ifd[1]);
135 fd = ifd[0];
136 fp = fdopen (fd, "r");
137 if (fp == NULL)
139 fprintf (stderr, "fdopen() failed\n");
140 exit (1);
142 if (fread (line, 1, 80, fp) < 2)
144 fprintf (stderr, "could not read expected output\n");
145 exit (1);
147 /* For a process running under QEMU user-mode, knownfd points to the directory
148 that is the value of the QEMU_LD_PREFIX environment variable or of the -L
149 command-line option, and the line produced by 'pwd' is that directory, not
150 "/". */
151 if (!is_qemu)
153 if (memcmp (line, KNOWNDIR "\n", strlen (KNOWNDIR) + 1) != 0)
155 fprintf (stderr, "read output is not the expected output\n");
156 exit (1);
159 fclose (fp);
160 status = 0;
161 while (waitpid (child, &status, 0) != child)
163 if (!WIFEXITED (status))
165 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
166 exit (1);
168 exitstatus = WEXITSTATUS (status);
169 if (exitstatus != 0)
171 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
172 exit (1);
177 main ()
179 is_qemu = is_running_under_qemu_user ();
181 test ("pwd");
183 /* Verify that if a program is given as a relative file name with at least one
184 slash, it is interpreted w.r.t. the current directory after fchdir has been
185 executed. */
186 if (!is_qemu)
188 const char *abs_pwd_prog = find_in_path ("pwd");
190 if (abs_pwd_prog != NULL
191 && abs_pwd_prog[0] == '/'
192 && abs_pwd_prog[1] != '0' && abs_pwd_prog[1] != '/')
194 /* Determine the location of the 'pwd' program, relative to
195 KNOWNDIR. */
196 const char *relative_pwd_prog;
197 #if defined __ANDROID__
198 relative_pwd_prog = xasprintf ("..%s", abs_pwd_prog);
199 #else
200 relative_pwd_prog = &abs_pwd_prog[1];
201 #endif
203 test (relative_pwd_prog);
207 return 0;