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