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)
201 /* http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
202 * "Dereferencing a NULL Pointer: contrary to popular belief,
203 * dereferencing a null pointer in C is undefined. It is not defined
206 volatile int *ptr = NULL;
211 /* Run an arbitrary shell command using /bin/sh from the appliance.
213 * Note this is somewhat different from the ordinary guestfs_sh command
214 * because it's not using the guest shell, and is not chrooted.
217 debug_sh (const char *subcmd, int argc, char *const *const argv)
220 reply_with_error ("sh: expecting a command to run");
227 /* guestfish splits the parameter(s) into a list of strings,
228 * and we have to reassemble them here. Not ideal. XXX
230 for (i = len = 0; i < argc; ++i)
231 len += strlen (argv[i]) + 1;
234 reply_with_perror ("malloc");
237 for (i = j = 0; i < argc; ++i) {
238 len = strlen (argv[i]);
239 memcpy (&cmd[j], argv[i], len);
246 /* Set up some environment variables. */
247 setenv ("root", sysroot, 1);
248 if (access ("/sys/block/sda", F_OK) == 0)
249 setenv ("sd", "sd", 1);
250 else if (access ("/sys/block/hda", F_OK) == 0)
251 setenv ("sd", "hd", 1);
252 else if (access ("/sys/block/vda", F_OK) == 0)
253 setenv ("sd", "vd", 1);
256 int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
257 "/bin/sh", "-c", cmd, NULL);
261 reply_with_error ("%s", err);
269 /* Print the environment that commands get (by running external printenv). */
271 debug_env (const char *subcmd, int argc, char *const *const argv)
276 r = command (&out, &err, "printenv", NULL);
278 reply_with_error ("printenv: %s", err);
289 /* Return binaries in the appliance.
290 * See tests/regressions/rhbz727178.sh
293 debug_binaries (const char *subcmd, int argc, char *const *const argv)
299 "find / -xdev -type f -executable "
301 "| grep application/x-executable "
302 "| gawk -F: '{print $1}'";
304 r = command (&out, &err, "sh", "-c", cmd, NULL);
306 reply_with_error ("find: %s", err);
317 /* Run 'ldd' on a file from the appliance.
318 * See tests/regressions/rhbz727178.sh
321 debug_ldd (const char *subcmd, int argc, char *const *const argv)
324 char *out, *err, *ret;
327 reply_with_error ("ldd: no file argument");
331 /* Note that 'ldd' doesn't fail if it finds errors. We have to grep
332 * for errors in the regression test instead. 'ldd' only fails here
333 * if the binary is not a binary at all (eg. for shell scripts).
334 * Also 'ldd' randomly sends messages to stderr and errors to stdout
335 * depending on the phase of the moon.
337 r = command (&out, &err, "ldd", "-r", argv[0], NULL);
339 reply_with_error ("ldd: %s: %s", argv[0], err);
345 /* Concatenate stdout and stderr in the result. */
346 ret = realloc (out, strlen (out) + strlen (err) + 1);
348 reply_with_perror ("realloc");
360 /* List files in the appliance. */
362 debug_ls (const char *subcmd, int argc, char *const *const argv)
364 int len = count_strings (argv);
365 const char *cargv[len+3];
370 for (i = 0; i < len; ++i)
371 cargv[2+i] = argv[i];
377 r = commandv (&out, &err, (void *) cargv);
379 reply_with_error ("ls: %s", err);
390 /* List files in the appliance. */
392 debug_ll (const char *subcmd, int argc, char *const *const argv)
394 int len = count_strings (argv);
395 const char *cargv[len+3];
400 for (i = 0; i < len; ++i)
401 cargv[2+i] = argv[i];
407 r = commandv (&out, &err, (void *) cargv);
409 reply_with_error ("ll: %s", err);
420 /* Generate progress notification messages in order to test progress bars. */
422 debug_progress (const char *subcmd, int argc, char *const *const argv)
426 reply_with_error ("progress: expecting arg (time in seconds as string)");
430 char *secs_str = argv[0];
432 if (sscanf (secs_str, "%u", &secs) != 1 || secs == 0)
436 unsigned tsecs = secs * 10; /* 1/10ths of seconds */
437 for (i = 1; i <= tsecs; ++i) {
439 notify_progress ((uint64_t) i, (uint64_t) tsecs);
442 char *ret = strdup ("ok");
444 reply_with_perror ("strdup");
451 /* Enable core dumping to the given core pattern.
452 * Note that this pattern is relative to any chroot of the process which
453 * crashes. This means that if you want to write the core file to the guest's
454 * storage the pattern must start with /sysroot only if the command which
455 * crashes doesn't chroot.
458 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
461 reply_with_error ("core_pattern: expecting a core pattern");
465 const char *pattern = argv[0];
466 const size_t pattern_len = strlen(pattern);
468 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
469 int fd = open (CORE_PATTERN, O_WRONLY);
471 reply_with_perror ("open: " CORE_PATTERN);
474 if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
475 reply_with_perror ("write: " CORE_PATTERN);
478 if (close (fd) == -1) {
479 reply_with_perror ("close: " CORE_PATTERN);
483 struct rlimit limit = {
484 .rlim_cur = RLIM_INFINITY,
485 .rlim_max = RLIM_INFINITY
487 if (setrlimit (RLIMIT_CORE, &limit) == -1) {
488 reply_with_perror ("setrlimit (RLIMIT_CORE)");
492 char *ret = strdup ("ok");
494 reply_with_perror ("strdup");
502 write_cb (void *fd_ptr, const void *buf, size_t len)
504 int fd = *(int *)fd_ptr;
505 return xwrite (fd, buf, len);
508 /* This requires a non-upstream qemu patch. See contrib/visualize-alignment/
509 * directory in the libguestfs source tree.
512 debug_qtrace (const char *subcmd, int argc, char *const *const argv)
518 reply_with_error ("qtrace <device> <on|off>");
522 if (STREQ (argv[1], "on"))
524 else if (STREQ (argv[1], "off"))
529 /* This does a sync and flushes all caches. */
530 if (do_drop_caches (3) == -1)
533 /* Note this doesn't do device name translation or check this is a device. */
534 int fd = open (argv[0], O_RDONLY | O_DIRECT);
536 reply_with_perror ("qtrace: %s: open", argv[0]);
540 /* The pattern of reads is what signals to the analysis program that
541 * tracing should be started or stopped. Note this assumes both 512
542 * byte sectors, and that O_DIRECT will let us do 512 byte aligned
543 * reads. We ought to read the sector size of the device and use
544 * that instead (XXX). The analysis program currently assumes 512
545 * byte sectors anyway.
547 #define QTRACE_SIZE 512
548 const int patterns[2][5] = {
549 { 2, 15, 21, 2, -1 }, /* disable trace */
550 { 2, 21, 15, 2, -1 } /* enable trace */
555 /* For O_DIRECT, buffer must be aligned too (thanks Matt).
556 * Note posix_memalign has this strange errno behaviour.
558 errno = posix_memalign (&buf, QTRACE_SIZE, QTRACE_SIZE);
560 reply_with_perror ("posix_memalign");
565 for (i = 0; patterns[enable][i] >= 0; ++i) {
566 if (lseek (fd, patterns[enable][i]*QTRACE_SIZE, SEEK_SET) == -1) {
567 reply_with_perror ("qtrace: %s: lseek", argv[0]);
573 if (read (fd, buf, QTRACE_SIZE) == -1) {
574 reply_with_perror ("qtrace: %s: read", argv[0]);
584 /* This does a sync and flushes all caches. */
585 if (do_drop_caches (3) == -1)
588 char *ret = strdup ("ok");
590 reply_with_perror ("strdup");
597 /* Has one FileIn parameter. */
599 do_debug_upload (const char *filename, int mode)
601 /* Not chrooted - this command lets you upload a file to anywhere
604 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
610 reply_with_perror ("%s", filename);
614 int r = receive_file (write_cb, &fd);
615 if (r == -1) { /* write error */
619 reply_with_error ("write error: %s", filename);
623 if (r == -2) { /* cancellation from library */
624 /* This error is ignored by the library since it initiated the
625 * cancel. Nevertheless we must send an error reply here.
627 reply_with_error ("file upload cancelled");
632 if (close (fd) == -1) {
633 reply_with_perror ("close: %s", filename);