summaryrefslogtreecommitdiffstats
path: root/src/info.c
blob: 76f1f4e016b26c3c7c4e4ddee13589a58f75a17c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* libguestfs
 * Copyright (C) 2012 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
#include "guestfs_protocol.h"

#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif

static int run_qemu_img_info (guestfs_h *g, const char *filename, int (*fn) (guestfs_h *g, char *line, void *data), void *data);
static int check_disk_format (guestfs_h *h, char *line, void *data);
static int check_disk_virtual_size (guestfs_h *h, char *line, void *data);
static int check_disk_has_backing_file (guestfs_h *h, char *line, void *data);

char *
guestfs__disk_format (guestfs_h *g, const char *filename)
{
  char *ret = NULL;

  if (run_qemu_img_info (g, filename, check_disk_format, &ret) == -1) {
    free (ret);
    return NULL;
  }

  if (ret == NULL)
    ret = safe_strdup (g, "unknown");

  return ret;
}

static int
check_disk_format (guestfs_h *g, char *line, void *retpv)
{
  char **retp = retpv;
  char *p;
  size_t n;

  if (STRPREFIX (line, "file format: ")) {
    p = &line[13];
    n = strlen (p);
    if (n > 0 && p[n-1] == '\n')
      p[n-1] = '\0';
    *retp = safe_strdup (g, p);
    return 0;                   /* finish processing */
  }

  return 1;                     /* continue processing */
}

int64_t
guestfs__disk_virtual_size (guestfs_h *g, const char *filename)
{
  int64_t ret = -1;

  if (run_qemu_img_info (g, filename, check_disk_virtual_size, &ret) == -1)
    return -1;

  if (ret == -1)
    error (g, _("%s: cannot detect virtual size of disk image"), filename);

  return ret;
}

static int
check_disk_virtual_size (guestfs_h *g, char *line, void *retpv)
{
  int64_t *retp = retpv;
  char *p;

  if (STRPREFIX (line, "virtual size: ")) {
    /* "virtual size: 500M (524288000 bytes)\n" */
    p = &line[14];
    p = strchr (p, ' ');
    if (!p || p[1] != '(' || sscanf (&p[2], "%" SCNi64, retp) != 1) {
      error (g, _("cannot parse output of qemu-img info: '%s'"),
             line);
      return -1;
    }
    return 0;                   /* finish processing */
  }

  return 1;                     /* continue processing */
}

int
guestfs__disk_has_backing_file (guestfs_h *g, const char *filename)
{
  int ret = 0;

  if (run_qemu_img_info (g, filename, check_disk_has_backing_file, &ret) == -1)
    return -1;

  return ret;
}

static int
check_disk_has_backing_file (guestfs_h *g, char *line, void *retpv)
{
  int *retp = retpv;

  if (STRPREFIX (line, "backing file: ")) {
    *retp = 1;
    return 0;                   /* finish processing */
  }

  return 1;                     /* continue processing */
}

/* It's hard to use 'qemu-img info' safely.  See:
 * https://lists.gnu.org/archive/html/qemu-devel/2012-09/msg00137.html
 * Eventually we should switch to the JSON output format, when it
 * becomes available.  In the meantime: (1) make a symlink to ensure
 * we control the input filename, and (2) bail parsing as soon as
 * /^backing file: / is seen in the input.
 */
static int
run_qemu_img_info (guestfs_h *g, const char *filename,
                   int (*fn) (guestfs_h *g, char *line, void *data),
                   void *data)
{
  char *abs_filename = NULL;
  char *safe_filename = NULL;
  pid_t pid = 0;
  int fd[2] = { -1, -1 };
  FILE *fp = NULL;
  char *line = NULL;
  size_t len;
  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;
  }

  if (pipe2 (fd, O_CLOEXEC) == -1) {
    perrorf (g, "pipe2");
    goto error;
  }

  pid = fork ();
  if (pid == -1) {
    perrorf (g, "fork");
    goto error;
  }

  if (pid == 0) {               /* child */
    close (fd[0]);
    dup2 (fd[1], 1);
    close (fd[1]);

    setenv ("LC_ALL", "C", 1);

    /* XXX stderr to event log */

    execlp ("qemu-img", "qemu-img", "info", safe_filename, NULL);
    perror ("could not execute 'qemu-img info' command");
    _exit (EXIT_FAILURE);
  }

  close (fd[1]);
  fd[1] = -1;

  fp = fdopen (fd[0], "r");
  if (fp == NULL) {
    perrorf (g, "fdopen: qemu-img info");
    goto error;
  }
  fd[0] = -1;

  while (getline (&line, &len, fp) != -1) {
    r = fn (g, line, data);
    if (r == -1)                /* error */
      goto error;
    if (r == 0)                 /* finished processing */
      break;

    /* This is for security reasons, see comment above. */
    if (STRPREFIX (line, "backing file: "))
      break;
  }

  if (fclose (fp) == -1) { /* also closes fd[0] */
    perrorf (g, "fclose");
    fp = NULL;
    goto error;
  }
  fp = NULL;

  if (waitpid (pid, &r, 0) == -1) {
    perrorf (g, "waitpid");
    pid = 0;
    goto error;
  }
  pid = 0;

  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
    error (g, "qemu-img: %s: child process failed", filename);
    goto error;
  }

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

 error:
  if (fd[0] >= 0)
    close (fd[0]);
  if (fd[1] >= 0)
    close (fd[1]);
  if (fp != NULL)
    fclose (fp);
  if (pid > 0)
    waitpid (pid, NULL, 0);

  free (safe_filename);
  free (abs_filename);
  free (line);

  return -1;
}