* grub-core/osdep/unix/getroot.c: Move exec functions to ...

* osdep/unix/exec.c: ... here. Add few additional exec_* variants.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-19 02:56:40 +02:00
parent d3923ab956
commit 40346de6d3
9 changed files with 269 additions and 79 deletions

View File

@ -1,3 +1,8 @@
2013-10-19 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/osdep/unix/getroot.c: Move exec functions to ...
* osdep/unix/exec.c: ... here. Add few additional exec_* variants.
2013-10-19 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/lib/libgcrypt_wrap/cipher_wrap.h: Define size_t to

View File

@ -17,6 +17,7 @@ library = {
common = grub-core/osdep/devmapper/hostdisk.c;
common = grub-core/osdep/hostdisk.c;
common = grub-core/osdep/unix/hostdisk.c;
common = grub-core/osdep/exec.c;
common = grub-core/osdep/sleep.c;
common = grub-core/osdep/password.c;
common = grub-core/kern/emu/misc.c;

View File

@ -241,6 +241,8 @@ kernel = {
emu = kern/emu/cache_s.S;
emu = kern/emu/hostdisk.c;
emu = osdep/unix/hostdisk.c;
emu = osdep/exec.c;
extra_dist = osdep/unix/exec.c;
emu = osdep/devmapper/hostdisk.c;
emu = osdep/hostdisk.c;
emu = kern/emu/hostfs.c;

3
grub-core/osdep/exec.c Normal file
View File

@ -0,0 +1,3 @@
#if !defined (__MINGW32__) && !defined (__CYGWIN__) && !defined (__AROS__)
#include "unix/exec.c"
#endif

View File

@ -54,6 +54,7 @@
#include <linux/raid/md_p.h>
#include <linux/raid/md_u.h>
#include <grub/i18n.h>
#include <grub/emu/exec.h>
#define LVM_DEV_MAPPER_STRING "/dev/mapper/"
@ -397,7 +398,7 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
static char *
get_mdadm_uuid (const char *os_dev)
{
char *argv[5];
const char *argv[5];
int fd;
pid_t pid;
FILE *mdadm;
@ -405,12 +406,10 @@ get_mdadm_uuid (const char *os_dev)
size_t len = 0;
char *name = NULL;
/* execvp has inconvenient types, hence the casts. None of these
strings will actually be modified. */
argv[0] = (char *) "mdadm";
argv[1] = (char *) "--detail";
argv[2] = (char *) "--export";
argv[3] = (char *) os_dev;
argv[0] = "mdadm";
argv[1] = "--detail";
argv[2] = "--export";
argv[3] = os_dev;
argv[4] = NULL;
pid = grub_util_exec_pipe (argv, &fd);
@ -464,7 +463,7 @@ grub_util_is_imsm (const char *os_dev)
do
{
char *argv[5];
const char *argv[5];
int fd;
pid_t pid;
FILE *mdadm;
@ -473,12 +472,10 @@ grub_util_is_imsm (const char *os_dev)
retry = 0; /* We'll do one more pass if device is part of container */
/* execvp has inconvenient types, hence the casts. None of these
strings will actually be modified. */
argv[0] = (char *) "mdadm";
argv[1] = (char *) "--detail";
argv[2] = (char *) "--export";
argv[3] = (char *) dev;
argv[0] = "mdadm";
argv[1] = "--detail";
argv[2] = "--export";
argv[3] = dev;
argv[4] = NULL;
pid = grub_util_exec_pipe (argv, &fd);

195
grub-core/osdep/unix/exec.c Normal file
View File

@ -0,0 +1,195 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011 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 <http://www.gnu.org/licenses/>.
*/
#include <config-util.h>
#include <config.h>
#include <grub/misc.h>
#include <unistd.h>
#include <grub/emu/exec.h>
#include <grub/emu/hostdisk.h>
#include <grub/emu/getroot.h>
#include <grub/util/misc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
int
grub_util_exec (const char *const *argv)
{
pid_t pid;
int status = -1;
pid = fork ();
if (pid < 0)
grub_util_error (_("Unable to fork: %s"), strerror (errno));
else if (pid == 0)
{
/* Child. */
/* Close fd's. */
grub_util_devmapper_cleanup ();
grub_diskfilter_fini ();
/* 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 mdadm_pid;
int status = -1;
mdadm_pid = fork ();
if (mdadm_pid < 0)
grub_util_error (_("Unable to fork: %s"), strerror (errno));
else if (mdadm_pid == 0)
{
int in, out;
/* Child. */
/* Close fd's. */
grub_util_devmapper_cleanup ();
grub_diskfilter_fini ();
in = open (stdin_file, O_RDONLY);
dup2 (in, STDIN_FILENO);
close (in);
out = open (stdout_file, O_WRONLY | O_CREAT, 0700);
dup2 (out, STDOUT_FILENO);
close (out);
/* Ensure child is not localised. */
setenv ("LC_ALL", "C", 1);
execvp ((char *) argv[0], (char **) argv);
exit (127);
}
waitpid (mdadm_pid, &status, 0);
if (!WIFEXITED (status))
return -1;
return WEXITSTATUS (status);
}
int
grub_util_exec_redirect_null (const char *const *argv)
{
return grub_util_exec_redirect (argv, "/dev/null", "/dev/null");
}
pid_t
grub_util_exec_pipe (const char *const *argv, int *fd)
{
int mdadm_pipe[2];
pid_t mdadm_pid;
*fd = 0;
if (pipe (mdadm_pipe) < 0)
{
grub_util_warn (_("Unable to create pipe: %s"),
strerror (errno));
return 0;
}
mdadm_pid = fork ();
if (mdadm_pid < 0)
grub_util_error (_("Unable to fork: %s"), strerror (errno));
else if (mdadm_pid == 0)
{
/* Child. */
/* Close fd's. */
grub_util_devmapper_cleanup ();
grub_diskfilter_fini ();
/* Ensure child is not localised. */
setenv ("LC_ALL", "C", 1);
close (mdadm_pipe[0]);
dup2 (mdadm_pipe[1], STDOUT_FILENO);
close (mdadm_pipe[1]);
execvp ((char *) argv[0], (char **) argv);
exit (127);
}
else
{
close (mdadm_pipe[1]);
*fd = mdadm_pipe[0];
return mdadm_pid;
}
}
pid_t
grub_util_exec_pipe_stderr (const char *const *argv, int *fd)
{
int mdadm_pipe[2];
pid_t mdadm_pid;
*fd = 0;
if (pipe (mdadm_pipe) < 0)
{
grub_util_warn (_("Unable to create pipe: %s"),
strerror (errno));
return 0;
}
mdadm_pid = fork ();
if (mdadm_pid < 0)
grub_util_error (_("Unable to fork: %s"), strerror (errno));
else if (mdadm_pid == 0)
{
/* Child. */
/* Close fd's. */
grub_util_devmapper_cleanup ();
grub_diskfilter_fini ();
/* Ensure child is not localised. */
setenv ("LC_ALL", "C", 1);
close (mdadm_pipe[0]);
dup2 (mdadm_pipe[1], STDOUT_FILENO);
dup2 (mdadm_pipe[1], STDERR_FILENO);
close (mdadm_pipe[1]);
execvp ((char *) argv[0], (char **) argv);
exit (127);
}
else
{
close (mdadm_pipe[1]);
*fd = mdadm_pipe[0];
return mdadm_pid;
}
}

View File

@ -35,6 +35,7 @@
#include <limits.h>
#endif
#include <grub/util/misc.h>
#include <grub/emu/exec.h>
#include <grub/cryptodisk.h>
#include <grub/i18n.h>
@ -149,49 +150,6 @@ xgetcwd (void)
return path;
}
pid_t
grub_util_exec_pipe (char **argv, int *fd)
{
int mdadm_pipe[2];
pid_t mdadm_pid;
*fd = 0;
if (pipe (mdadm_pipe) < 0)
{
grub_util_warn (_("Unable to create pipe: %s"),
strerror (errno));
return 0;
}
mdadm_pid = fork ();
if (mdadm_pid < 0)
grub_util_error (_("Unable to fork: %s"), strerror (errno));
else if (mdadm_pid == 0)
{
/* Child. */
/* Close fd's. */
grub_util_devmapper_cleanup ();
grub_diskfilter_fini ();
/* Ensure child is not localised. */
setenv ("LC_ALL", "C", 1);
close (mdadm_pipe[0]);
dup2 (mdadm_pipe[1], STDOUT_FILENO);
close (mdadm_pipe[1]);
execvp (argv[0], argv);
exit (127);
}
else
{
close (mdadm_pipe[1]);
*fd = mdadm_pipe[0];
return mdadm_pid;
}
}
#if !defined (__GNU__)
char **
grub_util_find_root_devices_from_poolname (char *poolname)
@ -271,15 +229,13 @@ grub_util_find_root_devices_from_poolname (char *poolname)
char name[PATH_MAX + 1], state[257], readlen[257], writelen[257];
char cksum[257], notes[257];
unsigned int dummy;
char *argv[4];
const char *argv[4];
pid_t pid;
int fd;
/* execvp has inconvenient types, hence the casts. None of these
strings will actually be modified. */
argv[0] = (char *) "zpool";
argv[1] = (char *) "status";
argv[2] = (char *) poolname;
argv[0] = "zpool";
argv[1] = "status";
argv[2] = poolname;
argv[3] = NULL;
pid = grub_util_exec_pipe (argv, &fd);
@ -603,7 +559,7 @@ grub_guess_root_devices (const char *dir_in)
void
grub_util_pull_lvm_by_command (const char *os_dev)
{
char *argv[8];
const char *argv[8];
int fd;
pid_t pid;
FILE *mdadm;
@ -640,20 +596,18 @@ grub_util_pull_lvm_by_command (const char *os_dev)
*optr = '\0';
}
/* execvp has inconvenient types, hence the casts. None of these
strings will actually be modified. */
/* by default PV name is left aligned in 10 character field, meaning that
we do not know where name ends. Using dummy --separator disables
alignment. We have a single field, so separator itself is not output */
argv[0] = (char *) "vgs";
argv[1] = (char *) "--options";
argv[0] = "vgs";
argv[1] = "--options";
if (vgid)
argv[2] = (char *) "vg_uuid,pv_name";
argv[2] = "vg_uuid,pv_name";
else
argv[2] = (char *) "pv_name";
argv[3] = (char *) "--noheadings";
argv[4] = (char *) "--separator";
argv[5] = (char *) ":";
argv[2] = "pv_name";
argv[3] = "--noheadings";
argv[4] = "--separator";
argv[5] = ":";
argv[6] = vgname;
argv[7] = NULL;

39
include/grub/emu/exec.h Normal file
View File

@ -0,0 +1,39 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_EMU_EXEC_H
#define GRUB_EMU_EXEC_H 1
#include <config.h>
#include <stdarg.h>
#include <sys/types.h>
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

View File

@ -65,12 +65,6 @@ grub_util_devmapper_part_to_disk (struct stat *st,
char *
grub_util_get_devmapper_grub_dev (const char *os_dev);
/* Functions provided by getroot.c. */
#if !defined (__MINGW32__) && !defined (__CYGWIN__)
#include <sys/types.h>
pid_t
grub_util_exec_pipe (char **argv, int *fd);
#endif
void
grub_util_pull_lvm_by_command (const char *os_dev);
char **