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", "-h", 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
);
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(bool has_time
, int64_t time_ns
, Error
**errp
)
150 Error
*local_err
= NULL
;
153 /* If user has passed a time, validate and set it. */
155 /* year-2038 will overflow in case time_t is 32bit */
156 if (time_ns
/ 1000000000 != (time_t)(time_ns
/ 1000000000)) {
157 error_setg(errp
, "Time %" PRId64
" is too large", time_ns
);
161 tv
.tv_sec
= time_ns
/ 1000000000;
162 tv
.tv_usec
= (time_ns
% 1000000000) / 1000;
164 ret
= settimeofday(&tv
, NULL
);
166 error_setg_errno(errp
, errno
, "Failed to set time to guest");
171 /* Now, if user has passed a time to set and the system time is set, we
172 * just need to synchronize the hardware clock. However, if no time was
173 * passed, user is requesting the opposite: set the system time from the
178 reopen_fd_to_null(0);
179 reopen_fd_to_null(1);
180 reopen_fd_to_null(2);
182 /* Use '/sbin/hwclock -w' to set RTC from the system time,
183 * or '/sbin/hwclock -s' to set the system time from RTC. */
184 execle("/sbin/hwclock", "hwclock", has_time
? "-w" : "-s",
187 } else if (pid
< 0) {
188 error_setg_errno(errp
, errno
, "failed to create child process");
192 ga_wait_child(pid
, &status
, &local_err
);
194 error_propagate(errp
, local_err
);
198 if (!WIFEXITED(status
)) {
199 error_setg(errp
, "child process has terminated abnormally");
203 if (WEXITSTATUS(status
)) {
204 error_setg(errp
, "hwclock failed to set hardware clock to system time");
209 typedef struct GuestFileHandle
{
212 QTAILQ_ENTRY(GuestFileHandle
) next
;
216 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
219 static int64_t guest_file_handle_add(FILE *fh
, Error
**errp
)
221 GuestFileHandle
*gfh
;
224 handle
= ga_get_fd_handle(ga_state
, errp
);
225 if (error_is_set(errp
)) {
229 gfh
= g_malloc0(sizeof(GuestFileHandle
));
232 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
237 static GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**err
)
239 GuestFileHandle
*gfh
;
241 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
248 error_setg(err
, "handle '%" PRId64
"' has not been found", id
);
252 typedef const char * const ccpc
;
258 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
259 static const struct {
262 } guest_file_open_modes
[] = {
263 { (ccpc
[]){ "r", NULL
}, O_RDONLY
},
264 { (ccpc
[]){ "rb", NULL
}, O_RDONLY
| O_BINARY
},
265 { (ccpc
[]){ "w", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
},
266 { (ccpc
[]){ "wb", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
},
267 { (ccpc
[]){ "a", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
},
268 { (ccpc
[]){ "ab", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
},
269 { (ccpc
[]){ "r+", NULL
}, O_RDWR
},
270 { (ccpc
[]){ "rb+", "r+b", NULL
}, O_RDWR
| O_BINARY
},
271 { (ccpc
[]){ "w+", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
},
272 { (ccpc
[]){ "wb+", "w+b", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
},
273 { (ccpc
[]){ "a+", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
},
274 { (ccpc
[]){ "ab+", "a+b", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
}
278 find_open_flag(const char *mode_str
, Error
**err
)
282 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
285 form
= guest_file_open_modes
[mode
].forms
;
286 while (*form
!= NULL
&& strcmp(*form
, mode_str
) != 0) {
294 if (mode
== ARRAY_SIZE(guest_file_open_modes
)) {
295 error_setg(err
, "invalid file open mode '%s'", mode_str
);
298 return guest_file_open_modes
[mode
].oflag_base
| O_NOCTTY
| O_NONBLOCK
;
301 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
302 S_IRGRP | S_IWGRP | \
306 safe_open_or_create(const char *path
, const char *mode
, Error
**err
)
308 Error
*local_err
= NULL
;
311 oflag
= find_open_flag(mode
, &local_err
);
312 if (local_err
== NULL
) {
315 /* If the caller wants / allows creation of a new file, we implement it
316 * with a two step process: open() + (open() / fchmod()).
318 * First we insist on creating the file exclusively as a new file. If
319 * that succeeds, we're free to set any file-mode bits on it. (The
320 * motivation is that we want to set those file-mode bits independently
321 * of the current umask.)
323 * If the exclusive creation fails because the file already exists
324 * (EEXIST is not possible for any other reason), we just attempt to
325 * open the file, but in this case we won't be allowed to change the
326 * file-mode bits on the preexistent file.
328 * The pathname should never disappear between the two open()s in
329 * practice. If it happens, then someone very likely tried to race us.
330 * In this case just go ahead and report the ENOENT from the second
331 * open() to the caller.
333 * If the caller wants to open a preexistent file, then the first
334 * open() is decisive and its third argument is ignored, and the second
335 * open() and the fchmod() are never called.
337 fd
= open(path
, oflag
| ((oflag
& O_CREAT
) ? O_EXCL
: 0), 0);
338 if (fd
== -1 && errno
== EEXIST
) {
339 oflag
&= ~(unsigned)O_CREAT
;
340 fd
= open(path
, oflag
);
344 error_setg_errno(&local_err
, errno
, "failed to open file '%s' "
345 "(mode: '%s')", path
, mode
);
347 qemu_set_cloexec(fd
);
349 if ((oflag
& O_CREAT
) && fchmod(fd
, DEFAULT_NEW_FILE_MODE
) == -1) {
350 error_setg_errno(&local_err
, errno
, "failed to set permission "
351 "0%03o on new file '%s' (mode: '%s')",
352 (unsigned)DEFAULT_NEW_FILE_MODE
, path
, mode
);
356 f
= fdopen(fd
, mode
);
358 error_setg_errno(&local_err
, errno
, "failed to associate "
359 "stdio stream with file descriptor %d, "
360 "file '%s' (mode: '%s')", fd
, path
, mode
);
367 if (oflag
& O_CREAT
) {
373 error_propagate(err
, local_err
);
377 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
, Error
**err
)
380 Error
*local_err
= NULL
;
382 int64_t ret
= -1, handle
;
387 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
388 fh
= safe_open_or_create(path
, mode
, &local_err
);
389 if (local_err
!= NULL
) {
390 error_propagate(err
, local_err
);
394 /* set fd non-blocking to avoid common use cases (like reading from a
395 * named pipe) from hanging the agent
398 ret
= fcntl(fd
, F_GETFL
);
399 ret
= fcntl(fd
, F_SETFL
, ret
| O_NONBLOCK
);
401 error_setg_errno(err
, errno
, "failed to make file '%s' non-blocking",
407 handle
= guest_file_handle_add(fh
, err
);
408 if (error_is_set(err
)) {
413 slog("guest-file-open, handle: %" PRId64
, handle
);
417 void qmp_guest_file_close(int64_t handle
, Error
**err
)
419 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
422 slog("guest-file-close called, handle: %" PRId64
, handle
);
427 ret
= fclose(gfh
->fh
);
429 error_setg_errno(err
, errno
, "failed to close handle");
433 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
437 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
438 int64_t count
, Error
**err
)
440 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
441 GuestFileRead
*read_data
= NULL
;
451 count
= QGA_READ_COUNT_DEFAULT
;
452 } else if (count
< 0) {
453 error_setg(err
, "value '%" PRId64
"' is invalid for argument count",
459 buf
= g_malloc0(count
+1);
460 read_count
= fread(buf
, 1, count
, fh
);
462 error_setg_errno(err
, errno
, "failed to read file");
463 slog("guest-file-read failed, handle: %" PRId64
, handle
);
466 read_data
= g_malloc0(sizeof(GuestFileRead
));
467 read_data
->count
= read_count
;
468 read_data
->eof
= feof(fh
);
470 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
479 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
480 bool has_count
, int64_t count
, Error
**err
)
482 GuestFileWrite
*write_data
= NULL
;
486 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
494 buf
= g_base64_decode(buf_b64
, &buf_len
);
498 } else if (count
< 0 || count
> buf_len
) {
499 error_setg(err
, "value '%" PRId64
"' is invalid for argument count",
505 write_count
= fwrite(buf
, 1, count
, fh
);
507 error_setg_errno(err
, errno
, "failed to write to file");
508 slog("guest-file-write failed, handle: %" PRId64
, handle
);
510 write_data
= g_malloc0(sizeof(GuestFileWrite
));
511 write_data
->count
= write_count
;
512 write_data
->eof
= feof(fh
);
520 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
521 int64_t whence
, Error
**err
)
523 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
524 GuestFileSeek
*seek_data
= NULL
;
533 ret
= fseek(fh
, offset
, whence
);
535 error_setg_errno(err
, errno
, "failed to seek file");
537 seek_data
= g_new0(GuestFileSeek
, 1);
538 seek_data
->position
= ftell(fh
);
539 seek_data
->eof
= feof(fh
);
546 void qmp_guest_file_flush(int64_t handle
, Error
**err
)
548 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, err
);
559 error_setg_errno(err
, errno
, "failed to flush file");
563 static void guest_file_init(void)
565 QTAILQ_INIT(&guest_file_state
.filehandles
);
568 /* linux-specific implementations. avoid this if at all possible. */
569 #if defined(__linux__)
571 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
572 typedef struct FsMount
{
575 QTAILQ_ENTRY(FsMount
) next
;
578 typedef QTAILQ_HEAD(FsMountList
, FsMount
) FsMountList
;
580 static void free_fs_mount_list(FsMountList
*mounts
)
582 FsMount
*mount
, *temp
;
588 QTAILQ_FOREACH_SAFE(mount
, mounts
, next
, temp
) {
589 QTAILQ_REMOVE(mounts
, mount
, next
);
590 g_free(mount
->dirname
);
591 g_free(mount
->devtype
);
597 * Walk the mount table and build a list of local file systems
599 static void build_fs_mount_list(FsMountList
*mounts
, Error
**err
)
603 char const *mtab
= "/proc/self/mounts";
606 fp
= setmntent(mtab
, "r");
608 error_setg(err
, "failed to open mtab file: '%s'", mtab
);
612 while ((ment
= getmntent(fp
))) {
614 * An entry which device name doesn't start with a '/' is
615 * either a dummy file system or a network file system.
616 * Add special handling for smbfs and cifs as is done by
619 if ((ment
->mnt_fsname
[0] != '/') ||
620 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
621 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
625 mount
= g_malloc0(sizeof(FsMount
));
626 mount
->dirname
= g_strdup(ment
->mnt_dir
);
627 mount
->devtype
= g_strdup(ment
->mnt_type
);
629 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
636 #if defined(CONFIG_FSFREEZE)
639 FSFREEZE_HOOK_THAW
= 0,
640 FSFREEZE_HOOK_FREEZE
,
643 const char *fsfreeze_hook_arg_string
[] = {
648 static void execute_fsfreeze_hook(FsfreezeHookArg arg
, Error
**err
)
653 const char *arg_str
= fsfreeze_hook_arg_string
[arg
];
654 Error
*local_err
= NULL
;
656 hook
= ga_fsfreeze_hook(ga_state
);
660 if (access(hook
, X_OK
) != 0) {
661 error_setg_errno(err
, errno
, "can't access fsfreeze hook '%s'", hook
);
665 slog("executing fsfreeze hook with arg '%s'", arg_str
);
669 reopen_fd_to_null(0);
670 reopen_fd_to_null(1);
671 reopen_fd_to_null(2);
673 execle(hook
, hook
, arg_str
, NULL
, environ
);
675 } else if (pid
< 0) {
676 error_setg_errno(err
, errno
, "failed to create child process");
680 ga_wait_child(pid
, &status
, &local_err
);
682 error_propagate(err
, local_err
);
686 if (!WIFEXITED(status
)) {
687 error_setg(err
, "fsfreeze hook has terminated abnormally");
691 status
= WEXITSTATUS(status
);
693 error_setg(err
, "fsfreeze hook has failed with status %d", status
);
699 * Return status of freeze/thaw
701 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
703 if (ga_is_frozen(ga_state
)) {
704 return GUEST_FSFREEZE_STATUS_FROZEN
;
707 return GUEST_FSFREEZE_STATUS_THAWED
;
711 * Walk list of mounted file systems in the guest, and freeze the ones which
712 * are real local file systems.
714 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
718 struct FsMount
*mount
;
719 Error
*local_err
= NULL
;
722 slog("guest-fsfreeze called");
724 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE
, &local_err
);
726 error_propagate(err
, local_err
);
730 QTAILQ_INIT(&mounts
);
731 build_fs_mount_list(&mounts
, &local_err
);
733 error_propagate(err
, local_err
);
737 /* cannot risk guest agent blocking itself on a write in this state */
738 ga_set_frozen(ga_state
);
740 QTAILQ_FOREACH_REVERSE(mount
, &mounts
, FsMountList
, next
) {
741 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
743 error_setg_errno(err
, errno
, "failed to open %s", mount
->dirname
);
747 /* we try to cull filesytems we know won't work in advance, but other
748 * filesytems may not implement fsfreeze for less obvious reasons.
749 * these will report EOPNOTSUPP. we simply ignore these when tallying
750 * the number of frozen filesystems.
752 * any other error means a failure to freeze a filesystem we
753 * expect to be freezable, so return an error in those cases
754 * and return system to thawed state.
756 ret
= ioctl(fd
, FIFREEZE
);
758 if (errno
!= EOPNOTSUPP
) {
759 error_setg_errno(err
, errno
, "failed to freeze %s",
770 free_fs_mount_list(&mounts
);
774 free_fs_mount_list(&mounts
);
775 qmp_guest_fsfreeze_thaw(NULL
);
780 * Walk list of frozen file systems in the guest, and thaw them.
782 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
787 int fd
, i
= 0, logged
;
788 Error
*local_err
= NULL
;
790 QTAILQ_INIT(&mounts
);
791 build_fs_mount_list(&mounts
, &local_err
);
793 error_propagate(err
, local_err
);
797 QTAILQ_FOREACH(mount
, &mounts
, next
) {
799 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
803 /* we have no way of knowing whether a filesystem was actually unfrozen
804 * as a result of a successful call to FITHAW, only that if an error
805 * was returned the filesystem was *not* unfrozen by that particular
808 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
809 * to unfreeze, continuing issuing FITHAW until an error is returned,
810 * in which case either the filesystem is in an unfreezable state, or,
811 * more likely, it was thawed previously (and remains so afterward).
813 * also, since the most recent successful call is the one that did
814 * the actual unfreeze, we can use this to provide an accurate count
815 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
816 * may * be useful for determining whether a filesystem was unfrozen
817 * during the freeze/thaw phase by a process other than qemu-ga.
820 ret
= ioctl(fd
, FITHAW
);
821 if (ret
== 0 && !logged
) {
829 ga_unset_frozen(ga_state
);
830 free_fs_mount_list(&mounts
);
832 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW
, err
);
837 static void guest_fsfreeze_cleanup(void)
841 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
842 qmp_guest_fsfreeze_thaw(&err
);
844 slog("failed to clean up frozen filesystems: %s",
845 error_get_pretty(err
));
850 #endif /* CONFIG_FSFREEZE */
852 #if defined(CONFIG_FSTRIM)
854 * Walk list of mounted file systems in the guest, and trim them.
856 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**err
)
860 struct FsMount
*mount
;
862 Error
*local_err
= NULL
;
863 struct fstrim_range r
= {
866 .minlen
= has_minimum
? minimum
: 0,
869 slog("guest-fstrim called");
871 QTAILQ_INIT(&mounts
);
872 build_fs_mount_list(&mounts
, &local_err
);
874 error_propagate(err
, local_err
);
878 QTAILQ_FOREACH(mount
, &mounts
, next
) {
879 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
881 error_setg_errno(err
, errno
, "failed to open %s", mount
->dirname
);
885 /* We try to cull filesytems we know won't work in advance, but other
886 * filesytems may not implement fstrim for less obvious reasons. These
887 * will report EOPNOTSUPP; we simply ignore these errors. Any other
888 * error means an unexpected error, so return it in those cases. In
889 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
891 ret
= ioctl(fd
, FITRIM
, &r
);
893 if (errno
!= ENOTTY
&& errno
!= EOPNOTSUPP
) {
894 error_setg_errno(err
, errno
, "failed to trim %s",
904 free_fs_mount_list(&mounts
);
906 #endif /* CONFIG_FSTRIM */
909 #define LINUX_SYS_STATE_FILE "/sys/power/state"
910 #define SUSPEND_SUPPORTED 0
911 #define SUSPEND_NOT_SUPPORTED 1
913 static void bios_supports_mode(const char *pmutils_bin
, const char *pmutils_arg
,
914 const char *sysfile_str
, Error
**err
)
916 Error
*local_err
= NULL
;
921 pmutils_path
= g_find_program_in_path(pmutils_bin
);
925 char buf
[32]; /* hopefully big enough */
930 reopen_fd_to_null(0);
931 reopen_fd_to_null(1);
932 reopen_fd_to_null(2);
935 execle(pmutils_path
, pmutils_bin
, pmutils_arg
, NULL
, environ
);
939 * If we get here either pm-utils is not installed or execle() has
940 * failed. Let's try the manual method if the caller wants it.
944 _exit(SUSPEND_NOT_SUPPORTED
);
947 fd
= open(LINUX_SYS_STATE_FILE
, O_RDONLY
);
949 _exit(SUSPEND_NOT_SUPPORTED
);
952 ret
= read(fd
, buf
, sizeof(buf
)-1);
954 _exit(SUSPEND_NOT_SUPPORTED
);
958 if (strstr(buf
, sysfile_str
)) {
959 _exit(SUSPEND_SUPPORTED
);
962 _exit(SUSPEND_NOT_SUPPORTED
);
963 } else if (pid
< 0) {
964 error_setg_errno(err
, errno
, "failed to create child process");
968 ga_wait_child(pid
, &status
, &local_err
);
970 error_propagate(err
, local_err
);
974 if (!WIFEXITED(status
)) {
975 error_setg(err
, "child process has terminated abnormally");
979 switch (WEXITSTATUS(status
)) {
980 case SUSPEND_SUPPORTED
:
982 case SUSPEND_NOT_SUPPORTED
:
984 "the requested suspend mode is not supported by the guest");
988 "the helper program '%s' returned an unexpected exit status"
989 " code (%d)", pmutils_path
, WEXITSTATUS(status
));
994 g_free(pmutils_path
);
997 static void guest_suspend(const char *pmutils_bin
, const char *sysfile_str
,
1000 Error
*local_err
= NULL
;
1005 pmutils_path
= g_find_program_in_path(pmutils_bin
);
1013 reopen_fd_to_null(0);
1014 reopen_fd_to_null(1);
1015 reopen_fd_to_null(2);
1018 execle(pmutils_path
, pmutils_bin
, NULL
, environ
);
1022 * If we get here either pm-utils is not installed or execle() has
1023 * failed. Let's try the manual method if the caller wants it.
1027 _exit(EXIT_FAILURE
);
1030 fd
= open(LINUX_SYS_STATE_FILE
, O_WRONLY
);
1032 _exit(EXIT_FAILURE
);
1035 if (write(fd
, sysfile_str
, strlen(sysfile_str
)) < 0) {
1036 _exit(EXIT_FAILURE
);
1039 _exit(EXIT_SUCCESS
);
1040 } else if (pid
< 0) {
1041 error_setg_errno(err
, errno
, "failed to create child process");
1045 ga_wait_child(pid
, &status
, &local_err
);
1047 error_propagate(err
, local_err
);
1051 if (!WIFEXITED(status
)) {
1052 error_setg(err
, "child process has terminated abnormally");
1056 if (WEXITSTATUS(status
)) {
1057 error_setg(err
, "child process has failed to suspend");
1062 g_free(pmutils_path
);
1065 void qmp_guest_suspend_disk(Error
**err
)
1067 bios_supports_mode("pm-is-supported", "--hibernate", "disk", err
);
1068 if (error_is_set(err
)) {
1072 guest_suspend("pm-hibernate", "disk", err
);
1075 void qmp_guest_suspend_ram(Error
**err
)
1077 bios_supports_mode("pm-is-supported", "--suspend", "mem", err
);
1078 if (error_is_set(err
)) {
1082 guest_suspend("pm-suspend", "mem", err
);
1085 void qmp_guest_suspend_hybrid(Error
**err
)
1087 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL
, err
);
1088 if (error_is_set(err
)) {
1092 guest_suspend("pm-suspend-hybrid", NULL
, err
);
1095 static GuestNetworkInterfaceList
*
1096 guest_find_interface(GuestNetworkInterfaceList
*head
,
1099 for (; head
; head
= head
->next
) {
1100 if (strcmp(head
->value
->name
, name
) == 0) {
1109 * Build information about guest interfaces
1111 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1113 GuestNetworkInterfaceList
*head
= NULL
, *cur_item
= NULL
;
1114 struct ifaddrs
*ifap
, *ifa
;
1116 if (getifaddrs(&ifap
) < 0) {
1117 error_setg_errno(errp
, errno
, "getifaddrs failed");
1121 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1122 GuestNetworkInterfaceList
*info
;
1123 GuestIpAddressList
**address_list
= NULL
, *address_item
= NULL
;
1124 char addr4
[INET_ADDRSTRLEN
];
1125 char addr6
[INET6_ADDRSTRLEN
];
1128 unsigned char *mac_addr
;
1131 g_debug("Processing %s interface", ifa
->ifa_name
);
1133 info
= guest_find_interface(head
, ifa
->ifa_name
);
1136 info
= g_malloc0(sizeof(*info
));
1137 info
->value
= g_malloc0(sizeof(*info
->value
));
1138 info
->value
->name
= g_strdup(ifa
->ifa_name
);
1141 head
= cur_item
= info
;
1143 cur_item
->next
= info
;
1148 if (!info
->value
->has_hardware_address
&&
1149 ifa
->ifa_flags
& SIOCGIFHWADDR
) {
1150 /* we haven't obtained HW address yet */
1151 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1153 error_setg_errno(errp
, errno
, "failed to create socket");
1157 memset(&ifr
, 0, sizeof(ifr
));
1158 pstrcpy(ifr
.ifr_name
, IF_NAMESIZE
, info
->value
->name
);
1159 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) == -1) {
1160 error_setg_errno(errp
, errno
,
1161 "failed to get MAC address of %s",
1168 mac_addr
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
1170 info
->value
->hardware_address
=
1171 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1172 (int) mac_addr
[0], (int) mac_addr
[1],
1173 (int) mac_addr
[2], (int) mac_addr
[3],
1174 (int) mac_addr
[4], (int) mac_addr
[5]);
1176 info
->value
->has_hardware_address
= true;
1179 if (ifa
->ifa_addr
&&
1180 ifa
->ifa_addr
->sa_family
== AF_INET
) {
1181 /* interface with IPv4 address */
1182 p
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
1183 if (!inet_ntop(AF_INET
, p
, addr4
, sizeof(addr4
))) {
1184 error_setg_errno(errp
, errno
, "inet_ntop failed");
1188 address_item
= g_malloc0(sizeof(*address_item
));
1189 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1190 address_item
->value
->ip_address
= g_strdup(addr4
);
1191 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV4
;
1193 if (ifa
->ifa_netmask
) {
1194 /* Count the number of set bits in netmask.
1195 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1196 p
= &((struct sockaddr_in
*)ifa
->ifa_netmask
)->sin_addr
;
1197 address_item
->value
->prefix
= ctpop32(((uint32_t *) p
)[0]);
1199 } else if (ifa
->ifa_addr
&&
1200 ifa
->ifa_addr
->sa_family
== AF_INET6
) {
1201 /* interface with IPv6 address */
1202 p
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
1203 if (!inet_ntop(AF_INET6
, p
, addr6
, sizeof(addr6
))) {
1204 error_setg_errno(errp
, errno
, "inet_ntop failed");
1208 address_item
= g_malloc0(sizeof(*address_item
));
1209 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1210 address_item
->value
->ip_address
= g_strdup(addr6
);
1211 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV6
;
1213 if (ifa
->ifa_netmask
) {
1214 /* Count the number of set bits in netmask.
1215 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1216 p
= &((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
1217 address_item
->value
->prefix
=
1218 ctpop32(((uint32_t *) p
)[0]) +
1219 ctpop32(((uint32_t *) p
)[1]) +
1220 ctpop32(((uint32_t *) p
)[2]) +
1221 ctpop32(((uint32_t *) p
)[3]);
1225 if (!address_item
) {
1229 address_list
= &info
->value
->ip_addresses
;
1231 while (*address_list
&& (*address_list
)->next
) {
1232 address_list
= &(*address_list
)->next
;
1235 if (!*address_list
) {
1236 *address_list
= address_item
;
1238 (*address_list
)->next
= address_item
;
1241 info
->value
->has_ip_addresses
= true;
1251 qapi_free_GuestNetworkInterfaceList(head
);
1255 #define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1257 static long sysconf_exact(int name
, const char *name_str
, Error
**err
)
1262 ret
= sysconf(name
);
1265 error_setg(err
, "sysconf(%s): value indefinite", name_str
);
1267 error_setg_errno(err
, errno
, "sysconf(%s)", name_str
);
1273 /* Transfer online/offline status between @vcpu and the guest system.
1275 * On input either @errp or *@errp must be NULL.
1277 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1278 * - R: vcpu->logical_id
1280 * - W: vcpu->can_offline
1282 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1283 * - R: vcpu->logical_id
1286 * Written members remain unmodified on error.
1288 static void transfer_vcpu(GuestLogicalProcessor
*vcpu
, bool sys2vcpu
,
1294 dirpath
= g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64
"/",
1296 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
1298 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
1300 static const char fn
[] = "online";
1304 fd
= openat(dirfd
, fn
, sys2vcpu
? O_RDONLY
: O_RDWR
);
1306 if (errno
!= ENOENT
) {
1307 error_setg_errno(errp
, errno
, "open(\"%s/%s\")", dirpath
, fn
);
1308 } else if (sys2vcpu
) {
1309 vcpu
->online
= true;
1310 vcpu
->can_offline
= false;
1311 } else if (!vcpu
->online
) {
1312 error_setg(errp
, "logical processor #%" PRId64
" can't be "
1313 "offlined", vcpu
->logical_id
);
1314 } /* otherwise pretend successful re-onlining */
1316 unsigned char status
;
1318 res
= pread(fd
, &status
, 1, 0);
1320 error_setg_errno(errp
, errno
, "pread(\"%s/%s\")", dirpath
, fn
);
1321 } else if (res
== 0) {
1322 error_setg(errp
, "pread(\"%s/%s\"): unexpected EOF", dirpath
,
1324 } else if (sys2vcpu
) {
1325 vcpu
->online
= (status
!= '0');
1326 vcpu
->can_offline
= true;
1327 } else if (vcpu
->online
!= (status
!= '0')) {
1328 status
= '0' + vcpu
->online
;
1329 if (pwrite(fd
, &status
, 1, 0) == -1) {
1330 error_setg_errno(errp
, errno
, "pwrite(\"%s/%s\")", dirpath
,
1333 } /* otherwise pretend successful re-(on|off)-lining */
1346 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1349 GuestLogicalProcessorList
*head
, **link
;
1351 Error
*local_err
= NULL
;
1356 sc_max
= SYSCONF_EXACT(_SC_NPROCESSORS_CONF
, &local_err
);
1358 while (local_err
== NULL
&& current
< sc_max
) {
1359 GuestLogicalProcessor
*vcpu
;
1360 GuestLogicalProcessorList
*entry
;
1362 vcpu
= g_malloc0(sizeof *vcpu
);
1363 vcpu
->logical_id
= current
++;
1364 vcpu
->has_can_offline
= true; /* lolspeak ftw */
1365 transfer_vcpu(vcpu
, true, &local_err
);
1367 entry
= g_malloc0(sizeof *entry
);
1368 entry
->value
= vcpu
;
1371 link
= &entry
->next
;
1374 if (local_err
== NULL
) {
1375 /* there's no guest with zero VCPUs */
1376 g_assert(head
!= NULL
);
1380 qapi_free_GuestLogicalProcessorList(head
);
1381 error_propagate(errp
, local_err
);
1385 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1388 Error
*local_err
= NULL
;
1391 while (vcpus
!= NULL
) {
1392 transfer_vcpu(vcpus
->value
, false, &local_err
);
1393 if (local_err
!= NULL
) {
1397 vcpus
= vcpus
->next
;
1400 if (local_err
!= NULL
) {
1401 if (processed
== 0) {
1402 error_propagate(errp
, local_err
);
1404 error_free(local_err
);
1411 #else /* defined(__linux__) */
1413 void qmp_guest_suspend_disk(Error
**err
)
1415 error_set(err
, QERR_UNSUPPORTED
);
1418 void qmp_guest_suspend_ram(Error
**err
)
1420 error_set(err
, QERR_UNSUPPORTED
);
1423 void qmp_guest_suspend_hybrid(Error
**err
)
1425 error_set(err
, QERR_UNSUPPORTED
);
1428 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1430 error_set(errp
, QERR_UNSUPPORTED
);
1434 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1436 error_set(errp
, QERR_UNSUPPORTED
);
1440 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1442 error_set(errp
, QERR_UNSUPPORTED
);
1448 #if !defined(CONFIG_FSFREEZE)
1450 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**err
)
1452 error_set(err
, QERR_UNSUPPORTED
);
1457 int64_t qmp_guest_fsfreeze_freeze(Error
**err
)
1459 error_set(err
, QERR_UNSUPPORTED
);
1464 int64_t qmp_guest_fsfreeze_thaw(Error
**err
)
1466 error_set(err
, QERR_UNSUPPORTED
);
1470 #endif /* CONFIG_FSFREEZE */
1472 #if !defined(CONFIG_FSTRIM)
1473 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**err
)
1475 error_set(err
, QERR_UNSUPPORTED
);
1479 /* register init/cleanup routines for stateful command groups */
1480 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
1482 #if defined(CONFIG_FSFREEZE)
1483 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);
1485 ga_command_state_add(cs
, guest_file_init
, NULL
);