From 396311f0f606d2c3cf1ba68f6007e1ccbe465531 Mon Sep 17 00:00:00 2001 From: Andrey Borzenkov Date: Mon, 16 Dec 2013 22:41:15 +0400 Subject: [PATCH 1/4] consolidate grub_util_exec code We need to hide "modprobe efivars" error output to avoid confusion. So consolidate grub_util_exec_* into single function that can optionally redirect all three standard descriptors and make all other functions compatibility wrappers. Also remove include/grub/osdep/exec_unix.h which does not appear to be used anywhere. --- grub-core/osdep/unix/exec.c | 140 ++++++++++++++++----------------- include/grub/emu/exec.h | 3 + include/grub/osdep/exec_unix.h | 39 --------- 3 files changed, 71 insertions(+), 111 deletions(-) delete mode 100644 include/grub/osdep/exec_unix.h diff --git a/grub-core/osdep/unix/exec.c b/grub-core/osdep/unix/exec.c index d4865f617..935ff120e 100644 --- a/grub-core/osdep/unix/exec.c +++ b/grub-core/osdep/unix/exec.c @@ -34,7 +34,8 @@ #include int -grub_util_exec (const char *const *argv) +grub_util_exec_redirect_all (const char *const *argv, const char *stdin_file, + const char *stdout_file, const char *stderr_file) { pid_t pid; int status = -1; @@ -43,15 +44,39 @@ grub_util_exec (const char *const *argv) grub_size_t strl = 0; for (ptr = argv; *ptr; ptr++) strl += grub_strlen (*ptr) + 1; + if (stdin_file) + strl += grub_strlen (stdin_file) + 2; + if (stdout_file) + strl += grub_strlen (stdout_file) + 2; + if (stderr_file) + strl += grub_strlen (stderr_file) + 3; + pstr = str = xmalloc (strl); for (ptr = argv; *ptr; ptr++) { pstr = grub_stpcpy (pstr, *ptr); *pstr++ = ' '; } - if (pstr > str) - pstr--; - *pstr = '\0'; + if (stdin_file) + { + *pstr++ = '<'; + pstr = grub_stpcpy (pstr, stdin_file); + *pstr++ = ' '; + } + if (stdout_file) + { + *pstr++ = '>'; + pstr = grub_stpcpy (pstr, stdout_file); + *pstr++ = ' '; + } + if (stderr_file) + { + *pstr++ = '2'; + *pstr++ = '>'; + pstr = grub_stpcpy (pstr, stderr_file); + pstr++; + } + *--pstr = '\0'; grub_util_info ("executing %s", str); grub_free (str); @@ -61,63 +86,7 @@ grub_util_exec (const char *const *argv) grub_util_error (_("Unable to fork: %s"), strerror (errno)); else if (pid == 0) { - /* Child. */ - - /* Close fd's. */ -#ifdef GRUB_UTIL - grub_util_devmapper_cleanup (); - grub_diskfilter_fini (); -#endif - - /* Ensure child is not localised. */ - setenv ("LC_ALL", "C", 1); - - execvp ((char *) argv[0], (char **) argv); - exit (127); - } - - waitpid (pid, &status, 0); - if (!WIFEXITED (status)) - return -1; - return WEXITSTATUS (status); -} - -int -grub_util_exec_redirect (const char *const *argv, const char *stdin_file, - const char *stdout_file) -{ - pid_t pid; - int status = -1; - char *str, *pstr; - const char *const *ptr; - grub_size_t strl = 0; - for (ptr = argv; *ptr; ptr++) - strl += grub_strlen (*ptr) + 1; - strl += grub_strlen (stdin_file) + 2; - strl += grub_strlen (stdout_file) + 2; - - pstr = str = xmalloc (strl); - for (ptr = argv; *ptr; ptr++) - { - pstr = grub_stpcpy (pstr, *ptr); - *pstr++ = ' '; - } - *pstr++ = '<'; - pstr = grub_stpcpy (pstr, stdin_file); - *pstr++ = ' '; - *pstr++ = '>'; - pstr = grub_stpcpy (pstr, stdout_file); - *pstr = '\0'; - - grub_util_info ("executing %s", str); - grub_free (str); - - pid = fork (); - if (pid < 0) - grub_util_error (_("Unable to fork: %s"), strerror (errno)); - else if (pid == 0) - { - int in, out; + int fd; /* Child. */ /* Close fd's. */ @@ -126,18 +95,32 @@ grub_util_exec_redirect (const char *const *argv, const char *stdin_file, grub_diskfilter_fini (); #endif - in = open (stdin_file, O_RDONLY); - if (in < 0) - exit (127); - dup2 (in, STDIN_FILENO); - close (in); + if (stdin_file) + { + fd = open (stdin_file, O_RDONLY); + if (fd < 0) + exit (127); + dup2 (fd, STDIN_FILENO); + close (fd); + } - out = open (stdout_file, O_WRONLY | O_CREAT, 0700); - dup2 (out, STDOUT_FILENO); - close (out); + if (stdout_file) + { + fd = open (stdout_file, O_WRONLY | O_CREAT, 0700); + if (fd < 0) + exit (127); + dup2 (fd, STDOUT_FILENO); + close (fd); + } - if (out < 0) - exit (127); + if (stderr_file) + { + fd = open (stderr_file, O_WRONLY | O_CREAT, 0700); + if (fd < 0) + exit (127); + dup2 (fd, STDERR_FILENO); + close (fd); + } /* Ensure child is not localised. */ setenv ("LC_ALL", "C", 1); @@ -151,10 +134,23 @@ grub_util_exec_redirect (const char *const *argv, const char *stdin_file, return WEXITSTATUS (status); } +int +grub_util_exec (const char *const *argv) +{ + return grub_util_exec_redirect_all (argv, NULL, NULL, NULL); +} + +int +grub_util_exec_redirect (const char *const *argv, const char *stdin_file, + const char *stdout_file) +{ + return grub_util_exec_redirect_all (argv, stdin_file, stdout_file, NULL); +} + int grub_util_exec_redirect_null (const char *const *argv) { - return grub_util_exec_redirect (argv, "/dev/null", "/dev/null"); + return grub_util_exec_redirect_all (argv, "/dev/null", "/dev/null", NULL); } pid_t diff --git a/include/grub/emu/exec.h b/include/grub/emu/exec.h index ecc3adc30..d1073ef86 100644 --- a/include/grub/emu/exec.h +++ b/include/grub/emu/exec.h @@ -28,6 +28,9 @@ grub_util_exec_pipe (const char *const *argv, int *fd); pid_t grub_util_exec_pipe_stderr (const char *const *argv, int *fd); +int +grub_util_exec_redirect_all (const char *const *argv, const char *stdin_file, + const char *stdout_file, const char *stderr_file); int grub_util_exec (const char *const *argv); int diff --git a/include/grub/osdep/exec_unix.h b/include/grub/osdep/exec_unix.h deleted file mode 100644 index ecc3adc30..000000000 --- a/include/grub/osdep/exec_unix.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2013 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GRUB. If not, see . - */ - -#ifndef GRUB_EMU_EXEC_H -#define GRUB_EMU_EXEC_H 1 - -#include -#include - -#include -pid_t -grub_util_exec_pipe (const char *const *argv, int *fd); -pid_t -grub_util_exec_pipe_stderr (const char *const *argv, int *fd); - -int -grub_util_exec (const char *const *argv); -int -grub_util_exec_redirect (const char *const *argv, const char *stdin_file, - const char *stdout_file); -int -grub_util_exec_redirect_null (const char *const *argv); - -#endif From d770e63557f461060d16d7c4377fc6455219ee34 Mon Sep 17 00:00:00 2001 From: Andrey Borzenkov Date: Mon, 16 Dec 2013 22:41:16 +0400 Subject: [PATCH 2/4] use grub_util_exec_redirect_all to silence "moprobe efivars" modprobe -q still prints error on non-EFI. Redirect stderr instead. Reported by Javier Vasquez. --- grub-core/osdep/linux/platform.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grub-core/osdep/linux/platform.c b/grub-core/osdep/linux/platform.c index ec67aad35..86561f734 100644 --- a/grub-core/osdep/linux/platform.c +++ b/grub-core/osdep/linux/platform.c @@ -69,8 +69,8 @@ grub_install_get_default_x86_platform (void) anyway later. So it should be safe to try to load it here. */ - grub_util_exec ((const char * []){ "modprobe", "-q", - "efivars", NULL }); + grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL }, + NULL, NULL, "/dev/null"); if (is_not_empty_directory ("/sys/firmware/efi")) { if (is_64_kernel ()) From 35248820ea8e8bcef4f8517256ed6d78a1df9800 Mon Sep 17 00:00:00 2001 From: Andrey Borzenkov Date: Mon, 16 Dec 2013 22:56:48 +0400 Subject: [PATCH 3/4] ChangeLog for previous two commits. --- ChangeLog | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ChangeLog b/ChangeLog index 62759bb25..2b7968f7c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2013-12-16 Andrey Borzenkov + + * grub-core/osdep/unix/exec.c (grub_util_exec_redirect_all): New + function to optionally redirect all three standard descriptors. + Redefine grub_util_exec, grub_util_exec_redirect and + grub_util_exec_redirect_null to use it. + * include/grub/emu/exec.h: Define it. + * include/grub/osdep/exec_unix.h: Delete, it is unused. + * grub-core/osdep/linux/platform.c (grub_install_get_default_x86_platform): + Use grub_util_exec_redirect_all to redirect error to NULL. + 2013-12-16 Vladimir Serbinenko Make grub_xen_hypercall on i386 cdecl rather than stdcall to avoid From 840a2c059247c89b7151ad48ccdd277374672bf7 Mon Sep 17 00:00:00 2001 From: Andrey Borzenkov Date: Mon, 16 Dec 2013 23:17:40 +0400 Subject: [PATCH 4/4] add verbose information to linux platform check As suggested by Vladimir Serbienko, add additional verbose output to inform why we selected this specific platform. --- ChangeLog | 5 +++++ grub-core/osdep/linux/platform.c | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2b7968f7c..92262ceb1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2013-12-16 Andrey Borzenkov + + * grub-core/osdep/linux/platform.c (grub_install_get_default_x86_platform): + Add verbose information which firmware directories were tried. + 2013-12-16 Andrey Borzenkov * grub-core/osdep/unix/exec.c (grub_util_exec_redirect_all): New diff --git a/grub-core/osdep/linux/platform.c b/grub-core/osdep/linux/platform.c index 86561f734..4b9f6ef9d 100644 --- a/grub-core/osdep/linux/platform.c +++ b/grub-core/osdep/linux/platform.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -71,15 +72,24 @@ grub_install_get_default_x86_platform (void) */ grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL }, NULL, NULL, "/dev/null"); + + grub_util_info ("Looking for /sys/firmware/efi .."); if (is_not_empty_directory ("/sys/firmware/efi")) { + grub_util_info ("...found"); if (is_64_kernel ()) return "x86_64-efi"; else return "i386-efi"; } - else if (is_not_empty_directory ("/proc/device-tree")) - return "i386-ieee1275"; - else - return "i386-pc"; + + grub_util_info ("... not found. Looking for /proc/device-tree .."); + if (is_not_empty_directory ("/proc/device-tree")) + { + grub_util_info ("...found"); + return "i386-ieee1275"; + } + + grub_util_info ("... not found"); + return "i386-pc"; }