[ChangeLog]
[pspp] / lib / savewd.c
1 /* Save and restore the working directory, possibly using a child process.
2
3    Copyright (C) 2006 Free Software Foundation, Inc.
4
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 2, or (at your option)
8    any later version.
9
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.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #include <config.h>
22
23 #include "savewd.h"
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <stdbool.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33
34 #include "exit.h"
35 #include "dirname.h"
36 #include "fcntl-safer.h"
37
38
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)
44 {
45   switch (wd->state)
46     {
47     case INITIAL_STATE:
48       /* Save the working directory, or prepare to fall back if possible.  */
49       {
50         int fd = open_safer (".", O_RDONLY);
51         if (0 <= fd)
52           {
53             wd->state = FD_STATE;
54             wd->val.fd = fd;
55             break;
56           }
57         if (errno != EACCES)
58           {
59             wd->state = ERROR_STATE;
60             wd->val.errnum = errno;
61             break;
62           }
63       }
64       wd->state = FORKING_STATE;
65       wd->val.child = -1;
66       /* Fall through.  */
67     case FORKING_STATE:
68       if (wd->val.child < 0)
69         {
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)
75             {
76               if (0 < wd->val.child)
77                 return true;
78               wd->state = ERROR_STATE;
79               wd->val.errnum = errno;
80             }
81         }
82       break;
83
84     case FD_STATE:
85     case FD_POST_CHDIR_STATE:
86     case ERROR_STATE:
87     case FINAL_STATE:
88       break;
89
90     default:
91       assert (false);
92     }
93
94   return false;
95 }
96
97 int
98 savewd_chdir (struct savewd *wd, char const *dir, int options,
99               int open_result[2])
100 {
101   int fd = -1;
102   int result = 0;
103
104   /* Open the directory if requested, or if avoiding a race condition
105      is requested and possible.  */
106   if (open_result || (options & (O_NOFOLLOW ? SAVEWD_CHDIR_NOFOLLOW : 0)))
107     {
108       fd = open (dir,
109                  (O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK
110                   | (options & SAVEWD_CHDIR_NOFOLLOW ? O_NOFOLLOW : 0)));
111
112       if (open_result)
113         {
114           open_result[0] = fd;
115           open_result[1] = errno;
116         }
117
118       if (fd < 0 && (errno != EACCES || (options & SAVEWD_CHDIR_READABLE)))
119         result = -1;
120     }
121
122   if (result == 0 && ! (0 <= fd && options & SAVEWD_CHDIR_SKIP_READABLE))
123     {
124       if (savewd_save (wd))
125         {
126           open_result = NULL;
127           result = -2;
128         }
129       else
130         {
131           result = (fd < 0 ? chdir (dir) : fchdir (fd));
132
133           if (result == 0)
134             switch (wd->state)
135               {
136               case FD_STATE:
137                 wd->state = FD_POST_CHDIR_STATE;
138                 break;
139
140               case ERROR_STATE:
141               case FD_POST_CHDIR_STATE:
142               case FINAL_STATE:
143                 break;
144
145               case FORKING_STATE:
146                 assert (wd->val.child == 0);
147                 break;
148
149               default:
150                 assert (false);
151               }
152         }
153     }
154
155   if (0 <= fd && ! open_result)
156     {
157       int e = errno;
158       close (fd);
159       errno = e;
160     }
161
162   return result;
163 }
164
165 int
166 savewd_restore (struct savewd *wd, int status)
167 {
168   switch (wd->state)
169     {
170     case INITIAL_STATE:
171     case FD_STATE:
172       /* The working directory is the desired directory, so there's no
173          work to do.  */
174       break;
175
176     case FD_POST_CHDIR_STATE:
177       /* Restore the working directory using fchdir.  */
178       if (fchdir (wd->val.fd) == 0)
179         {
180           wd->state = FD_STATE;
181           break;
182         }
183       else
184         {
185           int chdir_errno = errno;
186           close (wd->val.fd);
187           wd->state = ERROR_STATE;
188           wd->val.errnum = chdir_errno;
189         }
190       /* Fall through.  */
191     case ERROR_STATE:
192       /* Report an error if asked to restore the working directory.  */
193       errno = wd->val.errnum;
194       return -1;
195
196     case FORKING_STATE:
197       /* "Restore" the working directory by waiting for the subprocess
198          to finish.  */
199       {
200         pid_t child = wd->val.child;
201         if (child == 0)
202           _exit (status);
203         if (0 < child)
204           {
205             int child_status;
206             while (waitpid (child, &child_status, 0) < 0)
207               assert (errno == EINTR);
208             wd->val.child = -1;
209             if (! WIFEXITED (child_status))
210               raise (WTERMSIG (child_status));
211             return WEXITSTATUS (child_status);
212           }
213       }
214       break;
215
216     default:
217       assert (false);
218     }
219
220   return 0;
221 }
222
223 void
224 savewd_finish (struct savewd *wd)
225 {
226   switch (wd->state)
227     {
228     case INITIAL_STATE:
229     case ERROR_STATE:
230       break;
231
232     case FD_STATE:
233     case FD_POST_CHDIR_STATE:
234       close (wd->val.fd);
235       break;
236
237     case FORKING_STATE:
238       assert (wd->val.child < 0);
239       break;
240
241     default:
242       assert (false);
243     }
244
245   wd->state = FINAL_STATE;
246 }
247
248 /* Return true if the actual work is currently being done by a
249    subprocess.
250
251    A true return means that the caller and the subprocess should
252    resynchronize later with savewd_restore, using only their own
253    memory to decide when to resynchronize; they should not consult the
254    file system to decide, because that might lead to race conditions.
255    This is why savewd_chdir is broken out into another function;
256    savewd_chdir's callers _can_ inspect the file system to decide
257    whether to call savewd_chdir.  */
258 static inline bool
259 savewd_delegating (struct savewd const *wd)
260 {
261   return wd->state == FORKING_STATE && 0 < wd->val.child;
262 }
263
264 int
265 savewd_process_files (int n_files, char **file,
266                       int (*act) (char *, struct savewd *, void *),
267                       void *options)
268 {
269   int i = 0;
270   int last_relative;
271   int exit_status = EXIT_SUCCESS;
272   struct savewd wd;
273   savewd_init (&wd);
274
275   for (last_relative = n_files - 1; 0 <= last_relative; last_relative--)
276     if (! IS_ABSOLUTE_FILE_NAME (file[last_relative]))
277       break;
278
279   for (; i < last_relative; i++)
280     {
281       if (! savewd_delegating (&wd))
282         {
283           int s = act (file[i], &wd, options);
284           if (exit_status < s)
285             exit_status = s;
286         }
287
288       if (! IS_ABSOLUTE_FILE_NAME (file[i + 1]))
289         {
290           int r = savewd_restore (&wd, exit_status);
291           if (exit_status < r)
292             exit_status = r;
293         }
294     }
295
296   savewd_finish (&wd);
297
298   for (; i < n_files; i++)
299     {
300       int s = act (file[i], &wd, options);
301       if (exit_status < s)
302         exit_status = s;
303     }
304
305   return exit_status;
306 }