2 * Copyright (C) 2009-2010 Red Hat Inc.
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.
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.
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
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
34 #include <sys/select.h>
38 #include <rpc/types.h>
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
49 #ifdef HAVE_SYS_WAIT_H
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
61 #include <arpa/inet.h>
62 #include <netinet/in.h>
65 #include "glthread/lock.h"
66 #include "ignore-value.h"
69 #include "guestfs-internal.h"
70 #include "guestfs-internal-actions.h"
71 #include "guestfs_protocol.h"
73 static void default_error_cb (guestfs_h *g, void *data, const char *msg);
74 static void close_handles (void);
76 gl_lock_define_initialized (static, handles_lock);
77 static guestfs_h *handles = NULL;
78 static int atexit_handler_set = 0;
86 g = malloc (sizeof (*g));
89 memset (g, 0, sizeof (*g));
98 g->error_cb = default_error_cb;
99 g->error_cb_data = NULL;
101 g->recovery_proc = 1;
103 str = getenv ("LIBGUESTFS_DEBUG");
104 g->verbose = str != NULL && STREQ (str, "1");
106 str = getenv ("LIBGUESTFS_TRACE");
107 g->trace = str != NULL && STREQ (str, "1");
109 str = getenv ("LIBGUESTFS_PATH");
110 g->path = str != NULL ? strdup (str) : strdup (GUESTFS_DEFAULT_PATH);
111 if (!g->path) goto error;
113 str = getenv ("LIBGUESTFS_QEMU");
114 g->qemu = str != NULL ? strdup (str) : strdup (QEMU);
115 if (!g->qemu) goto error;
117 str = getenv ("LIBGUESTFS_APPEND");
119 g->append = strdup (str);
120 if (!g->append) goto error;
123 /* Choose a suitable memory size. Previously we tried to choose
124 * a minimal memory size, but this isn't really necessary since
125 * recent QEMU and KVM don't do anything nasty like locking
126 * memory into core any more. Thus we can safely choose a
127 * large, generous amount of memory, and it'll just get swapped
128 * on smaller systems.
130 str = getenv ("LIBGUESTFS_MEMSIZE");
132 if (sscanf (str, "%d", &g->memsize) != 1 || g->memsize <= 256) {
133 fprintf (stderr, "libguestfs: non-numeric or too small value for LIBGUESTFS_MEMSIZE\n");
139 /* Start with large serial numbers so they are easy to spot
140 * inside the protocol.
142 g->msg_next_serial = 0x00123400;
144 /* Link the handles onto a global list. */
145 gl_lock_lock (handles_lock);
148 if (!atexit_handler_set) {
149 atexit (close_handles);
150 atexit_handler_set = 1;
152 gl_lock_unlock (handles_lock);
155 fprintf (stderr, "new guestfs handle %p\n", g);
168 guestfs_close (guestfs_h *g)
174 if (g->state == NO_HANDLE) {
175 /* Not safe to call 'error' here, so ... */
176 fprintf (stderr, _("guestfs_close: called twice on the same handle\n"));
181 fprintf (stderr, "closing guestfs handle %p (state %d)\n", g, g->state);
183 /* Run user close callback before anything else. */
185 g->close_cb (g, g->close_cb_data);
187 guestfs___free_inspect_info (g);
189 /* Try to sync if autosync flag is set. */
190 if (g->autosync && g->state == READY) {
191 guestfs_umount_all (g);
195 /* Remove any handlers that might be called back before we kill the
198 g->log_message_cb = NULL;
200 if (g->state != CONFIG)
201 guestfs_kill_subprocess (g);
214 /* Wait for subprocess(es) to exit. */
215 waitpid (g->pid, NULL, 0);
216 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
218 /* Remove tmpfiles. */
220 snprintf (filename, sizeof filename, "%s/sock", g->tmpdir);
229 for (i = 0; i < g->cmdline_size; ++i)
230 free (g->cmdline[i]);
234 /* Mark the handle as dead before freeing it. */
235 g->state = NO_HANDLE;
237 gl_lock_lock (handles_lock);
241 for (gg = handles; gg->next != g; gg = gg->next)
245 gl_lock_unlock (handles_lock);
247 free (g->last_error);
252 free (g->qemu_version);
256 /* Close all open handles (called from atexit(3)). */
260 while (handles) guestfs_close (handles);
264 guestfs_last_error (guestfs_h *g)
266 return g->last_error;
270 set_last_error (guestfs_h *g, const char *msg)
272 free (g->last_error);
273 g->last_error = strdup (msg);
277 default_error_cb (guestfs_h *g, void *data, const char *msg)
279 fprintf (stderr, _("libguestfs: error: %s\n"), msg);
283 guestfs_error (guestfs_h *g, const char *fs, ...)
289 int err = vasprintf (&msg, fs, args);
294 if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
295 set_last_error (g, msg);
301 guestfs_perrorf (guestfs_h *g, const char *fs, ...)
308 int err = vasprintf (&msg, fs, args);
313 #if !defined(_GNU_SOURCE) || defined(__APPLE__)
315 strerror_r (errnum, buf, sizeof buf);
319 buf = strerror_r (errnum, _buf, sizeof _buf);
322 msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
326 if (g->error_cb) g->error_cb (g, g->error_cb_data, msg);
327 set_last_error (g, msg);
333 guestfs_safe_malloc (guestfs_h *g, size_t nbytes)
335 void *ptr = malloc (nbytes);
336 if (nbytes > 0 && !ptr) g->abort_cb ();
340 /* Return 1 if an array of N objects, each of size S, cannot exist due
341 to size arithmetic overflow. S must be positive and N must be
342 nonnegative. This is a macro, not an inline function, so that it
343 works correctly even when SIZE_MAX < N.
345 By gnulib convention, SIZE_MAX represents overflow in size
346 calculations, so the conservative dividend to use here is
347 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
348 However, malloc (SIZE_MAX) fails on all known hosts where
349 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
350 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
351 branch when S is known to be 1. */
352 # define xalloc_oversized(n, s) \
353 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
355 /* Technically we should add an autoconf test for this, testing for the desired
356 functionality, like what's done in gnulib, but for now, this is fine. */
357 #if defined(__GLIBC__)
358 #define HAVE_GNU_CALLOC (__GLIBC__ >= 2)
360 #define HAVE_GNU_CALLOC 0
363 /* Allocate zeroed memory for N elements of S bytes, with error
364 checking. S must be nonzero. */
366 guestfs_safe_calloc (guestfs_h *g, size_t n, size_t s)
368 /* From gnulib's calloc function in xmalloc.c. */
370 /* Test for overflow, since some calloc implementations don't have
371 proper overflow checks. But omit overflow and size-zero tests if
372 HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
373 returns NULL if successful. */
374 if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
375 || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
381 guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes)
383 void *p = realloc (ptr, nbytes);
384 if (nbytes > 0 && !p) g->abort_cb ();
389 guestfs_safe_strdup (guestfs_h *g, const char *str)
391 char *s = strdup (str);
392 if (!s) g->abort_cb ();
397 guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n)
399 char *s = strndup (str, n);
400 if (!s) g->abort_cb ();
405 guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size)
407 void *p = malloc (size);
408 if (!p) g->abort_cb ();
409 memcpy (p, ptr, size);
414 guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb)
420 guestfs_get_out_of_memory_handler (guestfs_h *g)
426 guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data)
429 g->error_cb_data = data;
432 guestfs_error_handler_cb
433 guestfs_get_error_handler (guestfs_h *g, void **data_rtn)
435 if (data_rtn) *data_rtn = g->error_cb_data;
440 guestfs__set_verbose (guestfs_h *g, int v)
447 guestfs__get_verbose (guestfs_h *g)
453 guestfs__set_autosync (guestfs_h *g, int a)
460 guestfs__get_autosync (guestfs_h *g)
466 guestfs__set_path (guestfs_h *g, const char *path)
473 safe_strdup (g, GUESTFS_DEFAULT_PATH) : safe_strdup (g, path);
478 guestfs__get_path (guestfs_h *g)
484 guestfs__set_qemu (guestfs_h *g, const char *qemu)
489 g->qemu = qemu == NULL ? safe_strdup (g, QEMU) : safe_strdup (g, qemu);
494 guestfs__get_qemu (guestfs_h *g)
500 guestfs__set_append (guestfs_h *g, const char *append)
505 g->append = append ? safe_strdup (g, append) : NULL;
510 guestfs__get_append (guestfs_h *g)
516 guestfs__set_memsize (guestfs_h *g, int memsize)
518 g->memsize = memsize;
523 guestfs__get_memsize (guestfs_h *g)
529 guestfs__set_selinux (guestfs_h *g, int selinux)
531 g->selinux = selinux;
536 guestfs__get_selinux (guestfs_h *g)
542 guestfs__get_pid (guestfs_h *g)
547 error (g, "get_pid: no qemu subprocess");
552 struct guestfs_version *
553 guestfs__version (guestfs_h *g)
555 struct guestfs_version *r;
557 r = safe_malloc (g, sizeof *r);
558 r->major = PACKAGE_VERSION_MAJOR;
559 r->minor = PACKAGE_VERSION_MINOR;
560 r->release = PACKAGE_VERSION_RELEASE;
561 r->extra = safe_strdup (g, PACKAGE_VERSION_EXTRA);
566 guestfs__set_trace (guestfs_h *g, int t)
573 guestfs__get_trace (guestfs_h *g)
579 guestfs__set_direct (guestfs_h *g, int d)
586 guestfs__get_direct (guestfs_h *g)
592 guestfs__set_recovery_proc (guestfs_h *g, int f)
594 g->recovery_proc = !!f;
599 guestfs__get_recovery_proc (guestfs_h *g)
601 return g->recovery_proc;
605 guestfs__set_network (guestfs_h *g, int v)
607 g->enable_network = !!v;
612 guestfs__get_network (guestfs_h *g)
614 return g->enable_network;
618 guestfs_set_log_message_callback (guestfs_h *g,
619 guestfs_log_message_cb cb, void *opaque)
621 g->log_message_cb = cb;
622 g->log_message_cb_data = opaque;
626 guestfs_set_subprocess_quit_callback (guestfs_h *g,
627 guestfs_subprocess_quit_cb cb, void *opaque)
629 g->subprocess_quit_cb = cb;
630 g->subprocess_quit_cb_data = opaque;
634 guestfs_set_launch_done_callback (guestfs_h *g,
635 guestfs_launch_done_cb cb, void *opaque)
637 g->launch_done_cb = cb;
638 g->launch_done_cb_data = opaque;
642 guestfs_set_close_callback (guestfs_h *g,
643 guestfs_close_cb cb, void *opaque)
646 g->close_cb_data = opaque;