1 /* Save and restore the working directory, possibly using a child process.
3 Copyright (C) 2006, 2007 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"
41 /* Save the working directory into *WD, if it hasn't been saved
42 already. Return true if a child has been forked to do the real
45 savewd_save (struct savewd *wd)
50 /* Save the working directory, or prepare to fall back if possible. */
52 int fd = open_safer (".", O_RDONLY);
59 if (errno != EACCES && errno != ESTALE)
61 wd->state = ERROR_STATE;
62 wd->val.errnum = errno;
66 wd->state = FORKING_STATE;
70 if (wd->val.child < 0)
72 /* "Save" the initial working directory by forking a new
73 subprocess that will attempt all the work from the chdir
74 until until the next savewd_restore. */
75 wd->val.child = fork ();
76 if (wd->val.child != 0)
78 if (0 < wd->val.child)
80 wd->state = ERROR_STATE;
81 wd->val.errnum = errno;
87 case FD_POST_CHDIR_STATE:
100 savewd_chdir (struct savewd *wd, char const *dir, int options,
106 /* Open the directory if requested, or if avoiding a race condition
107 is requested and possible. */
109 || (options & (HAVE_WORKING_O_NOFOLLOW ? SAVEWD_CHDIR_NOFOLLOW : 0)))
112 (O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
113 | (options & SAVEWD_CHDIR_NOFOLLOW ? O_NOFOLLOW : 0)));
118 open_result[1] = errno;
121 if (fd < 0 && (errno != EACCES || (options & SAVEWD_CHDIR_READABLE)))
125 if (result == 0 && ! (0 <= fd && options & SAVEWD_CHDIR_SKIP_READABLE))
127 if (savewd_save (wd))
134 result = (fd < 0 ? chdir (dir) : fchdir (fd));
140 wd->state = FD_POST_CHDIR_STATE;
144 case FD_POST_CHDIR_STATE:
149 assert (wd->val.child == 0);
158 if (0 <= fd && ! open_result)
169 savewd_restore (struct savewd *wd, int status)
175 /* The working directory is the desired directory, so there's no
179 case FD_POST_CHDIR_STATE:
180 /* Restore the working directory using fchdir. */
181 if (fchdir (wd->val.fd) == 0)
183 wd->state = FD_STATE;
188 int chdir_errno = errno;
190 wd->state = ERROR_STATE;
191 wd->val.errnum = chdir_errno;
195 /* Report an error if asked to restore the working directory. */
196 errno = wd->val.errnum;
200 /* "Restore" the working directory by waiting for the subprocess
203 pid_t child = wd->val.child;
209 while (waitpid (child, &child_status, 0) < 0)
210 assert (errno == EINTR);
212 if (! WIFEXITED (child_status))
213 raise (WTERMSIG (child_status));
214 return WEXITSTATUS (child_status);
227 savewd_finish (struct savewd *wd)
236 case FD_POST_CHDIR_STATE:
241 assert (wd->val.child < 0);
248 wd->state = FINAL_STATE;
251 /* Return true if the actual work is currently being done by a
254 A true return means that the caller and the subprocess should
255 resynchronize later with savewd_restore, using only their own
256 memory to decide when to resynchronize; they should not consult the
257 file system to decide, because that might lead to race conditions.
258 This is why savewd_chdir is broken out into another function;
259 savewd_chdir's callers _can_ inspect the file system to decide
260 whether to call savewd_chdir. */
262 savewd_delegating (struct savewd const *wd)
264 return wd->state == FORKING_STATE && 0 < wd->val.child;
268 savewd_process_files (int n_files, char **file,
269 int (*act) (char *, struct savewd *, void *),
274 int exit_status = EXIT_SUCCESS;
278 for (last_relative = n_files - 1; 0 <= last_relative; last_relative--)
279 if (! IS_ABSOLUTE_FILE_NAME (file[last_relative]))
282 for (; i < last_relative; i++)
284 if (! savewd_delegating (&wd))
286 int s = act (file[i], &wd, options);
291 if (! IS_ABSOLUTE_FILE_NAME (file[i + 1]))
293 int r = savewd_restore (&wd, exit_status);
301 for (; i < n_files; i++)
303 int s = act (file[i], &wd, options);