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.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
25 #include "qga/guest-agent-core.h"
26 #include "qga-qmp-commands.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/queue.h"
29 #include "qemu/host-utils.h"
31 #ifndef CONFIG_HAS_ENVIRON
33 #include <crt_externs.h>
34 #define environ (*_NSGetEnviron())
36 extern char **environ
;
40 #if defined(__linux__)
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
49 #define CONFIG_FSFREEZE
56 static void ga_wait_child(pid_t pid
, int *status
, Error
**err
)
63 rpid
= waitpid(pid
, status
, 0);
64 } while (rpid
== -1 && errno
== EINTR
);
67 error_setg_errno(err
, errno
, "failed to wait for child (pid: %d)", pid
);
71 g_assert(rpid
== pid
);
74 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**err
)
76 const char *shutdown_flag
;
77 Error
*local_err
= NULL
;
81 slog("guest-shutdown called, mode: %s", mode
);
82 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
84 } else if (strcmp(mode
, "halt") == 0) {
86 } else if (strcmp(mode
, "reboot") == 0) {
90 "mode is invalid (valid values are: halt|powerdown|reboot");
96 /* child, start the shutdown */
100 reopen_fd_to_null(2);
102 execle("/sbin/shutdown", "shutdown", shutdown_flag
, "+0",
103 "hypervisor initiated shutdown", (char*)NULL
, environ
);
105 } else if (pid
< 0) {
106 error_setg_errno(err
, errno
, "failed to create child process");
110 ga_wait_child(pid
, &status
, &local_err
);
111 if (error_is_set(&local_err
)) {
112 error_propagate(err
, local_err
);
116 if (!WIFEXITED(status
)) {
117 error_setg(err
, "child process has terminated abnormally");
121 if (WEXITSTATUS(status
)) {
122 error_setg(err
, "child process has failed to shutdown");
129 int64_t qmp_guest_get_time(Error
**errp
)
135 ret
= qemu_gettimeofday(&tq
);
137 error_setg_errno(errp
, errno
, "Failed to get time");
141 time_ns
= tq
.tv_sec
* 1000000000LL + tq
.tv_usec
* 1000;
145 void qmp_guest_set_time(int64_t time_ns
, Error
**errp
)
150 Error
*local_err
= NULL
;
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;
162 ret
= settimeofday(&tv
, NULL
);
164 error_setg_errno(errp
, errno
, "Failed to set time to guest");
168 /* Set the Hardware Clock to the current System Time. */
172 reopen_fd_to_null(0);
173 reopen_fd_to_null(1);
174 reopen_fd_to_null(2);
176 execle("/sbin/hwclock", "hwclock", "-w", NULL
, environ
);
178 } else if (pid
< 0) {
179 error_setg_errno(errp
, errno
, "failed to create child process");
183 ga_wait_child(pid
, &status
, &local_err
);
184 if (error_is_set(&local_err
)) {
185 error_propagate(errp
, local_err
);
189 if (!WIFEXITED(status
)) {
190 error_setg(errp
, "child process has terminated abnormally");
194 if (WEXITSTATUS(status
)) {
195 error_setg(errp
, "hwclock failed to set hardware clock to system time");
200 typedef struct GuestFileHandle
{
203 QTAILQ_ENTRY(GuestFileHandle
) next
;
207 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
210 static int64_t guest_file_handle_add(FILE *fh
, Error
**errp
)
212 GuestFileHandle
*gfh
;
215 handle
= ga_get_fd_handle(ga_state
, errp
);
216 if (error_is_set(errp
)) {
220 gfh
= g_malloc0(sizeof(GuestFileHandle
));
223 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
228 static GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**err
)
230 GuestFileHandle
*gfh
;
232 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
239 error_setg(err
, "handle '%" PRId64
"' has not been found", id
);
243 typedef const char * const ccpc
;
245 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
246 static const struct {
249 } guest_file_open_modes
[] = {
250 { (ccpc
[]){ "r", "rb", NULL
}, O_RDONLY
},
251 { (ccpc
[]){ "w", "wb", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
},
252 { (ccpc
[]){ "a", "ab", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
},
253 { (ccpc
[]){ "r+", "rb+", "r+b", NULL
}, O_RDWR
},
254 { (ccpc
[]){ "w+", "wb+", "w+b", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
},
255 { (ccpc
[]){ "a+", "ab+", "a+b", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
}
259 find_open_flag(const char *mode_str
, Error
**err
)
263 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
266 form
= guest_file_open_modes
[mode
].forms
;
267 while (*form
!= NULL
&& strcmp(*form
, mode_str
) != 0) {
275 if (mode
== ARRAY_SIZE(guest_file_open_modes
)) {
276 error_setg(err
, "invalid file open mode '%s'", mode_str
);
279 return guest_file_open_modes
[mode
].oflag_base
| O_NOCTTY
| O_NONBLOCK
;
282 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
283 S_IRGRP | S_IWGRP | \
287 safe_open_or_create(const char *path
, const char *mode
, Error
**err
)
289 Error
*local_err
= NULL
;
292 oflag
= find_open_flag(mode
, &local_err
);
293 if (local_err
== NULL
) {
296 /* If the caller wants / allows creation of a new file, we implement it
297 * with a two step process: open() + (open() / fchmod()).
299 * First we insist on creating the file exclusively as a new file. If
300 * that succeeds, we're free to set any file-mode bits on it. (The
301 * motivation is that we want to set those file-mode bits independently
302 * of the current umask.)
304 * If the exclusive creation fails because the file already exists
305 * (EEXIST is not possible for any other reason), we just attempt to
306 * open the file, but in this case we won't be allowed to change the
307 * file-mode bits on the preexistent file.
309 * The pathname should never disappear between the two open()s in
310 * practice. If it happens, then someone very likely tried to race us.
311 * In this case just go ahead and report the ENOENT from the second
312 * open() to the caller.
314 * If the caller wants to open a preexistent file, then the first
315 * open() is decisive and its third argument is ignored, and the second
316 * open() and the fchmod() are never called.
318 fd
= open(path
, oflag
| ((oflag
& O_CREAT
) ? O_EXCL
: 0), 0);
319 if (fd
== -1 && errno
== EEXIST
) {
320 oflag
&= ~(unsigned)O_CREAT
;
321 fd
= open(path
, oflag
);
325 error_setg_errno(&local_err
, errno
, "failed to open file '%s' "
326 "(mode: '%s')", path
, mode
);
328 qemu_set_cloexec(fd
);
330 if ((oflag
& O_CREAT
) && fchmod(fd
, DEFAULT_NEW_FILE_MODE
) == -1) {
331 error_setg_errno(&local_err
, errno
, "failed to set permission "
332 "0%03o on new file '%s' (mode: '%s')",
333 (unsigned)DEFAULT_NEW_FILE_MODE
, path
, mode
);
337 f
= fdopen(fd
, mode
);
339 error_setg_errno(&local_err
, errno
, "failed to associate "
340 "stdio stream with file descriptor %d, "
341 "file '%s' (mode: '%s')", fd
, path
, mode
);
351 error_propagate(err
, local_err
);
355 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
, Error
**err
)
358 Error
*local_err
= NULL
;
360 int64_t ret
= -1, handle
;
365 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
366 fh
= safe_open_or_create(path
, mode
, &local_err
);
367 if (local_err
!= NULL
) {
368 error_propagate(err
, local_err
);
372 /* set fd non-blocking to avoid common use cases (like reading from a
373 * named pipe) from hanging the agent
376 ret
= fcntl(fd
, F_GETFL
);
377 ret
= fcntl(fd
, F_SETFL
, ret
| O_NONBLOCK
);
379 error_setg_errno(err
, errno
, "failed to make file '%s' non-blocking",
385 handle
= guest_file_handle_add(fh
, err
);
386 if (error_is_set(err
)) {
391 slog("guest-file-open, handle: %d", handle
);
395 void qmp_guest_file_close(int64_t handle
, Error
**err
)
397 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
400 slog("guest-file-close called, handle: %ld", handle
);
405 ret
= fclose(gfh
->fh
);
407 error_setg_errno(err
, errno
, "failed to close handle");
411 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
415 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
416 int64_t count
, Error
**err
)
418 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
419 GuestFileRead
*read_data
= NULL
;
429 count
= QGA_READ_COUNT_DEFAULT
;
430 } else if (count
< 0) {
431 error_setg(err
, "value '%" PRId64
"' is invalid for argument count",
437 buf
= g_malloc0(count
+1);
438 read_count
= fread(buf
, 1, count
, fh
);
440 error_setg_errno(err
, errno
, "failed to read file");
441 slog("guest-file-read failed, handle: %ld", handle
);
444 read_data
= g_malloc0(sizeof(GuestFileRead
));
445 read_data
->count
= read_count
;
446 read_data
->eof
= feof(fh
);
448 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
457 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
458 bool has_count
, int64_t count
, Error
**err
)
460 GuestFileWrite
*write_data
= NULL
;
464 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
472 buf
= g_base64_decode(buf_b64
, &buf_len
);
476 } else if (count
< 0 || count
> buf_len
) {
477 error_setg(err
, "value '%" PRId64
"' is invalid for argument count",
483 write_count
= fwrite(buf
, 1, count
, fh
);
485 error_setg_errno(err
, errno
, "failed to write to file");
486 slog("guest-file-write failed, handle: %ld", handle
);
488 write_data
= g_malloc0(sizeof(GuestFileWrite
));
489 write_data
->count
= write_count
;
490 write_data
->eof
= feof(fh
);
498 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
499 int64_t whence
, Error
**err
)
501 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
502 GuestFileSeek
*seek_data
= NULL
;
511 ret
= fseek(fh
, offset
, whence
);
513 error_setg_errno(err
, errno
, "failed to seek file");
515 seek_data
= g_malloc0(sizeof(GuestFileRead
));
516 seek_data
->position
= ftell(fh
);
517 seek_data
->eof
= feof(fh
);
524 void qmp_guest_file_flush(int64_t handle
, Error
**err
)
526 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
537 error_setg_errno(err
, errno
, "failed to flush file");
541 static void guest_file_init(void)
543 QTAILQ_INIT(&guest_file_state
.filehandles
);
546 /* linux-specific implementations. avoid this if at all possible. */
547 #if defined(__linux__)
549 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
550 typedef struct FsMount
{
553 QTAILQ_ENTRY(FsMount
) next
;
556 typedef QTAILQ_HEAD(, FsMount
) FsMountList
;
558 static void free_fs_mount_list(FsMountList
*mounts
)
560 FsMount
*mount
, *temp
;
566 QTAILQ_FOREACH_SAFE(mount
, mounts
, next
, temp
) {
567 QTAILQ_REMOVE(mounts
, mount
, next
);
568 g_free(mount
->dirname
);
569 g_free(mount
->devtype
);
575 * Walk the mount table and build a list of local file systems
577 static void build_fs_mount_list(FsMountList
*mounts
, Error
**err
)
581 char const *mtab
= "/proc/self/mounts";
584 fp
= setmntent(mtab
, "r");
586 error_setg(err
, "failed to open mtab file: '%s'", mtab
);
590 while ((ment
= getmntent(fp
))) {
592 * An entry which device name doesn't start with a '/' is
593 * either a dummy file system or a network file system.
594 * Add special handling for smbfs and cifs as is done by
597 if ((ment
->mnt_fsname
[0] != '/') ||
598 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
599 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
603 mount
= g_malloc0(sizeof(FsMount
));
604 mount
->dirname
= g_strdup(ment
->mnt_dir
);
605 mount
->devtype
= g_strdup(ment
->mnt_type
);
607 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
614 #if defined(CONFIG_FSFREEZE)
617 FSFREEZE_HOOK_THAW
= 0,
618 FSFREEZE_HOOK_FREEZE
,
621 const char *fsfreeze_hook_arg_string
[] = {
626 static void execute_fsfreeze_hook(FsfreezeHookArg arg
, Error
**err
)
631 const char *arg_str
= fsfreeze_hook_arg_string
[arg
];
632 Error
*local_err
= NULL
;
634 hook
= ga_fsfreeze_hook(ga_state
);
638 if (access(hook
, X_OK
) != 0) {
639 error_setg_errno(err
, errno
, "can't access fsfreeze hook '%s'", hook
);
643 slog("executing fsfreeze hook with arg '%s'", arg_str
);
647 reopen_fd_to_null(0);
648 reopen_fd_to_null(1);
649 reopen_fd_to_null(2);
651 execle(hook
, hook
, arg_str
, NULL
, environ
);
653 } else if (pid
< 0) {
654 error_setg_errno(err
, errno
, "failed to create child process");
658 ga_wait_child(pid
, &status
, &local_err
);
659 if (error_is_set(&local_err
)) {
660 error_propagate(err
, local_err
);
664 if (!WIFEXITED(status
)) {
665 error_setg(err
, "fsfreeze hook has terminated abnormally");
669 status
= WEXITSTATUS(status
);
671 error_setg(err
, "fsfreeze hook has failed with status %d", status
);
677 * Return status of freeze/thaw
679 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
681 if (ga_is_frozen(ga_state
)) {
682 return GUEST_FSFREEZE_STATUS_FROZEN
;
685 return GUEST_FSFREEZE_STATUS_THAWED
;
689 * Walk list of mounted file systems in the guest, and freeze the ones which
690 * are real local file systems.
692 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
696 struct FsMount
*mount
;
697 Error
*local_err
= NULL
;
700 slog("guest-fsfreeze called");
702 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE
, &local_err
);
703 if (error_is_set(&local_err
)) {
704 error_propagate(err
, local_err
);
708 QTAILQ_INIT(&mounts
);
709 build_fs_mount_list(&mounts
, &local_err
);
710 if (error_is_set(&local_err
)) {
711 error_propagate(err
, local_err
);
715 /* cannot risk guest agent blocking itself on a write in this state */
716 ga_set_frozen(ga_state
);
718 QTAILQ_FOREACH(mount
, &mounts
, next
) {
719 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
721 error_setg_errno(err
, errno
, "failed to open %s", mount
->dirname
);
725 /* we try to cull filesytems we know won't work in advance, but other
726 * filesytems may not implement fsfreeze for less obvious reasons.
727 * these will report EOPNOTSUPP. we simply ignore these when tallying
728 * the number of frozen filesystems.
730 * any other error means a failure to freeze a filesystem we
731 * expect to be freezable, so return an error in those cases
732 * and return system to thawed state.
734 ret
= ioctl(fd
, FIFREEZE
);
736 if (errno
!= EOPNOTSUPP
) {
737 error_setg_errno(err
, errno
, "failed to freeze %s",
748 free_fs_mount_list(&mounts
);
752 free_fs_mount_list(&mounts
);
753 qmp_guest_fsfreeze_thaw(NULL
);
758 * Walk list of frozen file systems in the guest, and thaw them.
760 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
765 int fd
, i
= 0, logged
;
766 Error
*local_err
= NULL
;
768 QTAILQ_INIT(&mounts
);
769 build_fs_mount_list(&mounts
, &local_err
);
770 if (error_is_set(&local_err
)) {
771 error_propagate(err
, local_err
);
775 QTAILQ_FOREACH(mount
, &mounts
, next
) {
777 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
781 /* we have no way of knowing whether a filesystem was actually unfrozen
782 * as a result of a successful call to FITHAW, only that if an error
783 * was returned the filesystem was *not* unfrozen by that particular
786 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
787 * to unfreeze, continuing issuing FITHAW until an error is returned,
788 * in which case either the filesystem is in an unfreezable state, or,
789 * more likely, it was thawed previously (and remains so afterward).
791 * also, since the most recent successful call is the one that did
792 * the actual unfreeze, we can use this to provide an accurate count
793 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
794 * may * be useful for determining whether a filesystem was unfrozen
795 * during the freeze/thaw phase by a process other than qemu-ga.
798 ret
= ioctl(fd
, FITHAW
);
799 if (ret
== 0 && !logged
) {
807 ga_unset_frozen(ga_state
);
808 free_fs_mount_list(&mounts
);
810 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW
, err
);
815 static void guest_fsfreeze_cleanup(void)
819 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
820 qmp_guest_fsfreeze_thaw(&err
);
822 slog("failed to clean up frozen filesystems: %s",
823 error_get_pretty(err
));
828 #endif /* CONFIG_FSFREEZE */
830 #if defined(CONFIG_FSTRIM)
832 * Walk list of mounted file systems in the guest, and trim them.
834 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**err
)
838 struct FsMount
*mount
;
840 Error
*local_err
= NULL
;
841 struct fstrim_range r
= {
844 .minlen
= has_minimum
? minimum
: 0,
847 slog("guest-fstrim called");
849 QTAILQ_INIT(&mounts
);
850 build_fs_mount_list(&mounts
, &local_err
);
851 if (error_is_set(&local_err
)) {
852 error_propagate(err
, local_err
);
856 QTAILQ_FOREACH(mount
, &mounts
, next
) {
857 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
859 error_setg_errno(err
, errno
, "failed to open %s", mount
->dirname
);
863 /* We try to cull filesytems we know won't work in advance, but other
864 * filesytems may not implement fstrim for less obvious reasons. These
865 * will report EOPNOTSUPP; we simply ignore these errors. Any other
866 * error means an unexpected error, so return it in those cases. In
867 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
869 ret
= ioctl(fd
, FITRIM
, &r
);
871 if (errno
!= ENOTTY
&& errno
!= EOPNOTSUPP
) {
872 error_setg_errno(err
, errno
, "failed to trim %s",
882 free_fs_mount_list(&mounts
);
884 #endif /* CONFIG_FSTRIM */
887 #define LINUX_SYS_STATE_FILE "/sys/power/state"
888 #define SUSPEND_SUPPORTED 0
889 #define SUSPEND_NOT_SUPPORTED 1
891 static void bios_supports_mode(const char *pmutils_bin
, const char *pmutils_arg
,
892 const char *sysfile_str
, Error
**err
)
894 Error
*local_err
= NULL
;
899 pmutils_path
= g_find_program_in_path(pmutils_bin
);
903 char buf
[32]; /* hopefully big enough */
908 reopen_fd_to_null(0);
909 reopen_fd_to_null(1);
910 reopen_fd_to_null(2);
913 execle(pmutils_path
, pmutils_bin
, pmutils_arg
, NULL
, environ
);
917 * If we get here either pm-utils is not installed or execle() has
918 * failed. Let's try the manual method if the caller wants it.
922 _exit(SUSPEND_NOT_SUPPORTED
);
925 fd
= open(LINUX_SYS_STATE_FILE
, O_RDONLY
);
927 _exit(SUSPEND_NOT_SUPPORTED
);
930 ret
= read(fd
, buf
, sizeof(buf
)-1);
932 _exit(SUSPEND_NOT_SUPPORTED
);
936 if (strstr(buf
, sysfile_str
)) {
937 _exit(SUSPEND_SUPPORTED
);
940 _exit(SUSPEND_NOT_SUPPORTED
);
941 } else if (pid
< 0) {
942 error_setg_errno(err
, errno
, "failed to create child process");
946 ga_wait_child(pid
, &status
, &local_err
);
947 if (error_is_set(&local_err
)) {
948 error_propagate(err
, local_err
);
952 if (!WIFEXITED(status
)) {
953 error_setg(err
, "child process has terminated abnormally");
957 switch (WEXITSTATUS(status
)) {
958 case SUSPEND_SUPPORTED
:
960 case SUSPEND_NOT_SUPPORTED
:
962 "the requested suspend mode is not supported by the guest");
966 "the helper program '%s' returned an unexpected exit status"
967 " code (%d)", pmutils_path
, WEXITSTATUS(status
));
972 g_free(pmutils_path
);
975 static void guest_suspend(const char *pmutils_bin
, const char *sysfile_str
,
978 Error
*local_err
= NULL
;
983 pmutils_path
= g_find_program_in_path(pmutils_bin
);
991 reopen_fd_to_null(0);
992 reopen_fd_to_null(1);
993 reopen_fd_to_null(2);
996 execle(pmutils_path
, pmutils_bin
, NULL
, environ
);
1000 * If we get here either pm-utils is not installed or execle() has
1001 * failed. Let's try the manual method if the caller wants it.
1005 _exit(EXIT_FAILURE
);
1008 fd
= open(LINUX_SYS_STATE_FILE
, O_WRONLY
);
1010 _exit(EXIT_FAILURE
);
1013 if (write(fd
, sysfile_str
, strlen(sysfile_str
)) < 0) {
1014 _exit(EXIT_FAILURE
);
1017 _exit(EXIT_SUCCESS
);
1018 } else if (pid
< 0) {
1019 error_setg_errno(err
, errno
, "failed to create child process");
1023 ga_wait_child(pid
, &status
, &local_err
);
1024 if (error_is_set(&local_err
)) {
1025 error_propagate(err
, local_err
);
1029 if (!WIFEXITED(status
)) {
1030 error_setg(err
, "child process has terminated abnormally");
1034 if (WEXITSTATUS(status
)) {
1035 error_setg(err
, "child process has failed to suspend");
1040 g_free(pmutils_path
);
1043 void qmp_guest_suspend_disk(Error
**err
)
1045 bios_supports_mode("pm-is-supported", "--hibernate", "disk", err
);
1046 if (error_is_set(err
)) {
1050 guest_suspend("pm-hibernate", "disk", err
);
1053 void qmp_guest_suspend_ram(Error
**err
)
1055 bios_supports_mode("pm-is-supported", "--suspend", "mem", err
);
1056 if (error_is_set(err
)) {
1060 guest_suspend("pm-suspend", "mem", err
);
1063 void qmp_guest_suspend_hybrid(Error
**err
)
1065 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL
, err
);
1066 if (error_is_set(err
)) {
1070 guest_suspend("pm-suspend-hybrid", NULL
, err
);
1073 static GuestNetworkInterfaceList
*
1074 guest_find_interface(GuestNetworkInterfaceList
*head
,
1077 for (; head
; head
= head
->next
) {
1078 if (strcmp(head
->value
->name
, name
) == 0) {
1087 * Build information about guest interfaces
1089 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1091 GuestNetworkInterfaceList
*head
= NULL
, *cur_item
= NULL
;
1092 struct ifaddrs
*ifap
, *ifa
;
1094 if (getifaddrs(&ifap
) < 0) {
1095 error_setg_errno(errp
, errno
, "getifaddrs failed");
1099 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1100 GuestNetworkInterfaceList
*info
;
1101 GuestIpAddressList
**address_list
= NULL
, *address_item
= NULL
;
1102 char addr4
[INET_ADDRSTRLEN
];
1103 char addr6
[INET6_ADDRSTRLEN
];
1106 unsigned char *mac_addr
;
1109 g_debug("Processing %s interface", ifa
->ifa_name
);
1111 info
= guest_find_interface(head
, ifa
->ifa_name
);
1114 info
= g_malloc0(sizeof(*info
));
1115 info
->value
= g_malloc0(sizeof(*info
->value
));
1116 info
->value
->name
= g_strdup(ifa
->ifa_name
);
1119 head
= cur_item
= info
;
1121 cur_item
->next
= info
;
1126 if (!info
->value
->has_hardware_address
&&
1127 ifa
->ifa_flags
& SIOCGIFHWADDR
) {
1128 /* we haven't obtained HW address yet */
1129 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1131 error_setg_errno(errp
, errno
, "failed to create socket");
1135 memset(&ifr
, 0, sizeof(ifr
));
1136 pstrcpy(ifr
.ifr_name
, IF_NAMESIZE
, info
->value
->name
);
1137 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) == -1) {
1138 error_setg_errno(errp
, errno
,
1139 "failed to get MAC address of %s",
1146 mac_addr
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
1148 info
->value
->hardware_address
=
1149 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1150 (int) mac_addr
[0], (int) mac_addr
[1],
1151 (int) mac_addr
[2], (int) mac_addr
[3],
1152 (int) mac_addr
[4], (int) mac_addr
[5]);
1154 info
->value
->has_hardware_address
= true;
1157 if (ifa
->ifa_addr
&&
1158 ifa
->ifa_addr
->sa_family
== AF_INET
) {
1159 /* interface with IPv4 address */
1160 p
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
1161 if (!inet_ntop(AF_INET
, p
, addr4
, sizeof(addr4
))) {
1162 error_setg_errno(errp
, errno
, "inet_ntop failed");
1166 address_item
= g_malloc0(sizeof(*address_item
));
1167 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1168 address_item
->value
->ip_address
= g_strdup(addr4
);
1169 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV4
;
1171 if (ifa
->ifa_netmask
) {
1172 /* Count the number of set bits in netmask.
1173 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1174 p
= &((struct sockaddr_in
*)ifa
->ifa_netmask
)->sin_addr
;
1175 address_item
->value
->prefix
= ctpop32(((uint32_t *) p
)[0]);
1177 } else if (ifa
->ifa_addr
&&
1178 ifa
->ifa_addr
->sa_family
== AF_INET6
) {
1179 /* interface with IPv6 address */
1180 p
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
1181 if (!inet_ntop(AF_INET6
, p
, addr6
, sizeof(addr6
))) {
1182 error_setg_errno(errp
, errno
, "inet_ntop failed");
1186 address_item
= g_malloc0(sizeof(*address_item
));
1187 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1188 address_item
->value
->ip_address
= g_strdup(addr6
);
1189 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV6
;
1191 if (ifa
->ifa_netmask
) {
1192 /* Count the number of set bits in netmask.
1193 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1194 p
= &((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
1195 address_item
->value
->prefix
=
1196 ctpop32(((uint32_t *) p
)[0]) +
1197 ctpop32(((uint32_t *) p
)[1]) +
1198 ctpop32(((uint32_t *) p
)[2]) +
1199 ctpop32(((uint32_t *) p
)[3]);
1203 if (!address_item
) {
1207 address_list
= &info
->value
->ip_addresses
;
1209 while (*address_list
&& (*address_list
)->next
) {
1210 address_list
= &(*address_list
)->next
;
1213 if (!*address_list
) {
1214 *address_list
= address_item
;
1216 (*address_list
)->next
= address_item
;
1219 info
->value
->has_ip_addresses
= true;
1229 qapi_free_GuestNetworkInterfaceList(head
);
1233 #define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1235 static long sysconf_exact(int name
, const char *name_str
, Error
**err
)
1240 ret
= sysconf(name
);
1243 error_setg(err
, "sysconf(%s): value indefinite", name_str
);
1245 error_setg_errno(err
, errno
, "sysconf(%s)", name_str
);
1251 /* Transfer online/offline status between @vcpu and the guest system.
1253 * On input either @errp or *@errp must be NULL.
1255 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1256 * - R: vcpu->logical_id
1258 * - W: vcpu->can_offline
1260 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1261 * - R: vcpu->logical_id
1264 * Written members remain unmodified on error.
1266 static void transfer_vcpu(GuestLogicalProcessor
*vcpu
, bool sys2vcpu
,
1272 dirpath
= g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64
"/",
1274 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
1276 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
1278 static const char fn
[] = "online";
1282 fd
= openat(dirfd
, fn
, sys2vcpu
? O_RDONLY
: O_RDWR
);
1284 if (errno
!= ENOENT
) {
1285 error_setg_errno(errp
, errno
, "open(\"%s/%s\")", dirpath
, fn
);
1286 } else if (sys2vcpu
) {
1287 vcpu
->online
= true;
1288 vcpu
->can_offline
= false;
1289 } else if (!vcpu
->online
) {
1290 error_setg(errp
, "logical processor #%" PRId64
" can't be "
1291 "offlined", vcpu
->logical_id
);
1292 } /* otherwise pretend successful re-onlining */
1294 unsigned char status
;
1296 res
= pread(fd
, &status
, 1, 0);
1298 error_setg_errno(errp
, errno
, "pread(\"%s/%s\")", dirpath
, fn
);
1299 } else if (res
== 0) {
1300 error_setg(errp
, "pread(\"%s/%s\"): unexpected EOF", dirpath
,
1302 } else if (sys2vcpu
) {
1303 vcpu
->online
= (status
!= '0');
1304 vcpu
->can_offline
= true;
1305 } else if (vcpu
->online
!= (status
!= '0')) {
1306 status
= '0' + vcpu
->online
;
1307 if (pwrite(fd
, &status
, 1, 0) == -1) {
1308 error_setg_errno(errp
, errno
, "pwrite(\"%s/%s\")", dirpath
,
1311 } /* otherwise pretend successful re-(on|off)-lining */
1324 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1327 GuestLogicalProcessorList
*head
, **link
;
1329 Error
*local_err
= NULL
;
1334 sc_max
= SYSCONF_EXACT(_SC_NPROCESSORS_CONF
, &local_err
);
1336 while (local_err
== NULL
&& current
< sc_max
) {
1337 GuestLogicalProcessor
*vcpu
;
1338 GuestLogicalProcessorList
*entry
;
1340 vcpu
= g_malloc0(sizeof *vcpu
);
1341 vcpu
->logical_id
= current
++;
1342 vcpu
->has_can_offline
= true; /* lolspeak ftw */
1343 transfer_vcpu(vcpu
, true, &local_err
);
1345 entry
= g_malloc0(sizeof *entry
);
1346 entry
->value
= vcpu
;
1349 link
= &entry
->next
;
1352 if (local_err
== NULL
) {
1353 /* there's no guest with zero VCPUs */
1354 g_assert(head
!= NULL
);
1358 qapi_free_GuestLogicalProcessorList(head
);
1359 error_propagate(errp
, local_err
);
1363 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1366 Error
*local_err
= NULL
;
1369 while (vcpus
!= NULL
) {
1370 transfer_vcpu(vcpus
->value
, false, &local_err
);
1371 if (local_err
!= NULL
) {
1375 vcpus
= vcpus
->next
;
1378 if (local_err
!= NULL
) {
1379 if (processed
== 0) {
1380 error_propagate(errp
, local_err
);
1382 error_free(local_err
);
1389 #else /* defined(__linux__) */
1391 void qmp_guest_suspend_disk(Error
**err
)
1393 error_set(err
, QERR_UNSUPPORTED
);
1396 void qmp_guest_suspend_ram(Error
**err
)
1398 error_set(err
, QERR_UNSUPPORTED
);
1401 void qmp_guest_suspend_hybrid(Error
**err
)
1403 error_set(err
, QERR_UNSUPPORTED
);
1406 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1408 error_set(errp
, QERR_UNSUPPORTED
);
1412 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1414 error_set(errp
, QERR_UNSUPPORTED
);
1418 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1420 error_set(errp
, QERR_UNSUPPORTED
);
1426 #if !defined(CONFIG_FSFREEZE)
1428 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
1430 error_set(err
, QERR_UNSUPPORTED
);
1435 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
1437 error_set(err
, QERR_UNSUPPORTED
);
1442 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
1444 error_set(err
, QERR_UNSUPPORTED
);
1448 #endif /* CONFIG_FSFREEZE */
1450 #if !defined(CONFIG_FSTRIM)
1451 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**err
)
1453 error_set(err
, QERR_UNSUPPORTED
);
1457 /* register init/cleanup routines for stateful command groups */
1458 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
1460 #if defined(CONFIG_FSFREEZE)
1461 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);
1463 ga_command_state_add(cs
, guest_file_init
, NULL
);