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