Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Guest Agent common/cross-platform command implementations |
| 3 | * |
| 4 | * Copyright IBM Corp. 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Michael Roth <mdroth@linux.vnet.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
Peter Maydell | 4459bf3 | 2016-01-29 17:49:58 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | 1329651f | 2020-04-14 15:30:44 +0200 | [diff] [blame] | 14 | #include "qemu/units.h" |
Michael S. Tsirkin | dc03272 | 2018-05-03 22:50:57 +0300 | [diff] [blame] | 15 | #include "guest-agent-core.h" |
Markus Armbruster | eb815e2 | 2018-02-11 10:36:05 +0100 | [diff] [blame] | 16 | #include "qga-qapi-commands.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Daniel P. Berrange | 920639c | 2015-11-23 15:37:07 +0000 | [diff] [blame] | 18 | #include "qemu/base64.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 19 | #include "qemu/cutils.h" |
Philippe Mathieu-Daudé | ead83a1 | 2020-04-14 15:30:43 +0200 | [diff] [blame] | 20 | #include "commands-common.h" |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 21 | |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 22 | /* Maximum captured guest-exec out_data/err_data - 16MB */ |
AlexChen | 0697e9e | 2020-10-26 17:05:38 +0800 | [diff] [blame] | 23 | #define GUEST_EXEC_MAX_OUTPUT (16 * 1024 * 1024) |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 24 | /* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */ |
AlexChen | 0697e9e | 2020-10-26 17:05:38 +0800 | [diff] [blame] | 25 | #define GUEST_EXEC_IO_SIZE (4 * 1024) |
Philippe Mathieu-Daudé | 1329651f | 2020-04-14 15:30:44 +0200 | [diff] [blame] | 26 | /* |
| 27 | * Maximum file size to read - 48MB |
| 28 | * |
| 29 | * (48MB + Base64 3:4 overhead = JSON parser 64 MB limit) |
| 30 | */ |
| 31 | #define GUEST_FILE_READ_COUNT_MAX (48 * MiB) |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 32 | |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 33 | /* Note: in some situations, like with the fsfreeze, logging may be |
Markus Armbruster | c402335 | 2023-02-07 08:51:11 +0100 | [diff] [blame] | 34 | * temporarily disabled. if it is necessary that a command be able |
| 35 | * to log for accounting purposes, check ga_logging_enabled() beforehand. |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 36 | */ |
| 37 | void slog(const gchar *fmt, ...) |
| 38 | { |
| 39 | va_list ap; |
| 40 | |
| 41 | va_start(ap, fmt); |
| 42 | g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap); |
| 43 | va_end(ap); |
| 44 | } |
| 45 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 46 | int64_t qmp_guest_sync_delimited(int64_t id, Error **errp) |
| 47 | { |
| 48 | ga_set_response_delimited(ga_state); |
| 49 | return id; |
| 50 | } |
| 51 | |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 52 | int64_t qmp_guest_sync(int64_t id, Error **errp) |
| 53 | { |
| 54 | return id; |
| 55 | } |
| 56 | |
Markus Armbruster | 77dbc81 | 2014-05-02 13:26:30 +0200 | [diff] [blame] | 57 | void qmp_guest_ping(Error **errp) |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 58 | { |
| 59 | slog("guest-ping called"); |
| 60 | } |
| 61 | |
Marc-André Lureau | f0ccc00 | 2020-03-16 18:18:24 +0100 | [diff] [blame] | 62 | static void qmp_command_info(const QmpCommand *cmd, void *opaque) |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 63 | { |
| 64 | GuestAgentInfo *info = opaque; |
| 65 | GuestAgentCommandInfo *cmd_info; |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 66 | |
Markus Armbruster | f3a0640 | 2015-09-14 13:50:44 +0200 | [diff] [blame] | 67 | cmd_info = g_new0(GuestAgentCommandInfo, 1); |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 68 | cmd_info->name = g_strdup(qmp_command_name(cmd)); |
| 69 | cmd_info->enabled = qmp_command_is_enabled(cmd); |
Mark Wu | 0106dc4 | 2013-10-09 10:37:26 +0800 | [diff] [blame] | 70 | cmd_info->success_response = qmp_has_success_response(cmd); |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 71 | |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 72 | QAPI_LIST_PREPEND(info->supported_commands, cmd_info); |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 73 | } |
| 74 | |
Markus Armbruster | 77dbc81 | 2014-05-02 13:26:30 +0200 | [diff] [blame] | 75 | struct GuestAgentInfo *qmp_guest_info(Error **errp) |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 76 | { |
Markus Armbruster | f3a0640 | 2015-09-14 13:50:44 +0200 | [diff] [blame] | 77 | GuestAgentInfo *info = g_new0(GuestAgentInfo, 1); |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 78 | |
Michael Roth | 8efacc4 | 2012-05-14 09:33:48 -0500 | [diff] [blame] | 79 | info->version = g_strdup(QEMU_VERSION); |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 80 | qmp_for_each_command(&ga_commands, qmp_command_info, info); |
Michael Roth | 42074a9 | 2012-01-19 22:19:27 -0600 | [diff] [blame] | 81 | return info; |
| 82 | } |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 83 | |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 84 | struct GuestExecIOData { |
| 85 | guchar *data; |
| 86 | gsize size; |
| 87 | gsize length; |
Alex Bennée | a31393e | 2016-09-30 22:31:02 +0100 | [diff] [blame] | 88 | bool closed; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 89 | bool truncated; |
| 90 | const char *name; |
| 91 | }; |
| 92 | typedef struct GuestExecIOData GuestExecIOData; |
| 93 | |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 94 | struct GuestExecInfo { |
| 95 | GPid pid; |
| 96 | int64_t pid_numeric; |
| 97 | gint status; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 98 | bool has_output; |
Alex Bennée | a31393e | 2016-09-30 22:31:02 +0100 | [diff] [blame] | 99 | bool finished; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 100 | GuestExecIOData in; |
| 101 | GuestExecIOData out; |
| 102 | GuestExecIOData err; |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 103 | QTAILQ_ENTRY(GuestExecInfo) next; |
| 104 | }; |
| 105 | typedef struct GuestExecInfo GuestExecInfo; |
| 106 | |
| 107 | static struct { |
| 108 | QTAILQ_HEAD(, GuestExecInfo) processes; |
| 109 | } guest_exec_state = { |
| 110 | .processes = QTAILQ_HEAD_INITIALIZER(guest_exec_state.processes), |
| 111 | }; |
| 112 | |
| 113 | static int64_t gpid_to_int64(GPid pid) |
| 114 | { |
| 115 | #ifdef G_OS_WIN32 |
| 116 | return GetProcessId(pid); |
| 117 | #else |
| 118 | return (int64_t)pid; |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | static GuestExecInfo *guest_exec_info_add(GPid pid) |
| 123 | { |
| 124 | GuestExecInfo *gei; |
| 125 | |
| 126 | gei = g_new0(GuestExecInfo, 1); |
| 127 | gei->pid = pid; |
| 128 | gei->pid_numeric = gpid_to_int64(pid); |
| 129 | QTAILQ_INSERT_TAIL(&guest_exec_state.processes, gei, next); |
| 130 | |
| 131 | return gei; |
| 132 | } |
| 133 | |
| 134 | static GuestExecInfo *guest_exec_info_find(int64_t pid_numeric) |
| 135 | { |
| 136 | GuestExecInfo *gei; |
| 137 | |
| 138 | QTAILQ_FOREACH(gei, &guest_exec_state.processes, next) { |
| 139 | if (gei->pid_numeric == pid_numeric) { |
| 140 | return gei; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Vladimir Sementsov-Ogievskiy | b90abba | 2019-12-05 20:46:26 +0300 | [diff] [blame] | 147 | GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp) |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 148 | { |
| 149 | GuestExecInfo *gei; |
| 150 | GuestExecStatus *ges; |
| 151 | |
| 152 | slog("guest-exec-status called, pid: %u", (uint32_t)pid); |
| 153 | |
| 154 | gei = guest_exec_info_find(pid); |
| 155 | if (gei == NULL) { |
Markus Armbruster | b665165 | 2023-10-31 12:10:55 +0100 | [diff] [blame] | 156 | error_setg(errp, "PID " PRId64 " does not exist"); |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | ges = g_new0(GuestExecStatus, 1); |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 161 | |
Marc-André Lureau | c267d75 | 2022-04-20 17:26:22 +0400 | [diff] [blame] | 162 | bool finished = gei->finished; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 163 | |
| 164 | /* need to wait till output channels are closed |
| 165 | * to be sure we captured all output at this point */ |
| 166 | if (gei->has_output) { |
Marc-André Lureau | c267d75 | 2022-04-20 17:26:22 +0400 | [diff] [blame] | 167 | finished &= gei->out.closed && gei->err.closed; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ges->exited = finished; |
| 171 | if (finished) { |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 172 | /* Glib has no portable way to parse exit status. |
| 173 | * On UNIX, we can get either exit code from normal termination |
| 174 | * or signal number. |
| 175 | * On Windows, it is either the same exit code or the exception |
| 176 | * value for an unhandled exception that caused the process |
| 177 | * to terminate. |
| 178 | * See MSDN for GetExitCodeProcess() and ntstatus.h for possible |
| 179 | * well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV |
| 180 | * References: |
| 181 | * https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx |
| 182 | * https://msdn.microsoft.com/en-us/library/aa260331(v=vs.60).aspx |
| 183 | */ |
| 184 | #ifdef G_OS_WIN32 |
| 185 | /* Additionally WIN32 does not provide any additional information |
Stefan Weil | cb8d4c8 | 2016-03-23 15:59:57 +0100 | [diff] [blame] | 186 | * on whether the child exited or terminated via signal. |
| 187 | * We use this simple range check to distinguish application exit code |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 188 | * (usually value less then 256) and unhandled exception code with |
| 189 | * ntstatus (always value greater then 0xC0000005). */ |
| 190 | if ((uint32_t)gei->status < 0xC0000000U) { |
| 191 | ges->has_exitcode = true; |
| 192 | ges->exitcode = gei->status; |
| 193 | } else { |
| 194 | ges->has_signal = true; |
| 195 | ges->signal = gei->status; |
| 196 | } |
| 197 | #else |
| 198 | if (WIFEXITED(gei->status)) { |
| 199 | ges->has_exitcode = true; |
| 200 | ges->exitcode = WEXITSTATUS(gei->status); |
| 201 | } else if (WIFSIGNALED(gei->status)) { |
| 202 | ges->has_signal = true; |
| 203 | ges->signal = WTERMSIG(gei->status); |
| 204 | } |
| 205 | #endif |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 206 | if (gei->out.length > 0) { |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 207 | ges->out_data = g_base64_encode(gei->out.data, gei->out.length); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 208 | ges->has_out_truncated = gei->out.truncated; |
| 209 | } |
Daniel Xu | d6f67b8 | 2023-10-01 12:38:25 -0600 | [diff] [blame] | 210 | g_free(gei->out.data); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 211 | |
| 212 | if (gei->err.length > 0) { |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 213 | ges->err_data = g_base64_encode(gei->err.data, gei->err.length); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 214 | ges->has_err_truncated = gei->err.truncated; |
| 215 | } |
Daniel Xu | d6f67b8 | 2023-10-01 12:38:25 -0600 | [diff] [blame] | 216 | g_free(gei->err.data); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 217 | |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 218 | QTAILQ_REMOVE(&guest_exec_state.processes, gei, next); |
| 219 | g_free(gei); |
| 220 | } |
| 221 | |
| 222 | return ges; |
| 223 | } |
| 224 | |
| 225 | /* Get environment variables or arguments array for execve(). */ |
| 226 | static char **guest_exec_get_args(const strList *entry, bool log) |
| 227 | { |
| 228 | const strList *it; |
| 229 | int count = 1, i = 0; /* reserve for NULL terminator */ |
| 230 | char **args; |
| 231 | char *str; /* for logging array of arguments */ |
| 232 | size_t str_size = 1; |
| 233 | |
| 234 | for (it = entry; it != NULL; it = it->next) { |
| 235 | count++; |
| 236 | str_size += 1 + strlen(it->value); |
| 237 | } |
| 238 | |
| 239 | str = g_malloc(str_size); |
| 240 | *str = 0; |
Markus Armbruster | b21e238 | 2022-03-15 15:41:56 +0100 | [diff] [blame] | 241 | args = g_new(char *, count); |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 242 | for (it = entry; it != NULL; it = it->next) { |
| 243 | args[i++] = it->value; |
| 244 | pstrcat(str, str_size, it->value); |
| 245 | if (it->next) { |
| 246 | pstrcat(str, str_size, " "); |
| 247 | } |
| 248 | } |
| 249 | args[i] = NULL; |
| 250 | |
| 251 | if (log) { |
| 252 | slog("guest-exec called: \"%s\"", str); |
| 253 | } |
| 254 | g_free(str); |
| 255 | |
| 256 | return args; |
| 257 | } |
| 258 | |
| 259 | static void guest_exec_child_watch(GPid pid, gint status, gpointer data) |
| 260 | { |
| 261 | GuestExecInfo *gei = (GuestExecInfo *)data; |
| 262 | |
| 263 | g_debug("guest_exec_child_watch called, pid: %d, status: %u", |
| 264 | (int32_t)gpid_to_int64(pid), (uint32_t)status); |
| 265 | |
| 266 | gei->status = status; |
Marc-André Lureau | c267d75 | 2022-04-20 17:26:22 +0400 | [diff] [blame] | 267 | gei->finished = true; |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 268 | |
| 269 | g_spawn_close_pid(pid); |
| 270 | } |
| 271 | |
Denis V. Lunev | 4005b47 | 2015-10-13 18:41:21 +0300 | [diff] [blame] | 272 | static void guest_exec_task_setup(gpointer data) |
| 273 | { |
| 274 | #if !defined(G_OS_WIN32) |
Daniel Xu | 810f677 | 2023-03-22 18:19:27 -0600 | [diff] [blame] | 275 | bool has_merge = *(bool *)data; |
Denis V. Lunev | 4005b47 | 2015-10-13 18:41:21 +0300 | [diff] [blame] | 276 | struct sigaction sigact; |
| 277 | |
Daniel Xu | 810f677 | 2023-03-22 18:19:27 -0600 | [diff] [blame] | 278 | if (has_merge) { |
| 279 | /* |
| 280 | * FIXME: When `GLIB_VERSION_MIN_REQUIRED` is bumped to 2.58+, use |
| 281 | * g_spawn_async_with_fds() to be portable on windows. The current |
| 282 | * logic does not work on windows b/c `GSpawnChildSetupFunc` is run |
| 283 | * inside the parent, not the child. |
| 284 | */ |
| 285 | if (dup2(STDOUT_FILENO, STDERR_FILENO) != 0) { |
| 286 | slog("dup2() failed to merge stderr into stdout: %s", |
| 287 | strerror(errno)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* Reset ignored signals back to default. */ |
Denis V. Lunev | 4005b47 | 2015-10-13 18:41:21 +0300 | [diff] [blame] | 292 | memset(&sigact, 0, sizeof(struct sigaction)); |
| 293 | sigact.sa_handler = SIG_DFL; |
| 294 | |
| 295 | if (sigaction(SIGPIPE, &sigact, NULL) != 0) { |
| 296 | slog("sigaction() failed to reset child process's SIGPIPE: %s", |
| 297 | strerror(errno)); |
| 298 | } |
| 299 | #endif |
| 300 | } |
| 301 | |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 302 | static gboolean guest_exec_input_watch(GIOChannel *ch, |
| 303 | GIOCondition cond, gpointer p_) |
| 304 | { |
| 305 | GuestExecIOData *p = (GuestExecIOData *)p_; |
| 306 | gsize bytes_written = 0; |
| 307 | GIOStatus status; |
| 308 | GError *gerr = NULL; |
| 309 | |
| 310 | /* nothing left to write */ |
| 311 | if (p->size == p->length) { |
| 312 | goto done; |
| 313 | } |
| 314 | |
| 315 | status = g_io_channel_write_chars(ch, (gchar *)p->data + p->length, |
| 316 | p->size - p->length, &bytes_written, &gerr); |
| 317 | |
| 318 | /* can be not 0 even if not G_IO_STATUS_NORMAL */ |
| 319 | if (bytes_written != 0) { |
| 320 | p->length += bytes_written; |
| 321 | } |
| 322 | |
| 323 | /* continue write, our callback will be called again */ |
| 324 | if (status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_AGAIN) { |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | if (gerr) { |
| 329 | g_warning("qga: i/o error writing to input_data channel: %s", |
| 330 | gerr->message); |
| 331 | g_error_free(gerr); |
| 332 | } |
| 333 | |
| 334 | done: |
| 335 | g_io_channel_shutdown(ch, true, NULL); |
| 336 | g_io_channel_unref(ch); |
Marc-André Lureau | c267d75 | 2022-04-20 17:26:22 +0400 | [diff] [blame] | 337 | p->closed = true; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 338 | g_free(p->data); |
| 339 | |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | static gboolean guest_exec_output_watch(GIOChannel *ch, |
| 344 | GIOCondition cond, gpointer p_) |
| 345 | { |
| 346 | GuestExecIOData *p = (GuestExecIOData *)p_; |
| 347 | gsize bytes_read; |
| 348 | GIOStatus gstatus; |
| 349 | |
| 350 | if (cond == G_IO_HUP || cond == G_IO_ERR) { |
| 351 | goto close; |
| 352 | } |
| 353 | |
| 354 | if (p->size == p->length) { |
| 355 | gpointer t = NULL; |
| 356 | if (!p->truncated && p->size < GUEST_EXEC_MAX_OUTPUT) { |
| 357 | t = g_try_realloc(p->data, p->size + GUEST_EXEC_IO_SIZE); |
| 358 | } |
| 359 | if (t == NULL) { |
| 360 | /* ignore truncated output */ |
| 361 | gchar buf[GUEST_EXEC_IO_SIZE]; |
| 362 | |
| 363 | p->truncated = true; |
| 364 | gstatus = g_io_channel_read_chars(ch, buf, sizeof(buf), |
| 365 | &bytes_read, NULL); |
| 366 | if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) { |
| 367 | goto close; |
| 368 | } |
| 369 | |
| 370 | return true; |
| 371 | } |
| 372 | p->size += GUEST_EXEC_IO_SIZE; |
| 373 | p->data = t; |
| 374 | } |
| 375 | |
| 376 | /* Calling read API once. |
| 377 | * On next available data our callback will be called again */ |
| 378 | gstatus = g_io_channel_read_chars(ch, (gchar *)p->data + p->length, |
| 379 | p->size - p->length, &bytes_read, NULL); |
| 380 | if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) { |
| 381 | goto close; |
| 382 | } |
| 383 | |
| 384 | p->length += bytes_read; |
| 385 | |
| 386 | return true; |
| 387 | |
| 388 | close: |
Yuriy Pudgorodskiy | 3005c2c | 2016-04-06 08:43:30 +0300 | [diff] [blame] | 389 | g_io_channel_shutdown(ch, true, NULL); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 390 | g_io_channel_unref(ch); |
Marc-André Lureau | c267d75 | 2022-04-20 17:26:22 +0400 | [diff] [blame] | 391 | p->closed = true; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 395 | static GuestExecCaptureOutputMode ga_parse_capture_output( |
| 396 | GuestExecCaptureOutput *capture_output) |
| 397 | { |
| 398 | if (!capture_output) |
| 399 | return GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE; |
| 400 | else if (capture_output->type == QTYPE_QBOOL) |
| 401 | return capture_output->u.flag ? GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED |
| 402 | : GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE; |
| 403 | else |
| 404 | return capture_output->u.mode; |
| 405 | } |
| 406 | |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 407 | GuestExec *qmp_guest_exec(const char *path, |
| 408 | bool has_arg, strList *arg, |
| 409 | bool has_env, strList *env, |
Markus Armbruster | 91eab32 | 2022-11-04 17:07:11 +0100 | [diff] [blame] | 410 | const char *input_data, |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 411 | GuestExecCaptureOutput *capture_output, |
Vladimir Sementsov-Ogievskiy | b90abba | 2019-12-05 20:46:26 +0300 | [diff] [blame] | 412 | Error **errp) |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 413 | { |
| 414 | GPid pid; |
| 415 | GuestExec *ge = NULL; |
| 416 | GuestExecInfo *gei; |
| 417 | char **argv, **envp; |
| 418 | strList arglist; |
| 419 | gboolean ret; |
| 420 | GError *gerr = NULL; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 421 | gint in_fd, out_fd, err_fd; |
| 422 | GIOChannel *in_ch, *out_ch, *err_ch; |
| 423 | GSpawnFlags flags; |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 424 | bool has_output = false; |
Daniel Xu | 810f677 | 2023-03-22 18:19:27 -0600 | [diff] [blame] | 425 | bool has_merge = false; |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 426 | GuestExecCaptureOutputMode output_mode; |
Daniel P. Berrangé | 057489d | 2021-08-09 14:10:29 +0100 | [diff] [blame] | 427 | g_autofree uint8_t *input = NULL; |
Daniel P. Berrange | 920639c | 2015-11-23 15:37:07 +0000 | [diff] [blame] | 428 | size_t ninput = 0; |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 429 | |
| 430 | arglist.value = (char *)path; |
| 431 | arglist.next = has_arg ? arg : NULL; |
| 432 | |
Markus Armbruster | 91eab32 | 2022-11-04 17:07:11 +0100 | [diff] [blame] | 433 | if (input_data) { |
Vladimir Sementsov-Ogievskiy | b90abba | 2019-12-05 20:46:26 +0300 | [diff] [blame] | 434 | input = qbase64_decode(input_data, -1, &ninput, errp); |
Daniel P. Berrange | 920639c | 2015-11-23 15:37:07 +0000 | [diff] [blame] | 435 | if (!input) { |
| 436 | return NULL; |
| 437 | } |
| 438 | } |
| 439 | |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 440 | argv = guest_exec_get_args(&arglist, true); |
Yuri Pudgorodskiy | 02a4d82 | 2015-11-12 16:36:20 +0300 | [diff] [blame] | 441 | envp = has_env ? guest_exec_get_args(env, false) : NULL; |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 442 | |
Daniel P. Berrangé | e7b3af8 | 2018-05-04 15:34:46 +0100 | [diff] [blame] | 443 | flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD | |
| 444 | G_SPAWN_SEARCH_PATH_FROM_ENVP; |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 445 | |
| 446 | output_mode = ga_parse_capture_output(capture_output); |
| 447 | switch (output_mode) { |
| 448 | case GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE: |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 449 | flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL; |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 450 | break; |
| 451 | case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDOUT: |
| 452 | has_output = true; |
| 453 | flags |= G_SPAWN_STDERR_TO_DEV_NULL; |
| 454 | break; |
| 455 | case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDERR: |
| 456 | has_output = true; |
| 457 | flags |= G_SPAWN_STDOUT_TO_DEV_NULL; |
| 458 | break; |
| 459 | case GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED: |
| 460 | has_output = true; |
| 461 | break; |
Daniel Xu | 810f677 | 2023-03-22 18:19:27 -0600 | [diff] [blame] | 462 | #if !defined(G_OS_WIN32) |
| 463 | case GUEST_EXEC_CAPTURE_OUTPUT_MODE_MERGED: |
| 464 | has_output = true; |
| 465 | has_merge = true; |
| 466 | break; |
| 467 | #endif |
Daniel Xu | 9c5ccc5 | 2023-03-22 18:19:26 -0600 | [diff] [blame] | 468 | case GUEST_EXEC_CAPTURE_OUTPUT_MODE__MAX: |
| 469 | /* Silence warning; impossible branch */ |
| 470 | break; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | ret = g_spawn_async_with_pipes(NULL, argv, envp, flags, |
Daniel Xu | 810f677 | 2023-03-22 18:19:27 -0600 | [diff] [blame] | 474 | guest_exec_task_setup, &has_merge, &pid, input_data ? &in_fd : NULL, |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 475 | has_output ? &out_fd : NULL, has_output ? &err_fd : NULL, &gerr); |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 476 | if (!ret) { |
Markus Armbruster | cec07c7 | 2024-05-14 12:58:27 +0200 | [diff] [blame] | 477 | error_setg(errp, "%s", gerr->message); |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 478 | g_error_free(gerr); |
| 479 | goto done; |
| 480 | } |
| 481 | |
| 482 | ge = g_new0(GuestExec, 1); |
| 483 | ge->pid = gpid_to_int64(pid); |
| 484 | |
| 485 | gei = guest_exec_info_add(pid); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 486 | gei->has_output = has_output; |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 487 | g_child_watch_add(pid, guest_exec_child_watch, gei); |
| 488 | |
Markus Armbruster | 91eab32 | 2022-11-04 17:07:11 +0100 | [diff] [blame] | 489 | if (input_data) { |
Daniel P. Berrangé | 057489d | 2021-08-09 14:10:29 +0100 | [diff] [blame] | 490 | gei->in.data = g_steal_pointer(&input); |
Daniel P. Berrange | 920639c | 2015-11-23 15:37:07 +0000 | [diff] [blame] | 491 | gei->in.size = ninput; |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 492 | #ifdef G_OS_WIN32 |
| 493 | in_ch = g_io_channel_win32_new_fd(in_fd); |
| 494 | #else |
| 495 | in_ch = g_io_channel_unix_new(in_fd); |
| 496 | #endif |
| 497 | g_io_channel_set_encoding(in_ch, NULL, NULL); |
| 498 | g_io_channel_set_buffered(in_ch, false); |
| 499 | g_io_channel_set_flags(in_ch, G_IO_FLAG_NONBLOCK, NULL); |
Yuriy Pudgorodskiy | 3005c2c | 2016-04-06 08:43:30 +0300 | [diff] [blame] | 500 | g_io_channel_set_close_on_unref(in_ch, true); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 501 | g_io_add_watch(in_ch, G_IO_OUT, guest_exec_input_watch, &gei->in); |
| 502 | } |
| 503 | |
| 504 | if (has_output) { |
| 505 | #ifdef G_OS_WIN32 |
| 506 | out_ch = g_io_channel_win32_new_fd(out_fd); |
| 507 | err_ch = g_io_channel_win32_new_fd(err_fd); |
| 508 | #else |
| 509 | out_ch = g_io_channel_unix_new(out_fd); |
| 510 | err_ch = g_io_channel_unix_new(err_fd); |
| 511 | #endif |
| 512 | g_io_channel_set_encoding(out_ch, NULL, NULL); |
| 513 | g_io_channel_set_encoding(err_ch, NULL, NULL); |
| 514 | g_io_channel_set_buffered(out_ch, false); |
| 515 | g_io_channel_set_buffered(err_ch, false); |
Yuriy Pudgorodskiy | 3005c2c | 2016-04-06 08:43:30 +0300 | [diff] [blame] | 516 | g_io_channel_set_close_on_unref(out_ch, true); |
| 517 | g_io_channel_set_close_on_unref(err_ch, true); |
Yuri Pudgorodskiy | a1853dc | 2015-10-13 18:41:23 +0300 | [diff] [blame] | 518 | g_io_add_watch(out_ch, G_IO_IN | G_IO_HUP, |
| 519 | guest_exec_output_watch, &gei->out); |
| 520 | g_io_add_watch(err_ch, G_IO_IN | G_IO_HUP, |
| 521 | guest_exec_output_watch, &gei->err); |
| 522 | } |
| 523 | |
Yuri Pudgorodskiy | d697e30 | 2015-10-13 18:41:20 +0300 | [diff] [blame] | 524 | done: |
| 525 | g_free(argv); |
| 526 | g_free(envp); |
| 527 | |
| 528 | return ge; |
| 529 | } |
Eric Blake | 0b4b493 | 2016-02-09 14:27:16 -0700 | [diff] [blame] | 530 | |
| 531 | /* Convert GuestFileWhence (either a raw integer or an enum value) into |
| 532 | * the guest's SEEK_ constants. */ |
| 533 | int ga_parse_whence(GuestFileWhence *whence, Error **errp) |
| 534 | { |
Eric Blake | a23f38a | 2020-03-20 10:05:07 -0500 | [diff] [blame] | 535 | /* |
| 536 | * Exploit the fact that we picked values to match QGA_SEEK_*; |
| 537 | * however, we have to use a temporary variable since the union |
| 538 | * members may have different size. |
| 539 | */ |
Eric Blake | 0b4b493 | 2016-02-09 14:27:16 -0700 | [diff] [blame] | 540 | if (whence->type == QTYPE_QSTRING) { |
Eric Blake | a23f38a | 2020-03-20 10:05:07 -0500 | [diff] [blame] | 541 | int value = whence->u.name; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 542 | whence->type = QTYPE_QNUM; |
Eric Blake | a23f38a | 2020-03-20 10:05:07 -0500 | [diff] [blame] | 543 | whence->u.value = value; |
Eric Blake | 0b4b493 | 2016-02-09 14:27:16 -0700 | [diff] [blame] | 544 | } |
| 545 | switch (whence->u.value) { |
| 546 | case QGA_SEEK_SET: |
| 547 | return SEEK_SET; |
| 548 | case QGA_SEEK_CUR: |
| 549 | return SEEK_CUR; |
| 550 | case QGA_SEEK_END: |
| 551 | return SEEK_END; |
| 552 | } |
| 553 | error_setg(errp, "invalid whence code %"PRId64, whence->u.value); |
| 554 | return -1; |
| 555 | } |
Vinzenz Feenstra | 0a3d197 | 2017-04-04 08:46:31 +0200 | [diff] [blame] | 556 | |
Vladimir Sementsov-Ogievskiy | b90abba | 2019-12-05 20:46:26 +0300 | [diff] [blame] | 557 | GuestHostName *qmp_guest_get_host_name(Error **errp) |
Vinzenz Feenstra | 0a3d197 | 2017-04-04 08:46:31 +0200 | [diff] [blame] | 558 | { |
| 559 | GuestHostName *result = NULL; |
Marc-André Lureau | 548fb0d | 2022-04-20 17:26:14 +0400 | [diff] [blame] | 560 | g_autofree char *hostname = qga_get_host_name(errp); |
Michal Privoznik | 0d3a8f3 | 2020-06-22 20:19:36 +0200 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * We want to avoid using g_get_host_name() because that |
| 564 | * caches the result and we wouldn't reflect changes in the |
| 565 | * host name. |
| 566 | */ |
| 567 | |
| 568 | if (!hostname) { |
| 569 | hostname = g_strdup("localhost"); |
Vinzenz Feenstra | 0a3d197 | 2017-04-04 08:46:31 +0200 | [diff] [blame] | 570 | } |
Michal Privoznik | 0d3a8f3 | 2020-06-22 20:19:36 +0200 | [diff] [blame] | 571 | |
| 572 | result = g_new0(GuestHostName, 1); |
| 573 | result->host_name = g_steal_pointer(&hostname); |
Vinzenz Feenstra | 0a3d197 | 2017-04-04 08:46:31 +0200 | [diff] [blame] | 574 | return result; |
| 575 | } |
Vinzenz Feenstra | 53c58e6 | 2017-04-19 12:52:58 +0200 | [diff] [blame] | 576 | |
| 577 | GuestTimezone *qmp_guest_get_timezone(Error **errp) |
| 578 | { |
Vinzenz Feenstra | 53c58e6 | 2017-04-19 12:52:58 +0200 | [diff] [blame] | 579 | GuestTimezone *info = NULL; |
| 580 | GTimeZone *tz = NULL; |
| 581 | gint64 now = 0; |
| 582 | gint32 intv = 0; |
| 583 | gchar const *name = NULL; |
| 584 | |
| 585 | info = g_new0(GuestTimezone, 1); |
| 586 | tz = g_time_zone_new_local(); |
| 587 | if (tz == NULL) { |
Markus Armbruster | cec07c7 | 2024-05-14 12:58:27 +0200 | [diff] [blame] | 588 | error_setg(errp, "Couldn't retrieve local timezone"); |
Vinzenz Feenstra | 53c58e6 | 2017-04-19 12:52:58 +0200 | [diff] [blame] | 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | now = g_get_real_time() / G_USEC_PER_SEC; |
| 593 | intv = g_time_zone_find_interval(tz, G_TIME_TYPE_UNIVERSAL, now); |
| 594 | info->offset = g_time_zone_get_offset(tz, intv); |
| 595 | name = g_time_zone_get_abbreviation(tz, intv); |
| 596 | if (name != NULL) { |
Vinzenz Feenstra | 53c58e6 | 2017-04-19 12:52:58 +0200 | [diff] [blame] | 597 | info->zone = g_strdup(name); |
| 598 | } |
| 599 | g_time_zone_unref(tz); |
| 600 | |
| 601 | return info; |
| 602 | |
| 603 | error: |
| 604 | g_free(info); |
| 605 | return NULL; |
Vinzenz Feenstra | 53c58e6 | 2017-04-19 12:52:58 +0200 | [diff] [blame] | 606 | } |
Philippe Mathieu-Daudé | ead83a1 | 2020-04-14 15:30:43 +0200 | [diff] [blame] | 607 | |
| 608 | GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count, |
| 609 | int64_t count, Error **errp) |
| 610 | { |
| 611 | GuestFileHandle *gfh = guest_file_handle_find(handle, errp); |
| 612 | GuestFileRead *read_data; |
| 613 | |
| 614 | if (!gfh) { |
| 615 | return NULL; |
| 616 | } |
| 617 | if (!has_count) { |
| 618 | count = QGA_READ_COUNT_DEFAULT; |
Philippe Mathieu-Daudé | 1329651f | 2020-04-14 15:30:44 +0200 | [diff] [blame] | 619 | } else if (count < 0 || count > GUEST_FILE_READ_COUNT_MAX) { |
Philippe Mathieu-Daudé | ead83a1 | 2020-04-14 15:30:43 +0200 | [diff] [blame] | 620 | error_setg(errp, "value '%" PRId64 "' is invalid for argument count", |
| 621 | count); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | read_data = guest_file_read_unsafe(gfh, count, errp); |
| 626 | if (!read_data) { |
| 627 | slog("guest-file-write failed, handle: %" PRId64, handle); |
| 628 | } |
| 629 | |
| 630 | return read_data; |
| 631 | } |
Marc-André Lureau | 287698e | 2022-03-07 11:03:59 +0400 | [diff] [blame] | 632 | |
| 633 | int64_t qmp_guest_get_time(Error **errp) |
| 634 | { |
| 635 | return g_get_real_time() * 1000; |
| 636 | } |