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>
26 #include "qga/guest-agent-core.h"
27 #include "qga-qmp-commands.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qemu/queue.h"
30 #include "qemu/host-utils.h"
32 #ifndef CONFIG_HAS_ENVIRON
34 #include <crt_externs.h>
35 #define environ (*_NSGetEnviron())
37 extern char **environ
;
41 #if defined(__linux__)
45 #include <arpa/inet.h>
46 #include <sys/socket.h>
50 #define CONFIG_FSFREEZE
57 static void ga_wait_child(pid_t pid
, int *status
, Error
**errp
)
64 rpid
= waitpid(pid
, status
, 0);
65 } while (rpid
== -1 && errno
== EINTR
);
68 error_setg_errno(errp
, errno
, "failed to wait for child (pid: %d)",
73 g_assert(rpid
== pid
);
76 void qmp_guest_shutdown(bool has_mode
, const char *mode
, Error
**errp
)
78 const char *shutdown_flag
;
79 Error
*local_err
= NULL
;
83 slog("guest-shutdown called, mode: %s", mode
);
84 if (!has_mode
|| strcmp(mode
, "powerdown") == 0) {
86 } else if (strcmp(mode
, "halt") == 0) {
88 } else if (strcmp(mode
, "reboot") == 0) {
92 "mode is invalid (valid values are: halt|powerdown|reboot");
98 /* child, start the shutdown */
100 reopen_fd_to_null(0);
101 reopen_fd_to_null(1);
102 reopen_fd_to_null(2);
104 execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag
, "+0",
105 "hypervisor initiated shutdown", (char*)NULL
, environ
);
107 } else if (pid
< 0) {
108 error_setg_errno(errp
, errno
, "failed to create child process");
112 ga_wait_child(pid
, &status
, &local_err
);
114 error_propagate(errp
, local_err
);
118 if (!WIFEXITED(status
)) {
119 error_setg(errp
, "child process has terminated abnormally");
123 if (WEXITSTATUS(status
)) {
124 error_setg(errp
, "child process has failed to shutdown");
131 int64_t qmp_guest_get_time(Error
**errp
)
137 ret
= qemu_gettimeofday(&tq
);
139 error_setg_errno(errp
, errno
, "Failed to get time");
143 time_ns
= tq
.tv_sec
* 1000000000LL + tq
.tv_usec
* 1000;
147 void qmp_guest_set_time(bool has_time
, int64_t time_ns
, Error
**errp
)
152 Error
*local_err
= NULL
;
155 /* If user has passed a time, validate and set it. */
157 /* year-2038 will overflow in case time_t is 32bit */
158 if (time_ns
/ 1000000000 != (time_t)(time_ns
/ 1000000000)) {
159 error_setg(errp
, "Time %" PRId64
" is too large", time_ns
);
163 tv
.tv_sec
= time_ns
/ 1000000000;
164 tv
.tv_usec
= (time_ns
% 1000000000) / 1000;
166 ret
= settimeofday(&tv
, NULL
);
168 error_setg_errno(errp
, errno
, "Failed to set time to guest");
173 /* Now, if user has passed a time to set and the system time is set, we
174 * just need to synchronize the hardware clock. However, if no time was
175 * passed, user is requesting the opposite: set the system time from the
176 * hardware clock (RTC). */
180 reopen_fd_to_null(0);
181 reopen_fd_to_null(1);
182 reopen_fd_to_null(2);
184 /* Use '/sbin/hwclock -w' to set RTC from the system time,
185 * or '/sbin/hwclock -s' to set the system time from RTC. */
186 execle("/sbin/hwclock", "hwclock", has_time
? "-w" : "-s",
189 } else if (pid
< 0) {
190 error_setg_errno(errp
, errno
, "failed to create child process");
194 ga_wait_child(pid
, &status
, &local_err
);
196 error_propagate(errp
, local_err
);
200 if (!WIFEXITED(status
)) {
201 error_setg(errp
, "child process has terminated abnormally");
205 if (WEXITSTATUS(status
)) {
206 error_setg(errp
, "hwclock failed to set hardware clock to system time");
211 typedef struct GuestFileHandle
{
214 QTAILQ_ENTRY(GuestFileHandle
) next
;
218 QTAILQ_HEAD(, GuestFileHandle
) filehandles
;
221 static int64_t guest_file_handle_add(FILE *fh
, Error
**errp
)
223 GuestFileHandle
*gfh
;
226 handle
= ga_get_fd_handle(ga_state
, errp
);
231 gfh
= g_malloc0(sizeof(GuestFileHandle
));
234 QTAILQ_INSERT_TAIL(&guest_file_state
.filehandles
, gfh
, next
);
239 static GuestFileHandle
*guest_file_handle_find(int64_t id
, Error
**errp
)
241 GuestFileHandle
*gfh
;
243 QTAILQ_FOREACH(gfh
, &guest_file_state
.filehandles
, next
)
250 error_setg(errp
, "handle '%" PRId64
"' has not been found", id
);
254 typedef const char * const ccpc
;
260 /* http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html */
261 static const struct {
264 } guest_file_open_modes
[] = {
265 { (ccpc
[]){ "r", NULL
}, O_RDONLY
},
266 { (ccpc
[]){ "rb", NULL
}, O_RDONLY
| O_BINARY
},
267 { (ccpc
[]){ "w", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
},
268 { (ccpc
[]){ "wb", NULL
}, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
},
269 { (ccpc
[]){ "a", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
},
270 { (ccpc
[]){ "ab", NULL
}, O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
},
271 { (ccpc
[]){ "r+", NULL
}, O_RDWR
},
272 { (ccpc
[]){ "rb+", "r+b", NULL
}, O_RDWR
| O_BINARY
},
273 { (ccpc
[]){ "w+", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
},
274 { (ccpc
[]){ "wb+", "w+b", NULL
}, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
},
275 { (ccpc
[]){ "a+", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
},
276 { (ccpc
[]){ "ab+", "a+b", NULL
}, O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
}
280 find_open_flag(const char *mode_str
, Error
**errp
)
284 for (mode
= 0; mode
< ARRAY_SIZE(guest_file_open_modes
); ++mode
) {
287 form
= guest_file_open_modes
[mode
].forms
;
288 while (*form
!= NULL
&& strcmp(*form
, mode_str
) != 0) {
296 if (mode
== ARRAY_SIZE(guest_file_open_modes
)) {
297 error_setg(errp
, "invalid file open mode '%s'", mode_str
);
300 return guest_file_open_modes
[mode
].oflag_base
| O_NOCTTY
| O_NONBLOCK
;
303 #define DEFAULT_NEW_FILE_MODE (S_IRUSR | S_IWUSR | \
304 S_IRGRP | S_IWGRP | \
308 safe_open_or_create(const char *path
, const char *mode
, Error
**errp
)
310 Error
*local_err
= NULL
;
313 oflag
= find_open_flag(mode
, &local_err
);
314 if (local_err
== NULL
) {
317 /* If the caller wants / allows creation of a new file, we implement it
318 * with a two step process: open() + (open() / fchmod()).
320 * First we insist on creating the file exclusively as a new file. If
321 * that succeeds, we're free to set any file-mode bits on it. (The
322 * motivation is that we want to set those file-mode bits independently
323 * of the current umask.)
325 * If the exclusive creation fails because the file already exists
326 * (EEXIST is not possible for any other reason), we just attempt to
327 * open the file, but in this case we won't be allowed to change the
328 * file-mode bits on the preexistent file.
330 * The pathname should never disappear between the two open()s in
331 * practice. If it happens, then someone very likely tried to race us.
332 * In this case just go ahead and report the ENOENT from the second
333 * open() to the caller.
335 * If the caller wants to open a preexistent file, then the first
336 * open() is decisive and its third argument is ignored, and the second
337 * open() and the fchmod() are never called.
339 fd
= open(path
, oflag
| ((oflag
& O_CREAT
) ? O_EXCL
: 0), 0);
340 if (fd
== -1 && errno
== EEXIST
) {
341 oflag
&= ~(unsigned)O_CREAT
;
342 fd
= open(path
, oflag
);
346 error_setg_errno(&local_err
, errno
, "failed to open file '%s' "
347 "(mode: '%s')", path
, mode
);
349 qemu_set_cloexec(fd
);
351 if ((oflag
& O_CREAT
) && fchmod(fd
, DEFAULT_NEW_FILE_MODE
) == -1) {
352 error_setg_errno(&local_err
, errno
, "failed to set permission "
353 "0%03o on new file '%s' (mode: '%s')",
354 (unsigned)DEFAULT_NEW_FILE_MODE
, path
, mode
);
358 f
= fdopen(fd
, mode
);
360 error_setg_errno(&local_err
, errno
, "failed to associate "
361 "stdio stream with file descriptor %d, "
362 "file '%s' (mode: '%s')", fd
, path
, mode
);
369 if (oflag
& O_CREAT
) {
375 error_propagate(errp
, local_err
);
379 static int guest_file_toggle_flags(int fd
, int flags
, bool set
, Error
**err
)
383 old_flags
= fcntl(fd
, F_GETFL
);
384 if (old_flags
== -1) {
385 error_set_errno(err
, errno
, QERR_QGA_COMMAND_FAILED
,
386 "failed to fetch filehandle flags");
390 ret
= fcntl(fd
, F_SETFL
, set
? (old_flags
| flags
) : (old_flags
& ~flags
));
392 error_set_errno(err
, errno
, QERR_QGA_COMMAND_FAILED
,
393 "failed to set filehandle flags");
400 int64_t qmp_guest_file_open(const char *path
, bool has_mode
, const char *mode
,
404 Error
*local_err
= NULL
;
410 slog("guest-file-open called, filepath: %s, mode: %s", path
, mode
);
411 fh
= safe_open_or_create(path
, mode
, &local_err
);
412 if (local_err
!= NULL
) {
413 error_propagate(errp
, local_err
);
417 /* set fd non-blocking to avoid common use cases (like reading from a
418 * named pipe) from hanging the agent
420 if (guest_file_toggle_flags(fileno(fh
), O_NONBLOCK
, true, errp
) < 0) {
425 handle
= guest_file_handle_add(fh
, errp
);
431 slog("guest-file-open, handle: %" PRId64
, handle
);
435 void qmp_guest_file_close(int64_t handle
, Error
**errp
)
437 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
440 slog("guest-file-close called, handle: %" PRId64
, handle
);
445 ret
= fclose(gfh
->fh
);
447 error_setg_errno(errp
, errno
, "failed to close handle");
451 QTAILQ_REMOVE(&guest_file_state
.filehandles
, gfh
, next
);
455 struct GuestFileRead
*qmp_guest_file_read(int64_t handle
, bool has_count
,
456 int64_t count
, Error
**errp
)
458 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
459 GuestFileRead
*read_data
= NULL
;
469 count
= QGA_READ_COUNT_DEFAULT
;
470 } else if (count
< 0) {
471 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
477 buf
= g_malloc0(count
+1);
478 read_count
= fread(buf
, 1, count
, fh
);
480 error_setg_errno(errp
, errno
, "failed to read file");
481 slog("guest-file-read failed, handle: %" PRId64
, handle
);
484 read_data
= g_malloc0(sizeof(GuestFileRead
));
485 read_data
->count
= read_count
;
486 read_data
->eof
= feof(fh
);
488 read_data
->buf_b64
= g_base64_encode(buf
, read_count
);
497 GuestFileWrite
*qmp_guest_file_write(int64_t handle
, const char *buf_b64
,
498 bool has_count
, int64_t count
,
501 GuestFileWrite
*write_data
= NULL
;
505 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
513 buf
= g_base64_decode(buf_b64
, &buf_len
);
517 } else if (count
< 0 || count
> buf_len
) {
518 error_setg(errp
, "value '%" PRId64
"' is invalid for argument count",
524 write_count
= fwrite(buf
, 1, count
, fh
);
526 error_setg_errno(errp
, errno
, "failed to write to file");
527 slog("guest-file-write failed, handle: %" PRId64
, handle
);
529 write_data
= g_malloc0(sizeof(GuestFileWrite
));
530 write_data
->count
= write_count
;
531 write_data
->eof
= feof(fh
);
539 struct GuestFileSeek
*qmp_guest_file_seek(int64_t handle
, int64_t offset
,
540 int64_t whence
, Error
**errp
)
542 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
543 GuestFileSeek
*seek_data
= NULL
;
552 ret
= fseek(fh
, offset
, whence
);
554 error_setg_errno(errp
, errno
, "failed to seek file");
556 seek_data
= g_new0(GuestFileSeek
, 1);
557 seek_data
->position
= ftell(fh
);
558 seek_data
->eof
= feof(fh
);
565 void qmp_guest_file_flush(int64_t handle
, Error
**errp
)
567 GuestFileHandle
*gfh
= guest_file_handle_find(handle
, errp
);
578 error_setg_errno(errp
, errno
, "failed to flush file");
582 static void guest_file_init(void)
584 QTAILQ_INIT(&guest_file_state
.filehandles
);
587 /* linux-specific implementations. avoid this if at all possible. */
588 #if defined(__linux__)
590 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
591 typedef struct FsMount
{
594 unsigned int devmajor
, devminor
;
595 QTAILQ_ENTRY(FsMount
) next
;
598 typedef QTAILQ_HEAD(FsMountList
, FsMount
) FsMountList
;
600 static void free_fs_mount_list(FsMountList
*mounts
)
602 FsMount
*mount
, *temp
;
608 QTAILQ_FOREACH_SAFE(mount
, mounts
, next
, temp
) {
609 QTAILQ_REMOVE(mounts
, mount
, next
);
610 g_free(mount
->dirname
);
611 g_free(mount
->devtype
);
616 static int dev_major_minor(const char *devpath
,
617 unsigned int *devmajor
, unsigned int *devminor
)
624 if (stat(devpath
, &st
) < 0) {
625 slog("failed to stat device file '%s': %s", devpath
, strerror(errno
));
628 if (S_ISDIR(st
.st_mode
)) {
629 /* It is bind mount */
632 if (S_ISBLK(st
.st_mode
)) {
633 *devmajor
= major(st
.st_rdev
);
634 *devminor
= minor(st
.st_rdev
);
641 * Walk the mount table and build a list of local file systems
643 static void build_fs_mount_list_from_mtab(FsMountList
*mounts
, Error
**errp
)
647 char const *mtab
= "/proc/self/mounts";
649 unsigned int devmajor
, devminor
;
651 fp
= setmntent(mtab
, "r");
653 error_setg(errp
, "failed to open mtab file: '%s'", mtab
);
657 while ((ment
= getmntent(fp
))) {
659 * An entry which device name doesn't start with a '/' is
660 * either a dummy file system or a network file system.
661 * Add special handling for smbfs and cifs as is done by
664 if ((ment
->mnt_fsname
[0] != '/') ||
665 (strcmp(ment
->mnt_type
, "smbfs") == 0) ||
666 (strcmp(ment
->mnt_type
, "cifs") == 0)) {
669 if (dev_major_minor(ment
->mnt_fsname
, &devmajor
, &devminor
) == -2) {
670 /* Skip bind mounts */
674 mount
= g_malloc0(sizeof(FsMount
));
675 mount
->dirname
= g_strdup(ment
->mnt_dir
);
676 mount
->devtype
= g_strdup(ment
->mnt_type
);
677 mount
->devmajor
= devmajor
;
678 mount
->devminor
= devminor
;
680 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
686 static void decode_mntname(char *name
, int len
)
689 for (i
= 0; i
<= len
; i
++) {
690 if (name
[i
] != '\\') {
692 } else if (name
[i
+ 1] == '\\') {
695 } else if (name
[i
+ 1] >= '0' && name
[i
+ 1] <= '3' &&
696 name
[i
+ 2] >= '0' && name
[i
+ 2] <= '7' &&
697 name
[i
+ 3] >= '0' && name
[i
+ 3] <= '7') {
698 name
[j
++] = (name
[i
+ 1] - '0') * 64 +
699 (name
[i
+ 2] - '0') * 8 +
708 static void build_fs_mount_list(FsMountList
*mounts
, Error
**errp
)
711 char const *mountinfo
= "/proc/self/mountinfo";
713 char *line
= NULL
, *dash
;
716 unsigned int devmajor
, devminor
;
717 int ret
, dir_s
, dir_e
, type_s
, type_e
, dev_s
, dev_e
;
719 fp
= fopen(mountinfo
, "r");
721 build_fs_mount_list_from_mtab(mounts
, errp
);
725 while (getline(&line
, &n
, fp
) != -1) {
726 ret
= sscanf(line
, "%*u %*u %u:%u %*s %n%*s%n%c",
727 &devmajor
, &devminor
, &dir_s
, &dir_e
, &check
);
731 dash
= strstr(line
+ dir_e
, " - ");
735 ret
= sscanf(dash
, " - %n%*s%n %n%*s%n%c",
736 &type_s
, &type_e
, &dev_s
, &dev_e
, &check
);
743 decode_mntname(line
+ dir_s
, dir_e
- dir_s
);
744 decode_mntname(dash
+ dev_s
, dev_e
- dev_s
);
746 /* btrfs reports major number = 0 */
747 if (strcmp("btrfs", dash
+ type_s
) != 0 ||
748 dev_major_minor(dash
+ dev_s
, &devmajor
, &devminor
) < 0) {
753 mount
= g_malloc0(sizeof(FsMount
));
754 mount
->dirname
= g_strdup(line
+ dir_s
);
755 mount
->devtype
= g_strdup(dash
+ type_s
);
756 mount
->devmajor
= devmajor
;
757 mount
->devminor
= devminor
;
759 QTAILQ_INSERT_TAIL(mounts
, mount
, next
);
767 #if defined(CONFIG_FSFREEZE)
769 static char *get_pci_driver(char const *syspath
, int pathlen
, Error
**errp
)
777 path
= g_strndup(syspath
, pathlen
);
778 dpath
= g_strdup_printf("%s/driver", path
);
779 len
= readlink(dpath
, buf
, sizeof(buf
) - 1);
782 driver
= g_strdup(basename(buf
));
789 static int compare_uint(const void *_a
, const void *_b
)
791 unsigned int a
= *(unsigned int *)_a
;
792 unsigned int b
= *(unsigned int *)_b
;
794 return a
< b
? -1 : a
> b
? 1 : 0;
797 /* Walk the specified sysfs and build a sorted list of host or ata numbers */
798 static int build_hosts(char const *syspath
, char const *host
, bool ata
,
799 unsigned int *hosts
, int hosts_max
, Error
**errp
)
803 struct dirent
*entry
;
806 path
= g_strndup(syspath
, host
- syspath
);
809 error_setg_errno(errp
, errno
, "opendir(\"%s\")", path
);
814 while (i
< hosts_max
) {
815 entry
= readdir(dir
);
819 if (ata
&& sscanf(entry
->d_name
, "ata%d", hosts
+ i
) == 1) {
821 } else if (!ata
&& sscanf(entry
->d_name
, "host%d", hosts
+ i
) == 1) {
826 qsort(hosts
, i
, sizeof(hosts
[0]), compare_uint
);
833 /* Store disk device info specified by @sysfs into @fs */
834 static void build_guest_fsinfo_for_real_device(char const *syspath
,
835 GuestFilesystemInfo
*fs
,
838 unsigned int pci
[4], host
, hosts
[8], tgt
[3];
839 int i
, nhosts
= 0, pcilen
;
840 GuestDiskAddress
*disk
;
841 GuestPCIAddress
*pciaddr
;
842 GuestDiskAddressList
*list
= NULL
;
843 bool has_ata
= false, has_host
= false, has_tgt
= false;
844 char *p
, *q
, *driver
= NULL
;
846 p
= strstr(syspath
, "/devices/pci");
847 if (!p
|| sscanf(p
+ 12, "%*x:%*x/%x:%x:%x.%x%n",
848 pci
, pci
+ 1, pci
+ 2, pci
+ 3, &pcilen
) < 4) {
849 g_debug("only pci device is supported: sysfs path \"%s\"", syspath
);
853 driver
= get_pci_driver(syspath
, (p
+ 12 + pcilen
) - syspath
, errp
);
858 p
= strstr(syspath
, "/target");
859 if (p
&& sscanf(p
+ 7, "%*u:%*u:%*u/%*u:%u:%u:%u",
860 tgt
, tgt
+ 1, tgt
+ 2) == 3) {
864 p
= strstr(syspath
, "/ata");
869 p
= strstr(syspath
, "/host");
872 if (p
&& sscanf(q
, "%u", &host
) == 1) {
874 nhosts
= build_hosts(syspath
, p
, has_ata
, hosts
,
875 sizeof(hosts
) / sizeof(hosts
[0]), errp
);
881 pciaddr
= g_malloc0(sizeof(*pciaddr
));
882 pciaddr
->domain
= pci
[0];
883 pciaddr
->bus
= pci
[1];
884 pciaddr
->slot
= pci
[2];
885 pciaddr
->function
= pci
[3];
887 disk
= g_malloc0(sizeof(*disk
));
888 disk
->pci_controller
= pciaddr
;
890 list
= g_malloc0(sizeof(*list
));
893 if (strcmp(driver
, "ata_piix") == 0) {
894 /* a host per ide bus, target*:0:<unit>:0 */
895 if (!has_host
|| !has_tgt
) {
896 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
899 for (i
= 0; i
< nhosts
; i
++) {
900 if (host
== hosts
[i
]) {
901 disk
->bus_type
= GUEST_DISK_BUS_TYPE_IDE
;
908 g_debug("no host for '%s' (driver '%s')", syspath
, driver
);
911 } else if (strcmp(driver
, "sym53c8xx") == 0) {
912 /* scsi(LSI Logic): target*:0:<unit>:0 */
914 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
917 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
919 } else if (strcmp(driver
, "virtio-pci") == 0) {
921 /* virtio-scsi: target*:0:0:<unit> */
922 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SCSI
;
925 /* virtio-blk: 1 disk per 1 device */
926 disk
->bus_type
= GUEST_DISK_BUS_TYPE_VIRTIO
;
928 } else if (strcmp(driver
, "ahci") == 0) {
929 /* ahci: 1 host per 1 unit */
930 if (!has_host
|| !has_tgt
) {
931 g_debug("invalid sysfs path '%s' (driver '%s')", syspath
, driver
);
934 for (i
= 0; i
< nhosts
; i
++) {
935 if (host
== hosts
[i
]) {
937 disk
->bus_type
= GUEST_DISK_BUS_TYPE_SATA
;
942 g_debug("no host for '%s' (driver '%s')", syspath
, driver
);
946 g_debug("unknown driver '%s' (sysfs path '%s')", driver
, syspath
);
950 list
->next
= fs
->disk
;
957 qapi_free_GuestDiskAddressList(list
);
962 static void build_guest_fsinfo_for_device(char const *devpath
,
963 GuestFilesystemInfo
*fs
,
966 /* Store a list of slave devices of virtual volume specified by @syspath into
968 static void build_guest_fsinfo_for_virtual_device(char const *syspath
,
969 GuestFilesystemInfo
*fs
,
974 struct dirent
*entry
;
976 dirpath
= g_strdup_printf("%s/slaves", syspath
);
977 dir
= opendir(dirpath
);
979 error_setg_errno(errp
, errno
, "opendir(\"%s\")", dirpath
);
986 entry
= readdir(dir
);
989 error_setg_errno(errp
, errno
, "readdir(\"%s\")", dirpath
);
994 if (entry
->d_type
== DT_LNK
) {
997 g_debug(" slave device '%s'", entry
->d_name
);
998 path
= g_strdup_printf("%s/slaves/%s", syspath
, entry
->d_name
);
999 build_guest_fsinfo_for_device(path
, fs
, errp
);
1012 /* Dispatch to functions for virtual/real device */
1013 static void build_guest_fsinfo_for_device(char const *devpath
,
1014 GuestFilesystemInfo
*fs
,
1017 char *syspath
= realpath(devpath
, NULL
);
1020 error_setg_errno(errp
, errno
, "realpath(\"%s\")", devpath
);
1025 fs
->name
= g_strdup(basename(syspath
));
1028 g_debug(" parse sysfs path '%s'", syspath
);
1030 if (strstr(syspath
, "/devices/virtual/block/")) {
1031 build_guest_fsinfo_for_virtual_device(syspath
, fs
, errp
);
1033 build_guest_fsinfo_for_real_device(syspath
, fs
, errp
);
1039 /* Return a list of the disk device(s)' info which @mount lies on */
1040 static GuestFilesystemInfo
*build_guest_fsinfo(struct FsMount
*mount
,
1043 GuestFilesystemInfo
*fs
= g_malloc0(sizeof(*fs
));
1044 char *devpath
= g_strdup_printf("/sys/dev/block/%u:%u",
1045 mount
->devmajor
, mount
->devminor
);
1047 fs
->mountpoint
= g_strdup(mount
->dirname
);
1048 fs
->type
= g_strdup(mount
->devtype
);
1049 build_guest_fsinfo_for_device(devpath
, fs
, errp
);
1055 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
1058 struct FsMount
*mount
;
1059 GuestFilesystemInfoList
*new, *ret
= NULL
;
1060 Error
*local_err
= NULL
;
1062 QTAILQ_INIT(&mounts
);
1063 build_fs_mount_list(&mounts
, &local_err
);
1065 error_propagate(errp
, local_err
);
1069 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1070 g_debug("Building guest fsinfo for '%s'", mount
->dirname
);
1072 new = g_malloc0(sizeof(*ret
));
1073 new->value
= build_guest_fsinfo(mount
, &local_err
);
1077 error_propagate(errp
, local_err
);
1078 qapi_free_GuestFilesystemInfoList(ret
);
1084 free_fs_mount_list(&mounts
);
1090 FSFREEZE_HOOK_THAW
= 0,
1091 FSFREEZE_HOOK_FREEZE
,
1094 static const char *fsfreeze_hook_arg_string
[] = {
1099 static void execute_fsfreeze_hook(FsfreezeHookArg arg
, Error
**errp
)
1104 const char *arg_str
= fsfreeze_hook_arg_string
[arg
];
1105 Error
*local_err
= NULL
;
1107 hook
= ga_fsfreeze_hook(ga_state
);
1111 if (access(hook
, X_OK
) != 0) {
1112 error_setg_errno(errp
, errno
, "can't access fsfreeze hook '%s'", hook
);
1116 slog("executing fsfreeze hook with arg '%s'", arg_str
);
1120 reopen_fd_to_null(0);
1121 reopen_fd_to_null(1);
1122 reopen_fd_to_null(2);
1124 execle(hook
, hook
, arg_str
, NULL
, environ
);
1125 _exit(EXIT_FAILURE
);
1126 } else if (pid
< 0) {
1127 error_setg_errno(errp
, errno
, "failed to create child process");
1131 ga_wait_child(pid
, &status
, &local_err
);
1133 error_propagate(errp
, local_err
);
1137 if (!WIFEXITED(status
)) {
1138 error_setg(errp
, "fsfreeze hook has terminated abnormally");
1142 status
= WEXITSTATUS(status
);
1144 error_setg(errp
, "fsfreeze hook has failed with status %d", status
);
1150 * Return status of freeze/thaw
1152 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
1154 if (ga_is_frozen(ga_state
)) {
1155 return GUEST_FSFREEZE_STATUS_FROZEN
;
1158 return GUEST_FSFREEZE_STATUS_THAWED
;
1161 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
1163 return qmp_guest_fsfreeze_freeze_list(false, NULL
, errp
);
1167 * Walk list of mounted file systems in the guest, and freeze the ones which
1168 * are real local file systems.
1170 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
1171 strList
*mountpoints
,
1177 struct FsMount
*mount
;
1178 Error
*local_err
= NULL
;
1181 slog("guest-fsfreeze called");
1183 execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE
, &local_err
);
1185 error_propagate(errp
, local_err
);
1189 QTAILQ_INIT(&mounts
);
1190 build_fs_mount_list(&mounts
, &local_err
);
1192 error_propagate(errp
, local_err
);
1196 /* cannot risk guest agent blocking itself on a write in this state */
1197 ga_set_frozen(ga_state
);
1199 QTAILQ_FOREACH_REVERSE(mount
, &mounts
, FsMountList
, next
) {
1200 /* To issue fsfreeze in the reverse order of mounts, check if the
1201 * mount is listed in the list here */
1202 if (has_mountpoints
) {
1203 for (list
= mountpoints
; list
; list
= list
->next
) {
1204 if (strcmp(list
->value
, mount
->dirname
) == 0) {
1213 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
1215 error_setg_errno(errp
, errno
, "failed to open %s", mount
->dirname
);
1219 /* we try to cull filesytems we know won't work in advance, but other
1220 * filesytems may not implement fsfreeze for less obvious reasons.
1221 * these will report EOPNOTSUPP. we simply ignore these when tallying
1222 * the number of frozen filesystems.
1224 * any other error means a failure to freeze a filesystem we
1225 * expect to be freezable, so return an error in those cases
1226 * and return system to thawed state.
1228 ret
= ioctl(fd
, FIFREEZE
);
1230 if (errno
!= EOPNOTSUPP
) {
1231 error_setg_errno(errp
, errno
, "failed to freeze %s",
1242 free_fs_mount_list(&mounts
);
1246 free_fs_mount_list(&mounts
);
1247 qmp_guest_fsfreeze_thaw(NULL
);
1252 * Walk list of frozen file systems in the guest, and thaw them.
1254 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
1259 int fd
, i
= 0, logged
;
1260 Error
*local_err
= NULL
;
1262 QTAILQ_INIT(&mounts
);
1263 build_fs_mount_list(&mounts
, &local_err
);
1265 error_propagate(errp
, local_err
);
1269 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1271 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
1275 /* we have no way of knowing whether a filesystem was actually unfrozen
1276 * as a result of a successful call to FITHAW, only that if an error
1277 * was returned the filesystem was *not* unfrozen by that particular
1280 * since multiple preceding FIFREEZEs require multiple calls to FITHAW
1281 * to unfreeze, continuing issuing FITHAW until an error is returned,
1282 * in which case either the filesystem is in an unfreezable state, or,
1283 * more likely, it was thawed previously (and remains so afterward).
1285 * also, since the most recent successful call is the one that did
1286 * the actual unfreeze, we can use this to provide an accurate count
1287 * of the number of filesystems unfrozen by guest-fsfreeze-thaw, which
1288 * may * be useful for determining whether a filesystem was unfrozen
1289 * during the freeze/thaw phase by a process other than qemu-ga.
1292 ret
= ioctl(fd
, FITHAW
);
1293 if (ret
== 0 && !logged
) {
1301 ga_unset_frozen(ga_state
);
1302 free_fs_mount_list(&mounts
);
1304 execute_fsfreeze_hook(FSFREEZE_HOOK_THAW
, errp
);
1309 static void guest_fsfreeze_cleanup(void)
1313 if (ga_is_frozen(ga_state
) == GUEST_FSFREEZE_STATUS_FROZEN
) {
1314 qmp_guest_fsfreeze_thaw(&err
);
1316 slog("failed to clean up frozen filesystems: %s",
1317 error_get_pretty(err
));
1322 #endif /* CONFIG_FSFREEZE */
1324 #if defined(CONFIG_FSTRIM)
1326 * Walk list of mounted file systems in the guest, and trim them.
1328 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
1332 struct FsMount
*mount
;
1334 Error
*local_err
= NULL
;
1335 struct fstrim_range r
= {
1338 .minlen
= has_minimum
? minimum
: 0,
1341 slog("guest-fstrim called");
1343 QTAILQ_INIT(&mounts
);
1344 build_fs_mount_list(&mounts
, &local_err
);
1346 error_propagate(errp
, local_err
);
1350 QTAILQ_FOREACH(mount
, &mounts
, next
) {
1351 fd
= qemu_open(mount
->dirname
, O_RDONLY
);
1353 error_setg_errno(errp
, errno
, "failed to open %s", mount
->dirname
);
1357 /* We try to cull filesytems we know won't work in advance, but other
1358 * filesytems may not implement fstrim for less obvious reasons. These
1359 * will report EOPNOTSUPP; we simply ignore these errors. Any other
1360 * error means an unexpected error, so return it in those cases. In
1361 * some other cases ENOTTY will be reported (e.g. CD-ROMs).
1363 ret
= ioctl(fd
, FITRIM
, &r
);
1365 if (errno
!= ENOTTY
&& errno
!= EOPNOTSUPP
) {
1366 error_setg_errno(errp
, errno
, "failed to trim %s",
1376 free_fs_mount_list(&mounts
);
1378 #endif /* CONFIG_FSTRIM */
1381 #define LINUX_SYS_STATE_FILE "/sys/power/state"
1382 #define SUSPEND_SUPPORTED 0
1383 #define SUSPEND_NOT_SUPPORTED 1
1385 static void bios_supports_mode(const char *pmutils_bin
, const char *pmutils_arg
,
1386 const char *sysfile_str
, Error
**errp
)
1388 Error
*local_err
= NULL
;
1393 pmutils_path
= g_find_program_in_path(pmutils_bin
);
1397 char buf
[32]; /* hopefully big enough */
1402 reopen_fd_to_null(0);
1403 reopen_fd_to_null(1);
1404 reopen_fd_to_null(2);
1407 execle(pmutils_path
, pmutils_bin
, pmutils_arg
, NULL
, environ
);
1411 * If we get here either pm-utils is not installed or execle() has
1412 * failed. Let's try the manual method if the caller wants it.
1416 _exit(SUSPEND_NOT_SUPPORTED
);
1419 fd
= open(LINUX_SYS_STATE_FILE
, O_RDONLY
);
1421 _exit(SUSPEND_NOT_SUPPORTED
);
1424 ret
= read(fd
, buf
, sizeof(buf
)-1);
1426 _exit(SUSPEND_NOT_SUPPORTED
);
1430 if (strstr(buf
, sysfile_str
)) {
1431 _exit(SUSPEND_SUPPORTED
);
1434 _exit(SUSPEND_NOT_SUPPORTED
);
1435 } else if (pid
< 0) {
1436 error_setg_errno(errp
, errno
, "failed to create child process");
1440 ga_wait_child(pid
, &status
, &local_err
);
1442 error_propagate(errp
, local_err
);
1446 if (!WIFEXITED(status
)) {
1447 error_setg(errp
, "child process has terminated abnormally");
1451 switch (WEXITSTATUS(status
)) {
1452 case SUSPEND_SUPPORTED
:
1454 case SUSPEND_NOT_SUPPORTED
:
1456 "the requested suspend mode is not supported by the guest");
1460 "the helper program '%s' returned an unexpected exit status"
1461 " code (%d)", pmutils_path
, WEXITSTATUS(status
));
1466 g_free(pmutils_path
);
1469 static void guest_suspend(const char *pmutils_bin
, const char *sysfile_str
,
1472 Error
*local_err
= NULL
;
1477 pmutils_path
= g_find_program_in_path(pmutils_bin
);
1485 reopen_fd_to_null(0);
1486 reopen_fd_to_null(1);
1487 reopen_fd_to_null(2);
1490 execle(pmutils_path
, pmutils_bin
, NULL
, environ
);
1494 * If we get here either pm-utils is not installed or execle() has
1495 * failed. Let's try the manual method if the caller wants it.
1499 _exit(EXIT_FAILURE
);
1502 fd
= open(LINUX_SYS_STATE_FILE
, O_WRONLY
);
1504 _exit(EXIT_FAILURE
);
1507 if (write(fd
, sysfile_str
, strlen(sysfile_str
)) < 0) {
1508 _exit(EXIT_FAILURE
);
1511 _exit(EXIT_SUCCESS
);
1512 } else if (pid
< 0) {
1513 error_setg_errno(errp
, errno
, "failed to create child process");
1517 ga_wait_child(pid
, &status
, &local_err
);
1519 error_propagate(errp
, local_err
);
1523 if (!WIFEXITED(status
)) {
1524 error_setg(errp
, "child process has terminated abnormally");
1528 if (WEXITSTATUS(status
)) {
1529 error_setg(errp
, "child process has failed to suspend");
1534 g_free(pmutils_path
);
1537 void qmp_guest_suspend_disk(Error
**errp
)
1539 Error
*local_err
= NULL
;
1541 bios_supports_mode("pm-is-supported", "--hibernate", "disk", &local_err
);
1543 error_propagate(errp
, local_err
);
1547 guest_suspend("pm-hibernate", "disk", errp
);
1550 void qmp_guest_suspend_ram(Error
**errp
)
1552 Error
*local_err
= NULL
;
1554 bios_supports_mode("pm-is-supported", "--suspend", "mem", &local_err
);
1556 error_propagate(errp
, local_err
);
1560 guest_suspend("pm-suspend", "mem", errp
);
1563 void qmp_guest_suspend_hybrid(Error
**errp
)
1565 Error
*local_err
= NULL
;
1567 bios_supports_mode("pm-is-supported", "--suspend-hybrid", NULL
,
1570 error_propagate(errp
, local_err
);
1574 guest_suspend("pm-suspend-hybrid", NULL
, errp
);
1577 static GuestNetworkInterfaceList
*
1578 guest_find_interface(GuestNetworkInterfaceList
*head
,
1581 for (; head
; head
= head
->next
) {
1582 if (strcmp(head
->value
->name
, name
) == 0) {
1591 * Build information about guest interfaces
1593 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
1595 GuestNetworkInterfaceList
*head
= NULL
, *cur_item
= NULL
;
1596 struct ifaddrs
*ifap
, *ifa
;
1598 if (getifaddrs(&ifap
) < 0) {
1599 error_setg_errno(errp
, errno
, "getifaddrs failed");
1603 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
1604 GuestNetworkInterfaceList
*info
;
1605 GuestIpAddressList
**address_list
= NULL
, *address_item
= NULL
;
1606 char addr4
[INET_ADDRSTRLEN
];
1607 char addr6
[INET6_ADDRSTRLEN
];
1610 unsigned char *mac_addr
;
1613 g_debug("Processing %s interface", ifa
->ifa_name
);
1615 info
= guest_find_interface(head
, ifa
->ifa_name
);
1618 info
= g_malloc0(sizeof(*info
));
1619 info
->value
= g_malloc0(sizeof(*info
->value
));
1620 info
->value
->name
= g_strdup(ifa
->ifa_name
);
1623 head
= cur_item
= info
;
1625 cur_item
->next
= info
;
1630 if (!info
->value
->has_hardware_address
&&
1631 ifa
->ifa_flags
& SIOCGIFHWADDR
) {
1632 /* we haven't obtained HW address yet */
1633 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
1635 error_setg_errno(errp
, errno
, "failed to create socket");
1639 memset(&ifr
, 0, sizeof(ifr
));
1640 pstrcpy(ifr
.ifr_name
, IF_NAMESIZE
, info
->value
->name
);
1641 if (ioctl(sock
, SIOCGIFHWADDR
, &ifr
) == -1) {
1642 error_setg_errno(errp
, errno
,
1643 "failed to get MAC address of %s",
1650 mac_addr
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
1652 info
->value
->hardware_address
=
1653 g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
1654 (int) mac_addr
[0], (int) mac_addr
[1],
1655 (int) mac_addr
[2], (int) mac_addr
[3],
1656 (int) mac_addr
[4], (int) mac_addr
[5]);
1658 info
->value
->has_hardware_address
= true;
1661 if (ifa
->ifa_addr
&&
1662 ifa
->ifa_addr
->sa_family
== AF_INET
) {
1663 /* interface with IPv4 address */
1664 p
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
1665 if (!inet_ntop(AF_INET
, p
, addr4
, sizeof(addr4
))) {
1666 error_setg_errno(errp
, errno
, "inet_ntop failed");
1670 address_item
= g_malloc0(sizeof(*address_item
));
1671 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1672 address_item
->value
->ip_address
= g_strdup(addr4
);
1673 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV4
;
1675 if (ifa
->ifa_netmask
) {
1676 /* Count the number of set bits in netmask.
1677 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1678 p
= &((struct sockaddr_in
*)ifa
->ifa_netmask
)->sin_addr
;
1679 address_item
->value
->prefix
= ctpop32(((uint32_t *) p
)[0]);
1681 } else if (ifa
->ifa_addr
&&
1682 ifa
->ifa_addr
->sa_family
== AF_INET6
) {
1683 /* interface with IPv6 address */
1684 p
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
1685 if (!inet_ntop(AF_INET6
, p
, addr6
, sizeof(addr6
))) {
1686 error_setg_errno(errp
, errno
, "inet_ntop failed");
1690 address_item
= g_malloc0(sizeof(*address_item
));
1691 address_item
->value
= g_malloc0(sizeof(*address_item
->value
));
1692 address_item
->value
->ip_address
= g_strdup(addr6
);
1693 address_item
->value
->ip_address_type
= GUEST_IP_ADDRESS_TYPE_IPV6
;
1695 if (ifa
->ifa_netmask
) {
1696 /* Count the number of set bits in netmask.
1697 * This is safe as '1' and '0' cannot be shuffled in netmask. */
1698 p
= &((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
1699 address_item
->value
->prefix
=
1700 ctpop32(((uint32_t *) p
)[0]) +
1701 ctpop32(((uint32_t *) p
)[1]) +
1702 ctpop32(((uint32_t *) p
)[2]) +
1703 ctpop32(((uint32_t *) p
)[3]);
1707 if (!address_item
) {
1711 address_list
= &info
->value
->ip_addresses
;
1713 while (*address_list
&& (*address_list
)->next
) {
1714 address_list
= &(*address_list
)->next
;
1717 if (!*address_list
) {
1718 *address_list
= address_item
;
1720 (*address_list
)->next
= address_item
;
1723 info
->value
->has_ip_addresses
= true;
1733 qapi_free_GuestNetworkInterfaceList(head
);
1737 #define SYSCONF_EXACT(name, errp) sysconf_exact((name), #name, (errp))
1739 static long sysconf_exact(int name
, const char *name_str
, Error
**errp
)
1744 ret
= sysconf(name
);
1747 error_setg(errp
, "sysconf(%s): value indefinite", name_str
);
1749 error_setg_errno(errp
, errno
, "sysconf(%s)", name_str
);
1755 /* Transfer online/offline status between @vcpu and the guest system.
1757 * On input either @errp or *@errp must be NULL.
1759 * In system-to-@vcpu direction, the following @vcpu fields are accessed:
1760 * - R: vcpu->logical_id
1762 * - W: vcpu->can_offline
1764 * In @vcpu-to-system direction, the following @vcpu fields are accessed:
1765 * - R: vcpu->logical_id
1768 * Written members remain unmodified on error.
1770 static void transfer_vcpu(GuestLogicalProcessor
*vcpu
, bool sys2vcpu
,
1776 dirpath
= g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64
"/",
1778 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
1780 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
1782 static const char fn
[] = "online";
1786 fd
= openat(dirfd
, fn
, sys2vcpu
? O_RDONLY
: O_RDWR
);
1788 if (errno
!= ENOENT
) {
1789 error_setg_errno(errp
, errno
, "open(\"%s/%s\")", dirpath
, fn
);
1790 } else if (sys2vcpu
) {
1791 vcpu
->online
= true;
1792 vcpu
->can_offline
= false;
1793 } else if (!vcpu
->online
) {
1794 error_setg(errp
, "logical processor #%" PRId64
" can't be "
1795 "offlined", vcpu
->logical_id
);
1796 } /* otherwise pretend successful re-onlining */
1798 unsigned char status
;
1800 res
= pread(fd
, &status
, 1, 0);
1802 error_setg_errno(errp
, errno
, "pread(\"%s/%s\")", dirpath
, fn
);
1803 } else if (res
== 0) {
1804 error_setg(errp
, "pread(\"%s/%s\"): unexpected EOF", dirpath
,
1806 } else if (sys2vcpu
) {
1807 vcpu
->online
= (status
!= '0');
1808 vcpu
->can_offline
= true;
1809 } else if (vcpu
->online
!= (status
!= '0')) {
1810 status
= '0' + vcpu
->online
;
1811 if (pwrite(fd
, &status
, 1, 0) == -1) {
1812 error_setg_errno(errp
, errno
, "pwrite(\"%s/%s\")", dirpath
,
1815 } /* otherwise pretend successful re-(on|off)-lining */
1828 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
1831 GuestLogicalProcessorList
*head
, **link
;
1833 Error
*local_err
= NULL
;
1838 sc_max
= SYSCONF_EXACT(_SC_NPROCESSORS_CONF
, &local_err
);
1840 while (local_err
== NULL
&& current
< sc_max
) {
1841 GuestLogicalProcessor
*vcpu
;
1842 GuestLogicalProcessorList
*entry
;
1844 vcpu
= g_malloc0(sizeof *vcpu
);
1845 vcpu
->logical_id
= current
++;
1846 vcpu
->has_can_offline
= true; /* lolspeak ftw */
1847 transfer_vcpu(vcpu
, true, &local_err
);
1849 entry
= g_malloc0(sizeof *entry
);
1850 entry
->value
= vcpu
;
1853 link
= &entry
->next
;
1856 if (local_err
== NULL
) {
1857 /* there's no guest with zero VCPUs */
1858 g_assert(head
!= NULL
);
1862 qapi_free_GuestLogicalProcessorList(head
);
1863 error_propagate(errp
, local_err
);
1867 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
1870 Error
*local_err
= NULL
;
1873 while (vcpus
!= NULL
) {
1874 transfer_vcpu(vcpus
->value
, false, &local_err
);
1875 if (local_err
!= NULL
) {
1879 vcpus
= vcpus
->next
;
1882 if (local_err
!= NULL
) {
1883 if (processed
== 0) {
1884 error_propagate(errp
, local_err
);
1886 error_free(local_err
);
1893 void qmp_guest_set_user_password(const char *username
,
1894 const char *password
,
1898 Error
*local_err
= NULL
;
1899 char *passwd_path
= NULL
;
1902 int datafd
[2] = { -1, -1 };
1903 char *rawpasswddata
= NULL
;
1904 size_t rawpasswdlen
;
1905 char *chpasswddata
= NULL
;
1908 rawpasswddata
= (char *)g_base64_decode(password
, &rawpasswdlen
);
1909 rawpasswddata
= g_renew(char, rawpasswddata
, rawpasswdlen
+ 1);
1910 rawpasswddata
[rawpasswdlen
] = '\0';
1912 if (strchr(rawpasswddata
, '\n')) {
1913 error_setg(errp
, "forbidden characters in raw password");
1917 if (strchr(username
, '\n') ||
1918 strchr(username
, ':')) {
1919 error_setg(errp
, "forbidden characters in username");
1923 chpasswddata
= g_strdup_printf("%s:%s\n", username
, rawpasswddata
);
1924 chpasswdlen
= strlen(chpasswddata
);
1926 passwd_path
= g_find_program_in_path("chpasswd");
1929 error_setg(errp
, "cannot find 'passwd' program in PATH");
1933 if (pipe(datafd
) < 0) {
1934 error_setg(errp
, "cannot create pipe FDs");
1944 reopen_fd_to_null(1);
1945 reopen_fd_to_null(2);
1948 execle(passwd_path
, "chpasswd", "-e", NULL
, environ
);
1950 execle(passwd_path
, "chpasswd", NULL
, environ
);
1952 _exit(EXIT_FAILURE
);
1953 } else if (pid
< 0) {
1954 error_setg_errno(errp
, errno
, "failed to create child process");
1960 if (qemu_write_full(datafd
[1], chpasswddata
, chpasswdlen
) != chpasswdlen
) {
1961 error_setg_errno(errp
, errno
, "cannot write new account password");
1967 ga_wait_child(pid
, &status
, &local_err
);
1969 error_propagate(errp
, local_err
);
1973 if (!WIFEXITED(status
)) {
1974 error_setg(errp
, "child process has terminated abnormally");
1978 if (WEXITSTATUS(status
)) {
1979 error_setg(errp
, "child process has failed to set user password");
1984 g_free(chpasswddata
);
1985 g_free(rawpasswddata
);
1986 g_free(passwd_path
);
1987 if (datafd
[0] != -1) {
1990 if (datafd
[1] != -1) {
1995 static void ga_read_sysfs_file(int dirfd
, const char *pathname
, char *buf
,
1996 int size
, Error
**errp
)
2002 fd
= openat(dirfd
, pathname
, O_RDONLY
);
2004 error_setg_errno(errp
, errno
, "open sysfs file \"%s\"", pathname
);
2008 res
= pread(fd
, buf
, size
, 0);
2010 error_setg_errno(errp
, errno
, "pread sysfs file \"%s\"", pathname
);
2011 } else if (res
== 0) {
2012 error_setg(errp
, "pread sysfs file \"%s\": unexpected EOF", pathname
);
2017 static void ga_write_sysfs_file(int dirfd
, const char *pathname
,
2018 const char *buf
, int size
, Error
**errp
)
2023 fd
= openat(dirfd
, pathname
, O_WRONLY
);
2025 error_setg_errno(errp
, errno
, "open sysfs file \"%s\"", pathname
);
2029 if (pwrite(fd
, buf
, size
, 0) == -1) {
2030 error_setg_errno(errp
, errno
, "pwrite sysfs file \"%s\"", pathname
);
2036 /* Transfer online/offline status between @mem_blk and the guest system.
2038 * On input either @errp or *@errp must be NULL.
2040 * In system-to-@mem_blk direction, the following @mem_blk fields are accessed:
2041 * - R: mem_blk->phys_index
2042 * - W: mem_blk->online
2043 * - W: mem_blk->can_offline
2045 * In @mem_blk-to-system direction, the following @mem_blk fields are accessed:
2046 * - R: mem_blk->phys_index
2047 * - R: mem_blk->online
2048 *- R: mem_blk->can_offline
2049 * Written members remain unmodified on error.
2051 static void transfer_memory_block(GuestMemoryBlock
*mem_blk
, bool sys2memblk
,
2052 GuestMemoryBlockResponse
*result
,
2058 Error
*local_err
= NULL
;
2064 error_setg(errp
, "Internal error, 'result' should not be NULL");
2068 dp
= opendir("/sys/devices/system/memory/");
2069 /* if there is no 'memory' directory in sysfs,
2070 * we think this VM does not support online/offline memory block,
2071 * any other solution?
2073 if (!dp
&& errno
== ENOENT
) {
2075 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED
;
2081 dirpath
= g_strdup_printf("/sys/devices/system/memory/memory%" PRId64
"/",
2082 mem_blk
->phys_index
);
2083 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
2086 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
2088 if (errno
== ENOENT
) {
2089 result
->response
= GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND
;
2092 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2100 status
= g_malloc0(10);
2101 ga_read_sysfs_file(dirfd
, "state", status
, 10, &local_err
);
2103 /* treat with sysfs file that not exist in old kernel */
2104 if (errno
== ENOENT
) {
2105 error_free(local_err
);
2107 mem_blk
->online
= true;
2108 mem_blk
->can_offline
= false;
2109 } else if (!mem_blk
->online
) {
2111 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED
;
2115 error_propagate(errp
, local_err
);
2118 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2125 char removable
= '0';
2127 mem_blk
->online
= (strncmp(status
, "online", 6) == 0);
2129 ga_read_sysfs_file(dirfd
, "removable", &removable
, 1, &local_err
);
2131 /* if no 'removable' file, it does't support offline mem blk */
2132 if (errno
== ENOENT
) {
2133 error_free(local_err
);
2134 mem_blk
->can_offline
= false;
2136 error_propagate(errp
, local_err
);
2139 mem_blk
->can_offline
= (removable
!= '0');
2142 if (mem_blk
->online
!= (strncmp(status
, "online", 6) == 0)) {
2143 char *new_state
= mem_blk
->online
? g_strdup("online") :
2144 g_strdup("offline");
2146 ga_write_sysfs_file(dirfd
, "state", new_state
, strlen(new_state
),
2150 error_free(local_err
);
2152 GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED
;
2156 result
->response
= GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS
;
2157 result
->has_error_code
= false;
2158 } /* otherwise pretend successful re-(on|off)-lining */
2169 result
->has_error_code
= true;
2170 result
->error_code
= errno
;
2174 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
2176 GuestMemoryBlockList
*head
, **link
;
2177 Error
*local_err
= NULL
;
2184 dp
= opendir("/sys/devices/system/memory/");
2186 error_setg_errno(errp
, errno
, "Can't open directory"
2187 "\"/sys/devices/system/memory/\"\n");
2191 /* Note: the phys_index of memory block may be discontinuous,
2192 * this is because a memblk is the unit of the Sparse Memory design, which
2193 * allows discontinuous memory ranges (ex. NUMA), so here we should
2194 * traverse the memory block directory.
2196 while ((de
= readdir(dp
)) != NULL
) {
2197 GuestMemoryBlock
*mem_blk
;
2198 GuestMemoryBlockList
*entry
;
2200 if ((strncmp(de
->d_name
, "memory", 6) != 0) ||
2201 !(de
->d_type
& DT_DIR
)) {
2205 mem_blk
= g_malloc0(sizeof *mem_blk
);
2206 /* The d_name is "memoryXXX", phys_index is block id, same as XXX */
2207 mem_blk
->phys_index
= strtoul(&de
->d_name
[6], NULL
, 10);
2208 mem_blk
->has_can_offline
= true; /* lolspeak ftw */
2209 transfer_memory_block(mem_blk
, true, NULL
, &local_err
);
2211 entry
= g_malloc0(sizeof *entry
);
2212 entry
->value
= mem_blk
;
2215 link
= &entry
->next
;
2219 if (local_err
== NULL
) {
2220 /* there's no guest with zero memory blocks */
2222 error_setg(errp
, "guest reported zero memory blocks!");
2227 qapi_free_GuestMemoryBlockList(head
);
2228 error_propagate(errp
, local_err
);
2232 GuestMemoryBlockResponseList
*
2233 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
2235 GuestMemoryBlockResponseList
*head
, **link
;
2236 Error
*local_err
= NULL
;
2241 while (mem_blks
!= NULL
) {
2242 GuestMemoryBlockResponse
*result
;
2243 GuestMemoryBlockResponseList
*entry
;
2244 GuestMemoryBlock
*current_mem_blk
= mem_blks
->value
;
2246 result
= g_malloc0(sizeof(*result
));
2247 result
->phys_index
= current_mem_blk
->phys_index
;
2248 transfer_memory_block(current_mem_blk
, false, result
, &local_err
);
2249 if (local_err
) { /* should never happen */
2252 entry
= g_malloc0(sizeof *entry
);
2253 entry
->value
= result
;
2256 link
= &entry
->next
;
2257 mem_blks
= mem_blks
->next
;
2262 qapi_free_GuestMemoryBlockResponseList(head
);
2263 error_propagate(errp
, local_err
);
2267 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
2269 Error
*local_err
= NULL
;
2273 GuestMemoryBlockInfo
*info
;
2275 dirpath
= g_strdup_printf("/sys/devices/system/memory/");
2276 dirfd
= open(dirpath
, O_RDONLY
| O_DIRECTORY
);
2278 error_setg_errno(errp
, errno
, "open(\"%s\")", dirpath
);
2284 buf
= g_malloc0(20);
2285 ga_read_sysfs_file(dirfd
, "block_size_bytes", buf
, 20, &local_err
);
2288 error_propagate(errp
, local_err
);
2292 info
= g_new0(GuestMemoryBlockInfo
, 1);
2293 info
->size
= strtol(buf
, NULL
, 16); /* the unit is bytes */
2300 #else /* defined(__linux__) */
2302 void qmp_guest_suspend_disk(Error
**errp
)
2304 error_set(errp
, QERR_UNSUPPORTED
);
2307 void qmp_guest_suspend_ram(Error
**errp
)
2309 error_set(errp
, QERR_UNSUPPORTED
);
2312 void qmp_guest_suspend_hybrid(Error
**errp
)
2314 error_set(errp
, QERR_UNSUPPORTED
);
2317 GuestNetworkInterfaceList
*qmp_guest_network_get_interfaces(Error
**errp
)
2319 error_set(errp
, QERR_UNSUPPORTED
);
2323 GuestLogicalProcessorList
*qmp_guest_get_vcpus(Error
**errp
)
2325 error_set(errp
, QERR_UNSUPPORTED
);
2329 int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList
*vcpus
, Error
**errp
)
2331 error_set(errp
, QERR_UNSUPPORTED
);
2335 void qmp_guest_set_user_password(const char *username
,
2336 const char *password
,
2340 error_set(errp
, QERR_UNSUPPORTED
);
2343 GuestMemoryBlockList
*qmp_guest_get_memory_blocks(Error
**errp
)
2345 error_set(errp
, QERR_UNSUPPORTED
);
2349 GuestMemoryBlockResponseList
*
2350 qmp_guest_set_memory_blocks(GuestMemoryBlockList
*mem_blks
, Error
**errp
)
2352 error_set(errp
, QERR_UNSUPPORTED
);
2356 GuestMemoryBlockInfo
*qmp_guest_get_memory_block_info(Error
**errp
)
2358 error_set(errp
, QERR_UNSUPPORTED
);
2364 #if !defined(CONFIG_FSFREEZE)
2366 GuestFilesystemInfoList
*qmp_guest_get_fsinfo(Error
**errp
)
2368 error_set(errp
, QERR_UNSUPPORTED
);
2372 GuestFsfreezeStatus
qmp_guest_fsfreeze_status(Error
**errp
)
2374 error_set(errp
, QERR_UNSUPPORTED
);
2379 int64_t qmp_guest_fsfreeze_freeze(Error
**errp
)
2381 error_set(errp
, QERR_UNSUPPORTED
);
2386 int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints
,
2387 strList
*mountpoints
,
2390 error_set(errp
, QERR_UNSUPPORTED
);
2395 int64_t qmp_guest_fsfreeze_thaw(Error
**errp
)
2397 error_set(errp
, QERR_UNSUPPORTED
);
2401 #endif /* CONFIG_FSFREEZE */
2403 #if !defined(CONFIG_FSTRIM)
2404 void qmp_guest_fstrim(bool has_minimum
, int64_t minimum
, Error
**errp
)
2406 error_set(errp
, QERR_UNSUPPORTED
);
2410 /* add unsupported commands to the blacklist */
2411 GList
*ga_command_blacklist_init(GList
*blacklist
)
2413 #if !defined(__linux__)
2415 const char *list
[] = {
2416 "guest-suspend-disk", "guest-suspend-ram",
2417 "guest-suspend-hybrid", "guest-network-get-interfaces",
2418 "guest-get-vcpus", "guest-set-vcpus",
2419 "guest-get-memory-blocks", "guest-set-memory-blocks",
2420 "guest-get-memory-block-size", NULL
};
2421 char **p
= (char **)list
;
2424 blacklist
= g_list_append(blacklist
, *p
++);
2429 #if !defined(CONFIG_FSFREEZE)
2431 const char *list
[] = {
2432 "guest-get-fsinfo", "guest-fsfreeze-status",
2433 "guest-fsfreeze-freeze", "guest-fsfreeze-freeze-list",
2434 "guest-fsfreeze-thaw", "guest-get-fsinfo", NULL
};
2435 char **p
= (char **)list
;
2438 blacklist
= g_list_append(blacklist
, *p
++);
2443 #if !defined(CONFIG_FSTRIM)
2444 blacklist
= g_list_append(blacklist
, (char *)"guest-fstrim");
2450 /* register init/cleanup routines for stateful command groups */
2451 void ga_command_state_init(GAState
*s
, GACommandState
*cs
)
2453 #if defined(CONFIG_FSFREEZE)
2454 ga_command_state_add(cs
, NULL
, guest_fsfreeze_cleanup
);
2456 ga_command_state_add(cs
, guest_file_init
, NULL
);