blob: 10682f58dc6b53cfeb3f1da6bb0f1a9e320520ee [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
whitearchey485e7412013-11-06 10:54:04 +0900102 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
Luiz Capitulino36748382012-05-14 15:25:20 -0300103 "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);
Laszlo Ersek2b720012013-05-08 17:31:36 +0200358 if (oflag & O_CREAT) {
359 unlink(path);
360 }
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200361 }
362 }
363
364 error_propagate(err, local_err);
365 return NULL;
366}
367
Michael Rothe3d4d252011-07-19 15:41:55 -0500368int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
369{
370 FILE *fh;
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200371 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500372 int fd;
Michael Roth39097da2013-03-01 11:40:27 -0600373 int64_t ret = -1, handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500374
375 if (!has_mode) {
376 mode = "r";
377 }
378 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
Laszlo Ersekc689b4f2013-04-24 13:13:18 +0200379 fh = safe_open_or_create(path, mode, &local_err);
380 if (local_err != NULL) {
381 error_propagate(err, local_err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500382 return -1;
383 }
384
385 /* set fd non-blocking to avoid common use cases (like reading from a
386 * named pipe) from hanging the agent
387 */
388 fd = fileno(fh);
389 ret = fcntl(fd, F_GETFL);
390 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
391 if (ret == -1) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200392 error_setg_errno(err, errno, "failed to make file '%s' non-blocking",
393 path);
Michael Rothe3d4d252011-07-19 15:41:55 -0500394 fclose(fh);
395 return -1;
396 }
397
Michael Roth39097da2013-03-01 11:40:27 -0600398 handle = guest_file_handle_add(fh, err);
399 if (error_is_set(err)) {
400 fclose(fh);
401 return -1;
402 }
403
404 slog("guest-file-open, handle: %d", handle);
405 return handle;
Michael Rothe3d4d252011-07-19 15:41:55 -0500406}
407
408void qmp_guest_file_close(int64_t handle, Error **err)
409{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200410 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500411 int ret;
412
413 slog("guest-file-close called, handle: %ld", handle);
414 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500415 return;
416 }
417
418 ret = fclose(gfh->fh);
Luiz Capitulino3ac4b7c2012-11-27 11:01:56 -0200419 if (ret == EOF) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200420 error_setg_errno(err, errno, "failed to close handle");
Michael Rothe3d4d252011-07-19 15:41:55 -0500421 return;
422 }
423
424 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500425 g_free(gfh);
Michael Rothe3d4d252011-07-19 15:41:55 -0500426}
427
428struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
429 int64_t count, Error **err)
430{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200431 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500432 GuestFileRead *read_data = NULL;
433 guchar *buf;
434 FILE *fh;
435 size_t read_count;
436
437 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500438 return NULL;
439 }
440
441 if (!has_count) {
442 count = QGA_READ_COUNT_DEFAULT;
443 } else if (count < 0) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200444 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
445 count);
Michael Rothe3d4d252011-07-19 15:41:55 -0500446 return NULL;
447 }
448
449 fh = gfh->fh;
Anthony Liguori7267c092011-08-20 22:09:37 -0500450 buf = g_malloc0(count+1);
Michael Rothe3d4d252011-07-19 15:41:55 -0500451 read_count = fread(buf, 1, count, fh);
452 if (ferror(fh)) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200453 error_setg_errno(err, errno, "failed to read file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500454 slog("guest-file-read failed, handle: %ld", handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500455 } else {
456 buf[read_count] = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500457 read_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500458 read_data->count = read_count;
459 read_data->eof = feof(fh);
460 if (read_count) {
461 read_data->buf_b64 = g_base64_encode(buf, read_count);
462 }
463 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500464 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500465 clearerr(fh);
466
467 return read_data;
468}
469
470GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
471 bool has_count, int64_t count, Error **err)
472{
473 GuestFileWrite *write_data = NULL;
474 guchar *buf;
475 gsize buf_len;
476 int write_count;
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200477 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500478 FILE *fh;
479
480 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500481 return NULL;
482 }
483
484 fh = gfh->fh;
485 buf = g_base64_decode(buf_b64, &buf_len);
486
487 if (!has_count) {
488 count = buf_len;
489 } else if (count < 0 || count > buf_len) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200490 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
491 count);
Anthony Liguori7267c092011-08-20 22:09:37 -0500492 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500493 return NULL;
494 }
495
496 write_count = fwrite(buf, 1, count, fh);
497 if (ferror(fh)) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200498 error_setg_errno(err, errno, "failed to write to file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500499 slog("guest-file-write failed, handle: %ld", handle);
Michael Rothe3d4d252011-07-19 15:41:55 -0500500 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500501 write_data = g_malloc0(sizeof(GuestFileWrite));
Michael Rothe3d4d252011-07-19 15:41:55 -0500502 write_data->count = write_count;
503 write_data->eof = feof(fh);
504 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500505 g_free(buf);
Michael Rothe3d4d252011-07-19 15:41:55 -0500506 clearerr(fh);
507
508 return write_data;
509}
510
511struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
512 int64_t whence, Error **err)
513{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200514 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500515 GuestFileSeek *seek_data = NULL;
516 FILE *fh;
517 int ret;
518
519 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500520 return NULL;
521 }
522
523 fh = gfh->fh;
524 ret = fseek(fh, offset, whence);
525 if (ret == -1) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200526 error_setg_errno(err, errno, "failed to seek file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500527 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500528 seek_data = g_malloc0(sizeof(GuestFileRead));
Michael Rothe3d4d252011-07-19 15:41:55 -0500529 seek_data->position = ftell(fh);
530 seek_data->eof = feof(fh);
531 }
532 clearerr(fh);
533
534 return seek_data;
535}
536
537void qmp_guest_file_flush(int64_t handle, Error **err)
538{
Luiz Capitulinoa9de6d02012-11-27 11:01:55 -0200539 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500540 FILE *fh;
541 int ret;
542
543 if (!gfh) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500544 return;
545 }
546
547 fh = gfh->fh;
548 ret = fflush(fh);
549 if (ret == EOF) {
Luiz Capitulinodb3edb62012-11-27 11:01:57 -0200550 error_setg_errno(err, errno, "failed to flush file");
Michael Rothe3d4d252011-07-19 15:41:55 -0500551 }
552}
553
554static void guest_file_init(void)
555{
556 QTAILQ_INIT(&guest_file_state.filehandles);
557}
558
Michael Rothe72c3f22012-03-25 13:59:41 -0500559/* linux-specific implementations. avoid this if at all possible. */
560#if defined(__linux__)
561
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200562#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
Paolo Bonziniaf022032012-06-13 07:41:27 +0200563typedef struct FsMount {
Michael Rothe3d4d252011-07-19 15:41:55 -0500564 char *dirname;
565 char *devtype;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200566 QTAILQ_ENTRY(FsMount) next;
567} FsMount;
Michael Rothe3d4d252011-07-19 15:41:55 -0500568
Tomoki Sekiyamae5d9adb2013-10-01 17:09:53 -0400569typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
Michael Roth9e8aded432012-04-16 19:52:17 -0500570
Paolo Bonziniaf022032012-06-13 07:41:27 +0200571static void free_fs_mount_list(FsMountList *mounts)
Michael Roth9e8aded432012-04-16 19:52:17 -0500572{
Paolo Bonziniaf022032012-06-13 07:41:27 +0200573 FsMount *mount, *temp;
Michael Roth9e8aded432012-04-16 19:52:17 -0500574
575 if (!mounts) {
576 return;
577 }
578
579 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
580 QTAILQ_REMOVE(mounts, mount, next);
581 g_free(mount->dirname);
582 g_free(mount->devtype);
583 g_free(mount);
584 }
585}
586
Michael Rothe3d4d252011-07-19 15:41:55 -0500587/*
588 * Walk the mount table and build a list of local file systems
589 */
Luiz Capitulino261551d2012-11-29 15:29:11 -0200590static void build_fs_mount_list(FsMountList *mounts, Error **err)
Michael Rothe3d4d252011-07-19 15:41:55 -0500591{
592 struct mntent *ment;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200593 FsMount *mount;
Michael Roth9e2fa412012-05-29 10:08:50 -0500594 char const *mtab = "/proc/self/mounts";
Michael Rothe3d4d252011-07-19 15:41:55 -0500595 FILE *fp;
596
Michael Rothe3d4d252011-07-19 15:41:55 -0500597 fp = setmntent(mtab, "r");
598 if (!fp) {
Luiz Capitulino261551d2012-11-29 15:29:11 -0200599 error_setg(err, "failed to open mtab file: '%s'", mtab);
600 return;
Michael Rothe3d4d252011-07-19 15:41:55 -0500601 }
602
603 while ((ment = getmntent(fp))) {
604 /*
605 * An entry which device name doesn't start with a '/' is
606 * either a dummy file system or a network file system.
607 * Add special handling for smbfs and cifs as is done by
608 * coreutils as well.
609 */
610 if ((ment->mnt_fsname[0] != '/') ||
611 (strcmp(ment->mnt_type, "smbfs") == 0) ||
612 (strcmp(ment->mnt_type, "cifs") == 0)) {
613 continue;
614 }
615
Paolo Bonziniaf022032012-06-13 07:41:27 +0200616 mount = g_malloc0(sizeof(FsMount));
Anthony Liguori7267c092011-08-20 22:09:37 -0500617 mount->dirname = g_strdup(ment->mnt_dir);
618 mount->devtype = g_strdup(ment->mnt_type);
Michael Rothe3d4d252011-07-19 15:41:55 -0500619
Michael Roth9e8aded432012-04-16 19:52:17 -0500620 QTAILQ_INSERT_TAIL(mounts, mount, next);
Michael Rothe3d4d252011-07-19 15:41:55 -0500621 }
622
623 endmntent(fp);
Michael Rothe3d4d252011-07-19 15:41:55 -0500624}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200625#endif
626
627#if defined(CONFIG_FSFREEZE)
Michael Rothe3d4d252011-07-19 15:41:55 -0500628
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900629typedef enum {
630 FSFREEZE_HOOK_THAW = 0,
631 FSFREEZE_HOOK_FREEZE,
632} FsfreezeHookArg;
633
634const char *fsfreeze_hook_arg_string[] = {
635 "thaw",
636 "freeze",
637};
638
639static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
640{
641 int status;
642 pid_t pid;
643 const char *hook;
644 const char *arg_str = fsfreeze_hook_arg_string[arg];
645 Error *local_err = NULL;
646
647 hook = ga_fsfreeze_hook(ga_state);
648 if (!hook) {
649 return;
650 }
651 if (access(hook, X_OK) != 0) {
652 error_setg_errno(err, errno, "can't access fsfreeze hook '%s'", hook);
653 return;
654 }
655
656 slog("executing fsfreeze hook with arg '%s'", arg_str);
657 pid = fork();
658 if (pid == 0) {
659 setsid();
660 reopen_fd_to_null(0);
661 reopen_fd_to_null(1);
662 reopen_fd_to_null(2);
663
664 execle(hook, hook, arg_str, NULL, environ);
665 _exit(EXIT_FAILURE);
666 } else if (pid < 0) {
667 error_setg_errno(err, errno, "failed to create child process");
668 return;
669 }
670
671 ga_wait_child(pid, &status, &local_err);
672 if (error_is_set(&local_err)) {
673 error_propagate(err, local_err);
674 return;
675 }
676
677 if (!WIFEXITED(status)) {
678 error_setg(err, "fsfreeze hook has terminated abnormally");
679 return;
680 }
681
682 status = WEXITSTATUS(status);
683 if (status) {
684 error_setg(err, "fsfreeze hook has failed with status %d", status);
685 return;
686 }
687}
688
Michael Rothe3d4d252011-07-19 15:41:55 -0500689/*
690 * Return status of freeze/thaw
691 */
692GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
693{
Michael Rothf22d85e2012-04-17 19:01:45 -0500694 if (ga_is_frozen(ga_state)) {
695 return GUEST_FSFREEZE_STATUS_FROZEN;
696 }
697
698 return GUEST_FSFREEZE_STATUS_THAWED;
Michael Rothe3d4d252011-07-19 15:41:55 -0500699}
700
701/*
702 * Walk list of mounted file systems in the guest, and freeze the ones which
703 * are real local file systems.
704 */
705int64_t qmp_guest_fsfreeze_freeze(Error **err)
706{
707 int ret = 0, i = 0;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200708 FsMountList mounts;
709 struct FsMount *mount;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200710 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500711 int fd;
Michael Rothe3d4d252011-07-19 15:41:55 -0500712
713 slog("guest-fsfreeze called");
714
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900715 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
716 if (error_is_set(&local_err)) {
717 error_propagate(err, local_err);
718 return -1;
719 }
720
Michael Roth9e8aded432012-04-16 19:52:17 -0500721 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200722 build_fs_mount_list(&mounts, &local_err);
723 if (error_is_set(&local_err)) {
724 error_propagate(err, local_err);
725 return -1;
Michael Rothe3d4d252011-07-19 15:41:55 -0500726 }
727
728 /* cannot risk guest agent blocking itself on a write in this state */
Michael Rothf22d85e2012-04-17 19:01:45 -0500729 ga_set_frozen(ga_state);
Michael Rothe3d4d252011-07-19 15:41:55 -0500730
Tomoki Sekiyamae5d9adb2013-10-01 17:09:53 -0400731 QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500732 fd = qemu_open(mount->dirname, O_RDONLY);
733 if (fd == -1) {
Luiz Capitulino617fbbc2012-11-27 11:02:00 -0200734 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
Michael Rothe3d4d252011-07-19 15:41:55 -0500735 goto error;
736 }
737
738 /* we try to cull filesytems we know won't work in advance, but other
739 * filesytems may not implement fsfreeze for less obvious reasons.
Michael Roth9e8aded432012-04-16 19:52:17 -0500740 * these will report EOPNOTSUPP. we simply ignore these when tallying
741 * the number of frozen filesystems.
742 *
743 * any other error means a failure to freeze a filesystem we
744 * expect to be freezable, so return an error in those cases
745 * and return system to thawed state.
Michael Rothe3d4d252011-07-19 15:41:55 -0500746 */
747 ret = ioctl(fd, FIFREEZE);
Michael Roth9e8aded432012-04-16 19:52:17 -0500748 if (ret == -1) {
749 if (errno != EOPNOTSUPP) {
Luiz Capitulino617fbbc2012-11-27 11:02:00 -0200750 error_setg_errno(err, errno, "failed to freeze %s",
751 mount->dirname);
Michael Roth9e8aded432012-04-16 19:52:17 -0500752 close(fd);
753 goto error;
754 }
755 } else {
756 i++;
Michael Rothe3d4d252011-07-19 15:41:55 -0500757 }
758 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -0500759 }
760
Paolo Bonziniaf022032012-06-13 07:41:27 +0200761 free_fs_mount_list(&mounts);
Michael Rothe3d4d252011-07-19 15:41:55 -0500762 return i;
763
764error:
Paolo Bonziniaf022032012-06-13 07:41:27 +0200765 free_fs_mount_list(&mounts);
Michael Roth9e8aded432012-04-16 19:52:17 -0500766 qmp_guest_fsfreeze_thaw(NULL);
Michael Rothe3d4d252011-07-19 15:41:55 -0500767 return 0;
768}
769
770/*
771 * Walk list of frozen file systems in the guest, and thaw them.
772 */
773int64_t qmp_guest_fsfreeze_thaw(Error **err)
774{
775 int ret;
Paolo Bonziniaf022032012-06-13 07:41:27 +0200776 FsMountList mounts;
777 FsMount *mount;
Michael Roth9e8aded432012-04-16 19:52:17 -0500778 int fd, i = 0, logged;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200779 Error *local_err = NULL;
Michael Rothe3d4d252011-07-19 15:41:55 -0500780
Michael Roth9e8aded432012-04-16 19:52:17 -0500781 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200782 build_fs_mount_list(&mounts, &local_err);
783 if (error_is_set(&local_err)) {
784 error_propagate(err, local_err);
Michael Roth9e8aded432012-04-16 19:52:17 -0500785 return 0;
786 }
787
788 QTAILQ_FOREACH(mount, &mounts, next) {
789 logged = false;
Michael Rothe3d4d252011-07-19 15:41:55 -0500790 fd = qemu_open(mount->dirname, O_RDONLY);
791 if (fd == -1) {
Michael Rothe3d4d252011-07-19 15:41:55 -0500792 continue;
793 }
Michael Roth9e8aded432012-04-16 19:52:17 -0500794 /* we have no way of knowing whether a filesystem was actually unfrozen
795 * as a result of a successful call to FITHAW, only that if an error
796 * was returned the filesystem was *not* unfrozen by that particular
797 * call.
798 *
Jim Meyeringa31f0532012-05-09 05:12:04 +0000799 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
Michael Roth9e8aded432012-04-16 19:52:17 -0500800 * to unfreeze, continuing issuing FITHAW until an error is returned,
801 * in which case either the filesystem is in an unfreezable state, or,
802 * more likely, it was thawed previously (and remains so afterward).
803 *
804 * also, since the most recent successful call is the one that did
805 * the actual unfreeze, we can use this to provide an accurate count
806 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
807 * may * be useful for determining whether a filesystem was unfrozen
808 * during the freeze/thaw phase by a process other than qemu-ga.
809 */
810 do {
811 ret = ioctl(fd, FITHAW);
812 if (ret == 0 && !logged) {
813 i++;
814 logged = true;
815 }
816 } while (ret == 0);
Michael Rothe3d4d252011-07-19 15:41:55 -0500817 close(fd);
Michael Rothe3d4d252011-07-19 15:41:55 -0500818 }
819
Michael Rothf22d85e2012-04-17 19:01:45 -0500820 ga_unset_frozen(ga_state);
Paolo Bonziniaf022032012-06-13 07:41:27 +0200821 free_fs_mount_list(&mounts);
Tomoki Sekiyamaec0f6942012-12-12 12:55:55 +0900822
823 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, err);
824
Michael Rothe3d4d252011-07-19 15:41:55 -0500825 return i;
826}
827
Michael Rothe3d4d252011-07-19 15:41:55 -0500828static void guest_fsfreeze_cleanup(void)
829{
Michael Rothe3d4d252011-07-19 15:41:55 -0500830 Error *err = NULL;
831
Michael Rothf22d85e2012-04-17 19:01:45 -0500832 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
Markus Armbruster6f686742013-01-16 18:15:08 +0100833 qmp_guest_fsfreeze_thaw(&err);
834 if (err) {
835 slog("failed to clean up frozen filesystems: %s",
836 error_get_pretty(err));
837 error_free(err);
Michael Rothe3d4d252011-07-19 15:41:55 -0500838 }
839 }
840}
Michael Rothe72c3f22012-03-25 13:59:41 -0500841#endif /* CONFIG_FSFREEZE */
Michael Rothe3d4d252011-07-19 15:41:55 -0500842
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200843#if defined(CONFIG_FSTRIM)
844/*
845 * Walk list of mounted file systems in the guest, and trim them.
846 */
847void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
848{
849 int ret = 0;
850 FsMountList mounts;
851 struct FsMount *mount;
852 int fd;
Luiz Capitulino261551d2012-11-29 15:29:11 -0200853 Error *local_err = NULL;
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200854 struct fstrim_range r = {
855 .start = 0,
856 .len = -1,
857 .minlen = has_minimum ? minimum : 0,
858 };
859
860 slog("guest-fstrim called");
861
862 QTAILQ_INIT(&mounts);
Luiz Capitulino261551d2012-11-29 15:29:11 -0200863 build_fs_mount_list(&mounts, &local_err);
864 if (error_is_set(&local_err)) {
865 error_propagate(err, local_err);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200866 return;
867 }
868
869 QTAILQ_FOREACH(mount, &mounts, next) {
870 fd = qemu_open(mount->dirname, O_RDONLY);
871 if (fd == -1) {
Luiz Capitulino071673b2012-11-27 11:02:01 -0200872 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200873 goto error;
874 }
875
876 /* We try to cull filesytems we know won't work in advance, but other
877 * filesytems may not implement fstrim for less obvious reasons. These
878 * will report EOPNOTSUPP; we simply ignore these errors. Any other
879 * error means an unexpected error, so return it in those cases. In
880 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
881 */
882 ret = ioctl(fd, FITRIM, &r);
883 if (ret == -1) {
884 if (errno != ENOTTY && errno != EOPNOTSUPP) {
Luiz Capitulino071673b2012-11-27 11:02:01 -0200885 error_setg_errno(err, errno, "failed to trim %s",
886 mount->dirname);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +0200887 close(fd);
888 goto error;
889 }
890 }
891 close(fd);
892 }
893
894error:
895 free_fs_mount_list(&mounts);
896}
897#endif /* CONFIG_FSTRIM */
898
899
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300900#define LINUX_SYS_STATE_FILE "/sys/power/state"
901#define SUSPEND_SUPPORTED 0
902#define SUSPEND_NOT_SUPPORTED 1
903
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300904static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
905 const char *sysfile_str, Error **err)
906{
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200907 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300908 char *pmutils_path;
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200909 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300910 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300911
912 pmutils_path = g_find_program_in_path(pmutils_bin);
913
914 pid = fork();
915 if (!pid) {
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300916 char buf[32]; /* hopefully big enough */
917 ssize_t ret;
918 int fd;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300919
920 setsid();
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300921 reopen_fd_to_null(0);
922 reopen_fd_to_null(1);
923 reopen_fd_to_null(2);
924
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300925 if (pmutils_path) {
926 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
927 }
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300928
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300929 /*
930 * If we get here either pm-utils is not installed or execle() has
931 * failed. Let's try the manual method if the caller wants it.
932 */
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300933
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300934 if (!sysfile_str) {
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300935 _exit(SUSPEND_NOT_SUPPORTED);
936 }
937
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300938 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
939 if (fd < 0) {
940 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300941 }
942
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300943 ret = read(fd, buf, sizeof(buf)-1);
944 if (ret <= 0) {
945 _exit(SUSPEND_NOT_SUPPORTED);
946 }
947 buf[ret] = '\0';
948
949 if (strstr(buf, sysfile_str)) {
950 _exit(SUSPEND_SUPPORTED);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300951 }
952
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300953 _exit(SUSPEND_NOT_SUPPORTED);
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200954 } else if (pid < 0) {
955 error_setg_errno(err, errno, "failed to create child process");
956 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300957 }
958
Luiz Capitulino6b26e832012-11-27 11:02:03 -0200959 ga_wait_child(pid, &status, &local_err);
960 if (error_is_set(&local_err)) {
961 error_propagate(err, local_err);
962 goto out;
963 }
964
965 if (!WIFEXITED(status)) {
966 error_setg(err, "child process has terminated abnormally");
967 goto out;
968 }
969
970 switch (WEXITSTATUS(status)) {
971 case SUSPEND_SUPPORTED:
972 goto out;
973 case SUSPEND_NOT_SUPPORTED:
974 error_setg(err,
975 "the requested suspend mode is not supported by the guest");
976 goto out;
977 default:
978 error_setg(err,
979 "the helper program '%s' returned an unexpected exit status"
980 " code (%d)", pmutils_path, WEXITSTATUS(status));
981 goto out;
982 }
983
984out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300985 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300986}
987
988static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
989 Error **err)
990{
Luiz Capitulino7b376082012-11-27 11:02:04 -0200991 Error *local_err = NULL;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300992 char *pmutils_path;
Luiz Capitulino7b376082012-11-27 11:02:04 -0200993 pid_t pid;
Luiz Capitulinodc8764f02012-05-11 16:19:46 -0300994 int status;
Luiz Capitulino11d0f122012-02-28 11:03:03 -0300995
996 pmutils_path = g_find_program_in_path(pmutils_bin);
997
998 pid = fork();
999 if (pid == 0) {
1000 /* child */
1001 int fd;
1002
1003 setsid();
1004 reopen_fd_to_null(0);
1005 reopen_fd_to_null(1);
1006 reopen_fd_to_null(2);
1007
1008 if (pmutils_path) {
1009 execle(pmutils_path, pmutils_bin, NULL, environ);
1010 }
1011
1012 /*
1013 * If we get here either pm-utils is not installed or execle() has
1014 * failed. Let's try the manual method if the caller wants it.
1015 */
1016
1017 if (!sysfile_str) {
1018 _exit(EXIT_FAILURE);
1019 }
1020
1021 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1022 if (fd < 0) {
1023 _exit(EXIT_FAILURE);
1024 }
1025
1026 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1027 _exit(EXIT_FAILURE);
1028 }
1029
1030 _exit(EXIT_SUCCESS);
Luiz Capitulino7b376082012-11-27 11:02:04 -02001031 } else if (pid < 0) {
1032 error_setg_errno(err, errno, "failed to create child process");
1033 goto out;
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001034 }
1035
Luiz Capitulino7b376082012-11-27 11:02:04 -02001036 ga_wait_child(pid, &status, &local_err);
1037 if (error_is_set(&local_err)) {
1038 error_propagate(err, local_err);
1039 goto out;
1040 }
1041
1042 if (!WIFEXITED(status)) {
1043 error_setg(err, "child process has terminated abnormally");
1044 goto out;
1045 }
1046
1047 if (WEXITSTATUS(status)) {
1048 error_setg(err, "child process has failed to suspend");
1049 goto out;
1050 }
1051
1052out:
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001053 g_free(pmutils_path);
Luiz Capitulino11d0f122012-02-28 11:03:03 -03001054}
1055
1056void qmp_guest_suspend_disk(Error **err)
1057{
1058 bios_supports_mode("pm-is-supported", "--hibernate", "disk", err);
1059 if (error_is_set(err)) {
1060 return;
1061 }
1062
1063 guest_suspend("pm-hibernate", "disk", err);
1064}
1065
Luiz Capitulinofbf42212012-02-28 11:03:04 -03001066void qmp_guest_suspend_ram(Error **err)
1067{
1068 bios_supports_mode("pm-is-supported", "--suspend", "mem", err);
1069 if (error_is_set(err)) {
1070 return;
1071 }
1072
1073 guest_suspend("pm-suspend", "mem", err);
1074}
1075
Luiz Capitulino95f4f402012-02-28 11:03:05 -03001076void qmp_guest_suspend_hybrid(Error **err)
1077{
1078 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL, err);
1079 if (error_is_set(err)) {
1080 return;
1081 }
1082
1083 guest_suspend("pm-suspend-hybrid", NULL, err);
1084}
1085
Michal Privoznik3424fc92012-02-29 17:02:23 +01001086static GuestNetworkInterfaceList *
1087guest_find_interface(GuestNetworkInterfaceList *head,
1088 const char *name)
1089{
1090 for (; head; head = head->next) {
1091 if (strcmp(head->value->name, name) == 0) {
1092 break;
1093 }
1094 }
1095
1096 return head;
1097}
1098
1099/*
1100 * Build information about guest interfaces
1101 */
1102GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1103{
1104 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1105 struct ifaddrs *ifap, *ifa;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001106
1107 if (getifaddrs(&ifap) < 0) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001108 error_setg_errno(errp, errno, "getifaddrs failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001109 goto error;
1110 }
1111
1112 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1113 GuestNetworkInterfaceList *info;
1114 GuestIpAddressList **address_list = NULL, *address_item = NULL;
1115 char addr4[INET_ADDRSTRLEN];
1116 char addr6[INET6_ADDRSTRLEN];
1117 int sock;
1118 struct ifreq ifr;
1119 unsigned char *mac_addr;
1120 void *p;
1121
1122 g_debug("Processing %s interface", ifa->ifa_name);
1123
1124 info = guest_find_interface(head, ifa->ifa_name);
1125
1126 if (!info) {
1127 info = g_malloc0(sizeof(*info));
1128 info->value = g_malloc0(sizeof(*info->value));
1129 info->value->name = g_strdup(ifa->ifa_name);
1130
1131 if (!cur_item) {
1132 head = cur_item = info;
1133 } else {
1134 cur_item->next = info;
1135 cur_item = info;
1136 }
1137 }
1138
1139 if (!info->value->has_hardware_address &&
1140 ifa->ifa_flags & SIOCGIFHWADDR) {
1141 /* we haven't obtained HW address yet */
1142 sock = socket(PF_INET, SOCK_STREAM, 0);
1143 if (sock == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001144 error_setg_errno(errp, errno, "failed to create socket");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001145 goto error;
1146 }
1147
1148 memset(&ifr, 0, sizeof(ifr));
Jim Meyering1ab516e2012-10-04 13:09:58 +02001149 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001150 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001151 error_setg_errno(errp, errno,
1152 "failed to get MAC address of %s",
1153 ifa->ifa_name);
Markus Armbruster10a21582013-01-16 18:15:09 +01001154 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001155 goto error;
1156 }
1157
Markus Armbruster10a21582013-01-16 18:15:09 +01001158 close(sock);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001159 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1160
Stefan Weile4ada482013-01-16 18:37:23 +01001161 info->value->hardware_address =
1162 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1163 (int) mac_addr[0], (int) mac_addr[1],
1164 (int) mac_addr[2], (int) mac_addr[3],
1165 (int) mac_addr[4], (int) mac_addr[5]);
Michal Privoznik3424fc92012-02-29 17:02:23 +01001166
1167 info->value->has_hardware_address = true;
Michal Privoznik3424fc92012-02-29 17:02:23 +01001168 }
1169
1170 if (ifa->ifa_addr &&
1171 ifa->ifa_addr->sa_family == AF_INET) {
1172 /* interface with IPv4 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001173 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1174 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001175 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001176 goto error;
1177 }
1178
Markus Armbruster10a21582013-01-16 18:15:09 +01001179 address_item = g_malloc0(sizeof(*address_item));
1180 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001181 address_item->value->ip_address = g_strdup(addr4);
1182 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1183
1184 if (ifa->ifa_netmask) {
1185 /* Count the number of set bits in netmask.
1186 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1187 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1188 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1189 }
1190 } else if (ifa->ifa_addr &&
1191 ifa->ifa_addr->sa_family == AF_INET6) {
1192 /* interface with IPv6 address */
Michal Privoznik3424fc92012-02-29 17:02:23 +01001193 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1194 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
Luiz Capitulino878a0ae2012-11-27 11:02:02 -02001195 error_setg_errno(errp, errno, "inet_ntop failed");
Michal Privoznik3424fc92012-02-29 17:02:23 +01001196 goto error;
1197 }
1198
Markus Armbruster10a21582013-01-16 18:15:09 +01001199 address_item = g_malloc0(sizeof(*address_item));
1200 address_item->value = g_malloc0(sizeof(*address_item->value));
Michal Privoznik3424fc92012-02-29 17:02:23 +01001201 address_item->value->ip_address = g_strdup(addr6);
1202 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1203
1204 if (ifa->ifa_netmask) {
1205 /* Count the number of set bits in netmask.
1206 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1207 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1208 address_item->value->prefix =
1209 ctpop32(((uint32_t *) p)[0]) +
1210 ctpop32(((uint32_t *) p)[1]) +
1211 ctpop32(((uint32_t *) p)[2]) +
1212 ctpop32(((uint32_t *) p)[3]);
1213 }
1214 }
1215
1216 if (!address_item) {
1217 continue;
1218 }
1219
1220 address_list = &info->value->ip_addresses;
1221
1222 while (*address_list && (*address_list)->next) {
1223 address_list = &(*address_list)->next;
1224 }
1225
1226 if (!*address_list) {
1227 *address_list = address_item;
1228 } else {
1229 (*address_list)->next = address_item;
1230 }
1231
1232 info->value->has_ip_addresses = true;
1233
1234
1235 }
1236
1237 freeifaddrs(ifap);
1238 return head;
1239
1240error:
1241 freeifaddrs(ifap);
1242 qapi_free_GuestNetworkInterfaceList(head);
1243 return NULL;
1244}
1245
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001246#define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1247
1248static long sysconf_exact(int name, const char *name_str, Error **err)
1249{
1250 long ret;
1251
1252 errno = 0;
1253 ret = sysconf(name);
1254 if (ret == -1) {
1255 if (errno == 0) {
1256 error_setg(err, "sysconf(%s): value indefinite", name_str);
1257 } else {
1258 error_setg_errno(err, errno, "sysconf(%s)", name_str);
1259 }
1260 }
1261 return ret;
1262}
1263
1264/* Transfer online/offline status between @vcpu and the guest system.
1265 *
1266 * On input either @errp or *@errp must be NULL.
1267 *
1268 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1269 * - R: vcpu->logical_id
1270 * - W: vcpu->online
1271 * - W: vcpu->can_offline
1272 *
1273 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1274 * - R: vcpu->logical_id
1275 * - R: vcpu->online
1276 *
1277 * Written members remain unmodified on error.
1278 */
1279static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1280 Error **errp)
1281{
1282 char *dirpath;
1283 int dirfd;
1284
1285 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1286 vcpu->logical_id);
1287 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1288 if (dirfd == -1) {
1289 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1290 } else {
1291 static const char fn[] = "online";
1292 int fd;
1293 int res;
1294
1295 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1296 if (fd == -1) {
1297 if (errno != ENOENT) {
1298 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1299 } else if (sys2vcpu) {
1300 vcpu->online = true;
1301 vcpu->can_offline = false;
1302 } else if (!vcpu->online) {
1303 error_setg(errp, "logical processor #%" PRId64 " can't be "
1304 "offlined", vcpu->logical_id);
1305 } /* otherwise pretend successful re-onlining */
1306 } else {
1307 unsigned char status;
1308
1309 res = pread(fd, &status, 1, 0);
1310 if (res == -1) {
1311 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1312 } else if (res == 0) {
1313 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1314 fn);
1315 } else if (sys2vcpu) {
1316 vcpu->online = (status != '0');
1317 vcpu->can_offline = true;
1318 } else if (vcpu->online != (status != '0')) {
1319 status = '0' + vcpu->online;
1320 if (pwrite(fd, &status, 1, 0) == -1) {
1321 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1322 fn);
1323 }
1324 } /* otherwise pretend successful re-(on|off)-lining */
1325
1326 res = close(fd);
1327 g_assert(res == 0);
1328 }
1329
1330 res = close(dirfd);
1331 g_assert(res == 0);
1332 }
1333
1334 g_free(dirpath);
1335}
1336
1337GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1338{
1339 int64_t current;
1340 GuestLogicalProcessorList *head, **link;
1341 long sc_max;
1342 Error *local_err = NULL;
1343
1344 current = 0;
1345 head = NULL;
1346 link = &head;
1347 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1348
1349 while (local_err == NULL && current < sc_max) {
1350 GuestLogicalProcessor *vcpu;
1351 GuestLogicalProcessorList *entry;
1352
1353 vcpu = g_malloc0(sizeof *vcpu);
1354 vcpu->logical_id = current++;
1355 vcpu->has_can_offline = true; /* lolspeak ftw */
1356 transfer_vcpu(vcpu, true, &local_err);
1357
1358 entry = g_malloc0(sizeof *entry);
1359 entry->value = vcpu;
1360
1361 *link = entry;
1362 link = &entry->next;
1363 }
1364
1365 if (local_err == NULL) {
1366 /* there's no guest with zero VCPUs */
1367 g_assert(head != NULL);
1368 return head;
1369 }
1370
1371 qapi_free_GuestLogicalProcessorList(head);
1372 error_propagate(errp, local_err);
1373 return NULL;
1374}
1375
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001376int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1377{
1378 int64_t processed;
1379 Error *local_err = NULL;
1380
1381 processed = 0;
1382 while (vcpus != NULL) {
1383 transfer_vcpu(vcpus->value, false, &local_err);
1384 if (local_err != NULL) {
1385 break;
1386 }
1387 ++processed;
1388 vcpus = vcpus->next;
1389 }
1390
1391 if (local_err != NULL) {
1392 if (processed == 0) {
1393 error_propagate(errp, local_err);
1394 } else {
1395 error_free(local_err);
1396 }
1397 }
1398
1399 return processed;
1400}
1401
Michael Rothe72c3f22012-03-25 13:59:41 -05001402#else /* defined(__linux__) */
1403
Michael Rothe72c3f22012-03-25 13:59:41 -05001404void qmp_guest_suspend_disk(Error **err)
1405{
1406 error_set(err, QERR_UNSUPPORTED);
1407}
1408
1409void qmp_guest_suspend_ram(Error **err)
1410{
1411 error_set(err, QERR_UNSUPPORTED);
1412}
1413
1414void qmp_guest_suspend_hybrid(Error **err)
1415{
1416 error_set(err, QERR_UNSUPPORTED);
1417}
1418
1419GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1420{
1421 error_set(errp, QERR_UNSUPPORTED);
1422 return NULL;
1423}
1424
Laszlo Ersekd2baff62013-03-06 22:59:30 +01001425GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1426{
1427 error_set(errp, QERR_UNSUPPORTED);
1428 return NULL;
1429}
1430
Laszlo Ersekcbb65fc2013-03-06 22:59:31 +01001431int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1432{
1433 error_set(errp, QERR_UNSUPPORTED);
1434 return -1;
1435}
1436
Michael Rothe72c3f22012-03-25 13:59:41 -05001437#endif
1438
Michael Rothd35d4cb2012-04-13 21:07:36 -05001439#if !defined(CONFIG_FSFREEZE)
1440
1441GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
1442{
1443 error_set(err, QERR_UNSUPPORTED);
1444
1445 return 0;
1446}
1447
1448int64_t qmp_guest_fsfreeze_freeze(Error **err)
1449{
1450 error_set(err, QERR_UNSUPPORTED);
1451
1452 return 0;
1453}
1454
1455int64_t qmp_guest_fsfreeze_thaw(Error **err)
1456{
1457 error_set(err, QERR_UNSUPPORTED);
1458
1459 return 0;
1460}
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001461#endif /* CONFIG_FSFREEZE */
Michael Rothd35d4cb2012-04-13 21:07:36 -05001462
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001463#if !defined(CONFIG_FSTRIM)
1464void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
1465{
1466 error_set(err, QERR_UNSUPPORTED);
Paolo Bonzinieab5fd52012-06-13 07:41:28 +02001467}
Michael Rothd35d4cb2012-04-13 21:07:36 -05001468#endif
1469
Michael Rothe3d4d252011-07-19 15:41:55 -05001470/* register init/cleanup routines for stateful command groups */
1471void ga_command_state_init(GAState *s, GACommandState *cs)
1472{
Anthony Liguori7006b9c2011-07-22 14:14:17 -05001473#if defined(CONFIG_FSFREEZE)
Michael Rothf22d85e2012-04-17 19:01:45 -05001474 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
Anthony Liguori7006b9c2011-07-22 14:14:17 -05001475#endif
Michael Rothe3d4d252011-07-19 15:41:55 -05001476 ga_command_state_add(cs, guest_file_init, NULL);
1477}