[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 4/7] qga/commands-posix: qmp_guest_set_time: use ga_run_comman
From: |
Andrey Drobyshev |
Subject: |
[PATCH v2 4/7] qga/commands-posix: qmp_guest_set_time: use ga_run_command helper |
Date: |
Fri, 1 Mar 2024 19:28:55 +0200 |
There's no need to check for the existence of "/sbin/hwclock", the
exec() call will do that for us.
Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
qga/commands-posix.c | 43 +++----------------------------------------
1 file changed, 3 insertions(+), 40 deletions(-)
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 003054891f..e44203a273 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -269,21 +269,9 @@ void qmp_guest_shutdown(const char *mode, Error **errp)
void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
{
int ret;
- int status;
- pid_t pid;
Error *local_err = NULL;
struct timeval tv;
- static const char hwclock_path[] = "/sbin/hwclock";
- static int hwclock_available = -1;
-
- if (hwclock_available < 0) {
- hwclock_available = (access(hwclock_path, X_OK) == 0);
- }
-
- if (!hwclock_available) {
- error_setg(errp, QERR_UNSUPPORTED);
- return;
- }
+ const char *argv[] = {"/sbin/hwclock", has_time ? "-w" : "-s", NULL};
/* If user has passed a time, validate and set it. */
if (has_time) {
@@ -314,37 +302,12 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns,
Error **errp)
* 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 (RTC). */
- pid = fork();
- if (pid == 0) {
- setsid();
- reopen_fd_to_null(0);
- reopen_fd_to_null(1);
- reopen_fd_to_null(2);
-
- /* Use '/sbin/hwclock -w' to set RTC from the system time,
- * or '/sbin/hwclock -s' to set the system time from RTC. */
- execl(hwclock_path, "hwclock", has_time ? "-w" : "-s", NULL);
- _exit(EXIT_FAILURE);
- } else if (pid < 0) {
- error_setg_errno(errp, errno, "failed to create child process");
- return;
- }
-
- ga_wait_child(pid, &status, &local_err);
+ ga_run_command(argv, NULL, "set hardware clock to system time",
+ &local_err);
if (local_err) {
error_propagate(errp, local_err);
return;
}
-
- if (!WIFEXITED(status)) {
- error_setg(errp, "child process has terminated abnormally");
- return;
- }
-
- if (WEXITSTATUS(status)) {
- error_setg(errp, "hwclock failed to set hardware clock to system
time");
- return;
- }
}
typedef enum {
--
2.39.3
- [PATCH v2 0/7] qga/commands-posix: replace code duplicating commands with a helper, Andrey Drobyshev, 2024/03/01
- [PATCH v2 6/7] qga/commands-posix: use ga_run_command helper when suspending via sysfs, Andrey Drobyshev, 2024/03/01
- [PATCH v2 5/7] qga/commands-posix: execute_fsfreeze_hook: use ga_run_command helper, Andrey Drobyshev, 2024/03/01
- [PATCH v2 2/7] qga: introduce ga_run_command() helper for guest cmd execution, Andrey Drobyshev, 2024/03/01
- [PATCH v2 1/7] qga: guest-get-fsinfo: add optional 'total-bytes-root' field, Andrey Drobyshev, 2024/03/01
- [PATCH v2 4/7] qga/commands-posix: qmp_guest_set_time: use ga_run_command helper,
Andrey Drobyshev <=
- [PATCH v2 3/7] qga/commands-posix: qmp_guest_shutdown: use ga_run_command helper, Andrey Drobyshev, 2024/03/01
- [PATCH v2 7/7] qga/commands-posix: qmp_guest_set_user_password: use ga_run_command helper, Andrey Drobyshev, 2024/03/01
- Re: [PATCH v2 0/7] qga/commands-posix: replace code duplicating commands with a helper, Konstantin Kostiuk, 2024/03/04