int main (int argc, char *argv[]) { guestfs_h *g = guestfs_create (); /* Call some non-daemon functions that have a String parameter, but * setting that parameter to NULL. Previously this would cause a * segfault inside libguestfs. After this bug was fixed, this * turned into an error message. */ assert (guestfs_add_drive (g, NULL) == -1); assert (guestfs_config (g, NULL, NULL) == -1); /* This optional argument must not be NULL. */ assert (guestfs_add_drive_opts (g, "/dev/null", GUESTFS_ADD_DRIVE_OPTS_FORMAT, NULL, -1) == -1); /* These can be safely set to NULL, should be no error. */ assert (guestfs_set_path (g, NULL) == 0); assert (guestfs_set_append (g, NULL) == 0); assert (guestfs_set_qemu (g, NULL) == 0); guestfs_close (g); exit (EXIT_SUCCESS); }
static int parse_environment (guestfs_h *g, char *(*do_getenv) (const void *data, const char *), const void *data) { int memsize, b; char *str; /* Don't bother checking the return values of functions * that cannot return errors. */ str = do_getenv (data, "LIBGUESTFS_TRACE"); if (str) { b = guestfs_int_is_true (str); if (b == -1) { error (g, _("%s=%s: non-boolean value"), "LIBGUESTFS_TRACE", str); return -1; } guestfs_set_trace (g, b); } str = do_getenv (data, "LIBGUESTFS_DEBUG"); if (str) { b = guestfs_int_is_true (str); if (b == -1) { error (g, _("%s=%s: non-boolean value"), "LIBGUESTFS_TRACE", str); return -1; } guestfs_set_verbose (g, b); } str = do_getenv (data, "LIBGUESTFS_TMPDIR"); if (str && STRNEQ (str, "")) { if (guestfs_set_tmpdir (g, str) == -1) return -1; } str = do_getenv (data, "LIBGUESTFS_CACHEDIR"); if (str && STRNEQ (str, "")) { if (guestfs_set_cachedir (g, str) == -1) return -1; } str = do_getenv (data, "TMPDIR"); if (guestfs_int_set_env_tmpdir (g, str) == -1) return -1; str = do_getenv (data, "LIBGUESTFS_PATH"); if (str && STRNEQ (str, "")) guestfs_set_path (g, str); str = do_getenv (data, "LIBGUESTFS_HV"); if (str && STRNEQ (str, "")) guestfs_set_hv (g, str); else { str = do_getenv (data, "LIBGUESTFS_QEMU"); if (str && STRNEQ (str, "")) guestfs_set_hv (g, str); } str = do_getenv (data, "LIBGUESTFS_APPEND"); if (str) guestfs_set_append (g, str); str = do_getenv (data, "LIBGUESTFS_MEMSIZE"); if (str && STRNEQ (str, "")) { if (sscanf (str, "%d", &memsize) != 1) { error (g, _("non-numeric value for LIBGUESTFS_MEMSIZE")); return -1; } if (guestfs_set_memsize (g, memsize) == -1) { /* set_memsize produces an error message already. */ return -1; } } str = do_getenv (data, "LIBGUESTFS_BACKEND"); if (str && STRNEQ (str, "")) { if (guestfs_set_backend (g, str) == -1) return -1; } else { str = do_getenv (data, "LIBGUESTFS_ATTACH_METHOD"); if (str && STRNEQ (str, "")) { if (guestfs_set_backend (g, str) == -1) return -1; } } str = do_getenv (data, "LIBGUESTFS_BACKEND_SETTINGS"); if (str) { CLEANUP_FREE_STRING_LIST char **settings = guestfs_int_split_string (':', str); if (settings == NULL) { perrorf (g, "split_string: malloc"); return -1; } if (guestfs_set_backend_settings (g, settings) == -1) return -1; } return 0; }
int run_reopen (const char *cmd, size_t argc, char *argv[]) { guestfs_h *g2; int r; const char *p; guestfs_error_handler_cb cb; void *cb_data; if (argc > 0) { fprintf (stderr, _("'reopen' command takes no parameters\n")); return -1; } if (guestfs_shutdown (g) == -1) return -1; /* Open the new handle first, so we can copy the settings from the * old one to the new one, and also so if it fails we still have an * open handle. */ g2 = guestfs_create (); if (g2 == NULL) { fprintf (stderr, _("reopen: guestfs_create: failed to create handle\n")); return -1; } /* Now copy some of the settings from the old handle. The settings * we copy are those which are set by guestfish itself. */ cb = guestfs_get_error_handler (g, &cb_data); guestfs_set_error_handler (g2, cb, cb_data); r = guestfs_get_verbose (g); if (r >= 0) guestfs_set_verbose (g2, r); r = guestfs_get_trace (g); if (r >= 0) guestfs_set_trace (g2, r); r = guestfs_get_autosync (g); if (r >= 0) guestfs_set_autosync (g2, r); p = guestfs_get_path (g); if (p) guestfs_set_path (g2, p); r = guestfs_get_pgroup (g); if (r >= 0) guestfs_set_pgroup (g2, r); if (progress_bars) guestfs_set_event_callback (g2, progress_callback, GUESTFS_EVENT_PROGRESS, 0, NULL); /* Close the original handle. */ guestfs_close (g); g = g2; /* We don't bother copying event handlers over to the new handle, * but we have to reset the list because they were registered * against the old handle. */ free_event_handlers (); init_event_handlers (); return 0; }