daemon: debug segv correct use of dereferencing NULL.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "ignore-value.h"
30
31 static inline void
32 umount_ignore_fail (const char *path)
33 {
34   ignore_value (command (NULL, NULL, "umount", path, NULL));
35 }
36
37 char *
38 do_command (char *const *argv)
39 {
40   char *out, *err;
41   int r;
42   char *sysroot_dev, *sysroot_dev_pts, *sysroot_proc,
43     *sysroot_selinux, *sysroot_sys;
44   int dev_ok, dev_pts_ok, proc_ok, selinux_ok, sys_ok;
45
46   /* We need a root filesystem mounted to do this. */
47   NEED_ROOT (, return NULL);
48
49   /* Conveniently, argv is already a NULL-terminated argv-style array
50    * of parameters, so we can pass it straight in to our internal
51    * commandv.  We just have to check the list is non-empty.
52    */
53   if (argv[0] == NULL) {
54     reply_with_error ("passed an empty list");
55     return NULL;
56   }
57
58   /* While running the command, bind-mount /dev, /proc, /sys
59    * into the chroot.  However we must be careful to unmount them
60    * afterwards because otherwise they would interfere with
61    * future mount and unmount operations.
62    *
63    * We deliberately allow these commands to fail silently, BUT
64    * if a mount fails, don't unmount the corresponding mount.
65    */
66   sysroot_dev = sysroot_path ("/dev");
67   sysroot_dev_pts = sysroot_path ("/dev/pts");
68   sysroot_proc = sysroot_path ("/proc");
69   sysroot_selinux = sysroot_path ("/selinux");
70   sysroot_sys = sysroot_path ("/sys");
71
72   if (sysroot_dev == NULL || sysroot_dev_pts == NULL ||
73       sysroot_proc == NULL || sysroot_selinux == NULL ||
74       sysroot_sys == NULL) {
75     reply_with_perror ("malloc");
76     free (sysroot_dev);
77     free (sysroot_dev_pts);
78     free (sysroot_proc);
79     free (sysroot_selinux);
80     free (sysroot_sys);
81     return NULL;
82   }
83
84   r = command (NULL, NULL, "mount", "--bind", "/dev", sysroot_dev, NULL);
85   dev_ok = r != -1;
86   r = command (NULL, NULL, "mount", "--bind", "/dev/pts", sysroot_dev_pts, NULL);
87   dev_pts_ok = r != -1;
88   r = command (NULL, NULL, "mount", "--bind", "/proc", sysroot_proc, NULL);
89   proc_ok = r != -1;
90   r = command (NULL, NULL, "mount", "--bind", "/selinux", sysroot_selinux, NULL);
91   selinux_ok = r != -1;
92   r = command (NULL, NULL, "mount", "--bind", "/sys", sysroot_sys, NULL);
93   sys_ok = r != -1;
94
95   CHROOT_IN;
96   r = commandv (&out, &err, (const char * const *) argv);
97   CHROOT_OUT;
98
99   if (sys_ok) umount_ignore_fail (sysroot_sys);
100   if (selinux_ok) umount_ignore_fail (sysroot_selinux);
101   if (proc_ok) umount_ignore_fail (sysroot_proc);
102   if (dev_pts_ok) umount_ignore_fail (sysroot_dev_pts);
103   if (dev_ok) umount_ignore_fail (sysroot_dev);
104
105   free (sysroot_dev);
106   free (sysroot_dev_pts);
107   free (sysroot_proc);
108   free (sysroot_selinux);
109   free (sysroot_sys);
110
111   if (r == -1) {
112     reply_with_error ("%s", err);
113     free (out);
114     free (err);
115     return NULL;
116   }
117
118   free (err);
119
120   return out;                   /* Caller frees. */
121 }
122
123 char **
124 do_command_lines (char *const *argv)
125 {
126   char *out;
127   char **lines;
128
129   out = do_command (argv);
130   if (out == NULL)
131     return NULL;
132
133   lines = split_lines (out);
134   free (out);
135
136   if (lines == NULL)
137     return NULL;
138
139   return lines;                 /* Caller frees. */
140 }
141
142 char *
143 do_sh (const char *cmd)
144 {
145   const char *argv[] = { "/bin/sh", "-c", cmd, NULL };
146
147   return do_command ((char **) argv);
148 }
149
150 char **
151 do_sh_lines (const char *cmd)
152 {
153   const char *argv[] = { "/bin/sh", "-c", cmd, NULL };
154
155   return do_command_lines ((char **) argv);
156 }