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. */
30 #include <sys/types.h>
35 #include "xvasprintf.h"
42 if (0 <= fd
&& fd
<= 2)
44 int f
= fd_safer (dup (fd
));
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"
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
;
80 knownfd
= open (KNOWNDIR
, O_RDONLY
);
83 perror ("cannot open directory");
86 if (pipe (ifd
) < 0 || (ifd
[0] = fd_safer (ifd
[0])) < 0)
88 perror ("cannot create pipe");
91 sigprocmask (SIG_SETMASK
, NULL
, &blocked_signals
);
92 sigemptyset (&fatal_signal_set
);
93 sigaddset (&fatal_signal_set
, SIGINT
);
94 sigaddset (&fatal_signal_set
, SIGTERM
);
96 sigaddset (&fatal_signal_set
, SIGHUP
);
99 sigaddset (&fatal_signal_set
, SIGPIPE
);
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__
116 (err
= posix_spawnattr_setsigmask (&attrs
, &blocked_signals
)) != 0
117 || (err
= posix_spawnattr_setflags (&attrs
, POSIX_SPAWN_SETSIGMASK
)) != 0
120 || (err
= posix_spawnp (&child
, pwd_prog
, &actions
, &attrs
, argv
, environ
)) != 0))
122 if (actions_allocated
)
123 posix_spawn_file_actions_destroy (&actions
);
125 posix_spawnattr_destroy (&attrs
);
126 sigprocmask (SIG_UNBLOCK
, &fatal_signal_set
, NULL
);
128 perror ("subprocess failed");
131 posix_spawn_file_actions_destroy (&actions
);
132 posix_spawnattr_destroy (&attrs
);
133 sigprocmask (SIG_UNBLOCK
, &fatal_signal_set
, NULL
);
136 fp
= fdopen (fd
, "r");
139 fprintf (stderr
, "fdopen() failed\n");
142 if (fread (line
, 1, 80, fp
) < 2)
144 fprintf (stderr
, "could not read expected output\n");
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
153 if (memcmp (line
, KNOWNDIR
"\n", strlen (KNOWNDIR
) + 1) != 0)
155 fprintf (stderr
, "read output is not the expected output\n");
161 while (waitpid (child
, &status
, 0) != child
)
163 if (!WIFEXITED (status
))
165 fprintf (stderr
, "subprocess terminated with unexpected wait status %d\n", status
);
168 exitstatus
= WEXITSTATUS (status
);
171 fprintf (stderr
, "subprocess terminated with unexpected exit status %d\n", exitstatus
);
179 is_qemu
= is_running_under_qemu_user ();
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
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
196 const char *relative_pwd_prog
;
197 #if defined __ANDROID__
198 relative_pwd_prog
= xasprintf ("..%s", abs_pwd_prog
);
200 relative_pwd_prog
= &abs_pwd_prog
[1];
203 test (relative_pwd_prog
);