diff options
| author | Paul Eggert | 2014-08-10 13:40:57 -0700 |
|---|---|---|
| committer | Paul Eggert | 2014-08-10 13:40:57 -0700 |
| commit | f22bc725a1c8294fc042f6a9541be01a177755f6 (patch) | |
| tree | c54be44171d43157177e8879d5ec3d583edfe5a1 /lib | |
| parent | d5f2feb5818b9da8323bda683983fa7e637c47fd (diff) | |
| download | emacs-f22bc725a1c8294fc042f6a9541be01a177755f6.tar.gz emacs-f22bc725a1c8294fc042f6a9541be01a177755f6.zip | |
Don't prevent random file systems from being unmounted.
This fix relies on having the 'fchdir' function, and on having
"." be searchable (or at least readable, on platforms lacking O_SEARCH),
but that's good enough to handle the vast majority of cases and the
remaining folks can just live with the annoyance of file systems
that occasionally can't be unmounted.
* configure.ac (fchdir): New function to check for.
* lib/save-cwd.c: Copy from gnulib, except omit the part that
allocates memory, since that can cause problems in Emacs.
* lib/save-cwd.h: Copy from gnulib.
Fixes: debbugs:18232
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/save-cwd.c | 90 | ||||
| -rw-r--r-- | lib/save-cwd.h | 40 |
2 files changed, 103 insertions, 27 deletions
diff --git a/lib/save-cwd.c b/lib/save-cwd.c index b8dae34ca02..fd746584fa8 100644 --- a/lib/save-cwd.c +++ b/lib/save-cwd.c | |||
| @@ -1,3 +1,91 @@ | |||
| 1 | /* save-cwd.c -- Save and restore current working directory. | ||
| 2 | |||
| 3 | Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2014 Free Software | ||
| 4 | Foundation, Inc. | ||
| 5 | |||
| 6 | This program is free software: you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation; either version 3 of the License, or | ||
| 9 | (at your option) any later version. | ||
| 10 | |||
| 11 | This program is distributed in the hope that it will be useful, | ||
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | GNU General Public License for more details. | ||
| 15 | |||
| 16 | You should have received a copy of the GNU General Public License | ||
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | ||
| 18 | |||
| 19 | /* Gnulib needs to save and restore the current working directory to | ||
| 20 | fully emulate functions like fstatat. But Emacs doesn't care what | ||
| 21 | the current working directory is; it always uses absolute file | ||
| 22 | names. This module replaces the Gnulib module by omitting the code | ||
| 23 | that Emacs does not need. */ | ||
| 24 | |||
| 1 | #include <config.h> | 25 | #include <config.h> |
| 2 | #define SAVE_CWD_INLINE _GL_EXTERN_INLINE | 26 | |
| 3 | #include "save-cwd.h" | 27 | #include "save-cwd.h" |
| 28 | |||
| 29 | #include <fcntl.h> | ||
| 30 | #include <unistd.h> | ||
| 31 | |||
| 32 | /* Record the location of the current working directory in CWD so that | ||
| 33 | the program may change to other directories and later use restore_cwd | ||
| 34 | to return to the recorded location. This function may allocate | ||
| 35 | space using malloc (via getcwd) or leave a file descriptor open; | ||
| 36 | use free_cwd to perform the necessary free or close. Upon failure, | ||
| 37 | no memory is allocated, any locally opened file descriptors are | ||
| 38 | closed; return non-zero -- in that case, free_cwd need not be | ||
| 39 | called, but doing so is ok. Otherwise, return zero. | ||
| 40 | |||
| 41 | The _raison d'etre_ for this interface is that the working directory | ||
| 42 | is sometimes inaccessible, and getcwd is not robust or as efficient. | ||
| 43 | So, we prefer to use the open/fchdir approach, but fall back on | ||
| 44 | getcwd if necessary. This module works for most cases with just | ||
| 45 | the getcwd-lgpl module, but to be truly robust, use the getcwd module. | ||
| 46 | |||
| 47 | Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin, | ||
| 48 | SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it | ||
| 49 | doesn't work for partitions on which auditing is enabled. If | ||
| 50 | you're still using an obsolete system with these problems, please | ||
| 51 | send email to the maintainer of this code. */ | ||
| 52 | |||
| 53 | int | ||
| 54 | save_cwd (struct saved_cwd *cwd) | ||
| 55 | { | ||
| 56 | #ifdef HAVE_FCHDIR | ||
| 57 | cwd->desc = open (".", O_SEARCH | O_CLOEXEC); | ||
| 58 | #else | ||
| 59 | cwd->desc = -1; | ||
| 60 | #endif | ||
| 61 | /* The 'name' member is present only to minimize differences from | ||
| 62 | gnulib. Initialize it to zero, if only to simplify debugging. */ | ||
| 63 | cwd->name = 0; | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | /* Change to recorded location, CWD, in directory hierarchy. | ||
| 68 | Upon failure, return -1 (errno is set by chdir or fchdir). | ||
| 69 | Upon success, return zero. */ | ||
| 70 | |||
| 71 | int | ||
| 72 | restore_cwd (const struct saved_cwd *cwd) | ||
| 73 | { | ||
| 74 | #ifdef HAVE_FCHDIR | ||
| 75 | /* Restore the previous directory if possible, to avoid tying down | ||
| 76 | the file system of the new directory (Bug#18232). */ | ||
| 77 | if (fchdir (cwd->desc) == 0) | ||
| 78 | return 0; | ||
| 79 | |||
| 80 | /* Don't worry if fchdir fails, as Emacs doesn't care what the | ||
| 81 | working directory is. The fchdir call is inside an 'if' merely to | ||
| 82 | pacify compilers that complain if fchdir's return value is ignored. */ | ||
| 83 | #endif | ||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | void | ||
| 88 | free_cwd (struct saved_cwd *cwd) | ||
| 89 | { | ||
| 90 | close (cwd->desc); | ||
| 91 | } | ||
diff --git a/lib/save-cwd.h b/lib/save-cwd.h index 9a1eb3519c8..6b84e4601d3 100644 --- a/lib/save-cwd.h +++ b/lib/save-cwd.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* Do not save and restore the current working directory. | 1 | /* Save and restore current working directory. |
| 2 | 2 | ||
| 3 | Copyright 2013-2014 Free Software Foundation, Inc. | 3 | Copyright (C) 1995, 1997-1998, 2003, 2009-2014 Free Software Foundation, |
| 4 | Inc. | ||
| 4 | 5 | ||
| 5 | This program is free software: you can redistribute it and/or modify | 6 | 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 | it under the terms of the GNU General Public License as published by |
| @@ -15,32 +16,19 @@ | |||
| 15 | You should have received a copy of the GNU General Public License | 16 | 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/>. */ | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | 18 | ||
| 18 | /* Gnulib needs to save and restore the current working directory to | 19 | /* Written by Jim Meyering. */ |
| 19 | fully emulate functions like fstatat. But Emacs doesn't care what | ||
| 20 | the current working directory is; it always uses absolute file | ||
| 21 | names. This module replaces the Gnulib module by omitting the code | ||
| 22 | that Emacs does not need. */ | ||
| 23 | 20 | ||
| 24 | #ifndef SAVE_CWD_H | 21 | #ifndef SAVE_CWD_H |
| 25 | #define SAVE_CWD_H 1 | 22 | # define SAVE_CWD_H 1 |
| 26 | 23 | ||
| 27 | _GL_INLINE_HEADER_BEGIN | 24 | struct saved_cwd |
| 28 | #ifndef SAVE_CWD_INLINE | 25 | { |
| 29 | # define SAVE_CWD_INLINE _GL_INLINE | 26 | int desc; |
| 30 | #endif | 27 | char *name; |
| 28 | }; | ||
| 31 | 29 | ||
| 32 | struct saved_cwd { int desc; }; | 30 | int save_cwd (struct saved_cwd *cwd); |
| 31 | int restore_cwd (const struct saved_cwd *cwd); | ||
| 32 | void free_cwd (struct saved_cwd *cwd); | ||
| 33 | 33 | ||
| 34 | SAVE_CWD_INLINE int | 34 | #endif /* SAVE_CWD_H */ |
| 35 | save_cwd (struct saved_cwd *cwd) | ||
| 36 | { | ||
| 37 | cwd->desc = -1; | ||
| 38 | return 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | SAVE_CWD_INLINE int restore_cwd (struct saved_cwd const *cwd) { return 0; } | ||
| 42 | SAVE_CWD_INLINE void free_cwd (struct saved_cwd *cwd) { } | ||
| 43 | |||
| 44 | _GL_INLINE_HEADER_END | ||
| 45 | |||
| 46 | #endif | ||