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