target-ppc: Synchronize VPA state with KVM
[qemu/agraf.git] / qga / commands-posix.c
blob3b5c536e41caec51a2805997530c84ea7a94ed3f
1 /*
2 * QEMU Guest Agent POSIX-specific command implementations
4 * Copyright IBM Corp. 2011
6 * Authors:
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
8 * Michal Privoznik <mprivozn@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include <glib.h>
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.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
29 #ifdef __APPLE__
30 #include <crt_externs.h>
31 #define environ (*_NSGetEnviron())
32 #else
33 extern char **environ;
34 #endif
35 #endif
37 #if defined(__linux__)
38 #include <mntent.h>
39 #include <linux/fs.h>
40 #include <ifaddrs.h>
41 #include <arpa/inet.h>
42 #include <sys/socket.h>
43 #include <net/if.h>
45 #ifdef FIFREEZE
46 #define CONFIG_FSFREEZE
47 #endif
48 #ifdef FITRIM
49 #define CONFIG_FSTRIM
50 #endif
51 #endif
53 static void ga_wait_child(pid_t pid, int *status, Error **err)
55 pid_t rpid;
57 *status = 0;
59 do {
60 rpid = waitpid(pid, status, 0);
61 } while (rpid == -1 && errno == EINTR);
63 if (rpid == -1) {
64 error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
65 return;
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;
75 pid_t pid;
76 int status;
78 slog("guest-shutdown called, mode: %s", mode);
79 if (!has_mode || strcmp(mode, "powerdown") == 0) {
80 shutdown_flag = "-P";
81 } else if (strcmp(mode, "halt") == 0) {
82 shutdown_flag = "-H";
83 } else if (strcmp(mode, "reboot") == 0) {
84 shutdown_flag = "-r";
85 } else {
86 error_setg(err,
87 "mode is invalid (valid values are: halt|powerdown|reboot");
88 return;
91 pid = fork();
92 if (pid == 0) {
93 /* child, start the shutdown */
94 setsid();
95 reopen_fd_to_null(0);
96 reopen_fd_to_null(1);
97 reopen_fd_to_null(2);
99 execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
100 "hypervisor initiated shutdown", (char*)NULL, environ);
101 _exit(EXIT_FAILURE);
102 } else if (pid < 0) {
103 error_setg_errno(err, errno, "failed to create child process");
104 return;
107 ga_wait_child(pid, &status, &local_err);
108 if (error_is_set(&local_err)) {
109 error_propagate(err, local_err);
110 return;
113 if (!WIFEXITED(status)) {
114 error_setg(err, "child process has terminated abnormally");
115 return;
118 if (WEXITSTATUS(status)) {
119 error_setg(err, "child process has failed to shutdown");
120 return;
123 /* succeeded */
126 int64_t qmp_guest_get_time(Error **errp)
128 int ret;
129 qemu_timeval tq;
130 int64_t time_ns;
132 ret = qemu_gettimeofday(&tq);
133 if (ret < 0) {
134 error_setg_errno(errp, errno, "Failed to get time");
135 return -1;
138 time_ns = tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
139 return time_ns;
142 void qmp_guest_set_time(int64_t time_ns, Error **errp)
144 int ret;
145 int status;
146 pid_t pid;
147 Error *local_err = NULL;
148 struct timeval tv;
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);
153 return;
156 tv.tv_sec = time_ns / 1000000000;
157 tv.tv_usec = (time_ns % 1000000000) / 1000;
159 ret = settimeofday(&tv, NULL);
160 if (ret < 0) {
161 error_setg_errno(errp, errno, "Failed to set time to guest");
162 return;
165 /* Set the Hardware Clock to the current System Time. */
166 pid = fork();
167 if (pid == 0) {
168 setsid();
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);
174 _exit(EXIT_FAILURE);
175 } else if (pid < 0) {
176 error_setg_errno(errp, errno, "failed to create child process");
177 return;
180 ga_wait_child(pid, &status, &local_err);
181 if (error_is_set(&local_err)) {
182 error_propagate(errp, local_err);
183 return;
186 if (!WIFEXITED(status)) {
187 error_setg(errp, "child process has terminated abnormally");
188 return;
191 if (WEXITSTATUS(status)) {
192 error_setg(errp, "hwclock failed to set hardware clock to system time");
193 return;
197 typedef struct GuestFileHandle {
198 uint64_t id;
199 FILE *fh;
200 QTAILQ_ENTRY(GuestFileHandle) next;
201 } GuestFileHandle;
203 static struct {
204 QTAILQ_HEAD(, GuestFileHandle) filehandles;
205 } guest_file_state;
207 static int64_t guest_file_handle_add(FILE *fh, Error **errp)
209 GuestFileHandle *gfh;
210 int64_t handle;
212 handle = ga_get_fd_handle(ga_state, errp);
213 if (error_is_set(errp)) {
214 return 0;
217 gfh = g_malloc0(sizeof(GuestFileHandle));
218 gfh->id = handle;
219 gfh->fh = fh;
220 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
222 return handle;
225 static GuestFileHandle *guest_file_handle_find(int64_t id, Error **err)
227 GuestFileHandle *gfh;
229 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
231 if (gfh->id == id) {
232 return gfh;
236 error_setg(err, "handle '%" PRId64 "' has not been found", id);
237 return NULL;
240 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
242 FILE *fh;
243 int fd;
244 int64_t ret = -1, handle;
246 if (!has_mode) {
247 mode = "r";
249 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
250 fh = fopen(path, mode);
251 if (!fh) {
252 error_setg_errno(err, errno, "failed to open file '%s' (mode: '%s')",
253 path, mode);
254 return -1;
257 /* set fd non-blocking to avoid common use cases (like reading from a
258 * named pipe) from hanging the agent
260 fd = fileno(fh);
261 ret = fcntl(fd, F_GETFL);
262 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
263 if (ret == -1) {
264 error_setg_errno(err, errno, "failed to make file '%s' non-blocking",
265 path);
266 fclose(fh);
267 return -1;
270 handle = guest_file_handle_add(fh, err);
271 if (error_is_set(err)) {
272 fclose(fh);
273 return -1;
276 slog("guest-file-open, handle: %d", handle);
277 return handle;
280 void qmp_guest_file_close(int64_t handle, Error **err)
282 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
283 int ret;
285 slog("guest-file-close called, handle: %ld", handle);
286 if (!gfh) {
287 return;
290 ret = fclose(gfh->fh);
291 if (ret == EOF) {
292 error_setg_errno(err, errno, "failed to close handle");
293 return;
296 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
297 g_free(gfh);
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;
305 guchar *buf;
306 FILE *fh;
307 size_t read_count;
309 if (!gfh) {
310 return NULL;
313 if (!has_count) {
314 count = QGA_READ_COUNT_DEFAULT;
315 } else if (count < 0) {
316 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
317 count);
318 return NULL;
321 fh = gfh->fh;
322 buf = g_malloc0(count+1);
323 read_count = fread(buf, 1, count, fh);
324 if (ferror(fh)) {
325 error_setg_errno(err, errno, "failed to read file");
326 slog("guest-file-read failed, handle: %ld", handle);
327 } else {
328 buf[read_count] = 0;
329 read_data = g_malloc0(sizeof(GuestFileRead));
330 read_data->count = read_count;
331 read_data->eof = feof(fh);
332 if (read_count) {
333 read_data->buf_b64 = g_base64_encode(buf, read_count);
336 g_free(buf);
337 clearerr(fh);
339 return read_data;
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;
346 guchar *buf;
347 gsize buf_len;
348 int write_count;
349 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
350 FILE *fh;
352 if (!gfh) {
353 return NULL;
356 fh = gfh->fh;
357 buf = g_base64_decode(buf_b64, &buf_len);
359 if (!has_count) {
360 count = buf_len;
361 } else if (count < 0 || count > buf_len) {
362 error_setg(err, "value '%" PRId64 "' is invalid for argument count",
363 count);
364 g_free(buf);
365 return NULL;
368 write_count = fwrite(buf, 1, count, fh);
369 if (ferror(fh)) {
370 error_setg_errno(err, errno, "failed to write to file");
371 slog("guest-file-write failed, handle: %ld", handle);
372 } else {
373 write_data = g_malloc0(sizeof(GuestFileWrite));
374 write_data->count = write_count;
375 write_data->eof = feof(fh);
377 g_free(buf);
378 clearerr(fh);
380 return write_data;
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;
388 FILE *fh;
389 int ret;
391 if (!gfh) {
392 return NULL;
395 fh = gfh->fh;
396 ret = fseek(fh, offset, whence);
397 if (ret == -1) {
398 error_setg_errno(err, errno, "failed to seek file");
399 } else {
400 seek_data = g_malloc0(sizeof(GuestFileRead));
401 seek_data->position = ftell(fh);
402 seek_data->eof = feof(fh);
404 clearerr(fh);
406 return seek_data;
409 void qmp_guest_file_flush(int64_t handle, Error **err)
411 GuestFileHandle *gfh = guest_file_handle_find(handle, err);
412 FILE *fh;
413 int ret;
415 if (!gfh) {
416 return;
419 fh = gfh->fh;
420 ret = fflush(fh);
421 if (ret == EOF) {
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 {
436 char *dirname;
437 char *devtype;
438 QTAILQ_ENTRY(FsMount) next;
439 } FsMount;
441 typedef QTAILQ_HEAD(, FsMount) FsMountList;
443 static void free_fs_mount_list(FsMountList *mounts)
445 FsMount *mount, *temp;
447 if (!mounts) {
448 return;
451 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
452 QTAILQ_REMOVE(mounts, mount, next);
453 g_free(mount->dirname);
454 g_free(mount->devtype);
455 g_free(mount);
460 * Walk the mount table and build a list of local file systems
462 static void build_fs_mount_list(FsMountList *mounts, Error **err)
464 struct mntent *ment;
465 FsMount *mount;
466 char const *mtab = "/proc/self/mounts";
467 FILE *fp;
469 fp = setmntent(mtab, "r");
470 if (!fp) {
471 error_setg(err, "failed to open mtab file: '%s'", mtab);
472 return;
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
480 * coreutils as well.
482 if ((ment->mnt_fsname[0] != '/') ||
483 (strcmp(ment->mnt_type, "smbfs") == 0) ||
484 (strcmp(ment->mnt_type, "cifs") == 0)) {
485 continue;
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);
495 endmntent(fp);
497 #endif
499 #if defined(CONFIG_FSFREEZE)
501 typedef enum {
502 FSFREEZE_HOOK_THAW = 0,
503 FSFREEZE_HOOK_FREEZE,
504 } FsfreezeHookArg;
506 const char *fsfreeze_hook_arg_string[] = {
507 "thaw",
508 "freeze",
511 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
513 int status;
514 pid_t pid;
515 const char *hook;
516 const char *arg_str = fsfreeze_hook_arg_string[arg];
517 Error *local_err = NULL;
519 hook = ga_fsfreeze_hook(ga_state);
520 if (!hook) {
521 return;
523 if (access(hook, X_OK) != 0) {
524 error_setg_errno(err, errno, "can't access fsfreeze hook '%s'", hook);
525 return;
528 slog("executing fsfreeze hook with arg '%s'", arg_str);
529 pid = fork();
530 if (pid == 0) {
531 setsid();
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);
537 _exit(EXIT_FAILURE);
538 } else if (pid < 0) {
539 error_setg_errno(err, errno, "failed to create child process");
540 return;
543 ga_wait_child(pid, &status, &local_err);
544 if (error_is_set(&local_err)) {
545 error_propagate(err, local_err);
546 return;
549 if (!WIFEXITED(status)) {
550 error_setg(err, "fsfreeze hook has terminated abnormally");
551 return;
554 status = WEXITSTATUS(status);
555 if (status) {
556 error_setg(err, "fsfreeze hook has failed with status %d", status);
557 return;
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)
579 int ret = 0, i = 0;
580 FsMountList mounts;
581 struct FsMount *mount;
582 Error *local_err = NULL;
583 int fd;
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);
590 return -1;
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);
597 return -1;
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);
605 if (fd == -1) {
606 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
607 goto error;
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);
620 if (ret == -1) {
621 if (errno != EOPNOTSUPP) {
622 error_setg_errno(err, errno, "failed to freeze %s",
623 mount->dirname);
624 close(fd);
625 goto error;
627 } else {
628 i++;
630 close(fd);
633 free_fs_mount_list(&mounts);
634 return i;
636 error:
637 free_fs_mount_list(&mounts);
638 qmp_guest_fsfreeze_thaw(NULL);
639 return 0;
643 * Walk list of frozen file systems in the guest, and thaw them.
645 int64_t qmp_guest_fsfreeze_thaw(Error **err)
647 int ret;
648 FsMountList mounts;
649 FsMount *mount;
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);
657 return 0;
660 QTAILQ_FOREACH(mount, &mounts, next) {
661 logged = false;
662 fd = qemu_open(mount->dirname, O_RDONLY);
663 if (fd == -1) {
664 continue;
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
669 * call.
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.
682 do {
683 ret = ioctl(fd, FITHAW);
684 if (ret == 0 && !logged) {
685 i++;
686 logged = true;
688 } while (ret == 0);
689 close(fd);
692 ga_unset_frozen(ga_state);
693 free_fs_mount_list(&mounts);
695 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, err);
697 return i;
700 static void guest_fsfreeze_cleanup(void)
702 Error *err = NULL;
704 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
705 qmp_guest_fsfreeze_thaw(&err);
706 if (err) {
707 slog("failed to clean up frozen filesystems: %s",
708 error_get_pretty(err));
709 error_free(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)
721 int ret = 0;
722 FsMountList mounts;
723 struct FsMount *mount;
724 int fd;
725 Error *local_err = NULL;
726 struct fstrim_range r = {
727 .start = 0,
728 .len = -1,
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);
738 return;
741 QTAILQ_FOREACH(mount, &mounts, next) {
742 fd = qemu_open(mount->dirname, O_RDONLY);
743 if (fd == -1) {
744 error_setg_errno(err, errno, "failed to open %s", mount->dirname);
745 goto error;
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);
755 if (ret == -1) {
756 if (errno != ENOTTY && errno != EOPNOTSUPP) {
757 error_setg_errno(err, errno, "failed to trim %s",
758 mount->dirname);
759 close(fd);
760 goto error;
763 close(fd);
766 error:
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;
780 char *pmutils_path;
781 pid_t pid;
782 int status;
784 pmutils_path = g_find_program_in_path(pmutils_bin);
786 pid = fork();
787 if (!pid) {
788 char buf[32]; /* hopefully big enough */
789 ssize_t ret;
790 int fd;
792 setsid();
793 reopen_fd_to_null(0);
794 reopen_fd_to_null(1);
795 reopen_fd_to_null(2);
797 if (pmutils_path) {
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.
806 if (!sysfile_str) {
807 _exit(SUSPEND_NOT_SUPPORTED);
810 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
811 if (fd < 0) {
812 _exit(SUSPEND_NOT_SUPPORTED);
815 ret = read(fd, buf, sizeof(buf)-1);
816 if (ret <= 0) {
817 _exit(SUSPEND_NOT_SUPPORTED);
819 buf[ret] = '\0';
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");
828 goto out;
831 ga_wait_child(pid, &status, &local_err);
832 if (error_is_set(&local_err)) {
833 error_propagate(err, local_err);
834 goto out;
837 if (!WIFEXITED(status)) {
838 error_setg(err, "child process has terminated abnormally");
839 goto out;
842 switch (WEXITSTATUS(status)) {
843 case SUSPEND_SUPPORTED:
844 goto out;
845 case SUSPEND_NOT_SUPPORTED:
846 error_setg(err,
847 "the requested suspend mode is not supported by the guest");
848 goto out;
849 default:
850 error_setg(err,
851 "the helper program '%s' returned an unexpected exit status"
852 " code (%d)", pmutils_path, WEXITSTATUS(status));
853 goto out;
856 out:
857 g_free(pmutils_path);
860 static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
861 Error **err)
863 Error *local_err = NULL;
864 char *pmutils_path;
865 pid_t pid;
866 int status;
868 pmutils_path = g_find_program_in_path(pmutils_bin);
870 pid = fork();
871 if (pid == 0) {
872 /* child */
873 int fd;
875 setsid();
876 reopen_fd_to_null(0);
877 reopen_fd_to_null(1);
878 reopen_fd_to_null(2);
880 if (pmutils_path) {
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.
889 if (!sysfile_str) {
890 _exit(EXIT_FAILURE);
893 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
894 if (fd < 0) {
895 _exit(EXIT_FAILURE);
898 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
899 _exit(EXIT_FAILURE);
902 _exit(EXIT_SUCCESS);
903 } else if (pid < 0) {
904 error_setg_errno(err, errno, "failed to create child process");
905 goto out;
908 ga_wait_child(pid, &status, &local_err);
909 if (error_is_set(&local_err)) {
910 error_propagate(err, local_err);
911 goto out;
914 if (!WIFEXITED(status)) {
915 error_setg(err, "child process has terminated abnormally");
916 goto out;
919 if (WEXITSTATUS(status)) {
920 error_setg(err, "child process has failed to suspend");
921 goto out;
924 out:
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)) {
932 return;
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)) {
942 return;
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)) {
952 return;
955 guest_suspend("pm-suspend-hybrid", NULL, err);
958 static GuestNetworkInterfaceList *
959 guest_find_interface(GuestNetworkInterfaceList *head,
960 const char *name)
962 for (; head; head = head->next) {
963 if (strcmp(head->value->name, name) == 0) {
964 break;
968 return head;
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");
981 goto error;
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];
989 int sock;
990 struct ifreq ifr;
991 unsigned char *mac_addr;
992 void *p;
994 g_debug("Processing %s interface", ifa->ifa_name);
996 info = guest_find_interface(head, ifa->ifa_name);
998 if (!info) {
999 info = g_malloc0(sizeof(*info));
1000 info->value = g_malloc0(sizeof(*info->value));
1001 info->value->name = g_strdup(ifa->ifa_name);
1003 if (!cur_item) {
1004 head = cur_item = info;
1005 } else {
1006 cur_item->next = info;
1007 cur_item = 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);
1015 if (sock == -1) {
1016 error_setg_errno(errp, errno, "failed to create socket");
1017 goto error;
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",
1025 ifa->ifa_name);
1026 close(sock);
1027 goto error;
1030 close(sock);
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");
1048 goto error;
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");
1068 goto error;
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) {
1089 continue;
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;
1100 } else {
1101 (*address_list)->next = address_item;
1104 info->value->has_ip_addresses = true;
1109 freeifaddrs(ifap);
1110 return head;
1112 error:
1113 freeifaddrs(ifap);
1114 qapi_free_GuestNetworkInterfaceList(head);
1115 return NULL;
1118 #define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
1120 static long sysconf_exact(int name, const char *name_str, Error **err)
1122 long ret;
1124 errno = 0;
1125 ret = sysconf(name);
1126 if (ret == -1) {
1127 if (errno == 0) {
1128 error_setg(err, "sysconf(%s): value indefinite", name_str);
1129 } else {
1130 error_setg_errno(err, errno, "sysconf(%s)", name_str);
1133 return ret;
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
1142 * - W: vcpu->online
1143 * - W: vcpu->can_offline
1145 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1146 * - R: vcpu->logical_id
1147 * - R: vcpu->online
1149 * Written members remain unmodified on error.
1151 static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
1152 Error **errp)
1154 char *dirpath;
1155 int dirfd;
1157 dirpath = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
1158 vcpu->logical_id);
1159 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
1160 if (dirfd == -1) {
1161 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
1162 } else {
1163 static const char fn[] = "online";
1164 int fd;
1165 int res;
1167 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
1168 if (fd == -1) {
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 */
1178 } else {
1179 unsigned char status;
1181 res = pread(fd, &status, 1, 0);
1182 if (res == -1) {
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,
1186 fn);
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,
1194 fn);
1196 } /* otherwise pretend successful re-(on|off)-lining */
1198 res = close(fd);
1199 g_assert(res == 0);
1202 res = close(dirfd);
1203 g_assert(res == 0);
1206 g_free(dirpath);
1209 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1211 int64_t current;
1212 GuestLogicalProcessorList *head, **link;
1213 long sc_max;
1214 Error *local_err = NULL;
1216 current = 0;
1217 head = NULL;
1218 link = &head;
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;
1233 *link = entry;
1234 link = &entry->next;
1237 if (local_err == NULL) {
1238 /* there's no guest with zero VCPUs */
1239 g_assert(head != NULL);
1240 return head;
1243 qapi_free_GuestLogicalProcessorList(head);
1244 error_propagate(errp, local_err);
1245 return NULL;
1248 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1250 int64_t processed;
1251 Error *local_err = NULL;
1253 processed = 0;
1254 while (vcpus != NULL) {
1255 transfer_vcpu(vcpus->value, false, &local_err);
1256 if (local_err != NULL) {
1257 break;
1259 ++processed;
1260 vcpus = vcpus->next;
1263 if (local_err != NULL) {
1264 if (processed == 0) {
1265 error_propagate(errp, local_err);
1266 } else {
1267 error_free(local_err);
1271 return processed;
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);
1294 return NULL;
1297 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
1299 error_set(errp, QERR_UNSUPPORTED);
1300 return NULL;
1303 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
1305 error_set(errp, QERR_UNSUPPORTED);
1306 return -1;
1309 #endif
1311 #if !defined(CONFIG_FSFREEZE)
1313 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
1315 error_set(err, QERR_UNSUPPORTED);
1317 return 0;
1320 int64_t qmp_guest_fsfreeze_freeze(Error **err)
1322 error_set(err, QERR_UNSUPPORTED);
1324 return 0;
1327 int64_t qmp_guest_fsfreeze_thaw(Error **err)
1329 error_set(err, QERR_UNSUPPORTED);
1331 return 0;
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);
1340 #endif
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);
1347 #endif
1348 ga_command_state_add(cs, guest_file_init, NULL);