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