Skip to contentSkip to conversation

Add grub_util_readlink()

PATCH Superseded Javier Martinez Canillas 2 participants 6 messages Oct 18, 2019 – Oct 28, 2019 Kbuild

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add grub_util_readlink()

From: Peter Jones <[email protected]>

Add a grub_util_readlink() helper function.

---

 grub-core/osdep/windows/hostdisk.c    | 6 ++++++
 include/grub/osdep/hostfile_aros.h    | 6 ++++++
 include/grub/osdep/hostfile_unix.h    | 6 ++++++
 include/grub/osdep/hostfile_windows.h | 2 ++
 4 files changed, 20 insertions(+)
grub-core/osdep/windows/hostdisk.c +6 -0
index 355100789a7..87a106c9b82 100644
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }
 
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {
include/grub/osdep/hostfile_aros.h +6 -0
index a059c0fa40a..161fbb7bdfd 100644
@@ -68,6 +68,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 struct grub_util_fd
include/grub/osdep/hostfile_unix.h +6 -0
index 9ffe46fa3ca..17cd3aa8b30 100644
@@ -71,6 +71,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 #if defined (__NetBSD__)
include/grub/osdep/hostfile_windows.h +2 -0
index bf6451b6db4..8c92d0591bb 100644
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/2 Make editenv chase symlinks including those across devices.

From: Peter Jones <[email protected]>

This lets us make /boot/grub2/grubenv a symlink to /boot/efi/EFI/fedora/grubenv even though they're different mount points, which allows /usr/bin/grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

---

 Makefile.util.def | 11 +++++++++++
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 2 deletions(-)
Makefile.util.def +11 -0
index 969d32f0097..733a397cb2b 100644
@@ -240,8 +240,19 @@ program = {
 
   common = util/grub-editenv.c;
   common = util/editenv.c;
+  common = util/grub-install-common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/compress.c;
+  extra_dist = grub-core/osdep/unix/compress.c;
+  extra_dist = grub-core/osdep/basic/compress.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
+  ldadd = '$(LIBLZMA)';
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
util/editenv.c +44 -2

57 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH 2/2] Make editenv chase symlinks including those across devices.

Quoted 3 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

This lets us make /boot/grub2/grubenv a symlink to

Who or what creates that link?

Quoted 24 lines

/boot/efi/EFI/fedora/grubenv even though they're different mount points, which allows /usr/bin/grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

---

 Makefile.util.def | 11 +++++++++++
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 2 deletions(-)
Makefile.util.def +2 -0
index 969d32f0097..733a397cb2b 100644
@@ -240,8 +240,19 @@ program = {
 
   common = util/grub-editenv.c;
   common = util/editenv.c;
+  common = util/grub-install-common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/compress.c;

Makefile.util.def program = { · line 255

Quoted 10 lines
Makefile.util.def +9 -0
+  extra_dist = grub-core/osdep/unix/compress.c;
+  extra_dist = grub-core/osdep/basic/compress.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
+  ldadd = '$(LIBLZMA)';

Hmmm... Why do we need that change? Does not it belong to different patch?

Quoted 16 lines
Makefile.util.def +0 -0
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
util/editenv.c +1 -0
index eb2d0c03a98..e61dc1283a4 100644
@@ -37,6 +37,7 @@ grub_util_create_envblk_file (const char *name)
   FILE *fp;
   char *buf;
   char *namenew;
+  char *rename_target = xstrdup(name);
 
   buf = xmalloc (DEFAULT_ENVBLK_SIZE);
 

util/editenv.c grub_util_create_envblk_file (const char *name) · line 64

Quoted 6 lines
util/editenv.c +1 -2
@@ -60,7 +61,48 @@ grub_util_create_envblk_file (const char *name)
   free (buf);
   fclose (fp);
 
-  if (grub_util_rename (namenew, name) < 0)
-    grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
+  ssize_t size = 1;

I do not like variables declarations mixed with the code in that way. Please move it to the beginning of the function.

util/editenv.c grub_util_create_envblk_file (const char *name) · line 68

Quoted 4 lines
util/editenv.c +4 -0
+  while (1)
+    {
+      char *linkbuf;
+      ssize_t retsize;

...but I am OK with that... Hmmm... And it seems to me that size can be moved here.

util/editenv.c grub_util_create_envblk_file (const char *name) · line 70

Quoted 2 lines
util/editenv.c +2 -0
+
+      linkbuf = xmalloc(size+1);

linkbuf = xmalloc (size + 1); Missing spaces...

Quoted 6 lines
util/editenv.c +6 -0
+      retsize = grub_util_readlink (rename_target, linkbuf, size);
+      if (retsize < 0 && (errno == ENOENT || errno == EINVAL))
+	{
+	  free (linkbuf);
+	  break;
+	}

util/editenv.c grub_util_create_envblk_file (const char *name) · line 86

Quoted 10 lines
util/editenv.c +10 -0
+      else if (retsize < 0)
+	{
+	  grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
+	  free (linkbuf);
+	  free (namenew);
+	  return;
+	}
+      else if (retsize == size)
+	{
+	  free(linkbuf);

free (linkbuf);

util/editenv.c grub_util_create_envblk_file (const char *name) · line 96

Quoted 10 lines
util/editenv.c +10 -0
+	  size += 128;
+	  continue;
+	}
+
+      free (rename_target);
+      linkbuf[retsize] = '\0';
+      rename_target = linkbuf;
+    }
+
+  int rc = grub_util_rename (namenew, rename_target);

Please move this variable declaration to the beginning of function.

Daniel

#3 lore raw

Re: [PATCH 2/2] Make editenv chase symlinks including those across devices.

Quoted 7 lines · Daniel Kiper
Quoted 3 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

This lets us make /boot/grub2/grubenv a symlink to

Who or what creates that link?

That symlink is created by the Fedora grub2-efi package. On legacy BIOS installs is just a regular file from the grub2-pc package. That way for both EFI and legacy BIOS installs the /boot/grub2/grubenv is always used, even when for EFI the grubenv is in the ESP.

In both cases is the installer (Anaconda) the component that creates and sets the values of the actual grubenv file.

Quoted 33 lines
Quoted 33 lines

/boot/efi/EFI/fedora/grubenv even though they're different mount points, which allows /usr/bin/grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

---

 Makefile.util.def | 11 +++++++++++
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 2 deletions(-)
Makefile.util.def +10 -0
index 969d32f0097..733a397cb2b 100644
@@ -240,8 +240,19 @@ program = {
 
   common = util/grub-editenv.c;
   common = util/editenv.c;
+  common = util/grub-install-common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/compress.c;
+  extra_dist = grub-core/osdep/unix/compress.c;
+  extra_dist = grub-core/osdep/basic/compress.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
Quoted 4 lines

Makefile.util.def program = { · line 255

Quoted 1 line
Makefile.util.def +1 -0
+  ldadd = '$(LIBLZMA)';

Hmmm... Why do we need that change? Does not it belong to different patch?

That's needed due a dependency. The patch uses grub_install_copy_file() from util/grub-install-common.c and that uses grub_install_generate_image() that is defined in util/mkimage.c, which in turn uses lzma_stream_encoder().

So all the files added to common for grub-editenv are due dependencies pulled by grub-editenv now using the grub_install_copy_file() function.

Quoted 19 lines
Quoted 19 lines
Makefile.util.def +0 -0
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
util/editenv.c +1 -0
index eb2d0c03a98..e61dc1283a4 100644
@@ -37,6 +37,7 @@ grub_util_create_envblk_file (const char *name)
   FILE *fp;
   char *buf;
   char *namenew;
+  char *rename_target = xstrdup(name);
 
   buf = xmalloc (DEFAULT_ENVBLK_SIZE);
 
@@ -60,7 +61,48 @@ grub_util_create_envblk_file (const char *name)
   free (buf);
   fclose (fp);
 
Quoted 7 lines

util/editenv.c grub_util_create_envblk_file (const char *name) · line 64

Quoted 3 lines
util/editenv.c +1 -2
-  if (grub_util_rename (namenew, name) < 0)
-    grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
+  ssize_t size = 1;

I do not like variables declarations mixed with the code in that way. Please move it to the beginning of the function.

Ok.

Quoted 8 lines

util/editenv.c grub_util_create_envblk_file (const char *name) · line 68

Quoted 4 lines
util/editenv.c +4 -0
+  while (1)
+    {
+      char *linkbuf;
+      ssize_t retsize;

...but I am OK with that... Hmmm... And it seems to me that size can be moved here.

It can't be moved inside the while loop because the initial condition has to be set outside the loop. I'll move it at the beginning of the function as you suggested.

Quoted 5 lines

util/editenv.c grub_util_create_envblk_file (const char *name) · line 70

Quoted 2 lines
util/editenv.c +2 -0
+
+      linkbuf = xmalloc(size+1);

linkbuf = xmalloc (size + 1); Missing spaces...

Ok.

Quoted 10 lines
Quoted 10 lines
util/editenv.c +10 -0
+      retsize = grub_util_readlink (rename_target, linkbuf, size);
+      if (retsize < 0 && (errno == ENOENT || errno == EINVAL))
+	{
+	  free (linkbuf);
+	  break;
+	}
+      else if (retsize < 0)
+	{
+	  grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
+	  free (linkbuf);
Quoted 9 lines

util/editenv.c grub_util_create_envblk_file (const char *name) · line 86

Quoted 6 lines
util/editenv.c +6 -0
+	  free (namenew);
+	  return;
+	}
+      else if (retsize == size)
+	{
+	  free(linkbuf);

free (linkbuf);

Ok.

Quoted 4 lines
Quoted 4 lines
util/editenv.c +4 -0
+	  size += 128;
+	  continue;
+	}
+
Quoted 9 lines

util/editenv.c grub_util_create_envblk_file (const char *name) · line 96

Quoted 6 lines
util/editenv.c +6 -0
+      free (rename_target);
+      linkbuf[retsize] = '\0';
+      rename_target = linkbuf;
+    }
+
+  int rc = grub_util_rename (namenew, rename_target);

Please move this variable declaration to the beginning of function.

Ok.

Quoted 2 lines

Daniel

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#4 lore raw
In reply to Javier Martinez Canillas
Quoted 3 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

Add a grub_util_readlink() helper function.

OK but for patches like that one I will be always asking for an explannation in the commit message why we need this functionality/function(s). So, please always add it. If you do that for this ptach feel free to add my RB.

Quoted 77 lines

---

 grub-core/osdep/windows/hostdisk.c    | 6 ++++++
 include/grub/osdep/hostfile_aros.h    | 6 ++++++
 include/grub/osdep/hostfile_unix.h    | 6 ++++++
 include/grub/osdep/hostfile_windows.h | 2 ++
 4 files changed, 20 insertions(+)
grub-core/osdep/windows/hostdisk.c +6 -0
index 355100789a7..87a106c9b82 100644
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }
 
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {
include/grub/osdep/hostfile_aros.h +6 -0
index a059c0fa40a..161fbb7bdfd 100644
@@ -68,6 +68,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 struct grub_util_fd
include/grub/osdep/hostfile_unix.h +6 -0
index 9ffe46fa3ca..17cd3aa8b30 100644
@@ -71,6 +71,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 #if defined (__NetBSD__)
include/grub/osdep/hostfile_windows.h +2 -1
index bf6451b6db4..8c92d0591bb 100644
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void
--

2.21.0

#5 lore raw

Hello Daniel,

Quoted 9 lines · Daniel Kiper
Quoted 3 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

Add a grub_util_readlink() helper function.

OK but for patches like that one I will be always asking for an explannation in the commit message why we need this functionality/function(s). So, please always add it. If you do that for this ptach feel free to add my RB.

Yes, sorry for missing that there wasn't an explanation about why the change is needed. I've added now a proper commit message for v2.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat