]> git.proxmox.com Git - mirror_qemu.git/blame - qga/commands-posix.c
Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2020-09-16' into...
[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
4459bf38 14#include "qemu/osdep.h"
e72c3f2e 15#include <sys/ioctl.h>
9848f797 16#include <sys/utsname.h>
2c02cbf6 17#include <sys/wait.h>
46d4c572 18#include <dirent.h>
a8d25326 19#include "qemu-common.h"
dc03272d 20#include "guest-agent-core.h"
eb815e24 21#include "qga-qapi-commands.h"
e688df6b 22#include "qapi/error.h"
7b1b5d19 23#include "qapi/qmp/qerror.h"
1de7afc9
PB
24#include "qemu/queue.h"
25#include "qemu/host-utils.h"
12505396 26#include "qemu/sockets.h"
920639ca 27#include "qemu/base64.h"
f348b6d1 28#include "qemu/cutils.h"
5d3586b8 29#include "commands-common.h"
4eb36d40 30
e674605f
TG
31#ifdef HAVE_UTMPX
32#include <utmpx.h>
33#endif
34
2c02cbf6 35#ifndef CONFIG_HAS_ENVIRON
eecae147
AF
36#ifdef __APPLE__
37#include <crt_externs.h>
38#define environ (*_NSGetEnviron())
39#else
2c02cbf6
LC
40extern char **environ;
41#endif
eecae147 42#endif
2c02cbf6 43
4eb36d40 44#if defined(__linux__)
e3d4d252 45#include <mntent.h>
7006b9cf 46#include <linux/fs.h>
3424fc9f
MP
47#include <ifaddrs.h>
48#include <arpa/inet.h>
49#include <sys/socket.h>
50#include <net/if.h>
25b5ff1a 51#include <sys/statvfs.h>
e3d4d252 52
b616105a
TG
53#ifdef CONFIG_LIBUDEV
54#include <libudev.h>
55#endif
56
eab5fd59 57#ifdef FIFREEZE
e72c3f2e
MR
58#define CONFIG_FSFREEZE
59#endif
eab5fd59
PB
60#ifdef FITRIM
61#define CONFIG_FSTRIM
62#endif
e72c3f2e
MR
63#endif
64
77dbc81b 65static void ga_wait_child(pid_t pid, int *status, Error **errp)
d220a6df
LC
66{
67 pid_t rpid;
68
69 *status = 0;
70
71 do {
72 rpid = waitpid(pid, status, 0);
73 } while (rpid == -1 && errno == EINTR);
74
75 if (rpid == -1) {
77dbc81b
MA
76 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)",
77 pid);
d220a6df
LC
78 return;
79 }
80
81 g_assert(rpid == pid);
82}
83
77dbc81b 84void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
e3d4d252 85{
e3d4d252 86 const char *shutdown_flag;
d220a6df
LC
87 Error *local_err = NULL;
88 pid_t pid;
3674838c 89 int status;
e3d4d252
MR
90
91 slog("guest-shutdown called, mode: %s", mode);
92 if (!has_mode || strcmp(mode, "powerdown") == 0) {
93 shutdown_flag = "-P";
94 } else if (strcmp(mode, "halt") == 0) {
95 shutdown_flag = "-H";
96 } else if (strcmp(mode, "reboot") == 0) {
97 shutdown_flag = "-r";
98 } else {
77dbc81b 99 error_setg(errp,
d220a6df 100 "mode is invalid (valid values are: halt|powerdown|reboot");
e3d4d252
MR
101 return;
102 }
103
d5dd3498
LC
104 pid = fork();
105 if (pid == 0) {
e3d4d252
MR
106 /* child, start the shutdown */
107 setsid();
3674838c
LC
108 reopen_fd_to_null(0);
109 reopen_fd_to_null(1);
110 reopen_fd_to_null(2);
e3d4d252 111
485e741c 112 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
3674838c
LC
113 "hypervisor initiated shutdown", (char*)NULL, environ);
114 _exit(EXIT_FAILURE);
d5dd3498 115 } else if (pid < 0) {
77dbc81b 116 error_setg_errno(errp, errno, "failed to create child process");
d220a6df 117 return;
e3d4d252 118 }
d5dd3498 119
d220a6df 120 ga_wait_child(pid, &status, &local_err);
84d18f06 121 if (local_err) {
77dbc81b 122 error_propagate(errp, local_err);
d220a6df
LC
123 return;
124 }
125
126 if (!WIFEXITED(status)) {
77dbc81b 127 error_setg(errp, "child process has terminated abnormally");
d220a6df
LC
128 return;
129 }
130
131 if (WEXITSTATUS(status)) {
77dbc81b 132 error_setg(errp, "child process has failed to shutdown");
d5dd3498
LC
133 return;
134 }
135
085d8134 136 /* succeeded */
e3d4d252
MR
137}
138
6912e6a9
LL
139int64_t qmp_guest_get_time(Error **errp)
140{
141 int ret;
142 qemu_timeval tq;
6912e6a9
LL
143
144 ret = qemu_gettimeofday(&tq);
145 if (ret < 0) {
146 error_setg_errno(errp, errno, "Failed to get time");
147 return -1;
148 }
149
9be38598 150 return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
6912e6a9
LL
151}
152
2c958923 153void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
a1bca57f
LL
154{
155 int ret;
156 int status;
157 pid_t pid;
158 Error *local_err = NULL;
159 struct timeval tv;
5c6096e5
CH
160 static const char hwclock_path[] = "/sbin/hwclock";
161 static int hwclock_available = -1;
162
163 if (hwclock_available < 0) {
164 hwclock_available = (access(hwclock_path, X_OK) == 0);
165 }
166
167 if (!hwclock_available) {
168 error_setg(errp, QERR_UNSUPPORTED);
169 return;
170 }
a1bca57f 171
2c958923
MP
172 /* If user has passed a time, validate and set it. */
173 if (has_time) {
00d2f370
MAL
174 GDate date = { 0, };
175
2c958923
MP
176 /* year-2038 will overflow in case time_t is 32bit */
177 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
178 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
179 return;
180 }
181
182 tv.tv_sec = time_ns / 1000000000;
183 tv.tv_usec = (time_ns % 1000000000) / 1000;
00d2f370
MAL
184 g_date_set_time_t(&date, tv.tv_sec);
185 if (date.year < 1970 || date.year >= 2070) {
186 error_setg_errno(errp, errno, "Invalid time");
187 return;
188 }
2c958923
MP
189
190 ret = settimeofday(&tv, NULL);
191 if (ret < 0) {
192 error_setg_errno(errp, errno, "Failed to set time to guest");
193 return;
194 }
a1bca57f
LL
195 }
196
2c958923
MP
197 /* Now, if user has passed a time to set and the system time is set, we
198 * just need to synchronize the hardware clock. However, if no time was
199 * passed, user is requesting the opposite: set the system time from the
1634df56 200 * hardware clock (RTC). */
a1bca57f
LL
201 pid = fork();
202 if (pid == 0) {
203 setsid();
204 reopen_fd_to_null(0);
205 reopen_fd_to_null(1);
206 reopen_fd_to_null(2);
207
2c958923
MP
208 /* Use '/sbin/hwclock -w' to set RTC from the system time,
209 * or '/sbin/hwclock -s' to set the system time from RTC. */
5c6096e5 210 execle(hwclock_path, "hwclock", has_time ? "-w" : "-s",
2c958923 211 NULL, environ);
a1bca57f
LL
212 _exit(EXIT_FAILURE);
213 } else if (pid < 0) {
214 error_setg_errno(errp, errno, "failed to create child process");
215 return;
216 }
217
218 ga_wait_child(pid, &status, &local_err);
84d18f06 219 if (local_err) {
a1bca57f
LL
220 error_propagate(errp, local_err);
221 return;
222 }
223
224 if (!WIFEXITED(status)) {
225 error_setg(errp, "child process has terminated abnormally");
226 return;
227 }
228
229 if (WEXITSTATUS(status)) {
230 error_setg(errp, "hwclock failed to set hardware clock to system time");
231 return;
232 }
233}
234
895b00f6
MAL
235typedef enum {
236 RW_STATE_NEW,
237 RW_STATE_READING,
238 RW_STATE_WRITING,
239} RwState;
240
5d3586b8 241struct GuestFileHandle {
e3d4d252
MR
242 uint64_t id;
243 FILE *fh;
895b00f6 244 RwState state;
e3d4d252 245 QTAILQ_ENTRY(GuestFileHandle) next;
5d3586b8 246};
e3d4d252
MR
247
248static struct {
249 QTAILQ_HEAD(, GuestFileHandle) filehandles;
b4fe97c8
DL
250} guest_file_state = {
251 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles),
252};
e3d4d252 253
39097daf 254static int64_t guest_file_handle_add(FILE *fh, Error **errp)
e3d4d252
MR
255{
256 GuestFileHandle *gfh;
39097daf
MR
257 int64_t handle;
258
259 handle = ga_get_fd_handle(ga_state, errp);
a903f40c
MA
260 if (handle < 0) {
261 return -1;
39097daf 262 }
e3d4d252 263
f3a06403 264 gfh = g_new0(GuestFileHandle, 1);
39097daf 265 gfh->id = handle;
e3d4d252
MR
266 gfh->fh = fh;
267 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
39097daf
MR
268
269 return handle;
e3d4d252
MR
270}
271
5d3586b8 272GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
e3d4d252
MR
273{
274 GuestFileHandle *gfh;
275
276 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
277 {
278 if (gfh->id == id) {
279 return gfh;
280 }
281 }
282
77dbc81b 283 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
e3d4d252
MR
284 return NULL;
285}
286
c689b4f1
LE
287typedef const char * const ccpc;
288
8fe6bbca
LE
289#ifndef O_BINARY
290#define O_BINARY 0
291#endif
292
c689b4f1
LE
293/* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
294static const struct {
295 ccpc *forms;
296 int oflag_base;
297} guest_file_open_modes[] = {
8fe6bbca
LE
298 { (ccpc[]){ "r", NULL }, O_RDONLY },
299 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY },
300 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC },
301 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY },
302 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND },
303 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY },
304 { (ccpc[]){ "r+", NULL }, O_RDWR },
305 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY },
306 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC },
307 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY },
308 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND },
309 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY }
c689b4f1
LE
310};
311
312static int
77dbc81b 313find_open_flag(const char *mode_str, Error **errp)
c689b4f1
LE
314{
315 unsigned mode;
316
317 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
318 ccpc *form;
319
320 form = guest_file_open_modes[mode].forms;
321 while (*form != NULL && strcmp(*form, mode_str) != 0) {
322 ++form;
323 }
324 if (*form != NULL) {
325 break;
326 }
327 }
328
329 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
77dbc81b 330 error_setg(errp, "invalid file open mode '%s'", mode_str);
c689b4f1
LE
331 return -1;
332 }
333 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
334}
335
336#define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
337 S_IRGRP | S_IWGRP | \
338 S_IROTH | S_IWOTH)
339
340static FILE *
77dbc81b 341safe_open_or_create(const char *path, const char *mode, Error **errp)
c689b4f1
LE
342{
343 Error *local_err = NULL;
344 int oflag;
345
346 oflag = find_open_flag(mode, &local_err);
347 if (local_err == NULL) {
348 int fd;
349
350 /* If the caller wants / allows creation of a new file, we implement it
351 * with a two step process: open() + (open() / fchmod()).
352 *
353 * First we insist on creating the file exclusively as a new file. If
354 * that succeeds, we're free to set any file-mode bits on it. (The
355 * motivation is that we want to set those file-mode bits independently
356 * of the current umask.)
357 *
358 * If the exclusive creation fails because the file already exists
359 * (EEXIST is not possible for any other reason), we just attempt to
360 * open the file, but in this case we won't be allowed to change the
361 * file-mode bits on the preexistent file.
362 *
363 * The pathname should never disappear between the two open()s in
364 * practice. If it happens, then someone very likely tried to race us.
365 * In this case just go ahead and report the ENOENT from the second
366 * open() to the caller.
367 *
368 * If the caller wants to open a preexistent file, then the first
369 * open() is decisive and its third argument is ignored, and the second
370 * open() and the fchmod() are never called.
371 */
372 fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0);
373 if (fd == -1 && errno == EEXIST) {
374 oflag &= ~(unsigned)O_CREAT;
375 fd = open(path, oflag);
376 }
377
378 if (fd == -1) {
379 error_setg_errno(&local_err, errno, "failed to open file '%s' "
380 "(mode: '%s')", path, mode);
381 } else {
382 qemu_set_cloexec(fd);
383
384 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) {
385 error_setg_errno(&local_err, errno, "failed to set permission "
386 "0%03o on new file '%s' (mode: '%s')",
387 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode);
388 } else {
389 FILE *f;
390
391 f = fdopen(fd, mode);
392 if (f == NULL) {
393 error_setg_errno(&local_err, errno, "failed to associate "
394 "stdio stream with file descriptor %d, "
395 "file '%s' (mode: '%s')", fd, path, mode);
396 } else {
397 return f;
398 }
399 }
400
401 close(fd);
2b720018
LE
402 if (oflag & O_CREAT) {
403 unlink(path);
404 }
c689b4f1
LE
405 }
406 }
407
77dbc81b 408 error_propagate(errp, local_err);
c689b4f1
LE
409 return NULL;
410}
411
77dbc81b
MA
412int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
413 Error **errp)
e3d4d252
MR
414{
415 FILE *fh;
c689b4f1 416 Error *local_err = NULL;
85b6f6f5 417 int64_t handle;
e3d4d252
MR
418
419 if (!has_mode) {
420 mode = "r";
421 }
422 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
c689b4f1
LE
423 fh = safe_open_or_create(path, mode, &local_err);
424 if (local_err != NULL) {
77dbc81b 425 error_propagate(errp, local_err);
e3d4d252
MR
426 return -1;
427 }
428
429 /* set fd non-blocking to avoid common use cases (like reading from a
430 * named pipe) from hanging the agent
431 */
12505396 432 qemu_set_nonblock(fileno(fh));
e3d4d252 433
77dbc81b 434 handle = guest_file_handle_add(fh, errp);
a903f40c 435 if (handle < 0) {
39097daf
MR
436 fclose(fh);
437 return -1;
438 }
439
d607a523 440 slog("guest-file-open, handle: %" PRId64, handle);
39097daf 441 return handle;
e3d4d252
MR
442}
443
77dbc81b 444void qmp_guest_file_close(int64_t handle, Error **errp)
e3d4d252 445{
77dbc81b 446 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
447 int ret;
448
d607a523 449 slog("guest-file-close called, handle: %" PRId64, handle);
e3d4d252 450 if (!gfh) {
e3d4d252
MR
451 return;
452 }
453
454 ret = fclose(gfh->fh);
3ac4b7c5 455 if (ret == EOF) {
77dbc81b 456 error_setg_errno(errp, errno, "failed to close handle");
e3d4d252
MR
457 return;
458 }
459
460 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
7267c094 461 g_free(gfh);
e3d4d252
MR
462}
463
ead83a13
PMD
464GuestFileRead *guest_file_read_unsafe(GuestFileHandle *gfh,
465 int64_t count, Error **errp)
e3d4d252 466{
e3d4d252
MR
467 GuestFileRead *read_data = NULL;
468 guchar *buf;
ead83a13 469 FILE *fh = gfh->fh;
e3d4d252
MR
470 size_t read_count;
471
895b00f6
MAL
472 /* explicitly flush when switching from writing to reading */
473 if (gfh->state == RW_STATE_WRITING) {
474 int ret = fflush(fh);
475 if (ret == EOF) {
476 error_setg_errno(errp, errno, "failed to flush file");
477 return NULL;
478 }
479 gfh->state = RW_STATE_NEW;
480 }
481
7267c094 482 buf = g_malloc0(count+1);
e3d4d252
MR
483 read_count = fread(buf, 1, count, fh);
484 if (ferror(fh)) {
77dbc81b 485 error_setg_errno(errp, errno, "failed to read file");
e3d4d252
MR
486 } else {
487 buf[read_count] = 0;
f3a06403 488 read_data = g_new0(GuestFileRead, 1);
e3d4d252
MR
489 read_data->count = read_count;
490 read_data->eof = feof(fh);
491 if (read_count) {
492 read_data->buf_b64 = g_base64_encode(buf, read_count);
493 }
895b00f6 494 gfh->state = RW_STATE_READING;
e3d4d252 495 }
7267c094 496 g_free(buf);
e3d4d252
MR
497 clearerr(fh);
498
499 return read_data;
500}
501
502GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
77dbc81b
MA
503 bool has_count, int64_t count,
504 Error **errp)
e3d4d252
MR
505{
506 GuestFileWrite *write_data = NULL;
507 guchar *buf;
508 gsize buf_len;
509 int write_count;
77dbc81b 510 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
511 FILE *fh;
512
513 if (!gfh) {
e3d4d252
MR
514 return NULL;
515 }
516
517 fh = gfh->fh;
895b00f6
MAL
518
519 if (gfh->state == RW_STATE_READING) {
520 int ret = fseek(fh, 0, SEEK_CUR);
521 if (ret == -1) {
522 error_setg_errno(errp, errno, "failed to seek file");
523 return NULL;
524 }
525 gfh->state = RW_STATE_NEW;
526 }
527
920639ca
DB
528 buf = qbase64_decode(buf_b64, -1, &buf_len, errp);
529 if (!buf) {
530 return NULL;
531 }
e3d4d252
MR
532
533 if (!has_count) {
534 count = buf_len;
535 } else if (count < 0 || count > buf_len) {
77dbc81b 536 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
db3edb66 537 count);
7267c094 538 g_free(buf);
e3d4d252
MR
539 return NULL;
540 }
541
542 write_count = fwrite(buf, 1, count, fh);
543 if (ferror(fh)) {
77dbc81b 544 error_setg_errno(errp, errno, "failed to write to file");
d607a523 545 slog("guest-file-write failed, handle: %" PRId64, handle);
e3d4d252 546 } else {
f3a06403 547 write_data = g_new0(GuestFileWrite, 1);
e3d4d252
MR
548 write_data->count = write_count;
549 write_data->eof = feof(fh);
895b00f6 550 gfh->state = RW_STATE_WRITING;
e3d4d252 551 }
7267c094 552 g_free(buf);
e3d4d252
MR
553 clearerr(fh);
554
555 return write_data;
556}
557
558struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
0b4b4938
EB
559 GuestFileWhence *whence_code,
560 Error **errp)
e3d4d252 561{
77dbc81b 562 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
563 GuestFileSeek *seek_data = NULL;
564 FILE *fh;
565 int ret;
0a982b1b 566 int whence;
0b4b4938 567 Error *err = NULL;
e3d4d252
MR
568
569 if (!gfh) {
e3d4d252
MR
570 return NULL;
571 }
572
0a982b1b 573 /* We stupidly exposed 'whence':'int' in our qapi */
0b4b4938
EB
574 whence = ga_parse_whence(whence_code, &err);
575 if (err) {
576 error_propagate(errp, err);
0a982b1b
EB
577 return NULL;
578 }
579
e3d4d252
MR
580 fh = gfh->fh;
581 ret = fseek(fh, offset, whence);
582 if (ret == -1) {
77dbc81b 583 error_setg_errno(errp, errno, "failed to seek file");
895b00f6
MAL
584 if (errno == ESPIPE) {
585 /* file is non-seekable, stdio shouldn't be buffering anyways */
586 gfh->state = RW_STATE_NEW;
587 }
e3d4d252 588 } else {
10b7c5dd 589 seek_data = g_new0(GuestFileSeek, 1);
e3d4d252
MR
590 seek_data->position = ftell(fh);
591 seek_data->eof = feof(fh);
895b00f6 592 gfh->state = RW_STATE_NEW;
e3d4d252
MR
593 }
594 clearerr(fh);
595
596 return seek_data;
597}
598
77dbc81b 599void qmp_guest_file_flush(int64_t handle, Error **errp)
e3d4d252 600{
77dbc81b 601 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
e3d4d252
MR
602 FILE *fh;
603 int ret;
604
605 if (!gfh) {
e3d4d252
MR
606 return;
607 }
608
609 fh = gfh->fh;
610 ret = fflush(fh);
611 if (ret == EOF) {
77dbc81b 612 error_setg_errno(errp, errno, "failed to flush file");
895b00f6
MAL
613 } else {
614 gfh->state = RW_STATE_NEW;
e3d4d252
MR
615 }
616}
617
e72c3f2e
MR
618/* linux-specific implementations. avoid this if at all possible. */
619#if defined(__linux__)
620
eab5fd59 621#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
af02203f 622typedef struct FsMount {
e3d4d252
MR
623 char *dirname;
624 char *devtype;
46d4c572 625 unsigned int devmajor, devminor;
af02203f
PB
626 QTAILQ_ENTRY(FsMount) next;
627} FsMount;
e3d4d252 628
e5d9adbd 629typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
9e8aded4 630
af02203f 631static void free_fs_mount_list(FsMountList *mounts)
9e8aded4 632{
af02203f 633 FsMount *mount, *temp;
9e8aded4
MR
634
635 if (!mounts) {
636 return;
637 }
638
639 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
640 QTAILQ_REMOVE(mounts, mount, next);
641 g_free(mount->dirname);
642 g_free(mount->devtype);
643 g_free(mount);
644 }
645}
646
46d4c572
TS
647static int dev_major_minor(const char *devpath,
648 unsigned int *devmajor, unsigned int *devminor)
649{
650 struct stat st;
651
652 *devmajor = 0;
653 *devminor = 0;
654
655 if (stat(devpath, &st) < 0) {
656 slog("failed to stat device file '%s': %s", devpath, strerror(errno));
657 return -1;
658 }
659 if (S_ISDIR(st.st_mode)) {
660 /* It is bind mount */
661 return -2;
662 }
663 if (S_ISBLK(st.st_mode)) {
664 *devmajor = major(st.st_rdev);
665 *devminor = minor(st.st_rdev);
666 return 0;
667 }
668 return -1;
669}
670
e3d4d252
MR
671/*
672 * Walk the mount table and build a list of local file systems
673 */
46d4c572 674static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
e3d4d252
MR
675{
676 struct mntent *ment;
af02203f 677 FsMount *mount;
9e2fa418 678 char const *mtab = "/proc/self/mounts";
e3d4d252 679 FILE *fp;
46d4c572 680 unsigned int devmajor, devminor;
e3d4d252 681
e3d4d252
MR
682 fp = setmntent(mtab, "r");
683 if (!fp) {
77dbc81b 684 error_setg(errp, "failed to open mtab file: '%s'", mtab);
261551d1 685 return;
e3d4d252
MR
686 }
687
688 while ((ment = getmntent(fp))) {
689 /*
690 * An entry which device name doesn't start with a '/' is
691 * either a dummy file system or a network file system.
692 * Add special handling for smbfs and cifs as is done by
693 * coreutils as well.
694 */
695 if ((ment->mnt_fsname[0] != '/') ||
696 (strcmp(ment->mnt_type, "smbfs") == 0) ||
697 (strcmp(ment->mnt_type, "cifs") == 0)) {
698 continue;
699 }
46d4c572
TS
700 if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) {
701 /* Skip bind mounts */
702 continue;
703 }
e3d4d252 704
f3a06403 705 mount = g_new0(FsMount, 1);
7267c094
AL
706 mount->dirname = g_strdup(ment->mnt_dir);
707 mount->devtype = g_strdup(ment->mnt_type);
46d4c572
TS
708 mount->devmajor = devmajor;
709 mount->devminor = devminor;
e3d4d252 710
9e8aded4 711 QTAILQ_INSERT_TAIL(mounts, mount, next);
e3d4d252
MR
712 }
713
714 endmntent(fp);
e3d4d252 715}
46d4c572
TS
716
717static void decode_mntname(char *name, int len)
718{
719 int i, j = 0;
720 for (i = 0; i <= len; i++) {
721 if (name[i] != '\\') {
722 name[j++] = name[i];
723 } else if (name[i + 1] == '\\') {
724 name[j++] = '\\';
725 i++;
726 } else if (name[i + 1] >= '0' && name[i + 1] <= '3' &&
727 name[i + 2] >= '0' && name[i + 2] <= '7' &&
728 name[i + 3] >= '0' && name[i + 3] <= '7') {
729 name[j++] = (name[i + 1] - '0') * 64 +
730 (name[i + 2] - '0') * 8 +
731 (name[i + 3] - '0');
732 i += 3;
733 } else {
734 name[j++] = name[i];
735 }
736 }
737}
738
739static void build_fs_mount_list(FsMountList *mounts, Error **errp)
740{
741 FsMount *mount;
742 char const *mountinfo = "/proc/self/mountinfo";
743 FILE *fp;
744 char *line = NULL, *dash;
745 size_t n;
746 char check;
747 unsigned int devmajor, devminor;
748 int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e;
749
750 fp = fopen(mountinfo, "r");
751 if (!fp) {
752 build_fs_mount_list_from_mtab(mounts, errp);
753 return;
754 }
755
756 while (getline(&line, &n, fp) != -1) {
757 ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c",
758 &devmajor, &devminor, &dir_s, &dir_e, &check);
759 if (ret < 3) {
760 continue;
761 }
762 dash = strstr(line + dir_e, " - ");
763 if (!dash) {
764 continue;
765 }
766 ret = sscanf(dash, " - %n%*s%n %n%*s%n%c",
767 &type_s, &type_e, &dev_s, &dev_e, &check);
768 if (ret < 1) {
769 continue;
770 }
771 line[dir_e] = 0;
772 dash[type_e] = 0;
773 dash[dev_e] = 0;
774 decode_mntname(line + dir_s, dir_e - dir_s);
775 decode_mntname(dash + dev_s, dev_e - dev_s);
776 if (devmajor == 0) {
777 /* btrfs reports major number = 0 */
778 if (strcmp("btrfs", dash + type_s) != 0 ||
779 dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) {
780 continue;
781 }
782 }
783
f3a06403 784 mount = g_new0(FsMount, 1);
46d4c572
TS
785 mount->dirname = g_strdup(line + dir_s);
786 mount->devtype = g_strdup(dash + type_s);
787 mount->devmajor = devmajor;
788 mount->devminor = devminor;
789
790 QTAILQ_INSERT_TAIL(mounts, mount, next);
791 }
792 free(line);
793
794 fclose(fp);
795}
eab5fd59
PB
796#endif
797
798#if defined(CONFIG_FSFREEZE)
e3d4d252 799
46d4c572
TS
800static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
801{
802 char *path;
803 char *dpath;
804 char *driver = NULL;
805 char buf[PATH_MAX];
806 ssize_t len;
807
808 path = g_strndup(syspath, pathlen);
809 dpath = g_strdup_printf("%s/driver", path);
810 len = readlink(dpath, buf, sizeof(buf) - 1);
811 if (len != -1) {
812 buf[len] = 0;
3e015d81 813 driver = g_path_get_basename(buf);
46d4c572
TS
814 }
815 g_free(dpath);
816 g_free(path);
817 return driver;
818}
819
820static int compare_uint(const void *_a, const void *_b)
821{
822 unsigned int a = *(unsigned int *)_a;
823 unsigned int b = *(unsigned int *)_b;
824
825 return a < b ? -1 : a > b ? 1 : 0;
826}
827
828/* Walk the specified sysfs and build a sorted list of host or ata numbers */
829static int build_hosts(char const *syspath, char const *host, bool ata,
830 unsigned int *hosts, int hosts_max, Error **errp)
831{
832 char *path;
833 DIR *dir;
834 struct dirent *entry;
835 int i = 0;
836
837 path = g_strndup(syspath, host - syspath);
838 dir = opendir(path);
839 if (!dir) {
840 error_setg_errno(errp, errno, "opendir(\"%s\")", path);
841 g_free(path);
842 return -1;
843 }
844
845 while (i < hosts_max) {
846 entry = readdir(dir);
847 if (!entry) {
848 break;
849 }
850 if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) {
851 ++i;
852 } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) {
853 ++i;
854 }
855 }
856
857 qsort(hosts, i, sizeof(hosts[0]), compare_uint);
858
859 g_free(path);
860 closedir(dir);
861 return i;
862}
863
d9fe4f0f
TH
864/*
865 * Store disk device info for devices on the PCI bus.
866 * Returns true if information has been stored, or false for failure.
867 */
868static bool build_guest_fsinfo_for_pci_dev(char const *syspath,
869 GuestDiskAddress *disk,
870 Error **errp)
46d4c572
TS
871{
872 unsigned int pci[4], host, hosts[8], tgt[3];
873 int i, nhosts = 0, pcilen;
d9fe4f0f 874 GuestPCIAddress *pciaddr = disk->pci_controller;
46d4c572
TS
875 bool has_ata = false, has_host = false, has_tgt = false;
876 char *p, *q, *driver = NULL;
d9fe4f0f 877 bool ret = false;
46d4c572
TS
878
879 p = strstr(syspath, "/devices/pci");
880 if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
881 pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) {
743c71d0 882 g_debug("only pci device is supported: sysfs path '%s'", syspath);
d9fe4f0f 883 return false;
46d4c572
TS
884 }
885
743c71d0
MAL
886 p += 12 + pcilen;
887 while (true) {
888 driver = get_pci_driver(syspath, p - syspath, errp);
889 if (driver && (g_str_equal(driver, "ata_piix") ||
890 g_str_equal(driver, "sym53c8xx") ||
891 g_str_equal(driver, "virtio-pci") ||
892 g_str_equal(driver, "ahci"))) {
893 break;
894 }
895
bb23a736 896 g_free(driver);
743c71d0
MAL
897 if (sscanf(p, "/%x:%x:%x.%x%n",
898 pci, pci + 1, pci + 2, pci + 3, &pcilen) == 4) {
899 p += pcilen;
900 continue;
901 }
902
903 g_debug("unsupported driver or sysfs path '%s'", syspath);
d9fe4f0f 904 return false;
46d4c572
TS
905 }
906
907 p = strstr(syspath, "/target");
908 if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
909 tgt, tgt + 1, tgt + 2) == 3) {
910 has_tgt = true;
911 }
912
913 p = strstr(syspath, "/ata");
914 if (p) {
915 q = p + 4;
916 has_ata = true;
917 } else {
918 p = strstr(syspath, "/host");
919 q = p + 5;
920 }
921 if (p && sscanf(q, "%u", &host) == 1) {
922 has_host = true;
923 nhosts = build_hosts(syspath, p, has_ata, hosts,
01a6df1b 924 ARRAY_SIZE(hosts), errp);
46d4c572
TS
925 if (nhosts < 0) {
926 goto cleanup;
927 }
928 }
929
46d4c572
TS
930 pciaddr->domain = pci[0];
931 pciaddr->bus = pci[1];
932 pciaddr->slot = pci[2];
933 pciaddr->function = pci[3];
934
46d4c572
TS
935 if (strcmp(driver, "ata_piix") == 0) {
936 /* a host per ide bus, target*:0:<unit>:0 */
937 if (!has_host || !has_tgt) {
938 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
939 goto cleanup;
940 }
941 for (i = 0; i < nhosts; i++) {
942 if (host == hosts[i]) {
943 disk->bus_type = GUEST_DISK_BUS_TYPE_IDE;
944 disk->bus = i;
945 disk->unit = tgt[1];
946 break;
947 }
948 }
949 if (i >= nhosts) {
950 g_debug("no host for '%s' (driver '%s')", syspath, driver);
951 goto cleanup;
952 }
953 } else if (strcmp(driver, "sym53c8xx") == 0) {
954 /* scsi(LSI Logic): target*:0:<unit>:0 */
955 if (!has_tgt) {
956 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
957 goto cleanup;
958 }
959 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
960 disk->unit = tgt[1];
961 } else if (strcmp(driver, "virtio-pci") == 0) {
962 if (has_tgt) {
963 /* virtio-scsi: target*:0:0:<unit> */
964 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
965 disk->unit = tgt[2];
966 } else {
967 /* virtio-blk: 1 disk per 1 device */
968 disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
969 }
970 } else if (strcmp(driver, "ahci") == 0) {
971 /* ahci: 1 host per 1 unit */
972 if (!has_host || !has_tgt) {
973 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
974 goto cleanup;
975 }
976 for (i = 0; i < nhosts; i++) {
977 if (host == hosts[i]) {
978 disk->unit = i;
979 disk->bus_type = GUEST_DISK_BUS_TYPE_SATA;
980 break;
981 }
982 }
983 if (i >= nhosts) {
984 g_debug("no host for '%s' (driver '%s')", syspath, driver);
985 goto cleanup;
986 }
987 } else {
988 g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath);
989 goto cleanup;
990 }
991
d9fe4f0f 992 ret = true;
46d4c572
TS
993
994cleanup:
46d4c572 995 g_free(driver);
d9fe4f0f
TH
996 return ret;
997}
998
23843c12
TH
999/*
1000 * Store disk device info for non-PCI virtio devices (for example s390x
1001 * channel I/O devices). Returns true if information has been stored, or
1002 * false for failure.
1003 */
1004static bool build_guest_fsinfo_for_nonpci_virtio(char const *syspath,
1005 GuestDiskAddress *disk,
1006 Error **errp)
1007{
1008 unsigned int tgt[3];
1009 char *p;
1010
1011 if (!strstr(syspath, "/virtio") || !strstr(syspath, "/block")) {
1012 g_debug("Unsupported virtio device '%s'", syspath);
1013 return false;
1014 }
1015
1016 p = strstr(syspath, "/target");
1017 if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
1018 &tgt[0], &tgt[1], &tgt[2]) == 3) {
1019 /* virtio-scsi: target*:0:<target>:<unit> */
1020 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
1021 disk->bus = tgt[0];
1022 disk->target = tgt[1];
1023 disk->unit = tgt[2];
1024 } else {
1025 /* virtio-blk: 1 disk per 1 device */
1026 disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
1027 }
1028
1029 return true;
1030}
1031
d9fe4f0f
TH
1032/* Store disk device info specified by @sysfs into @fs */
1033static void build_guest_fsinfo_for_real_device(char const *syspath,
1034 GuestFilesystemInfo *fs,
1035 Error **errp)
1036{
1037 GuestDiskAddress *disk;
1038 GuestPCIAddress *pciaddr;
1039 GuestDiskAddressList *list = NULL;
1040 bool has_hwinf;
43dadc43
TH
1041#ifdef CONFIG_LIBUDEV
1042 struct udev *udev = NULL;
1043 struct udev_device *udevice = NULL;
1044#endif
d9fe4f0f
TH
1045
1046 pciaddr = g_new0(GuestPCIAddress, 1);
43dadc43
TH
1047 pciaddr->domain = -1; /* -1 means field is invalid */
1048 pciaddr->bus = -1;
1049 pciaddr->slot = -1;
1050 pciaddr->function = -1;
d9fe4f0f
TH
1051
1052 disk = g_new0(GuestDiskAddress, 1);
1053 disk->pci_controller = pciaddr;
43dadc43 1054 disk->bus_type = GUEST_DISK_BUS_TYPE_UNKNOWN;
d9fe4f0f
TH
1055
1056 list = g_new0(GuestDiskAddressList, 1);
1057 list->value = disk;
1058
43dadc43
TH
1059#ifdef CONFIG_LIBUDEV
1060 udev = udev_new();
1061 udevice = udev_device_new_from_syspath(udev, syspath);
1062 if (udev == NULL || udevice == NULL) {
1063 g_debug("failed to query udev");
1064 } else {
1065 const char *devnode, *serial;
1066 devnode = udev_device_get_devnode(udevice);
1067 if (devnode != NULL) {
1068 disk->dev = g_strdup(devnode);
1069 disk->has_dev = true;
1070 }
1071 serial = udev_device_get_property_value(udevice, "ID_SERIAL");
1072 if (serial != NULL && *serial != 0) {
1073 disk->serial = g_strdup(serial);
1074 disk->has_serial = true;
1075 }
1076 }
1077
1078 udev_unref(udev);
1079 udev_device_unref(udevice);
1080#endif
1081
23843c12
TH
1082 if (strstr(syspath, "/devices/pci")) {
1083 has_hwinf = build_guest_fsinfo_for_pci_dev(syspath, disk, errp);
1084 } else if (strstr(syspath, "/virtio")) {
1085 has_hwinf = build_guest_fsinfo_for_nonpci_virtio(syspath, disk, errp);
1086 } else {
1087 g_debug("Unsupported device type for '%s'", syspath);
1088 has_hwinf = false;
1089 }
d9fe4f0f 1090
43dadc43 1091 if (has_hwinf || disk->has_dev || disk->has_serial) {
d9fe4f0f
TH
1092 list->next = fs->disk;
1093 fs->disk = list;
1094 } else {
1095 qapi_free_GuestDiskAddressList(list);
1096 }
46d4c572
TS
1097}
1098
1099static void build_guest_fsinfo_for_device(char const *devpath,
1100 GuestFilesystemInfo *fs,
1101 Error **errp);
1102
1103/* Store a list of slave devices of virtual volume specified by @syspath into
1104 * @fs */
1105static void build_guest_fsinfo_for_virtual_device(char const *syspath,
1106 GuestFilesystemInfo *fs,
1107 Error **errp)
1108{
292743d9 1109 Error *err = NULL;
46d4c572
TS
1110 DIR *dir;
1111 char *dirpath;
e668d1b8 1112 struct dirent *entry;
46d4c572
TS
1113
1114 dirpath = g_strdup_printf("%s/slaves", syspath);
1115 dir = opendir(dirpath);
1116 if (!dir) {
8251a72f
MR
1117 if (errno != ENOENT) {
1118 error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
1119 }
46d4c572
TS
1120 g_free(dirpath);
1121 return;
1122 }
46d4c572
TS
1123
1124 for (;;) {
e668d1b8
HZ
1125 errno = 0;
1126 entry = readdir(dir);
1127 if (entry == NULL) {
1128 if (errno) {
1129 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
1130 }
46d4c572
TS
1131 break;
1132 }
1133
e668d1b8
HZ
1134 if (entry->d_type == DT_LNK) {
1135 char *path;
1136
1137 g_debug(" slave device '%s'", entry->d_name);
1138 path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
292743d9 1139 build_guest_fsinfo_for_device(path, fs, &err);
e668d1b8 1140 g_free(path);
46d4c572 1141
292743d9
MA
1142 if (err) {
1143 error_propagate(errp, err);
46d4c572
TS
1144 break;
1145 }
1146 }
1147 }
1148
e668d1b8 1149 g_free(dirpath);
46d4c572
TS
1150 closedir(dir);
1151}
1152
1153/* Dispatch to functions for virtual/real device */
1154static void build_guest_fsinfo_for_device(char const *devpath,
1155 GuestFilesystemInfo *fs,
1156 Error **errp)
1157{
1158 char *syspath = realpath(devpath, NULL);
1159
1160 if (!syspath) {
1161 error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
1162 return;
1163 }
1164
1165 if (!fs->name) {
3e015d81 1166 fs->name = g_path_get_basename(syspath);
46d4c572
TS
1167 }
1168
1169 g_debug(" parse sysfs path '%s'", syspath);
1170
1171 if (strstr(syspath, "/devices/virtual/block/")) {
1172 build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
1173 } else {
1174 build_guest_fsinfo_for_real_device(syspath, fs, errp);
1175 }
1176
1177 free(syspath);
1178}
1179
1180/* Return a list of the disk device(s)' info which @mount lies on */
1181static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1182 Error **errp)
1183{
1184 GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
25b5ff1a
CH
1185 struct statvfs buf;
1186 unsigned long used, nonroot_total, fr_size;
46d4c572
TS
1187 char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1188 mount->devmajor, mount->devminor);
1189
1190 fs->mountpoint = g_strdup(mount->dirname);
1191 fs->type = g_strdup(mount->devtype);
1192 build_guest_fsinfo_for_device(devpath, fs, errp);
1193
25b5ff1a
CH
1194 if (statvfs(fs->mountpoint, &buf) == 0) {
1195 fr_size = buf.f_frsize;
1196 used = buf.f_blocks - buf.f_bfree;
1197 nonroot_total = used + buf.f_bavail;
1198 fs->used_bytes = used * fr_size;
1199 fs->total_bytes = nonroot_total * fr_size;
1200
1201 fs->has_total_bytes = true;
1202 fs->has_used_bytes = true;
1203 }
1204
46d4c572 1205 g_free(devpath);
25b5ff1a 1206
46d4c572
TS
1207 return fs;
1208}
1209
1210GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1211{
1212 FsMountList mounts;
1213 struct FsMount *mount;
1214 GuestFilesystemInfoList *new, *ret = NULL;
1215 Error *local_err = NULL;
1216
1217 QTAILQ_INIT(&mounts);
1218 build_fs_mount_list(&mounts, &local_err);
1219 if (local_err) {
1220 error_propagate(errp, local_err);
1221 return NULL;
1222 }
1223
1224 QTAILQ_FOREACH(mount, &mounts, next) {
1225 g_debug("Building guest fsinfo for '%s'", mount->dirname);
1226
1227 new = g_malloc0(sizeof(*ret));
1228 new->value = build_guest_fsinfo(mount, &local_err);
1229 new->next = ret;
1230 ret = new;
1231 if (local_err) {
1232 error_propagate(errp, local_err);
1233 qapi_free_GuestFilesystemInfoList(ret);
1234 ret = NULL;
1235 break;
1236 }
1237 }
1238
1239 free_fs_mount_list(&mounts);
1240 return ret;
1241}
1242
1243
ec0f694c
TS
1244typedef enum {
1245 FSFREEZE_HOOK_THAW = 0,
1246 FSFREEZE_HOOK_FREEZE,
1247} FsfreezeHookArg;
1248
13a439ec 1249static const char *fsfreeze_hook_arg_string[] = {
ec0f694c
TS
1250 "thaw",
1251 "freeze",
1252};
1253
77dbc81b 1254static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
ec0f694c
TS
1255{
1256 int status;
1257 pid_t pid;
1258 const char *hook;
1259 const char *arg_str = fsfreeze_hook_arg_string[arg];
1260 Error *local_err = NULL;
1261
1262 hook = ga_fsfreeze_hook(ga_state);
1263 if (!hook) {
1264 return;
1265 }
1266 if (access(hook, X_OK) != 0) {
77dbc81b 1267 error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
ec0f694c
TS
1268 return;
1269 }
1270
1271 slog("executing fsfreeze hook with arg '%s'", arg_str);
1272 pid = fork();
1273 if (pid == 0) {
1274 setsid();
1275 reopen_fd_to_null(0);
1276 reopen_fd_to_null(1);
1277 reopen_fd_to_null(2);
1278
1279 execle(hook, hook, arg_str, NULL, environ);
1280 _exit(EXIT_FAILURE);
1281 } else if (pid < 0) {
77dbc81b 1282 error_setg_errno(errp, errno, "failed to create child process");
ec0f694c
TS
1283 return;
1284 }
1285
1286 ga_wait_child(pid, &status, &local_err);
84d18f06 1287 if (local_err) {
77dbc81b 1288 error_propagate(errp, local_err);
ec0f694c
TS
1289 return;
1290 }
1291
1292 if (!WIFEXITED(status)) {
77dbc81b 1293 error_setg(errp, "fsfreeze hook has terminated abnormally");
ec0f694c
TS
1294 return;
1295 }
1296
1297 status = WEXITSTATUS(status);
1298 if (status) {
77dbc81b 1299 error_setg(errp, "fsfreeze hook has failed with status %d", status);
ec0f694c
TS
1300 return;
1301 }
1302}
1303
e3d4d252
MR
1304/*
1305 * Return status of freeze/thaw
1306 */
77dbc81b 1307GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
e3d4d252 1308{
f22d85e9
MR
1309 if (ga_is_frozen(ga_state)) {
1310 return GUEST_FSFREEZE_STATUS_FROZEN;
1311 }
1312
1313 return GUEST_FSFREEZE_STATUS_THAWED;
e3d4d252
MR
1314}
1315
e99bce20
TS
1316int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1317{
1318 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
1319}
1320
e3d4d252
MR
1321/*
1322 * Walk list of mounted file systems in the guest, and freeze the ones which
1323 * are real local file systems.
1324 */
e99bce20
TS
1325int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1326 strList *mountpoints,
1327 Error **errp)
e3d4d252
MR
1328{
1329 int ret = 0, i = 0;
e99bce20 1330 strList *list;
af02203f
PB
1331 FsMountList mounts;
1332 struct FsMount *mount;
261551d1 1333 Error *local_err = NULL;
e3d4d252 1334 int fd;
e3d4d252
MR
1335
1336 slog("guest-fsfreeze called");
1337
ec0f694c 1338 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
84d18f06 1339 if (local_err) {
77dbc81b 1340 error_propagate(errp, local_err);
ec0f694c
TS
1341 return -1;
1342 }
1343
9e8aded4 1344 QTAILQ_INIT(&mounts);
261551d1 1345 build_fs_mount_list(&mounts, &local_err);
84d18f06 1346 if (local_err) {
77dbc81b 1347 error_propagate(errp, local_err);
261551d1 1348 return -1;
e3d4d252
MR
1349 }
1350
1351 /* cannot risk guest agent blocking itself on a write in this state */
f22d85e9 1352 ga_set_frozen(ga_state);
e3d4d252 1353
eae3eb3e 1354 QTAILQ_FOREACH_REVERSE(mount, &mounts, next) {
e99bce20
TS
1355 /* To issue fsfreeze in the reverse order of mounts, check if the
1356 * mount is listed in the list here */
1357 if (has_mountpoints) {
1358 for (list = mountpoints; list; list = list->next) {
1359 if (strcmp(list->value, mount->dirname) == 0) {
1360 break;
1361 }
1362 }
1363 if (!list) {
1364 continue;
1365 }
1366 }
1367
448058aa 1368 fd = qemu_open_old(mount->dirname, O_RDONLY);
e3d4d252 1369 if (fd == -1) {
77dbc81b 1370 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
e3d4d252
MR
1371 goto error;
1372 }
1373
e35916ac
MT
1374 /* we try to cull filesystems we know won't work in advance, but other
1375 * filesystems may not implement fsfreeze for less obvious reasons.
9e8aded4
MR
1376 * these will report EOPNOTSUPP. we simply ignore these when tallying
1377 * the number of frozen filesystems.
ce2eb6c4
PL
1378 * if a filesystem is mounted more than once (aka bind mount) a
1379 * consecutive attempt to freeze an already frozen filesystem will
1380 * return EBUSY.
9e8aded4
MR
1381 *
1382 * any other error means a failure to freeze a filesystem we
1383 * expect to be freezable, so return an error in those cases
1384 * and return system to thawed state.
e3d4d252
MR
1385 */
1386 ret = ioctl(fd, FIFREEZE);
9e8aded4 1387 if (ret == -1) {
ce2eb6c4 1388 if (errno != EOPNOTSUPP && errno != EBUSY) {
77dbc81b 1389 error_setg_errno(errp, errno, "failed to freeze %s",
617fbbc1 1390 mount->dirname);
9e8aded4
MR
1391 close(fd);
1392 goto error;
1393 }
1394 } else {
1395 i++;
e3d4d252
MR
1396 }
1397 close(fd);
e3d4d252
MR
1398 }
1399
af02203f 1400 free_fs_mount_list(&mounts);
65650f01
CH
1401 /* We may not issue any FIFREEZE here.
1402 * Just unset ga_state here and ready for the next call.
1403 */
1404 if (i == 0) {
1405 ga_unset_frozen(ga_state);
1406 }
e3d4d252
MR
1407 return i;
1408
1409error:
af02203f 1410 free_fs_mount_list(&mounts);
9e8aded4 1411 qmp_guest_fsfreeze_thaw(NULL);
e3d4d252
MR
1412 return 0;
1413}
1414
1415/*
1416 * Walk list of frozen file systems in the guest, and thaw them.
1417 */
77dbc81b 1418int64_t qmp_guest_fsfreeze_thaw(Error **errp)
e3d4d252
MR
1419{
1420 int ret;
af02203f
PB
1421 FsMountList mounts;
1422 FsMount *mount;
9e8aded4 1423 int fd, i = 0, logged;
261551d1 1424 Error *local_err = NULL;
9e8aded4
MR
1425
1426 QTAILQ_INIT(&mounts);
261551d1 1427 build_fs_mount_list(&mounts, &local_err);
84d18f06 1428 if (local_err) {
77dbc81b 1429 error_propagate(errp, local_err);
9e8aded4
MR
1430 return 0;
1431 }
e3d4d252 1432
9e8aded4
MR
1433 QTAILQ_FOREACH(mount, &mounts, next) {
1434 logged = false;
448058aa 1435 fd = qemu_open_old(mount->dirname, O_RDONLY);
e3d4d252 1436 if (fd == -1) {
e3d4d252
MR
1437 continue;
1438 }
9e8aded4
MR
1439 /* we have no way of knowing whether a filesystem was actually unfrozen
1440 * as a result of a successful call to FITHAW, only that if an error
1441 * was returned the filesystem was *not* unfrozen by that particular
1442 * call.
1443 *
a31f0531 1444 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
9e8aded4
MR
1445 * to unfreeze, continuing issuing FITHAW until an error is returned,
1446 * in which case either the filesystem is in an unfreezable state, or,
1447 * more likely, it was thawed previously (and remains so afterward).
1448 *
1449 * also, since the most recent successful call is the one that did
1450 * the actual unfreeze, we can use this to provide an accurate count
1451 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1452 * may * be useful for determining whether a filesystem was unfrozen
1453 * during the freeze/thaw phase by a process other than qemu-ga.
1454 */
1455 do {
1456 ret = ioctl(fd, FITHAW);
1457 if (ret == 0 && !logged) {
1458 i++;
1459 logged = true;
1460 }
1461 } while (ret == 0);
e3d4d252 1462 close(fd);
e3d4d252
MR
1463 }
1464
f22d85e9 1465 ga_unset_frozen(ga_state);
af02203f 1466 free_fs_mount_list(&mounts);
ec0f694c 1467
77dbc81b 1468 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
ec0f694c 1469
e3d4d252
MR
1470 return i;
1471}
1472
e3d4d252
MR
1473static void guest_fsfreeze_cleanup(void)
1474{
e3d4d252
MR
1475 Error *err = NULL;
1476
f22d85e9 1477 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
6f686749
MA
1478 qmp_guest_fsfreeze_thaw(&err);
1479 if (err) {
1480 slog("failed to clean up frozen filesystems: %s",
1481 error_get_pretty(err));
1482 error_free(err);
e3d4d252
MR
1483 }
1484 }
1485}
e72c3f2e 1486#endif /* CONFIG_FSFREEZE */
e3d4d252 1487
eab5fd59
PB
1488#if defined(CONFIG_FSTRIM)
1489/*
1490 * Walk list of mounted file systems in the guest, and trim them.
1491 */
e82855d9
JO
1492GuestFilesystemTrimResponse *
1493qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
eab5fd59 1494{
e82855d9
JO
1495 GuestFilesystemTrimResponse *response;
1496 GuestFilesystemTrimResultList *list;
1497 GuestFilesystemTrimResult *result;
eab5fd59
PB
1498 int ret = 0;
1499 FsMountList mounts;
1500 struct FsMount *mount;
1501 int fd;
261551d1 1502 Error *local_err = NULL;
73a652a1 1503 struct fstrim_range r;
eab5fd59
PB
1504
1505 slog("guest-fstrim called");
1506
1507 QTAILQ_INIT(&mounts);
261551d1 1508 build_fs_mount_list(&mounts, &local_err);
84d18f06 1509 if (local_err) {
77dbc81b 1510 error_propagate(errp, local_err);
e82855d9 1511 return NULL;
eab5fd59
PB
1512 }
1513
e82855d9
JO
1514 response = g_malloc0(sizeof(*response));
1515
eab5fd59 1516 QTAILQ_FOREACH(mount, &mounts, next) {
e82855d9
JO
1517 result = g_malloc0(sizeof(*result));
1518 result->path = g_strdup(mount->dirname);
1519
1520 list = g_malloc0(sizeof(*list));
1521 list->value = result;
1522 list->next = response->paths;
1523 response->paths = list;
1524
448058aa 1525 fd = qemu_open_old(mount->dirname, O_RDONLY);
eab5fd59 1526 if (fd == -1) {
e82855d9
JO
1527 result->error = g_strdup_printf("failed to open: %s",
1528 strerror(errno));
1529 result->has_error = true;
1530 continue;
eab5fd59
PB
1531 }
1532
e35916ac
MT
1533 /* We try to cull filesystems we know won't work in advance, but other
1534 * filesystems may not implement fstrim for less obvious reasons.
1535 * These will report EOPNOTSUPP; while in some other cases ENOTTY
1536 * will be reported (e.g. CD-ROMs).
e82855d9 1537 * Any other error means an unexpected error.
eab5fd59 1538 */
73a652a1
JO
1539 r.start = 0;
1540 r.len = -1;
1541 r.minlen = has_minimum ? minimum : 0;
eab5fd59
PB
1542 ret = ioctl(fd, FITRIM, &r);
1543 if (ret == -1) {
e82855d9
JO
1544 result->has_error = true;
1545 if (errno == ENOTTY || errno == EOPNOTSUPP) {
1546 result->error = g_strdup("trim not supported");
1547 } else {
1548 result->error = g_strdup_printf("failed to trim: %s",
1549 strerror(errno));
eab5fd59 1550 }
e82855d9
JO
1551 close(fd);
1552 continue;
eab5fd59 1553 }
e82855d9
JO
1554
1555 result->has_minimum = true;
1556 result->minimum = r.minlen;
1557 result->has_trimmed = true;
1558 result->trimmed = r.len;
eab5fd59
PB
1559 close(fd);
1560 }
1561
eab5fd59 1562 free_fs_mount_list(&mounts);
e82855d9 1563 return response;
eab5fd59
PB
1564}
1565#endif /* CONFIG_FSTRIM */
1566
1567
11d0f125
LC
1568#define LINUX_SYS_STATE_FILE "/sys/power/state"
1569#define SUSPEND_SUPPORTED 0
1570#define SUSPEND_NOT_SUPPORTED 1
1571
8b020b5e
DHB
1572typedef enum {
1573 SUSPEND_MODE_DISK = 0,
1574 SUSPEND_MODE_RAM = 1,
1575 SUSPEND_MODE_HYBRID = 2,
1576} SuspendMode;
1577
1578/*
1579 * Executes a command in a child process using g_spawn_sync,
1580 * returning an int >= 0 representing the exit status of the
1581 * process.
1582 *
1583 * If the program wasn't found in path, returns -1.
1584 *
1585 * If a problem happened when creating the child process,
1586 * returns -1 and errp is set.
1587 */
1588static int run_process_child(const char *command[], Error **errp)
11d0f125 1589{
8b020b5e
DHB
1590 int exit_status, spawn_flag;
1591 GError *g_err = NULL;
1592 bool success;
1593
1594 spawn_flag = G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL |
1595 G_SPAWN_STDERR_TO_DEV_NULL;
11d0f125 1596
8b020b5e
DHB
1597 success = g_spawn_sync(NULL, (char **)command, environ, spawn_flag,
1598 NULL, NULL, NULL, NULL,
1599 &exit_status, &g_err);
304a0fcb 1600
8b020b5e
DHB
1601 if (success) {
1602 return WEXITSTATUS(exit_status);
304a0fcb
DHB
1603 }
1604
8b020b5e
DHB
1605 if (g_err && (g_err->code != G_SPAWN_ERROR_NOENT)) {
1606 error_setg(errp, "failed to create child process, error '%s'",
1607 g_err->message);
a5fcf0e3 1608 }
11d0f125 1609
8b020b5e
DHB
1610 g_error_free(g_err);
1611 return -1;
1612}
1613
067927d6
DHB
1614static bool systemd_supports_mode(SuspendMode mode, Error **errp)
1615{
067927d6
DHB
1616 const char *systemctl_args[3] = {"systemd-hibernate", "systemd-suspend",
1617 "systemd-hybrid-sleep"};
1618 const char *cmd[4] = {"systemctl", "status", systemctl_args[mode], NULL};
1619 int status;
1620
992861fb 1621 status = run_process_child(cmd, errp);
067927d6
DHB
1622
1623 /*
1624 * systemctl status uses LSB return codes so we can expect
1625 * status > 0 and be ok. To assert if the guest has support
1626 * for the selected suspend mode, status should be < 4. 4 is
1627 * the code for unknown service status, the return value when
1628 * the service does not exist. A common value is status = 3
1629 * (program is not running).
1630 */
1631 if (status > 0 && status < 4) {
1632 return true;
1633 }
1634
067927d6
DHB
1635 return false;
1636}
1637
1638static void systemd_suspend(SuspendMode mode, Error **errp)
1639{
1640 Error *local_err = NULL;
1641 const char *systemctl_args[3] = {"hibernate", "suspend", "hybrid-sleep"};
1642 const char *cmd[3] = {"systemctl", systemctl_args[mode], NULL};
1643 int status;
1644
1645 status = run_process_child(cmd, &local_err);
1646
1647 if (status == 0) {
1648 return;
1649 }
1650
1651 if ((status == -1) && !local_err) {
1652 error_setg(errp, "the helper program 'systemctl %s' was not found",
1653 systemctl_args[mode]);
1654 return;
1655 }
1656
1657 if (local_err) {
1658 error_propagate(errp, local_err);
1659 } else {
1660 error_setg(errp, "the helper program 'systemctl %s' returned an "
1661 "unexpected exit status code (%d)",
1662 systemctl_args[mode], status);
1663 }
1664}
1665
8b020b5e
DHB
1666static bool pmutils_supports_mode(SuspendMode mode, Error **errp)
1667{
1668 Error *local_err = NULL;
1669 const char *pmutils_args[3] = {"--hibernate", "--suspend",
1670 "--suspend-hybrid"};
1671 const char *cmd[3] = {"pm-is-supported", pmutils_args[mode], NULL};
1672 int status;
1673
1674 status = run_process_child(cmd, &local_err);
1675
1676 if (status == SUSPEND_SUPPORTED) {
1677 return true;
11d0f125
LC
1678 }
1679
8b020b5e
DHB
1680 if ((status == -1) && !local_err) {
1681 return false;
6b26e837 1682 }
11d0f125 1683
8b020b5e
DHB
1684 if (local_err) {
1685 error_propagate(errp, local_err);
1686 } else {
77dbc81b 1687 error_setg(errp,
8b020b5e
DHB
1688 "the helper program '%s' returned an unexpected exit"
1689 " status code (%d)", "pm-is-supported", status);
11d0f125
LC
1690 }
1691
8b020b5e 1692 return false;
a5fcf0e3
DHB
1693}
1694
8b020b5e 1695static void pmutils_suspend(SuspendMode mode, Error **errp)
246d76eb
DHB
1696{
1697 Error *local_err = NULL;
8b020b5e
DHB
1698 const char *pmutils_binaries[3] = {"pm-hibernate", "pm-suspend",
1699 "pm-suspend-hybrid"};
1700 const char *cmd[2] = {pmutils_binaries[mode], NULL};
246d76eb
DHB
1701 int status;
1702
8b020b5e 1703 status = run_process_child(cmd, &local_err);
246d76eb 1704
8b020b5e 1705 if (status == 0) {
246d76eb
DHB
1706 return;
1707 }
1708
8b020b5e
DHB
1709 if ((status == -1) && !local_err) {
1710 error_setg(errp, "the helper program '%s' was not found",
1711 pmutils_binaries[mode]);
1712 return;
246d76eb
DHB
1713 }
1714
246d76eb
DHB
1715 if (local_err) {
1716 error_propagate(errp, local_err);
8b020b5e 1717 } else {
246d76eb 1718 error_setg(errp,
8b020b5e
DHB
1719 "the helper program '%s' returned an unexpected exit"
1720 " status code (%d)", pmutils_binaries[mode], status);
246d76eb 1721 }
246d76eb
DHB
1722}
1723
8b020b5e 1724static bool linux_sys_state_supports_mode(SuspendMode mode, Error **errp)
a5fcf0e3 1725{
8b020b5e
DHB
1726 const char *sysfile_strs[3] = {"disk", "mem", NULL};
1727 const char *sysfile_str = sysfile_strs[mode];
a5fcf0e3
DHB
1728 char buf[32]; /* hopefully big enough */
1729 int fd;
1730 ssize_t ret;
1731
8b020b5e
DHB
1732 if (!sysfile_str) {
1733 error_setg(errp, "unknown guest suspend mode");
a5fcf0e3
DHB
1734 return false;
1735 }
1736
1737 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1738 if (fd < 0) {
1739 return false;
1740 }
1741
1742 ret = read(fd, buf, sizeof(buf) - 1);
d9c745c1 1743 close(fd);
a5fcf0e3
DHB
1744 if (ret <= 0) {
1745 return false;
1746 }
1747 buf[ret] = '\0';
1748
1749 if (strstr(buf, sysfile_str)) {
1750 return true;
1751 }
1752 return false;
1753}
1754
8b020b5e 1755static void linux_sys_state_suspend(SuspendMode mode, Error **errp)
11d0f125 1756{
7b376087 1757 Error *local_err = NULL;
8b020b5e
DHB
1758 const char *sysfile_strs[3] = {"disk", "mem", NULL};
1759 const char *sysfile_str = sysfile_strs[mode];
7b376087 1760 pid_t pid;
dc8764f0 1761 int status;
11d0f125 1762
8b020b5e 1763 if (!sysfile_str) {
304a0fcb
DHB
1764 error_setg(errp, "unknown guest suspend mode");
1765 return;
1766 }
1767
11d0f125 1768 pid = fork();
246d76eb 1769 if (!pid) {
11d0f125
LC
1770 /* child */
1771 int fd;
1772
1773 setsid();
1774 reopen_fd_to_null(0);
1775 reopen_fd_to_null(1);
1776 reopen_fd_to_null(2);
1777
11d0f125
LC
1778 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1779 if (fd < 0) {
1780 _exit(EXIT_FAILURE);
1781 }
1782
1783 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1784 _exit(EXIT_FAILURE);
1785 }
1786
1787 _exit(EXIT_SUCCESS);
7b376087 1788 } else if (pid < 0) {
77dbc81b 1789 error_setg_errno(errp, errno, "failed to create child process");
246d76eb 1790 return;
11d0f125
LC
1791 }
1792
7b376087 1793 ga_wait_child(pid, &status, &local_err);
84d18f06 1794 if (local_err) {
77dbc81b 1795 error_propagate(errp, local_err);
246d76eb 1796 return;
dc8764f0
LC
1797 }
1798
7b376087 1799 if (WEXITSTATUS(status)) {
77dbc81b 1800 error_setg(errp, "child process has failed to suspend");
11d0f125 1801 }
dc8764f0 1802
246d76eb
DHB
1803}
1804
8b020b5e 1805static void guest_suspend(SuspendMode mode, Error **errp)
246d76eb
DHB
1806{
1807 Error *local_err = NULL;
73e1d8eb 1808 bool mode_supported = false;
246d76eb 1809
73e1d8eb
DHB
1810 if (systemd_supports_mode(mode, &local_err)) {
1811 mode_supported = true;
1812 systemd_suspend(mode, &local_err);
246d76eb
DHB
1813 }
1814
067927d6
DHB
1815 if (!local_err) {
1816 return;
1817 }
1818
1819 error_free(local_err);
6a4a3853 1820 local_err = NULL;
067927d6 1821
73e1d8eb
DHB
1822 if (pmutils_supports_mode(mode, &local_err)) {
1823 mode_supported = true;
1824 pmutils_suspend(mode, &local_err);
1825 }
1826
246d76eb
DHB
1827 if (!local_err) {
1828 return;
1829 }
1830
1831 error_free(local_err);
6a4a3853 1832 local_err = NULL;
246d76eb 1833
73e1d8eb
DHB
1834 if (linux_sys_state_supports_mode(mode, &local_err)) {
1835 mode_supported = true;
1836 linux_sys_state_suspend(mode, &local_err);
1837 }
1838
1839 if (!mode_supported) {
6a4a3853 1840 error_free(local_err);
73e1d8eb
DHB
1841 error_setg(errp,
1842 "the requested suspend mode is not supported by the guest");
b2322003 1843 } else {
246d76eb
DHB
1844 error_propagate(errp, local_err);
1845 }
11d0f125
LC
1846}
1847
77dbc81b 1848void qmp_guest_suspend_disk(Error **errp)
11d0f125 1849{
304a0fcb 1850 guest_suspend(SUSPEND_MODE_DISK, errp);
11d0f125
LC
1851}
1852
77dbc81b 1853void qmp_guest_suspend_ram(Error **errp)
fbf42210 1854{
304a0fcb 1855 guest_suspend(SUSPEND_MODE_RAM, errp);
fbf42210
LC
1856}
1857
77dbc81b 1858void qmp_guest_suspend_hybrid(Error **errp)
95f4f404 1859{
304a0fcb 1860 guest_suspend(SUSPEND_MODE_HYBRID, errp);
95f4f404
LC
1861}
1862
3424fc9f
MP
1863static GuestNetworkInterfaceList *
1864guest_find_interface(GuestNetworkInterfaceList *head,
1865 const char *name)
1866{
1867 for (; head; head = head->next) {
1868 if (strcmp(head->value->name, name) == 0) {
1869 break;
1870 }
1871 }
1872
1873 return head;
1874}
1875
53f9fcb2
ZL
1876static int guest_get_network_stats(const char *name,
1877 GuestNetworkInterfaceStat *stats)
1878{
1879 int name_len;
1880 char const *devinfo = "/proc/net/dev";
1881 FILE *fp;
1882 char *line = NULL, *colon;
1883 size_t n = 0;
1884 fp = fopen(devinfo, "r");
1885 if (!fp) {
1886 return -1;
1887 }
1888 name_len = strlen(name);
1889 while (getline(&line, &n, fp) != -1) {
1890 long long dummy;
1891 long long rx_bytes;
1892 long long rx_packets;
1893 long long rx_errs;
1894 long long rx_dropped;
1895 long long tx_bytes;
1896 long long tx_packets;
1897 long long tx_errs;
1898 long long tx_dropped;
1899 char *trim_line;
1900 trim_line = g_strchug(line);
1901 if (trim_line[0] == '\0') {
1902 continue;
1903 }
1904 colon = strchr(trim_line, ':');
1905 if (!colon) {
1906 continue;
1907 }
1908 if (colon - name_len == trim_line &&
1909 strncmp(trim_line, name, name_len) == 0) {
1910 if (sscanf(colon + 1,
1911 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld",
1912 &rx_bytes, &rx_packets, &rx_errs, &rx_dropped,
1913 &dummy, &dummy, &dummy, &dummy,
1914 &tx_bytes, &tx_packets, &tx_errs, &tx_dropped,
1915 &dummy, &dummy, &dummy, &dummy) != 16) {
1916 continue;
1917 }
1918 stats->rx_bytes = rx_bytes;
1919 stats->rx_packets = rx_packets;
1920 stats->rx_errs = rx_errs;
1921 stats->rx_dropped = rx_dropped;
1922 stats->tx_bytes = tx_bytes;
1923 stats->tx_packets = tx_packets;
1924 stats->tx_errs = tx_errs;
1925 stats->tx_dropped = tx_dropped;
1926 fclose(fp);
1927 g_free(line);
1928 return 0;
1929 }
1930 }
1931 fclose(fp);
1932 g_free(line);
1933 g_debug("/proc/net/dev: Interface '%s' not found", name);
1934 return -1;
1935}
1936
3424fc9f
MP
1937/*
1938 * Build information about guest interfaces
1939 */
1940GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1941{
1942 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1943 struct ifaddrs *ifap, *ifa;
3424fc9f
MP
1944
1945 if (getifaddrs(&ifap) < 0) {
878a0ae0 1946 error_setg_errno(errp, errno, "getifaddrs failed");
3424fc9f
MP
1947 goto error;
1948 }
1949
1950 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1951 GuestNetworkInterfaceList *info;
1952 GuestIpAddressList **address_list = NULL, *address_item = NULL;
53f9fcb2 1953 GuestNetworkInterfaceStat *interface_stat = NULL;
3424fc9f
MP
1954 char addr4[INET_ADDRSTRLEN];
1955 char addr6[INET6_ADDRSTRLEN];
1956 int sock;
1957 struct ifreq ifr;
1958 unsigned char *mac_addr;
1959 void *p;
1960
1961 g_debug("Processing %s interface", ifa->ifa_name);
1962
1963 info = guest_find_interface(head, ifa->ifa_name);
1964
1965 if (!info) {
1966 info = g_malloc0(sizeof(*info));
1967 info->value = g_malloc0(sizeof(*info->value));
1968 info->value->name = g_strdup(ifa->ifa_name);
1969
1970 if (!cur_item) {
1971 head = cur_item = info;
1972 } else {
1973 cur_item->next = info;
1974 cur_item = info;
1975 }
1976 }
1977
1978 if (!info->value->has_hardware_address &&
1979 ifa->ifa_flags & SIOCGIFHWADDR) {
1980 /* we haven't obtained HW address yet */
1981 sock = socket(PF_INET, SOCK_STREAM, 0);
1982 if (sock == -1) {
878a0ae0 1983 error_setg_errno(errp, errno, "failed to create socket");
3424fc9f
MP
1984 goto error;
1985 }
1986
1987 memset(&ifr, 0, sizeof(ifr));
1ab516ed 1988 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
3424fc9f 1989 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
878a0ae0
LC
1990 error_setg_errno(errp, errno,
1991 "failed to get MAC address of %s",
1992 ifa->ifa_name);
10a2158f 1993 close(sock);
3424fc9f
MP
1994 goto error;
1995 }
1996
10a2158f 1997 close(sock);
3424fc9f
MP
1998 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1999
e4ada482
SW
2000 info->value->hardware_address =
2001 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
2002 (int) mac_addr[0], (int) mac_addr[1],
2003 (int) mac_addr[2], (int) mac_addr[3],
2004 (int) mac_addr[4], (int) mac_addr[5]);
3424fc9f
MP
2005
2006 info->value->has_hardware_address = true;
3424fc9f
MP
2007 }
2008
2009 if (ifa->ifa_addr &&
2010 ifa->ifa_addr->sa_family == AF_INET) {
2011 /* interface with IPv4 address */
3424fc9f
MP
2012 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
2013 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
878a0ae0 2014 error_setg_errno(errp, errno, "inet_ntop failed");
3424fc9f
MP
2015 goto error;
2016 }
2017
10a2158f
MA
2018 address_item = g_malloc0(sizeof(*address_item));
2019 address_item->value = g_malloc0(sizeof(*address_item->value));
3424fc9f
MP
2020 address_item->value->ip_address = g_strdup(addr4);
2021 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
2022
2023 if (ifa->ifa_netmask) {
2024 /* Count the number of set bits in netmask.
2025 * This is safe as '1' and '0' cannot be shuffled in netmask. */
2026 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
2027 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
2028 }
2029 } else if (ifa->ifa_addr &&
2030 ifa->ifa_addr->sa_family == AF_INET6) {
2031 /* interface with IPv6 address */
3424fc9f
MP
2032 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
2033 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
878a0ae0 2034 error_setg_errno(errp, errno, "inet_ntop failed");
3424fc9f
MP
2035 goto error;
2036 }
2037
10a2158f
MA
2038 address_item = g_malloc0(sizeof(*address_item));
2039 address_item->value = g_malloc0(sizeof(*address_item->value));
3424fc9f
MP
2040 address_item->value->ip_address = g_strdup(addr6);
2041 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
2042
2043 if (ifa->ifa_netmask) {
2044 /* Count the number of set bits in netmask.
2045 * This is safe as '1' and '0' cannot be shuffled in netmask. */
2046 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
2047 address_item->value->prefix =
2048 ctpop32(((uint32_t *) p)[0]) +
2049 ctpop32(((uint32_t *) p)[1]) +
2050 ctpop32(((uint32_t *) p)[2]) +
2051 ctpop32(((uint32_t *) p)[3]);
2052 }
2053 }
2054
2055 if (!address_item) {
2056 continue;
2057 }
2058
2059 address_list = &info->value->ip_addresses;
2060
2061 while (*address_list && (*address_list)->next) {
2062 address_list = &(*address_list)->next;
2063 }
2064
2065 if (!*address_list) {
2066 *address_list = address_item;
2067 } else {
2068 (*address_list)->next = address_item;
2069 }
2070
2071 info->value->has_ip_addresses = true;
2072
53f9fcb2
ZL
2073 if (!info->value->has_statistics) {
2074 interface_stat = g_malloc0(sizeof(*interface_stat));
2075 if (guest_get_network_stats(info->value->name,
2076 interface_stat) == -1) {
2077 info->value->has_statistics = false;
2078 g_free(interface_stat);
2079 } else {
2080 info->value->statistics = interface_stat;
2081 info->value->has_statistics = true;
2082 }
2083 }
3424fc9f
MP
2084 }
2085
2086 freeifaddrs(ifap);
2087 return head;
2088
2089error:
2090 freeifaddrs(ifap);
2091 qapi_free_GuestNetworkInterfaceList(head);
2092 return NULL;
2093}
2094
77dbc81b 2095#define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
d2baff62 2096
77dbc81b 2097static long sysconf_exact(int name, const char *name_str, Error **errp)
d2baff62
LE
2098{
2099 long ret;
2100
2101 errno = 0;
2102 ret = sysconf(name);
2103 if (ret == -1) {
2104 if (errno == 0) {
77dbc81b 2105 error_setg(errp, "sysconf(%s): value indefinite", name_str);
d2baff62 2106 } else {
77dbc81b 2107 error_setg_errno(errp, errno, "sysconf(%s)", name_str);
d2baff62
LE
2108 }
2109 }
2110 return ret;
2111}
2112
2113/* Transfer online/offline status between @vcpu and the guest system.
2114 *
2115 * On input either @errp or *@errp must be NULL.
2116 *
2117 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
2118 * - R: vcpu->logical_id
2119 * - W: vcpu->online
2120 * - W: vcpu->can_offline
2121 *
2122 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
2123 * - R: vcpu->logical_id
2124 * - R: vcpu->online
2125 *
2126 * Written members remain unmodified on error.
2127 */
2128static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
b4bf912a 2129 char *dirpath, Error **errp)
d2baff62 2130{
b4bf912a
IM
2131 int fd;
2132 int res;
d2baff62 2133 int dirfd;
b4bf912a 2134 static const char fn[] = "online";
d2baff62 2135
d2baff62
LE
2136 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2137 if (dirfd == -1) {
2138 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
b4bf912a
IM
2139 return;
2140 }
d2baff62 2141
b4bf912a
IM
2142 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
2143 if (fd == -1) {
2144 if (errno != ENOENT) {
2145 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
2146 } else if (sys2vcpu) {
2147 vcpu->online = true;
2148 vcpu->can_offline = false;
2149 } else if (!vcpu->online) {
2150 error_setg(errp, "logical processor #%" PRId64 " can't be "
2151 "offlined", vcpu->logical_id);
2152 } /* otherwise pretend successful re-onlining */
2153 } else {
2154 unsigned char status;
2155
2156 res = pread(fd, &status, 1, 0);
2157 if (res == -1) {
2158 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
2159 } else if (res == 0) {
2160 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
2161 fn);
2162 } else if (sys2vcpu) {
2163 vcpu->online = (status != '0');
2164 vcpu->can_offline = true;
2165 } else if (vcpu->online != (status != '0')) {
2166 status = '0' + vcpu->online;
2167 if (pwrite(fd, &status, 1, 0) == -1) {
2168 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
2169 fn);
2170 }
2171 } /* otherwise pretend successful re-(on|off)-lining */
d2baff62 2172
b4bf912a 2173 res = close(fd);
d2baff62
LE
2174 g_assert(res == 0);
2175 }
2176
b4bf912a
IM
2177 res = close(dirfd);
2178 g_assert(res == 0);
d2baff62
LE
2179}
2180
2181GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2182{
2183 int64_t current;
2184 GuestLogicalProcessorList *head, **link;
2185 long sc_max;
2186 Error *local_err = NULL;
2187
2188 current = 0;
2189 head = NULL;
2190 link = &head;
2191 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
2192
2193 while (local_err == NULL && current < sc_max) {
2194 GuestLogicalProcessor *vcpu;
2195 GuestLogicalProcessorList *entry;
b4bf912a
IM
2196 int64_t id = current++;
2197 char *path = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
2198 id);
2199
2200 if (g_file_test(path, G_FILE_TEST_EXISTS)) {
2201 vcpu = g_malloc0(sizeof *vcpu);
2202 vcpu->logical_id = id;
2203 vcpu->has_can_offline = true; /* lolspeak ftw */
2204 transfer_vcpu(vcpu, true, path, &local_err);
2205 entry = g_malloc0(sizeof *entry);
2206 entry->value = vcpu;
2207 *link = entry;
2208 link = &entry->next;
2209 }
2210 g_free(path);
d2baff62
LE
2211 }
2212
2213 if (local_err == NULL) {
2214 /* there's no guest with zero VCPUs */
2215 g_assert(head != NULL);
2216 return head;
2217 }
2218
2219 qapi_free_GuestLogicalProcessorList(head);
2220 error_propagate(errp, local_err);
2221 return NULL;
2222}
2223
cbb65fc2
LE
2224int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2225{
2226 int64_t processed;
2227 Error *local_err = NULL;
2228
2229 processed = 0;
2230 while (vcpus != NULL) {
b4bf912a
IM
2231 char *path = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
2232 vcpus->value->logical_id);
2233
2234 transfer_vcpu(vcpus->value, false, path, &local_err);
2235 g_free(path);
cbb65fc2
LE
2236 if (local_err != NULL) {
2237 break;
2238 }
2239 ++processed;
2240 vcpus = vcpus->next;
2241 }
2242
2243 if (local_err != NULL) {
2244 if (processed == 0) {
2245 error_propagate(errp, local_err);
2246 } else {
2247 error_free(local_err);
2248 }
2249 }
2250
2251 return processed;
2252}
2253
215a2771
DB
2254void qmp_guest_set_user_password(const char *username,
2255 const char *password,
2256 bool crypted,
2257 Error **errp)
2258{
2259 Error *local_err = NULL;
2260 char *passwd_path = NULL;
2261 pid_t pid;
2262 int status;
2263 int datafd[2] = { -1, -1 };
2264 char *rawpasswddata = NULL;
2265 size_t rawpasswdlen;
2266 char *chpasswddata = NULL;
2267 size_t chpasswdlen;
2268
920639ca
DB
2269 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
2270 if (!rawpasswddata) {
2271 return;
2272 }
215a2771
DB
2273 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
2274 rawpasswddata[rawpasswdlen] = '\0';
2275
2276 if (strchr(rawpasswddata, '\n')) {
2277 error_setg(errp, "forbidden characters in raw password");
2278 goto out;
2279 }
2280
2281 if (strchr(username, '\n') ||
2282 strchr(username, ':')) {
2283 error_setg(errp, "forbidden characters in username");
2284 goto out;
2285 }
2286
2287 chpasswddata = g_strdup_printf("%s:%s\n", username, rawpasswddata);
2288 chpasswdlen = strlen(chpasswddata);
2289
2290 passwd_path = g_find_program_in_path("chpasswd");
2291
2292 if (!passwd_path) {
2293 error_setg(errp, "cannot find 'passwd' program in PATH");
2294 goto out;
2295 }
2296
2297 if (pipe(datafd) < 0) {
2298 error_setg(errp, "cannot create pipe FDs");
2299 goto out;
2300 }
2301
2302 pid = fork();
2303 if (pid == 0) {
2304 close(datafd[1]);
2305 /* child */
2306 setsid();
2307 dup2(datafd[0], 0);
2308 reopen_fd_to_null(1);
2309 reopen_fd_to_null(2);
2310
2311 if (crypted) {
2312 execle(passwd_path, "chpasswd", "-e", NULL, environ);
2313 } else {
2314 execle(passwd_path, "chpasswd", NULL, environ);
2315 }
2316 _exit(EXIT_FAILURE);
2317 } else if (pid < 0) {
2318 error_setg_errno(errp, errno, "failed to create child process");
2319 goto out;
2320 }
2321 close(datafd[0]);
2322 datafd[0] = -1;
2323
2324 if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) != chpasswdlen) {
2325 error_setg_errno(errp, errno, "cannot write new account password");
2326 goto out;
2327 }
2328 close(datafd[1]);
2329 datafd[1] = -1;
2330
2331 ga_wait_child(pid, &status, &local_err);
2332 if (local_err) {
2333 error_propagate(errp, local_err);
2334 goto out;
2335 }
2336
2337 if (!WIFEXITED(status)) {
2338 error_setg(errp, "child process has terminated abnormally");
2339 goto out;
2340 }
2341
2342 if (WEXITSTATUS(status)) {
2343 error_setg(errp, "child process has failed to set user password");
2344 goto out;
2345 }
2346
2347out:
2348 g_free(chpasswddata);
2349 g_free(rawpasswddata);
2350 g_free(passwd_path);
2351 if (datafd[0] != -1) {
2352 close(datafd[0]);
2353 }
2354 if (datafd[1] != -1) {
2355 close(datafd[1]);
2356 }
2357}
2358
bd240fca
HZ
2359static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf,
2360 int size, Error **errp)
2361{
2362 int fd;
2363 int res;
2364
2365 errno = 0;
2366 fd = openat(dirfd, pathname, O_RDONLY);
2367 if (fd == -1) {
2368 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2369 return;
2370 }
2371
2372 res = pread(fd, buf, size, 0);
2373 if (res == -1) {
2374 error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname);
2375 } else if (res == 0) {
2376 error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname);
2377 }
2378 close(fd);
2379}
2380
2381static void ga_write_sysfs_file(int dirfd, const char *pathname,
2382 const char *buf, int size, Error **errp)
2383{
2384 int fd;
2385
2386 errno = 0;
2387 fd = openat(dirfd, pathname, O_WRONLY);
2388 if (fd == -1) {
2389 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2390 return;
2391 }
2392
2393 if (pwrite(fd, buf, size, 0) == -1) {
2394 error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname);
2395 }
2396
2397 close(fd);
2398}
2399
2400/* Transfer online/offline status between @mem_blk and the guest system.
2401 *
2402 * On input either @errp or *@errp must be NULL.
2403 *
2404 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2405 * - R: mem_blk->phys_index
2406 * - W: mem_blk->online
2407 * - W: mem_blk->can_offline
2408 *
2409 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2410 * - R: mem_blk->phys_index
2411 * - R: mem_blk->online
2412 *- R: mem_blk->can_offline
2413 * Written members remain unmodified on error.
2414 */
2415static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
2416 GuestMemoryBlockResponse *result,
2417 Error **errp)
2418{
2419 char *dirpath;
2420 int dirfd;
2421 char *status;
2422 Error *local_err = NULL;
2423
2424 if (!sys2memblk) {
2425 DIR *dp;
2426
2427 if (!result) {
2428 error_setg(errp, "Internal error, 'result' should not be NULL");
2429 return;
2430 }
2431 errno = 0;
2432 dp = opendir("/sys/devices/system/memory/");
2433 /* if there is no 'memory' directory in sysfs,
2434 * we think this VM does not support online/offline memory block,
2435 * any other solution?
2436 */
9879f5ac
PMD
2437 if (!dp) {
2438 if (errno == ENOENT) {
2439 result->response =
2440 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2441 }
bd240fca
HZ
2442 goto out1;
2443 }
2444 closedir(dp);
2445 }
2446
2447 dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/",
2448 mem_blk->phys_index);
2449 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2450 if (dirfd == -1) {
2451 if (sys2memblk) {
2452 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2453 } else {
2454 if (errno == ENOENT) {
2455 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND;
2456 } else {
2457 result->response =
2458 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2459 }
2460 }
2461 g_free(dirpath);
2462 goto out1;
2463 }
2464 g_free(dirpath);
2465
2466 status = g_malloc0(10);
2467 ga_read_sysfs_file(dirfd, "state", status, 10, &local_err);
2468 if (local_err) {
2469 /* treat with sysfs file that not exist in old kernel */
2470 if (errno == ENOENT) {
2471 error_free(local_err);
2472 if (sys2memblk) {
2473 mem_blk->online = true;
2474 mem_blk->can_offline = false;
2475 } else if (!mem_blk->online) {
2476 result->response =
2477 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2478 }
2479 } else {
2480 if (sys2memblk) {
2481 error_propagate(errp, local_err);
2482 } else {
b368123d 2483 error_free(local_err);
bd240fca
HZ
2484 result->response =
2485 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2486 }
2487 }
2488 goto out2;
2489 }
2490
2491 if (sys2memblk) {
2492 char removable = '0';
2493
2494 mem_blk->online = (strncmp(status, "online", 6) == 0);
2495
2496 ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err);
2497 if (local_err) {
67cc32eb 2498 /* if no 'removable' file, it doesn't support offline mem blk */
bd240fca
HZ
2499 if (errno == ENOENT) {
2500 error_free(local_err);
2501 mem_blk->can_offline = false;
2502 } else {
2503 error_propagate(errp, local_err);
2504 }
2505 } else {
2506 mem_blk->can_offline = (removable != '0');
2507 }
2508 } else {
2509 if (mem_blk->online != (strncmp(status, "online", 6) == 0)) {
7064024d 2510 const char *new_state = mem_blk->online ? "online" : "offline";
bd240fca
HZ
2511
2512 ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state),
2513 &local_err);
bd240fca
HZ
2514 if (local_err) {
2515 error_free(local_err);
2516 result->response =
2517 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2518 goto out2;
2519 }
2520
2521 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS;
2522 result->has_error_code = false;
2523 } /* otherwise pretend successful re-(on|off)-lining */
2524 }
2525 g_free(status);
2526 close(dirfd);
2527 return;
2528
2529out2:
2530 g_free(status);
2531 close(dirfd);
2532out1:
2533 if (!sys2memblk) {
2534 result->has_error_code = true;
2535 result->error_code = errno;
2536 }
2537}
2538
a065aaa9
HZ
2539GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2540{
bd240fca
HZ
2541 GuestMemoryBlockList *head, **link;
2542 Error *local_err = NULL;
2543 struct dirent *de;
2544 DIR *dp;
2545
2546 head = NULL;
2547 link = &head;
2548
2549 dp = opendir("/sys/devices/system/memory/");
2550 if (!dp) {
f693fe6e
MR
2551 /* it's ok if this happens to be a system that doesn't expose
2552 * memory blocks via sysfs, but otherwise we should report
2553 * an error
2554 */
2555 if (errno != ENOENT) {
2556 error_setg_errno(errp, errno, "Can't open directory"
9af9e0fe 2557 "\"/sys/devices/system/memory/\"");
f693fe6e 2558 }
bd240fca
HZ
2559 return NULL;
2560 }
2561
2562 /* Note: the phys_index of memory block may be discontinuous,
2563 * this is because a memblk is the unit of the Sparse Memory design, which
2564 * allows discontinuous memory ranges (ex. NUMA), so here we should
2565 * traverse the memory block directory.
2566 */
2567 while ((de = readdir(dp)) != NULL) {
2568 GuestMemoryBlock *mem_blk;
2569 GuestMemoryBlockList *entry;
2570
2571 if ((strncmp(de->d_name, "memory", 6) != 0) ||
2572 !(de->d_type & DT_DIR)) {
2573 continue;
2574 }
2575
2576 mem_blk = g_malloc0(sizeof *mem_blk);
2577 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */
2578 mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10);
2579 mem_blk->has_can_offline = true; /* lolspeak ftw */
2580 transfer_memory_block(mem_blk, true, NULL, &local_err);
4155c998
MA
2581 if (local_err) {
2582 break;
2583 }
bd240fca
HZ
2584
2585 entry = g_malloc0(sizeof *entry);
2586 entry->value = mem_blk;
2587
2588 *link = entry;
2589 link = &entry->next;
2590 }
2591
2592 closedir(dp);
2593 if (local_err == NULL) {
2594 /* there's no guest with zero memory blocks */
2595 if (head == NULL) {
2596 error_setg(errp, "guest reported zero memory blocks!");
2597 }
2598 return head;
2599 }
2600
2601 qapi_free_GuestMemoryBlockList(head);
2602 error_propagate(errp, local_err);
a065aaa9
HZ
2603 return NULL;
2604}
2605
2606GuestMemoryBlockResponseList *
2607qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2608{
32ca7927
HZ
2609 GuestMemoryBlockResponseList *head, **link;
2610 Error *local_err = NULL;
2611
2612 head = NULL;
2613 link = &head;
2614
2615 while (mem_blks != NULL) {
2616 GuestMemoryBlockResponse *result;
2617 GuestMemoryBlockResponseList *entry;
2618 GuestMemoryBlock *current_mem_blk = mem_blks->value;
2619
2620 result = g_malloc0(sizeof(*result));
2621 result->phys_index = current_mem_blk->phys_index;
2622 transfer_memory_block(current_mem_blk, false, result, &local_err);
2623 if (local_err) { /* should never happen */
2624 goto err;
2625 }
2626 entry = g_malloc0(sizeof *entry);
2627 entry->value = result;
2628
2629 *link = entry;
2630 link = &entry->next;
2631 mem_blks = mem_blks->next;
2632 }
2633
2634 return head;
2635err:
2636 qapi_free_GuestMemoryBlockResponseList(head);
2637 error_propagate(errp, local_err);
a065aaa9
HZ
2638 return NULL;
2639}
2640
2641GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2642{
ef82b60b
HZ
2643 Error *local_err = NULL;
2644 char *dirpath;
2645 int dirfd;
2646 char *buf;
2647 GuestMemoryBlockInfo *info;
2648
2649 dirpath = g_strdup_printf("/sys/devices/system/memory/");
2650 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2651 if (dirfd == -1) {
2652 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2653 g_free(dirpath);
2654 return NULL;
2655 }
2656 g_free(dirpath);
2657
2658 buf = g_malloc0(20);
2659 ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err);
8ce1ee46 2660 close(dirfd);
ef82b60b
HZ
2661 if (local_err) {
2662 g_free(buf);
2663 error_propagate(errp, local_err);
2664 return NULL;
2665 }
2666
2667 info = g_new0(GuestMemoryBlockInfo, 1);
2668 info->size = strtol(buf, NULL, 16); /* the unit is bytes */
2669
2670 g_free(buf);
2671
2672 return info;
a065aaa9
HZ
2673}
2674
e72c3f2e
MR
2675#else /* defined(__linux__) */
2676
77dbc81b 2677void qmp_guest_suspend_disk(Error **errp)
e72c3f2e 2678{
c6bd8c70 2679 error_setg(errp, QERR_UNSUPPORTED);
e72c3f2e
MR
2680}
2681
77dbc81b 2682void qmp_guest_suspend_ram(Error **errp)
e72c3f2e 2683{
c6bd8c70 2684 error_setg(errp, QERR_UNSUPPORTED);
e72c3f2e
MR
2685}
2686
77dbc81b 2687void qmp_guest_suspend_hybrid(Error **errp)
e72c3f2e 2688{
c6bd8c70 2689 error_setg(errp, QERR_UNSUPPORTED);
e72c3f2e
MR
2690}
2691
d35d4cb5 2692GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
e72c3f2e 2693{
c6bd8c70 2694 error_setg(errp, QERR_UNSUPPORTED);
d35d4cb5 2695 return NULL;
e72c3f2e
MR
2696}
2697
d2baff62
LE
2698GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2699{
c6bd8c70 2700 error_setg(errp, QERR_UNSUPPORTED);
d2baff62
LE
2701 return NULL;
2702}
2703
cbb65fc2
LE
2704int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2705{
c6bd8c70 2706 error_setg(errp, QERR_UNSUPPORTED);
cbb65fc2
LE
2707 return -1;
2708}
2709
215a2771
DB
2710void qmp_guest_set_user_password(const char *username,
2711 const char *password,
2712 bool crypted,
2713 Error **errp)
2714{
c6bd8c70 2715 error_setg(errp, QERR_UNSUPPORTED);
215a2771
DB
2716}
2717
a065aaa9
HZ
2718GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2719{
c6bd8c70 2720 error_setg(errp, QERR_UNSUPPORTED);
a065aaa9
HZ
2721 return NULL;
2722}
2723
2724GuestMemoryBlockResponseList *
2725qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2726{
c6bd8c70 2727 error_setg(errp, QERR_UNSUPPORTED);
a065aaa9
HZ
2728 return NULL;
2729}
2730
2731GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2732{
c6bd8c70 2733 error_setg(errp, QERR_UNSUPPORTED);
a065aaa9
HZ
2734 return NULL;
2735}
2736
d35d4cb5
MR
2737#endif
2738
2739#if !defined(CONFIG_FSFREEZE)
2740
46d4c572
TS
2741GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
2742{
c6bd8c70 2743 error_setg(errp, QERR_UNSUPPORTED);
46d4c572
TS
2744 return NULL;
2745}
2746
77dbc81b 2747GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
e72c3f2e 2748{
c6bd8c70 2749 error_setg(errp, QERR_UNSUPPORTED);
d35d4cb5
MR
2750
2751 return 0;
e72c3f2e
MR
2752}
2753
77dbc81b 2754int64_t qmp_guest_fsfreeze_freeze(Error **errp)
e72c3f2e 2755{
c6bd8c70 2756 error_setg(errp, QERR_UNSUPPORTED);
d35d4cb5
MR
2757
2758 return 0;
e72c3f2e
MR
2759}
2760
e99bce20
TS
2761int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
2762 strList *mountpoints,
2763 Error **errp)
2764{
c6bd8c70 2765 error_setg(errp, QERR_UNSUPPORTED);
e99bce20
TS
2766
2767 return 0;
2768}
2769
77dbc81b 2770int64_t qmp_guest_fsfreeze_thaw(Error **errp)
e72c3f2e 2771{
c6bd8c70 2772 error_setg(errp, QERR_UNSUPPORTED);
d35d4cb5
MR
2773
2774 return 0;
e72c3f2e 2775}
eab5fd59
PB
2776#endif /* CONFIG_FSFREEZE */
2777
2778#if !defined(CONFIG_FSTRIM)
e82855d9
JO
2779GuestFilesystemTrimResponse *
2780qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
eab5fd59 2781{
c6bd8c70 2782 error_setg(errp, QERR_UNSUPPORTED);
e82855d9 2783 return NULL;
eab5fd59 2784}
e72c3f2e
MR
2785#endif
2786
1281c08a
TS
2787/* add unsupported commands to the blacklist */
2788GList *ga_command_blacklist_init(GList *blacklist)
2789{
2790#if !defined(__linux__)
2791 {
2792 const char *list[] = {
2793 "guest-suspend-disk", "guest-suspend-ram",
2794 "guest-suspend-hybrid", "guest-network-get-interfaces",
0dd38a03
HZ
2795 "guest-get-vcpus", "guest-set-vcpus",
2796 "guest-get-memory-blocks", "guest-set-memory-blocks",
28d8dd35
BS
2797 "guest-get-memory-block-size", "guest-get-memory-block-info",
2798 NULL};
1281c08a
TS
2799 char **p = (char **)list;
2800
2801 while (*p) {
4bca81ce 2802 blacklist = g_list_append(blacklist, g_strdup(*p++));
1281c08a
TS
2803 }
2804 }
2805#endif
2806
2807#if !defined(CONFIG_FSFREEZE)
2808 {
2809 const char *list[] = {
2810 "guest-get-fsinfo", "guest-fsfreeze-status",
2811 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
2812 "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL};
2813 char **p = (char **)list;
2814
2815 while (*p) {
4bca81ce 2816 blacklist = g_list_append(blacklist, g_strdup(*p++));
1281c08a
TS
2817 }
2818 }
2819#endif
2820
2821#if !defined(CONFIG_FSTRIM)
4bca81ce 2822 blacklist = g_list_append(blacklist, g_strdup("guest-fstrim"));
1281c08a
TS
2823#endif
2824
2e4211ce
TG
2825 blacklist = g_list_append(blacklist, g_strdup("guest-get-devices"));
2826
1281c08a
TS
2827 return blacklist;
2828}
2829
e3d4d252
MR
2830/* register init/cleanup routines for stateful command groups */
2831void ga_command_state_init(GAState *s, GACommandState *cs)
2832{
7006b9cf 2833#if defined(CONFIG_FSFREEZE)
f22d85e9 2834 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
7006b9cf 2835#endif
e3d4d252 2836}
161a56a9 2837
e674605f
TG
2838#ifdef HAVE_UTMPX
2839
161a56a9
VF
2840#define QGA_MICRO_SECOND_TO_SECOND 1000000
2841
2842static double ga_get_login_time(struct utmpx *user_info)
2843{
2844 double seconds = (double)user_info->ut_tv.tv_sec;
2845 double useconds = (double)user_info->ut_tv.tv_usec;
2846 useconds /= QGA_MICRO_SECOND_TO_SECOND;
2847 return seconds + useconds;
2848}
2849
b90abbac 2850GuestUserList *qmp_guest_get_users(Error **errp)
161a56a9
VF
2851{
2852 GHashTable *cache = NULL;
2853 GuestUserList *head = NULL, *cur_item = NULL;
2854 struct utmpx *user_info = NULL;
2855 gpointer value = NULL;
2856 GuestUser *user = NULL;
2857 GuestUserList *item = NULL;
2858 double login_time = 0;
2859
2860 cache = g_hash_table_new(g_str_hash, g_str_equal);
2861 setutxent();
2862
2863 for (;;) {
2864 user_info = getutxent();
2865 if (user_info == NULL) {
2866 break;
2867 } else if (user_info->ut_type != USER_PROCESS) {
2868 continue;
2869 } else if (g_hash_table_contains(cache, user_info->ut_user)) {
2870 value = g_hash_table_lookup(cache, user_info->ut_user);
2871 user = (GuestUser *)value;
2872 login_time = ga_get_login_time(user_info);
2873 /* We're ensuring the earliest login time to be sent */
2874 if (login_time < user->login_time) {
2875 user->login_time = login_time;
2876 }
2877 continue;
2878 }
2879
2880 item = g_new0(GuestUserList, 1);
2881 item->value = g_new0(GuestUser, 1);
2882 item->value->user = g_strdup(user_info->ut_user);
2883 item->value->login_time = ga_get_login_time(user_info);
2884
2885 g_hash_table_insert(cache, item->value->user, item->value);
2886
2887 if (!cur_item) {
2888 head = cur_item = item;
2889 } else {
2890 cur_item->next = item;
2891 cur_item = item;
2892 }
2893 }
2894 endutxent();
2895 g_hash_table_destroy(cache);
2896 return head;
2897}
e674605f
TG
2898
2899#else
2900
2901GuestUserList *qmp_guest_get_users(Error **errp)
2902{
2903 error_setg(errp, QERR_UNSUPPORTED);
2904 return NULL;
2905}
2906
2907#endif
9848f797
TG
2908
2909/* Replace escaped special characters with theire real values. The replacement
2910 * is done in place -- returned value is in the original string.
2911 */
2912static void ga_osrelease_replace_special(gchar *value)
2913{
2914 gchar *p, *p2, quote;
2915
2916 /* Trim the string at first space or semicolon if it is not enclosed in
2917 * single or double quotes. */
2918 if ((value[0] != '"') || (value[0] == '\'')) {
2919 p = strchr(value, ' ');
2920 if (p != NULL) {
2921 *p = 0;
2922 }
2923 p = strchr(value, ';');
2924 if (p != NULL) {
2925 *p = 0;
2926 }
2927 return;
2928 }
2929
2930 quote = value[0];
2931 p2 = value;
2932 p = value + 1;
2933 while (*p != 0) {
2934 if (*p == '\\') {
2935 p++;
2936 switch (*p) {
2937 case '$':
2938 case '\'':
2939 case '"':
2940 case '\\':
2941 case '`':
2942 break;
2943 default:
2944 /* Keep literal backslash followed by whatever is there */
2945 p--;
2946 break;
2947 }
2948 } else if (*p == quote) {
2949 *p2 = 0;
2950 break;
2951 }
2952 *(p2++) = *(p++);
2953 }
2954}
2955
2956static GKeyFile *ga_parse_osrelease(const char *fname)
2957{
2958 gchar *content = NULL;
2959 gchar *content2 = NULL;
2960 GError *err = NULL;
2961 GKeyFile *keys = g_key_file_new();
2962 const char *group = "[os-release]\n";
2963
2964 if (!g_file_get_contents(fname, &content, NULL, &err)) {
2965 slog("failed to read '%s', error: %s", fname, err->message);
2966 goto fail;
2967 }
2968
2969 if (!g_utf8_validate(content, -1, NULL)) {
2970 slog("file is not utf-8 encoded: %s", fname);
2971 goto fail;
2972 }
2973 content2 = g_strdup_printf("%s%s", group, content);
2974
2975 if (!g_key_file_load_from_data(keys, content2, -1, G_KEY_FILE_NONE,
2976 &err)) {
2977 slog("failed to parse file '%s', error: %s", fname, err->message);
2978 goto fail;
2979 }
2980
2981 g_free(content);
2982 g_free(content2);
2983 return keys;
2984
2985fail:
2986 g_error_free(err);
2987 g_free(content);
2988 g_free(content2);
2989 g_key_file_free(keys);
2990 return NULL;
2991}
2992
2993GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
2994{
2995 GuestOSInfo *info = NULL;
2996 struct utsname kinfo;
339ca68b
TG
2997 GKeyFile *osrelease = NULL;
2998 const char *qga_os_release = g_getenv("QGA_OS_RELEASE");
9848f797
TG
2999
3000 info = g_new0(GuestOSInfo, 1);
3001
3002 if (uname(&kinfo) != 0) {
3003 error_setg_errno(errp, errno, "uname failed");
3004 } else {
3005 info->has_kernel_version = true;
3006 info->kernel_version = g_strdup(kinfo.version);
3007 info->has_kernel_release = true;
3008 info->kernel_release = g_strdup(kinfo.release);
3009 info->has_machine = true;
3010 info->machine = g_strdup(kinfo.machine);
3011 }
3012
339ca68b
TG
3013 if (qga_os_release != NULL) {
3014 osrelease = ga_parse_osrelease(qga_os_release);
3015 } else {
3016 osrelease = ga_parse_osrelease("/etc/os-release");
3017 if (osrelease == NULL) {
3018 osrelease = ga_parse_osrelease("/usr/lib/os-release");
3019 }
9848f797
TG
3020 }
3021
3022 if (osrelease != NULL) {
3023 char *value;
3024
3025#define GET_FIELD(field, osfield) do { \
3026 value = g_key_file_get_value(osrelease, "os-release", osfield, NULL); \
3027 if (value != NULL) { \
3028 ga_osrelease_replace_special(value); \
3029 info->has_ ## field = true; \
3030 info->field = value; \
3031 } \
3032} while (0)
3033 GET_FIELD(id, "ID");
3034 GET_FIELD(name, "NAME");
3035 GET_FIELD(pretty_name, "PRETTY_NAME");
3036 GET_FIELD(version, "VERSION");
3037 GET_FIELD(version_id, "VERSION_ID");
3038 GET_FIELD(variant, "VARIANT");
3039 GET_FIELD(variant_id, "VARIANT_ID");
3040#undef GET_FIELD
3041
3042 g_key_file_free(osrelease);
3043 }
3044
3045 return info;
3046}
2e4211ce
TG
3047
3048GuestDeviceInfoList *qmp_guest_get_devices(Error **errp)
3049{
3050 error_setg(errp, QERR_UNSUPPORTED);
3051
3052 return NULL;
3053}