grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] grub-fstest: Show error message if command causes grub_errno


From: Thomas Schmitt
Subject: [PATCH 1/2] grub-fstest: Show error message if command causes grub_errno
Date: Thu, 20 Jun 2024 22:05:03 +0200

Check grub_errno after a command was performed. If not GRUB_ERR_NONE then
print grub_errno and grub_errmsg. Count the errors and exit 1 with a
message if the count is larger than zero when function fs_test() ends.
The number of printed messages is restricted to 3.

Introduce a new option -E which re-enables the old behavior which did
not show grub_errmsg and did not exit with non-zero value if grub_errno
was found not being GRUB_ERR_NONE.

Signed-off-by: Thomas Schmitt <scdbackup@gmx.net>
---
 util/grub-fstest.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/util/grub-fstest.c b/util/grub-fstest.c
index 7ff9037b8..145b40a0e 100644
--- a/util/grub-fstest.c
+++ b/util/grub-fstest.c
@@ -47,16 +47,33 @@
 #pragma GCC diagnostic error "-Wmissing-prototypes"
 #pragma GCC diagnostic error "-Wmissing-declarations"

+static int execute_error_count = 0;
+static int no_cmd_error = 0;
+
 static grub_err_t
 execute_command (const char *name, int n, char **args)
 {
   grub_command_t cmd;
+  grub_err_t ret;

   cmd = grub_command_find (name);
   if (! cmd)
     grub_util_error (_("can't find command `%s'"), name);

-  return (cmd->func) (cmd, n, args);
+  ret = (cmd->func) (cmd, n, args);
+  if (grub_errno != GRUB_ERR_NONE && !no_cmd_error)
+    {
+      execute_error_count++;
+      if (execute_error_count <= 3)
+       grub_err_printf (_("%s : GRUB error %d with message '%s'\n"),
+                        name, (int) grub_errno, grub_errmsg);
+      else if (execute_error_count == 4)
+       grub_err_printf (_("%s : more GRUB errors encountered but not shown\n"),
+                        name);
+      grub_errno = GRUB_ERR_NONE;
+      grub_errmsg[0] = 0;
+    }
+  return ret;
 }

 enum {
@@ -510,6 +527,12 @@ fstest (int n)
       grub_free (loop_name);
       grub_free (argv[0]);
     }
+
+  if (execute_error_count == 1)
+    grub_util_error (_("encountered 1 error during command execution"));
+  else if (execute_error_count > 0)
+    grub_util_error (_("encountered %d errors during command execution"),
+                    execute_error_count);
 }

 static struct argp_option options[] = {
@@ -535,6 +558,7 @@ static struct argp_option options[] = {
    N_("FILE|prompt"), 0, N_("Load zfs crypto key."),                 2},
   {"verbose",   'v', NULL, 0, N_("print verbose messages."), 2},
   {"uncompress", 'u', NULL, 0, N_("Uncompress data."), 2},
+  {"no-cmd-error", 'E', NULL, 0, N_("Ignore errors of command execution."), 2},
   {0, 0, 0, 0, 0, 0}
 };

@@ -638,6 +662,10 @@ argp_parser (int key, char *arg, struct argp_state *state)
       uncompress = 1;
       return 0;

+    case 'E':
+      no_cmd_error = 1;
+      return 0;
+
     case ARGP_KEY_END:
       if (args_count < num_disks)
        {
--
2.39.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]