6b2dd0f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2ae5c1e
From: Peter Jones <pjones@redhat.com>
b9efc54
Date: Wed, 26 Feb 2014 21:49:12 -0500
31cddd6
Subject: [PATCH] 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 +++++++--
0ac23e2
 grub-core/kern/emu/main.c            |  2 +-
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 +-
6f1e3d5
 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 +-
6f1e3d5
 14 files changed, 39 insertions(+), 21 deletions(-)
f4c76c0
f4c76c0
diff --git a/grub-core/commands/minicmd.c b/grub-core/commands/minicmd.c
ec4acbb
index a3a11824172..b25ca4b9f17 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
752ceb1
index 708581fcbde..e339f264b3a 100644
f4c76c0
--- a/grub-core/kern/efi/efi.c
f4c76c0
+++ b/grub-core/kern/efi/efi.c
752ceb1
@@ -164,11 +164,16 @@ grub_reboot (void)
f4c76c0
 }
f4c76c0
 
f4c76c0
 void
f4c76c0
-grub_exit (void)
f4c76c0
+grub_exit (int retval)
f4c76c0
 {
6f1e3d5
+  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,
b9efc54
-              grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
f4c76c0
+              grub_efi_image_handle, rc, 0, 0);
f4c76c0
   for (;;) ;
f4c76c0
 }
f4c76c0
 
0ac23e2
diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c
ec4acbb
index 425bb960347..55ea5a11ccd 100644
0ac23e2
--- a/grub-core/kern/emu/main.c
0ac23e2
+++ b/grub-core/kern/emu/main.c
bc092b9
@@ -67,7 +67,7 @@ grub_reboot (void)
0ac23e2
 }
0ac23e2
 
0ac23e2
 void
0ac23e2
-grub_exit (void)
0ac23e2
+grub_exit (int retval __attribute__((unused)))
0ac23e2
 {
0ac23e2
   grub_reboot ();
0ac23e2
 }
f4c76c0
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
ec4acbb
index 76661337f10..82012a72fcb 100644
f4c76c0
--- a/grub-core/kern/emu/misc.c
f4c76c0
+++ b/grub-core/kern/emu/misc.c
bc092b9
@@ -137,9 +137,10 @@ xasprintf (const char *fmt, ...)
f4c76c0
 
b9efc54
 #if !defined (GRUB_MACHINE_EMU) || defined (GRUB_UTIL)
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
 }
b9efc54
 #endif
f4c76c0
 
f4c76c0
diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/coreboot/init.c
ec4acbb
index 3314f027fec..36f9134b7b7 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
ec4acbb
index 271b6fbfabd..9fafe98f015 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
752ceb1
index 0d8ebf58b95..f5423ce27d9 100644
f4c76c0
--- a/grub-core/kern/ieee1275/init.c
f4c76c0
+++ b/grub-core/kern/ieee1275/init.c
b9efc54
@@ -68,7 +68,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
ec4acbb
index 3834a149093..86b3a25ec40 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
ec4acbb
index 7b96531b983..dff598ca7b0 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
ec4acbb
index be88b77d22d..8b6c55ffc01 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
ec4acbb
index 3b633d51f4c..952411d5dc6 100644
f4c76c0
--- a/grub-core/kern/misc.c
f4c76c0
+++ b/grub-core/kern/misc.c
6f1e3d5
@@ -1095,7 +1095,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
ec4acbb
index 3e338645c57..be2a5be1d07 100644
f4c76c0
--- a/grub-core/kern/uboot/init.c
f4c76c0
+++ b/grub-core/kern/uboot/init.c
bc092b9
@@ -39,9 +39,9 @@ extern grub_size_t grub_total_module_size;
bc092b9
 static unsigned long timer_start;
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
 
bc092b9
 static grub_uint64_t
bc092b9
@@ -78,7 +78,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
ec4acbb
index 0559c033c3d..fce526d417b 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
752ceb1
index 372f009e84f..83fd69f4ada 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);