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