summaryrefslogtreecommitdiff
path: root/qga/commands-posix.c
diff options
context:
space:
mode:
Diffstat (limited to 'qga/commands-posix.c')
-rw-r--r--qga/commands-posix.c71
1 files changed, 40 insertions, 31 deletions
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index e199738c7..6b5f11f83 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -99,7 +99,7 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
reopen_fd_to_null(1);
reopen_fd_to_null(2);
- execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
+ execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
"hypervisor initiated shutdown", (char*)NULL, environ);
_exit(EXIT_FAILURE);
} else if (pid < 0) {
@@ -108,7 +108,7 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
}
ga_wait_child(pid, &status, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
return;
}
@@ -142,7 +142,7 @@ int64_t qmp_guest_get_time(Error **errp)
return time_ns;
}
-void qmp_guest_set_time(int64_t time_ns, Error **errp)
+void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
{
int ret;
int status;
@@ -150,22 +150,28 @@ void qmp_guest_set_time(int64_t time_ns, Error **errp)
Error *local_err = NULL;
struct timeval tv;
- /* year-2038 will overflow in case time_t is 32bit */
- if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
- error_setg(errp, "Time %" PRId64 " is too large", time_ns);
- return;
- }
+ /* If user has passed a time, validate and set it. */
+ if (has_time) {
+ /* year-2038 will overflow in case time_t is 32bit */
+ if (time_ns / 1000000000 != (time_t)(time_ns / 1000000000)) {
+ error_setg(errp, "Time %" PRId64 " is too large", time_ns);
+ return;
+ }
- tv.tv_sec = time_ns / 1000000000;
- tv.tv_usec = (time_ns % 1000000000) / 1000;
+ tv.tv_sec = time_ns / 1000000000;
+ tv.tv_usec = (time_ns % 1000000000) / 1000;
- ret = settimeofday(&tv, NULL);
- if (ret < 0) {
- error_setg_errno(errp, errno, "Failed to set time to guest");
- return;
+ ret = settimeofday(&tv, NULL);
+ if (ret < 0) {
+ error_setg_errno(errp, errno, "Failed to set time to guest");
+ return;
+ }
}
- /* Set the Hardware Clock to the current System Time. */
+ /* Now, if user has passed a time to set and the system time is set, we
+ * just need to synchronize the hardware clock. However, if no time was
+ * passed, user is requesting the opposite: set the system time from the
+ * hardware clock. */
pid = fork();
if (pid == 0) {
setsid();
@@ -173,7 +179,10 @@ void qmp_guest_set_time(int64_t time_ns, Error **errp)
reopen_fd_to_null(1);
reopen_fd_to_null(2);
- execle("/sbin/hwclock", "hwclock", "-w", NULL, environ);
+ /* Use '/sbin/hwclock -w' to set RTC from the system time,
+ * or '/sbin/hwclock -s' to set the system time from RTC. */
+ execle("/sbin/hwclock", "hwclock", has_time ? "-w" : "-s",
+ NULL, environ);
_exit(EXIT_FAILURE);
} else if (pid < 0) {
error_setg_errno(errp, errno, "failed to create child process");
@@ -181,7 +190,7 @@ void qmp_guest_set_time(int64_t time_ns, Error **errp)
}
ga_wait_child(pid, &status, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(errp, local_err);
return;
}
@@ -401,7 +410,7 @@ int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, E
return -1;
}
- slog("guest-file-open, handle: %d", handle);
+ slog("guest-file-open, handle: %" PRId64, handle);
return handle;
}
@@ -410,7 +419,7 @@ void qmp_guest_file_close(int64_t handle, Error **err)
GuestFileHandle *gfh = guest_file_handle_find(handle, err);
int ret;
- slog("guest-file-close called, handle: %ld", handle);
+ slog("guest-file-close called, handle: %" PRId64, handle);
if (!gfh) {
return;
}
@@ -451,7 +460,7 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
read_count = fread(buf, 1, count, fh);
if (ferror(fh)) {
error_setg_errno(err, errno, "failed to read file");
- slog("guest-file-read failed, handle: %ld", handle);
+ slog("guest-file-read failed, handle: %" PRId64, handle);
} else {
buf[read_count] = 0;
read_data = g_malloc0(sizeof(GuestFileRead));
@@ -496,7 +505,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
write_count = fwrite(buf, 1, count, fh);
if (ferror(fh)) {
error_setg_errno(err, errno, "failed to write to file");
- slog("guest-file-write failed, handle: %ld", handle);
+ slog("guest-file-write failed, handle: %" PRId64, handle);
} else {
write_data = g_malloc0(sizeof(GuestFileWrite));
write_data->count = write_count;
@@ -525,7 +534,7 @@ struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
if (ret == -1) {
error_setg_errno(err, errno, "failed to seek file");
} else {
- seek_data = g_malloc0(sizeof(GuestFileRead));
+ seek_data = g_new0(GuestFileSeek, 1);
seek_data->position = ftell(fh);
seek_data->eof = feof(fh);
}
@@ -566,7 +575,7 @@ typedef struct FsMount {
QTAILQ_ENTRY(FsMount) next;
} FsMount;
-typedef QTAILQ_HEAD(, FsMount) FsMountList;
+typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList;
static void free_fs_mount_list(FsMountList *mounts)
{
@@ -669,7 +678,7 @@ static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **err)
}
ga_wait_child(pid, &status, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
return;
}
@@ -713,14 +722,14 @@ int64_t qmp_guest_fsfreeze_freeze(Error **err)
slog("guest-fsfreeze called");
execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
return -1;
}
QTAILQ_INIT(&mounts);
build_fs_mount_list(&mounts, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
return -1;
}
@@ -728,7 +737,7 @@ int64_t qmp_guest_fsfreeze_freeze(Error **err)
/* cannot risk guest agent blocking itself on a write in this state */
ga_set_frozen(ga_state);
- QTAILQ_FOREACH(mount, &mounts, next) {
+ QTAILQ_FOREACH_REVERSE(mount, &mounts, FsMountList, next) {
fd = qemu_open(mount->dirname, O_RDONLY);
if (fd == -1) {
error_setg_errno(err, errno, "failed to open %s", mount->dirname);
@@ -780,7 +789,7 @@ int64_t qmp_guest_fsfreeze_thaw(Error **err)
QTAILQ_INIT(&mounts);
build_fs_mount_list(&mounts, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
return 0;
}
@@ -861,7 +870,7 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
QTAILQ_INIT(&mounts);
build_fs_mount_list(&mounts, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
return;
}
@@ -957,7 +966,7 @@ static void bios_supports_mode(const char *pmutils_bin, const char *pmutils_arg,
}
ga_wait_child(pid, &status, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
goto out;
}
@@ -1034,7 +1043,7 @@ static void guest_suspend(const char *pmutils_bin, const char *sysfile_str,
}
ga_wait_child(pid, &status, &local_err);
- if (error_is_set(&local_err)) {
+ if (local_err) {
error_propagate(err, local_err);
goto out;
}