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