New API: checksum-device for checksumming devices.
[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_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);
54
55 static struct cmd cmds[] = {
56   { "help", debug_help },
57   { "env", debug_env },
58   { "fds", debug_fds },
59   { "ls", debug_ls },
60   { "ll", debug_ll },
61   { "segv", debug_segv },
62   { "sh", debug_sh },
63   { NULL, NULL }
64 };
65 #endif
66
67 #if ! ENABLE_DEBUG_COMMAND
68 # define MAYBE_UNUSED ATTRIBUTE_UNUSED
69 #else
70 # define MAYBE_UNUSED /* empty */
71 #endif
72
73 char *
74 do_debug (const char *subcmd MAYBE_UNUSED, char *const *argv MAYBE_UNUSED)
75 {
76 #if ENABLE_DEBUG_COMMAND
77   int argc, i;
78
79   for (i = argc = 0; argv[i] != NULL; ++i)
80     argc++;
81
82   for (i = 0; cmds[i].cmd != NULL; ++i) {
83     if (STRCASEEQ (subcmd, cmds[i].cmd))
84       return cmds[i].f (subcmd, argc, argv);
85   }
86
87   reply_with_error ("use 'debug help' to list the supported commands");
88   return NULL;
89 #else
90   reply_with_error ("guestfsd was not configured with --enable-debug-command");
91   return NULL;
92 #endif
93 }
94
95 #if ENABLE_DEBUG_COMMAND
96 static char *
97 debug_help (const char *subcmd, int argc, char *const *const argv)
98 {
99   int len, i;
100   char *r, *p;
101
102   r = strdup ("Commands supported:");
103   if (!r) {
104     reply_with_perror ("strdup");
105     return NULL;
106   }
107
108   len = strlen (r);
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 */
112     if (p == NULL) {
113       reply_with_perror ("realloc");
114       free (r);
115       return NULL;
116     }
117     r = p;
118
119     strcat (r, " ");
120     strcat (r, cmds[i].cmd);
121   }
122
123   return r;
124 }
125
126 /* Show open FDs. */
127 static char *
128 debug_fds (const char *subcmd, int argc, char *const *const argv)
129 {
130   int r;
131   char *out;
132   size_t size;
133   FILE *fp;
134   DIR *dir;
135   struct dirent *d;
136   char fname[256], link[256];
137   struct stat statbuf;
138
139   fp = open_memstream (&out, &size);
140   if (!fp) {
141     reply_with_perror ("open_memstream");
142     return NULL;
143   }
144
145   dir = opendir ("/proc/self/fd");
146   if (!dir) {
147     reply_with_perror ("opendir: /proc/self/fd");
148     fclose (fp);
149     return NULL;
150   }
151
152   while ((d = readdir (dir)) != NULL) {
153     if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
154       continue;
155
156     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
157
158     r = lstat (fname, &statbuf);
159     if (r == -1) {
160       reply_with_perror ("stat: %s", fname);
161       fclose (fp);
162       free (out);
163       closedir (dir);
164       return NULL;
165     }
166
167     if (S_ISLNK (statbuf.st_mode)) {
168       r = readlink (fname, link, sizeof link - 1);
169       if (r == -1) {
170         reply_with_perror ("readline: %s", fname);
171         fclose (fp);
172         free (out);
173         closedir (dir);
174         return NULL;
175       }
176       link[r] = '\0';
177
178       fprintf (fp, "%2s %s\n", d->d_name, link);
179     } else
180       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
181   }
182
183   fclose (fp);
184
185   if (closedir (dir) == -1) {
186     reply_with_perror ("closedir");
187     free (out);
188     return NULL;
189   }
190
191   return out;
192 }
193
194 /* Force a segfault in the daemon. */
195 static char *
196 debug_segv (const char *subcmd, int argc, char *const *const argv)
197 {
198   *(int*)0 = 0;
199   return NULL;
200 }
201
202 /* Run an arbitrary shell command using /bin/sh from the appliance.
203  *
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.
206  *
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.
209  */
210 static char *
211 debug_sh (const char *subcmd, int argc, char *const *const argv)
212 {
213   char *cmd;
214   int len, i, j;
215   char *out;
216
217   if (argc < 1) {
218     reply_with_error ("sh: expecting a command to run");
219     return NULL;
220   }
221
222   /* guestfish splits the parameter(s) into a list of strings,
223    * and we have to reassemble them here.  Not ideal. XXX
224    */
225   for (i = len = 0; i < argc; ++i)
226     len += strlen (argv[i]) + 1;
227   cmd = malloc (len);
228   if (!cmd) {
229     reply_with_perror ("malloc");
230     return NULL;
231   }
232   for (i = j = 0; i < argc; ++i) {
233     len = strlen (argv[i]);
234     memcpy (&cmd[j], argv[i], len);
235     j += len;
236     cmd[j] = ' ';
237     j++;
238   }
239   cmd[j-1] = '\0';
240
241   command (&out, NULL, "/bin/sh", "-c", cmd, NULL);
242   free (cmd);
243   return out;
244 }
245
246 /* Print the environment that commands get (by running external printenv). */
247 static char *
248 debug_env (const char *subcmd, int argc, char *const *const argv)
249 {
250   int r;
251   char *out, *err;
252
253   r = command (&out, &err, "printenv", NULL);
254   if (r == -1) {
255     reply_with_error ("printenv: %s", err);
256     free (out);
257     free (err);
258     return NULL;
259   }
260
261   free (err);
262
263   return out;
264 }
265
266 /* List files in the appliance. */
267 static char *
268 debug_ls (const char *subcmd, int argc, char *const *const argv)
269 {
270   int len = count_strings (argv);
271   const char *cargv[len+3];
272   int i;
273
274   cargv[0] = "ls";
275   cargv[1] = "-a";
276   for (i = 0; i < len; ++i)
277     cargv[2+i] = argv[i];
278   cargv[2+len] = NULL;
279
280   int r;
281   char *out, *err;
282
283   r = commandv (&out, &err, (void *) cargv);
284   if (r == -1) {
285     reply_with_error ("ls: %s", err);
286     free (out);
287     free (err);
288     return NULL;
289   }
290
291   free (err);
292
293   return out;
294 }
295
296 /* List files in the appliance. */
297 static char *
298 debug_ll (const char *subcmd, int argc, char *const *const argv)
299 {
300   int len = count_strings (argv);
301   const char *cargv[len+3];
302   int i;
303
304   cargv[0] = "ls";
305   cargv[1] = "-la";
306   for (i = 0; i < len; ++i)
307     cargv[2+i] = argv[i];
308   cargv[2+len] = NULL;
309
310   int r;
311   char *out, *err;
312
313   r = commandv (&out, &err, (void *) cargv);
314   if (r == -1) {
315     reply_with_error ("ll: %s", err);
316     free (out);
317     free (err);
318     return NULL;
319   }
320
321   free (err);
322
323   return out;
324 }
325
326 #endif /* ENABLE_DEBUG_COMMAND */