daemon: Print error for invalid chunk.cancel field.
[libguestfs.git] / daemon / command.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "guestfs_protocol.h"
26 #include "daemon.h"
27 #include "actions.h"
28
29 char *
30 do_command (char *const *argv)
31 {
32   char *out, *err;
33   int r;
34   char *sysroot_dev, *sysroot_dev_pts, *sysroot_proc,
35     *sysroot_selinux, *sysroot_sys;
36   int dev_ok, dev_pts_ok, proc_ok, selinux_ok, sys_ok;
37
38   /* We need a root filesystem mounted to do this. */
39   NEED_ROOT (0, return NULL);
40
41   /* Conveniently, argv is already a NULL-terminated argv-style array
42    * of parameters, so we can pass it straight in to our internal
43    * commandv.  We just have to check the list is non-empty.
44    */
45   if (argv[0] == NULL) {
46     reply_with_error ("passed an empty list");
47     return NULL;
48   }
49
50   /* While running the command, bind-mount /dev, /proc, /sys
51    * into the chroot.  However we must be careful to unmount them
52    * afterwards because otherwise they would interfere with
53    * future mount and unmount operations.
54    *
55    * We deliberately allow these commands to fail silently, BUT
56    * if a mount fails, don't unmount the corresponding mount.
57    */
58   sysroot_dev = sysroot_path ("/dev");
59   sysroot_dev_pts = sysroot_path ("/dev/pts");
60   sysroot_proc = sysroot_path ("/proc");
61   sysroot_selinux = sysroot_path ("/selinux");
62   sysroot_sys = sysroot_path ("/sys");
63
64   if (sysroot_dev == NULL || sysroot_dev_pts == NULL ||
65       sysroot_proc == NULL || sysroot_selinux == NULL ||
66       sysroot_sys == NULL) {
67     reply_with_perror ("malloc");
68     free (sysroot_dev);
69     free (sysroot_dev_pts);
70     free (sysroot_proc);
71     free (sysroot_selinux);
72     free (sysroot_sys);
73     return NULL;
74   }
75
76   r = command (NULL, NULL, "mount", "--bind", "/dev", sysroot_dev, NULL);
77   dev_ok = r != -1;
78   r = command (NULL, NULL, "mount", "--bind", "/dev/pts", sysroot_dev_pts, NULL);
79   dev_pts_ok = r != -1;
80   r = command (NULL, NULL, "mount", "--bind", "/proc", sysroot_proc, NULL);
81   proc_ok = r != -1;
82   r = command (NULL, NULL, "mount", "--bind", "/selinux", sysroot_selinux, NULL);
83   selinux_ok = r != -1;
84   r = command (NULL, NULL, "mount", "--bind", "/sys", sysroot_sys, NULL);
85   sys_ok = r != -1;
86
87   CHROOT_IN;
88   r = commandv (&out, &err, (const char * const *) argv);
89   CHROOT_OUT;
90
91   if (sys_ok) command (NULL, NULL, "umount", sysroot_sys, NULL);
92   if (selinux_ok) command (NULL, NULL, "umount", sysroot_selinux, NULL);
93   if (proc_ok) command (NULL, NULL, "umount", sysroot_proc, NULL);
94   if (dev_pts_ok) command (NULL, NULL, "umount", sysroot_dev_pts, NULL);
95   if (dev_ok) command (NULL, NULL, "umount", sysroot_dev, NULL);
96
97   free (sysroot_dev);
98   free (sysroot_dev_pts);
99   free (sysroot_proc);
100   free (sysroot_selinux);
101   free (sysroot_sys);
102
103   if (r == -1) {
104     reply_with_error ("%s", err);
105     free (out);
106     free (err);
107     return NULL;
108   }
109
110   free (err);
111
112   return out;                   /* Caller frees. */
113 }
114
115 char **
116 do_command_lines (char *const *argv)
117 {
118   char *out;
119   char **lines;
120
121   out = do_command (argv);
122   if (out == NULL)
123     return NULL;
124
125   lines = split_lines (out);
126   free (out);
127
128   if (lines == NULL)
129     return NULL;
130
131   return lines;                 /* Caller frees. */
132 }
133
134 char *
135 do_sh (const char *cmd)
136 {
137   const char *argv[] = { "/bin/sh", "-c", cmd, NULL };
138
139   return do_command ((char **) argv);
140 }
141
142 char **
143 do_sh_lines (const char *cmd)
144 {
145   const char *argv[] = { "/bin/sh", "-c", cmd, NULL };
146
147   return do_command_lines ((char **) argv);
148 }