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