blob: 88c1c99fe51b55608abd82ce8686fe4ab4e27f8f [file] [log] [blame]
Michael Roth42074a92012-01-19 22:19:27 -06001/*
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 Maydell4459bf32016-01-29 17:49:58 +000013#include "qemu/osdep.h"
Philippe Mathieu-Daudé1329651f2020-04-14 15:30:44 +020014#include "qemu/units.h"
Michael S. Tsirkindc032722018-05-03 22:50:57 +030015#include "guest-agent-core.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010016#include "qga-qapi-commands.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010017#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/qerror.h"
Daniel P. Berrange920639c2015-11-23 15:37:07 +000019#include "qemu/base64.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020020#include "qemu/cutils.h"
Philippe Mathieu-Daudéead83a12020-04-14 15:30:43 +020021#include "commands-common.h"
Michael Roth42074a92012-01-19 22:19:27 -060022
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +030023/* Maximum captured guest-exec out_data/err_data - 16MB */
AlexChen0697e9e2020-10-26 17:05:38 +080024#define GUEST_EXEC_MAX_OUTPUT (16 * 1024 * 1024)
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +030025/* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */
AlexChen0697e9e2020-10-26 17:05:38 +080026#define GUEST_EXEC_IO_SIZE (4 * 1024)
Philippe Mathieu-Daudé1329651f2020-04-14 15:30:44 +020027/*
28 * Maximum file size to read - 48MB
29 *
30 * (48MB + Base64 3:4 overhead = JSON parser 64 MB limit)
31 */
32#define GUEST_FILE_READ_COUNT_MAX (48 * MiB)
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +030033
Michael Roth42074a92012-01-19 22:19:27 -060034/* Note: in some situations, like with the fsfreeze, logging may be
Markus Armbrusterc4023352023-02-07 08:51:11 +010035 * temporarily disabled. if it is necessary that a command be able
36 * to log for accounting purposes, check ga_logging_enabled() beforehand.
Michael Roth42074a92012-01-19 22:19:27 -060037 */
38void slog(const gchar *fmt, ...)
39{
40 va_list ap;
41
42 va_start(ap, fmt);
43 g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
44 va_end(ap);
45}
46
Michael Roth3cf0bed2012-02-07 13:56:48 -060047int64_t qmp_guest_sync_delimited(int64_t id, Error **errp)
48{
49 ga_set_response_delimited(ga_state);
50 return id;
51}
52
Michael Roth42074a92012-01-19 22:19:27 -060053int64_t qmp_guest_sync(int64_t id, Error **errp)
54{
55 return id;
56}
57
Markus Armbruster77dbc812014-05-02 13:26:30 +020058void qmp_guest_ping(Error **errp)
Michael Roth42074a92012-01-19 22:19:27 -060059{
60 slog("guest-ping called");
61}
62
Marc-André Lureauf0ccc002020-03-16 18:18:24 +010063static void qmp_command_info(const QmpCommand *cmd, void *opaque)
Mark Wu8dc4d912013-10-09 11:25:07 +080064{
65 GuestAgentInfo *info = opaque;
66 GuestAgentCommandInfo *cmd_info;
Mark Wu8dc4d912013-10-09 11:25:07 +080067
Markus Armbrusterf3a06402015-09-14 13:50:44 +020068 cmd_info = g_new0(GuestAgentCommandInfo, 1);
Mark Wu8dc4d912013-10-09 11:25:07 +080069 cmd_info->name = g_strdup(qmp_command_name(cmd));
70 cmd_info->enabled = qmp_command_is_enabled(cmd);
Mark Wu0106dc42013-10-09 10:37:26 +080071 cmd_info->success_response = qmp_has_success_response(cmd);
Mark Wu8dc4d912013-10-09 11:25:07 +080072
Eric Blake54aa3de2020-11-12 19:13:37 -060073 QAPI_LIST_PREPEND(info->supported_commands, cmd_info);
Mark Wu8dc4d912013-10-09 11:25:07 +080074}
75
Markus Armbruster77dbc812014-05-02 13:26:30 +020076struct GuestAgentInfo *qmp_guest_info(Error **errp)
Michael Roth42074a92012-01-19 22:19:27 -060077{
Markus Armbrusterf3a06402015-09-14 13:50:44 +020078 GuestAgentInfo *info = g_new0(GuestAgentInfo, 1);
Michael Roth42074a92012-01-19 22:19:27 -060079
Michael Roth8efacc42012-05-14 09:33:48 -050080 info->version = g_strdup(QEMU_VERSION);
Markus Armbruster1527bad2017-03-03 13:32:25 +010081 qmp_for_each_command(&ga_commands, qmp_command_info, info);
Michael Roth42074a92012-01-19 22:19:27 -060082 return info;
83}
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +030084
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +030085struct GuestExecIOData {
86 guchar *data;
87 gsize size;
88 gsize length;
Alex Bennéea31393e2016-09-30 22:31:02 +010089 bool closed;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +030090 bool truncated;
91 const char *name;
92};
93typedef struct GuestExecIOData GuestExecIOData;
94
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +030095struct GuestExecInfo {
96 GPid pid;
97 int64_t pid_numeric;
98 gint status;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +030099 bool has_output;
Alex Bennéea31393e2016-09-30 22:31:02 +0100100 bool finished;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300101 GuestExecIOData in;
102 GuestExecIOData out;
103 GuestExecIOData err;
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300104 QTAILQ_ENTRY(GuestExecInfo) next;
105};
106typedef struct GuestExecInfo GuestExecInfo;
107
108static struct {
109 QTAILQ_HEAD(, GuestExecInfo) processes;
110} guest_exec_state = {
111 .processes = QTAILQ_HEAD_INITIALIZER(guest_exec_state.processes),
112};
113
114static int64_t gpid_to_int64(GPid pid)
115{
116#ifdef G_OS_WIN32
117 return GetProcessId(pid);
118#else
119 return (int64_t)pid;
120#endif
121}
122
123static GuestExecInfo *guest_exec_info_add(GPid pid)
124{
125 GuestExecInfo *gei;
126
127 gei = g_new0(GuestExecInfo, 1);
128 gei->pid = pid;
129 gei->pid_numeric = gpid_to_int64(pid);
130 QTAILQ_INSERT_TAIL(&guest_exec_state.processes, gei, next);
131
132 return gei;
133}
134
135static GuestExecInfo *guest_exec_info_find(int64_t pid_numeric)
136{
137 GuestExecInfo *gei;
138
139 QTAILQ_FOREACH(gei, &guest_exec_state.processes, next) {
140 if (gei->pid_numeric == pid_numeric) {
141 return gei;
142 }
143 }
144
145 return NULL;
146}
147
Vladimir Sementsov-Ogievskiyb90abba2019-12-05 20:46:26 +0300148GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp)
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300149{
150 GuestExecInfo *gei;
151 GuestExecStatus *ges;
152
153 slog("guest-exec-status called, pid: %u", (uint32_t)pid);
154
155 gei = guest_exec_info_find(pid);
156 if (gei == NULL) {
Markus Armbrusterb6651652023-10-31 12:10:55 +0100157 error_setg(errp, "PID " PRId64 " does not exist");
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300158 return NULL;
159 }
160
161 ges = g_new0(GuestExecStatus, 1);
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300162
Marc-André Lureauc267d752022-04-20 17:26:22 +0400163 bool finished = gei->finished;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300164
165 /* need to wait till output channels are closed
166 * to be sure we captured all output at this point */
167 if (gei->has_output) {
Marc-André Lureauc267d752022-04-20 17:26:22 +0400168 finished &= gei->out.closed && gei->err.closed;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300169 }
170
171 ges->exited = finished;
172 if (finished) {
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300173 /* Glib has no portable way to parse exit status.
174 * On UNIX, we can get either exit code from normal termination
175 * or signal number.
176 * On Windows, it is either the same exit code or the exception
177 * value for an unhandled exception that caused the process
178 * to terminate.
179 * See MSDN for GetExitCodeProcess() and ntstatus.h for possible
180 * well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV
181 * References:
182 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
183 * https://msdn.microsoft.com/en-us/library/aa260331(v=vs.60).aspx
184 */
185#ifdef G_OS_WIN32
186 /* Additionally WIN32 does not provide any additional information
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100187 * on whether the child exited or terminated via signal.
188 * We use this simple range check to distinguish application exit code
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300189 * (usually value less then 256) and unhandled exception code with
190 * ntstatus (always value greater then 0xC0000005). */
191 if ((uint32_t)gei->status < 0xC0000000U) {
192 ges->has_exitcode = true;
193 ges->exitcode = gei->status;
194 } else {
195 ges->has_signal = true;
196 ges->signal = gei->status;
197 }
198#else
199 if (WIFEXITED(gei->status)) {
200 ges->has_exitcode = true;
201 ges->exitcode = WEXITSTATUS(gei->status);
202 } else if (WIFSIGNALED(gei->status)) {
203 ges->has_signal = true;
204 ges->signal = WTERMSIG(gei->status);
205 }
206#endif
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300207 if (gei->out.length > 0) {
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300208 ges->out_data = g_base64_encode(gei->out.data, gei->out.length);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300209 ges->has_out_truncated = gei->out.truncated;
210 }
Daniel Xud6f67b82023-10-01 12:38:25 -0600211 g_free(gei->out.data);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300212
213 if (gei->err.length > 0) {
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300214 ges->err_data = g_base64_encode(gei->err.data, gei->err.length);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300215 ges->has_err_truncated = gei->err.truncated;
216 }
Daniel Xud6f67b82023-10-01 12:38:25 -0600217 g_free(gei->err.data);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300218
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300219 QTAILQ_REMOVE(&guest_exec_state.processes, gei, next);
220 g_free(gei);
221 }
222
223 return ges;
224}
225
226/* Get environment variables or arguments array for execve(). */
227static char **guest_exec_get_args(const strList *entry, bool log)
228{
229 const strList *it;
230 int count = 1, i = 0; /* reserve for NULL terminator */
231 char **args;
232 char *str; /* for logging array of arguments */
233 size_t str_size = 1;
234
235 for (it = entry; it != NULL; it = it->next) {
236 count++;
237 str_size += 1 + strlen(it->value);
238 }
239
240 str = g_malloc(str_size);
241 *str = 0;
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100242 args = g_new(char *, count);
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300243 for (it = entry; it != NULL; it = it->next) {
244 args[i++] = it->value;
245 pstrcat(str, str_size, it->value);
246 if (it->next) {
247 pstrcat(str, str_size, " ");
248 }
249 }
250 args[i] = NULL;
251
252 if (log) {
253 slog("guest-exec called: \"%s\"", str);
254 }
255 g_free(str);
256
257 return args;
258}
259
260static void guest_exec_child_watch(GPid pid, gint status, gpointer data)
261{
262 GuestExecInfo *gei = (GuestExecInfo *)data;
263
264 g_debug("guest_exec_child_watch called, pid: %d, status: %u",
265 (int32_t)gpid_to_int64(pid), (uint32_t)status);
266
267 gei->status = status;
Marc-André Lureauc267d752022-04-20 17:26:22 +0400268 gei->finished = true;
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300269
270 g_spawn_close_pid(pid);
271}
272
Denis V. Lunev4005b472015-10-13 18:41:21 +0300273static void guest_exec_task_setup(gpointer data)
274{
275#if !defined(G_OS_WIN32)
Daniel Xu810f6772023-03-22 18:19:27 -0600276 bool has_merge = *(bool *)data;
Denis V. Lunev4005b472015-10-13 18:41:21 +0300277 struct sigaction sigact;
278
Daniel Xu810f6772023-03-22 18:19:27 -0600279 if (has_merge) {
280 /*
281 * FIXME: When `GLIB_VERSION_MIN_REQUIRED` is bumped to 2.58+, use
282 * g_spawn_async_with_fds() to be portable on windows. The current
283 * logic does not work on windows b/c `GSpawnChildSetupFunc` is run
284 * inside the parent, not the child.
285 */
286 if (dup2(STDOUT_FILENO, STDERR_FILENO) != 0) {
287 slog("dup2() failed to merge stderr into stdout: %s",
288 strerror(errno));
289 }
290 }
291
292 /* Reset ignored signals back to default. */
Denis V. Lunev4005b472015-10-13 18:41:21 +0300293 memset(&sigact, 0, sizeof(struct sigaction));
294 sigact.sa_handler = SIG_DFL;
295
296 if (sigaction(SIGPIPE, &sigact, NULL) != 0) {
297 slog("sigaction() failed to reset child process's SIGPIPE: %s",
298 strerror(errno));
299 }
300#endif
301}
302
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300303static gboolean guest_exec_input_watch(GIOChannel *ch,
304 GIOCondition cond, gpointer p_)
305{
306 GuestExecIOData *p = (GuestExecIOData *)p_;
307 gsize bytes_written = 0;
308 GIOStatus status;
309 GError *gerr = NULL;
310
311 /* nothing left to write */
312 if (p->size == p->length) {
313 goto done;
314 }
315
316 status = g_io_channel_write_chars(ch, (gchar *)p->data + p->length,
317 p->size - p->length, &bytes_written, &gerr);
318
319 /* can be not 0 even if not G_IO_STATUS_NORMAL */
320 if (bytes_written != 0) {
321 p->length += bytes_written;
322 }
323
324 /* continue write, our callback will be called again */
325 if (status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_AGAIN) {
326 return true;
327 }
328
329 if (gerr) {
330 g_warning("qga: i/o error writing to input_data channel: %s",
331 gerr->message);
332 g_error_free(gerr);
333 }
334
335done:
336 g_io_channel_shutdown(ch, true, NULL);
337 g_io_channel_unref(ch);
Marc-André Lureauc267d752022-04-20 17:26:22 +0400338 p->closed = true;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300339 g_free(p->data);
340
341 return false;
342}
343
344static gboolean guest_exec_output_watch(GIOChannel *ch,
345 GIOCondition cond, gpointer p_)
346{
347 GuestExecIOData *p = (GuestExecIOData *)p_;
348 gsize bytes_read;
349 GIOStatus gstatus;
350
351 if (cond == G_IO_HUP || cond == G_IO_ERR) {
352 goto close;
353 }
354
355 if (p->size == p->length) {
356 gpointer t = NULL;
357 if (!p->truncated && p->size < GUEST_EXEC_MAX_OUTPUT) {
358 t = g_try_realloc(p->data, p->size + GUEST_EXEC_IO_SIZE);
359 }
360 if (t == NULL) {
361 /* ignore truncated output */
362 gchar buf[GUEST_EXEC_IO_SIZE];
363
364 p->truncated = true;
365 gstatus = g_io_channel_read_chars(ch, buf, sizeof(buf),
366 &bytes_read, NULL);
367 if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) {
368 goto close;
369 }
370
371 return true;
372 }
373 p->size += GUEST_EXEC_IO_SIZE;
374 p->data = t;
375 }
376
377 /* Calling read API once.
378 * On next available data our callback will be called again */
379 gstatus = g_io_channel_read_chars(ch, (gchar *)p->data + p->length,
380 p->size - p->length, &bytes_read, NULL);
381 if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) {
382 goto close;
383 }
384
385 p->length += bytes_read;
386
387 return true;
388
389close:
Yuriy Pudgorodskiy3005c2c2016-04-06 08:43:30 +0300390 g_io_channel_shutdown(ch, true, NULL);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300391 g_io_channel_unref(ch);
Marc-André Lureauc267d752022-04-20 17:26:22 +0400392 p->closed = true;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300393 return false;
394}
395
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600396static GuestExecCaptureOutputMode ga_parse_capture_output(
397 GuestExecCaptureOutput *capture_output)
398{
399 if (!capture_output)
400 return GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE;
401 else if (capture_output->type == QTYPE_QBOOL)
402 return capture_output->u.flag ? GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED
403 : GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE;
404 else
405 return capture_output->u.mode;
406}
407
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300408GuestExec *qmp_guest_exec(const char *path,
409 bool has_arg, strList *arg,
410 bool has_env, strList *env,
Markus Armbruster91eab322022-11-04 17:07:11 +0100411 const char *input_data,
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600412 GuestExecCaptureOutput *capture_output,
Vladimir Sementsov-Ogievskiyb90abba2019-12-05 20:46:26 +0300413 Error **errp)
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300414{
415 GPid pid;
416 GuestExec *ge = NULL;
417 GuestExecInfo *gei;
418 char **argv, **envp;
419 strList arglist;
420 gboolean ret;
421 GError *gerr = NULL;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300422 gint in_fd, out_fd, err_fd;
423 GIOChannel *in_ch, *out_ch, *err_ch;
424 GSpawnFlags flags;
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600425 bool has_output = false;
Daniel Xu810f6772023-03-22 18:19:27 -0600426 bool has_merge = false;
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600427 GuestExecCaptureOutputMode output_mode;
Daniel P. Berrangé057489d2021-08-09 14:10:29 +0100428 g_autofree uint8_t *input = NULL;
Daniel P. Berrange920639c2015-11-23 15:37:07 +0000429 size_t ninput = 0;
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300430
431 arglist.value = (char *)path;
432 arglist.next = has_arg ? arg : NULL;
433
Markus Armbruster91eab322022-11-04 17:07:11 +0100434 if (input_data) {
Vladimir Sementsov-Ogievskiyb90abba2019-12-05 20:46:26 +0300435 input = qbase64_decode(input_data, -1, &ninput, errp);
Daniel P. Berrange920639c2015-11-23 15:37:07 +0000436 if (!input) {
437 return NULL;
438 }
439 }
440
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300441 argv = guest_exec_get_args(&arglist, true);
Yuri Pudgorodskiy02a4d822015-11-12 16:36:20 +0300442 envp = has_env ? guest_exec_get_args(env, false) : NULL;
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300443
Daniel P. Berrangée7b3af82018-05-04 15:34:46 +0100444 flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD |
445 G_SPAWN_SEARCH_PATH_FROM_ENVP;
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600446
447 output_mode = ga_parse_capture_output(capture_output);
448 switch (output_mode) {
449 case GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE:
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300450 flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL;
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600451 break;
452 case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDOUT:
453 has_output = true;
454 flags |= G_SPAWN_STDERR_TO_DEV_NULL;
455 break;
456 case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDERR:
457 has_output = true;
458 flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
459 break;
460 case GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED:
461 has_output = true;
462 break;
Daniel Xu810f6772023-03-22 18:19:27 -0600463#if !defined(G_OS_WIN32)
464 case GUEST_EXEC_CAPTURE_OUTPUT_MODE_MERGED:
465 has_output = true;
466 has_merge = true;
467 break;
468#endif
Daniel Xu9c5ccc52023-03-22 18:19:26 -0600469 case GUEST_EXEC_CAPTURE_OUTPUT_MODE__MAX:
470 /* Silence warning; impossible branch */
471 break;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300472 }
473
474 ret = g_spawn_async_with_pipes(NULL, argv, envp, flags,
Daniel Xu810f6772023-03-22 18:19:27 -0600475 guest_exec_task_setup, &has_merge, &pid, input_data ? &in_fd : NULL,
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300476 has_output ? &out_fd : NULL, has_output ? &err_fd : NULL, &gerr);
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300477 if (!ret) {
Vladimir Sementsov-Ogievskiyb90abba2019-12-05 20:46:26 +0300478 error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message);
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300479 g_error_free(gerr);
480 goto done;
481 }
482
483 ge = g_new0(GuestExec, 1);
484 ge->pid = gpid_to_int64(pid);
485
486 gei = guest_exec_info_add(pid);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300487 gei->has_output = has_output;
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300488 g_child_watch_add(pid, guest_exec_child_watch, gei);
489
Markus Armbruster91eab322022-11-04 17:07:11 +0100490 if (input_data) {
Daniel P. Berrangé057489d2021-08-09 14:10:29 +0100491 gei->in.data = g_steal_pointer(&input);
Daniel P. Berrange920639c2015-11-23 15:37:07 +0000492 gei->in.size = ninput;
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300493#ifdef G_OS_WIN32
494 in_ch = g_io_channel_win32_new_fd(in_fd);
495#else
496 in_ch = g_io_channel_unix_new(in_fd);
497#endif
498 g_io_channel_set_encoding(in_ch, NULL, NULL);
499 g_io_channel_set_buffered(in_ch, false);
500 g_io_channel_set_flags(in_ch, G_IO_FLAG_NONBLOCK, NULL);
Yuriy Pudgorodskiy3005c2c2016-04-06 08:43:30 +0300501 g_io_channel_set_close_on_unref(in_ch, true);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300502 g_io_add_watch(in_ch, G_IO_OUT, guest_exec_input_watch, &gei->in);
503 }
504
505 if (has_output) {
506#ifdef G_OS_WIN32
507 out_ch = g_io_channel_win32_new_fd(out_fd);
508 err_ch = g_io_channel_win32_new_fd(err_fd);
509#else
510 out_ch = g_io_channel_unix_new(out_fd);
511 err_ch = g_io_channel_unix_new(err_fd);
512#endif
513 g_io_channel_set_encoding(out_ch, NULL, NULL);
514 g_io_channel_set_encoding(err_ch, NULL, NULL);
515 g_io_channel_set_buffered(out_ch, false);
516 g_io_channel_set_buffered(err_ch, false);
Yuriy Pudgorodskiy3005c2c2016-04-06 08:43:30 +0300517 g_io_channel_set_close_on_unref(out_ch, true);
518 g_io_channel_set_close_on_unref(err_ch, true);
Yuri Pudgorodskiya1853dc2015-10-13 18:41:23 +0300519 g_io_add_watch(out_ch, G_IO_IN | G_IO_HUP,
520 guest_exec_output_watch, &gei->out);
521 g_io_add_watch(err_ch, G_IO_IN | G_IO_HUP,
522 guest_exec_output_watch, &gei->err);
523 }
524
Yuri Pudgorodskiyd697e302015-10-13 18:41:20 +0300525done:
526 g_free(argv);
527 g_free(envp);
528
529 return ge;
530}
Eric Blake0b4b4932016-02-09 14:27:16 -0700531
532/* Convert GuestFileWhence (either a raw integer or an enum value) into
533 * the guest's SEEK_ constants. */
534int ga_parse_whence(GuestFileWhence *whence, Error **errp)
535{
Eric Blakea23f38a2020-03-20 10:05:07 -0500536 /*
537 * Exploit the fact that we picked values to match QGA_SEEK_*;
538 * however, we have to use a temporary variable since the union
539 * members may have different size.
540 */
Eric Blake0b4b4932016-02-09 14:27:16 -0700541 if (whence->type == QTYPE_QSTRING) {
Eric Blakea23f38a2020-03-20 10:05:07 -0500542 int value = whence->u.name;
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400543 whence->type = QTYPE_QNUM;
Eric Blakea23f38a2020-03-20 10:05:07 -0500544 whence->u.value = value;
Eric Blake0b4b4932016-02-09 14:27:16 -0700545 }
546 switch (whence->u.value) {
547 case QGA_SEEK_SET:
548 return SEEK_SET;
549 case QGA_SEEK_CUR:
550 return SEEK_CUR;
551 case QGA_SEEK_END:
552 return SEEK_END;
553 }
554 error_setg(errp, "invalid whence code %"PRId64, whence->u.value);
555 return -1;
556}
Vinzenz Feenstra0a3d1972017-04-04 08:46:31 +0200557
Vladimir Sementsov-Ogievskiyb90abba2019-12-05 20:46:26 +0300558GuestHostName *qmp_guest_get_host_name(Error **errp)
Vinzenz Feenstra0a3d1972017-04-04 08:46:31 +0200559{
560 GuestHostName *result = NULL;
Marc-André Lureau548fb0d2022-04-20 17:26:14 +0400561 g_autofree char *hostname = qga_get_host_name(errp);
Michal Privoznik0d3a8f32020-06-22 20:19:36 +0200562
563 /*
564 * We want to avoid using g_get_host_name() because that
565 * caches the result and we wouldn't reflect changes in the
566 * host name.
567 */
568
569 if (!hostname) {
570 hostname = g_strdup("localhost");
Vinzenz Feenstra0a3d1972017-04-04 08:46:31 +0200571 }
Michal Privoznik0d3a8f32020-06-22 20:19:36 +0200572
573 result = g_new0(GuestHostName, 1);
574 result->host_name = g_steal_pointer(&hostname);
Vinzenz Feenstra0a3d1972017-04-04 08:46:31 +0200575 return result;
576}
Vinzenz Feenstra53c58e62017-04-19 12:52:58 +0200577
578GuestTimezone *qmp_guest_get_timezone(Error **errp)
579{
Vinzenz Feenstra53c58e62017-04-19 12:52:58 +0200580 GuestTimezone *info = NULL;
581 GTimeZone *tz = NULL;
582 gint64 now = 0;
583 gint32 intv = 0;
584 gchar const *name = NULL;
585
586 info = g_new0(GuestTimezone, 1);
587 tz = g_time_zone_new_local();
588 if (tz == NULL) {
589 error_setg(errp, QERR_QGA_COMMAND_FAILED,
590 "Couldn't retrieve local timezone");
591 goto error;
592 }
593
594 now = g_get_real_time() / G_USEC_PER_SEC;
595 intv = g_time_zone_find_interval(tz, G_TIME_TYPE_UNIVERSAL, now);
596 info->offset = g_time_zone_get_offset(tz, intv);
597 name = g_time_zone_get_abbreviation(tz, intv);
598 if (name != NULL) {
Vinzenz Feenstra53c58e62017-04-19 12:52:58 +0200599 info->zone = g_strdup(name);
600 }
601 g_time_zone_unref(tz);
602
603 return info;
604
605error:
606 g_free(info);
607 return NULL;
Vinzenz Feenstra53c58e62017-04-19 12:52:58 +0200608}
Philippe Mathieu-Daudéead83a12020-04-14 15:30:43 +0200609
610GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
611 int64_t count, Error **errp)
612{
613 GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
614 GuestFileRead *read_data;
615
616 if (!gfh) {
617 return NULL;
618 }
619 if (!has_count) {
620 count = QGA_READ_COUNT_DEFAULT;
Philippe Mathieu-Daudé1329651f2020-04-14 15:30:44 +0200621 } else if (count < 0 || count > GUEST_FILE_READ_COUNT_MAX) {
Philippe Mathieu-Daudéead83a12020-04-14 15:30:43 +0200622 error_setg(errp, "value '%" PRId64 "' is invalid for argument count",
623 count);
624 return NULL;
625 }
626
627 read_data = guest_file_read_unsafe(gfh, count, errp);
628 if (!read_data) {
629 slog("guest-file-write failed, handle: %" PRId64, handle);
630 }
631
632 return read_data;
633}
Marc-André Lureau287698e2022-03-07 11:03:59 +0400634
635int64_t qmp_guest_get_time(Error **errp)
636{
637 return g_get_real_time() * 1000;
638}