blob: 2eec712526ad05be506d276561e4f5c6d6ed7ef6 [file] [log] [blame]
Michael Rothe3d4d252011-07-19 15:41:55 -05001/*
Michael Roth42074a92012-01-19 22:19:27 -06002 * QEMU Guest Agent POSIX-specific command implementations
Michael Rothe3d4d252011-07-19 15:41:55 -05003 *
4 * Copyright IBM Corp. 2011
5 *
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
Michal Privoznik3424fc92012-02-29 17:02:23 +01008 * Michal Privoznik <mprivozn@redhat.com>
Michael Rothe3d4d252011-07-19 15:41:55 -05009 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
14#include <glib.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050015#include <sys/types.h>
16#include <sys/ioctl.h>
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030017#include <sys/wait.h>
Laszlo Ersekd2baff62013-03-06 22:59:30 +010018#include <unistd.h>
19#include <errno.h>
20#include <fcntl.h>
Laszlo Ersekc689b4f2013-04-24 13:13:18 +020021#include <stdio.h>
22#include <string.h>
23#include <sys/stat.h>
Laszlo Ersekd2baff62013-03-06 22:59:30 +010024#include <inttypes.h>
Michael Rothe3d4d252011-07-19 15:41:55 -050025#include "qga/guest-agent-core.h"
26#include "qga-qmp-commands.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/queue.h"
29#include "qemu/host-utils.h"
Michael Rothe3d4d252011-07-19 15:41:55 -050030
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030031#ifndef CONFIG_HAS_ENVIRON
Andreas Färbereecae142012-05-27 17:02:20 +020032#ifdef __APPLE__
33#include <crt_externs.h>
34#define environ (*_NSGetEnviron())
35#else
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030036extern char **environ;
37#endif
Andreas Färbereecae142012-05-27 17:02:20 +020038#endif
Luiz Capitulino2c02cbf2012-05-23 15:48:05 -030039
Michael Rothe72c3f22012-03-25 13:59:41 -050040#if defined(__linux__)
41#include <mntent.h>
42#include <linux/fs.h>
43#include <ifaddrs.h>
44#include <arpa/inet.h>
45#include <sys/socket.h>
46#include <net/if.h>
Michael Rothe72c3f22012-03-25 13:59:41 -050047
Paolo Bonzinieab5fd52012-06-13 07:41:28 +020048#ifdef FIFREEZE
Michael Rothe72c3f22012-03-25 13:59:41 -050049#define CONFIG_FSFREEZE
50#endif
Paolo Bonzinieab5fd52012-06-13 07:41:28 +020051#ifdef FITRIM
52#define CONFIG_FSTRIM
53#endif
Michael Rothe72c3f22012-03-25 13:59:41 -050054#endif
55
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020056static void ga_wait_child(pid_t pid, int *status, Error **err)
57{
58 pid_t rpid;
59
60 *status = 0;
61
62 do {
63 rpid = waitpid(pid, status, 0);
64 } while (rpid == -1 && errno == EINTR);
65
66 if (rpid == -1) {
67 error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
68 return;
69 }
70
71 g_assert(rpid == pid);
72}
73
Michael Rothe3d4d252011-07-19 15:41:55 -050074void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
75{
Michael Rothe3d4d252011-07-19 15:41:55 -050076 const char *shutdown_flag;
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020077 Error *local_err = NULL;
78 pid_t pid;
Luiz Capitulino36748382012-05-14 15:25:20 -030079 int status;
Michael Rothe3d4d252011-07-19 15:41:55 -050080
81 slog("guest-shutdown called, mode: %s", mode);
82 if (!has_mode || strcmp(mode, "powerdown") == 0) {
83 shutdown_flag = "-P";
84 } else if (strcmp(mode, "halt") == 0) {
85 shutdown_flag = "-H";
86 } else if (strcmp(mode, "reboot") == 0) {
87 shutdown_flag = "-r";
88 } else {
Luiz Capitulinod220a6d2012-11-27 11:01:58 -020089 error_setg(err,
90 "mode is invalid (valid values are: halt|powerdown|reboot");
Michael Rothe3d4d252011-07-19 15:41:55 -050091 return;
92 }
93
Luiz Capitulinod5dd3492012-05-11 16:19:47 -030094 pid = fork();
95 if (pid == 0) {
Michael Rothe3d4d252011-07-19 15:41:55 -050096 /* child, start the shutdown */
97 setsid();
Luiz Capitulino36748382012-05-14 15:25:20 -030098 reopen_fd_to_null(0);
99 reopen_fd_to_null(1);
100 reopen_fd_to_null(2);
Michael Rothe3d4d252011-07-19 15:41:55 -0500101
Luiz Capitulino36748382012-05-14 15:25:20 -0300102 execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
103 "hypervisor initiated shutdown", (char*)NULL, environ);
104 _exit(EXIT_FAILURE);
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300105 } else if (pid < 0) {
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200106 error_setg_errno(err, errno, "failed to create child process");
Luiz Capitulinod5dd3492012-05-11 16:19:47 -0300107 return;
108 }
109
Luiz Capitulinod220a6d2012-11-27 11:01:58 -0200110 ga_wait_child(pid, &status, &local_err);
111 if (error_is_set(&local_err)) {
112 error_propagate(err, local_err);
113 return;
114 }
115
116 if (!WIFEXITED(status)) {
117 error_setg(err, "child process has terminated abnormally");
118 return;
119 }
120
121 if (WEXITSTATUS(status)) {
122 error_setg(err, "child process has failed to shutdown");
123 return;
124 }
125
Peter Maydell085d8132013-03-18 17:20:07 +0000126 /* succeeded */
Michael Rothe3d4d252011-07-19 15:41:55 -0500127}
128
Lei Li6912e6a2013-03-05 17:39:11 +0800129int64_t qmp_guest_get_time(Error **errp)
130{
131 int ret;
132 qemu_timeval tq;
133 int64_t time_ns;
134
135 ret = qemu_gettimeofday(&tq);
136 if (ret < 0) {
137 error_setg_errno(errp, errno, "Failed to get time");
138 return -1;
139 }
140
141 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
142 return time_ns;
143}
144
Lei Lia1bca572013-03-05 17:39:12 +0800145void qmp_guest_set_time(int64_t time_ns, Error **errp)
146{
147 int ret;
148 int status;
149 pid_t pid;
150 Error *local_err = NULL;
151 struct timeval tv;
152
153 /* year-2038 will overflow in case time_t is 32bit */
154 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
155 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
156 return;
157 }
158
159 tv.tv_sec = time_ns / 1000000000;
160 tv.tv_usec = (time_ns % 1000000000) / 1000;
161
162 ret = settimeofday(&tv, NULL);
163 if (ret < 0) {
164 error_setg_errno(errp, errno, "Failed to set time to guest");
165 return;
166 }
167
168 /* Set the Hardware Clock to the current System Time. */
169 pid = fork();
170 if (pid == 0) {
171 setsid();
172 reopen_fd_to_null(0);
173 reopen_fd_to_null(1);
174 reopen_fd_to_null(2);
175
176 execle("/sbin/hwclock", "hwclock", "-w", NULL, environ);
177 _exit(EXIT_FAILURE);
178 } else if (pid < 0) {
179 error_setg_errno(errp, errno, "failed to create child process");
180 return;
181 }
182
183 ga_wait_child(pid, &status, &local_err);
184 if (error_is_set(&local_err)) {
185 error_propagate(errp, local_err);
186 return;
187 }
188
189 if (!WIFEXITED(status)) {
190 error_setg(errp, "child process has terminated abnormally");
191 return;
192 }
193
194 if (WEXITSTATUS(status)) {
195 error_setg(errp, "hwclock failed to set hardware clock to system time");
196 return;
197 }
198}
199
Michael Rothe3d4d252011-07-19 15:41:55 -0500200typedef struct GuestFileHandle {
201 uint64_t id;
202 FILE *fh;
203 QTAILQ_ENTRY(GuestFileHandle) next;
204} GuestFileHandle;
205
206static struct {
207 QTAILQ_HEAD(, GuestFileHandle) filehandles;
208} guest_file_state;
209
Michael Roth39097da2013-03-01 11:40:27 -0600210static int64_t guest_file_handle_add(FILE *fh, Error **errp)
Michael Rothe3d4d252011-07-19 15:41:55 -0500211{
212 GuestFileHandle *gfh;
Michael Roth39097da2013-03-01 11:40:27 -0600213 int64_t handle;
214
215 handle = ga_get_fd_handle(ga_state, errp);
216 if (error_is_set(errp)) {
217 return 0;
218 }
Michael Rothe3d4d252011-07-19 15:41:55 -0500219
Anthony Liguori7267c092011-08-20 22:09:37 -0500220 gfh = g_malloc0(sizeof(GuestFileHandle));
Michael Roth39097da2013-03-01 11:40:27 -0600221 gfh->id = handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500222 gfh->fh = fh;
223 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
Michael Roth39097da2013-03-01 11:40:27 -0600224
225 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500226}
227
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200228static GuestFileHandle *guest_file_handle_find(int64_t id, Error **err)
Michael Rothe3d4d252011-07-19 15:41:55 -0500229{
230 GuestFileHandle *gfh;
231
232 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
233 {
234 if (gfh->id == id) {
235 return gfh;
236 }
237 }
238
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200239 error_setg(err, "handle '%" PRId64 "' has not been found", id);
Michael Rothe3d4d252011-07-19 15:41:55 -0500240 return NULL;
241}
242
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200243typedef const char * const ccpc;
244
Laszlo Ersek8fe6bbc2013-05-08 17:31:35 +0200245#ifndef O_BINARY
246#define O_BINARY 0
247#endif
248
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200249/* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
250static const struct {
251 ccpc *forms;
252 int oflag_base;
253} guest_file_open_modes[] = {
Laszlo Ersek8fe6bbc2013-05-08 17:31:35 +0200254 { (ccpc[]){ "r", NULL }, O_RDONLY },
255 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY },
256 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC },
257 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY },
258 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND },
259 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY },
260 { (ccpc[]){ "r+", NULL }, O_RDWR },
261 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY },
262 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC },
263 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY },
264 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND },
265 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY }
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200266};
267
268static int
269find_open_flag(const char *mode_str, Error **err)
270{
271 unsigned mode;
272
273 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
274 ccpc *form;
275
276 form = guest_file_open_modes[mode].forms;
277 while (*form != NULL && strcmp(*form, mode_str) != 0) {
278 ++form;
279 }
280 if (*form != NULL) {
281 break;
282 }
283 }
284
285 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
286 error_setg(err, "invalid file open mode '%s'", mode_str);
287 return -1;
288 }
289 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
290}
291
292#define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
293 S_IRGRP | S_IWGRP | \
294 S_IROTH | S_IWOTH)
295
296static FILE *
297safe_open_or_create(const char *path, const char *mode, Error **err)
298{
299 Error *local_err = NULL;
300 int oflag;
301
302 oflag = find_open_flag(mode, &local_err);
303 if (local_err == NULL) {
304 int fd;
305
306 /* If the caller wants / allows creation of a new file, we implement it
307 * with a two step process: open() + (open() / fchmod()).
308 *
309 * First we insist on creating the file exclusively as a new file. If
310 * that succeeds, we're free to set any file-mode bits on it. (The
311 * motivation is that we want to set those file-mode bits independently
312 * of the current umask.)
313 *
314 * If the exclusive creation fails because the file already exists
315 * (EEXIST is not possible for any other reason), we just attempt to
316 * open the file, but in this case we won't be allowed to change the
317 * file-mode bits on the preexistent file.
318 *
319 * The pathname should never disappear between the two open()s in
320 * practice. If it happens, then someone very likely tried to race us.
321 * In this case just go ahead and report the ENOENT from the second
322 * open() to the caller.
323 *
324 * If the caller wants to open a preexistent file, then the first
325 * open() is decisive and its third argument is ignored, and the second
326 * open() and the fchmod() are never called.
327 */
328 fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0);
329 if (fd == -1 && errno == EEXIST) {
330 oflag &= ~(unsigned)O_CREAT;
331 fd = open(path, oflag);
332 }
333
334 if (fd == -1) {
335 error_setg_errno(&local_err, errno, "failed to open file '%s' "
336 "(mode: '%s')", path, mode);
337 } else {
338 qemu_set_cloexec(fd);
339
340 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) {
341 error_setg_errno(&local_err, errno, "failed to set permission "
342 "0%03o on new file '%s' (mode: '%s')",
343 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode);
344 } else {
345 FILE *f;
346
347 f = fdopen(fd, mode);
348 if (f == NULL) {
349 error_setg_errno(&local_err, errno, "failed to associate "
350 "stdio stream with file descriptor %d, "
351 "file '%s' (mode: '%s')", fd, path, mode);
352 } else {
353 return f;
354 }
355 }
356
357 close(fd);
358 }
359 }
360
361 error_propagate(err, local_err);
362 return NULL;
363}
364
Michael Rothe3d4d252011-07-19 15:41:55 -0500365int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
366{
367 FILE *fh;
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200368 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500369 int fd;
Michael Roth39097da2013-03-01 11:40:27 -0600370 int64_t ret = -1, handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500371
372 if (!has_mode) {
373 mode = "r";
374 }
375 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200376 fh = safe_open_or_create(path, mode, &local_err);
377 if (local_err != NULL) {
378 error_propagate(err, local_err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500379 return -1;
380 }
381
382 /* set fd non-blocking to avoid common use cases (like reading from a
383 * named pipe) from hanging the agent
384 */
385 fd = fileno(fh);
386 ret = fcntl(fd, F_GETFL);
387 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
388 if (ret == -1) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200389 error_setg_errno(err, errno, "failed to make file '%s' non-blocking",
390 path);
Michael Rothe3d4d252011-07-19 15:41:55 -0500391 fclose(fh);
392 return -1;
393 }
394
Michael Roth39097da2013-03-01 11:40:27 -0600395 handle = guest_file_handle_add(fh, err);
396 if (error_is_set(err)) {
397 fclose(fh);
398 return -1;
399 }
400
401 slog("guest-file-open, handle: %d", handle);
402 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500403}
404
405void qmp_guest_file_close(int64_t handle, Error **err)
406{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200407 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500408 int ret;
409
410 slog("guest-file-close called, handle: %ld", handle);
411 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500412 return;
413 }
414
415 ret = fclose(gfh->fh);
Luiz Capitulino3ac4b7c2012-11-27 11:01:56 -0200416 if (ret == EOF) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200417 error_setg_errno(err, errno, "failed to close handle");
Michael Rothe3d4d252011-07-19 15:41:55 -0500418 return;
419 }
420
421 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500422 g_free(gfh);
Michael Rothe3d4d252011-07-19 15:41:55 -0500423}
424
425struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
426 int64_t count, Error **err)
427{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200428 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500429 GuestFileRead *read_data = NULL;
430 guchar *buf;
431 FILE *fh;
432 size_t read_count;
433
434 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500435 return NULL;
436 }
437
438 if (!has_count) {
439 count = QGA_READ_COUNT_DEFAULT;
440 } else if (count < 0) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200441 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
442 count);
Michael Rothe3d4d252011-07-19 15:41:55 -0500443 return NULL;
444 }
445
446 fh = gfh->fh;
Anthony Liguori7267c092011-08-20 22:09:37 -0500447 buf = g_malloc0(count+1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500448 read_count = fread(buf, 1, count, fh);
449 if (ferror(fh)) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200450 error_setg_errno(err, errno, "failed to read file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500451 slog("guest-file-read failed, handle: %ld", handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500452 } else {
453 buf[read_count] = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500454 read_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500455 read_data->count = read_count;
456 read_data->eof = feof(fh);
457 if (read_count) {
458 read_data->buf_b64 = g_base64_encode(buf, read_count);
459 }
460 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500461 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500462 clearerr(fh);
463
464 return read_data;
465}
466
467GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
468 bool has_count, int64_t count, Error **err)
469{
470 GuestFileWrite *write_data = NULL;
471 guchar *buf;
472 gsize buf_len;
473 int write_count;
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200474 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500475 FILE *fh;
476
477 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500478 return NULL;
479 }
480
481 fh = gfh->fh;
482 buf = g_base64_decode(buf_b64, &buf_len);
483
484 if (!has_count) {
485 count = buf_len;
486 } else if (count < 0 || count > buf_len) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200487 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
488 count);
Anthony Liguori7267c092011-08-20 22:09:37 -0500489 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500490 return NULL;
491 }
492
493 write_count = fwrite(buf, 1, count, fh);
494 if (ferror(fh)) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200495 error_setg_errno(err, errno, "failed to write to file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500496 slog("guest-file-write failed, handle: %ld", handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500497 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500498 write_data = g_malloc0(sizeof(GuestFileWrite));
Michael Rothe3d4d252011-07-19 15:41:55 -0500499 write_data->count = write_count;
500 write_data->eof = feof(fh);
501 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500502 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500503 clearerr(fh);
504
505 return write_data;
506}
507
508struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
509 int64_t whence, Error **err)
510{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200511 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500512 GuestFileSeek *seek_data = NULL;
513 FILE *fh;
514 int ret;
515
516 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500517 return NULL;
518 }
519
520 fh = gfh->fh;
521 ret = fseek(fh, offset, whence);
522 if (ret == -1) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200523 error_setg_errno(err, errno, "failed to seek file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500524 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500525 seek_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500526 seek_data->position = ftell(fh);
527 seek_data->eof = feof(fh);
528 }
529 clearerr(fh);
530
531 return seek_data;
532}
533
534void qmp_guest_file_flush(int64_t handle, Error **err)
535{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200536 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500537 FILE *fh;
538 int ret;
539
540 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500541 return;
542 }
543
544 fh = gfh->fh;
545 ret = fflush(fh);
546 if (ret == EOF) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200547 error_setg_errno(err, errno, "failed to flush file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500548 }
549}
550
551static void guest_file_init(void)
552{
553 QTAILQ_INIT(&guest_file_state.filehandles);
554}
555
Michael Rothe72c3f22012-03-25 13:59:41 -0500556/* linux-specific implementations. avoid this if at all possible. */
557#if defined(__linux__)
558
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200559#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
Paolo Bonziniaf022032012-06-13 07:41:27 +0200560typedef struct FsMount {
Michael Rothe3d4d252011-07-19 15:41:55 -0500561 char *dirname;
562 char *devtype;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200563 QTAILQ_ENTRY(FsMount) next;
564} FsMount;
Michael Rothe3d4d252011-07-19 15:41:55 -0500565
Paolo Bonziniaf022032012-06-13 07:41:27 +0200566typedef QTAILQ_HEAD(, FsMount) FsMountList;
Michael Roth9e8aded432012-04-16 19:52:17 -0500567
Paolo Bonziniaf022032012-06-13 07:41:27 +0200568static void free_fs_mount_list(FsMountList *mounts)
Michael Roth9e8aded432012-04-16 19:52:17 -0500569{
Paolo Bonziniaf022032012-06-13 07:41:27 +0200570 FsMount *mount, *temp;
Michael Roth9e8aded432012-04-16 19:52:17 -0500571
572 if (!mounts) {
573 return;
574 }
575
576 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
577 QTAILQ_REMOVE(mounts, mount, next);
578 g_free(mount->dirname);
579 g_free(mount->devtype);
580 g_free(mount);
581 }
582}
583
Michael Rothe3d4d252011-07-19 15:41:55 -0500584/*
585 * Walk the mount table and build a list of local file systems
586 */
Luiz Capitulino261551d2012-11-29 15:29:11 -0200587static void build_fs_mount_list(FsMountList *mounts, Error **err)
Michael Rothe3d4d252011-07-19 15:41:55 -0500588{
589 struct mntent *ment;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200590 FsMount *mount;
Michael Roth9e2fa412012-05-29 10:08:50 -0500591 char const *mtab = "/proc/self/mounts";
Michael Rothe3d4d252011-07-19 15:41:55 -0500592 FILE *fp;
593
Michael Rothe3d4d252011-07-19 15:41:55 -0500594 fp = setmntent(mtab, "r");
595 if (!fp) {
Luiz Capitulino261551d2012-11-29 15:29:11 -0200596 error_setg(err, "failed to open mtab file: '%s'", mtab);
597 return;
Michael Rothe3d4d252011-07-19 15:41:55 -0500598 }
599
600 while ((ment = getmntent(fp))) {
601 /*
602 * An entry which device name doesn't start with a '/' is
603 * either a dummy file system or a network file system.
604 * Add special handling for smbfs and cifs as is done by
605 * coreutils as well.
606 */
607 if ((ment->mnt_fsname[0] != '/') ||
608 (strcmp(ment->mnt_type, "smbfs") == 0) ||
609 (strcmp(ment->mnt_type, "cifs") == 0)) {
610 continue;
611 }
612
Paolo Bonziniaf022032012-06-13 07:41:27 +0200613 mount = g_malloc0(sizeof(FsMount));
Anthony Liguori7267c092011-08-20 22:09:37 -0500614 mount->dirname = g_strdup(ment->mnt_dir);
615 mount->devtype = g_strdup(ment->mnt_type);
Michael Rothe3d4d252011-07-19 15:41:55 -0500616
Michael Roth9e8aded432012-04-16 19:52:17 -0500617 QTAILQ_INSERT_TAIL(mounts, mount, next);
Michael Rothe3d4d252011-07-19 15:41:55 -0500618 }
619
620 endmntent(fp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500621}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200622#endif
623
624#if defined(CONFIG_FSFREEZE)
Michael Rothe3d4d252011-07-19 15:41:55 -0500625
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900626typedef enum {
627 FSFREEZE_HOOK_THAW = 0,
628 FSFREEZE_HOOK_FREEZE,
629} FsfreezeHookArg;
630
631const char *fsfreeze_hook_arg_string[] = {
632 "thaw",
633 "freeze",
634};
635
636static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
637{
638 int status;
639 pid_t pid;
640 const char *hook;
641 const char *arg_str = fsfreeze_hook_arg_string[arg];
642 Error *local_err = NULL;
643
644 hook = ga_fsfreeze_hook(ga_state);
645 if (!hook) {
646 return;
647 }
648 if (access(hook, X_OK) != 0) {
649 error_setg_errno(err, errno, "can't access fsfreeze hook '%s'", hook);
650 return;
651 }
652
653 slog("executing fsfreeze hook with arg '%s'", arg_str);
654 pid = fork();
655 if (pid == 0) {
656 setsid();
657 reopen_fd_to_null(0);
658 reopen_fd_to_null(1);
659 reopen_fd_to_null(2);
660
661 execle(hook, hook, arg_str, NULL, environ);
662 _exit(EXIT_FAILURE);
663 } else if (pid < 0) {
664 error_setg_errno(err, errno, "failed to create child process");
665 return;
666 }
667
668 ga_wait_child(pid, &status, &local_err);
669 if (error_is_set(&local_err)) {
670 error_propagate(err, local_err);
671 return;
672 }
673
674 if (!WIFEXITED(status)) {
675 error_setg(err, "fsfreeze hook has terminated abnormally");
676 return;
677 }
678
679 status = WEXITSTATUS(status);
680 if (status) {
681 error_setg(err, "fsfreeze hook has failed with status %d", status);
682 return;
683 }
684}
685
Michael Rothe3d4d252011-07-19 15:41:55 -0500686/*
687 * Return status of freeze/thaw
688 */
689GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
690{
Michael Rothf22d85e2012-04-17 19:01:45 -0500691 if (ga_is_frozen(ga_state)) {
692 return GUEST_FSFREEZE_STATUS_FROZEN;
693 }
694
695 return GUEST_FSFREEZE_STATUS_THAWED;
Michael Rothe3d4d252011-07-19 15:41:55 -0500696}
697
698/*
699 * Walk list of mounted file systems in the guest, and freeze the ones which
700 * are real local file systems.
701 */
702int64_t qmp_guest_fsfreeze_freeze(Error **err)
703{
704 int ret = 0, i = 0;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200705 FsMountList mounts;
706 struct FsMount *mount;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200707 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500708 int fd;
Michael Rothe3d4d252011-07-19 15:41:55 -0500709
710 slog("guest-fsfreeze called");
711
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900712 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
713 if (error_is_set(&local_err)) {
714 error_propagate(err, local_err);
715 return -1;
716 }
717
Michael Roth9e8aded432012-04-16 19:52:17 -0500718 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200719 build_fs_mount_list(&mounts, &local_err);
720 if (error_is_set(&local_err)) {
721 error_propagate(err, local_err);
722 return -1;
Michael Rothe3d4d252011-07-19 15:41:55 -0500723 }
724
725 /* cannot risk guest agent blocking itself on a write in this state */
Michael Rothf22d85e2012-04-17 19:01:45 -0500726 ga_set_frozen(ga_state);
Michael Rothe3d4d252011-07-19 15:41:55 -0500727
Michael Roth9e8aded432012-04-16 19:52:17 -0500728 QTAILQ_FOREACH(mount, &mounts, next) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500729 fd = qemu_open(mount->dirname, O_RDONLY);
730 if (fd == -1) {
Luiz Capitulino617fbbc2012-11-27 11:02:00 -0200731 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
Michael Rothe3d4d252011-07-19 15:41:55 -0500732 goto error;
733 }
734
735 /* we try to cull filesytems we know won't work in advance, but other
736 * filesytems may not implement fsfreeze for less obvious reasons.
Michael Roth9e8aded432012-04-16 19:52:17 -0500737 * these will report EOPNOTSUPP. we simply ignore these when tallying
738 * the number of frozen filesystems.
739 *
740 * any other error means a failure to freeze a filesystem we
741 * expect to be freezable, so return an error in those cases
742 * and return system to thawed state.
Michael Rothe3d4d252011-07-19 15:41:55 -0500743 */
744 ret = ioctl(fd, FIFREEZE);
Michael Roth9e8aded432012-04-16 19:52:17 -0500745 if (ret == -1) {
746 if (errno != EOPNOTSUPP) {
Luiz Capitulino617fbbc2012-11-27 11:02:00 -0200747 error_setg_errno(err, errno, "failed to freeze %s",
748 mount->dirname);
Michael Roth9e8aded432012-04-16 19:52:17 -0500749 close(fd);
750 goto error;
751 }
752 } else {
753 i++;
Michael Rothe3d4d252011-07-19 15:41:55 -0500754 }
755 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -0500756 }
757
Paolo Bonziniaf022032012-06-13 07:41:27 +0200758 free_fs_mount_list(&mounts);
Michael Rothe3d4d252011-07-19 15:41:55 -0500759 return i;
760
761error:
Paolo Bonziniaf022032012-06-13 07:41:27 +0200762 free_fs_mount_list(&mounts);
Michael Roth9e8aded432012-04-16 19:52:17 -0500763 qmp_guest_fsfreeze_thaw(NULL);
Michael Rothe3d4d252011-07-19 15:41:55 -0500764 return 0;
765}
766
767/*
768 * Walk list of frozen file systems in the guest, and thaw them.
769 */
770int64_t qmp_guest_fsfreeze_thaw(Error **err)
771{
772 int ret;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200773 FsMountList mounts;
774 FsMount *mount;
Michael Roth9e8aded432012-04-16 19:52:17 -0500775 int fd, i = 0, logged;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200776 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500777
Michael Roth9e8aded432012-04-16 19:52:17 -0500778 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200779 build_fs_mount_list(&mounts, &local_err);
780 if (error_is_set(&local_err)) {
781 error_propagate(err, local_err);
Michael Roth9e8aded432012-04-16 19:52:17 -0500782 return 0;
783 }
784
785 QTAILQ_FOREACH(mount, &mounts, next) {
786 logged = false;
Michael Rothe3d4d252011-07-19 15:41:55 -0500787 fd = qemu_open(mount->dirname, O_RDONLY);
788 if (fd == -1) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500789 continue;
790 }
Michael Roth9e8aded432012-04-16 19:52:17 -0500791 /* we have no way of knowing whether a filesystem was actually unfrozen
792 * as a result of a successful call to FITHAW, only that if an error
793 * was returned the filesystem was *not* unfrozen by that particular
794 * call.
795 *
Jim Meyeringa31f0532012-05-09 05:12:04 +0000796 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
Michael Roth9e8aded432012-04-16 19:52:17 -0500797 * to unfreeze, continuing issuing FITHAW until an error is returned,
798 * in which case either the filesystem is in an unfreezable state, or,
799 * more likely, it was thawed previously (and remains so afterward).
800 *
801 * also, since the most recent successful call is the one that did
802 * the actual unfreeze, we can use this to provide an accurate count
803 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
804 * may * be useful for determining whether a filesystem was unfrozen
805 * during the freeze/thaw phase by a process other than qemu-ga.
806 */
807 do {
808 ret = ioctl(fd, FITHAW);
809 if (ret == 0 && !logged) {
810 i++;
811 logged = true;
812 }
813 } while (ret == 0);
Michael Rothe3d4d252011-07-19 15:41:55 -0500814 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -0500815 }
816
Michael Rothf22d85e2012-04-17 19:01:45 -0500817 ga_unset_frozen(ga_state);
Paolo Bonziniaf022032012-06-13 07:41:27 +0200818 free_fs_mount_list(&mounts);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900819
820 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, err);
821
Michael Rothe3d4d252011-07-19 15:41:55 -0500822 return i;
823}
824
Michael Rothe3d4d252011-07-19 15:41:55 -0500825static void guest_fsfreeze_cleanup(void)
826{
Michael Rothe3d4d252011-07-19 15:41:55 -0500827 Error *err = NULL;
828
Michael Rothf22d85e2012-04-17 19:01:45 -0500829 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
Markus Armbruster6f686742013-01-16 18:15:08 +0100830 qmp_guest_fsfreeze_thaw(&err);
831 if (err) {
832 slog("failed to clean up frozen filesystems: %s",
833 error_get_pretty(err));
834 error_free(err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500835 }
836 }
837}
Michael Rothe72c3f22012-03-25 13:59:41 -0500838#endif /* CONFIG_FSFREEZE */
Michael Rothe3d4d252011-07-19 15:41:55 -0500839
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200840#if defined(CONFIG_FSTRIM)
841/*
842 * Walk list of mounted file systems in the guest, and trim them.
843 */
844void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
845{
846 int ret = 0;
847 FsMountList mounts;
848 struct FsMount *mount;
849 int fd;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200850 Error *local_err = NULL;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200851 struct fstrim_range r = {
852 .start = 0,
853 .len = -1,
854 .minlen = has_minimum ? minimum : 0,
855 };
856
857 slog("guest-fstrim called");
858
859 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200860 build_fs_mount_list(&mounts, &local_err);
861 if (error_is_set(&local_err)) {
862 error_propagate(err, local_err);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200863 return;
864 }
865
866 QTAILQ_FOREACH(mount, &mounts, next) {
867 fd = qemu_open(mount->dirname, O_RDONLY);
868 if (fd == -1) {
Luiz Capitulino071673b2012-11-27 11:02:01 -0200869 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200870 goto error;
871 }
872
873 /* We try to cull filesytems we know won't work in advance, but other
874 * filesytems may not implement fstrim for less obvious reasons. These
875 * will report EOPNOTSUPP; we simply ignore these errors. Any other
876 * error means an unexpected error, so return it in those cases. In
877 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
878 */
879 ret = ioctl(fd, FITRIM, &r);
880 if (ret == -1) {
881 if (errno != ENOTTY && errno != EOPNOTSUPP) {
Luiz Capitulino071673b2012-11-27 11:02:01 -0200882 error_setg_errno(err, errno, "failed to trim %s",
883 mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200884 close(fd);
885 goto error;
886 }
887 }
888 close(fd);
889 }
890
891error:
892 free_fs_mount_list(&mounts);
893}
894#endif /* CONFIG_FSTRIM */
895
896
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300897#define LINUX_SYS_STATE_FILE "/sys/power/state"
898#define SUSPEND_SUPPORTED 0
899#define SUSPEND_NOT_SUPPORTED 1
900
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300901static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
902 const char *sysfile_str, Error **err)
903{
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200904 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300905 char *pmutils_path;
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200906 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300907 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300908
909 pmutils_path = g_find_program_in_path(pmutils_bin);
910
911 pid = fork();
912 if (!pid) {
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300913 char buf[32]; /* hopefully big enough */
914 ssize_t ret;
915 int fd;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300916
917 setsid();
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300918 reopen_fd_to_null(0);
919 reopen_fd_to_null(1);
920 reopen_fd_to_null(2);
921
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300922 if (pmutils_path) {
923 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
924 }
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300925
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300926 /*
927 * If we get here either pm-utils is not installed or execle() has
928 * failed. Let's try the manual method if the caller wants it.
929 */
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300930
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300931 if (!sysfile_str) {
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300932 _exit(SUSPEND_NOT_SUPPORTED);
933 }
934
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300935 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
936 if (fd < 0) {
937 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300938 }
939
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300940 ret = read(fd, buf, sizeof(buf)-1);
941 if (ret <= 0) {
942 _exit(SUSPEND_NOT_SUPPORTED);
943 }
944 buf[ret] = '\0';
945
946 if (strstr(buf, sysfile_str)) {
947 _exit(SUSPEND_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300948 }
949
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300950 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200951 } else if (pid < 0) {
952 error_setg_errno(err, errno, "failed to create child process");
953 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300954 }
955
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200956 ga_wait_child(pid, &status, &local_err);
957 if (error_is_set(&local_err)) {
958 error_propagate(err, local_err);
959 goto out;
960 }
961
962 if (!WIFEXITED(status)) {
963 error_setg(err, "child process has terminated abnormally");
964 goto out;
965 }
966
967 switch (WEXITSTATUS(status)) {
968 case SUSPEND_SUPPORTED:
969 goto out;
970 case SUSPEND_NOT_SUPPORTED:
971 error_setg(err,
972 "the requested suspend mode is not supported by the guest");
973 goto out;
974 default:
975 error_setg(err,
976 "the helper program '%s' returned an unexpected exit status"
977 " code (%d)", pmutils_path, WEXITSTATUS(status));
978 goto out;
979 }
980
981out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300982 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300983}
984
985static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
986 Error **err)
987{
Luiz Capitulino7b376082012-11-27 11:02:04 -0200988 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300989 char *pmutils_path;
Luiz Capitulino7b376082012-11-27 11:02:04 -0200990 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300991 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300992
993 pmutils_path = g_find_program_in_path(pmutils_bin);
994
995 pid = fork();
996 if (pid == 0) {
997 /* child */
998 int fd;
999
1000 setsid();
1001 reopen_fd_to_null(0);
1002 reopen_fd_to_null(1);
1003 reopen_fd_to_null(2);
1004
1005 if (pmutils_path) {
1006 execle(pmutils_path, pmutils_bin, NULL, environ);
1007 }
1008
1009 /*
1010 * If we get here either pm-utils is not installed or execle() has
1011 * failed. Let's try the manual method if the caller wants it.
1012 */
1013
1014 if (!sysfile_str) {
1015 _exit(EXIT_FAILURE);
1016 }
1017
1018 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1019 if (fd < 0) {
1020 _exit(EXIT_FAILURE);
1021 }
1022
1023 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1024 _exit(EXIT_FAILURE);
1025 }
1026
1027 _exit(EXIT_SUCCESS);
Luiz Capitulino7b376082012-11-27 11:02:04 -02001028 } else if (pid < 0) {
1029 error_setg_errno(err, errno, "failed to create child process");
1030 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001031 }
1032
Luiz Capitulino7b376082012-11-27 11:02:04 -02001033 ga_wait_child(pid, &status, &local_err);
1034 if (error_is_set(&local_err)) {
1035 error_propagate(err, local_err);
1036 goto out;
1037 }
1038
1039 if (!WIFEXITED(status)) {
1040 error_setg(err, "child process has terminated abnormally");
1041 goto out;
1042 }
1043
1044 if (WEXITSTATUS(status)) {
1045 error_setg(err, "child process has failed to suspend");
1046 goto out;
1047 }
1048
1049out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001050 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001051}
1052
1053void qmp_guest_suspend_disk(Error **err)
1054{
1055 bios_supports_mode("pm-is-supported", "--hibernate", "disk", err);
1056 if (error_is_set(err)) {
1057 return;
1058 }
1059
1060 guest_suspend("pm-hibernate", "disk", err);
1061}
1062
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001063void qmp_guest_suspend_ram(Error **err)
1064{
1065 bios_supports_mode("pm-is-supported", "--suspend", "mem", err);
1066 if (error_is_set(err)) {
1067 return;
1068 }
1069
1070 guest_suspend("pm-suspend", "mem", err);
1071}
1072
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001073void qmp_guest_suspend_hybrid(Error **err)
1074{
1075 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL, err);
1076 if (error_is_set(err)) {
1077 return;
1078 }
1079
1080 guest_suspend("pm-suspend-hybrid", NULL, err);
1081}
1082
Michal Privoznik3424fc92012-02-29 17:02:23 +01001083static GuestNetworkInterfaceList *
1084guest_find_interface(GuestNetworkInterfaceList *head,
1085 const char *name)
1086{
1087 for (; head; head = head->next) {
1088 if (strcmp(head->value->name, name) == 0) {
1089 break;
1090 }
1091 }
1092
1093 return head;
1094}
1095
1096/*
1097 * Build information about guest interfaces
1098 */
1099GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1100{
1101 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1102 struct ifaddrs *ifap, *ifa;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001103
1104 if (getifaddrs(&ifap) < 0) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001105 error_setg_errno(errp, errno, "getifaddrs failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001106 goto error;
1107 }
1108
1109 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1110 GuestNetworkInterfaceList *info;
1111 GuestIpAddressList **address_list = NULL, *address_item = NULL;
1112 char addr4[INET_ADDRSTRLEN];
1113 char addr6[INET6_ADDRSTRLEN];
1114 int sock;
1115 struct ifreq ifr;
1116 unsigned char *mac_addr;
1117 void *p;
1118
1119 g_debug("Processing %s interface", ifa->ifa_name);
1120
1121 info = guest_find_interface(head, ifa->ifa_name);
1122
1123 if (!info) {
1124 info = g_malloc0(sizeof(*info));
1125 info->value = g_malloc0(sizeof(*info->value));
1126 info->value->name = g_strdup(ifa->ifa_name);
1127
1128 if (!cur_item) {
1129 head = cur_item = info;
1130 } else {
1131 cur_item->next = info;
1132 cur_item = info;
1133 }
1134 }
1135
1136 if (!info->value->has_hardware_address &&
1137 ifa->ifa_flags & SIOCGIFHWADDR) {
1138 /* we haven't obtained HW address yet */
1139 sock = socket(PF_INET, SOCK_STREAM, 0);
1140 if (sock == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001141 error_setg_errno(errp, errno, "failed to create socket");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001142 goto error;
1143 }
1144
1145 memset(&ifr, 0, sizeof(ifr));
Jim Meyering1ab516e2012-10-04 13:09:58 +02001146 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001147 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001148 error_setg_errno(errp, errno,
1149 "failed to get MAC address of %s",
1150 ifa->ifa_name);
Markus Armbruster10a21582013-01-16 18:15:09 +01001151 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001152 goto error;
1153 }
1154
Markus Armbruster10a21582013-01-16 18:15:09 +01001155 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001156 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1157
Stefan Weile4ada482013-01-16 18:37:23 +01001158 info->value->hardware_address =
1159 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1160 (int) mac_addr[0], (int) mac_addr[1],
1161 (int) mac_addr[2], (int) mac_addr[3],
1162 (int) mac_addr[4], (int) mac_addr[5]);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001163
1164 info->value->has_hardware_address = true;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001165 }
1166
1167 if (ifa->ifa_addr &&
1168 ifa->ifa_addr->sa_family == AF_INET) {
1169 /* interface with IPv4 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001170 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1171 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001172 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001173 goto error;
1174 }
1175
Markus Armbruster10a21582013-01-16 18:15:09 +01001176 address_item = g_malloc0(sizeof(*address_item));
1177 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001178 address_item->value->ip_address = g_strdup(addr4);
1179 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1180
1181 if (ifa->ifa_netmask) {
1182 /* Count the number of set bits in netmask.
1183 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1184 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1185 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1186 }
1187 } else if (ifa->ifa_addr &&
1188 ifa->ifa_addr->sa_family == AF_INET6) {
1189 /* interface with IPv6 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001190 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1191 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001192 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001193 goto error;
1194 }
1195
Markus Armbruster10a21582013-01-16 18:15:09 +01001196 address_item = g_malloc0(sizeof(*address_item));
1197 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001198 address_item->value->ip_address = g_strdup(addr6);
1199 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1200
1201 if (ifa->ifa_netmask) {
1202 /* Count the number of set bits in netmask.
1203 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1204 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1205 address_item->value->prefix =
1206 ctpop32(((uint32_t *) p)[0]) +
1207 ctpop32(((uint32_t *) p)[1]) +
1208 ctpop32(((uint32_t *) p)[2]) +
1209 ctpop32(((uint32_t *) p)[3]);
1210 }
1211 }
1212
1213 if (!address_item) {
1214 continue;
1215 }
1216
1217 address_list = &info->value->ip_addresses;
1218
1219 while (*address_list && (*address_list)->next) {
1220 address_list = &(*address_list)->next;
1221 }
1222
1223 if (!*address_list) {
1224 *address_list = address_item;
1225 } else {
1226 (*address_list)->next = address_item;
1227 }
1228
1229 info->value->has_ip_addresses = true;
1230
1231
1232 }
1233
1234 freeifaddrs(ifap);
1235 return head;
1236
1237error:
1238 freeifaddrs(ifap);
1239 qapi_free_GuestNetworkInterfaceList(head);
1240 return NULL;
1241}
1242
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001243#define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1244
1245static long sysconf_exact(int name, const char *name_str, Error **err)
1246{
1247 long ret;
1248
1249 errno = 0;
1250 ret = sysconf(name);
1251 if (ret == -1) {
1252 if (errno == 0) {
1253 error_setg(err, "sysconf(%s): value indefinite", name_str);
1254 } else {
1255 error_setg_errno(err, errno, "sysconf(%s)", name_str);
1256 }
1257 }
1258 return ret;
1259}
1260
1261/* Transfer online/offline status between @vcpu and the guest system.
1262 *
1263 * On input either @errp or *@errp must be NULL.
1264 *
1265 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1266 * - R: vcpu->logical_id
1267 * - W: vcpu->online
1268 * - W: vcpu->can_offline
1269 *
1270 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1271 * - R: vcpu->logical_id
1272 * - R: vcpu->online
1273 *
1274 * Written members remain unmodified on error.
1275 */
1276static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1277 Error **errp)
1278{
1279 char *dirpath;
1280 int dirfd;
1281
1282 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1283 vcpu->logical_id);
1284 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1285 if (dirfd == -1) {
1286 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1287 } else {
1288 static const char fn[] = "online";
1289 int fd;
1290 int res;
1291
1292 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1293 if (fd == -1) {
1294 if (errno != ENOENT) {
1295 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1296 } else if (sys2vcpu) {
1297 vcpu->online = true;
1298 vcpu->can_offline = false;
1299 } else if (!vcpu->online) {
1300 error_setg(errp, "logical processor #%" PRId64 " can't be "
1301 "offlined", vcpu->logical_id);
1302 } /* otherwise pretend successful re-onlining */
1303 } else {
1304 unsigned char status;
1305
1306 res = pread(fd, &status, 1, 0);
1307 if (res == -1) {
1308 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1309 } else if (res == 0) {
1310 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1311 fn);
1312 } else if (sys2vcpu) {
1313 vcpu->online = (status != '0');
1314 vcpu->can_offline = true;
1315 } else if (vcpu->online != (status != '0')) {
1316 status = '0' + vcpu->online;
1317 if (pwrite(fd, &status, 1, 0) == -1) {
1318 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1319 fn);
1320 }
1321 } /* otherwise pretend successful re-(on|off)-lining */
1322
1323 res = close(fd);
1324 g_assert(res == 0);
1325 }
1326
1327 res = close(dirfd);
1328 g_assert(res == 0);
1329 }
1330
1331 g_free(dirpath);
1332}
1333
1334GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1335{
1336 int64_t current;
1337 GuestLogicalProcessorList *head, **link;
1338 long sc_max;
1339 Error *local_err = NULL;
1340
1341 current = 0;
1342 head = NULL;
1343 link = &head;
1344 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1345
1346 while (local_err == NULL && current < sc_max) {
1347 GuestLogicalProcessor *vcpu;
1348 GuestLogicalProcessorList *entry;
1349
1350 vcpu = g_malloc0(sizeof *vcpu);
1351 vcpu->logical_id = current++;
1352 vcpu->has_can_offline = true; /* lolspeak ftw */
1353 transfer_vcpu(vcpu, true, &local_err);
1354
1355 entry = g_malloc0(sizeof *entry);
1356 entry->value = vcpu;
1357
1358 *link = entry;
1359 link = &entry->next;
1360 }
1361
1362 if (local_err == NULL) {
1363 /* there's no guest with zero VCPUs */
1364 g_assert(head != NULL);
1365 return head;
1366 }
1367
1368 qapi_free_GuestLogicalProcessorList(head);
1369 error_propagate(errp, local_err);
1370 return NULL;
1371}
1372
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001373int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1374{
1375 int64_t processed;
1376 Error *local_err = NULL;
1377
1378 processed = 0;
1379 while (vcpus != NULL) {
1380 transfer_vcpu(vcpus->value, false, &local_err);
1381 if (local_err != NULL) {
1382 break;
1383 }
1384 ++processed;
1385 vcpus = vcpus->next;
1386 }
1387
1388 if (local_err != NULL) {
1389 if (processed == 0) {
1390 error_propagate(errp, local_err);
1391 } else {
1392 error_free(local_err);
1393 }
1394 }
1395
1396 return processed;
1397}
1398
Michael Rothe72c3f22012-03-25 13:59:41 -05001399#else /* defined(__linux__) */
1400
Michael Rothe72c3f22012-03-25 13:59:41 -05001401void qmp_guest_suspend_disk(Error **err)
1402{
1403 error_set(err, QERR_UNSUPPORTED);
1404}
1405
1406void qmp_guest_suspend_ram(Error **err)
1407{
1408 error_set(err, QERR_UNSUPPORTED);
1409}
1410
1411void qmp_guest_suspend_hybrid(Error **err)
1412{
1413 error_set(err, QERR_UNSUPPORTED);
1414}
1415
1416GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1417{
1418 error_set(errp, QERR_UNSUPPORTED);
1419 return NULL;
1420}
1421
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001422GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1423{
1424 error_set(errp, QERR_UNSUPPORTED);
1425 return NULL;
1426}
1427
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001428int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1429{
1430 error_set(errp, QERR_UNSUPPORTED);
1431 return -1;
1432}
1433
Michael Rothe72c3f22012-03-25 13:59:41 -05001434#endif
1435
Michael Rothd35d4cb2012-04-13 21:07:36 -05001436#if !defined(CONFIG_FSFREEZE)
1437
1438GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
1439{
1440 error_set(err, QERR_UNSUPPORTED);
1441
1442 return 0;
1443}
1444
1445int64_t qmp_guest_fsfreeze_freeze(Error **err)
1446{
1447 error_set(err, QERR_UNSUPPORTED);
1448
1449 return 0;
1450}
1451
1452int64_t qmp_guest_fsfreeze_thaw(Error **err)
1453{
1454 error_set(err, QERR_UNSUPPORTED);
1455
1456 return 0;
1457}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001458#endif /* CONFIG_FSFREEZE */
Michael Rothd35d4cb2012-04-13 21:07:36 -05001459
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001460#if !defined(CONFIG_FSTRIM)
1461void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
1462{
1463 error_set(err, QERR_UNSUPPORTED);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001464}
Michael Rothd35d4cb2012-04-13 21:07:36 -05001465#endif
1466
Michael Rothe3d4d252011-07-19 15:41:55 -05001467/* register init/cleanup routines for stateful command groups */
1468void ga_command_state_init(GAState *s, GACommandState *cs)
1469{
Anthony Liguori7006b9c2011-07-22 14:14:17 -05001470#if defined(CONFIG_FSFREEZE)
Michael Rothf22d85e2012-04-17 19:01:45 -05001471 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
Anthony Liguori7006b9c2011-07-22 14:14:17 -05001472#endif
Michael Rothe3d4d252011-07-19 15:41:55 -05001473 ga_command_state_add(cs, guest_file_init, NULL);
1474}