All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Add grub_util_readlink()
@ 2019-10-18 12:42 Javier Martinez Canillas
  2019-10-18 12:42 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Javier Martinez Canillas
  2019-10-23  9:19 ` [PATCH 1/2] Add grub_util_readlink() Daniel Kiper
  0 siblings, 2 replies; 8+ messages in thread
From: Javier Martinez Canillas @ 2019-10-18 12:42 UTC (permalink / raw)
  To: grub-devel; +Cc: Peter Jones, Adam Jackson, Javier Martinez Canillas

From: Peter Jones <pjones@redhat.com>

Add a grub_util_readlink() helper function.

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---

 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(+)

diff --git a/grub-core/osdep/windows/hostdisk.c b/grub-core/osdep/windows/hostdisk.c
index 355100789a7..87a106c9b82 100644
--- a/grub-core/osdep/windows/hostdisk.c
+++ b/grub-core/osdep/windows/hostdisk.c
@@ -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)
 {
diff --git a/include/grub/osdep/hostfile_aros.h b/include/grub/osdep/hostfile_aros.h
index a059c0fa40a..161fbb7bdfd 100644
--- a/include/grub/osdep/hostfile_aros.h
+++ b/include/grub/osdep/hostfile_aros.h
@@ -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
diff --git a/include/grub/osdep/hostfile_unix.h b/include/grub/osdep/hostfile_unix.h
index 9ffe46fa3ca..17cd3aa8b30 100644
--- a/include/grub/osdep/hostfile_unix.h
+++ b/include/grub/osdep/hostfile_unix.h
@@ -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__)
diff --git a/include/grub/osdep/hostfile_windows.h b/include/grub/osdep/hostfile_windows.h
index bf6451b6db4..8c92d0591bb 100644
--- a/include/grub/osdep/hostfile_windows.h
+++ b/include/grub/osdep/hostfile_windows.h
@@ -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



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] Make editenv chase symlinks including those across devices.
  2019-10-18 12:42 [PATCH 1/2] Add grub_util_readlink() Javier Martinez Canillas
@ 2019-10-18 12:42 ` Javier Martinez Canillas
  2019-10-23  9:42   ` Daniel Kiper
  2019-10-23  9:19 ` [PATCH 1/2] Add grub_util_readlink() Daniel Kiper
  1 sibling, 1 reply; 8+ messages in thread
From: Javier Martinez Canillas @ 2019-10-18 12:42 UTC (permalink / raw)
  To: grub-devel; +Cc: Peter Jones, Adam Jackson, Javier Martinez Canillas

From: Peter Jones <pjones@redhat.com>

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).

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---

 Makefile.util.def | 11 +++++++++++
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index 969d32f0097..733a397cb2b 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -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;
diff --git a/util/editenv.c b/util/editenv.c
index eb2d0c03a98..e61dc1283a4 100644
--- a/util/editenv.c
+++ b/util/editenv.c
@@ -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);
 
-  if (grub_util_rename (namenew, name) < 0)
-    grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
+  ssize_t size = 1;
+  while (1)
+    {
+      char *linkbuf;
+      ssize_t retsize;
+
+      linkbuf = xmalloc(size+1);
+      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);
+	  free (namenew);
+	  return;
+	}
+      else if (retsize == size)
+	{
+	  free(linkbuf);
+	  size += 128;
+	  continue;
+	}
+
+      free (rename_target);
+      linkbuf[retsize] = '\0';
+      rename_target = linkbuf;
+    }
+
+  int rc = grub_util_rename (namenew, rename_target);
+  if (rc < 0 && errno == EXDEV)
+    {
+      rc = grub_install_copy_file (namenew, rename_target, 1);
+      grub_util_unlink (namenew);
+    }
+
+  if (rc < 0)
+    grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
+
   free (namenew);
+  free (rename_target);
 }
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Add grub_util_readlink()
  2019-10-18 12:42 [PATCH 1/2] Add grub_util_readlink() Javier Martinez Canillas
  2019-10-18 12:42 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Javier Martinez Canillas
@ 2019-10-23  9:19 ` Daniel Kiper
  2019-10-28 13:04   ` Javier Martinez Canillas
  1 sibling, 1 reply; 8+ messages in thread
From: Daniel Kiper @ 2019-10-23  9:19 UTC (permalink / raw)
  To: Javier Martinez Canillas; +Cc: grub-devel, Peter Jones, Adam Jackson

On Fri, Oct 18, 2019 at 02:42:20PM +0200, Javier Martinez Canillas wrote:
> From: Peter Jones <pjones@redhat.com>
>
> 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.

> Signed-off-by: Peter Jones <pjones@redhat.com>
> Reviewed-by: Adam Jackson <ajax@redhat.com>
> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
> ---
>
>  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(+)
>
> diff --git a/grub-core/osdep/windows/hostdisk.c b/grub-core/osdep/windows/hostdisk.c
> index 355100789a7..87a106c9b82 100644
> --- a/grub-core/osdep/windows/hostdisk.c
> +++ b/grub-core/osdep/windows/hostdisk.c
> @@ -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)
>  {
> diff --git a/include/grub/osdep/hostfile_aros.h b/include/grub/osdep/hostfile_aros.h
> index a059c0fa40a..161fbb7bdfd 100644
> --- a/include/grub/osdep/hostfile_aros.h
> +++ b/include/grub/osdep/hostfile_aros.h
> @@ -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
> diff --git a/include/grub/osdep/hostfile_unix.h b/include/grub/osdep/hostfile_unix.h
> index 9ffe46fa3ca..17cd3aa8b30 100644
> --- a/include/grub/osdep/hostfile_unix.h
> +++ b/include/grub/osdep/hostfile_unix.h
> @@ -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__)
> diff --git a/include/grub/osdep/hostfile_windows.h b/include/grub/osdep/hostfile_windows.h
> index bf6451b6db4..8c92d0591bb 100644
> --- a/include/grub/osdep/hostfile_windows.h
> +++ b/include/grub/osdep/hostfile_windows.h
> @@ -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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Make editenv chase symlinks including those across devices.
  2019-10-18 12:42 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Javier Martinez Canillas
@ 2019-10-23  9:42   ` Daniel Kiper
  2019-10-28 13:09     ` Javier Martinez Canillas
  0 siblings, 1 reply; 8+ messages in thread
From: Daniel Kiper @ 2019-10-23  9:42 UTC (permalink / raw)
  To: Javier Martinez Canillas; +Cc: grub-devel, Peter Jones, Adam Jackson

On Fri, Oct 18, 2019 at 02:42:21PM +0200, Javier Martinez Canillas wrote:
> From: Peter Jones <pjones@redhat.com>
>
> This lets us make /boot/grub2/grubenv a symlink to

Who or what creates that link?

> /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).
>
> Signed-off-by: Peter Jones <pjones@redhat.com>
> Reviewed-by: Adam Jackson <ajax@redhat.com>
> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
> ---
>
>  Makefile.util.def | 11 +++++++++++
>  util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
>  2 files changed, 55 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile.util.def b/Makefile.util.def
> index 969d32f0097..733a397cb2b 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -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)';

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

>    ldadd = libgrubmods.a;
>    ldadd = libgrubgcry.a;
>    ldadd = libgrubkern.a;
> diff --git a/util/editenv.c b/util/editenv.c
> index eb2d0c03a98..e61dc1283a4 100644
> --- a/util/editenv.c
> +++ b/util/editenv.c
> @@ -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);
>
> -  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.

> +  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.

> +
> +      linkbuf = xmalloc(size+1);

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

> +      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);
> +	  free (namenew);
> +	  return;
> +	}
> +      else if (retsize == size)
> +	{
> +	  free(linkbuf);

free (linkbuf);

> +	  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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Add grub_util_readlink()
  2019-10-23  9:19 ` [PATCH 1/2] Add grub_util_readlink() Daniel Kiper
@ 2019-10-28 13:04   ` Javier Martinez Canillas
  0 siblings, 0 replies; 8+ messages in thread
From: Javier Martinez Canillas @ 2019-10-28 13:04 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: grub-devel, Peter Jones, Adam Jackson

Hello Daniel,

On 10/23/19 11:19 AM, Daniel Kiper wrote:
> On Fri, Oct 18, 2019 at 02:42:20PM +0200, Javier Martinez Canillas wrote:
>> From: Peter Jones <pjones@redhat.com>
>>
>> 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,
-- 
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Make editenv chase symlinks including those across devices.
  2019-10-23  9:42   ` Daniel Kiper
@ 2019-10-28 13:09     ` Javier Martinez Canillas
  0 siblings, 0 replies; 8+ messages in thread
From: Javier Martinez Canillas @ 2019-10-28 13:09 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: grub-devel, Peter Jones, Adam Jackson

On 10/23/19 11:42 AM, Daniel Kiper wrote:
> On Fri, Oct 18, 2019 at 02:42:21PM +0200, Javier Martinez Canillas wrote:
>> From: Peter Jones <pjones@redhat.com>
>>
>> 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.

>> /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).
>>
>> Signed-off-by: Peter Jones <pjones@redhat.com>
>> Reviewed-by: Adam Jackson <ajax@redhat.com>
>> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
>> ---
>>
>>  Makefile.util.def | 11 +++++++++++
>>  util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
>>  2 files changed, 55 insertions(+), 2 deletions(-)
>>
>> diff --git a/Makefile.util.def b/Makefile.util.def
>> index 969d32f0097..733a397cb2b 100644
>> --- a/Makefile.util.def
>> +++ b/Makefile.util.def
>> @@ -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)';
> 
> 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.

>>    ldadd = libgrubmods.a;
>>    ldadd = libgrubgcry.a;
>>    ldadd = libgrubkern.a;
>> diff --git a/util/editenv.c b/util/editenv.c
>> index eb2d0c03a98..e61dc1283a4 100644
>> --- a/util/editenv.c
>> +++ b/util/editenv.c
>> @@ -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);
>>
>> -  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.

>> +  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.

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

Ok.

>> +      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);
>> +	  free (namenew);
>> +	  return;
>> +	}
>> +      else if (retsize == size)
>> +	{
>> +	  free(linkbuf);
> 
> free (linkbuf);
>

Ok.

>> +	  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.
>

Ok.

> Daniel
> 

Best regards,
-- 
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Make editenv chase symlinks including those across devices.
  2014-09-04 15:36 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Peter Jones
@ 2014-09-21 16:15   ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 0 replies; 8+ messages in thread
From: Vladimir 'φ-coder/phcoder' Serbinenko @ 2014-09-21 16:15 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 3148 bytes --]

On 04.09.2014 17:36, Peter Jones wrote:
> 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).
> 
> Signed-off-by: Peter Jones <pjones@redhat.com>
> Reviewed-by: Adam Jackson <ajax@redhat.com>
> ---
>  Makefile.util.def |  9 +++++++++
>  util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
>  2 files changed, 53 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile.util.def b/Makefile.util.def
> index 8f40e78..87029a1 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -228,8 +228,17 @@ 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 = grub-core/osdep/config.c;
> +  common = util/config.c;
> +  common = util/resolve.c;
>  
> +  ldadd = '$(LIBLZMA)';
>    ldadd = libgrubmods.a;
>    ldadd = libgrubgcry.a;
>    ldadd = libgrubkern.a;
> diff --git a/util/editenv.c b/util/editenv.c
> index c6f8d22..d8d1dad 100644
> --- a/util/editenv.c
> +++ b/util/editenv.c
> @@ -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);
>  
> @@ -59,7 +60,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;
> +  while (1)
> +    {
> +      char *linkbuf;
> +      ssize_t retsize;
> +
> +      linkbuf = xmalloc(size+1);
> +      retsize = grub_util_readlink (rename_target, linkbuf, size);

If I get this code correctly you essentially canonicalize the link
manually. Why not use already available canonicalization functions?
> +      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);
> +	  free (namenew);
> +	  return;
> +	}
> +      else if (retsize == size)
> +	{
> +	  free(linkbuf);
> +	  size += 128;
> +	  continue;
> +	}
> +
> +      free (rename_target);
> +      linkbuf[retsize] = '\0';
> +      rename_target = linkbuf;
> +    }
> +
> +  int rc = grub_util_rename (namenew, rename_target);
> +  if (rc < 0 && errno == EXDEV)
> +    {
> +      rc = grub_install_copy_file (namenew, rename_target, 1);
> +      grub_util_unlink (namenew);
> +    }
> +
> +  if (rc < 0)
> +    grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
> +
>    free (namenew);
> +  free (rename_target);
>  }
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 213 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/2] Make editenv chase symlinks including those across devices.
  2014-09-04 15:36 [PATCH 0/2] Allow editenv to follow symlinks to find/make grubenv Peter Jones
@ 2014-09-04 15:36 ` Peter Jones
  2014-09-21 16:15   ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Jones @ 2014-09-04 15:36 UTC (permalink / raw)
  To: grub-devel

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).

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
---
 Makefile.util.def |  9 +++++++++
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index 8f40e78..87029a1 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -228,8 +228,17 @@ 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 = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
+  ldadd = '$(LIBLZMA)';
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
diff --git a/util/editenv.c b/util/editenv.c
index c6f8d22..d8d1dad 100644
--- a/util/editenv.c
+++ b/util/editenv.c
@@ -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);
 
@@ -59,7 +60,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;
+  while (1)
+    {
+      char *linkbuf;
+      ssize_t retsize;
+
+      linkbuf = xmalloc(size+1);
+      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);
+	  free (namenew);
+	  return;
+	}
+      else if (retsize == size)
+	{
+	  free(linkbuf);
+	  size += 128;
+	  continue;
+	}
+
+      free (rename_target);
+      linkbuf[retsize] = '\0';
+      rename_target = linkbuf;
+    }
+
+  int rc = grub_util_rename (namenew, rename_target);
+  if (rc < 0 && errno == EXDEV)
+    {
+      rc = grub_install_copy_file (namenew, rename_target, 1);
+      grub_util_unlink (namenew);
+    }
+
+  if (rc < 0)
+    grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
+
   free (namenew);
+  free (rename_target);
 }
-- 
1.9.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-10-28 13:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-18 12:42 [PATCH 1/2] Add grub_util_readlink() Javier Martinez Canillas
2019-10-18 12:42 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Javier Martinez Canillas
2019-10-23  9:42   ` Daniel Kiper
2019-10-28 13:09     ` Javier Martinez Canillas
2019-10-23  9:19 ` [PATCH 1/2] Add grub_util_readlink() Daniel Kiper
2019-10-28 13:04   ` Javier Martinez Canillas
  -- strict thread matches above, loose matches on Subject: below --
2014-09-04 15:36 [PATCH 0/2] Allow editenv to follow symlinks to find/make grubenv Peter Jones
2014-09-04 15:36 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Peter Jones
2014-09-21 16:15   ` Vladimir 'φ-coder/phcoder' Serbinenko

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.