Ejemplo n.º 1
0
static int
read_lpj_common (guestfs_h *g, const char *func, struct command *cmd)
{
  int r;
  CLEANUP_FREE char *buf = NULL;

  guestfs___cmd_set_stdout_callback (cmd, read_all, &buf,
                                     CMD_STDOUT_FLAG_WHOLE_BUFFER);
  r = guestfs___cmd_run (cmd);
  if (r == -1)
    return -1;
  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
    char status_string[80];

    debug (g, "%s: %s", func,
           guestfs___exit_status_to_string (r, "external command",
                                            status_string,
                                            sizeof status_string));
    return -1;
  }

  if (buf == NULL) {
    debug (g, "%s: callback not called", func);
    return -1;
  }

  if (strlen (buf) < 4 || sscanf (&buf[4], "%d", &r) != 1) {
    debug (g, "%s: invalid buffer returned by grep: %s", func, buf);
    return -1;
  }

  debug (g, "%s: calculated lpj=%d", func, r);

  return r;
}
Ejemplo n.º 2
0
static int
read_lpj_common (guestfs_h *g, const char *func, struct command *cmd)
{
  int r;
  char *buf = NULL;

  guestfs___cmd_set_stdout_callback (cmd, read_all, &buf,
                                     CMD_STDOUT_FLAG_WHOLE_BUFFER);
  r = guestfs___cmd_run (cmd);
  guestfs___cmd_close (cmd);

  if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) {
    debug (g, "%s: external command failed with code %d", func, r);
    free (buf);
    return -1;
  }

  if (buf == NULL) {
    debug (g, "%s: callback not called", func);
    return -1;
  }

  if (strlen (buf) < 4 || sscanf (&buf[4], "%d", &r) != 1) {
    debug (g, "%s: invalid buffer returned by grep: %s", func, buf);
    free (buf);
    return -1;
  }

  free (buf);

  debug (g, "%s: calculated lpj=%d", func, r);

  return r;
}
Ejemplo n.º 3
0
static int
run_qemu_img_info (guestfs_h *g, const char *filename,
                   cmd_stdout_callback fn, void *data)
{
  char *abs_filename = NULL;
  char *safe_filename = NULL;
  struct command *cmd;
  int r;

  if (guestfs___lazy_make_tmpdir (g) == -1)
    return -1;

  safe_filename = safe_asprintf (g, "%s/format.%d", g->tmpdir, ++g->unique);

  /* 'filename' must be an absolute path so we can link to it. */
  abs_filename = realpath (filename, NULL);
  if (abs_filename == NULL) {
    perrorf (g, "realpath");
    goto error;
  }

  if (symlink (abs_filename, safe_filename) == -1) {
    perrorf (g, "symlink");
    goto error;
  }

  cmd = guestfs___new_command (g);
  guestfs___cmd_add_arg (cmd, "qemu-img");
  guestfs___cmd_add_arg (cmd, "info");
  guestfs___cmd_add_arg (cmd, safe_filename);
  guestfs___cmd_set_stdout_callback (cmd, fn, data, 0);
  r = guestfs___cmd_run (cmd);
  guestfs___cmd_close (cmd);
  if (r == -1)
    goto error;
  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
    error (g, _("qemu-img: %s: child process failed"), filename);
    goto error;
  }

  free (safe_filename);
  free (abs_filename);
  return 0;

 error:
  free (safe_filename);
  free (abs_filename);

  return -1;
}