Use a squashfs attached as /dev/sdd during the C API tests.
[libguestfs.git] / daemon / debug.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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <dirent.h>
28
29 #include "../src/guestfs_protocol.h"
30 #include "daemon.h"
31 #include "actions.h"
32
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.
37  *
38  * Commands always output a freeform string.
39  */
40
41 #if ENABLE_DEBUG_COMMAND
42 struct cmd {
43   const char *cmd;
44   char * (*f) (const char *subcmd, int argc, char *const *const argv);
45 };
46
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_segv (const char *subcmd, int argc, char *const *const argv);
51 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
52
53 static struct cmd cmds[] = {
54   { "help", debug_help },
55   { "env", debug_env },
56   { "fds", debug_fds },
57   { "segv", debug_segv },
58   { "sh", debug_sh },
59   { NULL, NULL }
60 };
61 #endif
62
63 char *
64 do_debug (const char *subcmd, char *const *const argv)
65 {
66 #if ENABLE_DEBUG_COMMAND
67   int argc, i;
68
69   for (i = argc = 0; argv[i] != NULL; ++i)
70     argc++;
71
72   for (i = 0; cmds[i].cmd != NULL; ++i) {
73     if (strcasecmp (subcmd, cmds[i].cmd) == 0)
74       return cmds[i].f (subcmd, argc, argv);
75   }
76
77   reply_with_error ("use 'debug help' to list the supported commands");
78   return NULL;
79 #else
80   reply_with_error ("guestfsd was not configured with --enable-debug-command");
81   return NULL;
82 #endif
83 }
84
85 #if ENABLE_DEBUG_COMMAND
86 static char *
87 debug_help (const char *subcmd, int argc, char *const *const argv)
88 {
89   int len, i;
90   char *r, *p;
91
92   r = strdup ("Commands supported:");
93   if (!r) {
94     reply_with_perror ("strdup");
95     return NULL;
96   }
97
98   len = strlen (r);
99   for (i = 0; cmds[i].cmd != NULL; ++i) {
100     len += strlen (cmds[i].cmd) + 1; /* space + new command */
101     p = realloc (r, len + 1);        /* +1 for the final NUL */
102     if (p == NULL) {
103       reply_with_perror ("realloc");
104       free (r);
105       return NULL;
106     }
107     r = p;
108
109     strcat (r, " ");
110     strcat (r, cmds[i].cmd);
111   }
112
113   return r;
114 }
115
116 /* Show open FDs. */
117 static char *
118 debug_fds (const char *subcmd, int argc, char *const *const argv)
119 {
120   int r;
121   char *out;
122   size_t size;
123   FILE *fp;
124   DIR *dir;
125   struct dirent *d;
126   char fname[256], link[256];
127   struct stat statbuf;
128
129   fp = open_memstream (&out, &size);
130   if (!fp) {
131     reply_with_perror ("open_memstream");
132     return NULL;
133   }
134
135   dir = opendir ("/proc/self/fd");
136   if (!dir) {
137     reply_with_perror ("opendir: /proc/self/fd");
138     fclose (fp);
139     return NULL;
140   }
141
142   while ((d = readdir (dir)) != NULL) {
143     if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
144       continue;
145
146     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
147
148     r = lstat (fname, &statbuf);
149     if (r == -1) {
150       reply_with_perror ("stat: %s", fname);
151       fclose (fp);
152       free (out);
153       closedir (dir);
154       return NULL;
155     }
156
157     if (S_ISLNK (statbuf.st_mode)) {
158       r = readlink (fname, link, sizeof link - 1);
159       if (r == -1) {
160         reply_with_perror ("readline: %s", fname);
161         fclose (fp);
162         free (out);
163         closedir (dir);
164         return NULL;
165       }
166       link[r] = '\0';
167
168       fprintf (fp, "%2s %s\n", d->d_name, link);
169     } else
170       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
171   }
172
173   fclose (fp);
174
175   if (closedir (dir) == -1) {
176     reply_with_perror ("closedir");
177     free (out);
178     return NULL;
179   }
180
181   return out;
182 }
183
184 /* Force a segfault in the daemon. */
185 static char *
186 debug_segv (const char *subcmd, int argc, char *const *const argv)
187 {
188   *(int*)0 = 0;
189   return NULL;
190 }
191
192 /* Run an arbitrary shell command. */
193 static char *
194 debug_sh (const char *subcmd, int argc, char *const *const argv)
195 {
196   int r;
197   char *out, *err;
198
199   r = commandv (&out, &err, argv);
200   if (r == -1) {
201     reply_with_error ("sh: %s", err);
202     free (out);
203     free (err);
204     return NULL;
205   }
206
207   free (err);
208
209   return out;
210 }
211
212 /* Print the environment that commands get (by running external printenv). */
213 static char *
214 debug_env (const char *subcmd, int argc, char *const *const argv)
215 {
216   int r;
217   char *out, *err;
218
219   r = command (&out, &err, "printenv", NULL);
220   if (r == -1) {
221     reply_with_error ("printenv: %s", err);
222     free (out);
223     free (err);
224     return NULL;
225   }
226
227   free (err);
228
229   return out;
230 }
231
232 #endif /* ENABLE_DEBUG_COMMAND */