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>
29 #include "../src/guestfs_protocol.h"
33 /* This command exposes debugging information, internals and
34 * status. There is no comprehensive documentation for this
35 * command. You have to look at the source code in this file
36 * to find out what you can do.
38 * Commands always output a freeform string.
41 #if ENABLE_DEBUG_COMMAND
44 char * (*f) (const char *subcmd, int argc, char *const *const argv);
47 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
48 static char *debug_env (const char *subcmd, int argc, char *const *const argv);
49 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
50 static char *debug_ls (const char *subcmd, int argc, char *const *const argv);
51 static char *debug_ll (const char *subcmd, int argc, char *const *const argv);
52 static char *debug_segv (const char *subcmd, int argc, char *const *const argv);
53 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
55 static struct cmd cmds[] = {
56 { "help", debug_help },
61 { "segv", debug_segv },
67 #if ! ENABLE_DEBUG_COMMAND
68 # define MAYBE_UNUSED ATTRIBUTE_UNUSED
70 # define MAYBE_UNUSED /* empty */
74 do_debug (const char *subcmd MAYBE_UNUSED, char *const *argv MAYBE_UNUSED)
76 #if ENABLE_DEBUG_COMMAND
79 for (i = argc = 0; argv[i] != NULL; ++i)
82 for (i = 0; cmds[i].cmd != NULL; ++i) {
83 if (STRCASEEQ (subcmd, cmds[i].cmd))
84 return cmds[i].f (subcmd, argc, argv);
87 reply_with_error ("use 'debug help' to list the supported commands");
90 reply_with_error ("guestfsd was not configured with --enable-debug-command");
95 #if ENABLE_DEBUG_COMMAND
97 debug_help (const char *subcmd, int argc, char *const *const argv)
102 r = strdup ("Commands supported:");
104 reply_with_perror ("strdup");
109 for (i = 0; cmds[i].cmd != NULL; ++i) {
110 len += strlen (cmds[i].cmd) + 1; /* space + new command */
111 p = realloc (r, len + 1); /* +1 for the final NUL */
113 reply_with_perror ("realloc");
120 strcat (r, cmds[i].cmd);
128 debug_fds (const char *subcmd, int argc, char *const *const argv)
136 char fname[256], link[256];
139 fp = open_memstream (&out, &size);
141 reply_with_perror ("open_memstream");
145 dir = opendir ("/proc/self/fd");
147 reply_with_perror ("opendir: /proc/self/fd");
152 while ((d = readdir (dir)) != NULL) {
153 if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
156 snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
158 r = lstat (fname, &statbuf);
160 reply_with_perror ("stat: %s", fname);
167 if (S_ISLNK (statbuf.st_mode)) {
168 r = readlink (fname, link, sizeof link - 1);
170 reply_with_perror ("readline: %s", fname);
178 fprintf (fp, "%2s %s\n", d->d_name, link);
180 fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
185 if (closedir (dir) == -1) {
186 reply_with_perror ("closedir");
194 /* Force a segfault in the daemon. */
196 debug_segv (const char *subcmd, int argc, char *const *const argv)
202 /* Run an arbitrary shell command using /bin/sh from the appliance.
204 * Note this is somewhat different from the ordinary guestfs_sh command
205 * because it's not using the guest shell, and is not chrooted.
207 * Also we ignore any errors and you can see the full output if you
208 * add 2>&1 to the end of the command string.
211 debug_sh (const char *subcmd, int argc, char *const *const argv)
218 reply_with_error ("debug: sh: expecting a command to run");
222 /* guestfish splits the parameter(s) into a list of strings,
223 * and we have to reassemble them here. Not ideal. XXX
225 for (i = len = 0; i < argc; ++i)
226 len += strlen (argv[i]) + 1;
229 reply_with_perror ("malloc");
232 for (i = j = 0; i < argc; ++i) {
233 len = strlen (argv[i]);
234 memcpy (&cmd[j], argv[i], len);
241 command (&out, NULL, "/bin/sh", "-c", cmd, NULL);
246 /* Print the environment that commands get (by running external printenv). */
248 debug_env (const char *subcmd, int argc, char *const *const argv)
253 r = command (&out, &err, "printenv", NULL);
255 reply_with_error ("printenv: %s", err);
266 /* List files in the appliance. */
268 debug_ls (const char *subcmd, int argc, char *const *const argv)
270 int len = count_strings (argv);
271 const char *cargv[len+3];
276 for (i = 0; i < len; ++i)
277 cargv[2+i] = argv[i];
283 r = commandv (&out, &err, (void *) cargv);
285 reply_with_error ("ls: %s", err);
296 /* List files in the appliance. */
298 debug_ll (const char *subcmd, int argc, char *const *const argv)
300 int len = count_strings (argv);
301 const char *cargv[len+3];
306 for (i = 0; i < len; ++i)
307 cargv[2+i] = argv[i];
313 r = commandv (&out, &err, (void *) cargv);
315 reply_with_error ("ll: %s", err);
326 #endif /* ENABLE_DEBUG_COMMAND */