fts: simplify fts_build
[gnulib.git] / lib / savewd.c
blob4579d30664894a9efb5524b12b1b834eebb97659
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 #ifndef FALLTHROUGH
40 # if __GNUC__ < 7
41 # define FALLTHROUGH ((void) 0)
42 # else
43 # define FALLTHROUGH __attribute__ ((__fallthrough__))
44 # endif
45 #endif
47 /* Save the working directory into *WD, if it hasn't been saved
48 already. Return true if a child has been forked to do the real
49 work. */
50 static bool
51 savewd_save (struct savewd *wd)
53 switch (wd->state)
55 case INITIAL_STATE:
56 /* Save the working directory, or prepare to fall back if possible. */
58 int fd = open_safer (".", O_SEARCH);
59 if (0 <= fd)
61 wd->state = FD_STATE;
62 wd->val.fd = fd;
63 break;
65 if (errno != EACCES && errno != ESTALE)
67 wd->state = ERROR_STATE;
68 wd->val.errnum = errno;
69 break;
72 wd->state = FORKING_STATE;
73 wd->val.child = -1;
74 FALLTHROUGH;
75 case FORKING_STATE:
76 if (wd->val.child < 0)
78 /* "Save" the initial working directory by forking a new
79 subprocess that will attempt all the work from the chdir
80 until the next savewd_restore. */
81 wd->val.child = fork ();
82 if (wd->val.child != 0)
84 if (0 < wd->val.child)
85 return true;
86 wd->state = ERROR_STATE;
87 wd->val.errnum = errno;
90 break;
92 case FD_STATE:
93 case FD_POST_CHDIR_STATE:
94 case ERROR_STATE:
95 case FINAL_STATE:
96 break;
98 default:
99 assure (false);
102 return false;
106 savewd_chdir (struct savewd *wd, char const *dir, int options,
107 int open_result[2])
109 int fd = -1;
110 int result = 0;
112 /* Open the directory if requested, or if avoiding a race condition
113 is requested and possible. */
114 if (open_result
115 || (options & (HAVE_WORKING_O_NOFOLLOW ? SAVEWD_CHDIR_NOFOLLOW : 0)))
117 fd = open (dir,
118 (O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
119 | (options & SAVEWD_CHDIR_NOFOLLOW ? O_NOFOLLOW : 0)));
121 if (open_result)
123 open_result[0] = fd;
124 open_result[1] = errno;
127 if (fd < 0 && errno != EACCES)
128 result = -1;
131 if (result == 0 && ! (0 <= fd && options & SAVEWD_CHDIR_SKIP_READABLE))
133 if (savewd_save (wd))
135 open_result = NULL;
136 result = -2;
138 else
140 result = (fd < 0 ? chdir (dir) : fchdir (fd));
142 if (result == 0)
143 switch (wd->state)
145 case FD_STATE:
146 wd->state = FD_POST_CHDIR_STATE;
147 break;
149 case ERROR_STATE:
150 case FD_POST_CHDIR_STATE:
151 case FINAL_STATE:
152 break;
154 case FORKING_STATE:
155 assure (wd->val.child == 0);
156 break;
158 default:
159 assure (false);
164 if (0 <= fd && ! open_result)
166 int e = errno;
167 close (fd);
168 errno = e;
171 return result;
175 savewd_restore (struct savewd *wd, int status)
177 switch (wd->state)
179 case INITIAL_STATE:
180 case FD_STATE:
181 /* The working directory is the desired directory, so there's no
182 work to do. */
183 break;
185 case FD_POST_CHDIR_STATE:
186 /* Restore the working directory using fchdir. */
187 if (fchdir (wd->val.fd) == 0)
189 wd->state = FD_STATE;
190 break;
192 else
194 int chdir_errno = errno;
195 close (wd->val.fd);
196 wd->state = ERROR_STATE;
197 wd->val.errnum = chdir_errno;
199 FALLTHROUGH;
200 case ERROR_STATE:
201 /* Report an error if asked to restore the working directory. */
202 errno = wd->val.errnum;
203 return -1;
205 case FORKING_STATE:
206 /* "Restore" the working directory by waiting for the subprocess
207 to finish. */
209 pid_t child = wd->val.child;
210 if (child == 0)
211 _exit (status);
212 if (0 < child)
214 int child_status;
215 while (waitpid (child, &child_status, 0) < 0)
216 assure (errno == EINTR);
217 wd->val.child = -1;
218 if (! WIFEXITED (child_status))
219 raise (WTERMSIG (child_status));
220 return WEXITSTATUS (child_status);
223 break;
225 default:
226 assure (false);
229 return 0;
232 void
233 savewd_finish (struct savewd *wd)
235 switch (wd->state)
237 case INITIAL_STATE:
238 case ERROR_STATE:
239 break;
241 case FD_STATE:
242 case FD_POST_CHDIR_STATE:
243 close (wd->val.fd);
244 break;
246 case FORKING_STATE:
247 assure (wd->val.child < 0);
248 break;
250 default:
251 assure (false);
254 wd->state = FINAL_STATE;
257 /* Return true if the actual work is currently being done by a
258 subprocess.
260 A true return means that the caller and the subprocess should
261 resynchronize later with savewd_restore, using only their own
262 memory to decide when to resynchronize; they should not consult the
263 file system to decide, because that might lead to race conditions.
264 This is why savewd_chdir is broken out into another function;
265 savewd_chdir's callers _can_ inspect the file system to decide
266 whether to call savewd_chdir. */
267 static bool
268 savewd_delegating (struct savewd const *wd)
270 return wd->state == FORKING_STATE && 0 < wd->val.child;
274 savewd_process_files (int n_files, char **file,
275 int (*act) (char *, struct savewd *, void *),
276 void *options)
278 int i = 0;
279 int last_relative;
280 int exit_status = EXIT_SUCCESS;
281 struct savewd wd;
282 savewd_init (&wd);
284 for (last_relative = n_files - 1; 0 <= last_relative; last_relative--)
285 if (! IS_ABSOLUTE_FILE_NAME (file[last_relative]))
286 break;
288 for (; i < last_relative; i++)
290 if (! savewd_delegating (&wd))
292 int s = act (file[i], &wd, options);
293 if (exit_status < s)
294 exit_status = s;
297 if (! IS_ABSOLUTE_FILE_NAME (file[i + 1]))
299 int r = savewd_restore (&wd, exit_status);
300 if (exit_status < r)
301 exit_status = r;
305 savewd_finish (&wd);
307 for (; i < n_files; i++)
309 int s = act (file[i], &wd, options);
310 if (exit_status < s)
311 exit_status = s;
314 return exit_status;