Added regression test for RHBZ503169#c10
[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 * const * const argv)
31 {
32   char *out, *err;
33   int r;
34   int proc_ok, dev_ok, sys_ok;
35
36   /* We need a root filesystem mounted to do this. */
37   NEED_ROOT (NULL);
38
39   /* Conveniently, argv is already a NULL-terminated argv-style array
40    * of parameters, so we can pass it straight in to our internal
41    * commandv.  We just have to check the list is non-empty.
42    */
43   if (argv[0] == NULL) {
44     reply_with_error ("command: passed an empty list");
45     return NULL;
46   }
47
48   /* While running the command, bind-mount /dev, /proc, /sys
49    * into the chroot.  However we must be careful to unmount them
50    * afterwards because otherwise they would interfere with
51    * future mount and unmount operations.
52    *
53    * We deliberately allow these commands to fail silently, BUT
54    * if a mount fails, don't unmount the corresponding mount.
55    */
56   r = command (NULL, NULL, "mount", "--rbind", "/dev", "/sysroot/dev", NULL);
57   dev_ok = r != -1;
58   r = command (NULL, NULL, "mount", "--rbind", "/proc", "/sysroot/proc", NULL);
59   proc_ok = r != -1;
60   r = command (NULL, NULL, "mount", "--rbind", "/sys", "/sysroot/sys", NULL);
61   sys_ok = r != -1;
62
63   CHROOT_IN;
64   r = commandv (&out, &err, argv);
65   CHROOT_OUT;
66
67   if (sys_ok) command (NULL, NULL, "umount", "/sysroot/sys", NULL);
68   if (proc_ok) command (NULL, NULL, "umount", "/sysroot/proc", NULL);
69   if (dev_ok) command (NULL, NULL, "umount", "/sysroot/dev", NULL);
70
71   if (r == -1) {
72     reply_with_error ("%s", err);
73     free (out);
74     free (err);
75     return NULL;
76   }
77
78   free (err);
79
80   return out;                   /* Caller frees. */
81 }
82
83 char **
84 do_command_lines (char * const * const argv)
85 {
86   char *out;
87   char **lines;
88
89   out = do_command (argv);
90   if (out == NULL)
91     return NULL;
92
93   lines = split_lines (out);
94   free (out);
95
96   if (lines == NULL)
97     return NULL;
98
99   return lines;                 /* Caller frees. */
100 }