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.
24 #include <sys/types.h>
30 #include "../src/guestfs_protocol.h"
34 /* This command exposes debugging information, internals and
35 * status. There is no comprehensive documentation for this
36 * command. You have to look at the source code in this file
37 * to find out what you can do.
39 * Commands always output a freeform string.
42 #if ENABLE_DEBUG_COMMAND
45 char * (*f) (const char *subcmd, int argc, char *const *const argv);
48 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
49 static char *debug_env (const char *subcmd, int argc, char *const *const argv);
50 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
51 static char *debug_ls (const char *subcmd, int argc, char *const *const argv);
52 static char *debug_ll (const char *subcmd, int argc, char *const *const argv);
53 static char *debug_segv (const char *subcmd, int argc, char *const *const argv);
54 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
56 static struct cmd cmds[] = {
57 { "help", debug_help },
62 { "segv", debug_segv },
68 #if ! ENABLE_DEBUG_COMMAND
69 # define MAYBE_UNUSED ATTRIBUTE_UNUSED
71 # define MAYBE_UNUSED /* empty */
75 do_debug (const char *subcmd MAYBE_UNUSED, char *const *argv MAYBE_UNUSED)
77 #if ENABLE_DEBUG_COMMAND
80 for (i = argc = 0; argv[i] != NULL; ++i)
83 for (i = 0; cmds[i].cmd != NULL; ++i) {
84 if (STRCASEEQ (subcmd, cmds[i].cmd))
85 return cmds[i].f (subcmd, argc, argv);
88 reply_with_error ("use 'debug help' to list the supported commands");
91 reply_with_error ("guestfsd was not configured with --enable-debug-command");
96 #if ENABLE_DEBUG_COMMAND
98 debug_help (const char *subcmd, int argc, char *const *const argv)
103 r = strdup ("Commands supported:");
105 reply_with_perror ("strdup");
110 for (i = 0; cmds[i].cmd != NULL; ++i) {
111 len += strlen (cmds[i].cmd) + 1; /* space + new command */
112 p = realloc (r, len + 1); /* +1 for the final NUL */
114 reply_with_perror ("realloc");
121 strcat (r, cmds[i].cmd);
129 debug_fds (const char *subcmd, int argc, char *const *const argv)
137 char fname[256], link[256];
140 fp = open_memstream (&out, &size);
142 reply_with_perror ("open_memstream");
146 dir = opendir ("/proc/self/fd");
148 reply_with_perror ("opendir: /proc/self/fd");
153 while ((d = readdir (dir)) != NULL) {
154 if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
157 snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
159 r = lstat (fname, &statbuf);
161 reply_with_perror ("stat: %s", fname);
168 if (S_ISLNK (statbuf.st_mode)) {
169 r = readlink (fname, link, sizeof link - 1);
171 reply_with_perror ("readline: %s", fname);
179 fprintf (fp, "%2s %s\n", d->d_name, link);
181 fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
186 if (closedir (dir) == -1) {
187 reply_with_perror ("closedir");
195 /* Force a segfault in the daemon. */
197 debug_segv (const char *subcmd, int argc, char *const *const argv)
203 /* Run an arbitrary shell command using /bin/sh from the appliance.
205 * Note this is somewhat different from the ordinary guestfs_sh command
206 * because it's not using the guest shell, and is not chrooted.
209 debug_sh (const char *subcmd, int argc, char *const *const argv)
212 reply_with_error ("sh: expecting a command to run");
219 /* guestfish splits the parameter(s) into a list of strings,
220 * and we have to reassemble them here. Not ideal. XXX
222 for (i = len = 0; i < argc; ++i)
223 len += strlen (argv[i]) + 1;
226 reply_with_perror ("malloc");
229 for (i = j = 0; i < argc; ++i) {
230 len = strlen (argv[i]);
231 memcpy (&cmd[j], argv[i], len);
238 /* Set up some environment variables. */
239 setenv ("root", sysroot, 1);
240 if (access ("/sys/block/sda", F_OK) == 0)
241 setenv ("sd", "sd", 1);
242 else if (access ("/sys/block/hda", F_OK) == 0)
243 setenv ("sd", "hd", 1);
244 else if (access ("/sys/block/vda", F_OK) == 0)
245 setenv ("sd", "vd", 1);
248 int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
249 "/bin/sh", "-c", cmd, NULL);
253 reply_with_error ("%s", err);
261 /* Print the environment that commands get (by running external printenv). */
263 debug_env (const char *subcmd, int argc, char *const *const argv)
268 r = command (&out, &err, "printenv", NULL);
270 reply_with_error ("printenv: %s", err);
281 /* List files in the appliance. */
283 debug_ls (const char *subcmd, int argc, char *const *const argv)
285 int len = count_strings (argv);
286 const char *cargv[len+3];
291 for (i = 0; i < len; ++i)
292 cargv[2+i] = argv[i];
298 r = commandv (&out, &err, (void *) cargv);
300 reply_with_error ("ls: %s", err);
311 /* List files in the appliance. */
313 debug_ll (const char *subcmd, int argc, char *const *const argv)
315 int len = count_strings (argv);
316 const char *cargv[len+3];
321 for (i = 0; i < len; ++i)
322 cargv[2+i] = argv[i];
328 r = commandv (&out, &err, (void *) cargv);
330 reply_with_error ("ll: %s", err);
341 #endif /* ENABLE_DEBUG_COMMAND */
343 #if ENABLE_DEBUG_COMMAND
345 write_cb (void *fd_ptr, const void *buf, size_t len)
347 int fd = *(int *)fd_ptr;
348 return xwrite (fd, buf, len);
352 /* Has one FileIn parameter. */
354 do_debug_upload (const char *filename MAYBE_UNUSED, int mode MAYBE_UNUSED)
356 #if ENABLE_DEBUG_COMMAND
357 /* Not chrooted - this command lets you upload a file to anywhere
360 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
366 reply_with_perror ("%s", filename);
370 int r = receive_file (write_cb, &fd);
371 if (r == -1) { /* write error */
375 reply_with_error ("write error: %s", filename);
379 if (r == -2) { /* cancellation from library */
381 /* Do NOT send any error. */
385 if (close (fd) == -1) {
387 if (r == -1) /* if r == 0, file transfer ended already */
390 reply_with_perror ("close: %s", filename);
396 reply_with_error ("guestfsd was not configured with --enable-debug-command");