1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
25 #include "../src/guestfs_protocol.h"
30 do_command (char **argv)
34 int proc_ok, dev_ok, dev_pts_ok, sys_ok;
36 /* We need a root filesystem mounted to do this. */
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.
43 if (argv[0] == NULL) {
44 reply_with_error ("command: passed an empty list");
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.
53 * We deliberately allow these commands to fail silently, BUT
54 * if a mount fails, don't unmount the corresponding mount.
56 r = command (NULL, NULL, "mount", "--bind", "/dev", "/sysroot/dev", NULL);
58 r = command (NULL, NULL, "mount", "--bind", "/dev/pts", "/sysroot/dev/pts", NULL);
60 r = command (NULL, NULL, "mount", "--bind", "/proc", "/sysroot/proc", NULL);
62 r = command (NULL, NULL, "mount", "--bind", "/sys", "/sysroot/sys", NULL);
66 r = commandv (&out, &err, argv);
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);
75 reply_with_error ("%s", err);
83 return out; /* Caller frees. */
87 do_command_lines (char **argv)
92 out = do_command (argv);
96 lines = split_lines (out);
102 return lines; /* Caller frees. */
106 do_sh (char *command)
108 char *argv[] = { "/bin/sh", "-c", command, NULL };
110 return do_command (argv);
114 do_sh_lines (char *command)
116 char *argv[] = { "/bin/sh", "-c", command, NULL };
118 return do_command_lines (argv);