2 * QEMU Guest Agent POSIX-specific command implementations
4 * Copyright IBM Corp. 2011
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.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
22 #include "qga/guest-agent-core.h"
23 #include "qga-qmp-commands.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qemu/queue.h"
26 #include "qemu/host-utils.h"
28 #ifndef CONFIG_HAS_ENVIRON
30 #include <crt_externs.h>
31 #define environ (*_NSGetEnviron())
33 extern char **environ;
37 #if defined(__linux__)
41 #include <arpa/inet.h>
42 #include <sys/socket.h>
46 #define CONFIG_FSFREEZE
53 static void ga_wait_child(pid_t pid, int *status, Error **err)
60 rpid = waitpid(pid, status, 0);
61 } while (rpid == -1 && errno == EINTR);
64 error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
68 g_assert(rpid == pid);
71 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
73 const char *shutdown_flag;
74 Error *local_err = NULL;
78 slog("guest-shutdown called, mode: %s", mode);
79 if (!has_mode || strcmp(mode, "powerdown") == 0) {
81 } else if (strcmp(mode, "halt") == 0) {
83 } else if (strcmp(mode, "reboot") == 0) {
87 "mode is invalid (valid values are: halt|powerdown|reboot");
93 /* child, start the shutdown */
99 execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
100 "hypervisor initiated shutdown", (char*)NULL, environ);
102 } else if (pid < 0) {
103 error_setg_errno(err, errno, "failed to create child process");
107 ga_wait_child(pid, &status, &local_err);
108 if (error_is_set(&local_err)) {
109 error_propagate(err, local_err);
113 if (!WIFEXITED(status)) {
114 error_setg(err, "child process has terminated abnormally");
118 if (WEXITSTATUS(status)) {
119 error_setg(err, "child process has failed to shutdown");
126 int64_t qmp_guest_get_time(Error **errp)
132 ret = qemu_gettimeofday(&tq);
134 error_setg_errno(errp, errno, "Failed to get time");
138 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
142 void qmp_guest_set_time(int64_t time_ns, Error **errp)
147 Error *local_err = NULL;
150 /* year-2038 will overflow in case time_t is 32bit */
151 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
152 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
156 tv.tv_sec = time_ns / 1000000000;
157 tv.tv_usec = (time_ns % 1000000000) / 1000;
159 ret = settimeofday(&tv, NULL);
161 error_setg_errno(errp, errno, "Failed to set time to guest");
165 /* Set the Hardware Clock to the current System Time. */
169 reopen_fd_to_null(0);
170 reopen_fd_to_null(1);
171 reopen_fd_to_null(2);
173 execle("/sbin/hwclock", "hwclock", "-w", NULL, environ);
175 } else if (pid < 0) {
176 error_setg_errno(errp, errno, "failed to create child process");
180 ga_wait_child(pid, &status, &local_err);
181 if (error_is_set(&local_err)) {
182 error_propagate(errp, local_err);
186 if (!WIFEXITED(status)) {
187 error_setg(errp, "child process has terminated abnormally");
191 if (WEXITSTATUS(status)) {
192 error_setg(errp, "hwclock failed to set hardware clock to system time");
197 typedef struct GuestFileHandle {
200 QTAILQ_ENTRY(GuestFileHandle) next;
204 QTAILQ_HEAD(, GuestFileHandle) filehandles;
207 static int64_t guest_file_handle_add(FILE *fh, Error **errp)
209 GuestFileHandle *gfh;
212 handle = ga_get_fd_handle(ga_state, errp);
213 if (error_is_set(errp)) {
217 gfh = g_malloc0(sizeof(GuestFileHandle));
220 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
225 static GuestFileHandle *guest_file_handle_find(int64_t id, Error **err)
227 GuestFileHandle *gfh;
229 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
236 error_setg(err, "handle '%" PRId64 "' has not been found", id);
240 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
244 int64_t ret = -1, handle;
249 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
250 fh = fopen(path, mode);
252 error_setg_errno(err, errno, "failed to open file '%s' (mode: '%s')",
257 /* set fd non-blocking to avoid common use cases (like reading from a
258 * named pipe) from hanging the agent
261 ret = fcntl(fd, F_GETFL);
262 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
264 error_setg_errno(err, errno, "failed to make file '%s' non-blocking",
270 handle = guest_file_handle_add(fh, err);
271 if (error_is_set(err)) {
276 slog("guest-file-open, handle: %d", handle);
280 void qmp_guest_file_close(int64_t handle, Error **err)
282 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
285 slog("guest-file-close called, handle: %ld", handle);
290 ret = fclose(gfh->fh);
292 error_setg_errno(err, errno, "failed to close handle");
296 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
300 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
301 int64_t count, Error **err)
303 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
304 GuestFileRead *read_data = NULL;
314 count = QGA_READ_COUNT_DEFAULT;
315 } else if (count < 0) {
316 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
322 buf = g_malloc0(count+1);
323 read_count = fread(buf, 1, count, fh);
325 error_setg_errno(err, errno, "failed to read file");
326 slog("guest-file-read failed, handle: %ld", handle);
329 read_data = g_malloc0(sizeof(GuestFileRead));
330 read_data->count = read_count;
331 read_data->eof = feof(fh);
333 read_data->buf_b64 = g_base64_encode(buf, read_count);
342 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
343 bool has_count, int64_t count, Error **err)
345 GuestFileWrite *write_data = NULL;
349 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
357 buf = g_base64_decode(buf_b64, &buf_len);
361 } else if (count < 0 || count > buf_len) {
362 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
368 write_count = fwrite(buf, 1, count, fh);
370 error_setg_errno(err, errno, "failed to write to file");
371 slog("guest-file-write failed, handle: %ld", handle);
373 write_data = g_malloc0(sizeof(GuestFileWrite));
374 write_data->count = write_count;
375 write_data->eof = feof(fh);
383 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
384 int64_t whence, Error **err)
386 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
387 GuestFileSeek *seek_data = NULL;
396 ret = fseek(fh, offset, whence);
398 error_setg_errno(err, errno, "failed to seek file");
400 seek_data = g_malloc0(sizeof(GuestFileRead));
401 seek_data->position = ftell(fh);
402 seek_data->eof = feof(fh);
409 void qmp_guest_file_flush(int64_t handle, Error **err)
411 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
422 error_setg_errno(err, errno, "failed to flush file");
426 static void guest_file_init(void)
428 QTAILQ_INIT(&guest_file_state.filehandles);
431 /* linux-specific implementations. avoid this if at all possible. */
432 #if defined(__linux__)
434 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
435 typedef struct FsMount {
438 QTAILQ_ENTRY(FsMount) next;
441 typedef QTAILQ_HEAD(, FsMount) FsMountList;
443 static void free_fs_mount_list(FsMountList *mounts)
445 FsMount *mount, *temp;
451 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
452 QTAILQ_REMOVE(mounts, mount, next);
453 g_free(mount->dirname);
454 g_free(mount->devtype);
460 * Walk the mount table and build a list of local file systems
462 static void build_fs_mount_list(FsMountList *mounts, Error **err)
466 char const *mtab = "/proc/self/mounts";
469 fp = setmntent(mtab, "r");
471 error_setg(err, "failed to open mtab file: '%s'", mtab);
475 while ((ment = getmntent(fp))) {
477 * An entry which device name doesn't start with a '/' is
478 * either a dummy file system or a network file system.
479 * Add special handling for smbfs and cifs as is done by
482 if ((ment->mnt_fsname[0] != '/') ||
483 (strcmp(ment->mnt_type, "smbfs") == 0) ||
484 (strcmp(ment->mnt_type, "cifs") == 0)) {
488 mount = g_malloc0(sizeof(FsMount));
489 mount->dirname = g_strdup(ment->mnt_dir);
490 mount->devtype = g_strdup(ment->mnt_type);
492 QTAILQ_INSERT_TAIL(mounts, mount, next);
499 #if defined(CONFIG_FSFREEZE)
502 FSFREEZE_HOOK_THAW = 0,
503 FSFREEZE_HOOK_FREEZE,
506 const char *fsfreeze_hook_arg_string[] = {
511 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
516 const char *arg_str = fsfreeze_hook_arg_string[arg];
517 Error *local_err = NULL;
519 hook = ga_fsfreeze_hook(ga_state);
523 if (access(hook, X_OK) != 0) {
524 error_setg_errno(err, errno, "can't access fsfreeze hook '%s'", hook);
528 slog("executing fsfreeze hook with arg '%s'", arg_str);
532 reopen_fd_to_null(0);
533 reopen_fd_to_null(1);
534 reopen_fd_to_null(2);
536 execle(hook, hook, arg_str, NULL, environ);
538 } else if (pid < 0) {
539 error_setg_errno(err, errno, "failed to create child process");
543 ga_wait_child(pid, &status, &local_err);
544 if (error_is_set(&local_err)) {
545 error_propagate(err, local_err);
549 if (!WIFEXITED(status)) {
550 error_setg(err, "fsfreeze hook has terminated abnormally");
554 status = WEXITSTATUS(status);
556 error_setg(err, "fsfreeze hook has failed with status %d", status);
562 * Return status of freeze/thaw
564 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
566 if (ga_is_frozen(ga_state)) {
567 return GUEST_FSFREEZE_STATUS_FROZEN;
570 return GUEST_FSFREEZE_STATUS_THAWED;
574 * Walk list of mounted file systems in the guest, and freeze the ones which
575 * are real local file systems.
577 int64_t qmp_guest_fsfreeze_freeze(Error **err)
581 struct FsMount *mount;
582 Error *local_err = NULL;
585 slog("guest-fsfreeze called");
587 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
588 if (error_is_set(&local_err)) {
589 error_propagate(err, local_err);
593 QTAILQ_INIT(&mounts);
594 build_fs_mount_list(&mounts, &local_err);
595 if (error_is_set(&local_err)) {
596 error_propagate(err, local_err);
600 /* cannot risk guest agent blocking itself on a write in this state */
601 ga_set_frozen(ga_state);
603 QTAILQ_FOREACH(mount, &mounts, next) {
604 fd = qemu_open(mount->dirname, O_RDONLY);
606 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
610 /* we try to cull filesytems we know won't work in advance, but other
611 * filesytems may not implement fsfreeze for less obvious reasons.
612 * these will report EOPNOTSUPP. we simply ignore these when tallying
613 * the number of frozen filesystems.
615 * any other error means a failure to freeze a filesystem we
616 * expect to be freezable, so return an error in those cases
617 * and return system to thawed state.
619 ret = ioctl(fd, FIFREEZE);
621 if (errno != EOPNOTSUPP) {
622 error_setg_errno(err, errno, "failed to freeze %s",
633 free_fs_mount_list(&mounts);
637 free_fs_mount_list(&mounts);
638 qmp_guest_fsfreeze_thaw(NULL);
643 * Walk list of frozen file systems in the guest, and thaw them.
645 int64_t qmp_guest_fsfreeze_thaw(Error **err)
650 int fd, i = 0, logged;
651 Error *local_err = NULL;
653 QTAILQ_INIT(&mounts);
654 build_fs_mount_list(&mounts, &local_err);
655 if (error_is_set(&local_err)) {
656 error_propagate(err, local_err);
660 QTAILQ_FOREACH(mount, &mounts, next) {
662 fd = qemu_open(mount->dirname, O_RDONLY);
666 /* we have no way of knowing whether a filesystem was actually unfrozen
667 * as a result of a successful call to FITHAW, only that if an error
668 * was returned the filesystem was *not* unfrozen by that particular
671 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
672 * to unfreeze, continuing issuing FITHAW until an error is returned,
673 * in which case either the filesystem is in an unfreezable state, or,
674 * more likely, it was thawed previously (and remains so afterward).
676 * also, since the most recent successful call is the one that did
677 * the actual unfreeze, we can use this to provide an accurate count
678 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
679 * may * be useful for determining whether a filesystem was unfrozen
680 * during the freeze/thaw phase by a process other than qemu-ga.
683 ret = ioctl(fd, FITHAW);
684 if (ret == 0 && !logged) {
692 ga_unset_frozen(ga_state);
693 free_fs_mount_list(&mounts);
695 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, err);
700 static void guest_fsfreeze_cleanup(void)
704 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
705 qmp_guest_fsfreeze_thaw(&err);
707 slog("failed to clean up frozen filesystems: %s",
708 error_get_pretty(err));
713 #endif /* CONFIG_FSFREEZE */
715 #if defined(CONFIG_FSTRIM)
717 * Walk list of mounted file systems in the guest, and trim them.
719 void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
723 struct FsMount *mount;
725 Error *local_err = NULL;
726 struct fstrim_range r = {
729 .minlen = has_minimum ? minimum : 0,
732 slog("guest-fstrim called");
734 QTAILQ_INIT(&mounts);
735 build_fs_mount_list(&mounts, &local_err);
736 if (error_is_set(&local_err)) {
737 error_propagate(err, local_err);
741 QTAILQ_FOREACH(mount, &mounts, next) {
742 fd = qemu_open(mount->dirname, O_RDONLY);
744 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
748 /* We try to cull filesytems we know won't work in advance, but other
749 * filesytems may not implement fstrim for less obvious reasons. These
750 * will report EOPNOTSUPP; we simply ignore these errors. Any other
751 * error means an unexpected error, so return it in those cases. In
752 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
754 ret = ioctl(fd, FITRIM, &r);
756 if (errno != ENOTTY && errno != EOPNOTSUPP) {
757 error_setg_errno(err, errno, "failed to trim %s",
767 free_fs_mount_list(&mounts);
769 #endif /* CONFIG_FSTRIM */
772 #define LINUX_SYS_STATE_FILE "/sys/power/state"
773 #define SUSPEND_SUPPORTED 0
774 #define SUSPEND_NOT_SUPPORTED 1
776 static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
777 const char *sysfile_str, Error **err)
779 Error *local_err = NULL;
784 pmutils_path = g_find_program_in_path(pmutils_bin);
788 char buf[32]; /* hopefully big enough */
793 reopen_fd_to_null(0);
794 reopen_fd_to_null(1);
795 reopen_fd_to_null(2);
798 execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ);
802 * If we get here either pm-utils is not installed or execle() has
803 * failed. Let's try the manual method if the caller wants it.
807 _exit(SUSPEND_NOT_SUPPORTED);
810 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
812 _exit(SUSPEND_NOT_SUPPORTED);
815 ret = read(fd, buf, sizeof(buf)-1);
817 _exit(SUSPEND_NOT_SUPPORTED);
821 if (strstr(buf, sysfile_str)) {
822 _exit(SUSPEND_SUPPORTED);
825 _exit(SUSPEND_NOT_SUPPORTED);
826 } else if (pid < 0) {
827 error_setg_errno(err, errno, "failed to create child process");
831 ga_wait_child(pid, &status, &local_err);
832 if (error_is_set(&local_err)) {
833 error_propagate(err, local_err);
837 if (!WIFEXITED(status)) {
838 error_setg(err, "child process has terminated abnormally");
842 switch (WEXITSTATUS(status)) {
843 case SUSPEND_SUPPORTED:
845 case SUSPEND_NOT_SUPPORTED:
847 "the requested suspend mode is not supported by the guest");
851 "the helper program '%s' returned an unexpected exit status"
852 " code (%d)", pmutils_path, WEXITSTATUS(status));
857 g_free(pmutils_path);
860 static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
863 Error *local_err = NULL;
868 pmutils_path = g_find_program_in_path(pmutils_bin);
876 reopen_fd_to_null(0);
877 reopen_fd_to_null(1);
878 reopen_fd_to_null(2);
881 execle(pmutils_path, pmutils_bin, NULL, environ);
885 * If we get here either pm-utils is not installed or execle() has
886 * failed. Let's try the manual method if the caller wants it.
893 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
898 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
903 } else if (pid < 0) {
904 error_setg_errno(err, errno, "failed to create child process");
908 ga_wait_child(pid, &status, &local_err);
909 if (error_is_set(&local_err)) {
910 error_propagate(err, local_err);
914 if (!WIFEXITED(status)) {
915 error_setg(err, "child process has terminated abnormally");
919 if (WEXITSTATUS(status)) {
920 error_setg(err, "child process has failed to suspend");
925 g_free(pmutils_path);
928 void qmp_guest_suspend_disk(Error **err)
930 bios_supports_mode("pm-is-supported", "--hibernate", "disk", err);
931 if (error_is_set(err)) {
935 guest_suspend("pm-hibernate", "disk", err);
938 void qmp_guest_suspend_ram(Error **err)
940 bios_supports_mode("pm-is-supported", "--suspend", "mem", err);
941 if (error_is_set(err)) {
945 guest_suspend("pm-suspend", "mem", err);
948 void qmp_guest_suspend_hybrid(Error **err)
950 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL, err);
951 if (error_is_set(err)) {
955 guest_suspend("pm-suspend-hybrid", NULL, err);
958 static GuestNetworkInterfaceList *
959 guest_find_interface(GuestNetworkInterfaceList *head,
962 for (; head; head = head->next) {
963 if (strcmp(head->value->name, name) == 0) {
972 * Build information about guest interfaces
974 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
976 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
977 struct ifaddrs *ifap, *ifa;
979 if (getifaddrs(&ifap) < 0) {
980 error_setg_errno(errp, errno, "getifaddrs failed");
984 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
985 GuestNetworkInterfaceList *info;
986 GuestIpAddressList **address_list = NULL, *address_item = NULL;
987 char addr4[INET_ADDRSTRLEN];
988 char addr6[INET6_ADDRSTRLEN];
991 unsigned char *mac_addr;
994 g_debug("Processing %s interface", ifa->ifa_name);
996 info = guest_find_interface(head, ifa->ifa_name);
999 info = g_malloc0(sizeof(*info));
1000 info->value = g_malloc0(sizeof(*info->value));
1001 info->value->name = g_strdup(ifa->ifa_name);
1004 head = cur_item = info;
1006 cur_item->next = info;
1011 if (!info->value->has_hardware_address &&
1012 ifa->ifa_flags & SIOCGIFHWADDR) {
1013 /* we haven't obtained HW address yet */
1014 sock = socket(PF_INET, SOCK_STREAM, 0);
1016 error_setg_errno(errp, errno, "failed to create socket");
1020 memset(&ifr, 0, sizeof(ifr));
1021 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
1022 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
1023 error_setg_errno(errp, errno,
1024 "failed to get MAC address of %s",
1031 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1033 info->value->hardware_address =
1034 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1035 (int) mac_addr[0], (int) mac_addr[1],
1036 (int) mac_addr[2], (int) mac_addr[3],
1037 (int) mac_addr[4], (int) mac_addr[5]);
1039 info->value->has_hardware_address = true;
1042 if (ifa->ifa_addr &&
1043 ifa->ifa_addr->sa_family == AF_INET) {
1044 /* interface with IPv4 address */
1045 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1046 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
1047 error_setg_errno(errp, errno, "inet_ntop failed");
1051 address_item = g_malloc0(sizeof(*address_item));
1052 address_item->value = g_malloc0(sizeof(*address_item->value));
1053 address_item->value->ip_address = g_strdup(addr4);
1054 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1056 if (ifa->ifa_netmask) {
1057 /* Count the number of set bits in netmask.
1058 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1059 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1060 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1062 } else if (ifa->ifa_addr &&
1063 ifa->ifa_addr->sa_family == AF_INET6) {
1064 /* interface with IPv6 address */
1065 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1066 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
1067 error_setg_errno(errp, errno, "inet_ntop failed");
1071 address_item = g_malloc0(sizeof(*address_item));
1072 address_item->value = g_malloc0(sizeof(*address_item->value));
1073 address_item->value->ip_address = g_strdup(addr6);
1074 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1076 if (ifa->ifa_netmask) {
1077 /* Count the number of set bits in netmask.
1078 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1079 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1080 address_item->value->prefix =
1081 ctpop32(((uint32_t *) p)[0]) +
1082 ctpop32(((uint32_t *) p)[1]) +
1083 ctpop32(((uint32_t *) p)[2]) +
1084 ctpop32(((uint32_t *) p)[3]);
1088 if (!address_item) {
1092 address_list = &info->value->ip_addresses;
1094 while (*address_list && (*address_list)->next) {
1095 address_list = &(*address_list)->next;
1098 if (!*address_list) {
1099 *address_list = address_item;
1101 (*address_list)->next = address_item;
1104 info->value->has_ip_addresses = true;
1114 qapi_free_GuestNetworkInterfaceList(head);
1118 #define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1120 static long sysconf_exact(int name, const char *name_str, Error **err)
1125 ret = sysconf(name);
1128 error_setg(err, "sysconf(%s): value indefinite", name_str);
1130 error_setg_errno(err, errno, "sysconf(%s)", name_str);
1136 /* Transfer online/offline status between @vcpu and the guest system.
1138 * On input either @errp or *@errp must be NULL.
1140 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1141 * - R: vcpu->logical_id
1143 * - W: vcpu->can_offline
1145 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1146 * - R: vcpu->logical_id
1149 * Written members remain unmodified on error.
1151 static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1157 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1159 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1161 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1163 static const char fn[] = "online";
1167 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1169 if (errno != ENOENT) {
1170 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
1171 } else if (sys2vcpu) {
1172 vcpu->online = true;
1173 vcpu->can_offline = false;
1174 } else if (!vcpu->online) {
1175 error_setg(errp, "logical processor #%" PRId64 " can't be "
1176 "offlined", vcpu->logical_id);
1177 } /* otherwise pretend successful re-onlining */
1179 unsigned char status;
1181 res = pread(fd, &status, 1, 0);
1183 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
1184 } else if (res == 0) {
1185 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
1187 } else if (sys2vcpu) {
1188 vcpu->online = (status != '0');
1189 vcpu->can_offline = true;
1190 } else if (vcpu->online != (status != '0')) {
1191 status = '0' + vcpu->online;
1192 if (pwrite(fd, &status, 1, 0) == -1) {
1193 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
1196 } /* otherwise pretend successful re-(on|off)-lining */
1209 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1212 GuestLogicalProcessorList *head, **link;
1214 Error *local_err = NULL;
1219 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
1221 while (local_err == NULL && current < sc_max) {
1222 GuestLogicalProcessor *vcpu;
1223 GuestLogicalProcessorList *entry;
1225 vcpu = g_malloc0(sizeof *vcpu);
1226 vcpu->logical_id = current++;
1227 vcpu->has_can_offline = true; /* lolspeak ftw */
1228 transfer_vcpu(vcpu, true, &local_err);
1230 entry = g_malloc0(sizeof *entry);
1231 entry->value = vcpu;
1234 link = &entry->next;
1237 if (local_err == NULL) {
1238 /* there's no guest with zero VCPUs */
1239 g_assert(head != NULL);
1243 qapi_free_GuestLogicalProcessorList(head);
1244 error_propagate(errp, local_err);
1248 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1251 Error *local_err = NULL;
1254 while (vcpus != NULL) {
1255 transfer_vcpu(vcpus->value, false, &local_err);
1256 if (local_err != NULL) {
1260 vcpus = vcpus->next;
1263 if (local_err != NULL) {
1264 if (processed == 0) {
1265 error_propagate(errp, local_err);
1267 error_free(local_err);
1274 #else /* defined(__linux__) */
1276 void qmp_guest_suspend_disk(Error **err)
1278 error_set(err, QERR_UNSUPPORTED);
1281 void qmp_guest_suspend_ram(Error **err)
1283 error_set(err, QERR_UNSUPPORTED);
1286 void qmp_guest_suspend_hybrid(Error **err)
1288 error_set(err, QERR_UNSUPPORTED);
1291 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1293 error_set(errp, QERR_UNSUPPORTED);
1297 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1299 error_set(errp, QERR_UNSUPPORTED);
1303 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1305 error_set(errp, QERR_UNSUPPORTED);
1311 #if !defined(CONFIG_FSFREEZE)
1313 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
1315 error_set(err, QERR_UNSUPPORTED);
1320 int64_t qmp_guest_fsfreeze_freeze(Error **err)
1322 error_set(err, QERR_UNSUPPORTED);
1327 int64_t qmp_guest_fsfreeze_thaw(Error **err)
1329 error_set(err, QERR_UNSUPPORTED);
1333 #endif /* CONFIG_FSFREEZE */
1335 #if !defined(CONFIG_FSTRIM)
1336 void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
1338 error_set(err, QERR_UNSUPPORTED);
1342 /* register init/cleanup routines for stateful command groups */
1343 void ga_command_state_init(GAState *s, GACommandState *cs)
1345 #if defined(CONFIG_FSFREEZE)
1346 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
1348 ga_command_state_add(cs, guest_file_init, NULL);