All of lore.kernel.org
 help / color / mirror / Atom feed
From: Colin Watson <cjwatson@ubuntu.com>
To: grub-devel@gnu.org
Cc: Russell Coker <russell@coker.com.au>, 918700-forwarded@bugs.debian.org
Subject: [PATCH] Save/restore CWD more reliably on Unix
Date: Sat, 9 Mar 2019 14:37:17 +0000	[thread overview]
Message-ID: <20190309143717.zrkype4f2lewl42u@riva.ucam.org> (raw)
In-Reply-To: <154695275386.14026.18156383182224295530.reportbug@liv>

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>
---
 bootstrap.conf                 |  1 +
 grub-core/osdep/unix/getroot.c | 26 +++++++++++++++++---------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 6781d25a7..988dda099 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -33,6 +33,7 @@ gnulib_modules="
   progname
   realloc-gnu
   regex
+  save-cwd
 "
 
 gnulib_tool_option_extras="\
diff --git a/grub-core/osdep/unix/getroot.c b/grub-core/osdep/unix/getroot.c
index 3046e22cc..46d7116c6 100644
--- a/grub-core/osdep/unix/getroot.c
+++ b/grub-core/osdep/unix/getroot.c
@@ -34,6 +34,7 @@
 #ifdef HAVE_LIMITS_H
 #include <limits.h>
 #endif
+
 #include <grub/util/misc.h>
 #include <grub/emu/exec.h>
 
@@ -113,6 +114,8 @@
 #include <sys/mount.h>
 #endif
 
+#include "save-cwd.h"
+
 #if !defined (__GNU__)
 static void
 strip_extra_slashes (char *dir)
@@ -352,7 +355,7 @@ char *
 grub_find_device (const char *dir, dev_t dev)
 {
   DIR *dp;
-  char *saved_cwd;
+  struct saved_cwd saved_cwd;
   struct dirent *ent;
 
   if (! dir)
@@ -362,12 +365,17 @@ grub_find_device (const char *dir, dev_t dev)
   if (! dp)
     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);
   if (chdir (dir) < 0)
     {
-      free (saved_cwd);
+      free_cwd (&saved_cwd);
       closedir (dp);
       return 0;
     }
@@ -410,11 +418,11 @@ grub_find_device (const char *dir, dev_t dev)
 
 	  if (res)
 	    {
-	      if (chdir (saved_cwd) < 0)
+	      if (restore_cwd (&saved_cwd) < 0)
 		grub_util_error ("%s",
 				 _("cannot restore the original directory"));
 
-	      free (saved_cwd);
+	      free_cwd (&saved_cwd);
 	      closedir (dp);
 	      return res;
 	    }
@@ -468,19 +476,19 @@ grub_find_device (const char *dir, dev_t dev)
 	      continue;
 	    }
 
-	  if (chdir (saved_cwd) < 0)
+	  if (restore_cwd (&saved_cwd) < 0)
 	    grub_util_error ("%s", _("cannot restore the original directory"));
 
-	  free (saved_cwd);
+	  free_cwd (&saved_cwd);
 	  closedir (dp);
 	  return res;
 	}
     }
 
-  if (chdir (saved_cwd) < 0)
+  if (restore_cwd (&saved_cwd) < 0)
     grub_util_error ("%s", _("cannot restore the original directory"));
 
-  free (saved_cwd);
+  free_cwd (&saved_cwd);
   closedir (dp);
   return 0;
 }
-- 
2.17.1


       reply	other threads:[~2019-03-09 14:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <154695275386.14026.18156383182224295530.reportbug@liv>
2019-03-09 14:37 ` Colin Watson [this message]
2019-03-12 12:16   ` [PATCH] Save/restore CWD more reliably on Unix Daniel Kiper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190309143717.zrkype4f2lewl42u@riva.ucam.org \
    --to=cjwatson@ubuntu.com \
    --cc=918700-forwarded@bugs.debian.org \
    --cc=grub-devel@gnu.org \
    --cc=russell@coker.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.