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., 675 Mass Ave, Cambridge, MA 02139, 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' 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. See regressions/rhbz727178.sh */
285 debug_binaries (const char *subcmd, int argc, char *const *const argv)
291 "find / -xdev -type f -executable "
293 "| grep application/x-executable "
294 "| gawk -F: '{print $1}'";
296 r = command (&out, &err, "sh", "-c", cmd, NULL);
298 reply_with_error ("find: %s", err);
309 /* Run 'ldd' on a file from the appliance. See regressions/rhbz727178.sh */
311 debug_ldd (const char *subcmd, int argc, char *const *const argv)
314 char *out, *err, *ret;
317 reply_with_error ("ldd: no file argument");
321 /* Note that 'ldd' doesn't fail if it finds errors. We have to grep
322 * for errors in the regression test instead. 'ldd' only fails here
323 * if the binary is not a binary at all (eg. for shell scripts).
324 * Also 'ldd' randomly sends messages to stderr and errors to stdout
325 * depending on the phase of the moon.
327 r = command (&out, &err, "ldd", "-r", argv[0], NULL);
329 reply_with_error ("ldd: %s: %s", argv[0], err);
335 /* Concatenate stdout and stderr in the result. */
336 ret = realloc (out, strlen (out) + strlen (err) + 1);
338 reply_with_perror ("realloc");
350 /* List files in the appliance. */
352 debug_ls (const char *subcmd, int argc, char *const *const argv)
354 int len = count_strings (argv);
355 const char *cargv[len+3];
360 for (i = 0; i < len; ++i)
361 cargv[2+i] = argv[i];
367 r = commandv (&out, &err, (void *) cargv);
369 reply_with_error ("ls: %s", err);
380 /* List files in the appliance. */
382 debug_ll (const char *subcmd, int argc, char *const *const argv)
384 int len = count_strings (argv);
385 const char *cargv[len+3];
390 for (i = 0; i < len; ++i)
391 cargv[2+i] = argv[i];
397 r = commandv (&out, &err, (void *) cargv);
399 reply_with_error ("ll: %s", err);
410 /* Generate progress notification messages in order to test progress bars. */
412 debug_progress (const char *subcmd, int argc, char *const *const argv)
416 reply_with_error ("progress: expecting arg (time in seconds as string)");
420 char *secs_str = argv[0];
422 if (sscanf (secs_str, "%u", &secs) != 1 || secs == 0)
426 unsigned tsecs = secs * 10; /* 1/10ths of seconds */
427 for (i = 1; i <= tsecs; ++i) {
429 notify_progress ((uint64_t) i, (uint64_t) tsecs);
432 char *ret = strdup ("ok");
434 reply_with_perror ("strdup");
441 /* Enable core dumping to the given core pattern.
442 * Note that this pattern is relative to any chroot of the process which
443 * crashes. This means that if you want to write the core file to the guest's
444 * storage the pattern must start with /sysroot only if the command which
445 * crashes doesn't chroot.
448 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
451 reply_with_error ("core_pattern: expecting a core pattern");
455 const char *pattern = argv[0];
456 const size_t pattern_len = strlen(pattern);
458 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
459 int fd = open (CORE_PATTERN, O_WRONLY);
461 reply_with_perror ("open: " CORE_PATTERN);
464 if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
465 reply_with_perror ("write: " CORE_PATTERN);
468 if (close (fd) == -1) {
469 reply_with_perror ("close: " CORE_PATTERN);
473 struct rlimit limit = {
474 .rlim_cur = RLIM_INFINITY,
475 .rlim_max = RLIM_INFINITY
477 if (setrlimit (RLIMIT_CORE, &limit) == -1) {
478 reply_with_perror ("setrlimit (RLIMIT_CORE)");
482 char *ret = strdup ("ok");
484 reply_with_perror ("strdup");
492 write_cb (void *fd_ptr, const void *buf, size_t len)
494 int fd = *(int *)fd_ptr;
495 return xwrite (fd, buf, len);
498 /* This requires a non-upstream qemu patch. See contrib/visualize-alignment/
499 * directory in the libguestfs source tree.
502 debug_qtrace (const char *subcmd, int argc, char *const *const argv)
508 reply_with_error ("qtrace <device> <on|off>");
512 if (STREQ (argv[1], "on"))
514 else if (STREQ (argv[1], "off"))
519 /* This does a sync and flushes all caches. */
520 if (do_drop_caches (3) == -1)
523 /* Note this doesn't do device name translation or check this is a device. */
524 int fd = open (argv[0], O_RDONLY | O_DIRECT);
526 reply_with_perror ("qtrace: %s: open", argv[0]);
530 /* The pattern of reads is what signals to the analysis program that
531 * tracing should be started or stopped. Note this assumes both 512
532 * byte sectors, and that O_DIRECT will let us do 512 byte aligned
533 * reads. We ought to read the sector size of the device and use
534 * that instead (XXX). The analysis program currently assumes 512
535 * byte sectors anyway.
537 #define QTRACE_SIZE 512
538 const int patterns[2][5] = {
539 { 2, 15, 21, 2, -1 }, /* disable trace */
540 { 2, 21, 15, 2, -1 } /* enable trace */
545 /* For O_DIRECT, buffer must be aligned too (thanks Matt).
546 * Note posix_memalign has this strange errno behaviour.
548 errno = posix_memalign (&buf, QTRACE_SIZE, QTRACE_SIZE);
550 reply_with_perror ("posix_memalign");
555 for (i = 0; patterns[enable][i] >= 0; ++i) {
556 if (lseek (fd, patterns[enable][i]*QTRACE_SIZE, SEEK_SET) == -1) {
557 reply_with_perror ("qtrace: %s: lseek", argv[0]);
563 if (read (fd, buf, QTRACE_SIZE) == -1) {
564 reply_with_perror ("qtrace: %s: read", argv[0]);
574 /* This does a sync and flushes all caches. */
575 if (do_drop_caches (3) == -1)
578 char *ret = strdup ("ok");
580 reply_with_perror ("strdup");
587 /* Has one FileIn parameter. */
589 do_debug_upload (const char *filename, int mode)
591 /* Not chrooted - this command lets you upload a file to anywhere
594 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
600 reply_with_perror ("%s", filename);
604 int r = receive_file (write_cb, &fd);
605 if (r == -1) { /* write error */
609 reply_with_error ("write error: %s", filename);
613 if (r == -2) { /* cancellation from library */
614 /* This error is ignored by the library since it initiated the
615 * cancel. Nevertheless we must send an error reply here.
617 reply_with_error ("file upload cancelled");
622 if (close (fd) == -1) {
623 reply_with_perror ("close: %s", filename);