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
**errp
)
63 rpid
= waitpid(pid
, status
, 0);
64 } while (rpid
== -1 && errno
== EINTR
);
67 error_setg_errno(errp
, errno
, "failed to wait for child (pid: %d)",
72 g_assert(rpid
== pid
);
75 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**errp
)
77 const char *shutdown_flag
;
78 Error
*local_err
= NULL
;
82 slog("guest-shutdown called, mode: %s", mode
);
83 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
85 } else if (strcmp(mode
, "halt") == 0) {
87 } else if (strcmp(mode
, "reboot") == 0) {
91 "mode is invalid (valid values are: halt|powerdown|reboot");
97 /* child, start the shutdown */
100 reopen_fd_to_null(1);
101 reopen_fd_to_null(2);
103 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag
, "+0",
104 "hypervisor initiated shutdown", (char*)NULL
, environ
);
106 } else if (pid
< 0) {
107 error_setg_errno(errp
, errno
, "failed to create child process");
111 ga_wait_child(pid
, &status
, &local_err
);
113 error_propagate(errp
, local_err
);
117 if (!WIFEXITED(status
)) {
118 error_setg(errp
, "child process has terminated abnormally");
122 if (WEXITSTATUS(status
)) {
123 error_setg(errp
, "child process has failed to shutdown");
130 int64_t qmp_guest_get_time(Error
**errp
)
136 ret
= qemu_gettimeofday(&tq
);
138 error_setg_errno(errp
, errno
, "Failed to get time");
142 time_ns
= tq
.tv_sec
* 1000000000LL + tq
.tv_usec
* 1000;
146 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
151 Error
*local_err
= NULL
;
154 /* If user has passed a time, validate and set it. */
156 /* year-2038 will overflow in case time_t is 32bit */
157 if (time_ns
/ 1000000000 != (time_t)(time_ns
/ 1000000000)) {
158 error_setg(errp
, "Time %" PRId64
" is too large", time_ns
);
162 tv
.tv_sec
= time_ns
/ 1000000000;
163 tv
.tv_usec
= (time_ns
% 1000000000) / 1000;
165 ret
= settimeofday(&tv
, NULL
);
167 error_setg_errno(errp
, errno
, "Failed to set time to guest");
172 /* Now, if user has passed a time to set and the system time is set, we
173 * just need to synchronize the hardware clock. However, if no time was
174 * passed, user is requesting the opposite: set the system time from the
175 * hardware clock (RTC). */
179 reopen_fd_to_null(0);
180 reopen_fd_to_null(1);
181 reopen_fd_to_null(2);
183 /* Use '/sbin/hwclock -w' to set RTC from the system time,
184 * or '/sbin/hwclock -s' to set the system time from RTC. */
185 execle("/sbin/hwclock", "hwclock", has_time
? "-w" : "-s",
188 } else if (pid
< 0) {
189 error_setg_errno(errp
, errno
, "failed to create child process");
193 ga_wait_child(pid
, &status
, &local_err
);
195 error_propagate(errp
, local_err
);
199 if (!WIFEXITED(status
)) {
200 error_setg(errp
, "child process has terminated abnormally");
204 if (WEXITSTATUS(status
)) {
205 error_setg(errp
, "hwclock failed to set hardware clock to system time");
210 typedef struct GuestFileHandle
{
213 QTAILQ_ENTRY(GuestFileHandle
) next
;
217 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
220 static int64_t guest_file_handle_add(FILE *fh
, Error
**errp
)
222 GuestFileHandle
*gfh
;
225 handle
= ga_get_fd_handle(ga_state
, errp
);
230 gfh
= g_malloc0(sizeof(GuestFileHandle
));
233 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
238 static GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**errp
)
240 GuestFileHandle
*gfh
;
242 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
249 error_setg(errp
, "handle '%" PRId64
"' has not been found", id
);
253 typedef const char * const ccpc
;
259 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
260 static const struct {
263 } guest_file_open_modes
[] = {
264 { (ccpc
[]){ "r", NULL
}, O_RDONLY
},
265 { (ccpc
[]){ "rb", NULL
}, O_RDONLY
| O_BINARY
},
266 { (ccpc
[]){ "w", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
},
267 { (ccpc
[]){ "wb", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
},
268 { (ccpc
[]){ "a", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
},
269 { (ccpc
[]){ "ab", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
},
270 { (ccpc
[]){ "r+", NULL
}, O_RDWR
},
271 { (ccpc
[]){ "rb+", "r+b", NULL
}, O_RDWR
| O_BINARY
},
272 { (ccpc
[]){ "w+", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
},
273 { (ccpc
[]){ "wb+", "w+b", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
},
274 { (ccpc
[]){ "a+", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
},
275 { (ccpc
[]){ "ab+", "a+b", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
}
279 find_open_flag(const char *mode_str
, Error
**errp
)
283 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
286 form
= guest_file_open_modes
[mode
].forms
;
287 while (*form
!= NULL
&& strcmp(*form
, mode_str
) != 0) {
295 if (mode
== ARRAY_SIZE(guest_file_open_modes
)) {
296 error_setg(errp
, "invalid file open mode '%s'", mode_str
);
299 return guest_file_open_modes
[mode
].oflag_base
| O_NOCTTY
| O_NONBLOCK
;
302 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
303 S_IRGRP | S_IWGRP | \
307 safe_open_or_create(const char *path
, const char *mode
, Error
**errp
)
309 Error
*local_err
= NULL
;
312 oflag
= find_open_flag(mode
, &local_err
);
313 if (local_err
== NULL
) {
316 /* If the caller wants / allows creation of a new file, we implement it
317 * with a two step process: open() + (open() / fchmod()).
319 * First we insist on creating the file exclusively as a new file. If
320 * that succeeds, we're free to set any file-mode bits on it. (The
321 * motivation is that we want to set those file-mode bits independently
322 * of the current umask.)
324 * If the exclusive creation fails because the file already exists
325 * (EEXIST is not possible for any other reason), we just attempt to
326 * open the file, but in this case we won't be allowed to change the
327 * file-mode bits on the preexistent file.
329 * The pathname should never disappear between the two open()s in
330 * practice. If it happens, then someone very likely tried to race us.
331 * In this case just go ahead and report the ENOENT from the second
332 * open() to the caller.
334 * If the caller wants to open a preexistent file, then the first
335 * open() is decisive and its third argument is ignored, and the second
336 * open() and the fchmod() are never called.
338 fd
= open(path
, oflag
| ((oflag
& O_CREAT
) ? O_EXCL
: 0), 0);
339 if (fd
== -1 && errno
== EEXIST
) {
340 oflag
&= ~(unsigned)O_CREAT
;
341 fd
= open(path
, oflag
);
345 error_setg_errno(&local_err
, errno
, "failed to open file '%s' "
346 "(mode: '%s')", path
, mode
);
348 qemu_set_cloexec(fd
);
350 if ((oflag
& O_CREAT
) && fchmod(fd
, DEFAULT_NEW_FILE_MODE
) == -1) {
351 error_setg_errno(&local_err
, errno
, "failed to set permission "
352 "0%03o on new file '%s' (mode: '%s')",
353 (unsigned)DEFAULT_NEW_FILE_MODE
, path
, mode
);
357 f
= fdopen(fd
, mode
);
359 error_setg_errno(&local_err
, errno
, "failed to associate "
360 "stdio stream with file descriptor %d, "
361 "file '%s' (mode: '%s')", fd
, path
, mode
);
368 if (oflag
& O_CREAT
) {
374 error_propagate(errp
, local_err
);
378 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
,
382 Error
*local_err
= NULL
;
384 int64_t ret
= -1, handle
;
389 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
390 fh
= safe_open_or_create(path
, mode
, &local_err
);
391 if (local_err
!= NULL
) {
392 error_propagate(errp
, local_err
);
396 /* set fd non-blocking to avoid common use cases (like reading from a
397 * named pipe) from hanging the agent
400 ret
= fcntl(fd
, F_GETFL
);
401 ret
= fcntl(fd
, F_SETFL
, ret
| O_NONBLOCK
);
403 error_setg_errno(errp
, errno
, "failed to make file '%s' non-blocking",
409 handle
= guest_file_handle_add(fh
, errp
);
415 slog("guest-file-open, handle: %" PRId64
, handle
);
419 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
421 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
424 slog("guest-file-close called, handle: %" PRId64
, handle
);
429 ret
= fclose(gfh
->fh
);
431 error_setg_errno(errp
, errno
, "failed to close handle");
435 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
439 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
440 int64_t count
, Error
**errp
)
442 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
443 GuestFileRead
*read_data
= NULL
;
453 count
= QGA_READ_COUNT_DEFAULT
;
454 } else if (count
< 0) {
455 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
461 buf
= g_malloc0(count
+1);
462 read_count
= fread(buf
, 1, count
, fh
);
464 error_setg_errno(errp
, errno
, "failed to read file");
465 slog("guest-file-read failed, handle: %" PRId64
, handle
);
468 read_data
= g_malloc0(sizeof(GuestFileRead
));
469 read_data
->count
= read_count
;
470 read_data
->eof
= feof(fh
);
472 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
481 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
482 bool has_count
, int64_t count
,
485 GuestFileWrite
*write_data
= NULL
;
489 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
497 buf
= g_base64_decode(buf_b64
, &buf_len
);
501 } else if (count
< 0 || count
> buf_len
) {
502 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
508 write_count
= fwrite(buf
, 1, count
, fh
);
510 error_setg_errno(errp
, errno
, "failed to write to file");
511 slog("guest-file-write failed, handle: %" PRId64
, handle
);
513 write_data
= g_malloc0(sizeof(GuestFileWrite
));
514 write_data
->count
= write_count
;
515 write_data
->eof
= feof(fh
);
523 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
524 int64_t whence
, Error
**errp
)
526 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
527 GuestFileSeek
*seek_data
= NULL
;
536 ret
= fseek(fh
, offset
, whence
);
538 error_setg_errno(errp
, errno
, "failed to seek file");
540 seek_data
= g_new0(GuestFileSeek
, 1);
541 seek_data
->position
= ftell(fh
);
542 seek_data
->eof
= feof(fh
);
549 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
551 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
562 error_setg_errno(errp
, errno
, "failed to flush file");
566 static void guest_file_init(void)
568 QTAILQ_INIT(&guest_file_state
.filehandles
);
571 /* linux-specific implementations. avoid this if at all possible. */
572 #if defined(__linux__)
574 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
575 typedef struct FsMount
{
578 QTAILQ_ENTRY(FsMount
) next
;
581 typedef QTAILQ_HEAD(FsMountList
, FsMount
) FsMountList
;
583 static void free_fs_mount_list(FsMountList
*mounts
)
585 FsMount
*mount
, *temp
;
591 QTAILQ_FOREACH_SAFE(mount
, mounts
, next
, temp
) {
592 QTAILQ_REMOVE(mounts
, mount
, next
);
593 g_free(mount
->dirname
);
594 g_free(mount
->devtype
);
600 * Walk the mount table and build a list of local file systems
602 static void build_fs_mount_list(FsMountList
*mounts
, Error
**errp
)
606 char const *mtab
= "/proc/self/mounts";
609 fp
= setmntent(mtab
, "r");
611 error_setg(errp
, "failed to open mtab file: '%s'", mtab
);
615 while ((ment
= getmntent(fp
))) {
617 * An entry which device name doesn't start with a '/' is
618 * either a dummy file system or a network file system.
619 * Add special handling for smbfs and cifs as is done by
622 if ((ment
->mnt_fsname
[0] != '/') ||
623 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
624 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
628 mount
= g_malloc0(sizeof(FsMount
));
629 mount
->dirname
= g_strdup(ment
->mnt_dir
);
630 mount
->devtype
= g_strdup(ment
->mnt_type
);
632 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
639 #if defined(CONFIG_FSFREEZE)
642 FSFREEZE_HOOK_THAW
= 0,
643 FSFREEZE_HOOK_FREEZE
,
646 static const char *fsfreeze_hook_arg_string
[] = {
651 static void execute_fsfreeze_hook(FsfreezeHookArg arg
, Error
**errp
)
656 const char *arg_str
= fsfreeze_hook_arg_string
[arg
];
657 Error
*local_err
= NULL
;
659 hook
= ga_fsfreeze_hook(ga_state
);
663 if (access(hook
, X_OK
) != 0) {
664 error_setg_errno(errp
, errno
, "can't access fsfreeze hook '%s'", hook
);
668 slog("executing fsfreeze hook with arg '%s'", arg_str
);
672 reopen_fd_to_null(0);
673 reopen_fd_to_null(1);
674 reopen_fd_to_null(2);
676 execle(hook
, hook
, arg_str
, NULL
, environ
);
678 } else if (pid
< 0) {
679 error_setg_errno(errp
, errno
, "failed to create child process");
683 ga_wait_child(pid
, &status
, &local_err
);
685 error_propagate(errp
, local_err
);
689 if (!WIFEXITED(status
)) {
690 error_setg(errp
, "fsfreeze hook has terminated abnormally");
694 status
= WEXITSTATUS(status
);
696 error_setg(errp
, "fsfreeze hook has failed with status %d", status
);
702 * Return status of freeze/thaw
704 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
706 if (ga_is_frozen(ga_state
)) {
707 return GUEST_FSFREEZE_STATUS_FROZEN
;
710 return GUEST_FSFREEZE_STATUS_THAWED
;
713 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
715 return qmp_guest_fsfreeze_freeze_list(false, NULL
, errp
);
719 * Walk list of mounted file systems in the guest, and freeze the ones which
720 * are real local file systems.
722 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
723 strList
*mountpoints
,
729 struct FsMount
*mount
;
730 Error
*local_err
= NULL
;
733 slog("guest-fsfreeze called");
735 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE
, &local_err
);
737 error_propagate(errp
, local_err
);
741 QTAILQ_INIT(&mounts
);
742 build_fs_mount_list(&mounts
, &local_err
);
744 error_propagate(errp
, local_err
);
748 /* cannot risk guest agent blocking itself on a write in this state */
749 ga_set_frozen(ga_state
);
751 QTAILQ_FOREACH_REVERSE(mount
, &mounts
, FsMountList
, next
) {
752 /* To issue fsfreeze in the reverse order of mounts, check if the
753 * mount is listed in the list here */
754 if (has_mountpoints
) {
755 for (list
= mountpoints
; list
; list
= list
->next
) {
756 if (strcmp(list
->value
, mount
->dirname
) == 0) {
765 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
767 error_setg_errno(errp
, errno
, "failed to open %s", mount
->dirname
);
771 /* we try to cull filesytems we know won't work in advance, but other
772 * filesytems may not implement fsfreeze for less obvious reasons.
773 * these will report EOPNOTSUPP. we simply ignore these when tallying
774 * the number of frozen filesystems.
776 * any other error means a failure to freeze a filesystem we
777 * expect to be freezable, so return an error in those cases
778 * and return system to thawed state.
780 ret
= ioctl(fd
, FIFREEZE
);
782 if (errno
!= EOPNOTSUPP
) {
783 error_setg_errno(errp
, errno
, "failed to freeze %s",
794 free_fs_mount_list(&mounts
);
798 free_fs_mount_list(&mounts
);
799 qmp_guest_fsfreeze_thaw(NULL
);
804 * Walk list of frozen file systems in the guest, and thaw them.
806 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
811 int fd
, i
= 0, logged
;
812 Error
*local_err
= NULL
;
814 QTAILQ_INIT(&mounts
);
815 build_fs_mount_list(&mounts
, &local_err
);
817 error_propagate(errp
, local_err
);
821 QTAILQ_FOREACH(mount
, &mounts
, next
) {
823 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
827 /* we have no way of knowing whether a filesystem was actually unfrozen
828 * as a result of a successful call to FITHAW, only that if an error
829 * was returned the filesystem was *not* unfrozen by that particular
832 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
833 * to unfreeze, continuing issuing FITHAW until an error is returned,
834 * in which case either the filesystem is in an unfreezable state, or,
835 * more likely, it was thawed previously (and remains so afterward).
837 * also, since the most recent successful call is the one that did
838 * the actual unfreeze, we can use this to provide an accurate count
839 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
840 * may * be useful for determining whether a filesystem was unfrozen
841 * during the freeze/thaw phase by a process other than qemu-ga.
844 ret
= ioctl(fd
, FITHAW
);
845 if (ret
== 0 && !logged
) {
853 ga_unset_frozen(ga_state
);
854 free_fs_mount_list(&mounts
);
856 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW
, errp
);
861 static void guest_fsfreeze_cleanup(void)
865 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
866 qmp_guest_fsfreeze_thaw(&err
);
868 slog("failed to clean up frozen filesystems: %s",
869 error_get_pretty(err
));
874 #endif /* CONFIG_FSFREEZE */
876 #if defined(CONFIG_FSTRIM)
878 * Walk list of mounted file systems in the guest, and trim them.
880 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
884 struct FsMount
*mount
;
886 Error
*local_err
= NULL
;
887 struct fstrim_range r
= {
890 .minlen
= has_minimum
? minimum
: 0,
893 slog("guest-fstrim called");
895 QTAILQ_INIT(&mounts
);
896 build_fs_mount_list(&mounts
, &local_err
);
898 error_propagate(errp
, local_err
);
902 QTAILQ_FOREACH(mount
, &mounts
, next
) {
903 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
905 error_setg_errno(errp
, errno
, "failed to open %s", mount
->dirname
);
909 /* We try to cull filesytems we know won't work in advance, but other
910 * filesytems may not implement fstrim for less obvious reasons. These
911 * will report EOPNOTSUPP; we simply ignore these errors. Any other
912 * error means an unexpected error, so return it in those cases. In
913 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
915 ret
= ioctl(fd
, FITRIM
, &r
);
917 if (errno
!= ENOTTY
&& errno
!= EOPNOTSUPP
) {
918 error_setg_errno(errp
, errno
, "failed to trim %s",
928 free_fs_mount_list(&mounts
);
930 #endif /* CONFIG_FSTRIM */
933 #define LINUX_SYS_STATE_FILE "/sys/power/state"
934 #define SUSPEND_SUPPORTED 0
935 #define SUSPEND_NOT_SUPPORTED 1
937 static void bios_supports_mode(const char *pmutils_bin
, const char *pmutils_arg
,
938 const char *sysfile_str
, Error
**errp
)
940 Error
*local_err
= NULL
;
945 pmutils_path
= g_find_program_in_path(pmutils_bin
);
949 char buf
[32]; /* hopefully big enough */
954 reopen_fd_to_null(0);
955 reopen_fd_to_null(1);
956 reopen_fd_to_null(2);
959 execle(pmutils_path
, pmutils_bin
, pmutils_arg
, NULL
, environ
);
963 * If we get here either pm-utils is not installed or execle() has
964 * failed. Let's try the manual method if the caller wants it.
968 _exit(SUSPEND_NOT_SUPPORTED
);
971 fd
= open(LINUX_SYS_STATE_FILE
, O_RDONLY
);
973 _exit(SUSPEND_NOT_SUPPORTED
);
976 ret
= read(fd
, buf
, sizeof(buf
)-1);
978 _exit(SUSPEND_NOT_SUPPORTED
);
982 if (strstr(buf
, sysfile_str
)) {
983 _exit(SUSPEND_SUPPORTED
);
986 _exit(SUSPEND_NOT_SUPPORTED
);
987 } else if (pid
< 0) {
988 error_setg_errno(errp
, errno
, "failed to create child process");
992 ga_wait_child(pid
, &status
, &local_err
);
994 error_propagate(errp
, local_err
);
998 if (!WIFEXITED(status
)) {
999 error_setg(errp
, "child process has terminated abnormally");
1003 switch (WEXITSTATUS(status
)) {
1004 case SUSPEND_SUPPORTED
:
1006 case SUSPEND_NOT_SUPPORTED
:
1008 "the requested suspend mode is not supported by the guest");
1012 "the helper program '%s' returned an unexpected exit status"
1013 " code (%d)", pmutils_path
, WEXITSTATUS(status
));
1018 g_free(pmutils_path
);
1021 static void guest_suspend(const char *pmutils_bin
, const char *sysfile_str
,
1024 Error
*local_err
= NULL
;
1029 pmutils_path
= g_find_program_in_path(pmutils_bin
);
1037 reopen_fd_to_null(0);
1038 reopen_fd_to_null(1);
1039 reopen_fd_to_null(2);
1042 execle(pmutils_path
, pmutils_bin
, NULL
, environ
);
1046 * If we get here either pm-utils is not installed or execle() has
1047 * failed. Let's try the manual method if the caller wants it.
1051 _exit(EXIT_FAILURE
);
1054 fd
= open(LINUX_SYS_STATE_FILE
, O_WRONLY
);
1056 _exit(EXIT_FAILURE
);
1059 if (write(fd
, sysfile_str
, strlen(sysfile_str
)) < 0) {
1060 _exit(EXIT_FAILURE
);
1063 _exit(EXIT_SUCCESS
);
1064 } else if (pid
< 0) {
1065 error_setg_errno(errp
, errno
, "failed to create child process");
1069 ga_wait_child(pid
, &status
, &local_err
);
1071 error_propagate(errp
, local_err
);
1075 if (!WIFEXITED(status
)) {
1076 error_setg(errp
, "child process has terminated abnormally");
1080 if (WEXITSTATUS(status
)) {
1081 error_setg(errp
, "child process has failed to suspend");
1086 g_free(pmutils_path
);
1089 void qmp_guest_suspend_disk(Error
**errp
)
1091 Error
*local_err
= NULL
;
1093 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err
);
1095 error_propagate(errp
, local_err
);
1099 guest_suspend("pm-hibernate", "disk", errp
);
1102 void qmp_guest_suspend_ram(Error
**errp
)
1104 Error
*local_err
= NULL
;
1106 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err
);
1108 error_propagate(errp
, local_err
);
1112 guest_suspend("pm-suspend", "mem", errp
);
1115 void qmp_guest_suspend_hybrid(Error
**errp
)
1117 Error
*local_err
= NULL
;
1119 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL
,
1122 error_propagate(errp
, local_err
);
1126 guest_suspend("pm-suspend-hybrid", NULL
, errp
);
1129 static GuestNetworkInterfaceList
*
1130 guest_find_interface(GuestNetworkInterfaceList
*head
,
1133 for (; head
; head
= head
->next
) {
1134 if (strcmp(head
->value
->name
, name
) == 0) {
1143 * Build information about guest interfaces
1145 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1147 GuestNetworkInterfaceList
*head
= NULL
, *cur_item
= NULL
;
1148 struct ifaddrs
*ifap
, *ifa
;
1150 if (getifaddrs(&ifap
) < 0) {
1151 error_setg_errno(errp
, errno
, "getifaddrs failed");
1155 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1156 GuestNetworkInterfaceList
*info
;
1157 GuestIpAddressList
**address_list
= NULL
, *address_item
= NULL
;
1158 char addr4
[INET_ADDRSTRLEN
];
1159 char addr6
[INET6_ADDRSTRLEN
];
1162 unsigned char *mac_addr
;
1165 g_debug("Processing %s interface", ifa
->ifa_name
);
1167 info
= guest_find_interface(head
, ifa
->ifa_name
);
1170 info
= g_malloc0(sizeof(*info
));
1171 info
->value
= g_malloc0(sizeof(*info
->value
));
1172 info
->value
->name
= g_strdup(ifa
->ifa_name
);
1175 head
= cur_item
= info
;
1177 cur_item
->next
= info
;
1182 if (!info
->value
->has_hardware_address
&&
1183 ifa
->ifa_flags
& SIOCGIFHWADDR
) {
1184 /* we haven't obtained HW address yet */
1185 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1187 error_setg_errno(errp
, errno
, "failed to create socket");
1191 memset(&ifr
, 0, sizeof(ifr
));
1192 pstrcpy(ifr
.ifr_name
, IF_NAMESIZE
, info
->value
->name
);
1193 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) == -1) {
1194 error_setg_errno(errp
, errno
,
1195 "failed to get MAC address of %s",
1202 mac_addr
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
1204 info
->value
->hardware_address
=
1205 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1206 (int) mac_addr
[0], (int) mac_addr
[1],
1207 (int) mac_addr
[2], (int) mac_addr
[3],
1208 (int) mac_addr
[4], (int) mac_addr
[5]);
1210 info
->value
->has_hardware_address
= true;
1213 if (ifa
->ifa_addr
&&
1214 ifa
->ifa_addr
->sa_family
== AF_INET
) {
1215 /* interface with IPv4 address */
1216 p
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
1217 if (!inet_ntop(AF_INET
, p
, addr4
, sizeof(addr4
))) {
1218 error_setg_errno(errp
, errno
, "inet_ntop failed");
1222 address_item
= g_malloc0(sizeof(*address_item
));
1223 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1224 address_item
->value
->ip_address
= g_strdup(addr4
);
1225 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV4
;
1227 if (ifa
->ifa_netmask
) {
1228 /* Count the number of set bits in netmask.
1229 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1230 p
= &((struct sockaddr_in
*)ifa
->ifa_netmask
)->sin_addr
;
1231 address_item
->value
->prefix
= ctpop32(((uint32_t *) p
)[0]);
1233 } else if (ifa
->ifa_addr
&&
1234 ifa
->ifa_addr
->sa_family
== AF_INET6
) {
1235 /* interface with IPv6 address */
1236 p
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
1237 if (!inet_ntop(AF_INET6
, p
, addr6
, sizeof(addr6
))) {
1238 error_setg_errno(errp
, errno
, "inet_ntop failed");
1242 address_item
= g_malloc0(sizeof(*address_item
));
1243 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1244 address_item
->value
->ip_address
= g_strdup(addr6
);
1245 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV6
;
1247 if (ifa
->ifa_netmask
) {
1248 /* Count the number of set bits in netmask.
1249 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1250 p
= &((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
1251 address_item
->value
->prefix
=
1252 ctpop32(((uint32_t *) p
)[0]) +
1253 ctpop32(((uint32_t *) p
)[1]) +
1254 ctpop32(((uint32_t *) p
)[2]) +
1255 ctpop32(((uint32_t *) p
)[3]);
1259 if (!address_item
) {
1263 address_list
= &info
->value
->ip_addresses
;
1265 while (*address_list
&& (*address_list
)->next
) {
1266 address_list
= &(*address_list
)->next
;
1269 if (!*address_list
) {
1270 *address_list
= address_item
;
1272 (*address_list
)->next
= address_item
;
1275 info
->value
->has_ip_addresses
= true;
1285 qapi_free_GuestNetworkInterfaceList(head
);
1289 #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
1291 static long sysconf_exact(int name
, const char *name_str
, Error
**errp
)
1296 ret
= sysconf(name
);
1299 error_setg(errp
, "sysconf(%s): value indefinite", name_str
);
1301 error_setg_errno(errp
, errno
, "sysconf(%s)", name_str
);
1307 /* Transfer online/offline status between @vcpu and the guest system.
1309 * On input either @errp or *@errp must be NULL.
1311 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1312 * - R: vcpu->logical_id
1314 * - W: vcpu->can_offline
1316 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1317 * - R: vcpu->logical_id
1320 * Written members remain unmodified on error.
1322 static void transfer_vcpu(GuestLogicalProcessor
*vcpu
, bool sys2vcpu
,
1328 dirpath
= g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64
"/",
1330 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
1332 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
1334 static const char fn
[] = "online";
1338 fd
= openat(dirfd
, fn
, sys2vcpu
? O_RDONLY
: O_RDWR
);
1340 if (errno
!= ENOENT
) {
1341 error_setg_errno(errp
, errno
, "open(\"%s/%s\")", dirpath
, fn
);
1342 } else if (sys2vcpu
) {
1343 vcpu
->online
= true;
1344 vcpu
->can_offline
= false;
1345 } else if (!vcpu
->online
) {
1346 error_setg(errp
, "logical processor #%" PRId64
" can't be "
1347 "offlined", vcpu
->logical_id
);
1348 } /* otherwise pretend successful re-onlining */
1350 unsigned char status
;
1352 res
= pread(fd
, &status
, 1, 0);
1354 error_setg_errno(errp
, errno
, "pread(\"%s/%s\")", dirpath
, fn
);
1355 } else if (res
== 0) {
1356 error_setg(errp
, "pread(\"%s/%s\"): unexpected EOF", dirpath
,
1358 } else if (sys2vcpu
) {
1359 vcpu
->online
= (status
!= '0');
1360 vcpu
->can_offline
= true;
1361 } else if (vcpu
->online
!= (status
!= '0')) {
1362 status
= '0' + vcpu
->online
;
1363 if (pwrite(fd
, &status
, 1, 0) == -1) {
1364 error_setg_errno(errp
, errno
, "pwrite(\"%s/%s\")", dirpath
,
1367 } /* otherwise pretend successful re-(on|off)-lining */
1380 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1383 GuestLogicalProcessorList
*head
, **link
;
1385 Error
*local_err
= NULL
;
1390 sc_max
= SYSCONF_EXACT(_SC_NPROCESSORS_CONF
, &local_err
);
1392 while (local_err
== NULL
&& current
< sc_max
) {
1393 GuestLogicalProcessor
*vcpu
;
1394 GuestLogicalProcessorList
*entry
;
1396 vcpu
= g_malloc0(sizeof *vcpu
);
1397 vcpu
->logical_id
= current
++;
1398 vcpu
->has_can_offline
= true; /* lolspeak ftw */
1399 transfer_vcpu(vcpu
, true, &local_err
);
1401 entry
= g_malloc0(sizeof *entry
);
1402 entry
->value
= vcpu
;
1405 link
= &entry
->next
;
1408 if (local_err
== NULL
) {
1409 /* there's no guest with zero VCPUs */
1410 g_assert(head
!= NULL
);
1414 qapi_free_GuestLogicalProcessorList(head
);
1415 error_propagate(errp
, local_err
);
1419 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1422 Error
*local_err
= NULL
;
1425 while (vcpus
!= NULL
) {
1426 transfer_vcpu(vcpus
->value
, false, &local_err
);
1427 if (local_err
!= NULL
) {
1431 vcpus
= vcpus
->next
;
1434 if (local_err
!= NULL
) {
1435 if (processed
== 0) {
1436 error_propagate(errp
, local_err
);
1438 error_free(local_err
);
1445 #else /* defined(__linux__) */
1447 void qmp_guest_suspend_disk(Error
**errp
)
1449 error_set(errp
, QERR_UNSUPPORTED
);
1452 void qmp_guest_suspend_ram(Error
**errp
)
1454 error_set(errp
, QERR_UNSUPPORTED
);
1457 void qmp_guest_suspend_hybrid(Error
**errp
)
1459 error_set(errp
, QERR_UNSUPPORTED
);
1462 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1464 error_set(errp
, QERR_UNSUPPORTED
);
1468 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1470 error_set(errp
, QERR_UNSUPPORTED
);
1474 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1476 error_set(errp
, QERR_UNSUPPORTED
);
1482 #if !defined(CONFIG_FSFREEZE)
1484 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
1486 error_set(errp
, QERR_UNSUPPORTED
);
1491 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
1493 error_set(errp
, QERR_UNSUPPORTED
);
1498 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
1499 strList
*mountpoints
,
1502 error_set(errp
, QERR_UNSUPPORTED
);
1507 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
1509 error_set(errp
, QERR_UNSUPPORTED
);
1513 #endif /* CONFIG_FSFREEZE */
1515 #if !defined(CONFIG_FSTRIM)
1516 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
1518 error_set(errp
, QERR_UNSUPPORTED
);
1522 /* register init/cleanup routines for stateful command groups */
1523 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
1525 #if defined(CONFIG_FSFREEZE)
1526 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);
1528 ga_command_state_add(cs
, guest_file_init
, NULL
);