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