2 * QEMU Guest Agent POSIX-specific command implementations
4 * Copyright IBM Corp. 2011
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <sys/ioctl.h>
16 #include <sys/utsname.h>
19 #include "guest-agent-core.h"
20 #include "qga-qapi-commands.h"
21 #include "qapi/error.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qemu/queue.h"
24 #include "qemu/host-utils.h"
25 #include "qemu/sockets.h"
26 #include "qemu/base64.h"
27 #include "qemu/cutils.h"
33 #ifndef CONFIG_HAS_ENVIRON
35 #include <crt_externs.h>
36 #define environ (*_NSGetEnviron())
38 extern char **environ;
42 #if defined(__linux__)
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
49 #include <sys/statvfs.h>
56 #define CONFIG_FSFREEZE
63 static void ga_wait_child(pid_t pid, int *status, Error **errp)
70 rpid = waitpid(pid, status, 0);
71 } while (rpid == -1 && errno == EINTR);
74 error_setg_errno(errp, errno, "failed to wait for child (pid: %d)",
79 g_assert(rpid == pid);
82 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
84 const char *shutdown_flag;
85 Error *local_err = NULL;
89 slog("guest-shutdown called, mode: %s", mode);
90 if (!has_mode || strcmp(mode, "powerdown") == 0) {
92 } else if (strcmp(mode, "halt") == 0) {
94 } else if (strcmp(mode, "reboot") == 0) {
98 "mode is invalid (valid values are: halt|powerdown|reboot");
104 /* child, start the shutdown */
106 reopen_fd_to_null(0);
107 reopen_fd_to_null(1);
108 reopen_fd_to_null(2);
110 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
111 "hypervisor initiated shutdown", (char*)NULL, environ);
113 } else if (pid < 0) {
114 error_setg_errno(errp, errno, "failed to create child process");
118 ga_wait_child(pid, &status, &local_err);
120 error_propagate(errp, local_err);
124 if (!WIFEXITED(status)) {
125 error_setg(errp, "child process has terminated abnormally");
129 if (WEXITSTATUS(status)) {
130 error_setg(errp, "child process has failed to shutdown");
137 int64_t qmp_guest_get_time(Error **errp)
142 ret = qemu_gettimeofday(&tq);
144 error_setg_errno(errp, errno, "Failed to get time");
148 return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
151 void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
156 Error *local_err = NULL;
159 /* If user has passed a time, validate and set it. */
163 /* year-2038 will overflow in case time_t is 32bit */
164 if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
165 error_setg(errp, "Time %" PRId64 " is too large", time_ns);
169 tv.tv_sec = time_ns / 1000000000;
170 tv.tv_usec = (time_ns % 1000000000) / 1000;
171 g_date_set_time_t(&date, tv.tv_sec);
172 if (date.year < 1970 || date.year >= 2070) {
173 error_setg_errno(errp, errno, "Invalid time");
177 ret = settimeofday(&tv, NULL);
179 error_setg_errno(errp, errno, "Failed to set time to guest");
184 /* Now, if user has passed a time to set and the system time is set, we
185 * just need to synchronize the hardware clock. However, if no time was
186 * passed, user is requesting the opposite: set the system time from the
187 * hardware clock (RTC). */
191 reopen_fd_to_null(0);
192 reopen_fd_to_null(1);
193 reopen_fd_to_null(2);
195 /* Use '/sbin/hwclock -w' to set RTC from the system time,
196 * or '/sbin/hwclock -s' to set the system time from RTC. */
197 execle("/sbin/hwclock", "hwclock", has_time ? "-w" : "-s",
200 } else if (pid < 0) {
201 error_setg_errno(errp, errno, "failed to create child process");
205 ga_wait_child(pid, &status, &local_err);
207 error_propagate(errp, local_err);
211 if (!WIFEXITED(status)) {
212 error_setg(errp, "child process has terminated abnormally");
216 if (WEXITSTATUS(status)) {
217 error_setg(errp, "hwclock failed to set hardware clock to system time");
228 typedef struct GuestFileHandle {
232 QTAILQ_ENTRY(GuestFileHandle) next;
236 QTAILQ_HEAD(, GuestFileHandle) filehandles;
237 } guest_file_state = {
238 .filehandles = QTAILQ_HEAD_INITIALIZER(guest_file_state.filehandles),
241 static int64_t guest_file_handle_add(FILE *fh, Error **errp)
243 GuestFileHandle *gfh;
246 handle = ga_get_fd_handle(ga_state, errp);
251 gfh = g_new0(GuestFileHandle, 1);
254 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
259 static GuestFileHandle *guest_file_handle_find(int64_t id, Error **errp)
261 GuestFileHandle *gfh;
263 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
270 error_setg(errp, "handle '%" PRId64 "' has not been found", id);
274 typedef const char * const ccpc;
280 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
281 static const struct {
284 } guest_file_open_modes[] = {
285 { (ccpc[]){ "r", NULL }, O_RDONLY },
286 { (ccpc[]){ "rb", NULL }, O_RDONLY | O_BINARY },
287 { (ccpc[]){ "w", NULL }, O_WRONLY | O_CREAT | O_TRUNC },
288 { (ccpc[]){ "wb", NULL }, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY },
289 { (ccpc[]){ "a", NULL }, O_WRONLY | O_CREAT | O_APPEND },
290 { (ccpc[]){ "ab", NULL }, O_WRONLY | O_CREAT | O_APPEND | O_BINARY },
291 { (ccpc[]){ "r+", NULL }, O_RDWR },
292 { (ccpc[]){ "rb+", "r+b", NULL }, O_RDWR | O_BINARY },
293 { (ccpc[]){ "w+", NULL }, O_RDWR | O_CREAT | O_TRUNC },
294 { (ccpc[]){ "wb+", "w+b", NULL }, O_RDWR | O_CREAT | O_TRUNC | O_BINARY },
295 { (ccpc[]){ "a+", NULL }, O_RDWR | O_CREAT | O_APPEND },
296 { (ccpc[]){ "ab+", "a+b", NULL }, O_RDWR | O_CREAT | O_APPEND | O_BINARY }
300 find_open_flag(const char *mode_str, Error **errp)
304 for (mode = 0; mode < ARRAY_SIZE(guest_file_open_modes); ++mode) {
307 form = guest_file_open_modes[mode].forms;
308 while (*form != NULL && strcmp(*form, mode_str) != 0) {
316 if (mode == ARRAY_SIZE(guest_file_open_modes)) {
317 error_setg(errp, "invalid file open mode '%s'", mode_str);
320 return guest_file_open_modes[mode].oflag_base | O_NOCTTY | O_NONBLOCK;
323 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
324 S_IRGRP | S_IWGRP | \
328 safe_open_or_create(const char *path, const char *mode, Error **errp)
330 Error *local_err = NULL;
333 oflag = find_open_flag(mode, &local_err);
334 if (local_err == NULL) {
337 /* If the caller wants / allows creation of a new file, we implement it
338 * with a two step process: open() + (open() / fchmod()).
340 * First we insist on creating the file exclusively as a new file. If
341 * that succeeds, we're free to set any file-mode bits on it. (The
342 * motivation is that we want to set those file-mode bits independently
343 * of the current umask.)
345 * If the exclusive creation fails because the file already exists
346 * (EEXIST is not possible for any other reason), we just attempt to
347 * open the file, but in this case we won't be allowed to change the
348 * file-mode bits on the preexistent file.
350 * The pathname should never disappear between the two open()s in
351 * practice. If it happens, then someone very likely tried to race us.
352 * In this case just go ahead and report the ENOENT from the second
353 * open() to the caller.
355 * If the caller wants to open a preexistent file, then the first
356 * open() is decisive and its third argument is ignored, and the second
357 * open() and the fchmod() are never called.
359 fd = open(path, oflag | ((oflag & O_CREAT) ? O_EXCL : 0), 0);
360 if (fd == -1 && errno == EEXIST) {
361 oflag &= ~(unsigned)O_CREAT;
362 fd = open(path, oflag);
366 error_setg_errno(&local_err, errno, "failed to open file '%s' "
367 "(mode: '%s')", path, mode);
369 qemu_set_cloexec(fd);
371 if ((oflag & O_CREAT) && fchmod(fd, DEFAULT_NEW_FILE_MODE) == -1) {
372 error_setg_errno(&local_err, errno, "failed to set permission "
373 "0%03o on new file '%s' (mode: '%s')",
374 (unsigned)DEFAULT_NEW_FILE_MODE, path, mode);
378 f = fdopen(fd, mode);
380 error_setg_errno(&local_err, errno, "failed to associate "
381 "stdio stream with file descriptor %d, "
382 "file '%s' (mode: '%s')", fd, path, mode);
389 if (oflag & O_CREAT) {
395 error_propagate(errp, local_err);
399 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode,
403 Error *local_err = NULL;
409 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
410 fh = safe_open_or_create(path, mode, &local_err);
411 if (local_err != NULL) {
412 error_propagate(errp, local_err);
416 /* set fd non-blocking to avoid common use cases (like reading from a
417 * named pipe) from hanging the agent
419 qemu_set_nonblock(fileno(fh));
421 handle = guest_file_handle_add(fh, errp);
427 slog("guest-file-open, handle: %" PRId64, handle);
431 void qmp_guest_file_close(int64_t handle, Error **errp)
433 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
436 slog("guest-file-close called, handle: %" PRId64, handle);
441 ret = fclose(gfh->fh);
443 error_setg_errno(errp, errno, "failed to close handle");
447 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
451 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
452 int64_t count, Error **errp)
454 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
455 GuestFileRead *read_data = NULL;
465 count = QGA_READ_COUNT_DEFAULT;
466 } else if (count < 0 || count >= UINT32_MAX) {
467 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
474 /* explicitly flush when switching from writing to reading */
475 if (gfh->state == RW_STATE_WRITING) {
476 int ret = fflush(fh);
478 error_setg_errno(errp, errno, "failed to flush file");
481 gfh->state = RW_STATE_NEW;
484 buf = g_malloc0(count+1);
485 read_count = fread(buf, 1, count, fh);
487 error_setg_errno(errp, errno, "failed to read file");
488 slog("guest-file-read failed, handle: %" PRId64, handle);
491 read_data = g_new0(GuestFileRead, 1);
492 read_data->count = read_count;
493 read_data->eof = feof(fh);
495 read_data->buf_b64 = g_base64_encode(buf, read_count);
497 gfh->state = RW_STATE_READING;
505 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
506 bool has_count, int64_t count,
509 GuestFileWrite *write_data = NULL;
513 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
522 if (gfh->state == RW_STATE_READING) {
523 int ret = fseek(fh, 0, SEEK_CUR);
525 error_setg_errno(errp, errno, "failed to seek file");
528 gfh->state = RW_STATE_NEW;
531 buf = qbase64_decode(buf_b64, -1, &buf_len, errp);
538 } else if (count < 0 || count > buf_len) {
539 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
545 write_count = fwrite(buf, 1, count, fh);
547 error_setg_errno(errp, errno, "failed to write to file");
548 slog("guest-file-write failed, handle: %" PRId64, handle);
550 write_data = g_new0(GuestFileWrite, 1);
551 write_data->count = write_count;
552 write_data->eof = feof(fh);
553 gfh->state = RW_STATE_WRITING;
561 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
562 GuestFileWhence *whence_code,
565 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
566 GuestFileSeek *seek_data = NULL;
576 /* We stupidly exposed 'whence':'int' in our qapi */
577 whence = ga_parse_whence(whence_code, &err);
579 error_propagate(errp, err);
584 ret = fseek(fh, offset, whence);
586 error_setg_errno(errp, errno, "failed to seek file");
587 if (errno == ESPIPE) {
588 /* file is non-seekable, stdio shouldn't be buffering anyways */
589 gfh->state = RW_STATE_NEW;
592 seek_data = g_new0(GuestFileSeek, 1);
593 seek_data->position = ftell(fh);
594 seek_data->eof = feof(fh);
595 gfh->state = RW_STATE_NEW;
602 void qmp_guest_file_flush(int64_t handle, Error **errp)
604 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
615 error_setg_errno(errp, errno, "failed to flush file");
617 gfh->state = RW_STATE_NEW;
621 /* linux-specific implementations. avoid this if at all possible. */
622 #if defined(__linux__)
624 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
625 typedef struct FsMount {
628 unsigned int devmajor, devminor;
629 QTAILQ_ENTRY(FsMount) next;
632 typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
634 static void free_fs_mount_list(FsMountList *mounts)
636 FsMount *mount, *temp;
642 QTAILQ_FOREACH_SAFE(mount, mounts, next, temp) {
643 QTAILQ_REMOVE(mounts, mount, next);
644 g_free(mount->dirname);
645 g_free(mount->devtype);
650 static int dev_major_minor(const char *devpath,
651 unsigned int *devmajor, unsigned int *devminor)
658 if (stat(devpath, &st) < 0) {
659 slog("failed to stat device file '%s': %s", devpath, strerror(errno));
662 if (S_ISDIR(st.st_mode)) {
663 /* It is bind mount */
666 if (S_ISBLK(st.st_mode)) {
667 *devmajor = major(st.st_rdev);
668 *devminor = minor(st.st_rdev);
675 * Walk the mount table and build a list of local file systems
677 static void build_fs_mount_list_from_mtab(FsMountList *mounts, Error **errp)
681 char const *mtab = "/proc/self/mounts";
683 unsigned int devmajor, devminor;
685 fp = setmntent(mtab, "r");
687 error_setg(errp, "failed to open mtab file: '%s'", mtab);
691 while ((ment = getmntent(fp))) {
693 * An entry which device name doesn't start with a '/' is
694 * either a dummy file system or a network file system.
695 * Add special handling for smbfs and cifs as is done by
698 if ((ment->mnt_fsname[0] != '/') ||
699 (strcmp(ment->mnt_type, "smbfs") == 0) ||
700 (strcmp(ment->mnt_type, "cifs") == 0)) {
703 if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) == -2) {
704 /* Skip bind mounts */
708 mount = g_new0(FsMount, 1);
709 mount->dirname = g_strdup(ment->mnt_dir);
710 mount->devtype = g_strdup(ment->mnt_type);
711 mount->devmajor = devmajor;
712 mount->devminor = devminor;
714 QTAILQ_INSERT_TAIL(mounts, mount, next);
720 static void decode_mntname(char *name, int len)
723 for (i = 0; i <= len; i++) {
724 if (name[i] != '\\') {
726 } else if (name[i + 1] == '\\') {
729 } else if (name[i + 1] >= '0' && name[i + 1] <= '3' &&
730 name[i + 2] >= '0' && name[i + 2] <= '7' &&
731 name[i + 3] >= '0' && name[i + 3] <= '7') {
732 name[j++] = (name[i + 1] - '0') * 64 +
733 (name[i + 2] - '0') * 8 +
742 static void build_fs_mount_list(FsMountList *mounts, Error **errp)
745 char const *mountinfo = "/proc/self/mountinfo";
747 char *line = NULL, *dash;
750 unsigned int devmajor, devminor;
751 int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e;
753 fp = fopen(mountinfo, "r");
755 build_fs_mount_list_from_mtab(mounts, errp);
759 while (getline(&line, &n, fp) != -1) {
760 ret = sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c",
761 &devmajor, &devminor, &dir_s, &dir_e, &check);
765 dash = strstr(line + dir_e, " - ");
769 ret = sscanf(dash, " - %n%*s%n %n%*s%n%c",
770 &type_s, &type_e, &dev_s, &dev_e, &check);
777 decode_mntname(line + dir_s, dir_e - dir_s);
778 decode_mntname(dash + dev_s, dev_e - dev_s);
780 /* btrfs reports major number = 0 */
781 if (strcmp("btrfs", dash + type_s) != 0 ||
782 dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) {
787 mount = g_new0(FsMount, 1);
788 mount->dirname = g_strdup(line + dir_s);
789 mount->devtype = g_strdup(dash + type_s);
790 mount->devmajor = devmajor;
791 mount->devminor = devminor;
793 QTAILQ_INSERT_TAIL(mounts, mount, next);
801 #if defined(CONFIG_FSFREEZE)
803 static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
811 path = g_strndup(syspath, pathlen);
812 dpath = g_strdup_printf("%s/driver", path);
813 len = readlink(dpath, buf, sizeof(buf) - 1);
816 driver = g_path_get_basename(buf);
823 static int compare_uint(const void *_a, const void *_b)
825 unsigned int a = *(unsigned int *)_a;
826 unsigned int b = *(unsigned int *)_b;
828 return a < b ? -1 : a > b ? 1 : 0;
831 /* Walk the specified sysfs and build a sorted list of host or ata numbers */
832 static int build_hosts(char const *syspath, char const *host, bool ata,
833 unsigned int *hosts, int hosts_max, Error **errp)
837 struct dirent *entry;
840 path = g_strndup(syspath, host - syspath);
843 error_setg_errno(errp, errno, "opendir(\"%s\")", path);
848 while (i < hosts_max) {
849 entry = readdir(dir);
853 if (ata && sscanf(entry->d_name, "ata%d", hosts + i) == 1) {
855 } else if (!ata && sscanf(entry->d_name, "host%d", hosts + i) == 1) {
860 qsort(hosts, i, sizeof(hosts[0]), compare_uint);
867 /* Store disk device info specified by @sysfs into @fs */
868 static void build_guest_fsinfo_for_real_device(char const *syspath,
869 GuestFilesystemInfo *fs,
872 unsigned int pci[4], host, hosts[8], tgt[3];
873 int i, nhosts = 0, pcilen;
874 GuestDiskAddress *disk;
875 GuestPCIAddress *pciaddr;
876 GuestDiskAddressList *list = NULL;
877 bool has_ata = false, has_host = false, has_tgt = false;
878 char *p, *q, *driver = NULL;
879 #ifdef CONFIG_LIBUDEV
880 struct udev *udev = NULL;
881 struct udev_device *udevice = NULL;
884 p = strstr(syspath, "/devices/pci");
885 if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
886 pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) {
887 g_debug("only pci device is supported: sysfs path '%s'", syspath);
893 driver = get_pci_driver(syspath, p - syspath, errp);
894 if (driver && (g_str_equal(driver, "ata_piix") ||
895 g_str_equal(driver, "sym53c8xx") ||
896 g_str_equal(driver, "virtio-pci") ||
897 g_str_equal(driver, "ahci"))) {
902 if (sscanf(p, "/%x:%x:%x.%x%n",
903 pci, pci + 1, pci + 2, pci + 3, &pcilen) == 4) {
908 g_debug("unsupported driver or sysfs path '%s'", syspath);
912 p = strstr(syspath, "/target");
913 if (p && sscanf(p + 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
914 tgt, tgt + 1, tgt + 2) == 3) {
918 p = strstr(syspath, "/ata");
923 p = strstr(syspath, "/host");
926 if (p && sscanf(q, "%u", &host) == 1) {
928 nhosts = build_hosts(syspath, p, has_ata, hosts,
929 ARRAY_SIZE(hosts), errp);
935 pciaddr = g_malloc0(sizeof(*pciaddr));
936 pciaddr->domain = pci[0];
937 pciaddr->bus = pci[1];
938 pciaddr->slot = pci[2];
939 pciaddr->function = pci[3];
941 disk = g_malloc0(sizeof(*disk));
942 disk->pci_controller = pciaddr;
944 list = g_malloc0(sizeof(*list));
947 #ifdef CONFIG_LIBUDEV
949 udevice = udev_device_new_from_syspath(udev, syspath);
950 if (udev == NULL || udevice == NULL) {
951 g_debug("failed to query udev");
953 const char *devnode, *serial;
954 devnode = udev_device_get_devnode(udevice);
955 if (devnode != NULL) {
956 disk->dev = g_strdup(devnode);
957 disk->has_dev = true;
959 serial = udev_device_get_property_value(udevice, "ID_SERIAL");
960 if (serial != NULL && *serial != 0) {
961 disk->serial = g_strdup(serial);
962 disk->has_serial = true;
967 if (strcmp(driver, "ata_piix") == 0) {
968 /* a host per ide bus, target*:0:<unit>:0 */
969 if (!has_host || !has_tgt) {
970 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
973 for (i = 0; i < nhosts; i++) {
974 if (host == hosts[i]) {
975 disk->bus_type = GUEST_DISK_BUS_TYPE_IDE;
982 g_debug("no host for '%s' (driver '%s')", syspath, driver);
985 } else if (strcmp(driver, "sym53c8xx") == 0) {
986 /* scsi(LSI Logic): target*:0:<unit>:0 */
988 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
991 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
993 } else if (strcmp(driver, "virtio-pci") == 0) {
995 /* virtio-scsi: target*:0:0:<unit> */
996 disk->bus_type = GUEST_DISK_BUS_TYPE_SCSI;
999 /* virtio-blk: 1 disk per 1 device */
1000 disk->bus_type = GUEST_DISK_BUS_TYPE_VIRTIO;
1002 } else if (strcmp(driver, "ahci") == 0) {
1003 /* ahci: 1 host per 1 unit */
1004 if (!has_host || !has_tgt) {
1005 g_debug("invalid sysfs path '%s' (driver '%s')", syspath, driver);
1008 for (i = 0; i < nhosts; i++) {
1009 if (host == hosts[i]) {
1011 disk->bus_type = GUEST_DISK_BUS_TYPE_SATA;
1016 g_debug("no host for '%s' (driver '%s')", syspath, driver);
1020 g_debug("unknown driver '%s' (sysfs path '%s')", driver, syspath);
1024 list->next = fs->disk;
1030 qapi_free_GuestDiskAddressList(list);
1034 #ifdef CONFIG_LIBUDEV
1036 udev_device_unref(udevice);
1041 static void build_guest_fsinfo_for_device(char const *devpath,
1042 GuestFilesystemInfo *fs,
1045 /* Store a list of slave devices of virtual volume specified by @syspath into
1047 static void build_guest_fsinfo_for_virtual_device(char const *syspath,
1048 GuestFilesystemInfo *fs,
1053 struct dirent *entry;
1055 dirpath = g_strdup_printf("%s/slaves", syspath);
1056 dir = opendir(dirpath);
1058 if (errno != ENOENT) {
1059 error_setg_errno(errp, errno, "opendir(\"%s\")", dirpath);
1067 entry = readdir(dir);
1068 if (entry == NULL) {
1070 error_setg_errno(errp, errno, "readdir(\"%s\")", dirpath);
1075 if (entry->d_type == DT_LNK) {
1078 g_debug(" slave device '%s'", entry->d_name);
1079 path = g_strdup_printf("%s/slaves/%s", syspath, entry->d_name);
1080 build_guest_fsinfo_for_device(path, fs, errp);
1093 /* Dispatch to functions for virtual/real device */
1094 static void build_guest_fsinfo_for_device(char const *devpath,
1095 GuestFilesystemInfo *fs,
1098 char *syspath = realpath(devpath, NULL);
1101 error_setg_errno(errp, errno, "realpath(\"%s\")", devpath);
1106 fs->name = g_path_get_basename(syspath);
1109 g_debug(" parse sysfs path '%s'", syspath);
1111 if (strstr(syspath, "/devices/virtual/block/")) {
1112 build_guest_fsinfo_for_virtual_device(syspath, fs, errp);
1114 build_guest_fsinfo_for_real_device(syspath, fs, errp);
1120 /* Return a list of the disk device(s)' info which @mount lies on */
1121 static GuestFilesystemInfo *build_guest_fsinfo(struct FsMount *mount,
1124 GuestFilesystemInfo *fs = g_malloc0(sizeof(*fs));
1126 unsigned long used, nonroot_total, fr_size;
1127 char *devpath = g_strdup_printf("/sys/dev/block/%u:%u",
1128 mount->devmajor, mount->devminor);
1130 fs->mountpoint = g_strdup(mount->dirname);
1131 fs->type = g_strdup(mount->devtype);
1132 build_guest_fsinfo_for_device(devpath, fs, errp);
1134 if (statvfs(fs->mountpoint, &buf) == 0) {
1135 fr_size = buf.f_frsize;
1136 used = buf.f_blocks - buf.f_bfree;
1137 nonroot_total = used + buf.f_bavail;
1138 fs->used_bytes = used * fr_size;
1139 fs->total_bytes = nonroot_total * fr_size;
1141 fs->has_total_bytes = true;
1142 fs->has_used_bytes = true;
1150 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
1153 struct FsMount *mount;
1154 GuestFilesystemInfoList *new, *ret = NULL;
1155 Error *local_err = NULL;
1157 QTAILQ_INIT(&mounts);
1158 build_fs_mount_list(&mounts, &local_err);
1160 error_propagate(errp, local_err);
1164 QTAILQ_FOREACH(mount, &mounts, next) {
1165 g_debug("Building guest fsinfo for '%s'", mount->dirname);
1167 new = g_malloc0(sizeof(*ret));
1168 new->value = build_guest_fsinfo(mount, &local_err);
1172 error_propagate(errp, local_err);
1173 qapi_free_GuestFilesystemInfoList(ret);
1179 free_fs_mount_list(&mounts);
1185 FSFREEZE_HOOK_THAW = 0,
1186 FSFREEZE_HOOK_FREEZE,
1189 static const char *fsfreeze_hook_arg_string[] = {
1194 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
1199 const char *arg_str = fsfreeze_hook_arg_string[arg];
1200 Error *local_err = NULL;
1202 hook = ga_fsfreeze_hook(ga_state);
1206 if (access(hook, X_OK) != 0) {
1207 error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook);
1211 slog("executing fsfreeze hook with arg '%s'", arg_str);
1215 reopen_fd_to_null(0);
1216 reopen_fd_to_null(1);
1217 reopen_fd_to_null(2);
1219 execle(hook, hook, arg_str, NULL, environ);
1220 _exit(EXIT_FAILURE);
1221 } else if (pid < 0) {
1222 error_setg_errno(errp, errno, "failed to create child process");
1226 ga_wait_child(pid, &status, &local_err);
1228 error_propagate(errp, local_err);
1232 if (!WIFEXITED(status)) {
1233 error_setg(errp, "fsfreeze hook has terminated abnormally");
1237 status = WEXITSTATUS(status);
1239 error_setg(errp, "fsfreeze hook has failed with status %d", status);
1245 * Return status of freeze/thaw
1247 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
1249 if (ga_is_frozen(ga_state)) {
1250 return GUEST_FSFREEZE_STATUS_FROZEN;
1253 return GUEST_FSFREEZE_STATUS_THAWED;
1256 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
1258 return qmp_guest_fsfreeze_freeze_list(false, NULL, errp);
1262 * Walk list of mounted file systems in the guest, and freeze the ones which
1263 * are real local file systems.
1265 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
1266 strList *mountpoints,
1272 struct FsMount *mount;
1273 Error *local_err = NULL;
1276 slog("guest-fsfreeze called");
1278 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
1280 error_propagate(errp, local_err);
1284 QTAILQ_INIT(&mounts);
1285 build_fs_mount_list(&mounts, &local_err);
1287 error_propagate(errp, local_err);
1291 /* cannot risk guest agent blocking itself on a write in this state */
1292 ga_set_frozen(ga_state);
1294 QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
1295 /* To issue fsfreeze in the reverse order of mounts, check if the
1296 * mount is listed in the list here */
1297 if (has_mountpoints) {
1298 for (list = mountpoints; list; list = list->next) {
1299 if (strcmp(list->value, mount->dirname) == 0) {
1308 fd = qemu_open(mount->dirname, O_RDONLY);
1310 error_setg_errno(errp, errno, "failed to open %s", mount->dirname);
1314 /* we try to cull filesystems we know won't work in advance, but other
1315 * filesystems may not implement fsfreeze for less obvious reasons.
1316 * these will report EOPNOTSUPP. we simply ignore these when tallying
1317 * the number of frozen filesystems.
1318 * if a filesystem is mounted more than once (aka bind mount) a
1319 * consecutive attempt to freeze an already frozen filesystem will
1322 * any other error means a failure to freeze a filesystem we
1323 * expect to be freezable, so return an error in those cases
1324 * and return system to thawed state.
1326 ret = ioctl(fd, FIFREEZE);
1328 if (errno != EOPNOTSUPP && errno != EBUSY) {
1329 error_setg_errno(errp, errno, "failed to freeze %s",
1340 free_fs_mount_list(&mounts);
1341 /* We may not issue any FIFREEZE here.
1342 * Just unset ga_state here and ready for the next call.
1345 ga_unset_frozen(ga_state);
1350 free_fs_mount_list(&mounts);
1351 qmp_guest_fsfreeze_thaw(NULL);
1356 * Walk list of frozen file systems in the guest, and thaw them.
1358 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
1363 int fd, i = 0, logged;
1364 Error *local_err = NULL;
1366 QTAILQ_INIT(&mounts);
1367 build_fs_mount_list(&mounts, &local_err);
1369 error_propagate(errp, local_err);
1373 QTAILQ_FOREACH(mount, &mounts, next) {
1375 fd = qemu_open(mount->dirname, O_RDONLY);
1379 /* we have no way of knowing whether a filesystem was actually unfrozen
1380 * as a result of a successful call to FITHAW, only that if an error
1381 * was returned the filesystem was *not* unfrozen by that particular
1384 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
1385 * to unfreeze, continuing issuing FITHAW until an error is returned,
1386 * in which case either the filesystem is in an unfreezable state, or,
1387 * more likely, it was thawed previously (and remains so afterward).
1389 * also, since the most recent successful call is the one that did
1390 * the actual unfreeze, we can use this to provide an accurate count
1391 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1392 * may * be useful for determining whether a filesystem was unfrozen
1393 * during the freeze/thaw phase by a process other than qemu-ga.
1396 ret = ioctl(fd, FITHAW);
1397 if (ret == 0 && !logged) {
1405 ga_unset_frozen(ga_state);
1406 free_fs_mount_list(&mounts);
1408 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
1413 static void guest_fsfreeze_cleanup(void)
1417 if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
1418 qmp_guest_fsfreeze_thaw(&err);
1420 slog("failed to clean up frozen filesystems: %s",
1421 error_get_pretty(err));
1426 #endif /* CONFIG_FSFREEZE */
1428 #if defined(CONFIG_FSTRIM)
1430 * Walk list of mounted file systems in the guest, and trim them.
1432 GuestFilesystemTrimResponse *
1433 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
1435 GuestFilesystemTrimResponse *response;
1436 GuestFilesystemTrimResultList *list;
1437 GuestFilesystemTrimResult *result;
1440 struct FsMount *mount;
1442 Error *local_err = NULL;
1443 struct fstrim_range r;
1445 slog("guest-fstrim called");
1447 QTAILQ_INIT(&mounts);
1448 build_fs_mount_list(&mounts, &local_err);
1450 error_propagate(errp, local_err);
1454 response = g_malloc0(sizeof(*response));
1456 QTAILQ_FOREACH(mount, &mounts, next) {
1457 result = g_malloc0(sizeof(*result));
1458 result->path = g_strdup(mount->dirname);
1460 list = g_malloc0(sizeof(*list));
1461 list->value = result;
1462 list->next = response->paths;
1463 response->paths = list;
1465 fd = qemu_open(mount->dirname, O_RDONLY);
1467 result->error = g_strdup_printf("failed to open: %s",
1469 result->has_error = true;
1473 /* We try to cull filesystems we know won't work in advance, but other
1474 * filesystems may not implement fstrim for less obvious reasons.
1475 * These will report EOPNOTSUPP; while in some other cases ENOTTY
1476 * will be reported (e.g. CD-ROMs).
1477 * Any other error means an unexpected error.
1481 r.minlen = has_minimum ? minimum : 0;
1482 ret = ioctl(fd, FITRIM, &r);
1484 result->has_error = true;
1485 if (errno == ENOTTY || errno == EOPNOTSUPP) {
1486 result->error = g_strdup("trim not supported");
1488 result->error = g_strdup_printf("failed to trim: %s",
1495 result->has_minimum = true;
1496 result->minimum = r.minlen;
1497 result->has_trimmed = true;
1498 result->trimmed = r.len;
1502 free_fs_mount_list(&mounts);
1505 #endif /* CONFIG_FSTRIM */
1508 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1509 #define SUSPEND_SUPPORTED 0
1510 #define SUSPEND_NOT_SUPPORTED 1
1513 SUSPEND_MODE_DISK = 0,
1514 SUSPEND_MODE_RAM = 1,
1515 SUSPEND_MODE_HYBRID = 2,
1519 * Executes a command in a child process using g_spawn_sync,
1520 * returning an int >= 0 representing the exit status of the
1523 * If the program wasn't found in path, returns -1.
1525 * If a problem happened when creating the child process,
1526 * returns -1 and errp is set.
1528 static int run_process_child(const char *command[], Error **errp)
1530 int exit_status, spawn_flag;
1531 GError *g_err = NULL;
1534 spawn_flag = G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL |
1535 G_SPAWN_STDERR_TO_DEV_NULL;
1537 success = g_spawn_sync(NULL, (char **)command, environ, spawn_flag,
1538 NULL, NULL, NULL, NULL,
1539 &exit_status, &g_err);
1542 return WEXITSTATUS(exit_status);
1545 if (g_err && (g_err->code != G_SPAWN_ERROR_NOENT)) {
1546 error_setg(errp, "failed to create child process, error '%s'",
1550 g_error_free(g_err);
1554 static bool systemd_supports_mode(SuspendMode mode, Error **errp)
1556 Error *local_err = NULL;
1557 const char *systemctl_args[3] = {"systemd-hibernate", "systemd-suspend",
1558 "systemd-hybrid-sleep"};
1559 const char *cmd[4] = {"systemctl", "status", systemctl_args[mode], NULL};
1562 status = run_process_child(cmd, &local_err);
1565 * systemctl status uses LSB return codes so we can expect
1566 * status > 0 and be ok. To assert if the guest has support
1567 * for the selected suspend mode, status should be < 4. 4 is
1568 * the code for unknown service status, the return value when
1569 * the service does not exist. A common value is status = 3
1570 * (program is not running).
1572 if (status > 0 && status < 4) {
1577 error_propagate(errp, local_err);
1583 static void systemd_suspend(SuspendMode mode, Error **errp)
1585 Error *local_err = NULL;
1586 const char *systemctl_args[3] = {"hibernate", "suspend", "hybrid-sleep"};
1587 const char *cmd[3] = {"systemctl", systemctl_args[mode], NULL};
1590 status = run_process_child(cmd, &local_err);
1596 if ((status == -1) && !local_err) {
1597 error_setg(errp, "the helper program 'systemctl %s' was not found",
1598 systemctl_args[mode]);
1603 error_propagate(errp, local_err);
1605 error_setg(errp, "the helper program 'systemctl %s' returned an "
1606 "unexpected exit status code (%d)",
1607 systemctl_args[mode], status);
1611 static bool pmutils_supports_mode(SuspendMode mode, Error **errp)
1613 Error *local_err = NULL;
1614 const char *pmutils_args[3] = {"--hibernate", "--suspend",
1615 "--suspend-hybrid"};
1616 const char *cmd[3] = {"pm-is-supported", pmutils_args[mode], NULL};
1619 status = run_process_child(cmd, &local_err);
1621 if (status == SUSPEND_SUPPORTED) {
1625 if ((status == -1) && !local_err) {
1630 error_propagate(errp, local_err);
1633 "the helper program '%s' returned an unexpected exit"
1634 " status code (%d)", "pm-is-supported", status);
1640 static void pmutils_suspend(SuspendMode mode, Error **errp)
1642 Error *local_err = NULL;
1643 const char *pmutils_binaries[3] = {"pm-hibernate", "pm-suspend",
1644 "pm-suspend-hybrid"};
1645 const char *cmd[2] = {pmutils_binaries[mode], NULL};
1648 status = run_process_child(cmd, &local_err);
1654 if ((status == -1) && !local_err) {
1655 error_setg(errp, "the helper program '%s' was not found",
1656 pmutils_binaries[mode]);
1661 error_propagate(errp, local_err);
1664 "the helper program '%s' returned an unexpected exit"
1665 " status code (%d)", pmutils_binaries[mode], status);
1669 static bool linux_sys_state_supports_mode(SuspendMode mode, Error **errp)
1671 const char *sysfile_strs[3] = {"disk", "mem", NULL};
1672 const char *sysfile_str = sysfile_strs[mode];
1673 char buf[32]; /* hopefully big enough */
1678 error_setg(errp, "unknown guest suspend mode");
1682 fd = open(LINUX_SYS_STATE_FILE, O_RDONLY);
1687 ret = read(fd, buf, sizeof(buf) - 1);
1694 if (strstr(buf, sysfile_str)) {
1700 static void linux_sys_state_suspend(SuspendMode mode, Error **errp)
1702 Error *local_err = NULL;
1703 const char *sysfile_strs[3] = {"disk", "mem", NULL};
1704 const char *sysfile_str = sysfile_strs[mode];
1709 error_setg(errp, "unknown guest suspend mode");
1719 reopen_fd_to_null(0);
1720 reopen_fd_to_null(1);
1721 reopen_fd_to_null(2);
1723 fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
1725 _exit(EXIT_FAILURE);
1728 if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
1729 _exit(EXIT_FAILURE);
1732 _exit(EXIT_SUCCESS);
1733 } else if (pid < 0) {
1734 error_setg_errno(errp, errno, "failed to create child process");
1738 ga_wait_child(pid, &status, &local_err);
1740 error_propagate(errp, local_err);
1744 if (WEXITSTATUS(status)) {
1745 error_setg(errp, "child process has failed to suspend");
1750 static void guest_suspend(SuspendMode mode, Error **errp)
1752 Error *local_err = NULL;
1753 bool mode_supported = false;
1755 if (systemd_supports_mode(mode, &local_err)) {
1756 mode_supported = true;
1757 systemd_suspend(mode, &local_err);
1764 error_free(local_err);
1766 if (pmutils_supports_mode(mode, &local_err)) {
1767 mode_supported = true;
1768 pmutils_suspend(mode, &local_err);
1775 error_free(local_err);
1777 if (linux_sys_state_supports_mode(mode, &local_err)) {
1778 mode_supported = true;
1779 linux_sys_state_suspend(mode, &local_err);
1782 if (!mode_supported) {
1784 "the requested suspend mode is not supported by the guest");
1785 } else if (local_err) {
1786 error_propagate(errp, local_err);
1790 void qmp_guest_suspend_disk(Error **errp)
1792 guest_suspend(SUSPEND_MODE_DISK, errp);
1795 void qmp_guest_suspend_ram(Error **errp)
1797 guest_suspend(SUSPEND_MODE_RAM, errp);
1800 void qmp_guest_suspend_hybrid(Error **errp)
1802 guest_suspend(SUSPEND_MODE_HYBRID, errp);
1805 static GuestNetworkInterfaceList *
1806 guest_find_interface(GuestNetworkInterfaceList *head,
1809 for (; head; head = head->next) {
1810 if (strcmp(head->value->name, name) == 0) {
1818 static int guest_get_network_stats(const char *name,
1819 GuestNetworkInterfaceStat *stats)
1822 char const *devinfo = "/proc/net/dev";
1824 char *line = NULL, *colon;
1826 fp = fopen(devinfo, "r");
1830 name_len = strlen(name);
1831 while (getline(&line, &n, fp) != -1) {
1834 long long rx_packets;
1836 long long rx_dropped;
1838 long long tx_packets;
1840 long long tx_dropped;
1842 trim_line = g_strchug(line);
1843 if (trim_line[0] == '\0') {
1846 colon = strchr(trim_line, ':');
1850 if (colon - name_len == trim_line &&
1851 strncmp(trim_line, name, name_len) == 0) {
1852 if (sscanf(colon + 1,
1853 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld",
1854 &rx_bytes, &rx_packets, &rx_errs, &rx_dropped,
1855 &dummy, &dummy, &dummy, &dummy,
1856 &tx_bytes, &tx_packets, &tx_errs, &tx_dropped,
1857 &dummy, &dummy, &dummy, &dummy) != 16) {
1860 stats->rx_bytes = rx_bytes;
1861 stats->rx_packets = rx_packets;
1862 stats->rx_errs = rx_errs;
1863 stats->rx_dropped = rx_dropped;
1864 stats->tx_bytes = tx_bytes;
1865 stats->tx_packets = tx_packets;
1866 stats->tx_errs = tx_errs;
1867 stats->tx_dropped = tx_dropped;
1875 g_debug("/proc/net/dev: Interface '%s' not found", name);
1880 * Build information about guest interfaces
1882 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
1884 GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
1885 struct ifaddrs *ifap, *ifa;
1887 if (getifaddrs(&ifap) < 0) {
1888 error_setg_errno(errp, errno, "getifaddrs failed");
1892 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1893 GuestNetworkInterfaceList *info;
1894 GuestIpAddressList **address_list = NULL, *address_item = NULL;
1895 GuestNetworkInterfaceStat *interface_stat = NULL;
1896 char addr4[INET_ADDRSTRLEN];
1897 char addr6[INET6_ADDRSTRLEN];
1900 unsigned char *mac_addr;
1903 g_debug("Processing %s interface", ifa->ifa_name);
1905 info = guest_find_interface(head, ifa->ifa_name);
1908 info = g_malloc0(sizeof(*info));
1909 info->value = g_malloc0(sizeof(*info->value));
1910 info->value->name = g_strdup(ifa->ifa_name);
1913 head = cur_item = info;
1915 cur_item->next = info;
1920 if (!info->value->has_hardware_address &&
1921 ifa->ifa_flags & SIOCGIFHWADDR) {
1922 /* we haven't obtained HW address yet */
1923 sock = socket(PF_INET, SOCK_STREAM, 0);
1925 error_setg_errno(errp, errno, "failed to create socket");
1929 memset(&ifr, 0, sizeof(ifr));
1930 pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
1931 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
1932 error_setg_errno(errp, errno,
1933 "failed to get MAC address of %s",
1940 mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
1942 info->value->hardware_address =
1943 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1944 (int) mac_addr[0], (int) mac_addr[1],
1945 (int) mac_addr[2], (int) mac_addr[3],
1946 (int) mac_addr[4], (int) mac_addr[5]);
1948 info->value->has_hardware_address = true;
1951 if (ifa->ifa_addr &&
1952 ifa->ifa_addr->sa_family == AF_INET) {
1953 /* interface with IPv4 address */
1954 p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1955 if (!inet_ntop(AF_INET, p, addr4, sizeof(addr4))) {
1956 error_setg_errno(errp, errno, "inet_ntop failed");
1960 address_item = g_malloc0(sizeof(*address_item));
1961 address_item->value = g_malloc0(sizeof(*address_item->value));
1962 address_item->value->ip_address = g_strdup(addr4);
1963 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
1965 if (ifa->ifa_netmask) {
1966 /* Count the number of set bits in netmask.
1967 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1968 p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
1969 address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
1971 } else if (ifa->ifa_addr &&
1972 ifa->ifa_addr->sa_family == AF_INET6) {
1973 /* interface with IPv6 address */
1974 p = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
1975 if (!inet_ntop(AF_INET6, p, addr6, sizeof(addr6))) {
1976 error_setg_errno(errp, errno, "inet_ntop failed");
1980 address_item = g_malloc0(sizeof(*address_item));
1981 address_item->value = g_malloc0(sizeof(*address_item->value));
1982 address_item->value->ip_address = g_strdup(addr6);
1983 address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
1985 if (ifa->ifa_netmask) {
1986 /* Count the number of set bits in netmask.
1987 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1988 p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
1989 address_item->value->prefix =
1990 ctpop32(((uint32_t *) p)[0]) +
1991 ctpop32(((uint32_t *) p)[1]) +
1992 ctpop32(((uint32_t *) p)[2]) +
1993 ctpop32(((uint32_t *) p)[3]);
1997 if (!address_item) {
2001 address_list = &info->value->ip_addresses;
2003 while (*address_list && (*address_list)->next) {
2004 address_list = &(*address_list)->next;
2007 if (!*address_list) {
2008 *address_list = address_item;
2010 (*address_list)->next = address_item;
2013 info->value->has_ip_addresses = true;
2015 if (!info->value->has_statistics) {
2016 interface_stat = g_malloc0(sizeof(*interface_stat));
2017 if (guest_get_network_stats(info->value->name,
2018 interface_stat) == -1) {
2019 info->value->has_statistics = false;
2020 g_free(interface_stat);
2022 info->value->statistics = interface_stat;
2023 info->value->has_statistics = true;
2033 qapi_free_GuestNetworkInterfaceList(head);
2037 #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
2039 static long sysconf_exact(int name, const char *name_str, Error **errp)
2044 ret = sysconf(name);
2047 error_setg(errp, "sysconf(%s): value indefinite", name_str);
2049 error_setg_errno(errp, errno, "sysconf(%s)", name_str);
2055 /* Transfer online/offline status between @vcpu and the guest system.
2057 * On input either @errp or *@errp must be NULL.
2059 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
2060 * - R: vcpu->logical_id
2062 * - W: vcpu->can_offline
2064 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
2065 * - R: vcpu->logical_id
2068 * Written members remain unmodified on error.
2070 static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu,
2071 char *dirpath, Error **errp)
2076 static const char fn[] = "online";
2078 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2080 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2084 fd = openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR);
2086 if (errno != ENOENT) {
2087 error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn);
2088 } else if (sys2vcpu) {
2089 vcpu->online = true;
2090 vcpu->can_offline = false;
2091 } else if (!vcpu->online) {
2092 error_setg(errp, "logical processor #%" PRId64 " can't be "
2093 "offlined", vcpu->logical_id);
2094 } /* otherwise pretend successful re-onlining */
2096 unsigned char status;
2098 res = pread(fd, &status, 1, 0);
2100 error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn);
2101 } else if (res == 0) {
2102 error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath,
2104 } else if (sys2vcpu) {
2105 vcpu->online = (status != '0');
2106 vcpu->can_offline = true;
2107 } else if (vcpu->online != (status != '0')) {
2108 status = '0' + vcpu->online;
2109 if (pwrite(fd, &status, 1, 0) == -1) {
2110 error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath,
2113 } /* otherwise pretend successful re-(on|off)-lining */
2123 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2126 GuestLogicalProcessorList *head, **link;
2128 Error *local_err = NULL;
2133 sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
2135 while (local_err == NULL && current < sc_max) {
2136 GuestLogicalProcessor *vcpu;
2137 GuestLogicalProcessorList *entry;
2138 int64_t id = current++;
2139 char *path = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
2142 if (g_file_test(path, G_FILE_TEST_EXISTS)) {
2143 vcpu = g_malloc0(sizeof *vcpu);
2144 vcpu->logical_id = id;
2145 vcpu->has_can_offline = true; /* lolspeak ftw */
2146 transfer_vcpu(vcpu, true, path, &local_err);
2147 entry = g_malloc0(sizeof *entry);
2148 entry->value = vcpu;
2150 link = &entry->next;
2155 if (local_err == NULL) {
2156 /* there's no guest with zero VCPUs */
2157 g_assert(head != NULL);
2161 qapi_free_GuestLogicalProcessorList(head);
2162 error_propagate(errp, local_err);
2166 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2169 Error *local_err = NULL;
2172 while (vcpus != NULL) {
2173 char *path = g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/",
2174 vcpus->value->logical_id);
2176 transfer_vcpu(vcpus->value, false, path, &local_err);
2178 if (local_err != NULL) {
2182 vcpus = vcpus->next;
2185 if (local_err != NULL) {
2186 if (processed == 0) {
2187 error_propagate(errp, local_err);
2189 error_free(local_err);
2196 void qmp_guest_set_user_password(const char *username,
2197 const char *password,
2201 Error *local_err = NULL;
2202 char *passwd_path = NULL;
2205 int datafd[2] = { -1, -1 };
2206 char *rawpasswddata = NULL;
2207 size_t rawpasswdlen;
2208 char *chpasswddata = NULL;
2211 rawpasswddata = (char *)qbase64_decode(password, -1, &rawpasswdlen, errp);
2212 if (!rawpasswddata) {
2215 rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
2216 rawpasswddata[rawpasswdlen] = '\0';
2218 if (strchr(rawpasswddata, '\n')) {
2219 error_setg(errp, "forbidden characters in raw password");
2223 if (strchr(username, '\n') ||
2224 strchr(username, ':')) {
2225 error_setg(errp, "forbidden characters in username");
2229 chpasswddata = g_strdup_printf("%s:%s\n", username, rawpasswddata);
2230 chpasswdlen = strlen(chpasswddata);
2232 passwd_path = g_find_program_in_path("chpasswd");
2235 error_setg(errp, "cannot find 'passwd' program in PATH");
2239 if (pipe(datafd) < 0) {
2240 error_setg(errp, "cannot create pipe FDs");
2250 reopen_fd_to_null(1);
2251 reopen_fd_to_null(2);
2254 execle(passwd_path, "chpasswd", "-e", NULL, environ);
2256 execle(passwd_path, "chpasswd", NULL, environ);
2258 _exit(EXIT_FAILURE);
2259 } else if (pid < 0) {
2260 error_setg_errno(errp, errno, "failed to create child process");
2266 if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) != chpasswdlen) {
2267 error_setg_errno(errp, errno, "cannot write new account password");
2273 ga_wait_child(pid, &status, &local_err);
2275 error_propagate(errp, local_err);
2279 if (!WIFEXITED(status)) {
2280 error_setg(errp, "child process has terminated abnormally");
2284 if (WEXITSTATUS(status)) {
2285 error_setg(errp, "child process has failed to set user password");
2290 g_free(chpasswddata);
2291 g_free(rawpasswddata);
2292 g_free(passwd_path);
2293 if (datafd[0] != -1) {
2296 if (datafd[1] != -1) {
2301 static void ga_read_sysfs_file(int dirfd, const char *pathname, char *buf,
2302 int size, Error **errp)
2308 fd = openat(dirfd, pathname, O_RDONLY);
2310 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2314 res = pread(fd, buf, size, 0);
2316 error_setg_errno(errp, errno, "pread sysfs file \"%s\"", pathname);
2317 } else if (res == 0) {
2318 error_setg(errp, "pread sysfs file \"%s\": unexpected EOF", pathname);
2323 static void ga_write_sysfs_file(int dirfd, const char *pathname,
2324 const char *buf, int size, Error **errp)
2329 fd = openat(dirfd, pathname, O_WRONLY);
2331 error_setg_errno(errp, errno, "open sysfs file \"%s\"", pathname);
2335 if (pwrite(fd, buf, size, 0) == -1) {
2336 error_setg_errno(errp, errno, "pwrite sysfs file \"%s\"", pathname);
2342 /* Transfer online/offline status between @mem_blk and the guest system.
2344 * On input either @errp or *@errp must be NULL.
2346 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2347 * - R: mem_blk->phys_index
2348 * - W: mem_blk->online
2349 * - W: mem_blk->can_offline
2351 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2352 * - R: mem_blk->phys_index
2353 * - R: mem_blk->online
2354 *- R: mem_blk->can_offline
2355 * Written members remain unmodified on error.
2357 static void transfer_memory_block(GuestMemoryBlock *mem_blk, bool sys2memblk,
2358 GuestMemoryBlockResponse *result,
2364 Error *local_err = NULL;
2370 error_setg(errp, "Internal error, 'result' should not be NULL");
2374 dp = opendir("/sys/devices/system/memory/");
2375 /* if there is no 'memory' directory in sysfs,
2376 * we think this VM does not support online/offline memory block,
2377 * any other solution?
2380 if (errno == ENOENT) {
2382 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2389 dirpath = g_strdup_printf("/sys/devices/system/memory/memory%" PRId64 "/",
2390 mem_blk->phys_index);
2391 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2394 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2396 if (errno == ENOENT) {
2397 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND;
2400 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2408 status = g_malloc0(10);
2409 ga_read_sysfs_file(dirfd, "state", status, 10, &local_err);
2411 /* treat with sysfs file that not exist in old kernel */
2412 if (errno == ENOENT) {
2413 error_free(local_err);
2415 mem_blk->online = true;
2416 mem_blk->can_offline = false;
2417 } else if (!mem_blk->online) {
2419 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED;
2423 error_propagate(errp, local_err);
2426 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2433 char removable = '0';
2435 mem_blk->online = (strncmp(status, "online", 6) == 0);
2437 ga_read_sysfs_file(dirfd, "removable", &removable, 1, &local_err);
2439 /* if no 'removable' file, it doesn't support offline mem blk */
2440 if (errno == ENOENT) {
2441 error_free(local_err);
2442 mem_blk->can_offline = false;
2444 error_propagate(errp, local_err);
2447 mem_blk->can_offline = (removable != '0');
2450 if (mem_blk->online != (strncmp(status, "online", 6) == 0)) {
2451 const char *new_state = mem_blk->online ? "online" : "offline";
2453 ga_write_sysfs_file(dirfd, "state", new_state, strlen(new_state),
2456 error_free(local_err);
2458 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED;
2462 result->response = GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS;
2463 result->has_error_code = false;
2464 } /* otherwise pretend successful re-(on|off)-lining */
2475 result->has_error_code = true;
2476 result->error_code = errno;
2480 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2482 GuestMemoryBlockList *head, **link;
2483 Error *local_err = NULL;
2490 dp = opendir("/sys/devices/system/memory/");
2492 /* it's ok if this happens to be a system that doesn't expose
2493 * memory blocks via sysfs, but otherwise we should report
2496 if (errno != ENOENT) {
2497 error_setg_errno(errp, errno, "Can't open directory"
2498 "\"/sys/devices/system/memory/\"");
2503 /* Note: the phys_index of memory block may be discontinuous,
2504 * this is because a memblk is the unit of the Sparse Memory design, which
2505 * allows discontinuous memory ranges (ex. NUMA), so here we should
2506 * traverse the memory block directory.
2508 while ((de = readdir(dp)) != NULL) {
2509 GuestMemoryBlock *mem_blk;
2510 GuestMemoryBlockList *entry;
2512 if ((strncmp(de->d_name, "memory", 6) != 0) ||
2513 !(de->d_type & DT_DIR)) {
2517 mem_blk = g_malloc0(sizeof *mem_blk);
2518 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */
2519 mem_blk->phys_index = strtoul(&de->d_name[6], NULL, 10);
2520 mem_blk->has_can_offline = true; /* lolspeak ftw */
2521 transfer_memory_block(mem_blk, true, NULL, &local_err);
2523 entry = g_malloc0(sizeof *entry);
2524 entry->value = mem_blk;
2527 link = &entry->next;
2531 if (local_err == NULL) {
2532 /* there's no guest with zero memory blocks */
2534 error_setg(errp, "guest reported zero memory blocks!");
2539 qapi_free_GuestMemoryBlockList(head);
2540 error_propagate(errp, local_err);
2544 GuestMemoryBlockResponseList *
2545 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2547 GuestMemoryBlockResponseList *head, **link;
2548 Error *local_err = NULL;
2553 while (mem_blks != NULL) {
2554 GuestMemoryBlockResponse *result;
2555 GuestMemoryBlockResponseList *entry;
2556 GuestMemoryBlock *current_mem_blk = mem_blks->value;
2558 result = g_malloc0(sizeof(*result));
2559 result->phys_index = current_mem_blk->phys_index;
2560 transfer_memory_block(current_mem_blk, false, result, &local_err);
2561 if (local_err) { /* should never happen */
2564 entry = g_malloc0(sizeof *entry);
2565 entry->value = result;
2568 link = &entry->next;
2569 mem_blks = mem_blks->next;
2574 qapi_free_GuestMemoryBlockResponseList(head);
2575 error_propagate(errp, local_err);
2579 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2581 Error *local_err = NULL;
2585 GuestMemoryBlockInfo *info;
2587 dirpath = g_strdup_printf("/sys/devices/system/memory/");
2588 dirfd = open(dirpath, O_RDONLY | O_DIRECTORY);
2590 error_setg_errno(errp, errno, "open(\"%s\")", dirpath);
2596 buf = g_malloc0(20);
2597 ga_read_sysfs_file(dirfd, "block_size_bytes", buf, 20, &local_err);
2601 error_propagate(errp, local_err);
2605 info = g_new0(GuestMemoryBlockInfo, 1);
2606 info->size = strtol(buf, NULL, 16); /* the unit is bytes */
2613 #else /* defined(__linux__) */
2615 void qmp_guest_suspend_disk(Error **errp)
2617 error_setg(errp, QERR_UNSUPPORTED);
2620 void qmp_guest_suspend_ram(Error **errp)
2622 error_setg(errp, QERR_UNSUPPORTED);
2625 void qmp_guest_suspend_hybrid(Error **errp)
2627 error_setg(errp, QERR_UNSUPPORTED);
2630 GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
2632 error_setg(errp, QERR_UNSUPPORTED);
2636 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2638 error_setg(errp, QERR_UNSUPPORTED);
2642 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2644 error_setg(errp, QERR_UNSUPPORTED);
2648 void qmp_guest_set_user_password(const char *username,
2649 const char *password,
2653 error_setg(errp, QERR_UNSUPPORTED);
2656 GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)
2658 error_setg(errp, QERR_UNSUPPORTED);
2662 GuestMemoryBlockResponseList *
2663 qmp_guest_set_memory_blocks(GuestMemoryBlockList *mem_blks, Error **errp)
2665 error_setg(errp, QERR_UNSUPPORTED);
2669 GuestMemoryBlockInfo *qmp_guest_get_memory_block_info(Error **errp)
2671 error_setg(errp, QERR_UNSUPPORTED);
2677 #if !defined(CONFIG_FSFREEZE)
2679 GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp)
2681 error_setg(errp, QERR_UNSUPPORTED);
2685 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp)
2687 error_setg(errp, QERR_UNSUPPORTED);
2692 int64_t qmp_guest_fsfreeze_freeze(Error **errp)
2694 error_setg(errp, QERR_UNSUPPORTED);
2699 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
2700 strList *mountpoints,
2703 error_setg(errp, QERR_UNSUPPORTED);
2708 int64_t qmp_guest_fsfreeze_thaw(Error **errp)
2710 error_setg(errp, QERR_UNSUPPORTED);
2714 #endif /* CONFIG_FSFREEZE */
2716 #if !defined(CONFIG_FSTRIM)
2717 GuestFilesystemTrimResponse *
2718 qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
2720 error_setg(errp, QERR_UNSUPPORTED);
2725 /* add unsupported commands to the blacklist */
2726 GList *ga_command_blacklist_init(GList *blacklist)
2728 #if !defined(__linux__)
2730 const char *list[] = {
2731 "guest-suspend-disk", "guest-suspend-ram",
2732 "guest-suspend-hybrid", "guest-network-get-interfaces",
2733 "guest-get-vcpus", "guest-set-vcpus",
2734 "guest-get-memory-blocks", "guest-set-memory-blocks",
2735 "guest-get-memory-block-size", NULL};
2736 char **p = (char **)list;
2739 blacklist = g_list_append(blacklist, g_strdup(*p++));
2744 #if !defined(CONFIG_FSFREEZE)
2746 const char *list[] = {
2747 "guest-get-fsinfo", "guest-fsfreeze-status",
2748 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
2749 "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL};
2750 char **p = (char **)list;
2753 blacklist = g_list_append(blacklist, g_strdup(*p++));
2758 #if !defined(CONFIG_FSTRIM)
2759 blacklist = g_list_append(blacklist, g_strdup("guest-fstrim"));
2765 /* register init/cleanup routines for stateful command groups */
2766 void ga_command_state_init(GAState *s, GACommandState *cs)
2768 #if defined(CONFIG_FSFREEZE)
2769 ga_command_state_add(cs, NULL, guest_fsfreeze_cleanup);
2775 #define QGA_MICRO_SECOND_TO_SECOND 1000000
2777 static double ga_get_login_time(struct utmpx *user_info)
2779 double seconds = (double)user_info->ut_tv.tv_sec;
2780 double useconds = (double)user_info->ut_tv.tv_usec;
2781 useconds /= QGA_MICRO_SECOND_TO_SECOND;
2782 return seconds + useconds;
2785 GuestUserList *qmp_guest_get_users(Error **err)
2787 GHashTable *cache = NULL;
2788 GuestUserList *head = NULL, *cur_item = NULL;
2789 struct utmpx *user_info = NULL;
2790 gpointer value = NULL;
2791 GuestUser *user = NULL;
2792 GuestUserList *item = NULL;
2793 double login_time = 0;
2795 cache = g_hash_table_new(g_str_hash, g_str_equal);
2799 user_info = getutxent();
2800 if (user_info == NULL) {
2802 } else if (user_info->ut_type != USER_PROCESS) {
2804 } else if (g_hash_table_contains(cache, user_info->ut_user)) {
2805 value = g_hash_table_lookup(cache, user_info->ut_user);
2806 user = (GuestUser *)value;
2807 login_time = ga_get_login_time(user_info);
2808 /* We're ensuring the earliest login time to be sent */
2809 if (login_time < user->login_time) {
2810 user->login_time = login_time;
2815 item = g_new0(GuestUserList, 1);
2816 item->value = g_new0(GuestUser, 1);
2817 item->value->user = g_strdup(user_info->ut_user);
2818 item->value->login_time = ga_get_login_time(user_info);
2820 g_hash_table_insert(cache, item->value->user, item->value);
2823 head = cur_item = item;
2825 cur_item->next = item;
2830 g_hash_table_destroy(cache);
2836 GuestUserList *qmp_guest_get_users(Error **errp)
2838 error_setg(errp, QERR_UNSUPPORTED);
2844 /* Replace escaped special characters with theire real values. The replacement
2845 * is done in place -- returned value is in the original string.
2847 static void ga_osrelease_replace_special(gchar *value)
2849 gchar *p, *p2, quote;
2851 /* Trim the string at first space or semicolon if it is not enclosed in
2852 * single or double quotes. */
2853 if ((value[0] != '"') || (value[0] == '\'')) {
2854 p = strchr(value, ' ');
2858 p = strchr(value, ';');
2879 /* Keep literal backslash followed by whatever is there */
2883 } else if (*p == quote) {
2891 static GKeyFile *ga_parse_osrelease(const char *fname)
2893 gchar *content = NULL;
2894 gchar *content2 = NULL;
2896 GKeyFile *keys = g_key_file_new();
2897 const char *group = "[os-release]\n";
2899 if (!g_file_get_contents(fname, &content, NULL, &err)) {
2900 slog("failed to read '%s', error: %s", fname, err->message);
2904 if (!g_utf8_validate(content, -1, NULL)) {
2905 slog("file is not utf-8 encoded: %s", fname);
2908 content2 = g_strdup_printf("%s%s", group, content);
2910 if (!g_key_file_load_from_data(keys, content2, -1, G_KEY_FILE_NONE,
2912 slog("failed to parse file '%s', error: %s", fname, err->message);
2924 g_key_file_free(keys);
2928 GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
2930 GuestOSInfo *info = NULL;
2931 struct utsname kinfo;
2932 GKeyFile *osrelease = NULL;
2933 const char *qga_os_release = g_getenv("QGA_OS_RELEASE");
2935 info = g_new0(GuestOSInfo, 1);
2937 if (uname(&kinfo) != 0) {
2938 error_setg_errno(errp, errno, "uname failed");
2940 info->has_kernel_version = true;
2941 info->kernel_version = g_strdup(kinfo.version);
2942 info->has_kernel_release = true;
2943 info->kernel_release = g_strdup(kinfo.release);
2944 info->has_machine = true;
2945 info->machine = g_strdup(kinfo.machine);
2948 if (qga_os_release != NULL) {
2949 osrelease = ga_parse_osrelease(qga_os_release);
2951 osrelease = ga_parse_osrelease("/etc/os-release");
2952 if (osrelease == NULL) {
2953 osrelease = ga_parse_osrelease("/usr/lib/os-release");
2957 if (osrelease != NULL) {
2960 #define GET_FIELD(field, osfield) do { \
2961 value = g_key_file_get_value(osrelease, "os-release", osfield, NULL); \
2962 if (value != NULL) { \
2963 ga_osrelease_replace_special(value); \
2964 info->has_ ## field = true; \
2965 info->field = value; \
2968 GET_FIELD(id, "ID");
2969 GET_FIELD(name, "NAME");
2970 GET_FIELD(pretty_name, "PRETTY_NAME");
2971 GET_FIELD(version, "VERSION");
2972 GET_FIELD(version_id, "VERSION_ID");
2973 GET_FIELD(variant, "VARIANT");
2974 GET_FIELD(variant_id, "VARIANT_ID");
2977 g_key_file_free(osrelease);