Implement 'debug sh' and 'debug fds' commands.
[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_fds (const char *subcmd, int argc, char *const *const argv);
49 static char *debug_mem (const char *subcmd, int argc, char *const *const argv);
50 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
51
52 static struct cmd cmds[] = {
53   { "help", debug_help },
54   { "fds", debug_fds },
55   { "mem", debug_mem },
56   { "sh", debug_sh },
57   { NULL, NULL }
58 };
59 #endif
60
61 char *
62 do_debug (const char *subcmd, char *const *const argv)
63 {
64 #if ENABLE_DEBUG_COMMAND
65   int argc, i;
66
67   for (i = argc = 0; argv[i] != NULL; ++i)
68     argc++;
69
70   for (i = 0; cmds[i].cmd != NULL; ++i) {
71     if (strcasecmp (subcmd, cmds[i].cmd) == 0)
72       return cmds[i].f (subcmd, argc, argv);
73   }
74
75   reply_with_error ("use 'debug help' to list the supported commands");
76   return NULL;
77 #else
78   reply_with_error ("guestfsd was not configured with --enable-debug-command");
79   return NULL;
80 #endif
81 }
82
83 #if ENABLE_DEBUG_COMMAND
84 static char *
85 debug_help (const char *subcmd, int argc, char *const *const argv)
86 {
87   int len, i;
88   char *r, *p;
89
90   r = strdup ("Commands supported:");
91   if (!r) {
92     reply_with_perror ("strdup");
93     return NULL;
94   }
95
96   len = strlen (r);
97   for (i = 0; cmds[i].cmd != NULL; ++i) {
98     len += strlen (cmds[i].cmd) + 1; /* space + new command */
99     p = realloc (r, len + 1);        /* +1 for the final NUL */
100     if (p == NULL) {
101       reply_with_perror ("realloc");
102       free (r);
103       return NULL;
104     }
105     r = p;
106
107     strcat (r, " ");
108     strcat (r, cmds[i].cmd);
109   }
110
111   return r;
112 }
113
114 /* Show open FDs. */
115 static char *
116 debug_fds (const char *subcmd, int argc, char *const *const argv)
117 {
118   int r;
119   char *out = NULL;
120   DIR *dir;
121   struct dirent *d;
122   char fname[256], link[256];
123   struct stat statbuf;
124
125   dir = opendir ("/proc/self/fd");
126   if (!dir) {
127     reply_with_perror ("opendir: /proc/self/fd");
128     return NULL;
129   }
130
131   while ((d = readdir (dir)) != NULL) {
132     if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
133       continue;
134
135     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
136
137     r = lstat (fname, &statbuf);
138     if (r == -1) {
139       reply_with_perror ("stat: %s", fname);
140       free (out);
141       closedir (dir);
142       return NULL;
143     }
144
145     if (S_ISLNK (statbuf.st_mode)) {
146       r = readlink (fname, link, sizeof link - 1);
147       if (r == -1) {
148         reply_with_perror ("readline: %s", fname);
149         free (out);
150         closedir (dir);
151         return NULL;
152       }
153       link[r] = '\0';
154
155       r = catprintf (&out, "%2s %s\n", d->d_name, link);
156     } else
157       r = catprintf (&out, "%2s 0%o\n", d->d_name, statbuf.st_mode);
158
159     if (r == -1) {
160       reply_with_perror ("catprintf");
161       free (out);
162       closedir (dir);
163       return NULL;
164     }
165   }
166
167   if (closedir (dir) == -1) {
168     reply_with_perror ("closedir");
169     free (out);
170     return NULL;
171   }
172
173   return out;
174 }
175
176 /* Report how much memory we can blindly allocate before
177  * we get an error.
178  */
179 static char *
180 debug_mem (const char *subcmd, int argc, char *const *const argv)
181 {
182   char *mem = NULL, *p;
183   int size = 0;
184   char *buf;
185
186   for (;;) {
187     size += 128 * 1024;
188     p = realloc (mem, size);
189     if (p == NULL) {
190       free (mem);
191       break;
192     }
193     mem = p;
194   }
195
196   if (asprintf (&buf, "%.1f MBytes", size / 1024.0 / 1024.0) == -1) {
197     reply_with_perror ("asprintf");
198     return NULL;
199   }
200
201   return buf;                   /* caller frees */
202 }
203
204 /* Run an arbitrary shell command. */
205 static char *
206 debug_sh (const char *subcmd, int argc, char *const *const argv)
207 {
208   int r;
209   char *out, *err;
210
211   r = commandv (&out, &err, argv);
212   if (r == -1) {
213     reply_with_error ("ps: %s", err);
214     free (out);
215     free (err);
216     return NULL;
217   }
218
219   free (err);
220
221   return out;
222 }
223
224 #endif /* ENABLE_DEBUG_COMMAND */