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_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);
64 static struct cmd cmds[] = {
65 { "help", debug_help },
66 { "core_pattern", debug_core_pattern },
71 { "progress", debug_progress },
72 { "qtrace", debug_qtrace },
73 { "segv", debug_segv },
79 do_debug (const char *subcmd, char *const *argv)
83 for (i = argc = 0; argv[i] != NULL; ++i)
86 for (i = 0; cmds[i].cmd != NULL; ++i) {
87 if (STRCASEEQ (subcmd, cmds[i].cmd))
88 return cmds[i].f (subcmd, argc, argv);
91 reply_with_error ("use 'debug help' to list the supported commands");
96 debug_help (const char *subcmd, int argc, char *const *const argv)
101 r = strdup ("Commands supported:");
103 reply_with_perror ("strdup");
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 */
112 reply_with_perror ("realloc");
119 strcat (r, cmds[i].cmd);
127 debug_fds (const char *subcmd, int argc, char *const *const argv)
135 char fname[256], link[256];
138 fp = open_memstream (&out, &size);
140 reply_with_perror ("open_memstream");
144 dir = opendir ("/proc/self/fd");
146 reply_with_perror ("opendir: /proc/self/fd");
151 while ((d = readdir (dir)) != NULL) {
152 if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
155 snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
157 r = lstat (fname, &statbuf);
159 reply_with_perror ("stat: %s", fname);
166 if (S_ISLNK (statbuf.st_mode)) {
167 r = readlink (fname, link, sizeof link - 1);
169 reply_with_perror ("readline: %s", fname);
177 fprintf (fp, "%2s %s\n", d->d_name, link);
179 fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
184 if (closedir (dir) == -1) {
185 reply_with_perror ("closedir");
193 /* Force a segfault in the daemon. */
195 debug_segv (const char *subcmd, int argc, char *const *const argv)
201 /* Run an arbitrary shell command using /bin/sh from the appliance.
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.
207 debug_sh (const char *subcmd, int argc, char *const *const argv)
210 reply_with_error ("sh: expecting a command to run");
217 /* guestfish splits the parameter(s) into a list of strings,
218 * and we have to reassemble them here. Not ideal. XXX
220 for (i = len = 0; i < argc; ++i)
221 len += strlen (argv[i]) + 1;
224 reply_with_perror ("malloc");
227 for (i = j = 0; i < argc; ++i) {
228 len = strlen (argv[i]);
229 memcpy (&cmd[j], argv[i], len);
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);
246 int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
247 "/bin/sh", "-c", cmd, NULL);
251 reply_with_error ("%s", err);
259 /* Print the environment that commands get (by running external printenv). */
261 debug_env (const char *subcmd, int argc, char *const *const argv)
266 r = command (&out, &err, "printenv", NULL);
268 reply_with_error ("printenv: %s", err);
279 /* List files in the appliance. */
281 debug_ls (const char *subcmd, int argc, char *const *const argv)
283 int len = count_strings (argv);
284 const char *cargv[len+3];
289 for (i = 0; i < len; ++i)
290 cargv[2+i] = argv[i];
296 r = commandv (&out, &err, (void *) cargv);
298 reply_with_error ("ls: %s", err);
309 /* List files in the appliance. */
311 debug_ll (const char *subcmd, int argc, char *const *const argv)
313 int len = count_strings (argv);
314 const char *cargv[len+3];
319 for (i = 0; i < len; ++i)
320 cargv[2+i] = argv[i];
326 r = commandv (&out, &err, (void *) cargv);
328 reply_with_error ("ll: %s", err);
339 /* Generate progress notification messages in order to test progress bars. */
341 debug_progress (const char *subcmd, int argc, char *const *const argv)
345 reply_with_error ("progress: expecting arg (time in seconds as string)");
349 char *secs_str = argv[0];
351 if (sscanf (secs_str, "%u", &secs) != 1 || secs == 0)
355 unsigned tsecs = secs * 10; /* 1/10ths of seconds */
356 for (i = 1; i <= tsecs; ++i) {
358 notify_progress ((uint64_t) i, (uint64_t) tsecs);
361 char *ret = strdup ("ok");
363 reply_with_perror ("strdup");
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.
377 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
380 reply_with_error ("core_pattern: expecting a core pattern");
384 const char *pattern = argv[0];
385 const size_t pattern_len = strlen(pattern);
387 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
388 int fd = open (CORE_PATTERN, O_WRONLY);
390 reply_with_perror ("open: " CORE_PATTERN);
393 if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
394 reply_with_perror ("write: " CORE_PATTERN);
397 if (close (fd) == -1) {
398 reply_with_perror ("close: " CORE_PATTERN);
402 struct rlimit limit = {
403 .rlim_cur = RLIM_INFINITY,
404 .rlim_max = RLIM_INFINITY
406 if (setrlimit (RLIMIT_CORE, &limit) == -1) {
407 reply_with_perror ("setrlimit (RLIMIT_CORE)");
411 char *ret = strdup ("ok");
413 reply_with_perror ("strdup");
421 write_cb (void *fd_ptr, const void *buf, size_t len)
423 int fd = *(int *)fd_ptr;
424 return xwrite (fd, buf, len);
427 /* This requires a non-upstream qemu patch. See contrib/visualize-alignment/
428 * directory in the libguestfs source tree.
431 debug_qtrace (const char *subcmd, int argc, char *const *const argv)
437 reply_with_error ("qtrace <device> <on|off>");
441 if (STREQ (argv[1], "on"))
443 else if (STREQ (argv[1], "off"))
448 /* This does a sync and flushes all caches. */
449 if (do_drop_caches (3) == -1)
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);
455 reply_with_perror ("qtrace: %s: open", argv[0]);
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.
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 */
474 /* For O_DIRECT, buffer must be aligned too (thanks Matt).
475 * Note posix_memalign has this strange errno behaviour.
477 errno = posix_memalign (&buf, QTRACE_SIZE, QTRACE_SIZE);
479 reply_with_perror ("posix_memalign");
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]);
492 if (read (fd, buf, QTRACE_SIZE) == -1) {
493 reply_with_perror ("qtrace: %s: read", argv[0]);
503 /* This does a sync and flushes all caches. */
504 if (do_drop_caches (3) == -1)
507 char *ret = strdup ("ok");
509 reply_with_perror ("strdup");
516 /* Has one FileIn parameter. */
518 do_debug_upload (const char *filename, int mode)
520 /* Not chrooted - this command lets you upload a file to anywhere
523 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
529 reply_with_perror ("%s", filename);
533 int r = receive_file (write_cb, &fd);
534 if (r == -1) { /* write error */
538 reply_with_error ("write error: %s", filename);
542 if (r == -2) { /* cancellation from library */
543 /* This error is ignored by the library since it initiated the
544 * cancel. Nevertheless we must send an error reply here.
546 reply_with_error ("file upload cancelled");
551 if (close (fd) == -1) {
553 if (r == -1) /* if r == 0, file transfer ended already */
556 reply_with_perror ("close: %s", filename);