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