In the daemon, change all const char * parameters to char *.
[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   int proc_ok, dev_ok, dev_pts_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", "--bind", "/dev", "/sysroot/dev", NULL);
57   dev_ok = r != -1;
58   r = command (NULL, NULL, "mount", "--bind", "/dev/pts", "/sysroot/dev/pts", NULL);
59   dev_pts_ok = r != -1;
60   r = command (NULL, NULL, "mount", "--bind", "/proc", "/sysroot/proc", NULL);
61   proc_ok = r != -1;
62   r = command (NULL, NULL, "mount", "--bind", "/sys", "/sysroot/sys", NULL);
63   sys_ok = r != -1;
64
65   CHROOT_IN;
66   r = commandv (&out, &err, argv);
67   CHROOT_OUT;
68
69   if (sys_ok) command (NULL, NULL, "umount", "/sysroot/sys", NULL);
70   if (proc_ok) command (NULL, NULL, "umount", "/sysroot/proc", NULL);
71   if (dev_pts_ok) command (NULL, NULL, "umount", "/sysroot/dev/pts", NULL);
72   if (dev_ok) command (NULL, NULL, "umount", "/sysroot/dev", NULL);
73
74   if (r == -1) {
75     reply_with_error ("%s", err);
76     free (out);
77     free (err);
78     return NULL;
79   }
80
81   free (err);
82
83   return out;                   /* Caller frees. */
84 }
85
86 char **
87 do_command_lines (char **argv)
88 {
89   char *out;
90   char **lines;
91
92   out = do_command (argv);
93   if (out == NULL)
94     return NULL;
95
96   lines = split_lines (out);
97   free (out);
98
99   if (lines == NULL)
100     return NULL;
101
102   return lines;                 /* Caller frees. */
103 }