adjust const "**" pointers to avoid warnings
[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 #if ! ENABLE_DEBUG_COMMAND
64 # define MAYBE_UNUSED ATTRIBUTE_UNUSED
65 #else
66 # define MAYBE_UNUSED /* empty */
67 #endif
68
69 char *
70 do_debug (const char *subcmd MAYBE_UNUSED, char *const *argv MAYBE_UNUSED)
71 {
72 #if ENABLE_DEBUG_COMMAND
73   int argc, i;
74
75   for (i = argc = 0; argv[i] != NULL; ++i)
76     argc++;
77
78   for (i = 0; cmds[i].cmd != NULL; ++i) {
79     if (strcasecmp (subcmd, cmds[i].cmd) == 0)
80       return cmds[i].f (subcmd, argc, argv);
81   }
82
83   reply_with_error ("use 'debug help' to list the supported commands");
84   return NULL;
85 #else
86   reply_with_error ("guestfsd was not configured with --enable-debug-command");
87   return NULL;
88 #endif
89 }
90
91 #if ENABLE_DEBUG_COMMAND
92 static char *
93 debug_help (const char *subcmd, int argc, char *const *const argv)
94 {
95   int len, i;
96   char *r, *p;
97
98   r = strdup ("Commands supported:");
99   if (!r) {
100     reply_with_perror ("strdup");
101     return NULL;
102   }
103
104   len = strlen (r);
105   for (i = 0; cmds[i].cmd != NULL; ++i) {
106     len += strlen (cmds[i].cmd) + 1; /* space + new command */
107     p = realloc (r, len + 1);        /* +1 for the final NUL */
108     if (p == NULL) {
109       reply_with_perror ("realloc");
110       free (r);
111       return NULL;
112     }
113     r = p;
114
115     strcat (r, " ");
116     strcat (r, cmds[i].cmd);
117   }
118
119   return r;
120 }
121
122 /* Show open FDs. */
123 static char *
124 debug_fds (const char *subcmd, int argc, char *const *const argv)
125 {
126   int r;
127   char *out;
128   size_t size;
129   FILE *fp;
130   DIR *dir;
131   struct dirent *d;
132   char fname[256], link[256];
133   struct stat statbuf;
134
135   fp = open_memstream (&out, &size);
136   if (!fp) {
137     reply_with_perror ("open_memstream");
138     return NULL;
139   }
140
141   dir = opendir ("/proc/self/fd");
142   if (!dir) {
143     reply_with_perror ("opendir: /proc/self/fd");
144     fclose (fp);
145     return NULL;
146   }
147
148   while ((d = readdir (dir)) != NULL) {
149     if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
150       continue;
151
152     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
153
154     r = lstat (fname, &statbuf);
155     if (r == -1) {
156       reply_with_perror ("stat: %s", fname);
157       fclose (fp);
158       free (out);
159       closedir (dir);
160       return NULL;
161     }
162
163     if (S_ISLNK (statbuf.st_mode)) {
164       r = readlink (fname, link, sizeof link - 1);
165       if (r == -1) {
166         reply_with_perror ("readline: %s", fname);
167         fclose (fp);
168         free (out);
169         closedir (dir);
170         return NULL;
171       }
172       link[r] = '\0';
173
174       fprintf (fp, "%2s %s\n", d->d_name, link);
175     } else
176       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
177   }
178
179   fclose (fp);
180
181   if (closedir (dir) == -1) {
182     reply_with_perror ("closedir");
183     free (out);
184     return NULL;
185   }
186
187   return out;
188 }
189
190 /* Force a segfault in the daemon. */
191 static char *
192 debug_segv (const char *subcmd, int argc, char *const *const argv)
193 {
194   *(int*)0 = 0;
195   return NULL;
196 }
197
198 /* Run an arbitrary shell command using /bin/sh from the appliance.
199  *
200  * Note this is somewhat different from the ordinary guestfs_sh command
201  * because it's not using the guest shell, and is not chrooted.
202  *
203  * Also we ignore any errors and you can see the full output if you
204  * add 2>&1 to the end of the command string.
205  */
206 static char *
207 debug_sh (const char *subcmd, int argc, char *const *const argv)
208 {
209   char *cmd;
210   int len, i, j;
211   char *out;
212
213   if (argc < 1) {
214     reply_with_error ("debug: sh: expecting a command to run");
215     return NULL;
216   }
217
218   /* guestfish splits the parameter(s) into a list of strings,
219    * and we have to reassemble them here.  Not ideal. XXX
220    */
221   for (i = len = 0; i < argc; ++i)
222     len += strlen (argv[i]) + 1;
223   cmd = malloc (len);
224   if (!cmd) {
225     reply_with_perror ("malloc");
226     return NULL;
227   }
228   for (i = j = 0; i < argc; ++i) {
229     len = strlen (argv[i]);
230     memcpy (&cmd[j], argv[i], len);
231     j += len;
232     cmd[j] = ' ';
233     j++;
234   }
235   cmd[j-1] = '\0';
236
237   command (&out, NULL, "/bin/sh", "-c", cmd, NULL);
238   free (cmd);
239   return out;
240 }
241
242 /* Print the environment that commands get (by running external printenv). */
243 static char *
244 debug_env (const char *subcmd, int argc, char *const *const argv)
245 {
246   int r;
247   char *out, *err;
248
249   r = command (&out, &err, "printenv", NULL);
250   if (r == -1) {
251     reply_with_error ("printenv: %s", err);
252     free (out);
253     free (err);
254     return NULL;
255   }
256
257   free (err);
258
259   return out;
260 }
261
262 #endif /* ENABLE_DEBUG_COMMAND */