Fixed focal glibc build issues for ubuntu and windows
[sdk/emulator/qemu.git] / qga / commands-posix.c
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 "qga/guest-agent-core.h"
19 #include "qga-qmp-commands.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qemu/queue.h"
22 #include "qemu/host-utils.h"
23 #include "qemu/sockets.h"
24 #include "qemu/base64.h"
25 #include "qemu/cutils.h"
26
27 #ifndef CONFIG_HAS_ENVIRON
28 #ifdef __APPLE__
29 #include <crt_externs.h>
30 #define environ (*_NSGetEnviron())
31 #else
32 extern char **environ;
33 #endif
34 #endif
35
36 #if defined(__linux__)
37 #include <mntent.h>
38 #include <linux/fs.h>
39 #include <ifaddrs.h>
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #include <sys/sysmacros.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         error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
1004         g_free(dirpath);
1005         return;
1006     }
1007
1008     for (;;) {
1009         errno = 0;
1010         entry = readdir(dir);
1011         if (entry == NULL) {
1012             if (errno) {
1013                 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
1014             }
1015             break;
1016         }
1017
1018         if (entry->d_type == DT_LNK) {
1019             char *path;
1020
1021             g_debug(" slave device '%s'", entry->d_name);
1022             path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
1023             build_guest_fsinfo_for_device(path, fs, errp);
1024             g_free(path);
1025
1026             if (*errp) {
1027                 break;
1028             }
1029         }
1030     }
1031
1032     g_free(dirpath);
1033     closedir(dir);
1034 }
1035
1036 /* Dispatch to functions for virtual/real device */
1037 static void build_guest_fsinfo_for_device(char const *devpath,
1038                                           GuestFilesystemInfo *fs,
1039                                           Error **errp)
1040 {
1041     char *syspath = realpath(devpath, NULL);
1042
1043     if (!syspath) {
1044         error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
1045         return;
1046     }
1047
1048     if (!fs->name) {
1049         fs->name = g_strdup(basename(syspath));
1050     }
1051
1052     g_debug("  parse sysfs path '%s'", syspath);
1053
1054     if (strstr(syspath, "/devices/virtual/block/")) {
1055         build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
1056     } else {
1057         build_guest_fsinfo_for_real_device(syspath, fs, errp);
1058     }
1059
1060     free(syspath);
1061 }
1062
1063 /* Return a list of the disk device(s)' info which @mount lies on */
1064 static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1065                                                Error **errp)
1066 {
1067     GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
1068     char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1069                                     mount->devmajor, mount->devminor);
1070
1071     fs->mountpoint = g_strdup(mount->dirname);
1072     fs->type = g_strdup(mount->devtype);
1073     build_guest_fsinfo_for_device(devpath, fs, errp);
1074
1075     g_free(devpath);
1076     return fs;
1077 }
1078
1079 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1080 {
1081     FsMountList mounts;
1082     struct FsMount *mount;
1083     GuestFilesystemInfoList *new, *ret = NULL;
1084     Error *local_err = NULL;
1085
1086     QTAILQ_INIT(&mounts);
1087     build_fs_mount_list(&mounts, &local_err);
1088     if (local_err) {
1089         error_propagate(errp, local_err);
1090         return NULL;
1091     }
1092
1093     QTAILQ_FOREACH(mount, &mounts, next) {
1094         g_debug("Building guest fsinfo for '%s'", mount->dirname);
1095
1096         new = g_malloc0(sizeof(*ret));
1097         new->value = build_guest_fsinfo(mount, &local_err);
1098         new->next = ret;
1099         ret = new;
1100         if (local_err) {
1101             error_propagate(errp, local_err);
1102             qapi_free_GuestFilesystemInfoList(ret);
1103             ret = NULL;
1104             break;
1105         }
1106     }
1107
1108     free_fs_mount_list(&mounts);
1109     return ret;
1110 }
1111
1112
1113 typedef enum {
1114     FSFREEZE_HOOK_THAW = 0,
1115     FSFREEZE_HOOK_FREEZE,
1116 } FsfreezeHookArg;
1117
1118 static const char *fsfreeze_hook_arg_string[] = {
1119     "thaw",
1120     "freeze",
1121 };
1122
1123 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
1124 {
1125     int status;
1126     pid_t pid;
1127     const char *hook;
1128     const char *arg_str = fsfreeze_hook_arg_string[arg];
1129     Error *local_err = NULL;
1130
1131     hook = ga_fsfreeze_hook(ga_state);
1132     if (!hook) {
1133         return;
1134     }
1135     if (access(hook, X_OK) != 0) {
1136         error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
1137         return;
1138     }
1139
1140     slog("executing fsfreeze hook with arg '%s'", arg_str);
1141     pid = fork();
1142     if (pid == 0) {
1143         setsid();
1144         reopen_fd_to_null(0);
1145         reopen_fd_to_null(1);
1146         reopen_fd_to_null(2);
1147
1148         execle(hook, hook, arg_str, NULL, environ);
1149         _exit(EXIT_FAILURE);
1150     } else if (pid < 0) {
1151         error_setg_errno(errp, errno, "failed to create child process");
1152         return;
1153     }
1154
1155     ga_wait_child(pid, &status, &local_err);
1156     if (local_err) {
1157         error_propagate(errp, local_err);
1158         return;
1159     }
1160
1161     if (!WIFEXITED(status)) {
1162         error_setg(errp, "fsfreeze hook has terminated abnormally");
1163         return;
1164     }
1165
1166     status = WEXITSTATUS(status);
1167     if (status) {
1168         error_setg(errp, "fsfreeze hook has failed with status %d", status);
1169         return;
1170     }
1171 }
1172
1173 /*
1174  * Return status of freeze/thaw
1175  */
1176 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
1177 {
1178     if (ga_is_frozen(ga_state)) {
1179         return GUEST_FSFREEZE_STATUS_FROZEN;
1180     }
1181
1182     return GUEST_FSFREEZE_STATUS_THAWED;
1183 }
1184
1185 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1186 {
1187     return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
1188 }
1189
1190 /*
1191  * Walk list of mounted file systems in the guest, and freeze the ones which
1192  * are real local file systems.
1193  */
1194 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1195                                        strList *mountpoints,
1196                                        Error **errp)
1197 {
1198     int ret = 0, i = 0;
1199     strList *list;
1200     FsMountList mounts;
1201     struct FsMount *mount;
1202     Error *local_err = NULL;
1203     int fd;
1204
1205     slog("guest-fsfreeze called");
1206
1207     execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
1208     if (local_err) {
1209         error_propagate(errp, local_err);
1210         return -1;
1211     }
1212
1213     QTAILQ_INIT(&mounts);
1214     build_fs_mount_list(&mounts, &local_err);
1215     if (local_err) {
1216         error_propagate(errp, local_err);
1217         return -1;
1218     }
1219
1220     /* cannot risk guest agent blocking itself on a write in this state */
1221     ga_set_frozen(ga_state);
1222
1223     QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
1224         /* To issue fsfreeze in the reverse order of mounts, check if the
1225          * mount is listed in the list here */
1226         if (has_mountpoints) {
1227             for (list = mountpoints; list; list = list->next) {
1228                 if (strcmp(list->value, mount->dirname) == 0) {
1229                     break;
1230                 }
1231             }
1232             if (!list) {
1233                 continue;
1234             }
1235         }
1236
1237         fd = qemu_open(mount->dirname, O_RDONLY);
1238         if (fd == -1) {
1239             error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
1240             goto error;
1241         }
1242
1243         /* we try to cull filesystems we know won't work in advance, but other
1244          * filesystems may not implement fsfreeze for less obvious reasons.
1245          * these will report EOPNOTSUPP. we simply ignore these when tallying
1246          * the number of frozen filesystems.
1247          *
1248          * any other error means a failure to freeze a filesystem we
1249          * expect to be freezable, so return an error in those cases
1250          * and return system to thawed state.
1251          */
1252         ret = ioctl(fd, FIFREEZE);
1253         if (ret == -1) {
1254             if (errno != EOPNOTSUPP) {
1255                 error_setg_errno(errp, errno, "failed to freeze %s",
1256                                  mount->dirname);
1257                 close(fd);
1258                 goto error;
1259             }
1260         } else {
1261             i++;
1262         }
1263         close(fd);
1264     }
1265
1266     free_fs_mount_list(&mounts);
1267     return i;
1268
1269 error:
1270     free_fs_mount_list(&mounts);
1271     qmp_guest_fsfreeze_thaw(NULL);
1272     return 0;
1273 }
1274
1275 /*
1276  * Walk list of frozen file systems in the guest, and thaw them.
1277  */
1278 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
1279 {
1280     int ret;
1281     FsMountList mounts;
1282     FsMount *mount;
1283     int fd, i = 0, logged;
1284     Error *local_err = NULL;
1285
1286     QTAILQ_INIT(&mounts);
1287     build_fs_mount_list(&mounts, &local_err);
1288     if (local_err) {
1289         error_propagate(errp, local_err);
1290         return 0;
1291     }
1292
1293     QTAILQ_FOREACH(mount, &mounts, next) {
1294         logged = false;
1295         fd = qemu_open(mount->dirname, O_RDONLY);
1296         if (fd == -1) {
1297             continue;
1298         }
1299         /* we have no way of knowing whether a filesystem was actually unfrozen
1300          * as a result of a successful call to FITHAW, only that if an error
1301          * was returned the filesystem was *not* unfrozen by that particular
1302          * call.
1303          *
1304          * since multiple preceding FIFREEZEs require multiple calls to FITHAW
1305          * to unfreeze, continuing issuing FITHAW until an error is returned,
1306          * in which case either the filesystem is in an unfreezable state, or,
1307          * more likely, it was thawed previously (and remains so afterward).
1308          *
1309          * also, since the most recent successful call is the one that did
1310          * the actual unfreeze, we can use this to provide an accurate count
1311          * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1312          * may * be useful for determining whether a filesystem was unfrozen
1313          * during the freeze/thaw phase by a process other than qemu-ga.
1314          */
1315         do {
1316             ret = ioctl(fd, FITHAW);
1317             if (ret == 0 && !logged) {
1318                 i++;
1319                 logged = true;
1320             }
1321         } while (ret == 0);
1322         close(fd);
1323     }
1324
1325     ga_unset_frozen(ga_state);
1326     free_fs_mount_list(&mounts);
1327
1328     execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
1329
1330     return i;
1331 }
1332
1333 static void guest_fsfreeze_cleanup(void)
1334 {
1335     Error *err = NULL;
1336
1337     if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
1338         qmp_guest_fsfreeze_thaw(&err);
1339         if (err) {
1340             slog("failed to clean up frozen filesystems: %s",
1341                  error_get_pretty(err));
1342             error_free(err);
1343         }
1344     }
1345 }
1346 #endif /* CONFIG_FSFREEZE */
1347
1348 #if defined(CONFIG_FSTRIM)
1349 /*
1350  * Walk list of mounted file systems in the guest, and trim them.
1351  */
1352 GuestFilesystemTrimResponse *
1353 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
1354 {
1355     GuestFilesystemTrimResponse *response;
1356     GuestFilesystemTrimResultList *list;
1357     GuestFilesystemTrimResult *result;
1358     int ret = 0;
1359     FsMountList mounts;
1360     struct FsMount *mount;
1361     int fd;
1362     Error *local_err = NULL;
1363     struct fstrim_range r;
1364
1365     slog("guest-fstrim called");
1366
1367     QTAILQ_INIT(&mounts);
1368     build_fs_mount_list(&mounts, &local_err);
1369     if (local_err) {
1370         error_propagate(errp, local_err);
1371         return NULL;
1372     }
1373
1374     response = g_malloc0(sizeof(*response));
1375
1376     QTAILQ_FOREACH(mount, &mounts, next) {
1377         result = g_malloc0(sizeof(*result));
1378         result->path = g_strdup(mount->dirname);
1379
1380         list = g_malloc0(sizeof(*list));
1381         list->value = result;
1382         list->next = response->paths;
1383         response->paths = list;
1384
1385         fd = qemu_open(mount->dirname, O_RDONLY);
1386         if (fd == -1) {
1387             result->error = g_strdup_printf("failed to open: %s",
1388                                             strerror(errno));
1389             result->has_error = true;
1390             continue;
1391         }
1392
1393         /* We try to cull filesystems we know won't work in advance, but other
1394          * filesystems may not implement fstrim for less obvious reasons.
1395          * These will report EOPNOTSUPP; while in some other cases ENOTTY
1396          * will be reported (e.g. CD-ROMs).
1397          * Any other error means an unexpected error.
1398          */
1399         r.start = 0;
1400         r.len = -1;
1401         r.minlen = has_minimum ? minimum : 0;
1402         ret = ioctl(fd, FITRIM, &r);
1403         if (ret == -1) {
1404             result->has_error = true;
1405             if (errno == ENOTTY || errno == EOPNOTSUPP) {
1406                 result->error = g_strdup("trim not supported");
1407             } else {
1408                 result->error = g_strdup_printf("failed to trim: %s",
1409                                                 strerror(errno));
1410             }
1411             close(fd);
1412             continue;
1413         }
1414
1415         result->has_minimum = true;
1416         result->minimum = r.minlen;
1417         result->has_trimmed = true;
1418         result->trimmed = r.len;
1419         close(fd);
1420     }
1421
1422     free_fs_mount_list(&mounts);
1423     return response;
1424 }
1425 #endif /* CONFIG_FSTRIM */
1426
1427
1428 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1429 #define SUSPEND_SUPPORTED 0
1430 #define SUSPEND_NOT_SUPPORTED 1
1431
1432 static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
1433                                const char *sysfile_str, Error **errp)
1434 {
1435     Error *local_err = NULL;
1436     char *pmutils_path;
1437     pid_t pid;
1438     int status;
1439
1440     pmutils_path = g_find_program_in_path(pmutils_bin);
1441
1442     pid = fork();
1443     if (!pid) {
1444         char buf[32]; /* hopefully big enough */
1445         ssize_t ret;
1446         int fd;
1447
1448         setsid();
1449         reopen_fd_to_null(0);
1450         reopen_fd_to_null(1);
1451         reopen_fd_to_null(2);
1452
1453         if (pmutils_path) {
1454             execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
1455         }
1456
1457         /*
1458          * If we get here either pm-utils is not installed or execle() has
1459          * failed. Let's try the manual method if the caller wants it.
1460          */
1461
1462         if (!sysfile_str) {
1463             _exit(SUSPEND_NOT_SUPPORTED);
1464         }
1465
1466         fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1467         if (fd < 0) {
1468             _exit(SUSPEND_NOT_SUPPORTED);
1469         }
1470
1471         ret = read(fd, buf, sizeof(buf)-1);
1472         if (ret <= 0) {
1473             _exit(SUSPEND_NOT_SUPPORTED);
1474         }
1475         buf[ret] = '\0';
1476
1477         if (strstr(buf, sysfile_str)) {
1478             _exit(SUSPEND_SUPPORTED);
1479         }
1480
1481         _exit(SUSPEND_NOT_SUPPORTED);
1482     } else if (pid < 0) {
1483         error_setg_errno(errp, errno, "failed to create child process");
1484         goto out;
1485     }
1486
1487     ga_wait_child(pid, &status, &local_err);
1488     if (local_err) {
1489         error_propagate(errp, local_err);
1490         goto out;
1491     }
1492
1493     if (!WIFEXITED(status)) {
1494         error_setg(errp, "child process has terminated abnormally");
1495         goto out;
1496     }
1497
1498     switch (WEXITSTATUS(status)) {
1499     case SUSPEND_SUPPORTED:
1500         goto out;
1501     case SUSPEND_NOT_SUPPORTED:
1502         error_setg(errp,
1503                    "the requested suspend mode is not supported by the guest");
1504         goto out;
1505     default:
1506         error_setg(errp,
1507                    "the helper program '%s' returned an unexpected exit status"
1508                    " code (%d)", pmutils_path, WEXITSTATUS(status));
1509         goto out;
1510     }
1511
1512 out:
1513     g_free(pmutils_path);
1514 }
1515
1516 static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
1517                           Error **errp)
1518 {
1519     Error *local_err = NULL;
1520     char *pmutils_path;
1521     pid_t pid;
1522     int status;
1523
1524     pmutils_path = g_find_program_in_path(pmutils_bin);
1525
1526     pid = fork();
1527     if (pid == 0) {
1528         /* child */
1529         int fd;
1530
1531         setsid();
1532         reopen_fd_to_null(0);
1533         reopen_fd_to_null(1);
1534         reopen_fd_to_null(2);
1535
1536         if (pmutils_path) {
1537             execle(pmutils_path, pmutils_bin, NULL, environ);
1538         }
1539
1540         /*
1541          * If we get here either pm-utils is not installed or execle() has
1542          * failed. Let's try the manual method if the caller wants it.
1543          */
1544
1545         if (!sysfile_str) {
1546             _exit(EXIT_FAILURE);
1547         }
1548
1549         fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1550         if (fd < 0) {
1551             _exit(EXIT_FAILURE);
1552         }
1553
1554         if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1555             _exit(EXIT_FAILURE);
1556         }
1557
1558         _exit(EXIT_SUCCESS);
1559     } else if (pid < 0) {
1560         error_setg_errno(errp, errno, "failed to create child process");
1561         goto out;
1562     }
1563
1564     ga_wait_child(pid, &status, &local_err);
1565     if (local_err) {
1566         error_propagate(errp, local_err);
1567         goto out;
1568     }
1569
1570     if (!WIFEXITED(status)) {
1571         error_setg(errp, "child process has terminated abnormally");
1572         goto out;
1573     }
1574
1575     if (WEXITSTATUS(status)) {
1576         error_setg(errp, "child process has failed to suspend");
1577         goto out;
1578     }
1579
1580 out:
1581     g_free(pmutils_path);
1582 }
1583
1584 void qmp_guest_suspend_disk(Error **errp)
1585 {
1586     Error *local_err = NULL;
1587
1588     bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err);
1589     if (local_err) {
1590         error_propagate(errp, local_err);
1591         return;
1592     }
1593
1594     guest_suspend("pm-hibernate", "disk", errp);
1595 }
1596
1597 void qmp_guest_suspend_ram(Error **errp)
1598 {
1599     Error *local_err = NULL;
1600
1601     bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err);
1602     if (local_err) {
1603         error_propagate(errp, local_err);
1604         return;
1605     }
1606
1607     guest_suspend("pm-suspend", "mem", errp);
1608 }
1609
1610 void qmp_guest_suspend_hybrid(Error **errp)
1611 {
1612     Error *local_err = NULL;
1613
1614     bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL,
1615                        &local_err);
1616     if (local_err) {
1617         error_propagate(errp, local_err);
1618         return;
1619     }
1620
1621     guest_suspend("pm-suspend-hybrid", NULL, errp);
1622 }
1623
1624 static GuestNetworkInterfaceList *
1625 guest_find_interface(GuestNetworkInterfaceList *head,
1626                      const char *name)
1627 {
1628     for (; head; head = head->next) {
1629         if (strcmp(head->value->name, name) == 0) {
1630             break;
1631         }
1632     }
1633
1634     return head;
1635 }
1636
1637 /*
1638  * Build information about guest interfaces
1639  */
1640 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1641 {
1642     GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1643     struct ifaddrs *ifap, *ifa;
1644
1645     if (getifaddrs(&ifap) < 0) {
1646         error_setg_errno(errp, errno, "getifaddrs failed");
1647         goto error;
1648     }
1649
1650     for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1651         GuestNetworkInterfaceList *info;
1652         GuestIpAddressList **address_list = NULL, *address_item = NULL;
1653         char addr4[INET_ADDRSTRLEN];
1654         char addr6[INET6_ADDRSTRLEN];
1655         int sock;
1656         struct ifreq ifr;
1657         unsigned char *mac_addr;
1658         void *p;
1659
1660         g_debug("Processing %s interface", ifa->ifa_name);
1661
1662         info = guest_find_interface(head, ifa->ifa_name);
1663
1664         if (!info) {
1665             info = g_malloc0(sizeof(*info));
1666             info->value = g_malloc0(sizeof(*info->value));
1667             info->value->name = g_strdup(ifa->ifa_name);
1668
1669             if (!cur_item) {
1670                 head = cur_item = info;
1671             } else {
1672                 cur_item->next = info;
1673                 cur_item = info;
1674             }
1675         }
1676
1677         if (!info->value->has_hardware_address &&
1678             ifa->ifa_flags & SIOCGIFHWADDR) {
1679             /* we haven't obtained HW address yet */
1680             sock = socket(PF_INET, SOCK_STREAM, 0);
1681             if (sock == -1) {
1682                 error_setg_errno(errp, errno, "failed to create socket");
1683                 goto error;
1684             }
1685
1686             memset(&ifr, 0, sizeof(ifr));
1687             pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
1688             if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
1689                 error_setg_errno(errp, errno,
1690                                  "failed to get MAC address of %s",
1691                                  ifa->ifa_name);
1692                 close(sock);
1693                 goto error;
1694             }
1695
1696             close(sock);
1697             mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1698
1699             info->value->hardware_address =
1700                 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1701                                 (int) mac_addr[0], (int) mac_addr[1],
1702                                 (int) mac_addr[2], (int) mac_addr[3],
1703                                 (int) mac_addr[4], (int) mac_addr[5]);
1704
1705             info->value->has_hardware_address = true;
1706         }
1707
1708         if (ifa->ifa_addr &&
1709             ifa->ifa_addr->sa_family == AF_INET) {
1710             /* interface with IPv4 address */
1711             p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1712             if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
1713                 error_setg_errno(errp, errno, "inet_ntop failed");
1714                 goto error;
1715             }
1716
1717             address_item = g_malloc0(sizeof(*address_item));
1718             address_item->value = g_malloc0(sizeof(*address_item->value));
1719             address_item->value->ip_address = g_strdup(addr4);
1720             address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1721
1722             if (ifa->ifa_netmask) {
1723                 /* Count the number of set bits in netmask.
1724                  * This is safe as '1' and '0' cannot be shuffled in netmask. */
1725                 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1726                 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1727             }
1728         } else if (ifa->ifa_addr &&
1729                    ifa->ifa_addr->sa_family == AF_INET6) {
1730             /* interface with IPv6 address */
1731             p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1732             if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
1733                 error_setg_errno(errp, errno, "inet_ntop failed");
1734                 goto error;
1735             }
1736
1737             address_item = g_malloc0(sizeof(*address_item));
1738             address_item->value = g_malloc0(sizeof(*address_item->value));
1739             address_item->value->ip_address = g_strdup(addr6);
1740             address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1741
1742             if (ifa->ifa_netmask) {
1743                 /* Count the number of set bits in netmask.
1744                  * This is safe as '1' and '0' cannot be shuffled in netmask. */
1745                 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1746                 address_item->value->prefix =
1747                     ctpop32(((uint32_t *) p)[0]) +
1748                     ctpop32(((uint32_t *) p)[1]) +
1749                     ctpop32(((uint32_t *) p)[2]) +
1750                     ctpop32(((uint32_t *) p)[3]);
1751             }
1752         }
1753
1754         if (!address_item) {
1755             continue;
1756         }
1757
1758         address_list = &info->value->ip_addresses;
1759
1760         while (*address_list && (*address_list)->next) {
1761             address_list = &(*address_list)->next;
1762         }
1763
1764         if (!*address_list) {
1765             *address_list = address_item;
1766         } else {
1767             (*address_list)->next = address_item;
1768         }
1769
1770         info->value->has_ip_addresses = true;
1771
1772
1773     }
1774
1775     freeifaddrs(ifap);
1776     return head;
1777
1778 error:
1779     freeifaddrs(ifap);
1780     qapi_free_GuestNetworkInterfaceList(head);
1781     return NULL;
1782 }
1783
1784 #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
1785
1786 static long sysconf_exact(int name, const char *name_str, Error **errp)
1787 {
1788     long ret;
1789
1790     errno = 0;
1791     ret = sysconf(name);
1792     if (ret == -1) {
1793         if (errno == 0) {
1794             error_setg(errp, "sysconf(%s): value indefinite", name_str);
1795         } else {
1796             error_setg_errno(errp, errno, "sysconf(%s)", name_str);
1797         }
1798     }
1799     return ret;
1800 }
1801
1802 /* Transfer online/offline status between @vcpu and the guest system.
1803  *
1804  * On input either @errp or *@errp must be NULL.
1805  *
1806  * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1807  * - R: vcpu->logical_id
1808  * - W: vcpu->online
1809  * - W: vcpu->can_offline
1810  *
1811  * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1812  * - R: vcpu->logical_id
1813  * - R: vcpu->online
1814  *
1815  * Written members remain unmodified on error.
1816  */
1817 static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1818                           Error **errp)
1819 {
1820     char *dirpath;
1821     int dirfd;
1822
1823     dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1824                               vcpu->logical_id);
1825     dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1826     if (dirfd == -1) {
1827         error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1828     } else {
1829         static const char fn[] = "online";
1830         int fd;
1831         int res;
1832
1833         fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1834         if (fd == -1) {
1835             if (errno != ENOENT) {
1836                 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1837             } else if (sys2vcpu) {
1838                 vcpu->online = true;
1839                 vcpu->can_offline = false;
1840             } else if (!vcpu->online) {
1841                 error_setg(errp, "logical processor #%" PRId64 " can't be "
1842                            "offlined", vcpu->logical_id);
1843             } /* otherwise pretend successful re-onlining */
1844         } else {
1845             unsigned char status;
1846
1847             res = pread(fd, &status, 1, 0);
1848             if (res == -1) {
1849                 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1850             } else if (res == 0) {
1851                 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1852                            fn);
1853             } else if (sys2vcpu) {
1854                 vcpu->online = (status != '0');
1855                 vcpu->can_offline = true;
1856             } else if (vcpu->online != (status != '0')) {
1857                 status = '0' + vcpu->online;
1858                 if (pwrite(fd, &status, 1, 0) == -1) {
1859                     error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1860                                      fn);
1861                 }
1862             } /* otherwise pretend successful re-(on|off)-lining */
1863
1864             res = close(fd);
1865             g_assert(res == 0);
1866         }
1867
1868         res = close(dirfd);
1869         g_assert(res == 0);
1870     }
1871
1872     g_free(dirpath);
1873 }
1874
1875 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1876 {
1877     int64_t current;
1878     GuestLogicalProcessorList *head, **link;
1879     long sc_max;
1880     Error *local_err = NULL;
1881
1882     current = 0;
1883     head = NULL;
1884     link = &head;
1885     sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1886
1887     while (local_err == NULL && current < sc_max) {
1888         GuestLogicalProcessor *vcpu;
1889         GuestLogicalProcessorList *entry;
1890
1891         vcpu = g_malloc0(sizeof *vcpu);
1892         vcpu->logical_id = current++;
1893         vcpu->has_can_offline = true; /* lolspeak ftw */
1894         transfer_vcpu(vcpu, true, &local_err);
1895
1896         entry = g_malloc0(sizeof *entry);
1897         entry->value = vcpu;
1898
1899         *link = entry;
1900         link = &entry->next;
1901     }
1902
1903     if (local_err == NULL) {
1904         /* there's no guest with zero VCPUs */
1905         g_assert(head != NULL);
1906         return head;
1907     }
1908
1909     qapi_free_GuestLogicalProcessorList(head);
1910     error_propagate(errp, local_err);
1911     return NULL;
1912 }
1913
1914 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1915 {
1916     int64_t processed;
1917     Error *local_err = NULL;
1918
1919     processed = 0;
1920     while (vcpus != NULL) {
1921         transfer_vcpu(vcpus->value, false, &local_err);
1922         if (local_err != NULL) {
1923             break;
1924         }
1925         ++processed;
1926         vcpus = vcpus->next;
1927     }
1928
1929     if (local_err != NULL) {
1930         if (processed == 0) {
1931             error_propagate(errp, local_err);
1932         } else {
1933             error_free(local_err);
1934         }
1935     }
1936
1937     return processed;
1938 }
1939
1940 void qmp_guest_set_user_password(const char *username,
1941                                  const char *password,
1942                                  bool crypted,
1943                                  Error **errp)
1944 {
1945     Error *local_err = NULL;
1946     char *passwd_path = NULL;
1947     pid_t pid;
1948     int status;
1949     int datafd[2] = { -1, -1 };
1950     char *rawpasswddata = NULL;
1951     size_t rawpasswdlen;
1952     char *chpasswddata = NULL;
1953     size_t chpasswdlen;
1954
1955     rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
1956     if (!rawpasswddata) {
1957         return;
1958     }
1959     rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
1960     rawpasswddata[rawpasswdlen] = '\0';
1961
1962     if (strchr(rawpasswddata, '\n')) {
1963         error_setg(errp, "forbidden characters in raw password");
1964         goto out;
1965     }
1966
1967     if (strchr(username, '\n') ||
1968         strchr(username, ':')) {
1969         error_setg(errp, "forbidden characters in username");
1970         goto out;
1971     }
1972
1973     chpasswddata = g_strdup_printf("%s:%s\n", username, rawpasswddata);
1974     chpasswdlen = strlen(chpasswddata);
1975
1976     passwd_path = g_find_program_in_path("chpasswd");
1977
1978     if (!passwd_path) {
1979         error_setg(errp, "cannot find 'passwd' program in PATH");
1980         goto out;
1981     }
1982
1983     if (pipe(datafd) < 0) {
1984         error_setg(errp, "cannot create pipe FDs");
1985         goto out;
1986     }
1987
1988     pid = fork();
1989     if (pid == 0) {
1990         close(datafd[1]);
1991         /* child */
1992         setsid();
1993         dup2(datafd[0], 0);
1994         reopen_fd_to_null(1);
1995         reopen_fd_to_null(2);
1996
1997         if (crypted) {
1998             execle(passwd_path, "chpasswd", "-e", NULL, environ);
1999         } else {
2000             execle(passwd_path, "chpasswd", NULL, environ);
2001         }
2002         _exit(EXIT_FAILURE);
2003     } else if (pid < 0) {
2004         error_setg_errno(errp, errno, "failed to create child process");
2005         goto out;
2006     }
2007     close(datafd[0]);
2008     datafd[0] = -1;
2009
2010     if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) != chpasswdlen) {
2011         error_setg_errno(errp, errno, "cannot write new account password");
2012         goto out;
2013     }
2014     close(datafd[1]);
2015     datafd[1] = -1;
2016
2017     ga_wait_child(pid, &status, &local_err);
2018     if (local_err) {
2019         error_propagate(errp, local_err);
2020         goto out;
2021     }
2022
2023     if (!WIFEXITED(status)) {
2024         error_setg(errp, "child process has terminated abnormally");
2025         goto out;
2026     }
2027
2028     if (WEXITSTATUS(status)) {
2029         error_setg(errp, "child process has failed to set user password");
2030         goto out;
2031     }
2032
2033 out:
2034     g_free(chpasswddata);
2035     g_free(rawpasswddata);
2036     g_free(passwd_path);
2037     if (datafd[0] != -1) {
2038         close(datafd[0]);
2039     }
2040     if (datafd[1] != -1) {
2041         close(datafd[1]);
2042     }
2043 }
2044
2045 static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf,
2046                                int size, Error **errp)
2047 {
2048     int fd;
2049     int res;
2050
2051     errno = 0;
2052     fd = openat(dirfd, pathname, O_RDONLY);
2053     if (fd == -1) {
2054         error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2055         return;
2056     }
2057
2058     res = pread(fd, buf, size, 0);
2059     if (res == -1) {
2060         error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname);
2061     } else if (res == 0) {
2062         error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname);
2063     }
2064     close(fd);
2065 }
2066
2067 static void ga_write_sysfs_file(int dirfd, const char *pathname,
2068                                 const char *buf, int size, Error **errp)
2069 {
2070     int fd;
2071
2072     errno = 0;
2073     fd = openat(dirfd, pathname, O_WRONLY);
2074     if (fd == -1) {
2075         error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2076         return;
2077     }
2078
2079     if (pwrite(fd, buf, size, 0) == -1) {
2080         error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname);
2081     }
2082
2083     close(fd);
2084 }
2085
2086 /* Transfer online/offline status between @mem_blk and the guest system.
2087  *
2088  * On input either @errp or *@errp must be NULL.
2089  *
2090  * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2091  * - R: mem_blk->phys_index
2092  * - W: mem_blk->online
2093  * - W: mem_blk->can_offline
2094  *
2095  * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2096  * - R: mem_blk->phys_index
2097  * - R: mem_blk->online
2098  *-  R: mem_blk->can_offline
2099  * Written members remain unmodified on error.
2100  */
2101 static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
2102                                   GuestMemoryBlockResponse *result,
2103                                   Error **errp)
2104 {
2105     char *dirpath;
2106     int dirfd;
2107     char *status;
2108     Error *local_err = NULL;
2109
2110     if (!sys2memblk) {
2111         DIR *dp;
2112
2113         if (!result) {
2114             error_setg(errp, "Internal error, 'result' should not be NULL");
2115             return;
2116         }
2117         errno = 0;
2118         dp = opendir("/sys/devices/system/memory/");
2119          /* if there is no 'memory' directory in sysfs,
2120          * we think this VM does not support online/offline memory block,
2121          * any other solution?
2122          */
2123         if (!dp && errno == ENOENT) {
2124             result->response =
2125                 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2126             goto out1;
2127         }
2128         closedir(dp);
2129     }
2130
2131     dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/",
2132                               mem_blk->phys_index);
2133     dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2134     if (dirfd == -1) {
2135         if (sys2memblk) {
2136             error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2137         } else {
2138             if (errno == ENOENT) {
2139                 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND;
2140             } else {
2141                 result->response =
2142                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2143             }
2144         }
2145         g_free(dirpath);
2146         goto out1;
2147     }
2148     g_free(dirpath);
2149
2150     status = g_malloc0(10);
2151     ga_read_sysfs_file(dirfd, "state", status, 10, &local_err);
2152     if (local_err) {
2153         /* treat with sysfs file that not exist in old kernel */
2154         if (errno == ENOENT) {
2155             error_free(local_err);
2156             if (sys2memblk) {
2157                 mem_blk->online = true;
2158                 mem_blk->can_offline = false;
2159             } else if (!mem_blk->online) {
2160                 result->response =
2161                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2162             }
2163         } else {
2164             if (sys2memblk) {
2165                 error_propagate(errp, local_err);
2166             } else {
2167                 result->response =
2168                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2169             }
2170         }
2171         goto out2;
2172     }
2173
2174     if (sys2memblk) {
2175         char removable = '0';
2176
2177         mem_blk->online = (strncmp(status, "online", 6) == 0);
2178
2179         ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err);
2180         if (local_err) {
2181             /* if no 'removable' file, it doesn't support offline mem blk */
2182             if (errno == ENOENT) {
2183                 error_free(local_err);
2184                 mem_blk->can_offline = false;
2185             } else {
2186                 error_propagate(errp, local_err);
2187             }
2188         } else {
2189             mem_blk->can_offline = (removable != '0');
2190         }
2191     } else {
2192         if (mem_blk->online != (strncmp(status, "online", 6) == 0)) {
2193             char *new_state = mem_blk->online ? g_strdup("online") :
2194                                                 g_strdup("offline");
2195
2196             ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state),
2197                                 &local_err);
2198             g_free(new_state);
2199             if (local_err) {
2200                 error_free(local_err);
2201                 result->response =
2202                     GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2203                 goto out2;
2204             }
2205
2206             result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS;
2207             result->has_error_code = false;
2208         } /* otherwise pretend successful re-(on|off)-lining */
2209     }
2210     g_free(status);
2211     close(dirfd);
2212     return;
2213
2214 out2:
2215     g_free(status);
2216     close(dirfd);
2217 out1:
2218     if (!sys2memblk) {
2219         result->has_error_code = true;
2220         result->error_code = errno;
2221     }
2222 }
2223
2224 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2225 {
2226     GuestMemoryBlockList *head, **link;
2227     Error *local_err = NULL;
2228     struct dirent *de;
2229     DIR *dp;
2230
2231     head = NULL;
2232     link = &head;
2233
2234     dp = opendir("/sys/devices/system/memory/");
2235     if (!dp) {
2236         /* it's ok if this happens to be a system that doesn't expose
2237          * memory blocks via sysfs, but otherwise we should report
2238          * an error
2239          */
2240         if (errno != ENOENT) {
2241             error_setg_errno(errp, errno, "Can't open directory"
2242                              "\"/sys/devices/system/memory/\"");
2243         }
2244         return NULL;
2245     }
2246
2247     /* Note: the phys_index of memory block may be discontinuous,
2248      * this is because a memblk is the unit of the Sparse Memory design, which
2249      * allows discontinuous memory ranges (ex. NUMA), so here we should
2250      * traverse the memory block directory.
2251      */
2252     while ((de = readdir(dp)) != NULL) {
2253         GuestMemoryBlock *mem_blk;
2254         GuestMemoryBlockList *entry;
2255
2256         if ((strncmp(de->d_name, "memory", 6) != 0) ||
2257             !(de->d_type & DT_DIR)) {
2258             continue;
2259         }
2260
2261         mem_blk = g_malloc0(sizeof *mem_blk);
2262         /* The d_name is "memoryXXX",  phys_index is block id, same as XXX */
2263         mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10);
2264         mem_blk->has_can_offline = true; /* lolspeak ftw */
2265         transfer_memory_block(mem_blk, true, NULL, &local_err);
2266
2267         entry = g_malloc0(sizeof *entry);
2268         entry->value = mem_blk;
2269
2270         *link = entry;
2271         link = &entry->next;
2272     }
2273
2274     closedir(dp);
2275     if (local_err == NULL) {
2276         /* there's no guest with zero memory blocks */
2277         if (head == NULL) {
2278             error_setg(errp, "guest reported zero memory blocks!");
2279         }
2280         return head;
2281     }
2282
2283     qapi_free_GuestMemoryBlockList(head);
2284     error_propagate(errp, local_err);
2285     return NULL;
2286 }
2287
2288 GuestMemoryBlockResponseList *
2289 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2290 {
2291     GuestMemoryBlockResponseList *head, **link;
2292     Error *local_err = NULL;
2293
2294     head = NULL;
2295     link = &head;
2296
2297     while (mem_blks != NULL) {
2298         GuestMemoryBlockResponse *result;
2299         GuestMemoryBlockResponseList *entry;
2300         GuestMemoryBlock *current_mem_blk = mem_blks->value;
2301
2302         result = g_malloc0(sizeof(*result));
2303         result->phys_index = current_mem_blk->phys_index;
2304         transfer_memory_block(current_mem_blk, false, result, &local_err);
2305         if (local_err) { /* should never happen */
2306             goto err;
2307         }
2308         entry = g_malloc0(sizeof *entry);
2309         entry->value = result;
2310
2311         *link = entry;
2312         link = &entry->next;
2313         mem_blks = mem_blks->next;
2314     }
2315
2316     return head;
2317 err:
2318     qapi_free_GuestMemoryBlockResponseList(head);
2319     error_propagate(errp, local_err);
2320     return NULL;
2321 }
2322
2323 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2324 {
2325     Error *local_err = NULL;
2326     char *dirpath;
2327     int dirfd;
2328     char *buf;
2329     GuestMemoryBlockInfo *info;
2330
2331     dirpath = g_strdup_printf("/sys/devices/system/memory/");
2332     dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2333     if (dirfd == -1) {
2334         error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2335         g_free(dirpath);
2336         return NULL;
2337     }
2338     g_free(dirpath);
2339
2340     buf = g_malloc0(20);
2341     ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err);
2342     close(dirfd);
2343     if (local_err) {
2344         g_free(buf);
2345         error_propagate(errp, local_err);
2346         return NULL;
2347     }
2348
2349     info = g_new0(GuestMemoryBlockInfo, 1);
2350     info->size = strtol(buf, NULL, 16); /* the unit is bytes */
2351
2352     g_free(buf);
2353
2354     return info;
2355 }
2356
2357 #else /* defined(__linux__) */
2358
2359 void qmp_guest_suspend_disk(Error **errp)
2360 {
2361     error_setg(errp, QERR_UNSUPPORTED);
2362 }
2363
2364 void qmp_guest_suspend_ram(Error **errp)
2365 {
2366     error_setg(errp, QERR_UNSUPPORTED);
2367 }
2368
2369 void qmp_guest_suspend_hybrid(Error **errp)
2370 {
2371     error_setg(errp, QERR_UNSUPPORTED);
2372 }
2373
2374 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
2375 {
2376     error_setg(errp, QERR_UNSUPPORTED);
2377     return NULL;
2378 }
2379
2380 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2381 {
2382     error_setg(errp, QERR_UNSUPPORTED);
2383     return NULL;
2384 }
2385
2386 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2387 {
2388     error_setg(errp, QERR_UNSUPPORTED);
2389     return -1;
2390 }
2391
2392 void qmp_guest_set_user_password(const char *username,
2393                                  const char *password,
2394                                  bool crypted,
2395                                  Error **errp)
2396 {
2397     error_setg(errp, QERR_UNSUPPORTED);
2398 }
2399
2400 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2401 {
2402     error_setg(errp, QERR_UNSUPPORTED);
2403     return NULL;
2404 }
2405
2406 GuestMemoryBlockResponseList *
2407 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2408 {
2409     error_setg(errp, QERR_UNSUPPORTED);
2410     return NULL;
2411 }
2412
2413 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2414 {
2415     error_setg(errp, QERR_UNSUPPORTED);
2416     return NULL;
2417 }
2418
2419 #endif
2420
2421 #if !defined(CONFIG_FSFREEZE)
2422
2423 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
2424 {
2425     error_setg(errp, QERR_UNSUPPORTED);
2426     return NULL;
2427 }
2428
2429 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
2430 {
2431     error_setg(errp, QERR_UNSUPPORTED);
2432
2433     return 0;
2434 }
2435
2436 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
2437 {
2438     error_setg(errp, QERR_UNSUPPORTED);
2439
2440     return 0;
2441 }
2442
2443 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
2444                                        strList *mountpoints,
2445                                        Error **errp)
2446 {
2447     error_setg(errp, QERR_UNSUPPORTED);
2448
2449     return 0;
2450 }
2451
2452 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
2453 {
2454     error_setg(errp, QERR_UNSUPPORTED);
2455
2456     return 0;
2457 }
2458 #endif /* CONFIG_FSFREEZE */
2459
2460 #if !defined(CONFIG_FSTRIM)
2461 GuestFilesystemTrimResponse *
2462 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
2463 {
2464     error_setg(errp, QERR_UNSUPPORTED);
2465     return NULL;
2466 }
2467 #endif
2468
2469 /* add unsupported commands to the blacklist */
2470 GList *ga_command_blacklist_init(GList *blacklist)
2471 {
2472 #if !defined(__linux__)
2473     {
2474         const char *list[] = {
2475             "guest-suspend-disk", "guest-suspend-ram",
2476             "guest-suspend-hybrid", "guest-network-get-interfaces",
2477             "guest-get-vcpus", "guest-set-vcpus",
2478             "guest-get-memory-blocks", "guest-set-memory-blocks",
2479             "guest-get-memory-block-size", NULL};
2480         char **p = (char **)list;
2481
2482         while (*p) {
2483             blacklist = g_list_append(blacklist, g_strdup(*p++));
2484         }
2485     }
2486 #endif
2487
2488 #if !defined(CONFIG_FSFREEZE)
2489     {
2490         const char *list[] = {
2491             "guest-get-fsinfo", "guest-fsfreeze-status",
2492             "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
2493             "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL};
2494         char **p = (char **)list;
2495
2496         while (*p) {
2497             blacklist = g_list_append(blacklist, g_strdup(*p++));
2498         }
2499     }
2500 #endif
2501
2502 #if !defined(CONFIG_FSTRIM)
2503     blacklist = g_list_append(blacklist, g_strdup("guest-fstrim"));
2504 #endif
2505
2506     return blacklist;
2507 }
2508
2509 /* register init/cleanup routines for stateful command groups */
2510 void ga_command_state_init(GAState *s, GACommandState *cs)
2511 {
2512 #if defined(CONFIG_FSFREEZE)
2513     ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
2514 #endif
2515 }