Rename $tmpdir/sock to $tmpdir/guestfsd.sock.
[libguestfs.git] / src / guestfs.c
1 /* libguestfs
2  * Copyright (C) 2009-2010 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   /* Link the handles onto a global list. */
147   gl_lock_lock (handles_lock);
148   g->next = handles;
149   handles = g;
150   if (!atexit_handler_set) {
151     atexit (close_handles);
152     atexit_handler_set = 1;
153   }
154   gl_lock_unlock (handles_lock);
155
156   debug (g, "new guestfs handle %p", g);
157
158   return g;
159
160  error:
161   free (g->path);
162   free (g->qemu);
163   free (g->append);
164   free (g);
165   return NULL;
166 }
167
168 void
169 guestfs_close (guestfs_h *g)
170 {
171   int i;
172   char filename[256];
173   guestfs_h *gg;
174
175   if (g->state == NO_HANDLE) {
176     /* Not safe to call ANY callbacks here, so ... */
177     fprintf (stderr, _("guestfs_close: called twice on the same handle\n"));
178     return;
179   }
180
181   debug (g, "closing guestfs handle %p (state %d)", g, g->state);
182
183   /* Try to sync if autosync flag is set. */
184   if (g->autosync && g->state == READY)
185     guestfs_internal_autosync (g);
186
187   /* Kill the qemu subprocess. */
188   if (g->state != CONFIG)
189     guestfs_kill_subprocess (g);
190
191   /* Run user close callbacks. */
192   guestfs___call_callbacks_void (g, GUESTFS_EVENT_CLOSE);
193
194   /* Remove all other registered callbacks.  Since we've already
195    * called the close callbacks, we shouldn't call any others.
196    */
197   free (g->events);
198   g->nr_events = 0;
199   g->events = NULL;
200
201   guestfs___free_inspect_info (g);
202
203   /* Close sockets. */
204   if (g->fd[0] >= 0)
205     close (g->fd[0]);
206   if (g->fd[1] >= 0)
207     close (g->fd[1]);
208   if (g->sock >= 0)
209     close (g->sock);
210   g->fd[0] = -1;
211   g->fd[1] = -1;
212   g->sock = -1;
213
214   /* Wait for subprocess(es) to exit. */
215   if (g->pid > 0) waitpid (g->pid, NULL, 0);
216   if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
217
218   /* Remove tmpfiles. */
219   if (g->tmpdir) {
220     snprintf (filename, sizeof filename, "%s/guestfsd.sock", g->tmpdir);
221     unlink (filename);
222
223     rmdir (g->tmpdir);
224
225     free (g->tmpdir);
226   }
227
228   if (g->cmdline) {
229     for (i = 0; i < g->cmdline_size; ++i)
230       free (g->cmdline[i]);
231     free (g->cmdline);
232   }
233
234   /* Mark the handle as dead before freeing it. */
235   g->state = NO_HANDLE;
236
237   gl_lock_lock (handles_lock);
238   if (handles == g)
239     handles = g->next;
240   else {
241     for (gg = handles; gg->next != g; gg = gg->next)
242       ;
243     gg->next = g->next;
244   }
245   gl_lock_unlock (handles_lock);
246
247   if (g->pda)
248     hash_free (g->pda);
249   free (g->last_error);
250   free (g->path);
251   free (g->qemu);
252   free (g->append);
253   free (g->qemu_help);
254   free (g->qemu_version);
255   free (g);
256 }
257
258 /* Close all open handles (called from atexit(3)). */
259 static void
260 close_handles (void)
261 {
262   while (handles) guestfs_close (handles);
263 }
264
265 const char *
266 guestfs_last_error (guestfs_h *g)
267 {
268   return g->last_error;
269 }
270
271 int
272 guestfs_last_errno (guestfs_h *g)
273 {
274   return g->last_errnum;
275 }
276
277 static void
278 set_last_error (guestfs_h *g, int errnum, const char *msg)
279 {
280   free (g->last_error);
281   g->last_error = strdup (msg);
282   g->last_errnum = errnum;
283 }
284
285 /* Warning are printed unconditionally.  We try to make these rare.
286  * Generally speaking, a warning should either be an error, or if it's
287  * not important for end users then it should be a debug message.
288  */
289 void
290 guestfs___warning (guestfs_h *g, const char *fs, ...)
291 {
292   va_list args;
293   char *msg, *msg2;
294   int len;
295
296   va_start (args, fs);
297   len = vasprintf (&msg, fs, args);
298   va_end (args);
299
300   if (len < 0) return;
301
302   len = asprintf (&msg2, _("warning: %s"), msg);
303   free (msg);
304
305   if (len < 0) return;
306
307   guestfs___call_callbacks_message (g, GUESTFS_EVENT_LIBRARY, msg2, len);
308
309   free (msg2);
310 }
311
312 /* Debug messages. */
313 void
314 guestfs___debug (guestfs_h *g, const char *fs, ...)
315 {
316   va_list args;
317   char *msg;
318   int len;
319
320   /* The cpp macro "debug" has already checked that g->verbose is true
321    * before calling this function, but we check it again just in case
322    * anyone calls this function directly.
323    */
324   if (!g->verbose)
325     return;
326
327   va_start (args, fs);
328   len = vasprintf (&msg, fs, args);
329   va_end (args);
330
331   if (len < 0) return;
332
333   guestfs___call_callbacks_message (g, GUESTFS_EVENT_LIBRARY, msg, len);
334 }
335
336 /* Call trace messages.  These are enabled by setting g->trace, and
337  * calls to this function should only happen from the generated code
338  * in src/actions.c
339  */
340 void
341 guestfs___trace (guestfs_h *g, const char *fs, ...)
342 {
343   va_list args;
344   char *msg;
345   int len;
346
347   va_start (args, fs);
348   len = vasprintf (&msg, fs, args);
349   va_end (args);
350
351   if (len < 0) return;
352
353   guestfs___call_callbacks_message (g, GUESTFS_EVENT_TRACE, msg, len);
354
355   free (msg);
356 }
357
358 static void
359 default_error_cb (guestfs_h *g, void *data, const char *msg)
360 {
361   fprintf (stderr, _("libguestfs: error: %s\n"), msg);
362 }
363
364 void
365 guestfs_error_errno (guestfs_h *g, int errnum, const char *fs, ...)
366 {
367   va_list args;
368   char *msg;
369
370   va_start (args, fs);
371   int err = vasprintf (&msg, fs, args);
372   va_end (args);
373
374   if (err < 0) return;
375
376   /* set_last_error first so that the callback can access the error
377    * message and errno through the handle if it wishes.
378    */
379   set_last_error (g, errnum, msg);
380   if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
381
382   free (msg);
383 }
384
385 void
386 guestfs_perrorf (guestfs_h *g, const char *fs, ...)
387 {
388   va_list args;
389   char *msg;
390   int errnum = errno;
391
392   va_start (args, fs);
393   int err = vasprintf (&msg, fs, args);
394   va_end (args);
395
396   if (err < 0) return;
397
398 #if !defined(_GNU_SOURCE) || defined(__APPLE__)
399   char buf[256];
400   strerror_r (errnum, buf, sizeof buf);
401 #else
402   char _buf[256];
403   char *buf;
404   buf = strerror_r (errnum, _buf, sizeof _buf);
405 #endif
406
407   msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
408   strcat (msg, ": ");
409   strcat (msg, buf);
410
411   /* set_last_error first so that the callback can access the error
412    * message and errno through the handle if it wishes.
413    */
414   set_last_error (g, errnum, msg);
415   if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
416
417   free (msg);
418 }
419
420 void *
421 guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
422 {
423   void *ptr = malloc (nbytes);
424   if (nbytes > 0 && !ptr) g->abort_cb ();
425   return ptr;
426 }
427
428 /* Return 1 if an array of N objects, each of size S, cannot exist due
429    to size arithmetic overflow.  S must be positive and N must be
430    nonnegative.  This is a macro, not an inline function, so that it
431    works correctly even when SIZE_MAX < N.
432
433    By gnulib convention, SIZE_MAX represents overflow in size
434    calculations, so the conservative dividend to use here is
435    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
436    However, malloc (SIZE_MAX) fails on all known hosts where
437    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
438    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
439    branch when S is known to be 1.  */
440 # define xalloc_oversized(n, s) \
441     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
442
443 /* Technically we should add an autoconf test for this, testing for the desired
444    functionality, like what's done in gnulib, but for now, this is fine.  */
445 #if defined(__GLIBC__)
446 #define HAVE_GNU_CALLOC (__GLIBC__ >= 2)
447 #else
448 #define HAVE_GNU_CALLOC 0
449 #endif
450
451 /* Allocate zeroed memory for N elements of S bytes, with error
452    checking.  S must be nonzero.  */
453 void *
454 guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s)
455 {
456   /* From gnulib's calloc function in xmalloc.c.  */
457   void *p;
458   /* Test for overflow, since some calloc implementations don't have
459      proper overflow checks.  But omit overflow and size-zero tests if
460      HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
461      returns NULL if successful.  */
462   if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
463       || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
464     g->abort_cb ();
465   return p;
466 }
467
468 void *
469 guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes)
470 {
471   void *p = realloc (ptr, nbytes);
472   if (nbytes > 0 && !p) g->abort_cb ();
473   return p;
474 }
475
476 char *
477 guestfs_safe_strdup (guestfs_h *g, const char *str)
478 {
479   char *s = strdup (str);
480   if (!s) g->abort_cb ();
481   return s;
482 }
483
484 char *
485 guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n)
486 {
487   char *s = strndup (str, n);
488   if (!s) g->abort_cb ();
489   return s;
490 }
491
492 void *
493 guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size)
494 {
495   void *p = malloc (size);
496   if (!p) g->abort_cb ();
497   memcpy (p, ptr, size);
498   return p;
499 }
500
501 char *
502 guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...)
503 {
504   va_list args;
505   char *msg;
506
507   va_start (args, fs);
508   int err = vasprintf (&msg, fs, args);
509   va_end (args);
510
511   if (err == -1)
512     g->abort_cb ();
513
514   return msg;
515 }
516
517 void
518 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
519 {
520   g->abort_cb = cb;
521 }
522
523 guestfs_abort_cb
524 guestfs_get_out_of_memory_handler (guestfs_h *g)
525 {
526   return g->abort_cb;
527 }
528
529 void
530 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
531 {
532   g->error_cb = cb;
533   g->error_cb_data = data;
534 }
535
536 guestfs_error_handler_cb
537 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
538 {
539   if (data_rtn) *data_rtn = g->error_cb_data;
540   return g->error_cb;
541 }
542
543 int
544 guestfs__set_verbose (guestfs_h *g, int v)
545 {
546   g->verbose = !!v;
547   return 0;
548 }
549
550 int
551 guestfs__get_verbose (guestfs_h *g)
552 {
553   return g->verbose;
554 }
555
556 int
557 guestfs__set_autosync (guestfs_h *g, int a)
558 {
559   g->autosync = !!a;
560   return 0;
561 }
562
563 int
564 guestfs__get_autosync (guestfs_h *g)
565 {
566   return g->autosync;
567 }
568
569 int
570 guestfs__set_path (guestfs_h *g, const char *path)
571 {
572   free (g->path);
573   g->path = NULL;
574
575   g->path =
576     path == NULL ?
577     safe_strdup (g, GUESTFS_DEFAULT_PATH) : safe_strdup (g, path);
578   return 0;
579 }
580
581 const char *
582 guestfs__get_path (guestfs_h *g)
583 {
584   return g->path;
585 }
586
587 int
588 guestfs__set_qemu (guestfs_h *g, const char *qemu)
589 {
590   free (g->qemu);
591   g->qemu = NULL;
592
593   g->qemu = qemu == NULL ? safe_strdup (g, QEMU) : safe_strdup (g, qemu);
594   return 0;
595 }
596
597 const char *
598 guestfs__get_qemu (guestfs_h *g)
599 {
600   return g->qemu;
601 }
602
603 int
604 guestfs__set_append (guestfs_h *g, const char *append)
605 {
606   free (g->append);
607   g->append = NULL;
608
609   g->append = append ? safe_strdup (g, append) : NULL;
610   return 0;
611 }
612
613 const char *
614 guestfs__get_append (guestfs_h *g)
615 {
616   return g->append;
617 }
618
619 int
620 guestfs__set_memsize (guestfs_h *g, int memsize)
621 {
622   g->memsize = memsize;
623   return 0;
624 }
625
626 int
627 guestfs__get_memsize (guestfs_h *g)
628 {
629   return g->memsize;
630 }
631
632 int
633 guestfs__set_selinux (guestfs_h *g, int selinux)
634 {
635   g->selinux = selinux;
636   return 0;
637 }
638
639 int
640 guestfs__get_selinux (guestfs_h *g)
641 {
642   return g->selinux;
643 }
644
645 int
646 guestfs__get_pid (guestfs_h *g)
647 {
648   if (g->pid > 0)
649     return g->pid;
650   else {
651     error (g, "get_pid: no qemu subprocess");
652     return -1;
653   }
654 }
655
656 struct guestfs_version *
657 guestfs__version (guestfs_h *g)
658 {
659   struct guestfs_version *r;
660
661   r = safe_malloc (g, sizeof *r);
662   r->major = PACKAGE_VERSION_MAJOR;
663   r->minor = PACKAGE_VERSION_MINOR;
664   r->release = PACKAGE_VERSION_RELEASE;
665   r->extra = safe_strdup (g, PACKAGE_VERSION_EXTRA);
666   return r;
667 }
668
669 int
670 guestfs__set_trace (guestfs_h *g, int t)
671 {
672   g->trace = !!t;
673   return 0;
674 }
675
676 int
677 guestfs__get_trace (guestfs_h *g)
678 {
679   return g->trace;
680 }
681
682 int
683 guestfs__set_direct (guestfs_h *g, int d)
684 {
685   g->direct = !!d;
686   return 0;
687 }
688
689 int
690 guestfs__get_direct (guestfs_h *g)
691 {
692   return g->direct;
693 }
694
695 int
696 guestfs__set_recovery_proc (guestfs_h *g, int f)
697 {
698   g->recovery_proc = !!f;
699   return 0;
700 }
701
702 int
703 guestfs__get_recovery_proc (guestfs_h *g)
704 {
705   return g->recovery_proc;
706 }
707
708 int
709 guestfs__set_network (guestfs_h *g, int v)
710 {
711   g->enable_network = !!v;
712   return 0;
713 }
714
715 int
716 guestfs__get_network (guestfs_h *g)
717 {
718   return g->enable_network;
719 }
720
721 int
722 guestfs__set_attach_method (guestfs_h *g, const char *method)
723 {
724   if (STREQ (method, "appliance")) {
725     g->attach_method = ATTACH_METHOD_APPLIANCE;
726     free (g->attach_method_arg);
727     g->attach_method_arg = NULL;
728   }
729   else if (STRPREFIX (method, "unix:") && strlen (method) > 5) {
730     g->attach_method = ATTACH_METHOD_UNIX;
731     free (g->attach_method_arg);
732     g->attach_method_arg = safe_strdup (g, method + 5);
733     /* Note that we don't check the path exists until launch is called. */
734   }
735   else {
736     error (g, "invalid attach method: %s", method);
737     return -1;
738   }
739
740   return 0;
741 }
742
743 char *
744 guestfs__get_attach_method (guestfs_h *g)
745 {
746   char *ret;
747
748   switch (g->attach_method) {
749   case ATTACH_METHOD_APPLIANCE:
750     ret = safe_strdup (g, "appliance");
751     break;
752
753   case ATTACH_METHOD_UNIX:
754     ret = safe_malloc (g, strlen (g->attach_method_arg) + 5 + 1);
755     strcpy (ret, "unix:");
756     strcat (ret, g->attach_method_arg);
757     break;
758
759   default: /* keep GCC happy - this is not reached */
760     abort ();
761   }
762
763   return ret;
764 }
765
766 /* Note the private data area is allocated lazily, since the vast
767  * majority of callers will never use it.  This means g->pda is
768  * likely to be NULL.
769  */
770 struct pda_entry {
771   char *key;                    /* key */
772   void *data;                   /* opaque user data pointer */
773 };
774
775 static size_t
776 hasher (void const *x, size_t table_size)
777 {
778   struct pda_entry const *p = x;
779   return hash_pjw (p->key, table_size);
780 }
781
782 static bool
783 comparator (void const *x, void const *y)
784 {
785   struct pda_entry const *a = x;
786   struct pda_entry const *b = y;
787   return STREQ (a->key, b->key);
788 }
789
790 static void
791 freer (void *x)
792 {
793   if (x) {
794     struct pda_entry *p = x;
795     free (p->key);
796     free (p);
797   }
798 }
799
800 void
801 guestfs_set_private (guestfs_h *g, const char *key, void *data)
802 {
803   if (g->pda == NULL) {
804     g->pda = hash_initialize (16, NULL, hasher, comparator, freer);
805     if (g->pda == NULL)
806       g->abort_cb ();
807   }
808
809   struct pda_entry *new_entry = safe_malloc (g, sizeof *new_entry);
810   new_entry->key = safe_strdup (g, key);
811   new_entry->data = data;
812
813   struct pda_entry *old_entry = hash_delete (g->pda, new_entry);
814   freer (old_entry);
815
816   struct pda_entry *entry = hash_insert (g->pda, new_entry);
817   if (entry == NULL)
818     g->abort_cb ();
819   assert (entry == new_entry);
820 }
821
822 static inline char *
823 bad_cast (char const *s)
824 {
825   return (char *) s;
826 }
827
828 void *
829 guestfs_get_private (guestfs_h *g, const char *key)
830 {
831   if (g->pda == NULL)
832     return NULL;                /* no keys have been set */
833
834   const struct pda_entry k = { .key = bad_cast (key) };
835   struct pda_entry *entry = hash_lookup (g->pda, &k);
836   if (entry)
837     return entry->data;
838   else
839     return NULL;
840 }
841
842 /* Iterator. */
843 void *
844 guestfs_first_private (guestfs_h *g, const char **key_rtn)
845 {
846   if (g->pda == NULL)
847     return NULL;
848
849   g->pda_next = hash_get_first (g->pda);
850
851   /* Ignore any keys with NULL data pointers. */
852   while (g->pda_next && g->pda_next->data == NULL)
853     g->pda_next = hash_get_next (g->pda, g->pda_next);
854
855   if (g->pda_next == NULL)
856     return NULL;
857
858   *key_rtn = g->pda_next->key;
859   return g->pda_next->data;
860 }
861
862 void *
863 guestfs_next_private (guestfs_h *g, const char **key_rtn)
864 {
865   if (g->pda == NULL)
866     return NULL;
867
868   if (g->pda_next == NULL)
869     return NULL;
870
871   /* Walk to the next key with a non-NULL data pointer. */
872   do {
873     g->pda_next = hash_get_next (g->pda, g->pda_next);
874   } while (g->pda_next && g->pda_next->data == NULL);
875
876   if (g->pda_next == NULL)
877     return NULL;
878
879   *key_rtn = g->pda_next->key;
880   return g->pda_next->data;
881 }
882
883 /* When tracing, be careful how we print BufferIn parameters which
884  * usually contain large amounts of binary data (RHBZ#646822).
885  */
886 void
887 guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size)
888 {
889   size_t i;
890   size_t orig_size = buf_size;
891
892   if (buf_size > 256)
893     buf_size = 256;
894
895   fputc ('"', out);
896
897   for (i = 0; i < buf_size; ++i) {
898     if (c_isprint (buf[i]))
899       fputc (buf[i], out);
900     else
901       fprintf (out, "\\x%02x", (unsigned char) buf[i]);
902   }
903
904   fputc ('"', out);
905
906   if (orig_size > buf_size)
907     fprintf (out,
908              _("<truncated, original size %zu bytes>"), orig_size);
909 }
910
911 void
912 guestfs___print_BufferOut (FILE *out, const char *buf, size_t buf_size)
913 {
914   guestfs___print_BufferIn (out, buf, buf_size);
915 }
916
917 void
918 guestfs___free_string_list (char **argv)
919 {
920   size_t i;
921   for (i = 0; argv[i] != NULL; ++i)
922     free (argv[i]);
923   free (argv);
924 }