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