1 /* Save and restore the working directory, possibly using a child process.
3 Copyright (C) 2006-2007, 2009-2010 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. */
30 #include <sys/types.h>
35 #include "fcntl-safer.h"
37 /* Save the working directory into *WD, if it hasn't been saved
38 already. Return true if a child has been forked to do the real
41 savewd_save (struct savewd *wd)
46 /* Save the working directory, or prepare to fall back if possible. */
48 int fd = open_safer (".", O_RDONLY);
55 if (errno != EACCES && errno != ESTALE)
57 wd->state = ERROR_STATE;
58 wd->val.errnum = errno;
62 wd->state = FORKING_STATE;
66 if (wd->val.child < 0)
68 /* "Save" the initial working directory by forking a new
69 subprocess that will attempt all the work from the chdir
70 until until the next savewd_restore. */
71 wd->val.child = fork ();
72 if (wd->val.child != 0)
74 if (0 < wd->val.child)
76 wd->state = ERROR_STATE;
77 wd->val.errnum = errno;
83 case FD_POST_CHDIR_STATE:
96 savewd_chdir (struct savewd *wd, char const *dir, int options,
102 /* Open the directory if requested, or if avoiding a race condition
103 is requested and possible. */
105 || (options & (HAVE_WORKING_O_NOFOLLOW ? SAVEWD_CHDIR_NOFOLLOW : 0)))
108 (O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
109 | (options & SAVEWD_CHDIR_NOFOLLOW ? O_NOFOLLOW : 0)));
114 open_result[1] = errno;
117 if (fd < 0 && (errno != EACCES || (options & SAVEWD_CHDIR_READABLE)))
121 if (result == 0 && ! (0 <= fd && options & SAVEWD_CHDIR_SKIP_READABLE))
123 if (savewd_save (wd))
130 result = (fd < 0 ? chdir (dir) : fchdir (fd));
136 wd->state = FD_POST_CHDIR_STATE;
140 case FD_POST_CHDIR_STATE:
145 assert (wd->val.child == 0);
154 if (0 <= fd && ! open_result)
165 savewd_restore (struct savewd *wd, int status)
171 /* The working directory is the desired directory, so there's no
175 case FD_POST_CHDIR_STATE:
176 /* Restore the working directory using fchdir. */
177 if (fchdir (wd->val.fd) == 0)
179 wd->state = FD_STATE;
184 int chdir_errno = errno;
186 wd->state = ERROR_STATE;
187 wd->val.errnum = chdir_errno;
191 /* Report an error if asked to restore the working directory. */
192 errno = wd->val.errnum;
196 /* "Restore" the working directory by waiting for the subprocess
199 pid_t child = wd->val.child;
205 while (waitpid (child, &child_status, 0) < 0)
206 assert (errno == EINTR);
208 if (! WIFEXITED (child_status))
209 raise (WTERMSIG (child_status));
210 return WEXITSTATUS (child_status);
223 savewd_finish (struct savewd *wd)
232 case FD_POST_CHDIR_STATE:
237 assert (wd->val.child < 0);
244 wd->state = FINAL_STATE;
247 /* Return true if the actual work is currently being done by a
250 A true return means that the caller and the subprocess should
251 resynchronize later with savewd_restore, using only their own
252 memory to decide when to resynchronize; they should not consult the
253 file system to decide, because that might lead to race conditions.
254 This is why savewd_chdir is broken out into another function;
255 savewd_chdir's callers _can_ inspect the file system to decide
256 whether to call savewd_chdir. */
258 savewd_delegating (struct savewd const *wd)
260 return wd->state == FORKING_STATE && 0 < wd->val.child;
264 savewd_process_files (int n_files, char **file,
265 int (*act) (char *, struct savewd *, void *),
270 int exit_status = EXIT_SUCCESS;
274 for (last_relative = n_files - 1; 0 <= last_relative; last_relative--)
275 if (! IS_ABSOLUTE_FILE_NAME (file[last_relative]))
278 for (; i < last_relative; i++)
280 if (! savewd_delegating (&wd))
282 int s = act (file[i], &wd, options);
287 if (! IS_ABSOLUTE_FILE_NAME (file[i + 1]))
289 int r = savewd_restore (&wd, exit_status);
297 for (; i < n_files; i++)
299 int s = act (file[i], &wd, options);