summaryrefslogtreecommitdiffstats
path: root/grub-core/osdep/unix/exec.c
blob: e8db9202fba282d6470c988bfe9bdf62a9cd4b91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 *  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 <grub/disk.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_redirect_all (const char *const *argv, const char *stdin_file,
			     const char *stdout_file, const char *stderr_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;
  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 (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);

  pid = fork ();
  if (pid < 0)
    grub_util_error (_("Unable to fork: %s"), strerror (errno));
  else if (pid == 0)
    {
      int fd;
      /* Child.  */
      
      /* Close fd's.  */
#ifdef GRUB_UTIL
      grub_util_devmapper_cleanup ();
      grub_diskfilter_fini ();
#endif

      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);

      execvp ((char *) argv[0], (char **) argv);
      _exit (127);
    }
  waitpid (pid, &status, 0);
  if (!WIFEXITED (status))
    return -1;
  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_all (argv, "/dev/null", "/dev/null", NULL);
}

pid_t
grub_util_exec_pipe (const char *const *argv, int *fd)
{
  int pipe_fd[2];
  pid_t pid;

  *fd = 0;

  if (pipe (pipe_fd) < 0)
    {
      grub_util_warn (_("Unable to create pipe: %s"),
		      strerror (errno));
      return 0;
    }
  pid = fork ();
  if (pid < 0)
    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);

      close (pipe_fd[0]);
      dup2 (pipe_fd[1], STDOUT_FILENO);
      close (pipe_fd[1]);

      execvp ((char *) argv[0], (char **) argv);
      _exit (127);
    }
  else
    {
      close (pipe_fd[1]);
      *fd = pipe_fd[0];
      return pid;
    }
}

pid_t
grub_util_exec_pipe_stderr (const char *const *argv, int *fd)
{
  int pipe_fd[2];
  pid_t pid;

  *fd = 0;

  if (pipe (pipe_fd) < 0)
    {
      grub_util_warn (_("Unable to create pipe: %s"),
		      strerror (errno));
      return 0;
    }
  pid = fork ();
  if (pid < 0)
    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);

      close (pipe_fd[0]);
      dup2 (pipe_fd[1], STDOUT_FILENO);
      dup2 (pipe_fd[1], STDERR_FILENO);
      close (pipe_fd[1]);

      execvp ((char *) argv[0], (char **) argv);
      _exit (127);
    }
  else
    {
      close (pipe_fd[1]);
      *fd = pipe_fd[0];
      return pid;
    }
}