getroot: Save/restore CWD more reliably on Unix

Various GRUB utilities fail if the current directory doesn't exist,
because grub_find_device() chdirs to a different directory and then
fails when trying to chdir back.  Gnulib's save-cwd module uses fchdir()
instead when it can, avoiding this category of problem.

Fixes Debian bug #918700.

Signed-off-by: Colin Watson <cjwatson@ubuntu.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Colin Watson 2019-03-09 14:37:17 +00:00 committed by Daniel Kiper
parent 5bc41db756
commit 5dfa0afa16
2 changed files with 18 additions and 9 deletions

View file

@ -33,6 +33,7 @@ gnulib_modules="
progname progname
realloc-gnu realloc-gnu
regex regex
save-cwd
" "
gnulib_tool_option_extras="\ gnulib_tool_option_extras="\

View file

@ -34,6 +34,7 @@
#ifdef HAVE_LIMITS_H #ifdef HAVE_LIMITS_H
#include <limits.h> #include <limits.h>
#endif #endif
#include <grub/util/misc.h> #include <grub/util/misc.h>
#include <grub/emu/exec.h> #include <grub/emu/exec.h>
@ -113,6 +114,8 @@
#include <sys/mount.h> #include <sys/mount.h>
#endif #endif
#include "save-cwd.h"
#if !defined (__GNU__) #if !defined (__GNU__)
static void static void
strip_extra_slashes (char *dir) strip_extra_slashes (char *dir)
@ -352,7 +355,7 @@ char *
grub_find_device (const char *dir, dev_t dev) grub_find_device (const char *dir, dev_t dev)
{ {
DIR *dp; DIR *dp;
char *saved_cwd; struct saved_cwd saved_cwd;
struct dirent *ent; struct dirent *ent;
if (! dir) if (! dir)
@ -362,12 +365,17 @@ grub_find_device (const char *dir, dev_t dev)
if (! dp) if (! dp)
return 0; return 0;
saved_cwd = xgetcwd (); if (save_cwd (&saved_cwd) < 0)
{
grub_util_error ("%s", _("cannot save the original directory"));
closedir (dp);
return 0;
}
grub_util_info ("changing current directory to %s", dir); grub_util_info ("changing current directory to %s", dir);
if (chdir (dir) < 0) if (chdir (dir) < 0)
{ {
free (saved_cwd); free_cwd (&saved_cwd);
closedir (dp); closedir (dp);
return 0; return 0;
} }
@ -410,11 +418,11 @@ grub_find_device (const char *dir, dev_t dev)
if (res) if (res)
{ {
if (chdir (saved_cwd) < 0) if (restore_cwd (&saved_cwd) < 0)
grub_util_error ("%s", grub_util_error ("%s",
_("cannot restore the original directory")); _("cannot restore the original directory"));
free (saved_cwd); free_cwd (&saved_cwd);
closedir (dp); closedir (dp);
return res; return res;
} }
@ -468,19 +476,19 @@ grub_find_device (const char *dir, dev_t dev)
continue; continue;
} }
if (chdir (saved_cwd) < 0) if (restore_cwd (&saved_cwd) < 0)
grub_util_error ("%s", _("cannot restore the original directory")); grub_util_error ("%s", _("cannot restore the original directory"));
free (saved_cwd); free_cwd (&saved_cwd);
closedir (dp); closedir (dp);
return res; return res;
} }
} }
if (chdir (saved_cwd) < 0) if (restore_cwd (&saved_cwd) < 0)
grub_util_error ("%s", _("cannot restore the original directory")); grub_util_error ("%s", _("cannot restore the original directory"));
free (saved_cwd); free_cwd (&saved_cwd);
closedir (dp); closedir (dp);
return 0; return 0;
} }