Skip to content

Commit

Permalink
Make "exit" take a return code.
Browse files Browse the repository at this point in the history
This adds "exit" with a return code.  With this patch, any "exit"
command /may/ include a return code, and on platforms that support
returning with an exit status, we will do so.  By default we return the
same exit status we did before this patch.

Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela authored and martinezjavier committed Aug 18, 2020
1 parent 92cb86b commit ccce3d6
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 21 deletions.
20 changes: 16 additions & 4 deletions grub-core/commands/minicmd.c
Expand Up @@ -179,12 +179,24 @@ grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
}

/* exit */
static grub_err_t __attribute__ ((noreturn))
static grub_err_t
grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused)))
int argc, char *argv[])
{
grub_exit ();
int retval = -1;
unsigned long n;

if (argc < 0 || argc > 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));

if (argc == 1)
{
n = grub_strtoul (argv[0], 0, 10);
if (n != ~0UL)
retval = n;
}

grub_exit (retval);
/* Not reached. */
}

Expand Down
9 changes: 7 additions & 2 deletions grub-core/kern/efi/efi.c
Expand Up @@ -164,11 +164,16 @@ grub_reboot (void)
}

void
grub_exit (void)
grub_exit (int retval)
{
int rc = GRUB_EFI_LOAD_ERROR;

if (retval == 0)
rc = GRUB_EFI_SUCCESS;

grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
efi_call_4 (grub_efi_system_table->boot_services->exit,
grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
grub_efi_image_handle, rc, 0, 0);
for (;;) ;
}

Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/emu/main.c
Expand Up @@ -67,7 +67,7 @@ grub_reboot (void)
}

void
grub_exit (void)
grub_exit (int retval __attribute__((unused)))
{
grub_reboot ();
}
Expand Down
5 changes: 3 additions & 2 deletions grub-core/kern/emu/misc.c
Expand Up @@ -139,9 +139,10 @@ xasprintf (const char *fmt, ...)

#if !defined (GRUB_MACHINE_EMU) || defined (GRUB_UTIL)
void
grub_exit (void)
__attribute__ ((noreturn))
grub_exit (int rc)
{
exit (1);
exit (rc < 0 ? 1 : rc);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/i386/coreboot/init.c
Expand Up @@ -41,7 +41,7 @@ extern grub_uint8_t _end[];
extern grub_uint8_t _edata[];

void __attribute__ ((noreturn))
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
/* We can't use grub_fatal() in this function. This would create an infinite
loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/i386/qemu/init.c
Expand Up @@ -42,7 +42,7 @@ extern grub_uint8_t _end[];
extern grub_uint8_t _edata[];

void __attribute__ ((noreturn))
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
/* We can't use grub_fatal() in this function. This would create an infinite
loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/ieee1275/init.c
Expand Up @@ -71,7 +71,7 @@ grub_addr_t grub_ieee1275_original_stack;
#endif

void
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
grub_ieee1275_exit ();
}
Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/mips/arc/init.c
Expand Up @@ -276,7 +276,7 @@ grub_halt (void)
}

void
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
GRUB_ARC_FIRMWARE_VECTOR->exit ();

Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/mips/loongson/init.c
Expand Up @@ -304,7 +304,7 @@ grub_halt (void)
}

void
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
grub_halt ();
}
Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/mips/qemu_mips/init.c
Expand Up @@ -75,7 +75,7 @@ grub_machine_fini (int flags __attribute__ ((unused)))
}

void
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
grub_halt ();
}
Expand Down
11 changes: 10 additions & 1 deletion grub-core/kern/misc.c
Expand Up @@ -1095,9 +1095,18 @@ grub_abort (void)
grub_getkey ();
}

grub_exit ();
grub_exit (1);
}

#if defined (__clang__) && !defined (GRUB_UTIL)
/* clang emits references to abort(). */
void __attribute__ ((noreturn))
abort (void)
{
grub_abort ();
}
#endif

void
grub_fatal (const char *fmt, ...)
{
Expand Down
6 changes: 3 additions & 3 deletions grub-core/kern/uboot/init.c
Expand Up @@ -39,9 +39,9 @@ extern grub_size_t grub_total_module_size;
static unsigned long timer_start;

void
grub_exit (void)
grub_exit (int rc)
{
grub_uboot_return (0);
grub_uboot_return (rc < 0 ? 1 : rc);
}

static grub_uint64_t
Expand Down Expand Up @@ -78,7 +78,7 @@ grub_machine_init (void)
if (!ver)
{
/* Don't even have a console to log errors to... */
grub_exit ();
grub_exit (-1);
}
else if (ver > API_SIG_VERSION)
{
Expand Down
2 changes: 1 addition & 1 deletion grub-core/kern/xen/init.c
Expand Up @@ -584,7 +584,7 @@ grub_machine_init (void)
}

void
grub_exit (void)
grub_exit (int rc __attribute__((unused)))
{
struct sched_shutdown arg;

Expand Down
2 changes: 1 addition & 1 deletion include/grub/misc.h
Expand Up @@ -334,7 +334,7 @@ int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
__attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
void EXPORT_FUNC(grub_exit) (int rc) __attribute__ ((noreturn));
grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
grub_uint64_t d,
grub_uint64_t *r);
Expand Down

0 comments on commit ccce3d6

Please sign in to comment.