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