2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <config-util.h>
22 #include <grub/misc.h>
24 #include <grub/emu/exec.h>
25 #include <grub/emu/hostdisk.h>
26 #include <grub/emu/getroot.h>
27 #include <grub/util/misc.h>
28 #include <grub/disk.h>
29 #include <sys/types.h>
37 grub_util_exec_redirect_all (const char *const *argv, const char *stdin_file,
38 const char *stdout_file, const char *stderr_file)
43 const char *const *ptr;
45 for (ptr = argv; *ptr; ptr++)
46 strl += grub_strlen (*ptr) + 1;
48 strl += grub_strlen (stdin_file) + 2;
50 strl += grub_strlen (stdout_file) + 2;
52 strl += grub_strlen (stderr_file) + 3;
54 pstr = str = xmalloc (strl);
55 for (ptr = argv; *ptr; ptr++)
57 pstr = grub_stpcpy (pstr, *ptr);
63 pstr = grub_stpcpy (pstr, stdin_file);
69 pstr = grub_stpcpy (pstr, stdout_file);
76 pstr = grub_stpcpy (pstr, stderr_file);
81 grub_util_info ("executing %s", str);
86 grub_util_error (_("Unable to fork: %s"), strerror (errno));
94 grub_util_devmapper_cleanup ();
95 grub_diskfilter_fini ();
100 fd = open (stdin_file, O_RDONLY);
103 dup2 (fd, STDIN_FILENO);
109 fd = open (stdout_file, O_WRONLY | O_CREAT, 0700);
112 dup2 (fd, STDOUT_FILENO);
118 fd = open (stderr_file, O_WRONLY | O_CREAT, 0700);
121 dup2 (fd, STDERR_FILENO);
125 /* Ensure child is not localised. */
126 setenv ("LC_ALL", "C", 1);
128 execvp ((char *) argv[0], (char **) argv);
131 waitpid (pid, &status, 0);
132 if (!WIFEXITED (status))
134 return WEXITSTATUS (status);
138 grub_util_exec (const char *const *argv)
140 return grub_util_exec_redirect_all (argv, NULL, NULL, NULL);
144 grub_util_exec_redirect (const char *const *argv, const char *stdin_file,
145 const char *stdout_file)
147 return grub_util_exec_redirect_all (argv, stdin_file, stdout_file, NULL);
151 grub_util_exec_redirect_null (const char *const *argv)
153 return grub_util_exec_redirect_all (argv, "/dev/null", "/dev/null", NULL);
157 grub_util_exec_pipe (const char *const *argv, int *fd)
164 if (pipe (pipe_fd) < 0)
166 grub_util_warn (_("Unable to create pipe: %s"),
172 grub_util_error (_("Unable to fork: %s"), strerror (errno));
179 grub_util_devmapper_cleanup ();
180 grub_diskfilter_fini ();
183 /* Ensure child is not localised. */
184 setenv ("LC_ALL", "C", 1);
187 dup2 (pipe_fd[1], STDOUT_FILENO);
190 execvp ((char *) argv[0], (char **) argv);
202 grub_util_exec_pipe_stderr (const char *const *argv, int *fd)
209 if (pipe (pipe_fd) < 0)
211 grub_util_warn (_("Unable to create pipe: %s"),
217 grub_util_error (_("Unable to fork: %s"), strerror (errno));
224 grub_util_devmapper_cleanup ();
225 grub_diskfilter_fini ();
228 /* Ensure child is not localised. */
229 setenv ("LC_ALL", "C", 1);
232 dup2 (pipe_fd[1], STDOUT_FILENO);
233 dup2 (pipe_fd[1], STDERR_FILENO);
236 execvp ((char *) argv[0], (char **) argv);