コード例 #1
0
int
main (int argc, char *argv[])
{
  guestfs_h *g;
  int r, default_memsize;

  /* What's the default memsize? */
  g = guestfs_create ();
  if (!g) error (EXIT_FAILURE, errno, "guestfs_create");
  default_memsize = guestfs_get_memsize (g);
  if (default_memsize == -1) exit (EXIT_FAILURE);
  guestfs_close (g);

  /* Check that guestfs_create parses the environment. */
  setenv ("LIBGUESTFS_MEMSIZE", "799", 1);
  g = guestfs_create ();
  if (!g) error (EXIT_FAILURE, errno, "guestfs_create");
  assert (guestfs_get_memsize (g) == 799);
  guestfs_close (g);

  /* Check that guestfs_create_flags with no flags parses the environment. */
  setenv ("LIBGUESTFS_MEMSIZE", "798", 1);
  g = guestfs_create_flags (0);
  if (!g) error (EXIT_FAILURE, errno, "guestfs_create_flags");
  assert (guestfs_get_memsize (g) == 798);
  guestfs_close (g);

  /* Check guestfs_create_flags + explicit guestfs_parse_environment. */
  setenv ("LIBGUESTFS_MEMSIZE", "797", 1);
  g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
  assert (guestfs_get_memsize (g) == default_memsize);
  if (!g) error (EXIT_FAILURE, errno, "guestfs_create_flags");
  setenv ("LIBGUESTFS_MEMSIZE", "796", 1);
  r = guestfs_parse_environment (g);
  if (r == -1) exit (EXIT_FAILURE);
  assert (guestfs_get_memsize (g) == 796);
  guestfs_close (g);

  /* Check guestfs_parse_environment_list. */
  setenv ("LIBGUESTFS_MEMSIZE", "795", 1);
  g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
  assert (guestfs_get_memsize (g) == default_memsize);
  if (!g) error (EXIT_FAILURE, errno, "guestfs_create_flags");
  setenv ("LIBGUESTFS_MEMSIZE", "794", 1);
  const char *local_environment[] = {
    "LIBGUESTFS_MEMSIZE=793",
    "LIBGUESTFS_MEMSIZE_NOT_REALLY_A_VARIABLE=1",
    "FOO=bar",
    "HOME=/homes",
    "BLAH",
    NULL
  };
  r = guestfs_parse_environment_list (g, (char **) local_environment);
  if (r == -1) exit (EXIT_FAILURE);
  assert (guestfs_get_memsize (g) == 793);
  guestfs_close (g);

  exit (EXIT_SUCCESS);
}
コード例 #2
0
ファイル: handle.c プロジェクト: myyyy/libguestfs
guestfs_h *
guestfs_create_flags (unsigned flags, ...)
{
  guestfs_h *g;

  g = calloc (1, sizeof (*g));
  if (!g) return NULL;

  g->state = CONFIG;

  g->conn = NULL;

  guestfs_int_init_error_handler (g);
  g->abort_cb = abort;

  g->recovery_proc = 1;
  g->autosync = 1;

  g->memsize = DEFAULT_MEMSIZE;

  /* Start with large serial numbers so they are easy to spot
   * inside the protocol.
   */
  g->msg_next_serial = 0x00123400;

  /* Default is uniprocessor appliance. */
  g->smp = 1;

  g->path = strdup (GUESTFS_DEFAULT_PATH);
  if (!g->path) goto error;

#ifdef QEMU
  g->hv = strdup (QEMU);
#else
  /* configure --without-qemu, so set QEMU to something which will
   * definitely fail.  The user is expected to override the hypervisor
   * by setting an environment variable or calling set_hv.
   */
  g->hv = strdup ("false");
#endif
  if (!g->hv) goto error;

  /* Get program name. */
#if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
  if (STRPREFIX (program_invocation_short_name, "lt-"))
    /* Remove libtool (lt-*) prefix from short name. */
    g->program = strdup (program_invocation_short_name + 3);
  else
    g->program = strdup (program_invocation_short_name);
#else
  g->program = strdup ("");
#endif
  if (!g->program) goto error;

  g->identifier = strdup ("");
  if (!g->identifier) goto error;

  if (guestfs_int_set_backend (g, DEFAULT_BACKEND) == -1) {
    warning (g, _("libguestfs was built with an invalid default backend, using 'direct' instead"));
    if (guestfs_int_set_backend (g, "direct") == -1) {
      warning (g, _("'direct' backend does not work"));
      goto error;
    }
  }

  if (!(flags & GUESTFS_CREATE_NO_ENVIRONMENT))
    ignore_value (guestfs_parse_environment (g));

  if (!(flags & GUESTFS_CREATE_NO_CLOSE_ON_EXIT)) {
    g->close_on_exit = true;

    /* Link the handles onto a global list. */
    gl_lock_lock (handles_lock);
    g->next = handles;
    handles = g;
    if (!atexit_handler_set) {
      atexit (close_handles);
      atexit_handler_set = 1;
    }
    gl_lock_unlock (handles_lock);
  }

  debug (g, "create: flags = %u, handle = %p, program = %s",
         flags, g, g->program);

  return g;

 error:
  guestfs_int_free_string_list (g->backend_settings);
  free (g->backend);
  free (g->identifier);
  free (g->program);
  free (g->path);
  free (g->hv);
  free (g->append);
  free (g);
  return NULL;
}