1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
29 #include <sys/resource.h>
31 #include "guestfs_protocol.h"
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.
40 * Commands always output a freeform string.
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.
50 char * (*f) (const char *subcmd, int argc, char *const *const argv);
53 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
54 static char *debug_binaries (const char *subcmd, int argc, char *const *const argv);
55 static char *debug_core_pattern (const char *subcmd, int argc, char *const *const argv);
56 static char *debug_env (const char *subcmd, int argc, char *const *const argv);
57 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
58 static char *debug_ldd (const char *subcmd, int argc, char *const *const argv);
59 static char *debug_ls (const char *subcmd, int argc, char *const *const argv);
60 static char *debug_ll (const char *subcmd, int argc, char *const *const argv);
61 static char *debug_progress (const char *subcmd, int argc, char *const *const argv);
62 static char *debug_qtrace (const char *subcmd, int argc, char *const *const argv);
63 static char *debug_segv (const char *subcmd, int argc, char *const *const argv);
64 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
66 static struct cmd cmds[] = {
67 { "help", debug_help },
68 { "binaries", debug_binaries },
69 { "core_pattern", debug_core_pattern },
75 { "progress", debug_progress },
76 { "qtrace", debug_qtrace },
77 { "segv", debug_segv },
83 do_debug (const char *subcmd, char *const *argv)
87 for (i = argc = 0; argv[i] != NULL; ++i)
90 for (i = 0; cmds[i].cmd != NULL; ++i) {
91 if (STRCASEEQ (subcmd, cmds[i].cmd))
92 return cmds[i].f (subcmd, argc, argv);
95 reply_with_error ("use 'debug help 0' to list the supported commands");
100 debug_help (const char *subcmd, int argc, char *const *const argv)
105 r = strdup ("Commands supported:");
107 reply_with_perror ("strdup");
112 for (i = 0; cmds[i].cmd != NULL; ++i) {
113 len += strlen (cmds[i].cmd) + 1; /* space + new command */
114 p = realloc (r, len + 1); /* +1 for the final NUL */
116 reply_with_perror ("realloc");
123 strcat (r, cmds[i].cmd);
131 debug_fds (const char *subcmd, int argc, char *const *const argv)
139 char fname[256], link[256];
142 fp = open_memstream (&out, &size);
144 reply_with_perror ("open_memstream");
148 dir = opendir ("/proc/self/fd");
150 reply_with_perror ("opendir: /proc/self/fd");
155 while ((d = readdir (dir)) != NULL) {
156 if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
159 snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
161 r = lstat (fname, &statbuf);
163 reply_with_perror ("stat: %s", fname);
170 if (S_ISLNK (statbuf.st_mode)) {
171 r = readlink (fname, link, sizeof link - 1);
173 reply_with_perror ("readline: %s", fname);
181 fprintf (fp, "%2s %s\n", d->d_name, link);
183 fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
188 if (closedir (dir) == -1) {
189 reply_with_perror ("closedir");
197 /* Force a segfault in the daemon. */
199 debug_segv (const char *subcmd, int argc, char *const *const argv)
205 /* Run an arbitrary shell command using /bin/sh from the appliance.
207 * Note this is somewhat different from the ordinary guestfs_sh command
208 * because it's not using the guest shell, and is not chrooted.
211 debug_sh (const char *subcmd, int argc, char *const *const argv)
214 reply_with_error ("sh: expecting a command to run");
221 /* guestfish splits the parameter(s) into a list of strings,
222 * and we have to reassemble them here. Not ideal. XXX
224 for (i = len = 0; i < argc; ++i)
225 len += strlen (argv[i]) + 1;
228 reply_with_perror ("malloc");
231 for (i = j = 0; i < argc; ++i) {
232 len = strlen (argv[i]);
233 memcpy (&cmd[j], argv[i], len);
240 /* Set up some environment variables. */
241 setenv ("root", sysroot, 1);
242 if (access ("/sys/block/sda", F_OK) == 0)
243 setenv ("sd", "sd", 1);
244 else if (access ("/sys/block/hda", F_OK) == 0)
245 setenv ("sd", "hd", 1);
246 else if (access ("/sys/block/vda", F_OK) == 0)
247 setenv ("sd", "vd", 1);
250 int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
251 "/bin/sh", "-c", cmd, NULL);
255 reply_with_error ("%s", err);
263 /* Print the environment that commands get (by running external printenv). */
265 debug_env (const char *subcmd, int argc, char *const *const argv)
270 r = command (&out, &err, "printenv", NULL);
272 reply_with_error ("printenv: %s", err);
283 /* Return binaries in the appliance.
284 * See tests/regressions/rhbz727178.sh
287 debug_binaries (const char *subcmd, int argc, char *const *const argv)
293 "find / -xdev -type f -executable "
295 "| grep application/x-executable "
296 "| gawk -F: '{print $1}'";
298 r = command (&out, &err, "sh", "-c", cmd, NULL);
300 reply_with_error ("find: %s", err);
311 /* Run 'ldd' on a file from the appliance.
312 * See tests/regressions/rhbz727178.sh
315 debug_ldd (const char *subcmd, int argc, char *const *const argv)
318 char *out, *err, *ret;
321 reply_with_error ("ldd: no file argument");
325 /* Note that 'ldd' doesn't fail if it finds errors. We have to grep
326 * for errors in the regression test instead. 'ldd' only fails here
327 * if the binary is not a binary at all (eg. for shell scripts).
328 * Also 'ldd' randomly sends messages to stderr and errors to stdout
329 * depending on the phase of the moon.
331 r = command (&out, &err, "ldd", "-r", argv[0], NULL);
333 reply_with_error ("ldd: %s: %s", argv[0], err);
339 /* Concatenate stdout and stderr in the result. */
340 ret = realloc (out, strlen (out) + strlen (err) + 1);
342 reply_with_perror ("realloc");
354 /* List files in the appliance. */
356 debug_ls (const char *subcmd, int argc, char *const *const argv)
358 int len = count_strings (argv);
359 const char *cargv[len+3];
364 for (i = 0; i < len; ++i)
365 cargv[2+i] = argv[i];
371 r = commandv (&out, &err, (void *) cargv);
373 reply_with_error ("ls: %s", err);
384 /* List files in the appliance. */
386 debug_ll (const char *subcmd, int argc, char *const *const argv)
388 int len = count_strings (argv);
389 const char *cargv[len+3];
394 for (i = 0; i < len; ++i)
395 cargv[2+i] = argv[i];
401 r = commandv (&out, &err, (void *) cargv);
403 reply_with_error ("ll: %s", err);
414 /* Generate progress notification messages in order to test progress bars. */
416 debug_progress (const char *subcmd, int argc, char *const *const argv)
420 reply_with_error ("progress: expecting arg (time in seconds as string)");
424 char *secs_str = argv[0];
426 if (sscanf (secs_str, "%u", &secs) != 1 || secs == 0)
430 unsigned tsecs = secs * 10; /* 1/10ths of seconds */
431 for (i = 1; i <= tsecs; ++i) {
433 notify_progress ((uint64_t) i, (uint64_t) tsecs);
436 char *ret = strdup ("ok");
438 reply_with_perror ("strdup");
445 /* Enable core dumping to the given core pattern.
446 * Note that this pattern is relative to any chroot of the process which
447 * crashes. This means that if you want to write the core file to the guest's
448 * storage the pattern must start with /sysroot only if the command which
449 * crashes doesn't chroot.
452 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
455 reply_with_error ("core_pattern: expecting a core pattern");
459 const char *pattern = argv[0];
460 const size_t pattern_len = strlen(pattern);
462 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
463 int fd = open (CORE_PATTERN, O_WRONLY);
465 reply_with_perror ("open: " CORE_PATTERN);
468 if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
469 reply_with_perror ("write: " CORE_PATTERN);
472 if (close (fd) == -1) {
473 reply_with_perror ("close: " CORE_PATTERN);
477 struct rlimit limit = {
478 .rlim_cur = RLIM_INFINITY,
479 .rlim_max = RLIM_INFINITY
481 if (setrlimit (RLIMIT_CORE, &limit) == -1) {
482 reply_with_perror ("setrlimit (RLIMIT_CORE)");
486 char *ret = strdup ("ok");
488 reply_with_perror ("strdup");
496 write_cb (void *fd_ptr, const void *buf, size_t len)
498 int fd = *(int *)fd_ptr;
499 return xwrite (fd, buf, len);
502 /* This requires a non-upstream qemu patch. See contrib/visualize-alignment/
503 * directory in the libguestfs source tree.
506 debug_qtrace (const char *subcmd, int argc, char *const *const argv)
512 reply_with_error ("qtrace <device> <on|off>");
516 if (STREQ (argv[1], "on"))
518 else if (STREQ (argv[1], "off"))
523 /* This does a sync and flushes all caches. */
524 if (do_drop_caches (3) == -1)
527 /* Note this doesn't do device name translation or check this is a device. */
528 int fd = open (argv[0], O_RDONLY | O_DIRECT);
530 reply_with_perror ("qtrace: %s: open", argv[0]);
534 /* The pattern of reads is what signals to the analysis program that
535 * tracing should be started or stopped. Note this assumes both 512
536 * byte sectors, and that O_DIRECT will let us do 512 byte aligned
537 * reads. We ought to read the sector size of the device and use
538 * that instead (XXX). The analysis program currently assumes 512
539 * byte sectors anyway.
541 #define QTRACE_SIZE 512
542 const int patterns[2][5] = {
543 { 2, 15, 21, 2, -1 }, /* disable trace */
544 { 2, 21, 15, 2, -1 } /* enable trace */
549 /* For O_DIRECT, buffer must be aligned too (thanks Matt).
550 * Note posix_memalign has this strange errno behaviour.
552 errno = posix_memalign (&buf, QTRACE_SIZE, QTRACE_SIZE);
554 reply_with_perror ("posix_memalign");
559 for (i = 0; patterns[enable][i] >= 0; ++i) {
560 if (lseek (fd, patterns[enable][i]*QTRACE_SIZE, SEEK_SET) == -1) {
561 reply_with_perror ("qtrace: %s: lseek", argv[0]);
567 if (read (fd, buf, QTRACE_SIZE) == -1) {
568 reply_with_perror ("qtrace: %s: read", argv[0]);
578 /* This does a sync and flushes all caches. */
579 if (do_drop_caches (3) == -1)
582 char *ret = strdup ("ok");
584 reply_with_perror ("strdup");
591 /* Has one FileIn parameter. */
593 do_debug_upload (const char *filename, int mode)
595 /* Not chrooted - this command lets you upload a file to anywhere
598 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
604 reply_with_perror ("%s", filename);
608 int r = receive_file (write_cb, &fd);
609 if (r == -1) { /* write error */
613 reply_with_error ("write error: %s", filename);
617 if (r == -2) { /* cancellation from library */
618 /* This error is ignored by the library since it initiated the
619 * cancel. Nevertheless we must send an error reply here.
621 reply_with_error ("file upload cancelled");
626 if (close (fd) == -1) {
627 reply_with_perror ("close: %s", filename);