ea2871d
From 05c9156ed3cee62e6aa8f955b96998b5d9302425 Mon Sep 17 00:00:00 2001
f4c76c0
From: Peter Jones <pjones@redhat.com>
f4c76c0
Date: Tue, 7 Jul 2015 12:00:26 -0400
f4c76c0
Subject: [PATCH 71/74] Make "exit" take a return code.
f4c76c0
f4c76c0
This adds "exit" with a return code.  With this patch, any "exit"
f4c76c0
command /may/ include a return code, and on platforms that support
f4c76c0
returning with an exit status, we will do so.  By default we return the
f4c76c0
same exit status we did before this patch.
f4c76c0
f4c76c0
Signed-off-by: Peter Jones <pjones@redhat.com>
f4c76c0
---
f4c76c0
 grub-core/commands/minicmd.c         | 20 ++++++++++++++++----
f4c76c0
 grub-core/kern/efi/efi.c             |  9 +++++++--
f4c76c0
 grub-core/kern/emu/misc.c            |  5 +++--
f4c76c0
 grub-core/kern/i386/coreboot/init.c  |  2 +-
f4c76c0
 grub-core/kern/i386/qemu/init.c      |  2 +-
f4c76c0
 grub-core/kern/ieee1275/init.c       |  2 +-
f4c76c0
 grub-core/kern/mips/arc/init.c       |  2 +-
f4c76c0
 grub-core/kern/mips/loongson/init.c  |  2 +-
f4c76c0
 grub-core/kern/mips/qemu_mips/init.c |  2 +-
f4c76c0
 grub-core/kern/misc.c                |  2 +-
f4c76c0
 grub-core/kern/uboot/init.c          |  6 +++---
f4c76c0
 grub-core/kern/xen/init.c            |  2 +-
f4c76c0
 include/grub/misc.h                  |  2 +-
f4c76c0
 13 files changed, 38 insertions(+), 20 deletions(-)
f4c76c0
f4c76c0
diff --git a/grub-core/commands/minicmd.c b/grub-core/commands/minicmd.c
f4c76c0
index a3a1182..b25ca4b 100644
f4c76c0
--- a/grub-core/commands/minicmd.c
f4c76c0
+++ b/grub-core/commands/minicmd.c
f4c76c0
@@ -176,12 +176,24 @@ grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
f4c76c0
 }
f4c76c0
 
f4c76c0
 /* exit */
f4c76c0
-static grub_err_t __attribute__ ((noreturn))
f4c76c0
+static grub_err_t
f4c76c0
 grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
f4c76c0
-		    int argc __attribute__ ((unused)),
f4c76c0
-		    char *argv[] __attribute__ ((unused)))
f4c76c0
+		    int argc, char *argv[])
f4c76c0
 {
f4c76c0
-  grub_exit ();
f4c76c0
+  int retval = -1;
f4c76c0
+  unsigned long n;
f4c76c0
+
f4c76c0
+  if (argc < 0 || argc > 1)
f4c76c0
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
f4c76c0
+
f4c76c0
+  if (argc == 1)
f4c76c0
+    {
f4c76c0
+      n = grub_strtoul (argv[0], 0, 10);
f4c76c0
+      if (n != ~0UL)
f4c76c0
+	retval = n;
f4c76c0
+    }
f4c76c0
+
f4c76c0
+  grub_exit (retval);
f4c76c0
   /* Not reached.  */
f4c76c0
 }
f4c76c0
 
f4c76c0
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
f4c76c0
index c80d85b..95c75d4 100644
f4c76c0
--- a/grub-core/kern/efi/efi.c
f4c76c0
+++ b/grub-core/kern/efi/efi.c
f4c76c0
@@ -155,11 +155,16 @@ grub_efi_get_loaded_image (grub_efi_handle_t image_handle)
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int retval)
f4c76c0
 {
f4c76c0
+  int rc = GRUB_EFI_LOAD_ERROR;
f4c76c0
+
f4c76c0
+  if (retval == 0)
f4c76c0
+    rc = GRUB_EFI_SUCCESS;
f4c76c0
+
f4c76c0
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
f4c76c0
   efi_call_4 (grub_efi_system_table->boot_services->exit,
f4c76c0
-              grub_efi_image_handle, GRUB_EFI_LOAD_ERROR, 0, 0);
f4c76c0
+              grub_efi_image_handle, rc, 0, 0);
f4c76c0
   for (;;) ;
f4c76c0
 }
f4c76c0
 
f4c76c0
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
f4c76c0
index bb606da..258a564 100644
f4c76c0
--- a/grub-core/kern/emu/misc.c
f4c76c0
+++ b/grub-core/kern/emu/misc.c
f4c76c0
@@ -135,9 +135,10 @@ xasprintf (const char *fmt, ...)
f4c76c0
 #endif
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+__attribute__ ((noreturn))
f4c76c0
+grub_exit (int rc)
f4c76c0
 {
f4c76c0
-  exit (1);
f4c76c0
+  exit (rc < 0 ? 1 : rc);
f4c76c0
 }
f4c76c0
 
f4c76c0
 grub_uint64_t
f4c76c0
diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/coreboot/init.c
f4c76c0
index 3314f02..36f9134 100644
f4c76c0
--- a/grub-core/kern/i386/coreboot/init.c
f4c76c0
+++ b/grub-core/kern/i386/coreboot/init.c
f4c76c0
@@ -41,7 +41,7 @@ extern grub_uint8_t _end[];
f4c76c0
 extern grub_uint8_t _edata[];
f4c76c0
 
f4c76c0
 void  __attribute__ ((noreturn))
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   /* We can't use grub_fatal() in this function.  This would create an infinite
f4c76c0
      loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit().  */
f4c76c0
diff --git a/grub-core/kern/i386/qemu/init.c b/grub-core/kern/i386/qemu/init.c
f4c76c0
index 271b6fb..9fafe98 100644
f4c76c0
--- a/grub-core/kern/i386/qemu/init.c
f4c76c0
+++ b/grub-core/kern/i386/qemu/init.c
f4c76c0
@@ -42,7 +42,7 @@ extern grub_uint8_t _end[];
f4c76c0
 extern grub_uint8_t _edata[];
f4c76c0
 
f4c76c0
 void  __attribute__ ((noreturn))
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   /* We can't use grub_fatal() in this function.  This would create an infinite
f4c76c0
      loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit().  */
f4c76c0
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
f4c76c0
index 8ca4bf7..e2540bc 100644
f4c76c0
--- a/grub-core/kern/ieee1275/init.c
f4c76c0
+++ b/grub-core/kern/ieee1275/init.c
f4c76c0
@@ -60,7 +60,7 @@ grub_addr_t grub_ieee1275_original_stack;
f4c76c0
 #endif
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   grub_ieee1275_exit ();
f4c76c0
 }
f4c76c0
diff --git a/grub-core/kern/mips/arc/init.c b/grub-core/kern/mips/arc/init.c
f4c76c0
index 3834a14..86b3a25 100644
f4c76c0
--- a/grub-core/kern/mips/arc/init.c
f4c76c0
+++ b/grub-core/kern/mips/arc/init.c
f4c76c0
@@ -276,7 +276,7 @@ grub_halt (void)
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   GRUB_ARC_FIRMWARE_VECTOR->exit ();
f4c76c0
 
f4c76c0
diff --git a/grub-core/kern/mips/loongson/init.c b/grub-core/kern/mips/loongson/init.c
f4c76c0
index 7b96531..dff598c 100644
f4c76c0
--- a/grub-core/kern/mips/loongson/init.c
f4c76c0
+++ b/grub-core/kern/mips/loongson/init.c
f4c76c0
@@ -304,7 +304,7 @@ grub_halt (void)
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   grub_halt ();
f4c76c0
 }
f4c76c0
diff --git a/grub-core/kern/mips/qemu_mips/init.c b/grub-core/kern/mips/qemu_mips/init.c
f4c76c0
index be88b77..8b6c55f 100644
f4c76c0
--- a/grub-core/kern/mips/qemu_mips/init.c
f4c76c0
+++ b/grub-core/kern/mips/qemu_mips/init.c
f4c76c0
@@ -75,7 +75,7 @@ grub_machine_fini (int flags __attribute__ ((unused)))
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   grub_halt ();
f4c76c0
 }
f4c76c0
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
f4c76c0
index 6b3397f..27d4b81 100644
f4c76c0
--- a/grub-core/kern/misc.c
f4c76c0
+++ b/grub-core/kern/misc.c
f4c76c0
@@ -1090,7 +1090,7 @@ grub_abort (void)
f4c76c0
       grub_getkey ();
f4c76c0
     }
f4c76c0
 
f4c76c0
-  grub_exit ();
f4c76c0
+  grub_exit (1);
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
diff --git a/grub-core/kern/uboot/init.c b/grub-core/kern/uboot/init.c
f4c76c0
index 5dcc106..430c62b 100644
f4c76c0
--- a/grub-core/kern/uboot/init.c
f4c76c0
+++ b/grub-core/kern/uboot/init.c
f4c76c0
@@ -43,9 +43,9 @@ extern grub_uint32_t grub_uboot_machine_type;
f4c76c0
 extern grub_addr_t grub_uboot_boot_data;
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc)
f4c76c0
 {
f4c76c0
-  grub_uboot_return (0);
f4c76c0
+  grub_uboot_return (rc < 0 ? 1 : rc);
f4c76c0
 }
f4c76c0
 
f4c76c0
 grub_uint32_t
f4c76c0
@@ -94,7 +94,7 @@ grub_machine_init (void)
f4c76c0
   if (!ver)
f4c76c0
     {
f4c76c0
       /* Don't even have a console to log errors to... */
f4c76c0
-      grub_exit ();
f4c76c0
+      grub_exit (-1);
f4c76c0
     }
f4c76c0
   else if (ver > API_SIG_VERSION)
f4c76c0
     {
f4c76c0
diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
f4c76c0
index 0559c03..fce526d 100644
f4c76c0
--- a/grub-core/kern/xen/init.c
f4c76c0
+++ b/grub-core/kern/xen/init.c
f4c76c0
@@ -549,7 +549,7 @@ grub_machine_init (void)
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int rc __attribute__((unused)))
f4c76c0
 {
f4c76c0
   struct sched_shutdown arg;
f4c76c0
 
f4c76c0
diff --git a/include/grub/misc.h b/include/grub/misc.h
f4c76c0
index 2a9f87c..0620814 100644
f4c76c0
--- a/include/grub/misc.h
f4c76c0
+++ b/include/grub/misc.h
f4c76c0
@@ -334,7 +334,7 @@ int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
f4c76c0
 char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
f4c76c0
      __attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
f4c76c0
 char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
f4c76c0
-void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
f4c76c0
+void EXPORT_FUNC(grub_exit) (int rc) __attribute__ ((noreturn));
f4c76c0
 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
f4c76c0
 					  grub_uint64_t d,
f4c76c0
 					  grub_uint64_t *r);
f4c76c0
-- 
f4c76c0
2.4.3
f4c76c0