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