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