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_fds (const char *subcmd, int argc, char *const *const argv);
49 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
51 static struct cmd cmds[] = {
52 { "help", debug_help },
60 do_debug (const char *subcmd, char *const *const argv)
62 #if ENABLE_DEBUG_COMMAND
65 for (i = argc = 0; argv[i] != NULL; ++i)
68 for (i = 0; cmds[i].cmd != NULL; ++i) {
69 if (strcasecmp (subcmd, cmds[i].cmd) == 0)
70 return cmds[i].f (subcmd, argc, argv);
73 reply_with_error ("use 'debug help' to list the supported commands");
76 reply_with_error ("guestfsd was not configured with --enable-debug-command");
81 #if ENABLE_DEBUG_COMMAND
83 debug_help (const char *subcmd, int argc, char *const *const argv)
88 r = strdup ("Commands supported:");
90 reply_with_perror ("strdup");
95 for (i = 0; cmds[i].cmd != NULL; ++i) {
96 len += strlen (cmds[i].cmd) + 1; /* space + new command */
97 p = realloc (r, len + 1); /* +1 for the final NUL */
99 reply_with_perror ("realloc");
106 strcat (r, cmds[i].cmd);
114 debug_fds (const char *subcmd, int argc, char *const *const argv)
122 char fname[256], link[256];
125 fp = open_memstream (&out, &size);
127 reply_with_perror ("open_memstream");
131 dir = opendir ("/proc/self/fd");
133 reply_with_perror ("opendir: /proc/self/fd");
138 while ((d = readdir (dir)) != NULL) {
139 if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
142 snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
144 r = lstat (fname, &statbuf);
146 reply_with_perror ("stat: %s", fname);
153 if (S_ISLNK (statbuf.st_mode)) {
154 r = readlink (fname, link, sizeof link - 1);
156 reply_with_perror ("readline: %s", fname);
164 fprintf (fp, "%2s %s\n", d->d_name, link);
166 fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
171 if (closedir (dir) == -1) {
172 reply_with_perror ("closedir");
180 /* Run an arbitrary shell command. */
182 debug_sh (const char *subcmd, int argc, char *const *const argv)
187 r = commandv (&out, &err, argv);
189 reply_with_error ("ps: %s", err);
200 #endif /* ENABLE_DEBUG_COMMAND */