get-rusage-data tests: Avoid failure on Linux/glibc.
[gnulib/ericb.git] / lib / savewd.c
blob138f865782f761e485aacaf71929842fda1a8ea5
1 /* Save and restore the working directory, possibly using a child process.
3 Copyright (C) 2006-2007, 2009-2017 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #include <config.h>
22 #define SAVEWD_INLINE _GL_EXTERN_INLINE
24 #include "savewd.h"
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <stdbool.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
35 #include "assure.h"
36 #include "dosname.h"
37 #include "fcntl-safer.h"
39 /* Save the working directory into *WD, if it hasn't been saved
40 already. Return true if a child has been forked to do the real
41 work. */
42 static bool
43 savewd_save (struct savewd *wd)
45 switch (wd->state)
47 case INITIAL_STATE:
48 /* Save the working directory, or prepare to fall back if possible. */
50 int fd = open_safer (".", O_SEARCH);
51 if (0 <= fd)
53 wd->state = FD_STATE;
54 wd->val.fd = fd;
55 break;
57 if (errno != EACCES && errno != ESTALE)
59 wd->state = ERROR_STATE;
60 wd->val.errnum = errno;
61 break;
64 wd->state = FORKING_STATE;
65 wd->val.child = -1;
66 /* Fall through. */
67 case FORKING_STATE:
68 if (wd->val.child < 0)
70 /* "Save" the initial working directory by forking a new
71 subprocess that will attempt all the work from the chdir
72 until until the next savewd_restore. */
73 wd->val.child = fork ();
74 if (wd->val.child != 0)
76 if (0 < wd->val.child)
77 return true;
78 wd->state = ERROR_STATE;
79 wd->val.errnum = errno;
82 break;
84 case FD_STATE:
85 case FD_POST_CHDIR_STATE:
86 case ERROR_STATE:
87 case FINAL_STATE:
88 break;
90 default:
91 assure (false);
94 return false;
97 int
98 savewd_chdir (struct savewd *wd, char const *dir, int options,
99 int open_result[2])
101 int fd = -1;
102 int result = 0;
104 /* Open the directory if requested, or if avoiding a race condition
105 is requested and possible. */
106 if (open_result
107 || (options & (HAVE_WORKING_O_NOFOLLOW ? SAVEWD_CHDIR_NOFOLLOW : 0)))
109 fd = open (dir,
110 (O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
111 | (options & SAVEWD_CHDIR_NOFOLLOW ? O_NOFOLLOW : 0)));
113 if (open_result)
115 open_result[0] = fd;
116 open_result[1] = errno;
119 if (fd < 0 && errno != EACCES)
120 result = -1;
123 if (result == 0 && ! (0 <= fd && options & SAVEWD_CHDIR_SKIP_READABLE))
125 if (savewd_save (wd))
127 open_result = NULL;
128 result = -2;
130 else
132 result = (fd < 0 ? chdir (dir) : fchdir (fd));
134 if (result == 0)
135 switch (wd->state)
137 case FD_STATE:
138 wd->state = FD_POST_CHDIR_STATE;
139 break;
141 case ERROR_STATE:
142 case FD_POST_CHDIR_STATE:
143 case FINAL_STATE:
144 break;
146 case FORKING_STATE:
147 assure (wd->val.child == 0);
148 break;
150 default:
151 assure (false);
156 if (0 <= fd && ! open_result)
158 int e = errno;
159 close (fd);
160 errno = e;
163 return result;
167 savewd_restore (struct savewd *wd, int status)
169 switch (wd->state)
171 case INITIAL_STATE:
172 case FD_STATE:
173 /* The working directory is the desired directory, so there's no
174 work to do. */
175 break;
177 case FD_POST_CHDIR_STATE:
178 /* Restore the working directory using fchdir. */
179 if (fchdir (wd->val.fd) == 0)
181 wd->state = FD_STATE;
182 break;
184 else
186 int chdir_errno = errno;
187 close (wd->val.fd);
188 wd->state = ERROR_STATE;
189 wd->val.errnum = chdir_errno;
191 /* Fall through. */
192 case ERROR_STATE:
193 /* Report an error if asked to restore the working directory. */
194 errno = wd->val.errnum;
195 return -1;
197 case FORKING_STATE:
198 /* "Restore" the working directory by waiting for the subprocess
199 to finish. */
201 pid_t child = wd->val.child;
202 if (child == 0)
203 _exit (status);
204 if (0 < child)
206 int child_status;
207 while (waitpid (child, &child_status, 0) < 0)
208 assure (errno == EINTR);
209 wd->val.child = -1;
210 if (! WIFEXITED (child_status))
211 raise (WTERMSIG (child_status));
212 return WEXITSTATUS (child_status);
215 break;
217 default:
218 assure (false);
221 return 0;
224 void
225 savewd_finish (struct savewd *wd)
227 switch (wd->state)
229 case INITIAL_STATE:
230 case ERROR_STATE:
231 break;
233 case FD_STATE:
234 case FD_POST_CHDIR_STATE:
235 close (wd->val.fd);
236 break;
238 case FORKING_STATE:
239 assure (wd->val.child < 0);
240 break;
242 default:
243 assure (false);
246 wd->state = FINAL_STATE;
249 /* Return true if the actual work is currently being done by a
250 subprocess.
252 A true return means that the caller and the subprocess should
253 resynchronize later with savewd_restore, using only their own
254 memory to decide when to resynchronize; they should not consult the
255 file system to decide, because that might lead to race conditions.
256 This is why savewd_chdir is broken out into another function;
257 savewd_chdir's callers _can_ inspect the file system to decide
258 whether to call savewd_chdir. */
259 static bool
260 savewd_delegating (struct savewd const *wd)
262 return wd->state == FORKING_STATE && 0 < wd->val.child;
266 savewd_process_files (int n_files, char **file,
267 int (*act) (char *, struct savewd *, void *),
268 void *options)
270 int i = 0;
271 int last_relative;
272 int exit_status = EXIT_SUCCESS;
273 struct savewd wd;
274 savewd_init (&wd);
276 for (last_relative = n_files - 1; 0 <= last_relative; last_relative--)
277 if (! IS_ABSOLUTE_FILE_NAME (file[last_relative]))
278 break;
280 for (; i < last_relative; i++)
282 if (! savewd_delegating (&wd))
284 int s = act (file[i], &wd, options);
285 if (exit_status < s)
286 exit_status = s;
289 if (! IS_ABSOLUTE_FILE_NAME (file[i + 1]))
291 int r = savewd_restore (&wd, exit_status);
292 if (exit_status < r)
293 exit_status = r;
297 savewd_finish (&wd);
299 for (; i < n_files; i++)
301 int s = act (file[i], &wd, options);
302 if (exit_status < s)
303 exit_status = s;
306 return exit_status;