virt-resize: Document guest boot stops at "GRUB" (RHBZ#640961).
[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  * Since libguestfs 1.5.7, the debug command has been enabled
43  * by default for all builds (previously you had to enable it
44  * in configure).  This command is not part of the stable ABI
45  * and may change at any time.
46  */
47
48 struct cmd {
49   const char *cmd;
50   char * (*f) (const char *subcmd, int argc, char *const *const argv);
51 };
52
53 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
54 static char *debug_core_pattern (const char *subcmd, int argc, char *const *const argv);
55 static char *debug_env (const char *subcmd, int argc, char *const *const argv);
56 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
57 static char *debug_ls (const char *subcmd, int argc, char *const *const argv);
58 static char *debug_ll (const char *subcmd, int argc, char *const *const argv);
59 static char *debug_progress (const char *subcmd, int argc, char *const *const argv);
60 static char *debug_qtrace (const char *subcmd, int argc, char *const *const argv);
61 static char *debug_segv (const char *subcmd, int argc, char *const *const argv);
62 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
63
64 static struct cmd cmds[] = {
65   { "help", debug_help },
66   { "core_pattern", debug_core_pattern },
67   { "env", debug_env },
68   { "fds", debug_fds },
69   { "ls", debug_ls },
70   { "ll", debug_ll },
71   { "progress", debug_progress },
72   { "qtrace", debug_qtrace },
73   { "segv", debug_segv },
74   { "sh", debug_sh },
75   { NULL, NULL }
76 };
77
78 char *
79 do_debug (const char *subcmd, char *const *argv)
80 {
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 }
94
95 static char *
96 debug_help (const char *subcmd, int argc, char *const *const argv)
97 {
98   int len, i;
99   char *r, *p;
100
101   r = strdup ("Commands supported:");
102   if (!r) {
103     reply_with_perror ("strdup");
104     return NULL;
105   }
106
107   len = strlen (r);
108   for (i = 0; cmds[i].cmd != NULL; ++i) {
109     len += strlen (cmds[i].cmd) + 1; /* space + new command */
110     p = realloc (r, len + 1);        /* +1 for the final NUL */
111     if (p == NULL) {
112       reply_with_perror ("realloc");
113       free (r);
114       return NULL;
115     }
116     r = p;
117
118     strcat (r, " ");
119     strcat (r, cmds[i].cmd);
120   }
121
122   return r;
123 }
124
125 /* Show open FDs. */
126 static char *
127 debug_fds (const char *subcmd, int argc, char *const *const argv)
128 {
129   int r;
130   char *out;
131   size_t size;
132   FILE *fp;
133   DIR *dir;
134   struct dirent *d;
135   char fname[256], link[256];
136   struct stat statbuf;
137
138   fp = open_memstream (&out, &size);
139   if (!fp) {
140     reply_with_perror ("open_memstream");
141     return NULL;
142   }
143
144   dir = opendir ("/proc/self/fd");
145   if (!dir) {
146     reply_with_perror ("opendir: /proc/self/fd");
147     fclose (fp);
148     return NULL;
149   }
150
151   while ((d = readdir (dir)) != NULL) {
152     if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
153       continue;
154
155     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
156
157     r = lstat (fname, &statbuf);
158     if (r == -1) {
159       reply_with_perror ("stat: %s", fname);
160       fclose (fp);
161       free (out);
162       closedir (dir);
163       return NULL;
164     }
165
166     if (S_ISLNK (statbuf.st_mode)) {
167       r = readlink (fname, link, sizeof link - 1);
168       if (r == -1) {
169         reply_with_perror ("readline: %s", fname);
170         fclose (fp);
171         free (out);
172         closedir (dir);
173         return NULL;
174       }
175       link[r] = '\0';
176
177       fprintf (fp, "%2s %s\n", d->d_name, link);
178     } else
179       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
180   }
181
182   fclose (fp);
183
184   if (closedir (dir) == -1) {
185     reply_with_perror ("closedir");
186     free (out);
187     return NULL;
188   }
189
190   return out;
191 }
192
193 /* Force a segfault in the daemon. */
194 static char *
195 debug_segv (const char *subcmd, int argc, char *const *const argv)
196 {
197   *(int*)0 = 0;
198   return NULL;
199 }
200
201 /* Run an arbitrary shell command using /bin/sh from the appliance.
202  *
203  * Note this is somewhat different from the ordinary guestfs_sh command
204  * because it's not using the guest shell, and is not chrooted.
205  */
206 static char *
207 debug_sh (const char *subcmd, int argc, char *const *const argv)
208 {
209   if (argc < 1) {
210     reply_with_error ("sh: expecting a command to run");
211     return NULL;
212   }
213
214   char *cmd;
215   int len, i, j;
216
217   /* guestfish splits the parameter(s) into a list of strings,
218    * and we have to reassemble them here.  Not ideal. XXX
219    */
220   for (i = len = 0; i < argc; ++i)
221     len += strlen (argv[i]) + 1;
222   cmd = malloc (len);
223   if (!cmd) {
224     reply_with_perror ("malloc");
225     return NULL;
226   }
227   for (i = j = 0; i < argc; ++i) {
228     len = strlen (argv[i]);
229     memcpy (&cmd[j], argv[i], len);
230     j += len;
231     cmd[j] = ' ';
232     j++;
233   }
234   cmd[j-1] = '\0';
235
236   /* Set up some environment variables. */
237   setenv ("root", sysroot, 1);
238   if (access ("/sys/block/sda", F_OK) == 0)
239     setenv ("sd", "sd", 1);
240   else if (access ("/sys/block/hda", F_OK) == 0)
241     setenv ("sd", "hd", 1);
242   else if (access ("/sys/block/vda", F_OK) == 0)
243     setenv ("sd", "vd", 1);
244
245   char *err;
246   int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
247                     "/bin/sh", "-c", cmd, NULL);
248   free (cmd);
249
250   if (r == -1) {
251     reply_with_error ("%s", err);
252     free (err);
253     return NULL;
254   }
255
256   return err;
257 }
258
259 /* Print the environment that commands get (by running external printenv). */
260 static char *
261 debug_env (const char *subcmd, int argc, char *const *const argv)
262 {
263   int r;
264   char *out, *err;
265
266   r = command (&out, &err, "printenv", NULL);
267   if (r == -1) {
268     reply_with_error ("printenv: %s", err);
269     free (out);
270     free (err);
271     return NULL;
272   }
273
274   free (err);
275
276   return out;
277 }
278
279 /* List files in the appliance. */
280 static char *
281 debug_ls (const char *subcmd, int argc, char *const *const argv)
282 {
283   int len = count_strings (argv);
284   const char *cargv[len+3];
285   int i;
286
287   cargv[0] = "ls";
288   cargv[1] = "-a";
289   for (i = 0; i < len; ++i)
290     cargv[2+i] = argv[i];
291   cargv[2+len] = NULL;
292
293   int r;
294   char *out, *err;
295
296   r = commandv (&out, &err, (void *) cargv);
297   if (r == -1) {
298     reply_with_error ("ls: %s", err);
299     free (out);
300     free (err);
301     return NULL;
302   }
303
304   free (err);
305
306   return out;
307 }
308
309 /* List files in the appliance. */
310 static char *
311 debug_ll (const char *subcmd, int argc, char *const *const argv)
312 {
313   int len = count_strings (argv);
314   const char *cargv[len+3];
315   int i;
316
317   cargv[0] = "ls";
318   cargv[1] = "-la";
319   for (i = 0; i < len; ++i)
320     cargv[2+i] = argv[i];
321   cargv[2+len] = NULL;
322
323   int r;
324   char *out, *err;
325
326   r = commandv (&out, &err, (void *) cargv);
327   if (r == -1) {
328     reply_with_error ("ll: %s", err);
329     free (out);
330     free (err);
331     return NULL;
332   }
333
334   free (err);
335
336   return out;
337 }
338
339 /* Generate progress notification messages in order to test progress bars. */
340 static char *
341 debug_progress (const char *subcmd, int argc, char *const *const argv)
342 {
343   if (argc < 1) {
344   error:
345     reply_with_error ("progress: expecting arg (time in seconds as string)");
346     return NULL;
347   }
348
349   char *secs_str = argv[0];
350   unsigned secs;
351   if (sscanf (secs_str, "%u", &secs) != 1 || secs == 0)
352     goto error;
353
354   unsigned i;
355   unsigned tsecs = secs * 10;   /* 1/10ths of seconds */
356   for (i = 1; i <= tsecs; ++i) {
357     usleep (100000);
358     notify_progress ((uint64_t) i, (uint64_t) tsecs);
359   }
360
361   char *ret = strdup ("ok");
362   if (ret == NULL) {
363     reply_with_perror ("strdup");
364     return NULL;
365   }
366
367   return ret;
368 }
369
370 /* Enable core dumping to the given core pattern.
371  * Note that this pattern is relative to any chroot of the process which
372  * crashes. This means that if you want to write the core file to the guest's
373  * storage the pattern must start with /sysroot only if the command which
374  * crashes doesn't chroot.
375  */
376 static char *
377 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
378 {
379   if (argc < 1) {
380     reply_with_error ("core_pattern: expecting a core pattern");
381     return NULL;
382   }
383
384   const char *pattern = argv[0];
385   const size_t pattern_len = strlen(pattern);
386
387 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
388   int fd = open (CORE_PATTERN, O_WRONLY);
389   if (fd == -1) {
390     reply_with_perror ("open: " CORE_PATTERN);
391     return NULL;
392   }
393   if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
394     reply_with_perror ("write: " CORE_PATTERN);
395     return NULL;
396   }
397   if (close (fd) == -1) {
398     reply_with_perror ("close: " CORE_PATTERN);
399     return NULL;
400   }
401
402   struct rlimit limit = {
403     .rlim_cur = RLIM_INFINITY,
404     .rlim_max = RLIM_INFINITY
405   };
406   if (setrlimit (RLIMIT_CORE, &limit) == -1) {
407     reply_with_perror ("setrlimit (RLIMIT_CORE)");
408     return NULL;
409   }
410
411   char *ret = strdup ("ok");
412   if (NULL == ret) {
413     reply_with_perror ("strdup");
414     return NULL;
415   }
416
417   return ret;
418 }
419
420 static int
421 write_cb (void *fd_ptr, const void *buf, size_t len)
422 {
423   int fd = *(int *)fd_ptr;
424   return xwrite (fd, buf, len);
425 }
426
427 /* This requires a non-upstream qemu patch.  See contrib/visualize-alignment/
428  * directory in the libguestfs source tree.
429  */
430 static char *
431 debug_qtrace (const char *subcmd, int argc, char *const *const argv)
432 {
433   int enable;
434
435   if (argc != 2) {
436   bad_args:
437     reply_with_error ("qtrace <device> <on|off>");
438     return NULL;
439   }
440
441   if (STREQ (argv[1], "on"))
442     enable = 1;
443   else if (STREQ (argv[1], "off"))
444     enable = 0;
445   else
446     goto bad_args;
447
448   /* This does a sync and flushes all caches. */
449   if (do_drop_caches (3) == -1)
450     return NULL;
451
452   /* Note this doesn't do device name translation or check this is a device. */
453   int fd = open (argv[0], O_RDONLY | O_DIRECT);
454   if (fd == -1) {
455     reply_with_perror ("qtrace: %s: open", argv[0]);
456     return NULL;
457   }
458
459   /* The pattern of reads is what signals to the analysis program that
460    * tracing should be started or stopped.  Note this assumes both 512
461    * byte sectors, and that O_DIRECT will let us do 512 byte aligned
462    * reads.  We ought to read the sector size of the device and use
463    * that instead (XXX).  The analysis program currently assumes 512
464    * byte sectors anyway.
465    */
466 #define QTRACE_SIZE 512
467   const int patterns[2][5] = {
468     { 2, 15, 21, 2, -1 }, /* disable trace */
469     { 2, 21, 15, 2, -1 }  /* enable trace */
470   };
471   void *buf;
472   size_t i;
473
474   /* For O_DIRECT, buffer must be aligned too (thanks Matt).
475    * Note posix_memalign has this strange errno behaviour.
476    */
477   errno = posix_memalign (&buf, QTRACE_SIZE, QTRACE_SIZE);
478   if (errno != 0) {
479     reply_with_perror ("posix_memalign");
480     close (fd);
481     return NULL;
482   }
483
484   for (i = 0; patterns[enable][i] >= 0; ++i) {
485     if (lseek (fd, patterns[enable][i]*QTRACE_SIZE, SEEK_SET) == -1) {
486       reply_with_perror ("qtrace: %s: lseek", argv[0]);
487       close (fd);
488       free (buf);
489       return NULL;
490     }
491
492     if (read (fd, buf, QTRACE_SIZE) == -1) {
493       reply_with_perror ("qtrace: %s: read", argv[0]);
494       close (fd);
495       free (buf);
496       return NULL;
497     }
498   }
499
500   close (fd);
501   free (buf);
502
503   /* This does a sync and flushes all caches. */
504   if (do_drop_caches (3) == -1)
505     return NULL;
506
507   char *ret = strdup ("ok");
508   if (NULL == ret) {
509     reply_with_perror ("strdup");
510     return NULL;
511   }
512
513   return ret;
514 }
515
516 /* Has one FileIn parameter. */
517 int
518 do_debug_upload (const char *filename, int mode)
519 {
520   /* Not chrooted - this command lets you upload a file to anywhere
521    * in the appliance.
522    */
523   int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
524
525   if (fd == -1) {
526     int err = errno;
527     cancel_receive ();
528     errno = err;
529     reply_with_perror ("%s", filename);
530     return -1;
531   }
532
533   int r = receive_file (write_cb, &fd);
534   if (r == -1) {                /* write error */
535     int err = errno;
536     cancel_receive ();
537     errno = err;
538     reply_with_error ("write error: %s", filename);
539     close (fd);
540     return -1;
541   }
542   if (r == -2) {                /* cancellation from library */
543     close (fd);
544     /* Do NOT send any error. */
545     return -1;
546   }
547
548   if (close (fd) == -1) {
549     int err = errno;
550     if (r == -1)                /* if r == 0, file transfer ended already */
551       cancel_receive ();
552     errno = err;
553     reply_with_perror ("close: %s", filename);
554     return -1;
555   }
556
557   return 0;
558 }