Remove the 'debug mem' command, it's never going to work.
[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_sh (const char *subcmd, int argc, char *const *const argv);
50
51 static struct cmd cmds[] = {
52   { "help", debug_help },
53   { "fds", debug_fds },
54   { "sh", debug_sh },
55   { NULL, NULL }
56 };
57 #endif
58
59 char *
60 do_debug (const char *subcmd, char *const *const argv)
61 {
62 #if ENABLE_DEBUG_COMMAND
63   int argc, i;
64
65   for (i = argc = 0; argv[i] != NULL; ++i)
66     argc++;
67
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);
71   }
72
73   reply_with_error ("use 'debug help' to list the supported commands");
74   return NULL;
75 #else
76   reply_with_error ("guestfsd was not configured with --enable-debug-command");
77   return NULL;
78 #endif
79 }
80
81 #if ENABLE_DEBUG_COMMAND
82 static char *
83 debug_help (const char *subcmd, int argc, char *const *const argv)
84 {
85   int len, i;
86   char *r, *p;
87
88   r = strdup ("Commands supported:");
89   if (!r) {
90     reply_with_perror ("strdup");
91     return NULL;
92   }
93
94   len = strlen (r);
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 */
98     if (p == NULL) {
99       reply_with_perror ("realloc");
100       free (r);
101       return NULL;
102     }
103     r = p;
104
105     strcat (r, " ");
106     strcat (r, cmds[i].cmd);
107   }
108
109   return r;
110 }
111
112 /* Show open FDs. */
113 static char *
114 debug_fds (const char *subcmd, int argc, char *const *const argv)
115 {
116   int r;
117   char *out = NULL;
118   DIR *dir;
119   struct dirent *d;
120   char fname[256], link[256];
121   struct stat statbuf;
122
123   dir = opendir ("/proc/self/fd");
124   if (!dir) {
125     reply_with_perror ("opendir: /proc/self/fd");
126     return NULL;
127   }
128
129   while ((d = readdir (dir)) != NULL) {
130     if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
131       continue;
132
133     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
134
135     r = lstat (fname, &statbuf);
136     if (r == -1) {
137       reply_with_perror ("stat: %s", fname);
138       free (out);
139       closedir (dir);
140       return NULL;
141     }
142
143     if (S_ISLNK (statbuf.st_mode)) {
144       r = readlink (fname, link, sizeof link - 1);
145       if (r == -1) {
146         reply_with_perror ("readline: %s", fname);
147         free (out);
148         closedir (dir);
149         return NULL;
150       }
151       link[r] = '\0';
152
153       r = catprintf (&out, "%2s %s\n", d->d_name, link);
154     } else
155       r = catprintf (&out, "%2s 0%o\n", d->d_name, statbuf.st_mode);
156
157     if (r == -1) {
158       reply_with_perror ("catprintf");
159       free (out);
160       closedir (dir);
161       return NULL;
162     }
163   }
164
165   if (closedir (dir) == -1) {
166     reply_with_perror ("closedir");
167     free (out);
168     return NULL;
169   }
170
171   return out;
172 }
173
174 /* Run an arbitrary shell command. */
175 static char *
176 debug_sh (const char *subcmd, int argc, char *const *const argv)
177 {
178   int r;
179   char *out, *err;
180
181   r = commandv (&out, &err, argv);
182   if (r == -1) {
183     reply_with_error ("ps: %s", err);
184     free (out);
185     free (err);
186     return NULL;
187   }
188
189   free (err);
190
191   return out;
192 }
193
194 #endif /* ENABLE_DEBUG_COMMAND */