debug: Add 'debug progress' command.
[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_segv (const char *subcmd, int argc, char *const *const argv);
61 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
62
63 static struct cmd cmds[] = {
64   { "help", debug_help },
65   { "core_pattern", debug_core_pattern },
66   { "env", debug_env },
67   { "fds", debug_fds },
68   { "ls", debug_ls },
69   { "ll", debug_ll },
70   { "progress", debug_progress },
71   { "segv", debug_segv },
72   { "sh", debug_sh },
73   { NULL, NULL }
74 };
75
76 char *
77 do_debug (const char *subcmd, char *const *argv)
78 {
79   int argc, i;
80
81   for (i = argc = 0; argv[i] != NULL; ++i)
82     argc++;
83
84   for (i = 0; cmds[i].cmd != NULL; ++i) {
85     if (STRCASEEQ (subcmd, cmds[i].cmd))
86       return cmds[i].f (subcmd, argc, argv);
87   }
88
89   reply_with_error ("use 'debug help' to list the supported commands");
90   return NULL;
91 }
92
93 static char *
94 debug_help (const char *subcmd, int argc, char *const *const argv)
95 {
96   int len, i;
97   char *r, *p;
98
99   r = strdup ("Commands supported:");
100   if (!r) {
101     reply_with_perror ("strdup");
102     return NULL;
103   }
104
105   len = strlen (r);
106   for (i = 0; cmds[i].cmd != NULL; ++i) {
107     len += strlen (cmds[i].cmd) + 1; /* space + new command */
108     p = realloc (r, len + 1);        /* +1 for the final NUL */
109     if (p == NULL) {
110       reply_with_perror ("realloc");
111       free (r);
112       return NULL;
113     }
114     r = p;
115
116     strcat (r, " ");
117     strcat (r, cmds[i].cmd);
118   }
119
120   return r;
121 }
122
123 /* Show open FDs. */
124 static char *
125 debug_fds (const char *subcmd, int argc, char *const *const argv)
126 {
127   int r;
128   char *out;
129   size_t size;
130   FILE *fp;
131   DIR *dir;
132   struct dirent *d;
133   char fname[256], link[256];
134   struct stat statbuf;
135
136   fp = open_memstream (&out, &size);
137   if (!fp) {
138     reply_with_perror ("open_memstream");
139     return NULL;
140   }
141
142   dir = opendir ("/proc/self/fd");
143   if (!dir) {
144     reply_with_perror ("opendir: /proc/self/fd");
145     fclose (fp);
146     return NULL;
147   }
148
149   while ((d = readdir (dir)) != NULL) {
150     if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
151       continue;
152
153     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
154
155     r = lstat (fname, &statbuf);
156     if (r == -1) {
157       reply_with_perror ("stat: %s", fname);
158       fclose (fp);
159       free (out);
160       closedir (dir);
161       return NULL;
162     }
163
164     if (S_ISLNK (statbuf.st_mode)) {
165       r = readlink (fname, link, sizeof link - 1);
166       if (r == -1) {
167         reply_with_perror ("readline: %s", fname);
168         fclose (fp);
169         free (out);
170         closedir (dir);
171         return NULL;
172       }
173       link[r] = '\0';
174
175       fprintf (fp, "%2s %s\n", d->d_name, link);
176     } else
177       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
178   }
179
180   fclose (fp);
181
182   if (closedir (dir) == -1) {
183     reply_with_perror ("closedir");
184     free (out);
185     return NULL;
186   }
187
188   return out;
189 }
190
191 /* Force a segfault in the daemon. */
192 static char *
193 debug_segv (const char *subcmd, int argc, char *const *const argv)
194 {
195   *(int*)0 = 0;
196   return NULL;
197 }
198
199 /* Run an arbitrary shell command using /bin/sh from the appliance.
200  *
201  * Note this is somewhat different from the ordinary guestfs_sh command
202  * because it's not using the guest shell, and is not chrooted.
203  */
204 static char *
205 debug_sh (const char *subcmd, int argc, char *const *const argv)
206 {
207   if (argc < 1) {
208     reply_with_error ("sh: expecting a command to run");
209     return NULL;
210   }
211
212   char *cmd;
213   int len, i, j;
214
215   /* guestfish splits the parameter(s) into a list of strings,
216    * and we have to reassemble them here.  Not ideal. XXX
217    */
218   for (i = len = 0; i < argc; ++i)
219     len += strlen (argv[i]) + 1;
220   cmd = malloc (len);
221   if (!cmd) {
222     reply_with_perror ("malloc");
223     return NULL;
224   }
225   for (i = j = 0; i < argc; ++i) {
226     len = strlen (argv[i]);
227     memcpy (&cmd[j], argv[i], len);
228     j += len;
229     cmd[j] = ' ';
230     j++;
231   }
232   cmd[j-1] = '\0';
233
234   /* Set up some environment variables. */
235   setenv ("root", sysroot, 1);
236   if (access ("/sys/block/sda", F_OK) == 0)
237     setenv ("sd", "sd", 1);
238   else if (access ("/sys/block/hda", F_OK) == 0)
239     setenv ("sd", "hd", 1);
240   else if (access ("/sys/block/vda", F_OK) == 0)
241     setenv ("sd", "vd", 1);
242
243   char *err;
244   int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
245                     "/bin/sh", "-c", cmd, NULL);
246   free (cmd);
247
248   if (r == -1) {
249     reply_with_error ("%s", err);
250     free (err);
251     return NULL;
252   }
253
254   return err;
255 }
256
257 /* Print the environment that commands get (by running external printenv). */
258 static char *
259 debug_env (const char *subcmd, int argc, char *const *const argv)
260 {
261   int r;
262   char *out, *err;
263
264   r = command (&out, &err, "printenv", NULL);
265   if (r == -1) {
266     reply_with_error ("printenv: %s", err);
267     free (out);
268     free (err);
269     return NULL;
270   }
271
272   free (err);
273
274   return out;
275 }
276
277 /* List files in the appliance. */
278 static char *
279 debug_ls (const char *subcmd, int argc, char *const *const argv)
280 {
281   int len = count_strings (argv);
282   const char *cargv[len+3];
283   int i;
284
285   cargv[0] = "ls";
286   cargv[1] = "-a";
287   for (i = 0; i < len; ++i)
288     cargv[2+i] = argv[i];
289   cargv[2+len] = NULL;
290
291   int r;
292   char *out, *err;
293
294   r = commandv (&out, &err, (void *) cargv);
295   if (r == -1) {
296     reply_with_error ("ls: %s", err);
297     free (out);
298     free (err);
299     return NULL;
300   }
301
302   free (err);
303
304   return out;
305 }
306
307 /* List files in the appliance. */
308 static char *
309 debug_ll (const char *subcmd, int argc, char *const *const argv)
310 {
311   int len = count_strings (argv);
312   const char *cargv[len+3];
313   int i;
314
315   cargv[0] = "ls";
316   cargv[1] = "-la";
317   for (i = 0; i < len; ++i)
318     cargv[2+i] = argv[i];
319   cargv[2+len] = NULL;
320
321   int r;
322   char *out, *err;
323
324   r = commandv (&out, &err, (void *) cargv);
325   if (r == -1) {
326     reply_with_error ("ll: %s", err);
327     free (out);
328     free (err);
329     return NULL;
330   }
331
332   free (err);
333
334   return out;
335 }
336
337 /* Generate progress notification messages in order to test progress bars. */
338 static char *
339 debug_progress (const char *subcmd, int argc, char *const *const argv)
340 {
341   if (argc < 1) {
342   error:
343     reply_with_error ("progress: expecting arg (time in seconds as string)");
344     return NULL;
345   }
346
347   char *secs_str = argv[0];
348   unsigned secs;
349   if (sscanf (secs_str, "%u", &secs) != 1 || secs == 0)
350     goto error;
351
352   unsigned i;
353   unsigned tsecs = secs * 10;   /* 1/10ths of seconds */
354   for (i = 1; i <= tsecs; ++i) {
355     usleep (100000);
356     notify_progress ((uint64_t) i, (uint64_t) tsecs);
357   }
358
359   char *ret = strdup ("ok");
360   if (ret == NULL) {
361     reply_with_perror ("strdup");
362     return NULL;
363   }
364
365   return ret;
366 }
367
368 /* Enable core dumping to the given core pattern.
369  * Note that this pattern is relative to any chroot of the process which
370  * crashes. This means that if you want to write the core file to the guest's
371  * storage the pattern must start with /sysroot only if the command which
372  * crashes doesn't chroot.
373  */
374 static char *
375 debug_core_pattern (const char *subcmd, int argc, char *const *const argv)
376 {
377   if (argc < 1) {
378     reply_with_error ("core_pattern: expecting a core pattern");
379     return NULL;
380   }
381
382   const char *pattern = argv[0];
383   const size_t pattern_len = strlen(pattern);
384
385 #define CORE_PATTERN "/proc/sys/kernel/core_pattern"
386   int fd = open (CORE_PATTERN, O_WRONLY);
387   if (fd == -1) {
388     reply_with_perror ("open: " CORE_PATTERN);
389     return NULL;
390   }
391   if (write (fd, pattern, pattern_len) < (ssize_t) pattern_len) {
392     reply_with_perror ("write: " CORE_PATTERN);
393     return NULL;
394   }
395   if (close (fd) == -1) {
396     reply_with_perror ("close: " CORE_PATTERN);
397     return NULL;
398   }
399
400   struct rlimit limit = {
401     .rlim_cur = RLIM_INFINITY,
402     .rlim_max = RLIM_INFINITY
403   };
404   if (setrlimit (RLIMIT_CORE, &limit) == -1) {
405     reply_with_perror ("setrlimit (RLIMIT_CORE)");
406     return NULL;
407   }
408
409   char *ret = strdup ("ok");
410   if (NULL == ret) {
411     reply_with_perror ("strdup");
412     return NULL;
413   }
414
415   return ret;
416 }
417
418 static int
419 write_cb (void *fd_ptr, const void *buf, size_t len)
420 {
421   int fd = *(int *)fd_ptr;
422   return xwrite (fd, buf, len);
423 }
424
425 /* Has one FileIn parameter. */
426 int
427 do_debug_upload (const char *filename, int mode)
428 {
429   /* Not chrooted - this command lets you upload a file to anywhere
430    * in the appliance.
431    */
432   int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
433
434   if (fd == -1) {
435     int err = errno;
436     cancel_receive ();
437     errno = err;
438     reply_with_perror ("%s", filename);
439     return -1;
440   }
441
442   int r = receive_file (write_cb, &fd);
443   if (r == -1) {                /* write error */
444     int err = errno;
445     cancel_receive ();
446     errno = err;
447     reply_with_error ("write error: %s", filename);
448     close (fd);
449     return -1;
450   }
451   if (r == -2) {                /* cancellation from library */
452     close (fd);
453     /* Do NOT send any error. */
454     return -1;
455   }
456
457   if (close (fd) == -1) {
458     int err = errno;
459     if (r == -1)                /* if r == 0, file transfer ended already */
460       cancel_receive ();
461     errno = err;
462     reply_with_perror ("close: %s", filename);
463     return -1;
464   }
465
466   return 0;
467 }