Blame SOURCES/0016-Make-exit-take-a-return-code.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Peter Jones <pjones@redhat.com>
5593c8
Date: Wed, 26 Feb 2014 21:49:12 -0500
5593c8
Subject: [PATCH] Make "exit" take a return code.
5593c8
5593c8
This adds "exit" with a return code.  With this patch, any "exit"
5593c8
command /may/ include a return code, and on platforms that support
5593c8
returning with an exit status, we will do so.  By default we return the
5593c8
same exit status we did before this patch.
5593c8
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
---
5593c8
 grub-core/commands/minicmd.c         | 20 ++++++++++++++++----
5593c8
 grub-core/kern/efi/efi.c             |  9 +++++++--
5593c8
 grub-core/kern/emu/main.c            |  2 +-
5593c8
 grub-core/kern/emu/misc.c            |  5 +++--
5593c8
 grub-core/kern/i386/coreboot/init.c  |  2 +-
5593c8
 grub-core/kern/i386/qemu/init.c      |  2 +-
5593c8
 grub-core/kern/ieee1275/init.c       |  2 +-
5593c8
 grub-core/kern/mips/arc/init.c       |  2 +-
5593c8
 grub-core/kern/mips/loongson/init.c  |  2 +-
5593c8
 grub-core/kern/mips/qemu_mips/init.c |  2 +-
5593c8
 grub-core/kern/misc.c                | 11 ++++++++++-
5593c8
 grub-core/kern/uboot/init.c          |  6 +++---
5593c8
 grub-core/kern/xen/init.c            |  2 +-
5593c8
 include/grub/misc.h                  |  2 +-
5593c8
 14 files changed, 48 insertions(+), 21 deletions(-)
5593c8
5593c8
diff --git a/grub-core/commands/minicmd.c b/grub-core/commands/minicmd.c
5593c8
index fa498931ed2..2bd3ac76f2d 100644
5593c8
--- a/grub-core/commands/minicmd.c
5593c8
+++ b/grub-core/commands/minicmd.c
5593c8
@@ -182,12 +182,24 @@ grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
5593c8
 }
5593c8
 
5593c8
 /* exit */
5593c8
-static grub_err_t __attribute__ ((noreturn))
5593c8
+static grub_err_t
5593c8
 grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
5593c8
-		    int argc __attribute__ ((unused)),
5593c8
-		    char *argv[] __attribute__ ((unused)))
5593c8
+		    int argc, char *argv[])
5593c8
 {
5593c8
-  grub_exit ();
5593c8
+  int retval = -1;
5593c8
+  unsigned long n;
5593c8
+
5593c8
+  if (argc < 0 || argc > 1)
5593c8
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
5593c8
+
5593c8
+  if (argc == 1)
5593c8
+    {
5593c8
+      n = grub_strtoul (argv[0], 0, 10);
5593c8
+      if (n != ~0UL)
5593c8
+	retval = n;
5593c8
+    }
5593c8
+
5593c8
+  grub_exit (retval);
5593c8
   /* Not reached.  */
5593c8
 }
5593c8
 
5593c8
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
5593c8
index 8cff7be0289..05d8237a9b2 100644
5593c8
--- a/grub-core/kern/efi/efi.c
5593c8
+++ b/grub-core/kern/efi/efi.c
5593c8
@@ -165,11 +165,16 @@ grub_reboot (void)
5593c8
 }
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int retval)
5593c8
 {
5593c8
+  int rc = GRUB_EFI_LOAD_ERROR;
5593c8
+
5593c8
+  if (retval == 0)
5593c8
+    rc = GRUB_EFI_SUCCESS;
5593c8
+
5593c8
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
5593c8
   efi_call_4 (grub_efi_system_table->boot_services->exit,
5593c8
-              grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
5593c8
+              grub_efi_image_handle, rc, 0, 0);
5593c8
   for (;;) ;
5593c8
 }
5593c8
 
5593c8
diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c
5593c8
index 425bb960347..55ea5a11ccd 100644
5593c8
--- a/grub-core/kern/emu/main.c
5593c8
+++ b/grub-core/kern/emu/main.c
5593c8
@@ -67,7 +67,7 @@ grub_reboot (void)
5593c8
 }
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int retval __attribute__((unused)))
5593c8
 {
5593c8
   grub_reboot ();
5593c8
 }
5593c8
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
5593c8
index dfd8a8ec488..0ff13bcaf8c 100644
5593c8
--- a/grub-core/kern/emu/misc.c
5593c8
+++ b/grub-core/kern/emu/misc.c
5593c8
@@ -151,9 +151,10 @@ xasprintf (const char *fmt, ...)
5593c8
 
5593c8
 #if !defined (GRUB_MACHINE_EMU) || defined (GRUB_UTIL)
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+__attribute__ ((noreturn))
5593c8
+grub_exit (int rc)
5593c8
 {
5593c8
-  exit (1);
5593c8
+  exit (rc < 0 ? 1 : rc);
5593c8
 }
5593c8
 #endif
5593c8
 
5593c8
diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/coreboot/init.c
5593c8
index 3314f027fec..36f9134b7b7 100644
5593c8
--- a/grub-core/kern/i386/coreboot/init.c
5593c8
+++ b/grub-core/kern/i386/coreboot/init.c
5593c8
@@ -41,7 +41,7 @@ extern grub_uint8_t _end[];
5593c8
 extern grub_uint8_t _edata[];
5593c8
 
5593c8
 void  __attribute__ ((noreturn))
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   /* We can't use grub_fatal() in this function.  This would create an infinite
5593c8
      loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit().  */
5593c8
diff --git a/grub-core/kern/i386/qemu/init.c b/grub-core/kern/i386/qemu/init.c
5593c8
index 271b6fbfabd..9fafe98f015 100644
5593c8
--- a/grub-core/kern/i386/qemu/init.c
5593c8
+++ b/grub-core/kern/i386/qemu/init.c
5593c8
@@ -42,7 +42,7 @@ extern grub_uint8_t _end[];
5593c8
 extern grub_uint8_t _edata[];
5593c8
 
5593c8
 void  __attribute__ ((noreturn))
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   /* We can't use grub_fatal() in this function.  This would create an infinite
5593c8
      loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit().  */
5593c8
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
5593c8
index d483e35eed2..e71d1584164 100644
5593c8
--- a/grub-core/kern/ieee1275/init.c
5593c8
+++ b/grub-core/kern/ieee1275/init.c
5593c8
@@ -71,7 +71,7 @@ grub_addr_t grub_ieee1275_original_stack;
5593c8
 #endif
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   grub_ieee1275_exit ();
5593c8
 }
5593c8
diff --git a/grub-core/kern/mips/arc/init.c b/grub-core/kern/mips/arc/init.c
5593c8
index 2ed3ff3191e..5c40c34078d 100644
5593c8
--- a/grub-core/kern/mips/arc/init.c
5593c8
+++ b/grub-core/kern/mips/arc/init.c
5593c8
@@ -276,7 +276,7 @@ grub_halt (void)
5593c8
 }
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   GRUB_ARC_FIRMWARE_VECTOR->exit ();
5593c8
 
5593c8
diff --git a/grub-core/kern/mips/loongson/init.c b/grub-core/kern/mips/loongson/init.c
5593c8
index 7b96531b983..dff598ca7b0 100644
5593c8
--- a/grub-core/kern/mips/loongson/init.c
5593c8
+++ b/grub-core/kern/mips/loongson/init.c
5593c8
@@ -304,7 +304,7 @@ grub_halt (void)
5593c8
 }
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   grub_halt ();
5593c8
 }
5593c8
diff --git a/grub-core/kern/mips/qemu_mips/init.c b/grub-core/kern/mips/qemu_mips/init.c
5593c8
index be88b77d22d..8b6c55ffc01 100644
5593c8
--- a/grub-core/kern/mips/qemu_mips/init.c
5593c8
+++ b/grub-core/kern/mips/qemu_mips/init.c
5593c8
@@ -75,7 +75,7 @@ grub_machine_fini (int flags __attribute__ ((unused)))
5593c8
 }
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   grub_halt ();
5593c8
 }
5593c8
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
5593c8
index 3af336ee227..63b586d09cb 100644
5593c8
--- a/grub-core/kern/misc.c
5593c8
+++ b/grub-core/kern/misc.c
5593c8
@@ -1209,9 +1209,18 @@ grub_abort (void)
5593c8
       grub_getkey ();
5593c8
     }
5593c8
 
5593c8
-  grub_exit ();
5593c8
+  grub_exit (1);
5593c8
 }
5593c8
 
5593c8
+#if defined (__clang__) && !defined (GRUB_UTIL)
5593c8
+/* clang emits references to abort().  */
5593c8
+void __attribute__ ((noreturn))
5593c8
+abort (void)
5593c8
+{
5593c8
+  grub_abort ();
5593c8
+}
5593c8
+#endif
5593c8
+
5593c8
 void
5593c8
 grub_fatal (const char *fmt, ...)
5593c8
 {
5593c8
diff --git a/grub-core/kern/uboot/init.c b/grub-core/kern/uboot/init.c
5593c8
index 3e338645c57..be2a5be1d07 100644
5593c8
--- a/grub-core/kern/uboot/init.c
5593c8
+++ b/grub-core/kern/uboot/init.c
5593c8
@@ -39,9 +39,9 @@ extern grub_size_t grub_total_module_size;
5593c8
 static unsigned long timer_start;
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc)
5593c8
 {
5593c8
-  grub_uboot_return (0);
5593c8
+  grub_uboot_return (rc < 0 ? 1 : rc);
5593c8
 }
5593c8
 
5593c8
 static grub_uint64_t
5593c8
@@ -78,7 +78,7 @@ grub_machine_init (void)
5593c8
   if (!ver)
5593c8
     {
5593c8
       /* Don't even have a console to log errors to... */
5593c8
-      grub_exit ();
5593c8
+      grub_exit (-1);
5593c8
     }
5593c8
   else if (ver > API_SIG_VERSION)
5593c8
     {
5593c8
diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
5593c8
index 782ca72952a..708b060f324 100644
5593c8
--- a/grub-core/kern/xen/init.c
5593c8
+++ b/grub-core/kern/xen/init.c
5593c8
@@ -584,7 +584,7 @@ grub_machine_init (void)
5593c8
 }
5593c8
 
5593c8
 void
5593c8
-grub_exit (void)
5593c8
+grub_exit (int rc __attribute__((unused)))
5593c8
 {
5593c8
   struct sched_shutdown arg;
5593c8
 
5593c8
diff --git a/include/grub/misc.h b/include/grub/misc.h
5593c8
index 7d2b5519690..fd18e6320b8 100644
5593c8
--- a/include/grub/misc.h
5593c8
+++ b/include/grub/misc.h
5593c8
@@ -353,7 +353,7 @@ int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
5593c8
 char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
5593c8
      __attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
5593c8
 char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
5593c8
-void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
5593c8
+void EXPORT_FUNC(grub_exit) (int rc) __attribute__ ((noreturn));
5593c8
 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
5593c8
 					  grub_uint64_t d,
5593c8
 					  grub_uint64_t *r);