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