2 * QEMU Guest Agent POSIX-specific command implementations
4 * Copyright IBM Corp. 2011
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 * Michal Privoznik <mprivozn@redhat.com>
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.
14 #include "qemu/osdep.h"
15 #include <sys/ioctl.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"
27 #ifndef CONFIG_HAS_ENVIRON
29 #include <crt_externs.h>
30 #define environ (*_NSGetEnviron())
32 extern char **environ;
36 #if defined(__linux__)
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
43 #include <sys/sysmacros.h>
46 #define CONFIG_FSFREEZE
53 static void ga_wait_child(pid_t pid, int *status, Error **errp)
60 rpid = waitpid(pid, status, 0);
61 } while (rpid == -1 && errno == EINTR);
64 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)",
69 g_assert(rpid == pid);
72 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
74 const char *shutdown_flag;
75 Error *local_err = NULL;
79 slog("guest-shutdown called, mode: %s", mode);
80 if (!has_mode || strcmp(mode, "powerdown") == 0) {
82 } else if (strcmp(mode, "halt") == 0) {
84 } else if (strcmp(mode, "reboot") == 0) {
88 "mode is invalid (valid values are: halt|powerdown|reboot");
94 /* child, start the shutdown */
100 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
101 "hypervisor initiated shutdown", (char*)NULL, environ);
103 } else if (pid < 0) {
104 error_setg_errno(errp, errno, "failed to create child process");
108 ga_wait_child(pid, &status, &local_err);
110 error_propagate(errp, local_err);
114 if (!WIFEXITED(status)) {
115 error_setg(errp, "child process has terminated abnormally");
119 if (WEXITSTATUS(status)) {
120 error_setg(errp, "child process has failed to shutdown");
127 int64_t qmp_guest_get_time(Error **errp)
132 ret = qemu_gettimeofday(&tq);
134 error_setg_errno(errp, errno, "Failed to get time");
138 return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
141 void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
146 Error *local_err = NULL;
149 /* If user has passed a time, validate and set it. */
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);
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");
167 ret = settimeofday(&tv, NULL);
169 error_setg_errno(errp, errno, "Failed to set time to guest");
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). */
181 reopen_fd_to_null(0);
182 reopen_fd_to_null(1);
183 reopen_fd_to_null(2);
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",
190 } else if (pid < 0) {
191 error_setg_errno(errp, errno, "failed to create child process");
195 ga_wait_child(pid, &status, &local_err);
197 error_propagate(errp, local_err);
201 if (!WIFEXITED(status)) {
202 error_setg(errp, "child process has terminated abnormally");
206 if (WEXITSTATUS(status)) {
207 error_setg(errp, "hwclock failed to set hardware clock to system time");
218 typedef struct GuestFileHandle {
222 QTAILQ_ENTRY(GuestFileHandle) next;
226 QTAILQ_HEAD(, GuestFileHandle) filehandles;
227 } guest_file_state = {
228 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles),
231 static int64_t guest_file_handle_add(FILE *fh, Error **errp)
233 GuestFileHandle *gfh;
236 handle = ga_get_fd_handle(ga_state, errp);
241 gfh = g_new0(GuestFileHandle, 1);
244 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
249 static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
251 GuestFileHandle *gfh;
253 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
260 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
264 typedef const char * const ccpc;
270 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
271 static const struct {
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 }
290 find_open_flag(const char *mode_str, Error **errp)
294 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
297 form = guest_file_open_modes[mode].forms;
298 while (*form != NULL && strcmp(*form, mode_str) != 0) {
306 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
307 error_setg(errp, "invalid file open mode '%s'", mode_str);
310 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
313 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
314 S_IRGRP | S_IWGRP | \
318 safe_open_or_create(const char *path, const char *mode, Error **errp)
320 Error *local_err = NULL;
323 oflag = find_open_flag(mode, &local_err);
324 if (local_err == NULL) {
327 /* If the caller wants / allows creation of a new file, we implement it
328 * with a two step process: open() + (open() / fchmod()).
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.)
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.
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.
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.
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);
356 error_setg_errno(&local_err, errno, "failed to open file '%s' "
357 "(mode: '%s')", path, mode);
359 qemu_set_cloexec(fd);
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);
368 f = fdopen(fd, mode);
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);
379 if (oflag & O_CREAT) {
385 error_propagate(errp, local_err);
389 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
393 Error *local_err = NULL;
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);
406 /* set fd non-blocking to avoid common use cases (like reading from a
407 * named pipe) from hanging the agent
409 qemu_set_nonblock(fileno(fh));
411 handle = guest_file_handle_add(fh, errp);
417 slog("guest-file-open, handle: %" PRId64, handle);
421 void qmp_guest_file_close(int64_t handle, Error **errp)
423 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
426 slog("guest-file-close called, handle: %" PRId64, handle);
431 ret = fclose(gfh->fh);
433 error_setg_errno(errp, errno, "failed to close handle");
437 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
441 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
442 int64_t count, Error **errp)
444 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
445 GuestFileRead *read_data = NULL;
455 count = QGA_READ_COUNT_DEFAULT;
456 } else if (count < 0) {
457 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
464 /* explicitly flush when switching from writing to reading */
465 if (gfh->state == RW_STATE_WRITING) {
466 int ret = fflush(fh);
468 error_setg_errno(errp, errno, "failed to flush file");
471 gfh->state = RW_STATE_NEW;
474 buf = g_malloc0(count+1);
475 read_count = fread(buf, 1, count, fh);
477 error_setg_errno(errp, errno, "failed to read file");
478 slog("guest-file-read failed, handle: %" PRId64, handle);
481 read_data = g_new0(GuestFileRead, 1);
482 read_data->count = read_count;
483 read_data->eof = feof(fh);
485 read_data->buf_b64 = g_base64_encode(buf, read_count);
487 gfh->state = RW_STATE_READING;
495 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
496 bool has_count, int64_t count,
499 GuestFileWrite *write_data = NULL;
503 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
512 if (gfh->state == RW_STATE_READING) {
513 int ret = fseek(fh, 0, SEEK_CUR);
515 error_setg_errno(errp, errno, "failed to seek file");
518 gfh->state = RW_STATE_NEW;
521 buf = qbase64_decode(buf_b64, -1, &buf_len, errp);
528 } else if (count < 0 || count > buf_len) {
529 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
535 write_count = fwrite(buf, 1, count, fh);
537 error_setg_errno(errp, errno, "failed to write to file");
538 slog("guest-file-write failed, handle: %" PRId64, handle);
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;
551 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
552 GuestFileWhence *whence_code,
555 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
556 GuestFileSeek *seek_data = NULL;
566 /* We stupidly exposed 'whence':'int' in our qapi */
567 whence = ga_parse_whence(whence_code, &err);
569 error_propagate(errp, err);
574 ret = fseek(fh, offset, whence);
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;
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;
592 void qmp_guest_file_flush(int64_t handle, Error **errp)
594 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
605 error_setg_errno(errp, errno, "failed to flush file");
607 gfh->state = RW_STATE_NEW;
611 /* linux-specific implementations. avoid this if at all possible. */
612 #if defined(__linux__)
614 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
615 typedef struct FsMount {
618 unsigned int devmajor, devminor;
619 QTAILQ_ENTRY(FsMount) next;
622 typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
624 static void free_fs_mount_list(FsMountList *mounts)
626 FsMount *mount, *temp;
632 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
633 QTAILQ_REMOVE(mounts, mount, next);
634 g_free(mount->dirname);
635 g_free(mount->devtype);
640 static int dev_major_minor(const char *devpath,
641 unsigned int *devmajor, unsigned int *devminor)
648 if (stat(devpath, &st) < 0) {
649 slog("failed to stat device file '%s': %s", devpath, strerror(errno));
652 if (S_ISDIR(st.st_mode)) {
653 /* It is bind mount */
656 if (S_ISBLK(st.st_mode)) {
657 *devmajor = major(st.st_rdev);
658 *devminor = minor(st.st_rdev);
665 * Walk the mount table and build a list of local file systems
667 static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
671 char const *mtab = "/proc/self/mounts";
673 unsigned int devmajor, devminor;
675 fp = setmntent(mtab, "r");
677 error_setg(errp, "failed to open mtab file: '%s'", mtab);
681 while ((ment = getmntent(fp))) {
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
688 if ((ment->mnt_fsname[0] != '/') ||
689 (strcmp(ment->mnt_type, "smbfs") == 0) ||
690 (strcmp(ment->mnt_type, "cifs") == 0)) {
693 if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) {
694 /* Skip bind mounts */
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;
704 QTAILQ_INSERT_TAIL(mounts, mount, next);
710 static void decode_mntname(char *name, int len)
713 for (i = 0; i <= len; i++) {
714 if (name[i] != '\\') {
716 } else if (name[i + 1] == '\\') {
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 +
732 static void build_fs_mount_list(FsMountList *mounts, Error **errp)
735 char const *mountinfo = "/proc/self/mountinfo";
737 char *line = NULL, *dash;
740 unsigned int devmajor, devminor;
741 int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e;
743 fp = fopen(mountinfo, "r");
745 build_fs_mount_list_from_mtab(mounts, errp);
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);
755 dash = strstr(line + dir_e, " - ");
759 ret = sscanf(dash, " - %n%*s%n %n%*s%n%c",
760 &type_s, &type_e, &dev_s, &dev_e, &check);
767 decode_mntname(line + dir_s, dir_e - dir_s);
768 decode_mntname(dash + dev_s, dev_e - dev_s);
770 /* btrfs reports major number = 0 */
771 if (strcmp("btrfs", dash + type_s) != 0 ||
772 dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) {
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;
783 QTAILQ_INSERT_TAIL(mounts, mount, next);
791 #if defined(CONFIG_FSFREEZE)
793 static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
801 path = g_strndup(syspath, pathlen);
802 dpath = g_strdup_printf("%s/driver", path);
803 len = readlink(dpath, buf, sizeof(buf) - 1);
806 driver = g_strdup(basename(buf));
813 static int compare_uint(const void *_a, const void *_b)
815 unsigned int a = *(unsigned int *)_a;
816 unsigned int b = *(unsigned int *)_b;
818 return a < b ? -1 : a > b ? 1 : 0;
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)
827 struct dirent *entry;
830 path = g_strndup(syspath, host - syspath);
833 error_setg_errno(errp, errno, "opendir(\"%s\")", path);
838 while (i < hosts_max) {
839 entry = readdir(dir);
843 if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) {
845 } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) {
850 qsort(hosts, i, sizeof(hosts[0]), compare_uint);
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,
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;
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);
877 driver = get_pci_driver(syspath, (p + 12 + pcilen) - syspath, errp);
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) {
888 p = strstr(syspath, "/ata");
893 p = strstr(syspath, "/host");
896 if (p && sscanf(q, "%u", &host) == 1) {
898 nhosts = build_hosts(syspath, p, has_ata, hosts,
899 sizeof(hosts) / sizeof(hosts[0]), errp);
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];
911 disk = g_malloc0(sizeof(*disk));
912 disk->pci_controller = pciaddr;
914 list = g_malloc0(sizeof(*list));
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);
923 for (i = 0; i < nhosts; i++) {
924 if (host == hosts[i]) {
925 disk->bus_type = GUEST_DISK_BUS_TYPE_IDE;
932 g_debug("no host for '%s' (driver '%s')", syspath, driver);
935 } else if (strcmp(driver, "sym53c8xx") == 0) {
936 /* scsi(LSI Logic): target*:0:<unit>:0 */
938 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
941 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
943 } else if (strcmp(driver, "virtio-pci") == 0) {
945 /* virtio-scsi: target*:0:0:<unit> */
946 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
949 /* virtio-blk: 1 disk per 1 device */
950 disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
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);
958 for (i = 0; i < nhosts; i++) {
959 if (host == hosts[i]) {
961 disk->bus_type = GUEST_DISK_BUS_TYPE_SATA;
966 g_debug("no host for '%s' (driver '%s')", syspath, driver);
970 g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath);
974 list->next = fs->disk;
981 qapi_free_GuestDiskAddressList(list);
986 static void build_guest_fsinfo_for_device(char const *devpath,
987 GuestFilesystemInfo *fs,
990 /* Store a list of slave devices of virtual volume specified by @syspath into
992 static void build_guest_fsinfo_for_virtual_device(char const *syspath,
993 GuestFilesystemInfo *fs,
998 struct dirent *entry;
1000 dirpath = g_strdup_printf("%s/slaves", syspath);
1001 dir = opendir(dirpath);
1003 error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
1010 entry = readdir(dir);
1011 if (entry == NULL) {
1013 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
1018 if (entry->d_type == DT_LNK) {
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);
1036 /* Dispatch to functions for virtual/real device */
1037 static void build_guest_fsinfo_for_device(char const *devpath,
1038 GuestFilesystemInfo *fs,
1041 char *syspath = realpath(devpath, NULL);
1044 error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
1049 fs->name = g_strdup(basename(syspath));
1052 g_debug(" parse sysfs path '%s'", syspath);
1054 if (strstr(syspath, "/devices/virtual/block/")) {
1055 build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
1057 build_guest_fsinfo_for_real_device(syspath, fs, errp);
1063 /* Return a list of the disk device(s)' info which @mount lies on */
1064 static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1067 GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
1068 char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1069 mount->devmajor, mount->devminor);
1071 fs->mountpoint = g_strdup(mount->dirname);
1072 fs->type = g_strdup(mount->devtype);
1073 build_guest_fsinfo_for_device(devpath, fs, errp);
1079 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1082 struct FsMount *mount;
1083 GuestFilesystemInfoList *new, *ret = NULL;
1084 Error *local_err = NULL;
1086 QTAILQ_INIT(&mounts);
1087 build_fs_mount_list(&mounts, &local_err);
1089 error_propagate(errp, local_err);
1093 QTAILQ_FOREACH(mount, &mounts, next) {
1094 g_debug("Building guest fsinfo for '%s'", mount->dirname);
1096 new = g_malloc0(sizeof(*ret));
1097 new->value = build_guest_fsinfo(mount, &local_err);
1101 error_propagate(errp, local_err);
1102 qapi_free_GuestFilesystemInfoList(ret);
1108 free_fs_mount_list(&mounts);
1114 FSFREEZE_HOOK_THAW = 0,
1115 FSFREEZE_HOOK_FREEZE,
1118 static const char *fsfreeze_hook_arg_string[] = {
1123 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
1128 const char *arg_str = fsfreeze_hook_arg_string[arg];
1129 Error *local_err = NULL;
1131 hook = ga_fsfreeze_hook(ga_state);
1135 if (access(hook, X_OK) != 0) {
1136 error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
1140 slog("executing fsfreeze hook with arg '%s'", arg_str);
1144 reopen_fd_to_null(0);
1145 reopen_fd_to_null(1);
1146 reopen_fd_to_null(2);
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");
1155 ga_wait_child(pid, &status, &local_err);
1157 error_propagate(errp, local_err);
1161 if (!WIFEXITED(status)) {
1162 error_setg(errp, "fsfreeze hook has terminated abnormally");
1166 status = WEXITSTATUS(status);
1168 error_setg(errp, "fsfreeze hook has failed with status %d", status);
1174 * Return status of freeze/thaw
1176 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
1178 if (ga_is_frozen(ga_state)) {
1179 return GUEST_FSFREEZE_STATUS_FROZEN;
1182 return GUEST_FSFREEZE_STATUS_THAWED;
1185 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1187 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
1191 * Walk list of mounted file systems in the guest, and freeze the ones which
1192 * are real local file systems.
1194 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1195 strList *mountpoints,
1201 struct FsMount *mount;
1202 Error *local_err = NULL;
1205 slog("guest-fsfreeze called");
1207 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
1209 error_propagate(errp, local_err);
1213 QTAILQ_INIT(&mounts);
1214 build_fs_mount_list(&mounts, &local_err);
1216 error_propagate(errp, local_err);
1220 /* cannot risk guest agent blocking itself on a write in this state */
1221 ga_set_frozen(ga_state);
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) {
1237 fd = qemu_open(mount->dirname, O_RDONLY);
1239 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
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.
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.
1252 ret = ioctl(fd, FIFREEZE);
1254 if (errno != EOPNOTSUPP) {
1255 error_setg_errno(errp, errno, "failed to freeze %s",
1266 free_fs_mount_list(&mounts);
1270 free_fs_mount_list(&mounts);
1271 qmp_guest_fsfreeze_thaw(NULL);
1276 * Walk list of frozen file systems in the guest, and thaw them.
1278 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
1283 int fd, i = 0, logged;
1284 Error *local_err = NULL;
1286 QTAILQ_INIT(&mounts);
1287 build_fs_mount_list(&mounts, &local_err);
1289 error_propagate(errp, local_err);
1293 QTAILQ_FOREACH(mount, &mounts, next) {
1295 fd = qemu_open(mount->dirname, O_RDONLY);
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
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).
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.
1316 ret = ioctl(fd, FITHAW);
1317 if (ret == 0 && !logged) {
1325 ga_unset_frozen(ga_state);
1326 free_fs_mount_list(&mounts);
1328 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
1333 static void guest_fsfreeze_cleanup(void)
1337 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
1338 qmp_guest_fsfreeze_thaw(&err);
1340 slog("failed to clean up frozen filesystems: %s",
1341 error_get_pretty(err));
1346 #endif /* CONFIG_FSFREEZE */
1348 #if defined(CONFIG_FSTRIM)
1350 * Walk list of mounted file systems in the guest, and trim them.
1352 GuestFilesystemTrimResponse *
1353 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
1355 GuestFilesystemTrimResponse *response;
1356 GuestFilesystemTrimResultList *list;
1357 GuestFilesystemTrimResult *result;
1360 struct FsMount *mount;
1362 Error *local_err = NULL;
1363 struct fstrim_range r;
1365 slog("guest-fstrim called");
1367 QTAILQ_INIT(&mounts);
1368 build_fs_mount_list(&mounts, &local_err);
1370 error_propagate(errp, local_err);
1374 response = g_malloc0(sizeof(*response));
1376 QTAILQ_FOREACH(mount, &mounts, next) {
1377 result = g_malloc0(sizeof(*result));
1378 result->path = g_strdup(mount->dirname);
1380 list = g_malloc0(sizeof(*list));
1381 list->value = result;
1382 list->next = response->paths;
1383 response->paths = list;
1385 fd = qemu_open(mount->dirname, O_RDONLY);
1387 result->error = g_strdup_printf("failed to open: %s",
1389 result->has_error = true;
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.
1401 r.minlen = has_minimum ? minimum : 0;
1402 ret = ioctl(fd, FITRIM, &r);
1404 result->has_error = true;
1405 if (errno == ENOTTY || errno == EOPNOTSUPP) {
1406 result->error = g_strdup("trim not supported");
1408 result->error = g_strdup_printf("failed to trim: %s",
1415 result->has_minimum = true;
1416 result->minimum = r.minlen;
1417 result->has_trimmed = true;
1418 result->trimmed = r.len;
1422 free_fs_mount_list(&mounts);
1425 #endif /* CONFIG_FSTRIM */
1428 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1429 #define SUSPEND_SUPPORTED 0
1430 #define SUSPEND_NOT_SUPPORTED 1
1432 static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
1433 const char *sysfile_str, Error **errp)
1435 Error *local_err = NULL;
1440 pmutils_path = g_find_program_in_path(pmutils_bin);
1444 char buf[32]; /* hopefully big enough */
1449 reopen_fd_to_null(0);
1450 reopen_fd_to_null(1);
1451 reopen_fd_to_null(2);
1454 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
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.
1463 _exit(SUSPEND_NOT_SUPPORTED);
1466 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1468 _exit(SUSPEND_NOT_SUPPORTED);
1471 ret = read(fd, buf, sizeof(buf)-1);
1473 _exit(SUSPEND_NOT_SUPPORTED);
1477 if (strstr(buf, sysfile_str)) {
1478 _exit(SUSPEND_SUPPORTED);
1481 _exit(SUSPEND_NOT_SUPPORTED);
1482 } else if (pid < 0) {
1483 error_setg_errno(errp, errno, "failed to create child process");
1487 ga_wait_child(pid, &status, &local_err);
1489 error_propagate(errp, local_err);
1493 if (!WIFEXITED(status)) {
1494 error_setg(errp, "child process has terminated abnormally");
1498 switch (WEXITSTATUS(status)) {
1499 case SUSPEND_SUPPORTED:
1501 case SUSPEND_NOT_SUPPORTED:
1503 "the requested suspend mode is not supported by the guest");
1507 "the helper program '%s' returned an unexpected exit status"
1508 " code (%d)", pmutils_path, WEXITSTATUS(status));
1513 g_free(pmutils_path);
1516 static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
1519 Error *local_err = NULL;
1524 pmutils_path = g_find_program_in_path(pmutils_bin);
1532 reopen_fd_to_null(0);
1533 reopen_fd_to_null(1);
1534 reopen_fd_to_null(2);
1537 execle(pmutils_path, pmutils_bin, NULL, environ);
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.
1546 _exit(EXIT_FAILURE);
1549 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1551 _exit(EXIT_FAILURE);
1554 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1555 _exit(EXIT_FAILURE);
1558 _exit(EXIT_SUCCESS);
1559 } else if (pid < 0) {
1560 error_setg_errno(errp, errno, "failed to create child process");
1564 ga_wait_child(pid, &status, &local_err);
1566 error_propagate(errp, local_err);
1570 if (!WIFEXITED(status)) {
1571 error_setg(errp, "child process has terminated abnormally");
1575 if (WEXITSTATUS(status)) {
1576 error_setg(errp, "child process has failed to suspend");
1581 g_free(pmutils_path);
1584 void qmp_guest_suspend_disk(Error **errp)
1586 Error *local_err = NULL;
1588 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err);
1590 error_propagate(errp, local_err);
1594 guest_suspend("pm-hibernate", "disk", errp);
1597 void qmp_guest_suspend_ram(Error **errp)
1599 Error *local_err = NULL;
1601 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err);
1603 error_propagate(errp, local_err);
1607 guest_suspend("pm-suspend", "mem", errp);
1610 void qmp_guest_suspend_hybrid(Error **errp)
1612 Error *local_err = NULL;
1614 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL,
1617 error_propagate(errp, local_err);
1621 guest_suspend("pm-suspend-hybrid", NULL, errp);
1624 static GuestNetworkInterfaceList *
1625 guest_find_interface(GuestNetworkInterfaceList *head,
1628 for (; head; head = head->next) {
1629 if (strcmp(head->value->name, name) == 0) {
1638 * Build information about guest interfaces
1640 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1642 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1643 struct ifaddrs *ifap, *ifa;
1645 if (getifaddrs(&ifap) < 0) {
1646 error_setg_errno(errp, errno, "getifaddrs failed");
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];
1657 unsigned char *mac_addr;
1660 g_debug("Processing %s interface", ifa->ifa_name);
1662 info = guest_find_interface(head, ifa->ifa_name);
1665 info = g_malloc0(sizeof(*info));
1666 info->value = g_malloc0(sizeof(*info->value));
1667 info->value->name = g_strdup(ifa->ifa_name);
1670 head = cur_item = info;
1672 cur_item->next = info;
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);
1682 error_setg_errno(errp, errno, "failed to create socket");
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",
1697 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
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]);
1705 info->value->has_hardware_address = true;
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");
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;
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]);
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");
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;
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]);
1754 if (!address_item) {
1758 address_list = &info->value->ip_addresses;
1760 while (*address_list && (*address_list)->next) {
1761 address_list = &(*address_list)->next;
1764 if (!*address_list) {
1765 *address_list = address_item;
1767 (*address_list)->next = address_item;
1770 info->value->has_ip_addresses = true;
1780 qapi_free_GuestNetworkInterfaceList(head);
1784 #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
1786 static long sysconf_exact(int name, const char *name_str, Error **errp)
1791 ret = sysconf(name);
1794 error_setg(errp, "sysconf(%s): value indefinite", name_str);
1796 error_setg_errno(errp, errno, "sysconf(%s)", name_str);
1802 /* Transfer online/offline status between @vcpu and the guest system.
1804 * On input either @errp or *@errp must be NULL.
1806 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1807 * - R: vcpu->logical_id
1809 * - W: vcpu->can_offline
1811 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1812 * - R: vcpu->logical_id
1815 * Written members remain unmodified on error.
1817 static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1823 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1825 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1827 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1829 static const char fn[] = "online";
1833 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
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 */
1845 unsigned char status;
1847 res = pread(fd, &status, 1, 0);
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,
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,
1862 } /* otherwise pretend successful re-(on|off)-lining */
1875 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1878 GuestLogicalProcessorList *head, **link;
1880 Error *local_err = NULL;
1885 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1887 while (local_err == NULL && current < sc_max) {
1888 GuestLogicalProcessor *vcpu;
1889 GuestLogicalProcessorList *entry;
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);
1896 entry = g_malloc0(sizeof *entry);
1897 entry->value = vcpu;
1900 link = &entry->next;
1903 if (local_err == NULL) {
1904 /* there's no guest with zero VCPUs */
1905 g_assert(head != NULL);
1909 qapi_free_GuestLogicalProcessorList(head);
1910 error_propagate(errp, local_err);
1914 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1917 Error *local_err = NULL;
1920 while (vcpus != NULL) {
1921 transfer_vcpu(vcpus->value, false, &local_err);
1922 if (local_err != NULL) {
1926 vcpus = vcpus->next;
1929 if (local_err != NULL) {
1930 if (processed == 0) {
1931 error_propagate(errp, local_err);
1933 error_free(local_err);
1940 void qmp_guest_set_user_password(const char *username,
1941 const char *password,
1945 Error *local_err = NULL;
1946 char *passwd_path = NULL;
1949 int datafd[2] = { -1, -1 };
1950 char *rawpasswddata = NULL;
1951 size_t rawpasswdlen;
1952 char *chpasswddata = NULL;
1955 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
1956 if (!rawpasswddata) {
1959 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
1960 rawpasswddata[rawpasswdlen] = '\0';
1962 if (strchr(rawpasswddata, '\n')) {
1963 error_setg(errp, "forbidden characters in raw password");
1967 if (strchr(username, '\n') ||
1968 strchr(username, ':')) {
1969 error_setg(errp, "forbidden characters in username");
1973 chpasswddata = g_strdup_printf("%s:%s\n", username, rawpasswddata);
1974 chpasswdlen = strlen(chpasswddata);
1976 passwd_path = g_find_program_in_path("chpasswd");
1979 error_setg(errp, "cannot find 'passwd' program in PATH");
1983 if (pipe(datafd) < 0) {
1984 error_setg(errp, "cannot create pipe FDs");
1994 reopen_fd_to_null(1);
1995 reopen_fd_to_null(2);
1998 execle(passwd_path, "chpasswd", "-e", NULL, environ);
2000 execle(passwd_path, "chpasswd", NULL, environ);
2002 _exit(EXIT_FAILURE);
2003 } else if (pid < 0) {
2004 error_setg_errno(errp, errno, "failed to create child process");
2010 if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) != chpasswdlen) {
2011 error_setg_errno(errp, errno, "cannot write new account password");
2017 ga_wait_child(pid, &status, &local_err);
2019 error_propagate(errp, local_err);
2023 if (!WIFEXITED(status)) {
2024 error_setg(errp, "child process has terminated abnormally");
2028 if (WEXITSTATUS(status)) {
2029 error_setg(errp, "child process has failed to set user password");
2034 g_free(chpasswddata);
2035 g_free(rawpasswddata);
2036 g_free(passwd_path);
2037 if (datafd[0] != -1) {
2040 if (datafd[1] != -1) {
2045 static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf,
2046 int size, Error **errp)
2052 fd = openat(dirfd, pathname, O_RDONLY);
2054 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2058 res = pread(fd, buf, size, 0);
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);
2067 static void ga_write_sysfs_file(int dirfd, const char *pathname,
2068 const char *buf, int size, Error **errp)
2073 fd = openat(dirfd, pathname, O_WRONLY);
2075 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2079 if (pwrite(fd, buf, size, 0) == -1) {
2080 error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname);
2086 /* Transfer online/offline status between @mem_blk and the guest system.
2088 * On input either @errp or *@errp must be NULL.
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
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.
2101 static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
2102 GuestMemoryBlockResponse *result,
2108 Error *local_err = NULL;
2114 error_setg(errp, "Internal error, 'result' should not be NULL");
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?
2123 if (!dp && errno == ENOENT) {
2125 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2131 dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/",
2132 mem_blk->phys_index);
2133 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2136 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2138 if (errno == ENOENT) {
2139 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND;
2142 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2150 status = g_malloc0(10);
2151 ga_read_sysfs_file(dirfd, "state", status, 10, &local_err);
2153 /* treat with sysfs file that not exist in old kernel */
2154 if (errno == ENOENT) {
2155 error_free(local_err);
2157 mem_blk->online = true;
2158 mem_blk->can_offline = false;
2159 } else if (!mem_blk->online) {
2161 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2165 error_propagate(errp, local_err);
2168 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2175 char removable = '0';
2177 mem_blk->online = (strncmp(status, "online", 6) == 0);
2179 ga_read_sysfs_file(dirfd, "removable", &removable, 1, &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;
2186 error_propagate(errp, local_err);
2189 mem_blk->can_offline = (removable != '0');
2192 if (mem_blk->online != (strncmp(status, "online", 6) == 0)) {
2193 char *new_state = mem_blk->online ? g_strdup("online") :
2194 g_strdup("offline");
2196 ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state),
2200 error_free(local_err);
2202 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2206 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS;
2207 result->has_error_code = false;
2208 } /* otherwise pretend successful re-(on|off)-lining */
2219 result->has_error_code = true;
2220 result->error_code = errno;
2224 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2226 GuestMemoryBlockList *head, **link;
2227 Error *local_err = NULL;
2234 dp = opendir("/sys/devices/system/memory/");
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
2240 if (errno != ENOENT) {
2241 error_setg_errno(errp, errno, "Can't open directory"
2242 "\"/sys/devices/system/memory/\"");
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.
2252 while ((de = readdir(dp)) != NULL) {
2253 GuestMemoryBlock *mem_blk;
2254 GuestMemoryBlockList *entry;
2256 if ((strncmp(de->d_name, "memory", 6) != 0) ||
2257 !(de->d_type & DT_DIR)) {
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);
2267 entry = g_malloc0(sizeof *entry);
2268 entry->value = mem_blk;
2271 link = &entry->next;
2275 if (local_err == NULL) {
2276 /* there's no guest with zero memory blocks */
2278 error_setg(errp, "guest reported zero memory blocks!");
2283 qapi_free_GuestMemoryBlockList(head);
2284 error_propagate(errp, local_err);
2288 GuestMemoryBlockResponseList *
2289 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2291 GuestMemoryBlockResponseList *head, **link;
2292 Error *local_err = NULL;
2297 while (mem_blks != NULL) {
2298 GuestMemoryBlockResponse *result;
2299 GuestMemoryBlockResponseList *entry;
2300 GuestMemoryBlock *current_mem_blk = mem_blks->value;
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 */
2308 entry = g_malloc0(sizeof *entry);
2309 entry->value = result;
2312 link = &entry->next;
2313 mem_blks = mem_blks->next;
2318 qapi_free_GuestMemoryBlockResponseList(head);
2319 error_propagate(errp, local_err);
2323 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2325 Error *local_err = NULL;
2329 GuestMemoryBlockInfo *info;
2331 dirpath = g_strdup_printf("/sys/devices/system/memory/");
2332 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2334 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2340 buf = g_malloc0(20);
2341 ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err);
2345 error_propagate(errp, local_err);
2349 info = g_new0(GuestMemoryBlockInfo, 1);
2350 info->size = strtol(buf, NULL, 16); /* the unit is bytes */
2357 #else /* defined(__linux__) */
2359 void qmp_guest_suspend_disk(Error **errp)
2361 error_setg(errp, QERR_UNSUPPORTED);
2364 void qmp_guest_suspend_ram(Error **errp)
2366 error_setg(errp, QERR_UNSUPPORTED);
2369 void qmp_guest_suspend_hybrid(Error **errp)
2371 error_setg(errp, QERR_UNSUPPORTED);
2374 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
2376 error_setg(errp, QERR_UNSUPPORTED);
2380 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2382 error_setg(errp, QERR_UNSUPPORTED);
2386 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2388 error_setg(errp, QERR_UNSUPPORTED);
2392 void qmp_guest_set_user_password(const char *username,
2393 const char *password,
2397 error_setg(errp, QERR_UNSUPPORTED);
2400 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2402 error_setg(errp, QERR_UNSUPPORTED);
2406 GuestMemoryBlockResponseList *
2407 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2409 error_setg(errp, QERR_UNSUPPORTED);
2413 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2415 error_setg(errp, QERR_UNSUPPORTED);
2421 #if !defined(CONFIG_FSFREEZE)
2423 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
2425 error_setg(errp, QERR_UNSUPPORTED);
2429 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
2431 error_setg(errp, QERR_UNSUPPORTED);
2436 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
2438 error_setg(errp, QERR_UNSUPPORTED);
2443 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
2444 strList *mountpoints,
2447 error_setg(errp, QERR_UNSUPPORTED);
2452 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
2454 error_setg(errp, QERR_UNSUPPORTED);
2458 #endif /* CONFIG_FSFREEZE */
2460 #if !defined(CONFIG_FSTRIM)
2461 GuestFilesystemTrimResponse *
2462 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
2464 error_setg(errp, QERR_UNSUPPORTED);
2469 /* add unsupported commands to the blacklist */
2470 GList *ga_command_blacklist_init(GList *blacklist)
2472 #if !defined(__linux__)
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;
2483 blacklist = g_list_append(blacklist, g_strdup(*p++));
2488 #if !defined(CONFIG_FSFREEZE)
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;
2497 blacklist = g_list_append(blacklist, g_strdup(*p++));
2502 #if !defined(CONFIG_FSTRIM)
2503 blacklist = g_list_append(blacklist, g_strdup("guest-fstrim"));
2509 /* register init/cleanup routines for stateful command groups */
2510 void ga_command_state_init(GAState *s, GACommandState *cs)
2512 #if defined(CONFIG_FSFREEZE)
2513 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);