Generated code for grub-install command.
[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;
118   size_t size;
119   FILE *fp;
120   DIR *dir;
121   struct dirent *d;
122   char fname[256], link[256];
123   struct stat statbuf;
124
125   fp = open_memstream (&out, &size);
126   if (!fp) {
127     reply_with_perror ("open_memstream");
128     return NULL;
129   }
130
131   dir = opendir ("/proc/self/fd");
132   if (!dir) {
133     reply_with_perror ("opendir: /proc/self/fd");
134     fclose (fp);
135     return NULL;
136   }
137
138   while ((d = readdir (dir)) != NULL) {
139     if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
140       continue;
141
142     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
143
144     r = lstat (fname, &statbuf);
145     if (r == -1) {
146       reply_with_perror ("stat: %s", fname);
147       fclose (fp);
148       free (out);
149       closedir (dir);
150       return NULL;
151     }
152
153     if (S_ISLNK (statbuf.st_mode)) {
154       r = readlink (fname, link, sizeof link - 1);
155       if (r == -1) {
156         reply_with_perror ("readline: %s", fname);
157         fclose (fp);
158         free (out);
159         closedir (dir);
160         return NULL;
161       }
162       link[r] = '\0';
163
164       fprintf (fp, "%2s %s\n", d->d_name, link);
165     } else
166       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
167   }
168
169   fclose (fp);
170
171   if (closedir (dir) == -1) {
172     reply_with_perror ("closedir");
173     free (out);
174     return NULL;
175   }
176
177   return out;
178 }
179
180 /* Run an arbitrary shell command. */
181 static char *
182 debug_sh (const char *subcmd, int argc, char *const *const argv)
183 {
184   int r;
185   char *out, *err;
186
187   r = commandv (&out, &err, argv);
188   if (r == -1) {
189     reply_with_error ("ps: %s", err);
190     free (out);
191     free (err);
192     return NULL;
193   }
194
195   free (err);
196
197   return out;
198 }
199
200 #endif /* ENABLE_DEBUG_COMMAND */