Add progress messages to download 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 <fcntl.h>
28 #include <dirent.h>
29 #include <sys/resource.h>
30
31 #include "../src/guestfs_protocol.h"
32 #include "daemon.h"
33 #include "actions.h"
34
35 /* This command exposes debugging information, internals and
36  * status.  There is no comprehensive documentation for this
37  * command.  You have to look at the source code in this file
38  * to find out what you can do.
39  *
40  * Commands always output a freeform string.
41  */
42
43 #if ENABLE_DEBUG_COMMAND
44 struct cmd {
45   const char *cmd;
46   char * (*f) (const char *subcmd, int argc, char *const *const argv);
47 };
48
49 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
50 static char *debug_env (const char *subcmd, int argc, char *const *const argv);
51 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
52 static char *debug_ls (const char *subcmd, int argc, char *const *const argv);
53 static char *debug_ll (const char *subcmd, int argc, char *const *const argv);
54 static char *debug_segv (const char *subcmd, int argc, char *const *const argv);
55 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
56 static char *debug_core_pattern (const char *subcmd, int argc, char *const *const argv);
57
58 static struct cmd cmds[] = {
59   { "help", debug_help },
60   { "core_pattern", debug_core_pattern },
61   { "env", debug_env },
62   { "fds", debug_fds },
63   { "ls", debug_ls },
64   { "ll", debug_ll },
65   { "segv", debug_segv },
66   { "sh", debug_sh },
67   { NULL, NULL }
68 };
69 #endif
70
71 #if ! ENABLE_DEBUG_COMMAND
72 # define MAYBE_UNUSED ATTRIBUTE_UNUSED
73 #else
74 # define MAYBE_UNUSED /* empty */
75 #endif
76
77 char *
78 do_debug (const char *subcmd MAYBE_UNUSED, char *const *argv MAYBE_UNUSED)
79 {
80 #if ENABLE_DEBUG_COMMAND
81   int argc, i;
82
83   for (i = argc = 0; argv[i] != NULL; ++i)
84     argc++;
85
86   for (i = 0; cmds[i].cmd != NULL; ++i) {
87     if (STRCASEEQ (subcmd, cmds[i].cmd))
88       return cmds[i].f (subcmd, argc, argv);
89   }
90
91   reply_with_error ("use 'debug help' to list the supported commands");
92   return NULL;
93 #else
94   reply_with_error ("guestfsd was not configured with --enable-debug-command");
95   return NULL;
96 #endif
97 }
98
99 #if ENABLE_DEBUG_COMMAND
100 static char *
101 debug_help (const char *subcmd, int argc, char *const *const argv)
102 {
103   int len, i;
104   char *r, *p;
105
106   r = strdup ("Commands supported:");
107   if (!r) {
108     reply_with_perror ("strdup");
109     return NULL;
110   }
111
112   len = strlen (r);
113   for (i = 0; cmds[i].cmd != NULL; ++i) {
114     len += strlen (cmds[i].cmd) + 1; /* space + new command */
115     p = realloc (r, len + 1);        /* +1 for the final NUL */
116     if (p == NULL) {
117       reply_with_perror ("realloc");
118       free (r);
119       return NULL;
120     }
121     r = p;
122
123     strcat (r, " ");
124     strcat (r, cmds[i].cmd);
125   }
126
127   return r;
128 }
129
130 /* Show open FDs. */
131 static char *
132 debug_fds (const char *subcmd, int argc, char *const *const argv)
133 {
134   int r;
135   char *out;
136   size_t size;
137   FILE *fp;
138   DIR *dir;
139   struct dirent *d;
140   char fname[256], link[256];
141   struct stat statbuf;
142
143   fp = open_memstream (&out, &size);
144   if (!fp) {
145     reply_with_perror ("open_memstream");
146     return NULL;
147   }
148
149   dir = opendir ("/proc/self/fd");
150   if (!dir) {
151     reply_with_perror ("opendir: /proc/self/fd");
152     fclose (fp);
153     return NULL;
154   }
155
156   while ((d = readdir (dir)) != NULL) {
157     if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
158       continue;
159
160     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
161
162     r = lstat (fname, &statbuf);
163     if (r == -1) {
164       reply_with_perror ("stat: %s", fname);
165       fclose (fp);
166       free (out);
167       closedir (dir);
168       return NULL;
169     }
170
171     if (S_ISLNK (statbuf.st_mode)) {
172       r = readlink (fname, link, sizeof link - 1);
173       if (r == -1) {
174         reply_with_perror ("readline: %s", fname);
175         fclose (fp);
176         free (out);
177         closedir (dir);
178         return NULL;
179       }
180       link[r] = '\0';
181
182       fprintf (fp, "%2s %s\n", d->d_name, link);
183     } else
184       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
185   }
186
187   fclose (fp);
188
189   if (closedir (dir) == -1) {
190     reply_with_perror ("closedir");
191     free (out);
192     return NULL;
193   }
194
195   return out;
196 }
197
198 /* Force a segfault in the daemon. */
199 static char *
200 debug_segv (const char *subcmd, int argc, char *const *const argv)
201 {
202   *(int*)0 = 0;
203   return NULL;
204 }
205
206 /* Run an arbitrary shell command using /bin/sh from the appliance.
207  *
208  * Note this is somewhat different from the ordinary guestfs_sh command
209  * because it's not using the guest shell, and is not chrooted.
210  */
211 static char *
212 debug_sh (const char *subcmd, int argc, char *const *const argv)
213 {
214   if (argc < 1) {
215     reply_with_error ("sh: expecting a command to run");
216     return NULL;
217   }
218
219   char *cmd;
220   int len, i, j;
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   /* Set up some environment variables. */
242   setenv ("root", sysroot, 1);
243   if (access ("/sys/block/sda", F_OK) == 0)
244     setenv ("sd", "sd", 1);
245   else if (access ("/sys/block/hda", F_OK) == 0)
246     setenv ("sd", "hd", 1);
247   else if (access ("/sys/block/vda", F_OK) == 0)
248     setenv ("sd", "vd", 1);
249
250   char *err;
251   int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
252                     "/bin/sh", "-c", cmd, NULL);
253   free (cmd);
254
255   if (r == -1) {
256     reply_with_error ("%s", err);
257     free (err);
258     return NULL;
259   }
260
261   return err;
262 }
263
264 /* Print the environment that commands get (by running external printenv). */
265 static char *
266 debug_env (const char *subcmd, int argc, char *const *const argv)
267 {
268   int r;
269   char *out, *err;
270
271   r = command (&out, &err, "printenv", NULL);
272   if (r == -1) {
273     reply_with_error ("printenv: %s", err);
274     free (out);
275     free (err);
276     return NULL;
277   }
278
279   free (err);
280
281   return out;
282 }
283
284 /* List files in the appliance. */
285 static char *
286 debug_ls (const char *subcmd, int argc, char *const *const argv)
287 {
288   int len = count_strings (argv);
289   const char *cargv[len+3];
290   int i;
291
292   cargv[0] = "ls";
293   cargv[1] = "-a";
294   for (i = 0; i < len; ++i)
295     cargv[2+i] = argv[i];
296   cargv[2+len] = NULL;
297
298   int r;
299   char *out, *err;
300
301   r = commandv (&out, &err, (void *) cargv);
302   if (r == -1) {
303     reply_with_error ("ls: %s", err);
304     free (out);
305     free (err);
306     return NULL;
307   }
308
309   free (err);
310
311   return out;
312 }
313
314 /* List files in the appliance. */
315 static char *
316 debug_ll (const char *subcmd, int argc, char *const *const argv)
317 {
318   int len = count_strings (argv);
319   const char *cargv[len+3];
320   int i;
321
322   cargv[0] = "ls";
323   cargv[1] = "-la";
324   for (i = 0; i < len; ++i)
325     cargv[2+i] = argv[i];
326   cargv[2+len] = NULL;
327
328   int r;
329   char *out, *err;
330
331   r = commandv (&out, &err, (void *) cargv);
332   if (r == -1) {
333     reply_with_error ("ll: %s", err);
334     free (out);
335     free (err);
336     return NULL;
337   }
338
339   free (err);
340
341   return out;
342 }
343
344 /* Enable core dumping to the given core pattern.
345  * Note that this pattern is relative to any chroot of the process which
346  * crashes. This means that if you want to write the core file to the guest's
347  * storage the pattern must start with /sysroot only if the command which
348  * crashes doesn't chroot.
349  */
350 static char *
351 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
352 {
353   if (argc < 1) {
354     reply_with_error ("core_pattern: expecting a core pattern");
355     return NULL;
356   }
357
358   const char *pattern = argv[0];
359   const size_t pattern_len = strlen(pattern);
360
361 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
362   int fd = open (CORE_PATTERN, O_WRONLY);
363   if (fd == -1) {
364     reply_with_perror ("open: " CORE_PATTERN);
365     return NULL;
366   }
367   if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
368     reply_with_perror ("write: " CORE_PATTERN);
369     return NULL;
370   }
371   if (close (fd) == -1) {
372     reply_with_perror ("close: " CORE_PATTERN);
373     return NULL;
374   }
375
376   struct rlimit limit = {
377     .rlim_cur = RLIM_INFINITY,
378     .rlim_max = RLIM_INFINITY
379   };
380   if (setrlimit (RLIMIT_CORE, &limit) == -1) {
381     reply_with_perror ("setrlimit (RLIMIT_CORE)");
382     return NULL;
383   }
384
385   char *ret = strdup ("ok");
386   if (NULL == ret) {
387     reply_with_perror ("strdup");
388     return NULL;
389   }
390
391   return ret;
392 }
393
394 #endif /* ENABLE_DEBUG_COMMAND */
395
396 #if ENABLE_DEBUG_COMMAND
397 static int
398 write_cb (void *fd_ptr, const void *buf, size_t len)
399 {
400   int fd = *(int *)fd_ptr;
401   return xwrite (fd, buf, len);
402 }
403 #endif
404
405 /* Has one FileIn parameter. */
406 int
407 do_debug_upload (const char *filename MAYBE_UNUSED, int mode MAYBE_UNUSED)
408 {
409 #if ENABLE_DEBUG_COMMAND
410   /* Not chrooted - this command lets you upload a file to anywhere
411    * in the appliance.
412    */
413   int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
414
415   if (fd == -1) {
416     int err = errno;
417     cancel_receive ();
418     errno = err;
419     reply_with_perror ("%s", filename);
420     return -1;
421   }
422
423   int r = receive_file (write_cb, &fd);
424   if (r == -1) {                /* write error */
425     int err = errno;
426     cancel_receive ();
427     errno = err;
428     reply_with_error ("write error: %s", filename);
429     close (fd);
430     return -1;
431   }
432   if (r == -2) {                /* cancellation from library */
433     close (fd);
434     /* Do NOT send any error. */
435     return -1;
436   }
437
438   if (close (fd) == -1) {
439     int err = errno;
440     if (r == -1)                /* if r == 0, file transfer ended already */
441       cancel_receive ();
442     errno = err;
443     reply_with_perror ("close: %s", filename);
444     return -1;
445   }
446
447   return 0;
448 #else
449   reply_with_error ("guestfsd was not configured with --enable-debug-command");
450   return NULL;
451 #endif
452 }