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