]> git.proxmox.com Git - mirror_qemu.git/blame - qga/commands-posix.c
qga: Add guest-fsfreeze-freeze-list command
[mirror_qemu.git] / qga / commands-posix.c
CommitLineData
e3d4d252 1/*
42074a9d 2 * QEMU Guest Agent POSIX-specific command implementations
e3d4d252
MR
3 *
4 * Copyright IBM Corp. 2011
5 *
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
3424fc9f 8 * Michal Privoznik <mprivozn@redhat.com>
e3d4d252
MR
9 *
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>
e72c3f2e
MR
15#include <sys/types.h>
16#include <sys/ioctl.h>
2c02cbf6 17#include <sys/wait.h>
d2baff62
LE
18#include <unistd.h>
19#include <errno.h>
20#include <fcntl.h>
c689b4f1
LE
21#include <stdio.h>
22#include <string.h>
23#include <sys/stat.h>
d2baff62 24#include <inttypes.h>
e72c3f2e
MR
25#include "qga/guest-agent-core.h"
26#include "qga-qmp-commands.h"
7b1b5d19 27#include "qapi/qmp/qerror.h"
1de7afc9
PB
28#include "qemu/queue.h"
29#include "qemu/host-utils.h"
4eb36d40 30
2c02cbf6 31#ifndef CONFIG_HAS_ENVIRON
eecae147
AF
32#ifdef __APPLE__
33#include <crt_externs.h>
34#define environ (*_NSGetEnviron())
35#else
2c02cbf6
LC
36extern char **environ;
37#endif
eecae147 38#endif
2c02cbf6 39
4eb36d40 40#if defined(__linux__)
e3d4d252 41#include <mntent.h>
7006b9cf 42#include <linux/fs.h>
3424fc9f
MP
43#include <ifaddrs.h>
44#include <arpa/inet.h>
45#include <sys/socket.h>
46#include <net/if.h>
e3d4d252 47
eab5fd59 48#ifdef FIFREEZE
e72c3f2e
MR
49#define CONFIG_FSFREEZE
50#endif
eab5fd59
PB
51#ifdef FITRIM
52#define CONFIG_FSTRIM
53#endif
e72c3f2e
MR
54#endif
55
77dbc81b 56static void ga_wait_child(pid_t pid, int *status, Error **errp)
d220a6df
LC
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) {
77dbc81b
MA
67 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)",
68 pid);
d220a6df
LC
69 return;
70 }
71
72 g_assert(rpid == pid);
73}
74
77dbc81b 75void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
e3d4d252 76{
e3d4d252 77 const char *shutdown_flag;
d220a6df
LC
78 Error *local_err = NULL;
79 pid_t pid;
3674838c 80 int status;
e3d4d252
MR
81
82 slog("guest-shutdown called, mode: %s", mode);
83 if (!has_mode || strcmp(mode, "powerdown") == 0) {
84 shutdown_flag = "-P";
85 } else if (strcmp(mode, "halt") == 0) {
86 shutdown_flag = "-H";
87 } else if (strcmp(mode, "reboot") == 0) {
88 shutdown_flag = "-r";
89 } else {
77dbc81b 90 error_setg(errp,
d220a6df 91 "mode is invalid (valid values are: halt|powerdown|reboot");
e3d4d252
MR
92 return;
93 }
94
d5dd3498
LC
95 pid = fork();
96 if (pid == 0) {
e3d4d252
MR
97 /* child, start the shutdown */
98 setsid();
3674838c
LC
99 reopen_fd_to_null(0);
100 reopen_fd_to_null(1);
101 reopen_fd_to_null(2);
e3d4d252 102
485e741c 103 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
3674838c
LC
104 "hypervisor initiated shutdown", (char*)NULL, environ);
105 _exit(EXIT_FAILURE);
d5dd3498 106 } else if (pid < 0) {
77dbc81b 107 error_setg_errno(errp, errno, "failed to create child process");
d220a6df 108 return;
e3d4d252 109 }
d5dd3498 110
d220a6df 111 ga_wait_child(pid, &status, &local_err);
84d18f06 112 if (local_err) {
77dbc81b 113 error_propagate(errp, local_err);
d220a6df
LC
114 return;
115 }
116
117 if (!WIFEXITED(status)) {
77dbc81b 118 error_setg(errp, "child process has terminated abnormally");
d220a6df
LC
119 return;
120 }
121
122 if (WEXITSTATUS(status)) {
77dbc81b 123 error_setg(errp, "child process has failed to shutdown");
d5dd3498
LC
124 return;
125 }
126
085d8134 127 /* succeeded */
e3d4d252
MR
128}
129
6912e6a9
LL
130int64_t qmp_guest_get_time(Error **errp)
131{
132 int ret;
133 qemu_timeval tq;
134 int64_t time_ns;
135
136 ret = qemu_gettimeofday(&tq);
137 if (ret < 0) {
138 error_setg_errno(errp, errno, "Failed to get time");
139 return -1;
140 }
141
142 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
143 return time_ns;
144}
145
2c958923 146void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
a1bca57f
LL
147{
148 int ret;
149 int status;
150 pid_t pid;
151 Error *local_err = NULL;
152 struct timeval tv;
153
2c958923
MP
154 /* If user has passed a time, validate and set it. */
155 if (has_time) {
156 /* year-2038 will overflow in case time_t is 32bit */
157 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
158 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
159 return;
160 }
161
162 tv.tv_sec = time_ns / 1000000000;
163 tv.tv_usec = (time_ns % 1000000000) / 1000;
164
165 ret = settimeofday(&tv, NULL);
166 if (ret < 0) {
167 error_setg_errno(errp, errno, "Failed to set time to guest");
168 return;
169 }
a1bca57f
LL
170 }
171
2c958923
MP
172 /* Now, if user has passed a time to set and the system time is set, we
173 * just need to synchronize the hardware clock. However, if no time was
174 * passed, user is requesting the opposite: set the system time from the
1634df56 175 * hardware clock (RTC). */
a1bca57f
LL
176 pid = fork();
177 if (pid == 0) {
178 setsid();
179 reopen_fd_to_null(0);
180 reopen_fd_to_null(1);
181 reopen_fd_to_null(2);
182
2c958923
MP
183 /* Use '/sbin/hwclock -w' to set RTC from the system time,
184 * or '/sbin/hwclock -s' to set the system time from RTC. */
185 execle("/sbin/hwclock", "hwclock", has_time ? "-w" : "-s",
186 NULL, environ);
a1bca57f
LL
187 _exit(EXIT_FAILURE);
188 } else if (pid < 0) {
189 error_setg_errno(errp, errno, "failed to create child process");
190 return;
191 }
192
193 ga_wait_child(pid, &status, &local_err);
84d18f06 194 if (local_err) {
a1bca57f
LL
195 error_propagate(errp, local_err);
196 return;
197 }
198
199 if (!WIFEXITED(status)) {
200 error_setg(errp, "child process has terminated abnormally");
201 return;
202 }
203
204 if (WEXITSTATUS(status)) {
205 error_setg(errp, "hwclock failed to set hardware clock to system time");
206 return;
207 }
208}
209
e3d4d252
MR
210typedef struct GuestFileHandle {
211 uint64_t id;
212 FILE *fh;
213 QTAILQ_ENTRY(GuestFileHandle) next;
214} GuestFileHandle;
215
216static struct {
217 QTAILQ_HEAD(, GuestFileHandle) filehandles;
218} guest_file_state;
219
39097daf 220static int64_t guest_file_handle_add(FILE *fh, Error **errp)
e3d4d252
MR
221{
222 GuestFileHandle *gfh;
39097daf
MR
223 int64_t handle;
224
225 handle = ga_get_fd_handle(ga_state, errp);
a903f40c
MA
226 if (handle < 0) {
227 return -1;
39097daf 228 }
e3d4d252 229
7267c094 230 gfh = g_malloc0(sizeof(GuestFileHandle));
39097daf 231 gfh->id = handle;
e3d4d252
MR
232 gfh->fh = fh;
233 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
39097daf
MR
234
235 return handle;
e3d4d252
MR
236}
237
77dbc81b 238static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
e3d4d252
MR
239{
240 GuestFileHandle *gfh;
241
242 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
243 {
244 if (gfh->id == id) {
245 return gfh;
246 }
247 }
248
77dbc81b 249 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
e3d4d252
MR
250 return NULL;
251}
252
c689b4f1
LE
253typedef const char * const ccpc;
254
8fe6bbca
LE
255#ifndef O_BINARY
256#define O_BINARY 0
257#endif
258
c689b4f1
LE
259/* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
260static const struct {
261 ccpc *forms;
262 int oflag_base;
263} guest_file_open_modes[] = {
8fe6bbca
LE
264 { (ccpc[]){ "r", NULL }, O_RDONLY },
265 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY },
266 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC },
267 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY },
268 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND },
269 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY },
270 { (ccpc[]){ "r+", NULL }, O_RDWR },
271 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY },
272 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC },
273 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY },
274 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND },
275 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY }
c689b4f1
LE
276};
277
278static int
77dbc81b 279find_open_flag(const char *mode_str, Error **errp)
c689b4f1
LE
280{
281 unsigned mode;
282
283 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
284 ccpc *form;
285
286 form = guest_file_open_modes[mode].forms;
287 while (*form != NULL && strcmp(*form, mode_str) != 0) {
288 ++form;
289 }
290 if (*form != NULL) {
291 break;
292 }
293 }
294
295 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
77dbc81b 296 error_setg(errp, "invalid file open mode '%s'", mode_str);
c689b4f1
LE
297 return -1;
298 }
299 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
300}
301
302#define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
303 S_IRGRP | S_IWGRP | \
304 S_IROTH | S_IWOTH)
305
306static FILE *
77dbc81b 307safe_open_or_create(const char *path, const char *mode, Error **errp)
c689b4f1
LE
308{
309 Error *local_err = NULL;
310 int oflag;
311
312 oflag = find_open_flag(mode, &local_err);
313 if (local_err == NULL) {
314 int fd;
315
316 /* If the caller wants / allows creation of a new file, we implement it
317 * with a two step process: open() + (open() / fchmod()).
318 *
319 * First we insist on creating the file exclusively as a new file. If
320 * that succeeds, we're free to set any file-mode bits on it. (The
321 * motivation is that we want to set those file-mode bits independently
322 * of the current umask.)
323 *
324 * If the exclusive creation fails because the file already exists
325 * (EEXIST is not possible for any other reason), we just attempt to
326 * open the file, but in this case we won't be allowed to change the
327 * file-mode bits on the preexistent file.
328 *
329 * The pathname should never disappear between the two open()s in
330 * practice. If it happens, then someone very likely tried to race us.
331 * In this case just go ahead and report the ENOENT from the second
332 * open() to the caller.
333 *
334 * If the caller wants to open a preexistent file, then the first
335 * open() is decisive and its third argument is ignored, and the second
336 * open() and the fchmod() are never called.
337 */
338 fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0);
339 if (fd == -1 && errno == EEXIST) {
340 oflag &= ~(unsigned)O_CREAT;
341 fd = open(path, oflag);
342 }
343
344 if (fd == -1) {
345 error_setg_errno(&local_err, errno, "failed to open file '%s' "
346 "(mode: '%s')", path, mode);
347 } else {
348 qemu_set_cloexec(fd);
349
350 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) {
351 error_setg_errno(&local_err, errno, "failed to set permission "
352 "0%03o on new file '%s' (mode: '%s')",
353 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode);
354 } else {
355 FILE *f;
356
357 f = fdopen(fd, mode);
358 if (f == NULL) {
359 error_setg_errno(&local_err, errno, "failed to associate "
360 "stdio stream with file descriptor %d, "
361 "file '%s' (mode: '%s')", fd, path, mode);
362 } else {
363 return f;
364 }
365 }
366
367 close(fd);
2b720018
LE
368 if (oflag & O_CREAT) {
369 unlink(path);
370 }
c689b4f1
LE
371 }
372 }
373
77dbc81b 374 error_propagate(errp, local_err);
c689b4f1
LE
375 return NULL;
376}
377
77dbc81b
MA
378int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
379 Error **errp)
e3d4d252
MR
380{
381 FILE *fh;
c689b4f1 382 Error *local_err = NULL;
e3d4d252 383 int fd;
39097daf 384 int64_t ret = -1, handle;
e3d4d252
MR
385
386 if (!has_mode) {
387 mode = "r";
388 }
389 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
c689b4f1
LE
390 fh = safe_open_or_create(path, mode, &local_err);
391 if (local_err != NULL) {
77dbc81b 392 error_propagate(errp, local_err);
e3d4d252
MR
393 return -1;
394 }
395
396 /* set fd non-blocking to avoid common use cases (like reading from a
397 * named pipe) from hanging the agent
398 */
399 fd = fileno(fh);
400 ret = fcntl(fd, F_GETFL);
401 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
402 if (ret == -1) {
77dbc81b 403 error_setg_errno(errp, errno, "failed to make file '%s' non-blocking",
db3edb66 404 path);
e3d4d252
MR
405 fclose(fh);
406 return -1;
407 }
408
77dbc81b 409 handle = guest_file_handle_add(fh, errp);
a903f40c 410 if (handle < 0) {
39097daf
MR
411 fclose(fh);
412 return -1;
413 }
414
d607a523 415 slog("guest-file-open, handle: %" PRId64, handle);
39097daf 416 return handle;
e3d4d252
MR
417}
418
77dbc81b 419void qmp_guest_file_close(int64_t handle, Error **errp)
e3d4d252 420{
77dbc81b 421 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
422 int ret;
423
d607a523 424 slog("guest-file-close called, handle: %" PRId64, handle);
e3d4d252 425 if (!gfh) {
e3d4d252
MR
426 return;
427 }
428
429 ret = fclose(gfh->fh);
3ac4b7c5 430 if (ret == EOF) {
77dbc81b 431 error_setg_errno(errp, errno, "failed to close handle");
e3d4d252
MR
432 return;
433 }
434
435 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
7267c094 436 g_free(gfh);
e3d4d252
MR
437}
438
439struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
77dbc81b 440 int64_t count, Error **errp)
e3d4d252 441{
77dbc81b 442 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
443 GuestFileRead *read_data = NULL;
444 guchar *buf;
445 FILE *fh;
446 size_t read_count;
447
448 if (!gfh) {
e3d4d252
MR
449 return NULL;
450 }
451
452 if (!has_count) {
453 count = QGA_READ_COUNT_DEFAULT;
454 } else if (count < 0) {
77dbc81b 455 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
db3edb66 456 count);
e3d4d252
MR
457 return NULL;
458 }
459
460 fh = gfh->fh;
7267c094 461 buf = g_malloc0(count+1);
e3d4d252
MR
462 read_count = fread(buf, 1, count, fh);
463 if (ferror(fh)) {
77dbc81b 464 error_setg_errno(errp, errno, "failed to read file");
d607a523 465 slog("guest-file-read failed, handle: %" PRId64, handle);
e3d4d252
MR
466 } else {
467 buf[read_count] = 0;
7267c094 468 read_data = g_malloc0(sizeof(GuestFileRead));
e3d4d252
MR
469 read_data->count = read_count;
470 read_data->eof = feof(fh);
471 if (read_count) {
472 read_data->buf_b64 = g_base64_encode(buf, read_count);
473 }
474 }
7267c094 475 g_free(buf);
e3d4d252
MR
476 clearerr(fh);
477
478 return read_data;
479}
480
481GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
77dbc81b
MA
482 bool has_count, int64_t count,
483 Error **errp)
e3d4d252
MR
484{
485 GuestFileWrite *write_data = NULL;
486 guchar *buf;
487 gsize buf_len;
488 int write_count;
77dbc81b 489 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
490 FILE *fh;
491
492 if (!gfh) {
e3d4d252
MR
493 return NULL;
494 }
495
496 fh = gfh->fh;
497 buf = g_base64_decode(buf_b64, &buf_len);
498
499 if (!has_count) {
500 count = buf_len;
501 } else if (count < 0 || count > buf_len) {
77dbc81b 502 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
db3edb66 503 count);
7267c094 504 g_free(buf);
e3d4d252
MR
505 return NULL;
506 }
507
508 write_count = fwrite(buf, 1, count, fh);
509 if (ferror(fh)) {
77dbc81b 510 error_setg_errno(errp, errno, "failed to write to file");
d607a523 511 slog("guest-file-write failed, handle: %" PRId64, handle);
e3d4d252 512 } else {
7267c094 513 write_data = g_malloc0(sizeof(GuestFileWrite));
e3d4d252
MR
514 write_data->count = write_count;
515 write_data->eof = feof(fh);
516 }
7267c094 517 g_free(buf);
e3d4d252
MR
518 clearerr(fh);
519
520 return write_data;
521}
522
523struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
77dbc81b 524 int64_t whence, Error **errp)
e3d4d252 525{
77dbc81b 526 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
527 GuestFileSeek *seek_data = NULL;
528 FILE *fh;
529 int ret;
530
531 if (!gfh) {
e3d4d252
MR
532 return NULL;
533 }
534
535 fh = gfh->fh;
536 ret = fseek(fh, offset, whence);
537 if (ret == -1) {
77dbc81b 538 error_setg_errno(errp, errno, "failed to seek file");
e3d4d252 539 } else {
10b7c5dd 540 seek_data = g_new0(GuestFileSeek, 1);
e3d4d252
MR
541 seek_data->position = ftell(fh);
542 seek_data->eof = feof(fh);
543 }
544 clearerr(fh);
545
546 return seek_data;
547}
548
77dbc81b 549void qmp_guest_file_flush(int64_t handle, Error **errp)
e3d4d252 550{
77dbc81b 551 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
552 FILE *fh;
553 int ret;
554
555 if (!gfh) {
e3d4d252
MR
556 return;
557 }
558
559 fh = gfh->fh;
560 ret = fflush(fh);
561 if (ret == EOF) {
77dbc81b 562 error_setg_errno(errp, errno, "failed to flush file");
e3d4d252
MR
563 }
564}
565
566static void guest_file_init(void)
567{
568 QTAILQ_INIT(&guest_file_state.filehandles);
569}
570
e72c3f2e
MR
571/* linux-specific implementations. avoid this if at all possible. */
572#if defined(__linux__)
573
eab5fd59 574#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
af02203f 575typedef struct FsMount {
e3d4d252
MR
576 char *dirname;
577 char *devtype;
af02203f
PB
578 QTAILQ_ENTRY(FsMount) next;
579} FsMount;
e3d4d252 580
e5d9adbd 581typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
9e8aded4 582
af02203f 583static void free_fs_mount_list(FsMountList *mounts)
9e8aded4 584{
af02203f 585 FsMount *mount, *temp;
9e8aded4
MR
586
587 if (!mounts) {
588 return;
589 }
590
591 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
592 QTAILQ_REMOVE(mounts, mount, next);
593 g_free(mount->dirname);
594 g_free(mount->devtype);
595 g_free(mount);
596 }
597}
598
e3d4d252
MR
599/*
600 * Walk the mount table and build a list of local file systems
601 */
77dbc81b 602static void build_fs_mount_list(FsMountList *mounts, Error **errp)
e3d4d252
MR
603{
604 struct mntent *ment;
af02203f 605 FsMount *mount;
9e2fa418 606 char const *mtab = "/proc/self/mounts";
e3d4d252
MR
607 FILE *fp;
608
e3d4d252
MR
609 fp = setmntent(mtab, "r");
610 if (!fp) {
77dbc81b 611 error_setg(errp, "failed to open mtab file: '%s'", mtab);
261551d1 612 return;
e3d4d252
MR
613 }
614
615 while ((ment = getmntent(fp))) {
616 /*
617 * An entry which device name doesn't start with a '/' is
618 * either a dummy file system or a network file system.
619 * Add special handling for smbfs and cifs as is done by
620 * coreutils as well.
621 */
622 if ((ment->mnt_fsname[0] != '/') ||
623 (strcmp(ment->mnt_type, "smbfs") == 0) ||
624 (strcmp(ment->mnt_type, "cifs") == 0)) {
625 continue;
626 }
627
af02203f 628 mount = g_malloc0(sizeof(FsMount));
7267c094
AL
629 mount->dirname = g_strdup(ment->mnt_dir);
630 mount->devtype = g_strdup(ment->mnt_type);
e3d4d252 631
9e8aded4 632 QTAILQ_INSERT_TAIL(mounts, mount, next);
e3d4d252
MR
633 }
634
635 endmntent(fp);
e3d4d252 636}
eab5fd59
PB
637#endif
638
639#if defined(CONFIG_FSFREEZE)
e3d4d252 640
ec0f694c
TS
641typedef enum {
642 FSFREEZE_HOOK_THAW = 0,
643 FSFREEZE_HOOK_FREEZE,
644} FsfreezeHookArg;
645
13a439ec 646static const char *fsfreeze_hook_arg_string[] = {
ec0f694c
TS
647 "thaw",
648 "freeze",
649};
650
77dbc81b 651static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
ec0f694c
TS
652{
653 int status;
654 pid_t pid;
655 const char *hook;
656 const char *arg_str = fsfreeze_hook_arg_string[arg];
657 Error *local_err = NULL;
658
659 hook = ga_fsfreeze_hook(ga_state);
660 if (!hook) {
661 return;
662 }
663 if (access(hook, X_OK) != 0) {
77dbc81b 664 error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
ec0f694c
TS
665 return;
666 }
667
668 slog("executing fsfreeze hook with arg '%s'", arg_str);
669 pid = fork();
670 if (pid == 0) {
671 setsid();
672 reopen_fd_to_null(0);
673 reopen_fd_to_null(1);
674 reopen_fd_to_null(2);
675
676 execle(hook, hook, arg_str, NULL, environ);
677 _exit(EXIT_FAILURE);
678 } else if (pid < 0) {
77dbc81b 679 error_setg_errno(errp, errno, "failed to create child process");
ec0f694c
TS
680 return;
681 }
682
683 ga_wait_child(pid, &status, &local_err);
84d18f06 684 if (local_err) {
77dbc81b 685 error_propagate(errp, local_err);
ec0f694c
TS
686 return;
687 }
688
689 if (!WIFEXITED(status)) {
77dbc81b 690 error_setg(errp, "fsfreeze hook has terminated abnormally");
ec0f694c
TS
691 return;
692 }
693
694 status = WEXITSTATUS(status);
695 if (status) {
77dbc81b 696 error_setg(errp, "fsfreeze hook has failed with status %d", status);
ec0f694c
TS
697 return;
698 }
699}
700
e3d4d252
MR
701/*
702 * Return status of freeze/thaw
703 */
77dbc81b 704GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
e3d4d252 705{
f22d85e9
MR
706 if (ga_is_frozen(ga_state)) {
707 return GUEST_FSFREEZE_STATUS_FROZEN;
708 }
709
710 return GUEST_FSFREEZE_STATUS_THAWED;
e3d4d252
MR
711}
712
e99bce20
TS
713int64_t qmp_guest_fsfreeze_freeze(Error **errp)
714{
715 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
716}
717
e3d4d252
MR
718/*
719 * Walk list of mounted file systems in the guest, and freeze the ones which
720 * are real local file systems.
721 */
e99bce20
TS
722int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
723 strList *mountpoints,
724 Error **errp)
e3d4d252
MR
725{
726 int ret = 0, i = 0;
e99bce20 727 strList *list;
af02203f
PB
728 FsMountList mounts;
729 struct FsMount *mount;
261551d1 730 Error *local_err = NULL;
e3d4d252 731 int fd;
e3d4d252
MR
732
733 slog("guest-fsfreeze called");
734
ec0f694c 735 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
84d18f06 736 if (local_err) {
77dbc81b 737 error_propagate(errp, local_err);
ec0f694c
TS
738 return -1;
739 }
740
9e8aded4 741 QTAILQ_INIT(&mounts);
261551d1 742 build_fs_mount_list(&mounts, &local_err);
84d18f06 743 if (local_err) {
77dbc81b 744 error_propagate(errp, local_err);
261551d1 745 return -1;
e3d4d252
MR
746 }
747
748 /* cannot risk guest agent blocking itself on a write in this state */
f22d85e9 749 ga_set_frozen(ga_state);
e3d4d252 750
e5d9adbd 751 QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
e99bce20
TS
752 /* To issue fsfreeze in the reverse order of mounts, check if the
753 * mount is listed in the list here */
754 if (has_mountpoints) {
755 for (list = mountpoints; list; list = list->next) {
756 if (strcmp(list->value, mount->dirname) == 0) {
757 break;
758 }
759 }
760 if (!list) {
761 continue;
762 }
763 }
764
e3d4d252
MR
765 fd = qemu_open(mount->dirname, O_RDONLY);
766 if (fd == -1) {
77dbc81b 767 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
e3d4d252
MR
768 goto error;
769 }
770
771 /* we try to cull filesytems we know won't work in advance, but other
772 * filesytems may not implement fsfreeze for less obvious reasons.
9e8aded4
MR
773 * these will report EOPNOTSUPP. we simply ignore these when tallying
774 * the number of frozen filesystems.
775 *
776 * any other error means a failure to freeze a filesystem we
777 * expect to be freezable, so return an error in those cases
778 * and return system to thawed state.
e3d4d252
MR
779 */
780 ret = ioctl(fd, FIFREEZE);
9e8aded4
MR
781 if (ret == -1) {
782 if (errno != EOPNOTSUPP) {
77dbc81b 783 error_setg_errno(errp, errno, "failed to freeze %s",
617fbbc1 784 mount->dirname);
9e8aded4
MR
785 close(fd);
786 goto error;
787 }
788 } else {
789 i++;
e3d4d252
MR
790 }
791 close(fd);
e3d4d252
MR
792 }
793
af02203f 794 free_fs_mount_list(&mounts);
e3d4d252
MR
795 return i;
796
797error:
af02203f 798 free_fs_mount_list(&mounts);
9e8aded4 799 qmp_guest_fsfreeze_thaw(NULL);
e3d4d252
MR
800 return 0;
801}
802
803/*
804 * Walk list of frozen file systems in the guest, and thaw them.
805 */
77dbc81b 806int64_t qmp_guest_fsfreeze_thaw(Error **errp)
e3d4d252
MR
807{
808 int ret;
af02203f
PB
809 FsMountList mounts;
810 FsMount *mount;
9e8aded4 811 int fd, i = 0, logged;
261551d1 812 Error *local_err = NULL;
9e8aded4
MR
813
814 QTAILQ_INIT(&mounts);
261551d1 815 build_fs_mount_list(&mounts, &local_err);
84d18f06 816 if (local_err) {
77dbc81b 817 error_propagate(errp, local_err);
9e8aded4
MR
818 return 0;
819 }
e3d4d252 820
9e8aded4
MR
821 QTAILQ_FOREACH(mount, &mounts, next) {
822 logged = false;
e3d4d252
MR
823 fd = qemu_open(mount->dirname, O_RDONLY);
824 if (fd == -1) {
e3d4d252
MR
825 continue;
826 }
9e8aded4
MR
827 /* we have no way of knowing whether a filesystem was actually unfrozen
828 * as a result of a successful call to FITHAW, only that if an error
829 * was returned the filesystem was *not* unfrozen by that particular
830 * call.
831 *
a31f0531 832 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
9e8aded4
MR
833 * to unfreeze, continuing issuing FITHAW until an error is returned,
834 * in which case either the filesystem is in an unfreezable state, or,
835 * more likely, it was thawed previously (and remains so afterward).
836 *
837 * also, since the most recent successful call is the one that did
838 * the actual unfreeze, we can use this to provide an accurate count
839 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
840 * may * be useful for determining whether a filesystem was unfrozen
841 * during the freeze/thaw phase by a process other than qemu-ga.
842 */
843 do {
844 ret = ioctl(fd, FITHAW);
845 if (ret == 0 && !logged) {
846 i++;
847 logged = true;
848 }
849 } while (ret == 0);
e3d4d252 850 close(fd);
e3d4d252
MR
851 }
852
f22d85e9 853 ga_unset_frozen(ga_state);
af02203f 854 free_fs_mount_list(&mounts);
ec0f694c 855
77dbc81b 856 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
ec0f694c 857
e3d4d252
MR
858 return i;
859}
860
e3d4d252
MR
861static void guest_fsfreeze_cleanup(void)
862{
e3d4d252
MR
863 Error *err = NULL;
864
f22d85e9 865 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
6f686749
MA
866 qmp_guest_fsfreeze_thaw(&err);
867 if (err) {
868 slog("failed to clean up frozen filesystems: %s",
869 error_get_pretty(err));
870 error_free(err);
e3d4d252
MR
871 }
872 }
873}
e72c3f2e 874#endif /* CONFIG_FSFREEZE */
e3d4d252 875
eab5fd59
PB
876#if defined(CONFIG_FSTRIM)
877/*
878 * Walk list of mounted file systems in the guest, and trim them.
879 */
77dbc81b 880void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
eab5fd59
PB
881{
882 int ret = 0;
883 FsMountList mounts;
884 struct FsMount *mount;
885 int fd;
261551d1 886 Error *local_err = NULL;
eab5fd59
PB
887 struct fstrim_range r = {
888 .start = 0,
889 .len = -1,
890 .minlen = has_minimum ? minimum : 0,
891 };
892
893 slog("guest-fstrim called");
894
895 QTAILQ_INIT(&mounts);
261551d1 896 build_fs_mount_list(&mounts, &local_err);
84d18f06 897 if (local_err) {
77dbc81b 898 error_propagate(errp, local_err);
eab5fd59
PB
899 return;
900 }
901
902 QTAILQ_FOREACH(mount, &mounts, next) {
903 fd = qemu_open(mount->dirname, O_RDONLY);
904 if (fd == -1) {
77dbc81b 905 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
eab5fd59
PB
906 goto error;
907 }
908
909 /* We try to cull filesytems we know won't work in advance, but other
910 * filesytems may not implement fstrim for less obvious reasons. These
911 * will report EOPNOTSUPP; we simply ignore these errors. Any other
912 * error means an unexpected error, so return it in those cases. In
913 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
914 */
915 ret = ioctl(fd, FITRIM, &r);
916 if (ret == -1) {
917 if (errno != ENOTTY && errno != EOPNOTSUPP) {
77dbc81b 918 error_setg_errno(errp, errno, "failed to trim %s",
071673b0 919 mount->dirname);
eab5fd59
PB
920 close(fd);
921 goto error;
922 }
923 }
924 close(fd);
925 }
926
927error:
928 free_fs_mount_list(&mounts);
929}
930#endif /* CONFIG_FSTRIM */
931
932
11d0f125
LC
933#define LINUX_SYS_STATE_FILE "/sys/power/state"
934#define SUSPEND_SUPPORTED 0
935#define SUSPEND_NOT_SUPPORTED 1
936
11d0f125 937static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
77dbc81b 938 const char *sysfile_str, Error **errp)
11d0f125 939{
6b26e837 940 Error *local_err = NULL;
11d0f125 941 char *pmutils_path;
6b26e837 942 pid_t pid;
dc8764f0 943 int status;
11d0f125
LC
944
945 pmutils_path = g_find_program_in_path(pmutils_bin);
946
947 pid = fork();
948 if (!pid) {
dc8764f0
LC
949 char buf[32]; /* hopefully big enough */
950 ssize_t ret;
951 int fd;
11d0f125
LC
952
953 setsid();
11d0f125
LC
954 reopen_fd_to_null(0);
955 reopen_fd_to_null(1);
956 reopen_fd_to_null(2);
957
dc8764f0
LC
958 if (pmutils_path) {
959 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
960 }
11d0f125 961
dc8764f0
LC
962 /*
963 * If we get here either pm-utils is not installed or execle() has
964 * failed. Let's try the manual method if the caller wants it.
965 */
11d0f125 966
dc8764f0
LC
967 if (!sysfile_str) {
968 _exit(SUSPEND_NOT_SUPPORTED);
969 }
11d0f125 970
dc8764f0
LC
971 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
972 if (fd < 0) {
11d0f125
LC
973 _exit(SUSPEND_NOT_SUPPORTED);
974 }
975
dc8764f0
LC
976 ret = read(fd, buf, sizeof(buf)-1);
977 if (ret <= 0) {
978 _exit(SUSPEND_NOT_SUPPORTED);
11d0f125 979 }
dc8764f0 980 buf[ret] = '\0';
11d0f125 981
dc8764f0
LC
982 if (strstr(buf, sysfile_str)) {
983 _exit(SUSPEND_SUPPORTED);
11d0f125
LC
984 }
985
dc8764f0 986 _exit(SUSPEND_NOT_SUPPORTED);
6b26e837 987 } else if (pid < 0) {
77dbc81b 988 error_setg_errno(errp, errno, "failed to create child process");
6b26e837 989 goto out;
11d0f125
LC
990 }
991
6b26e837 992 ga_wait_child(pid, &status, &local_err);
84d18f06 993 if (local_err) {
77dbc81b 994 error_propagate(errp, local_err);
6b26e837
LC
995 goto out;
996 }
11d0f125 997
6b26e837 998 if (!WIFEXITED(status)) {
77dbc81b 999 error_setg(errp, "child process has terminated abnormally");
6b26e837 1000 goto out;
dc8764f0
LC
1001 }
1002
6b26e837
LC
1003 switch (WEXITSTATUS(status)) {
1004 case SUSPEND_SUPPORTED:
1005 goto out;
1006 case SUSPEND_NOT_SUPPORTED:
77dbc81b 1007 error_setg(errp,
6b26e837
LC
1008 "the requested suspend mode is not supported by the guest");
1009 goto out;
1010 default:
77dbc81b 1011 error_setg(errp,
6b26e837
LC
1012 "the helper program '%s' returned an unexpected exit status"
1013 " code (%d)", pmutils_path, WEXITSTATUS(status));
1014 goto out;
11d0f125
LC
1015 }
1016
6b26e837
LC
1017out:
1018 g_free(pmutils_path);
11d0f125
LC
1019}
1020
1021static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
77dbc81b 1022 Error **errp)
11d0f125 1023{
7b376087 1024 Error *local_err = NULL;
11d0f125 1025 char *pmutils_path;
7b376087 1026 pid_t pid;
dc8764f0 1027 int status;
11d0f125
LC
1028
1029 pmutils_path = g_find_program_in_path(pmutils_bin);
1030
1031 pid = fork();
1032 if (pid == 0) {
1033 /* child */
1034 int fd;
1035
1036 setsid();
1037 reopen_fd_to_null(0);
1038 reopen_fd_to_null(1);
1039 reopen_fd_to_null(2);
1040
1041 if (pmutils_path) {
1042 execle(pmutils_path, pmutils_bin, NULL, environ);
1043 }
1044
1045 /*
1046 * If we get here either pm-utils is not installed or execle() has
1047 * failed. Let's try the manual method if the caller wants it.
1048 */
1049
1050 if (!sysfile_str) {
1051 _exit(EXIT_FAILURE);
1052 }
1053
1054 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1055 if (fd < 0) {
1056 _exit(EXIT_FAILURE);
1057 }
1058
1059 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1060 _exit(EXIT_FAILURE);
1061 }
1062
1063 _exit(EXIT_SUCCESS);
7b376087 1064 } else if (pid < 0) {
77dbc81b 1065 error_setg_errno(errp, errno, "failed to create child process");
7b376087 1066 goto out;
11d0f125
LC
1067 }
1068
7b376087 1069 ga_wait_child(pid, &status, &local_err);
84d18f06 1070 if (local_err) {
77dbc81b 1071 error_propagate(errp, local_err);
7b376087
LC
1072 goto out;
1073 }
11d0f125 1074
7b376087 1075 if (!WIFEXITED(status)) {
77dbc81b 1076 error_setg(errp, "child process has terminated abnormally");
7b376087 1077 goto out;
dc8764f0
LC
1078 }
1079
7b376087 1080 if (WEXITSTATUS(status)) {
77dbc81b 1081 error_setg(errp, "child process has failed to suspend");
7b376087 1082 goto out;
11d0f125 1083 }
dc8764f0 1084
7b376087
LC
1085out:
1086 g_free(pmutils_path);
11d0f125
LC
1087}
1088
77dbc81b 1089void qmp_guest_suspend_disk(Error **errp)
11d0f125 1090{
0f230bf7
MA
1091 Error *local_err = NULL;
1092
1093 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err);
1094 if (local_err) {
1095 error_propagate(errp, local_err);
11d0f125
LC
1096 return;
1097 }
1098
77dbc81b 1099 guest_suspend("pm-hibernate", "disk", errp);
11d0f125
LC
1100}
1101
77dbc81b 1102void qmp_guest_suspend_ram(Error **errp)
fbf42210 1103{
0f230bf7
MA
1104 Error *local_err = NULL;
1105
1106 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err);
1107 if (local_err) {
1108 error_propagate(errp, local_err);
fbf42210
LC
1109 return;
1110 }
1111
77dbc81b 1112 guest_suspend("pm-suspend", "mem", errp);
fbf42210
LC
1113}
1114
77dbc81b 1115void qmp_guest_suspend_hybrid(Error **errp)
95f4f404 1116{
0f230bf7
MA
1117 Error *local_err = NULL;
1118
1119 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL,
1120 &local_err);
1121 if (local_err) {
1122 error_propagate(errp, local_err);
95f4f404
LC
1123 return;
1124 }
1125
77dbc81b 1126 guest_suspend("pm-suspend-hybrid", NULL, errp);
95f4f404
LC
1127}
1128
3424fc9f
MP
1129static GuestNetworkInterfaceList *
1130guest_find_interface(GuestNetworkInterfaceList *head,
1131 const char *name)
1132{
1133 for (; head; head = head->next) {
1134 if (strcmp(head->value->name, name) == 0) {
1135 break;
1136 }
1137 }
1138
1139 return head;
1140}
1141
1142/*
1143 * Build information about guest interfaces
1144 */
1145GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1146{
1147 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1148 struct ifaddrs *ifap, *ifa;
3424fc9f
MP
1149
1150 if (getifaddrs(&ifap) < 0) {
878a0ae0 1151 error_setg_errno(errp, errno, "getifaddrs failed");
3424fc9f
MP
1152 goto error;
1153 }
1154
1155 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1156 GuestNetworkInterfaceList *info;
1157 GuestIpAddressList **address_list = NULL, *address_item = NULL;
1158 char addr4[INET_ADDRSTRLEN];
1159 char addr6[INET6_ADDRSTRLEN];
1160 int sock;
1161 struct ifreq ifr;
1162 unsigned char *mac_addr;
1163 void *p;
1164
1165 g_debug("Processing %s interface", ifa->ifa_name);
1166
1167 info = guest_find_interface(head, ifa->ifa_name);
1168
1169 if (!info) {
1170 info = g_malloc0(sizeof(*info));
1171 info->value = g_malloc0(sizeof(*info->value));
1172 info->value->name = g_strdup(ifa->ifa_name);
1173
1174 if (!cur_item) {
1175 head = cur_item = info;
1176 } else {
1177 cur_item->next = info;
1178 cur_item = info;
1179 }
1180 }
1181
1182 if (!info->value->has_hardware_address &&
1183 ifa->ifa_flags & SIOCGIFHWADDR) {
1184 /* we haven't obtained HW address yet */
1185 sock = socket(PF_INET, SOCK_STREAM, 0);
1186 if (sock == -1) {
878a0ae0 1187 error_setg_errno(errp, errno, "failed to create socket");
3424fc9f
MP
1188 goto error;
1189 }
1190
1191 memset(&ifr, 0, sizeof(ifr));
1ab516ed 1192 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
3424fc9f 1193 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
878a0ae0
LC
1194 error_setg_errno(errp, errno,
1195 "failed to get MAC address of %s",
1196 ifa->ifa_name);
10a2158f 1197 close(sock);
3424fc9f
MP
1198 goto error;
1199 }
1200
10a2158f 1201 close(sock);
3424fc9f
MP
1202 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1203
e4ada482
SW
1204 info->value->hardware_address =
1205 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1206 (int) mac_addr[0], (int) mac_addr[1],
1207 (int) mac_addr[2], (int) mac_addr[3],
1208 (int) mac_addr[4], (int) mac_addr[5]);
3424fc9f
MP
1209
1210 info->value->has_hardware_address = true;
3424fc9f
MP
1211 }
1212
1213 if (ifa->ifa_addr &&
1214 ifa->ifa_addr->sa_family == AF_INET) {
1215 /* interface with IPv4 address */
3424fc9f
MP
1216 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1217 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
878a0ae0 1218 error_setg_errno(errp, errno, "inet_ntop failed");
3424fc9f
MP
1219 goto error;
1220 }
1221
10a2158f
MA
1222 address_item = g_malloc0(sizeof(*address_item));
1223 address_item->value = g_malloc0(sizeof(*address_item->value));
3424fc9f
MP
1224 address_item->value->ip_address = g_strdup(addr4);
1225 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1226
1227 if (ifa->ifa_netmask) {
1228 /* Count the number of set bits in netmask.
1229 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1230 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1231 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1232 }
1233 } else if (ifa->ifa_addr &&
1234 ifa->ifa_addr->sa_family == AF_INET6) {
1235 /* interface with IPv6 address */
3424fc9f
MP
1236 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1237 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
878a0ae0 1238 error_setg_errno(errp, errno, "inet_ntop failed");
3424fc9f
MP
1239 goto error;
1240 }
1241
10a2158f
MA
1242 address_item = g_malloc0(sizeof(*address_item));
1243 address_item->value = g_malloc0(sizeof(*address_item->value));
3424fc9f
MP
1244 address_item->value->ip_address = g_strdup(addr6);
1245 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1246
1247 if (ifa->ifa_netmask) {
1248 /* Count the number of set bits in netmask.
1249 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1250 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1251 address_item->value->prefix =
1252 ctpop32(((uint32_t *) p)[0]) +
1253 ctpop32(((uint32_t *) p)[1]) +
1254 ctpop32(((uint32_t *) p)[2]) +
1255 ctpop32(((uint32_t *) p)[3]);
1256 }
1257 }
1258
1259 if (!address_item) {
1260 continue;
1261 }
1262
1263 address_list = &info->value->ip_addresses;
1264
1265 while (*address_list && (*address_list)->next) {
1266 address_list = &(*address_list)->next;
1267 }
1268
1269 if (!*address_list) {
1270 *address_list = address_item;
1271 } else {
1272 (*address_list)->next = address_item;
1273 }
1274
1275 info->value->has_ip_addresses = true;
1276
1277
1278 }
1279
1280 freeifaddrs(ifap);
1281 return head;
1282
1283error:
1284 freeifaddrs(ifap);
1285 qapi_free_GuestNetworkInterfaceList(head);
1286 return NULL;
1287}
1288
77dbc81b 1289#define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
d2baff62 1290
77dbc81b 1291static long sysconf_exact(int name, const char *name_str, Error **errp)
d2baff62
LE
1292{
1293 long ret;
1294
1295 errno = 0;
1296 ret = sysconf(name);
1297 if (ret == -1) {
1298 if (errno == 0) {
77dbc81b 1299 error_setg(errp, "sysconf(%s): value indefinite", name_str);
d2baff62 1300 } else {
77dbc81b 1301 error_setg_errno(errp, errno, "sysconf(%s)", name_str);
d2baff62
LE
1302 }
1303 }
1304 return ret;
1305}
1306
1307/* Transfer online/offline status between @vcpu and the guest system.
1308 *
1309 * On input either @errp or *@errp must be NULL.
1310 *
1311 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1312 * - R: vcpu->logical_id
1313 * - W: vcpu->online
1314 * - W: vcpu->can_offline
1315 *
1316 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1317 * - R: vcpu->logical_id
1318 * - R: vcpu->online
1319 *
1320 * Written members remain unmodified on error.
1321 */
1322static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1323 Error **errp)
1324{
1325 char *dirpath;
1326 int dirfd;
1327
1328 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1329 vcpu->logical_id);
1330 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1331 if (dirfd == -1) {
1332 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1333 } else {
1334 static const char fn[] = "online";
1335 int fd;
1336 int res;
1337
1338 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1339 if (fd == -1) {
1340 if (errno != ENOENT) {
1341 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1342 } else if (sys2vcpu) {
1343 vcpu->online = true;
1344 vcpu->can_offline = false;
1345 } else if (!vcpu->online) {
1346 error_setg(errp, "logical processor #%" PRId64 " can't be "
1347 "offlined", vcpu->logical_id);
1348 } /* otherwise pretend successful re-onlining */
1349 } else {
1350 unsigned char status;
1351
1352 res = pread(fd, &status, 1, 0);
1353 if (res == -1) {
1354 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1355 } else if (res == 0) {
1356 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1357 fn);
1358 } else if (sys2vcpu) {
1359 vcpu->online = (status != '0');
1360 vcpu->can_offline = true;
1361 } else if (vcpu->online != (status != '0')) {
1362 status = '0' + vcpu->online;
1363 if (pwrite(fd, &status, 1, 0) == -1) {
1364 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1365 fn);
1366 }
1367 } /* otherwise pretend successful re-(on|off)-lining */
1368
1369 res = close(fd);
1370 g_assert(res == 0);
1371 }
1372
1373 res = close(dirfd);
1374 g_assert(res == 0);
1375 }
1376
1377 g_free(dirpath);
1378}
1379
1380GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1381{
1382 int64_t current;
1383 GuestLogicalProcessorList *head, **link;
1384 long sc_max;
1385 Error *local_err = NULL;
1386
1387 current = 0;
1388 head = NULL;
1389 link = &head;
1390 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1391
1392 while (local_err == NULL && current < sc_max) {
1393 GuestLogicalProcessor *vcpu;
1394 GuestLogicalProcessorList *entry;
1395
1396 vcpu = g_malloc0(sizeof *vcpu);
1397 vcpu->logical_id = current++;
1398 vcpu->has_can_offline = true; /* lolspeak ftw */
1399 transfer_vcpu(vcpu, true, &local_err);
1400
1401 entry = g_malloc0(sizeof *entry);
1402 entry->value = vcpu;
1403
1404 *link = entry;
1405 link = &entry->next;
1406 }
1407
1408 if (local_err == NULL) {
1409 /* there's no guest with zero VCPUs */
1410 g_assert(head != NULL);
1411 return head;
1412 }
1413
1414 qapi_free_GuestLogicalProcessorList(head);
1415 error_propagate(errp, local_err);
1416 return NULL;
1417}
1418
cbb65fc2
LE
1419int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1420{
1421 int64_t processed;
1422 Error *local_err = NULL;
1423
1424 processed = 0;
1425 while (vcpus != NULL) {
1426 transfer_vcpu(vcpus->value, false, &local_err);
1427 if (local_err != NULL) {
1428 break;
1429 }
1430 ++processed;
1431 vcpus = vcpus->next;
1432 }
1433
1434 if (local_err != NULL) {
1435 if (processed == 0) {
1436 error_propagate(errp, local_err);
1437 } else {
1438 error_free(local_err);
1439 }
1440 }
1441
1442 return processed;
1443}
1444
e72c3f2e
MR
1445#else /* defined(__linux__) */
1446
77dbc81b 1447void qmp_guest_suspend_disk(Error **errp)
e72c3f2e 1448{
77dbc81b 1449 error_set(errp, QERR_UNSUPPORTED);
e72c3f2e
MR
1450}
1451
77dbc81b 1452void qmp_guest_suspend_ram(Error **errp)
e72c3f2e 1453{
77dbc81b 1454 error_set(errp, QERR_UNSUPPORTED);
e72c3f2e
MR
1455}
1456
77dbc81b 1457void qmp_guest_suspend_hybrid(Error **errp)
e72c3f2e 1458{
77dbc81b 1459 error_set(errp, QERR_UNSUPPORTED);
e72c3f2e
MR
1460}
1461
d35d4cb5 1462GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
e72c3f2e 1463{
d35d4cb5
MR
1464 error_set(errp, QERR_UNSUPPORTED);
1465 return NULL;
e72c3f2e
MR
1466}
1467
d2baff62
LE
1468GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1469{
1470 error_set(errp, QERR_UNSUPPORTED);
1471 return NULL;
1472}
1473
cbb65fc2
LE
1474int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1475{
1476 error_set(errp, QERR_UNSUPPORTED);
1477 return -1;
1478}
1479
d35d4cb5
MR
1480#endif
1481
1482#if !defined(CONFIG_FSFREEZE)
1483
77dbc81b 1484GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
e72c3f2e 1485{
77dbc81b 1486 error_set(errp, QERR_UNSUPPORTED);
d35d4cb5
MR
1487
1488 return 0;
e72c3f2e
MR
1489}
1490
77dbc81b 1491int64_t qmp_guest_fsfreeze_freeze(Error **errp)
e72c3f2e 1492{
77dbc81b 1493 error_set(errp, QERR_UNSUPPORTED);
d35d4cb5
MR
1494
1495 return 0;
e72c3f2e
MR
1496}
1497
e99bce20
TS
1498int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1499 strList *mountpoints,
1500 Error **errp)
1501{
1502 error_set(errp, QERR_UNSUPPORTED);
1503
1504 return 0;
1505}
1506
77dbc81b 1507int64_t qmp_guest_fsfreeze_thaw(Error **errp)
e72c3f2e 1508{
77dbc81b 1509 error_set(errp, QERR_UNSUPPORTED);
d35d4cb5
MR
1510
1511 return 0;
e72c3f2e 1512}
eab5fd59
PB
1513#endif /* CONFIG_FSFREEZE */
1514
1515#if !defined(CONFIG_FSTRIM)
77dbc81b 1516void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
eab5fd59 1517{
77dbc81b 1518 error_set(errp, QERR_UNSUPPORTED);
eab5fd59 1519}
e72c3f2e
MR
1520#endif
1521
e3d4d252
MR
1522/* register init/cleanup routines for stateful command groups */
1523void ga_command_state_init(GAState *s, GACommandState *cs)
1524{
7006b9cf 1525#if defined(CONFIG_FSFREEZE)
f22d85e9 1526 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
7006b9cf 1527#endif
e3d4d252
MR
1528 ga_command_state_add(cs, guest_file_init, NULL);
1529}