lib: Add guestfs___remove_tmpdir helper function.
[libguestfs.git] / src / guestfs.c
1 /* libguestfs
2  * Copyright (C) 2009-2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <sys/stat.h>
34 #include <sys/select.h>
35 #include <dirent.h>
36 #include <assert.h>
37
38 #include <rpc/types.h>
39 #include <rpc/xdr.h>
40
41 #ifdef HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
47 #endif
48
49 #ifdef HAVE_SYS_WAIT_H
50 #include <sys/wait.h>
51 #endif
52
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
55 #endif
56
57 #ifdef HAVE_SYS_UN_H
58 #include <sys/un.h>
59 #endif
60
61 #include <arpa/inet.h>
62 #include <netinet/in.h>
63
64 #include "c-ctype.h"
65 #include "glthread/lock.h"
66 #include "hash.h"
67 #include "hash-pjw.h"
68
69 #include "guestfs.h"
70 #include "guestfs-internal.h"
71 #include "guestfs-internal-actions.h"
72 #include "guestfs_protocol.h"
73
74 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
75 static void close_handles (void);
76
77 gl_lock_define_initialized (static, handles_lock);
78 static guestfs_h *handles = NULL;
79 static int atexit_handler_set = 0;
80
81 guestfs_h *
82 guestfs_create (void)
83 {
84   guestfs_h *g;
85   const char *str;
86
87   g = malloc (sizeof (*g));
88   if (!g) return NULL;
89
90   memset (g, 0, sizeof (*g));
91
92   g->state = CONFIG;
93
94   g->fd[0] = -1;
95   g->fd[1] = -1;
96   g->sock = -1;
97
98   g->abort_cb = abort;
99   g->error_cb = default_error_cb;
100   g->error_cb_data = NULL;
101
102   g->recovery_proc = 1;
103   g->autosync = 1;
104
105   str = getenv ("LIBGUESTFS_DEBUG");
106   g->verbose = str != NULL && STREQ (str, "1");
107
108   str = getenv ("LIBGUESTFS_TRACE");
109   g->trace = str != NULL && STREQ (str, "1");
110
111   str = getenv ("LIBGUESTFS_PATH");
112   g->path = str != NULL ? strdup (str) : strdup (GUESTFS_DEFAULT_PATH);
113   if (!g->path) goto error;
114
115   str = getenv ("LIBGUESTFS_QEMU");
116   g->qemu = str != NULL ? strdup (str) : strdup (QEMU);
117   if (!g->qemu) goto error;
118
119   str = getenv ("LIBGUESTFS_APPEND");
120   if (str) {
121     g->append = strdup (str);
122     if (!g->append) goto error;
123   }
124
125   /* Choose a suitable memory size.  Previously we tried to choose
126    * a minimal memory size, but this isn't really necessary since
127    * recent QEMU and KVM don't do anything nasty like locking
128    * memory into core any more.  Thus we can safely choose a
129    * large, generous amount of memory, and it'll just get swapped
130    * on smaller systems.
131    */
132   str = getenv ("LIBGUESTFS_MEMSIZE");
133   if (str) {
134     if (sscanf (str, "%d", &g->memsize) != 1 || g->memsize <= 256) {
135       warning (g, "non-numeric or too small value for LIBGUESTFS_MEMSIZE");
136       goto error;
137     }
138   } else
139     g->memsize = 500;
140
141   /* Start with large serial numbers so they are easy to spot
142    * inside the protocol.
143    */
144   g->msg_next_serial = 0x00123400;
145
146   /* Default is uniprocessor appliance. */
147   g->smp = 1;
148
149   /* Link the handles onto a global list. */
150   gl_lock_lock (handles_lock);
151   g->next = handles;
152   handles = g;
153   if (!atexit_handler_set) {
154     atexit (close_handles);
155     atexit_handler_set = 1;
156   }
157   gl_lock_unlock (handles_lock);
158
159   debug (g, "new guestfs handle %p", g);
160
161   return g;
162
163  error:
164   free (g->path);
165   free (g->qemu);
166   free (g->append);
167   free (g);
168   return NULL;
169 }
170
171 void
172 guestfs_close (guestfs_h *g)
173 {
174   if (g->state == NO_HANDLE) {
175     /* Not safe to call ANY callbacks here, so ... */
176     fprintf (stderr, _("guestfs_close: called twice on the same handle\n"));
177     return;
178   }
179
180   if (g->trace) {
181     const char trace_msg[] = "close";
182
183     guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE,
184                                       trace_msg, strlen (trace_msg));
185   }
186
187   debug (g, "closing guestfs handle %p (state %d)", g, g->state);
188
189   /* Try to sync if autosync flag is set. */
190   if (g->autosync && g->state == READY)
191     guestfs_internal_autosync (g);
192
193   /* Kill the qemu subprocess. */
194   if (g->state != CONFIG)
195     guestfs_kill_subprocess (g);
196
197   /* Run user close callbacks. */
198   guestfs___call_callbacks_void (g, GUESTFS_EVENT_CLOSE);
199
200   /* Remove all other registered callbacks.  Since we've already
201    * called the close callbacks, we shouldn't call any others.
202    */
203   free (g->events);
204   g->nr_events = 0;
205   g->events = NULL;
206
207   guestfs___free_inspect_info (g);
208   guestfs___free_drives (&g->drives);
209
210   /* Close sockets. */
211   if (g->fd[0] >= 0)
212     close (g->fd[0]);
213   if (g->fd[1] >= 0)
214     close (g->fd[1]);
215   if (g->sock >= 0)
216     close (g->sock);
217   g->fd[0] = -1;
218   g->fd[1] = -1;
219   g->sock = -1;
220
221   /* Wait for subprocess(es) to exit. */
222   if (g->pid > 0) waitpid (g->pid, NULL, 0);
223   if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
224
225   /* Remove whole temporary directory. */
226   guestfs___remove_tmpdir (g->tmpdir);
227   free (g->tmpdir);
228
229   if (g->cmdline) {
230     size_t i;
231
232     for (i = 0; i < g->cmdline_size; ++i)
233       free (g->cmdline[i]);
234     free (g->cmdline);
235   }
236
237   /* Mark the handle as dead before freeing it. */
238   g->state = NO_HANDLE;
239
240   gl_lock_lock (handles_lock);
241   if (handles == g)
242     handles = g->next;
243   else {
244     guestfs_h *gg;
245
246     for (gg = handles; gg->next != g; gg = gg->next)
247       ;
248     gg->next = g->next;
249   }
250   gl_lock_unlock (handles_lock);
251
252   if (g->pda)
253     hash_free (g->pda);
254   free (g->last_error);
255   free (g->path);
256   free (g->qemu);
257   free (g->append);
258   free (g->qemu_help);
259   free (g->qemu_version);
260   free (g);
261 }
262
263 /* Close all open handles (called from atexit(3)). */
264 static void
265 close_handles (void)
266 {
267   while (handles) guestfs_close (handles);
268 }
269
270 const char *
271 guestfs_last_error (guestfs_h *g)
272 {
273   return g->last_error;
274 }
275
276 int
277 guestfs_last_errno (guestfs_h *g)
278 {
279   return g->last_errnum;
280 }
281
282 static void
283 set_last_error (guestfs_h *g, int errnum, const char *msg)
284 {
285   free (g->last_error);
286   g->last_error = strdup (msg);
287   g->last_errnum = errnum;
288 }
289
290 /* Warning are printed unconditionally.  We try to make these rare.
291  * Generally speaking, a warning should either be an error, or if it's
292  * not important for end users then it should be a debug message.
293  */
294 void
295 guestfs___warning (guestfs_h *g, const char *fs, ...)
296 {
297   va_list args;
298   char *msg, *msg2;
299   int len;
300
301   va_start (args, fs);
302   len = vasprintf (&msg, fs, args);
303   va_end (args);
304
305   if (len < 0) return;
306
307   len = asprintf (&msg2, _("warning: %s"), msg);
308   free (msg);
309
310   if (len < 0) return;
311
312   guestfs___call_callbacks_message (g, GUESTFS_EVENT_LIBRARY, msg2, len);
313
314   free (msg2);
315 }
316
317 /* Debug messages. */
318 void
319 guestfs___debug (guestfs_h *g, const char *fs, ...)
320 {
321   va_list args;
322   char *msg;
323   int len;
324
325   /* The cpp macro "debug" has already checked that g->verbose is true
326    * before calling this function, but we check it again just in case
327    * anyone calls this function directly.
328    */
329   if (!g->verbose)
330     return;
331
332   va_start (args, fs);
333   len = vasprintf (&msg, fs, args);
334   va_end (args);
335
336   if (len < 0) return;
337
338   guestfs___call_callbacks_message (g, GUESTFS_EVENT_LIBRARY, msg, len);
339
340   free (msg);
341 }
342
343 /* Call trace messages.  These are enabled by setting g->trace, and
344  * calls to this function should only happen from the generated code
345  * in src/actions.c
346  */
347 void
348 guestfs___trace (guestfs_h *g, const char *fs, ...)
349 {
350   va_list args;
351   char *msg;
352   int len;
353
354   va_start (args, fs);
355   len = vasprintf (&msg, fs, args);
356   va_end (args);
357
358   if (len < 0) return;
359
360   guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE, msg, len);
361
362   free (msg);
363 }
364
365 static void
366 default_error_cb (guestfs_h *g, void *data, const char *msg)
367 {
368   fprintf (stderr, _("libguestfs: error: %s\n"), msg);
369 }
370
371 void
372 guestfs_error_errno (guestfs_h *g, int errnum, const char *fs, ...)
373 {
374   va_list args;
375   char *msg;
376
377   va_start (args, fs);
378   int err = vasprintf (&msg, fs, args);
379   va_end (args);
380
381   if (err < 0) return;
382
383   /* set_last_error first so that the callback can access the error
384    * message and errno through the handle if it wishes.
385    */
386   set_last_error (g, errnum, msg);
387   if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
388
389   free (msg);
390 }
391
392 void
393 guestfs_perrorf (guestfs_h *g, const char *fs, ...)
394 {
395   va_list args;
396   char *msg;
397   int errnum = errno;
398
399   va_start (args, fs);
400   int err = vasprintf (&msg, fs, args);
401   va_end (args);
402
403   if (err < 0) return;
404
405   char buf[256];
406   strerror_r (errnum, buf, sizeof buf);
407
408   msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
409   strcat (msg, ": ");
410   strcat (msg, buf);
411
412   /* set_last_error first so that the callback can access the error
413    * message and errno through the handle if it wishes.
414    */
415   set_last_error (g, errnum, msg);
416   if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
417
418   free (msg);
419 }
420
421 void *
422 guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
423 {
424   void *ptr = malloc (nbytes);
425   if (nbytes > 0 && !ptr) g->abort_cb ();
426   return ptr;
427 }
428
429 /* Return 1 if an array of N objects, each of size S, cannot exist due
430    to size arithmetic overflow.  S must be positive and N must be
431    nonnegative.  This is a macro, not an inline function, so that it
432    works correctly even when SIZE_MAX < N.
433
434    By gnulib convention, SIZE_MAX represents overflow in size
435    calculations, so the conservative dividend to use here is
436    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
437    However, malloc (SIZE_MAX) fails on all known hosts where
438    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
439    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
440    branch when S is known to be 1.  */
441 # define xalloc_oversized(n, s) \
442     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
443
444 /* Technically we should add an autoconf test for this, testing for the desired
445    functionality, like what's done in gnulib, but for now, this is fine.  */
446 #if defined(__GLIBC__)
447 #define HAVE_GNU_CALLOC (__GLIBC__ >= 2)
448 #else
449 #define HAVE_GNU_CALLOC 0
450 #endif
451
452 /* Allocate zeroed memory for N elements of S bytes, with error
453    checking.  S must be nonzero.  */
454 void *
455 guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s)
456 {
457   /* From gnulib's calloc function in xmalloc.c.  */
458   void *p;
459   /* Test for overflow, since some calloc implementations don't have
460      proper overflow checks.  But omit overflow and size-zero tests if
461      HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
462      returns NULL if successful.  */
463   if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
464       || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
465     g->abort_cb ();
466   return p;
467 }
468
469 void *
470 guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes)
471 {
472   void *p = realloc (ptr, nbytes);
473   if (nbytes > 0 && !p) g->abort_cb ();
474   return p;
475 }
476
477 char *
478 guestfs_safe_strdup (guestfs_h *g, const char *str)
479 {
480   char *s = strdup (str);
481   if (!s) g->abort_cb ();
482   return s;
483 }
484
485 char *
486 guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n)
487 {
488   char *s = strndup (str, n);
489   if (!s) g->abort_cb ();
490   return s;
491 }
492
493 void *
494 guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size)
495 {
496   void *p = malloc (size);
497   if (!p) g->abort_cb ();
498   memcpy (p, ptr, size);
499   return p;
500 }
501
502 char *
503 guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...)
504 {
505   va_list args;
506   char *msg;
507
508   va_start (args, fs);
509   int err = vasprintf (&msg, fs, args);
510   va_end (args);
511
512   if (err == -1)
513     g->abort_cb ();
514
515   return msg;
516 }
517
518 void
519 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
520 {
521   g->abort_cb = cb;
522 }
523
524 guestfs_abort_cb
525 guestfs_get_out_of_memory_handler (guestfs_h *g)
526 {
527   return g->abort_cb;
528 }
529
530 void
531 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
532 {
533   g->error_cb = cb;
534   g->error_cb_data = data;
535 }
536
537 guestfs_error_handler_cb
538 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
539 {
540   if (data_rtn) *data_rtn = g->error_cb_data;
541   return g->error_cb;
542 }
543
544 void
545 guestfs_user_cancel (guestfs_h *g)
546 {
547   g->user_cancel = 1;
548 }
549
550 int
551 guestfs__set_verbose (guestfs_h *g, int v)
552 {
553   g->verbose = !!v;
554   return 0;
555 }
556
557 int
558 guestfs__get_verbose (guestfs_h *g)
559 {
560   return g->verbose;
561 }
562
563 int
564 guestfs__set_autosync (guestfs_h *g, int a)
565 {
566   g->autosync = !!a;
567   return 0;
568 }
569
570 int
571 guestfs__get_autosync (guestfs_h *g)
572 {
573   return g->autosync;
574 }
575
576 int
577 guestfs__set_path (guestfs_h *g, const char *path)
578 {
579   free (g->path);
580   g->path = NULL;
581
582   g->path =
583     path == NULL ?
584     safe_strdup (g, GUESTFS_DEFAULT_PATH) : safe_strdup (g, path);
585   return 0;
586 }
587
588 const char *
589 guestfs__get_path (guestfs_h *g)
590 {
591   return g->path;
592 }
593
594 int
595 guestfs__set_qemu (guestfs_h *g, const char *qemu)
596 {
597   free (g->qemu);
598   g->qemu = NULL;
599
600   g->qemu = qemu == NULL ? safe_strdup (g, QEMU) : safe_strdup (g, qemu);
601   return 0;
602 }
603
604 const char *
605 guestfs__get_qemu (guestfs_h *g)
606 {
607   return g->qemu;
608 }
609
610 int
611 guestfs__set_append (guestfs_h *g, const char *append)
612 {
613   free (g->append);
614   g->append = NULL;
615
616   g->append = append ? safe_strdup (g, append) : NULL;
617   return 0;
618 }
619
620 const char *
621 guestfs__get_append (guestfs_h *g)
622 {
623   return g->append;
624 }
625
626 int
627 guestfs__set_memsize (guestfs_h *g, int memsize)
628 {
629   g->memsize = memsize;
630   return 0;
631 }
632
633 int
634 guestfs__get_memsize (guestfs_h *g)
635 {
636   return g->memsize;
637 }
638
639 int
640 guestfs__set_selinux (guestfs_h *g, int selinux)
641 {
642   g->selinux = selinux;
643   return 0;
644 }
645
646 int
647 guestfs__get_selinux (guestfs_h *g)
648 {
649   return g->selinux;
650 }
651
652 int
653 guestfs__get_pid (guestfs_h *g)
654 {
655   if (g->pid > 0)
656     return g->pid;
657   else {
658     error (g, "get_pid: no qemu subprocess");
659     return -1;
660   }
661 }
662
663 struct guestfs_version *
664 guestfs__version (guestfs_h *g)
665 {
666   struct guestfs_version *r;
667
668   r = safe_malloc (g, sizeof *r);
669   r->major = PACKAGE_VERSION_MAJOR;
670   r->minor = PACKAGE_VERSION_MINOR;
671   r->release = PACKAGE_VERSION_RELEASE;
672   r->extra = safe_strdup (g, PACKAGE_VERSION_EXTRA);
673   return r;
674 }
675
676 int
677 guestfs__set_trace (guestfs_h *g, int t)
678 {
679   g->trace = !!t;
680   return 0;
681 }
682
683 int
684 guestfs__get_trace (guestfs_h *g)
685 {
686   return g->trace;
687 }
688
689 int
690 guestfs__set_direct (guestfs_h *g, int d)
691 {
692   g->direct = !!d;
693   return 0;
694 }
695
696 int
697 guestfs__get_direct (guestfs_h *g)
698 {
699   return g->direct;
700 }
701
702 int
703 guestfs__set_recovery_proc (guestfs_h *g, int f)
704 {
705   g->recovery_proc = !!f;
706   return 0;
707 }
708
709 int
710 guestfs__get_recovery_proc (guestfs_h *g)
711 {
712   return g->recovery_proc;
713 }
714
715 int
716 guestfs__set_network (guestfs_h *g, int v)
717 {
718   g->enable_network = !!v;
719   return 0;
720 }
721
722 int
723 guestfs__get_network (guestfs_h *g)
724 {
725   return g->enable_network;
726 }
727
728 int
729 guestfs__set_attach_method (guestfs_h *g, const char *method)
730 {
731   if (STREQ (method, "appliance")) {
732     g->attach_method = ATTACH_METHOD_APPLIANCE;
733     free (g->attach_method_arg);
734     g->attach_method_arg = NULL;
735   }
736   else if (STRPREFIX (method, "unix:") && strlen (method) > 5) {
737     g->attach_method = ATTACH_METHOD_UNIX;
738     free (g->attach_method_arg);
739     g->attach_method_arg = safe_strdup (g, method + 5);
740     /* Note that we don't check the path exists until launch is called. */
741   }
742   else {
743     error (g, "invalid attach method: %s", method);
744     return -1;
745   }
746
747   return 0;
748 }
749
750 char *
751 guestfs__get_attach_method (guestfs_h *g)
752 {
753   char *ret;
754
755   switch (g->attach_method) {
756   case ATTACH_METHOD_APPLIANCE:
757     ret = safe_strdup (g, "appliance");
758     break;
759
760   case ATTACH_METHOD_UNIX:
761     ret = safe_malloc (g, strlen (g->attach_method_arg) + 5 + 1);
762     strcpy (ret, "unix:");
763     strcat (ret, g->attach_method_arg);
764     break;
765
766   default: /* keep GCC happy - this is not reached */
767     abort ();
768   }
769
770   return ret;
771 }
772
773 int
774 guestfs__set_pgroup (guestfs_h *g, int v)
775 {
776   g->pgroup = !!v;
777   return 0;
778 }
779
780 int
781 guestfs__get_pgroup (guestfs_h *g)
782 {
783   return g->pgroup;
784 }
785
786 int
787 guestfs__set_smp (guestfs_h *g, int v)
788 {
789   if (v >= 1) {
790     g->smp = v;
791     return 0;
792   } else {
793     error (g, "invalid smp parameter: %d", v);
794     return -1;
795   }
796 }
797
798 int
799 guestfs__get_smp (guestfs_h *g)
800 {
801   return g->smp;
802 }
803
804 /* Note the private data area is allocated lazily, since the vast
805  * majority of callers will never use it.  This means g->pda is
806  * likely to be NULL.
807  */
808 struct pda_entry {
809   char *key;                    /* key */
810   void *data;                   /* opaque user data pointer */
811 };
812
813 static size_t
814 hasher (void const *x, size_t table_size)
815 {
816   struct pda_entry const *p = x;
817   return hash_pjw (p->key, table_size);
818 }
819
820 static bool
821 comparator (void const *x, void const *y)
822 {
823   struct pda_entry const *a = x;
824   struct pda_entry const *b = y;
825   return STREQ (a->key, b->key);
826 }
827
828 static void
829 freer (void *x)
830 {
831   if (x) {
832     struct pda_entry *p = x;
833     free (p->key);
834     free (p);
835   }
836 }
837
838 void
839 guestfs_set_private (guestfs_h *g, const char *key, void *data)
840 {
841   if (g->pda == NULL) {
842     g->pda = hash_initialize (16, NULL, hasher, comparator, freer);
843     if (g->pda == NULL)
844       g->abort_cb ();
845   }
846
847   struct pda_entry *new_entry = safe_malloc (g, sizeof *new_entry);
848   new_entry->key = safe_strdup (g, key);
849   new_entry->data = data;
850
851   struct pda_entry *old_entry = hash_delete (g->pda, new_entry);
852   freer (old_entry);
853
854   struct pda_entry *entry = hash_insert (g->pda, new_entry);
855   if (entry == NULL)
856     g->abort_cb ();
857   assert (entry == new_entry);
858 }
859
860 static inline char *
861 bad_cast (char const *s)
862 {
863   return (char *) s;
864 }
865
866 void *
867 guestfs_get_private (guestfs_h *g, const char *key)
868 {
869   if (g->pda == NULL)
870     return NULL;                /* no keys have been set */
871
872   const struct pda_entry k = { .key = bad_cast (key) };
873   struct pda_entry *entry = hash_lookup (g->pda, &k);
874   if (entry)
875     return entry->data;
876   else
877     return NULL;
878 }
879
880 /* Iterator. */
881 void *
882 guestfs_first_private (guestfs_h *g, const char **key_rtn)
883 {
884   if (g->pda == NULL)
885     return NULL;
886
887   g->pda_next = hash_get_first (g->pda);
888
889   /* Ignore any keys with NULL data pointers. */
890   while (g->pda_next && g->pda_next->data == NULL)
891     g->pda_next = hash_get_next (g->pda, g->pda_next);
892
893   if (g->pda_next == NULL)
894     return NULL;
895
896   *key_rtn = g->pda_next->key;
897   return g->pda_next->data;
898 }
899
900 void *
901 guestfs_next_private (guestfs_h *g, const char **key_rtn)
902 {
903   if (g->pda == NULL)
904     return NULL;
905
906   if (g->pda_next == NULL)
907     return NULL;
908
909   /* Walk to the next key with a non-NULL data pointer. */
910   do {
911     g->pda_next = hash_get_next (g->pda, g->pda_next);
912   } while (g->pda_next && g->pda_next->data == NULL);
913
914   if (g->pda_next == NULL)
915     return NULL;
916
917   *key_rtn = g->pda_next->key;
918   return g->pda_next->data;
919 }
920
921 /* When tracing, be careful how we print BufferIn parameters which
922  * usually contain large amounts of binary data (RHBZ#646822).
923  */
924 void
925 guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size)
926 {
927   size_t i;
928   size_t orig_size = buf_size;
929
930   if (buf_size > 256)
931     buf_size = 256;
932
933   fputc ('"', out);
934
935   for (i = 0; i < buf_size; ++i) {
936     if (c_isprint (buf[i]))
937       fputc (buf[i], out);
938     else
939       fprintf (out, "\\x%02x", (unsigned char) buf[i]);
940   }
941
942   fputc ('"', out);
943
944   if (orig_size > buf_size)
945     fprintf (out,
946              _("<truncated, original size %zu bytes>"), orig_size);
947 }
948
949 void
950 guestfs___print_BufferOut (FILE *out, const char *buf, size_t buf_size)
951 {
952   guestfs___print_BufferIn (out, buf, buf_size);
953 }
954
955 void
956 guestfs___free_string_list (char **argv)
957 {
958   size_t i;
959   for (i = 0; argv[i] != NULL; ++i)
960     free (argv[i]);
961   free (argv);
962 }
963
964 void
965 guestfs___free_drives (struct drive **drives)
966 {
967   struct drive *i = *drives;
968   *drives = NULL;
969
970   while (i != NULL) {
971     struct drive *next = i->next;
972
973     free (i->path);
974     free (i->format);
975     free (i->iface);
976     free (i->name);
977     free (i);
978
979     i = next;
980   }
981 }