From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VscnC-0004oO-JX for mharc-grub-devel@gnu.org; Mon, 16 Dec 2013 13:21:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33545) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vscn5-0004l5-0T for grub-devel@gnu.org; Mon, 16 Dec 2013 13:21:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vscmv-0001VQ-A3 for grub-devel@gnu.org; Mon, 16 Dec 2013 13:21:14 -0500 Received: from mail-lb0-x229.google.com ([2a00:1450:4010:c04::229]:43806) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vscmu-0001V1-BW for grub-devel@gnu.org; Mon, 16 Dec 2013 13:21:05 -0500 Received: by mail-lb0-f169.google.com with SMTP id u14so1031255lbd.0 for ; Mon, 16 Dec 2013 10:21:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=fulvZ24+uTIH6Er//gz9270JPQinnoGlqniAwjqC2uY=; b=Wz/6Q2ugcVdbDOopoNnQKNr8sBLdOziFqrcMUUPB8OwGMFoSb3bt1beTSYYCwa05Ml WRda7wgW5McOVggO4Nv/HIVx7P43ev6evmn+wNEt3grx1Z7mhdVOapy9tVRJlORIO2r6 NX2+gg+J6pWuE/W9m5tcmO2N6UAXiFhCV7WlvMBN428wyeMWCs39mcJTZUd6qhTm64R/ SOIoWPMOkpjAdGqj625IDziiNseYOdU53k7GQ2qn8zsVLTU/FX/ma1YQRETTL4MbWFuD eTQPhVLGj05IhiPaPRkPmldK1AT687SYYF4iYGILsYJ3yLQUR+KkErgopDKAr19rAY/S 5EZw== X-Received: by 10.152.203.129 with SMTP id kq1mr1225615lac.77.1387218061971; Mon, 16 Dec 2013 10:21:01 -0800 (PST) Received: from localhost.localdomain (ppp91-76-134-134.pppoe.mtu-net.ru. [91.76.134.134]) by mx.google.com with ESMTPSA id y11sm10407016lbm.13.2013.12.16.10.21.01 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Dec 2013 10:21:01 -0800 (PST) From: Andrey Borzenkov To: grub-devel@gnu.org Subject: [PATCH 1/2] consolidate grub_util_exec code Date: Mon, 16 Dec 2013 22:20:51 +0400 Message-Id: <1387218052-22765-2-git-send-email-arvidjaar@gmail.com> X-Mailer: git-send-email 1.8.4 In-Reply-To: <1387218052-22765-1-git-send-email-arvidjaar@gmail.com> References: <20131216062201.45b549a4@opensuse.site> <1387218052-22765-1-git-send-email-arvidjaar@gmail.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4010:c04::229 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 Dec 2013 18:21:20 -0000 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 | 134 ++++++++++++++++++++--------------------- include/grub/emu/exec.h | 3 + include/grub/osdep/exec_unix.h | 39 ------------ 3 files changed, 68 insertions(+), 108 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 d4865f6..935ff12 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,71 +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'; - - 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) + if (stdin_file) { - /* 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); + *pstr++ = '<'; + pstr = grub_stpcpy (pstr, stdin_file); + *pstr++ = ' '; } - - 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++) + if (stdout_file) { - pstr = grub_stpcpy (pstr, *ptr); + *pstr++ = '>'; + pstr = grub_stpcpy (pstr, stdout_file); *pstr++ = ' '; } - *pstr++ = '<'; - pstr = grub_stpcpy (pstr, stdin_file); - *pstr++ = ' '; - *pstr++ = '>'; - pstr = grub_stpcpy (pstr, stdout_file); - *pstr = '\0'; + if (stderr_file) + { + *pstr++ = '2'; + *pstr++ = '>'; + pstr = grub_stpcpy (pstr, stderr_file); + pstr++; + } + *--pstr = '\0'; grub_util_info ("executing %s", str); grub_free (str); @@ -117,7 +86,7 @@ grub_util_exec_redirect (const char *const *argv, const char *stdin_file, 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); - - out = open (stdout_file, O_WRONLY | O_CREAT, 0700); - dup2 (out, STDOUT_FILENO); - close (out); - - if (out < 0) - exit (127); + if (stdin_file) + { + fd = open (stdin_file, O_RDONLY); + if (fd < 0) + exit (127); + dup2 (fd, STDIN_FILENO); + close (fd); + } + + if (stdout_file) + { + fd = open (stdout_file, O_WRONLY | O_CREAT, 0700); + if (fd < 0) + exit (127); + dup2 (fd, STDOUT_FILENO); + close (fd); + } + + 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); @@ -152,9 +135,22 @@ grub_util_exec_redirect (const char *const *argv, const char *stdin_file, } 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 ecc3adc..d1073ef 100644 --- a/include/grub/emu/exec.h +++ b/include/grub/emu/exec.h @@ -29,6 +29,9 @@ 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 grub_util_exec_redirect (const char *const *argv, const char *stdin_file, diff --git a/include/grub/osdep/exec_unix.h b/include/grub/osdep/exec_unix.h deleted file mode 100644 index ecc3adc..0000000 --- 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 -- 1.8.4