Add libguestfs-test-tool.
[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 "../src/guestfs_protocol.h"
26 #include "daemon.h"
27 #include "actions.h"
28
29 char *
30 do_command (char **argv)
31 {
32   char *out, *err;
33   int r;
34   char *sysroot_proc, *sysroot_dev, *sysroot_dev_pts, *sysroot_sys;
35   int proc_ok, dev_ok, dev_pts_ok, sys_ok;
36
37   /* We need a root filesystem mounted to do this. */
38   NEED_ROOT (NULL);
39
40   /* Conveniently, argv is already a NULL-terminated argv-style array
41    * of parameters, so we can pass it straight in to our internal
42    * commandv.  We just have to check the list is non-empty.
43    */
44   if (argv[0] == NULL) {
45     reply_with_error ("command: passed an empty list");
46     return NULL;
47   }
48
49   /* While running the command, bind-mount /dev, /proc, /sys
50    * into the chroot.  However we must be careful to unmount them
51    * afterwards because otherwise they would interfere with
52    * future mount and unmount operations.
53    *
54    * We deliberately allow these commands to fail silently, BUT
55    * if a mount fails, don't unmount the corresponding mount.
56    */
57   sysroot_dev = sysroot_path ("/dev");
58   sysroot_dev_pts = sysroot_path ("/dev/pts");
59   sysroot_proc = sysroot_path ("/proc");
60   sysroot_sys = sysroot_path ("/sys");
61
62   r = command (NULL, NULL, "mount", "--bind", "/dev", sysroot_dev, NULL);
63   dev_ok = r != -1;
64   r = command (NULL, NULL, "mount", "--bind", "/dev/pts", sysroot_dev_pts, NULL);
65   dev_pts_ok = r != -1;
66   r = command (NULL, NULL, "mount", "--bind", "/proc", sysroot_proc, NULL);
67   proc_ok = r != -1;
68   r = command (NULL, NULL, "mount", "--bind", "/sys", sysroot_sys, NULL);
69   sys_ok = r != -1;
70
71   CHROOT_IN;
72   r = commandv (&out, &err, argv);
73   CHROOT_OUT;
74
75   if (sys_ok) command (NULL, NULL, "umount", sysroot_sys, NULL);
76   if (proc_ok) command (NULL, NULL, "umount", sysroot_proc, NULL);
77   if (dev_pts_ok) command (NULL, NULL, "umount", sysroot_dev_pts, NULL);
78   if (dev_ok) command (NULL, NULL, "umount", sysroot_dev, NULL);
79
80   free (sysroot_dev);
81   free (sysroot_dev_pts);
82   free (sysroot_proc);
83   free (sysroot_sys);
84
85   if (r == -1) {
86     reply_with_error ("%s", err);
87     free (out);
88     free (err);
89     return NULL;
90   }
91
92   free (err);
93
94   return out;                   /* Caller frees. */
95 }
96
97 char **
98 do_command_lines (char **argv)
99 {
100   char *out;
101   char **lines;
102
103   out = do_command (argv);
104   if (out == NULL)
105     return NULL;
106
107   lines = split_lines (out);
108   free (out);
109
110   if (lines == NULL)
111     return NULL;
112
113   return lines;                 /* Caller frees. */
114 }
115
116 char *
117 do_sh (char *command)
118 {
119   char *argv[] = { "/bin/sh", "-c", command, NULL };
120
121   return do_command (argv);
122 }
123
124 char **
125 do_sh_lines (char *command)
126 {
127   char *argv[] = { "/bin/sh", "-c", command, NULL };
128
129   return do_command_lines (argv);
130 }