2 * QEMU Guest Agent common/cross-platform command implementations
4 * Copyright IBM Corp. 2012
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
13 #include "qemu/osdep.h"
14 #include "qga/guest-agent-core.h"
15 #include "qga-qmp-commands.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qemu/base64.h"
18 #include "qemu/cutils.h"
20 /* Maximum captured guest-exec out_data/err_data - 16MB */
21 #define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)
22 /* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */
23 #define GUEST_EXEC_IO_SIZE (4*1024)
25 /* Note: in some situations, like with the fsfreeze, logging may be
26 * temporarilly disabled. if it is necessary that a command be able
27 * to log for accounting purposes, check ga_logging_enabled() beforehand,
28 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
30 void slog(const gchar
*fmt
, ...)
35 g_logv("syslog", G_LOG_LEVEL_INFO
, fmt
, ap
);
39 int64_t qmp_guest_sync_delimited(int64_t id
, Error
**errp
)
41 ga_set_response_delimited(ga_state
);
45 int64_t qmp_guest_sync(int64_t id
, Error
**errp
)
50 void qmp_guest_ping(Error
**errp
)
52 slog("guest-ping called");
55 static void qmp_command_info(QmpCommand
*cmd
, void *opaque
)
57 GuestAgentInfo
*info
= opaque
;
58 GuestAgentCommandInfo
*cmd_info
;
59 GuestAgentCommandInfoList
*cmd_info_list
;
61 cmd_info
= g_new0(GuestAgentCommandInfo
, 1);
62 cmd_info
->name
= g_strdup(qmp_command_name(cmd
));
63 cmd_info
->enabled
= qmp_command_is_enabled(cmd
);
64 cmd_info
->success_response
= qmp_has_success_response(cmd
);
66 cmd_info_list
= g_new0(GuestAgentCommandInfoList
, 1);
67 cmd_info_list
->value
= cmd_info
;
68 cmd_info_list
->next
= info
->supported_commands
;
69 info
->supported_commands
= cmd_info_list
;
72 struct GuestAgentInfo
*qmp_guest_info(Error
**errp
)
74 GuestAgentInfo
*info
= g_new0(GuestAgentInfo
, 1);
76 info
->version
= g_strdup(QEMU_VERSION
);
77 qmp_for_each_command(qmp_command_info
, info
);
81 struct GuestExecIOData
{
89 typedef struct GuestExecIOData GuestExecIOData
;
91 struct GuestExecInfo
{
100 QTAILQ_ENTRY(GuestExecInfo
) next
;
102 typedef struct GuestExecInfo GuestExecInfo
;
105 QTAILQ_HEAD(, GuestExecInfo
) processes
;
106 } guest_exec_state
= {
107 .processes
= QTAILQ_HEAD_INITIALIZER(guest_exec_state
.processes
),
110 static int64_t gpid_to_int64(GPid pid
)
113 return GetProcessId(pid
);
119 static GuestExecInfo
*guest_exec_info_add(GPid pid
)
123 gei
= g_new0(GuestExecInfo
, 1);
125 gei
->pid_numeric
= gpid_to_int64(pid
);
126 QTAILQ_INSERT_TAIL(&guest_exec_state
.processes
, gei
, next
);
131 static GuestExecInfo
*guest_exec_info_find(int64_t pid_numeric
)
135 QTAILQ_FOREACH(gei
, &guest_exec_state
.processes
, next
) {
136 if (gei
->pid_numeric
== pid_numeric
) {
144 GuestExecStatus
*qmp_guest_exec_status(int64_t pid
, Error
**err
)
147 GuestExecStatus
*ges
;
149 slog("guest-exec-status called, pid: %u", (uint32_t)pid
);
151 gei
= guest_exec_info_find(pid
);
153 error_setg(err
, QERR_INVALID_PARAMETER
, "pid");
157 ges
= g_new0(GuestExecStatus
, 1);
159 bool finished
= g_atomic_int_get(&gei
->finished
);
161 /* need to wait till output channels are closed
162 * to be sure we captured all output at this point */
163 if (gei
->has_output
) {
164 finished
= finished
&& g_atomic_int_get(&gei
->out
.closed
);
165 finished
= finished
&& g_atomic_int_get(&gei
->err
.closed
);
168 ges
->exited
= finished
;
170 /* Glib has no portable way to parse exit status.
171 * On UNIX, we can get either exit code from normal termination
173 * On Windows, it is either the same exit code or the exception
174 * value for an unhandled exception that caused the process
176 * See MSDN for GetExitCodeProcess() and ntstatus.h for possible
177 * well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV
179 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
180 * https://msdn.microsoft.com/en-us/library/aa260331(v=vs.60).aspx
183 /* Additionally WIN32 does not provide any additional information
184 * on whether the child exited or terminated via signal.
185 * We use this simple range check to distinguish application exit code
186 * (usually value less then 256) and unhandled exception code with
187 * ntstatus (always value greater then 0xC0000005). */
188 if ((uint32_t)gei
->status
< 0xC0000000U
) {
189 ges
->has_exitcode
= true;
190 ges
->exitcode
= gei
->status
;
192 ges
->has_signal
= true;
193 ges
->signal
= gei
->status
;
196 if (WIFEXITED(gei
->status
)) {
197 ges
->has_exitcode
= true;
198 ges
->exitcode
= WEXITSTATUS(gei
->status
);
199 } else if (WIFSIGNALED(gei
->status
)) {
200 ges
->has_signal
= true;
201 ges
->signal
= WTERMSIG(gei
->status
);
204 if (gei
->out
.length
> 0) {
205 ges
->has_out_data
= true;
206 ges
->out_data
= g_base64_encode(gei
->out
.data
, gei
->out
.length
);
207 g_free(gei
->out
.data
);
208 ges
->has_out_truncated
= gei
->out
.truncated
;
211 if (gei
->err
.length
> 0) {
212 ges
->has_err_data
= true;
213 ges
->err_data
= g_base64_encode(gei
->err
.data
, gei
->err
.length
);
214 g_free(gei
->err
.data
);
215 ges
->has_err_truncated
= gei
->err
.truncated
;
218 QTAILQ_REMOVE(&guest_exec_state
.processes
, gei
, next
);
225 /* Get environment variables or arguments array for execve(). */
226 static char **guest_exec_get_args(const strList
*entry
, bool log
)
229 int count
= 1, i
= 0; /* reserve for NULL terminator */
231 char *str
; /* for logging array of arguments */
234 for (it
= entry
; it
!= NULL
; it
= it
->next
) {
236 str_size
+= 1 + strlen(it
->value
);
239 str
= g_malloc(str_size
);
241 args
= g_malloc(count
* sizeof(char *));
242 for (it
= entry
; it
!= NULL
; it
= it
->next
) {
243 args
[i
++] = it
->value
;
244 pstrcat(str
, str_size
, it
->value
);
246 pstrcat(str
, str_size
, " ");
252 slog("guest-exec called: \"%s\"", str
);
259 static void guest_exec_child_watch(GPid pid
, gint status
, gpointer data
)
261 GuestExecInfo
*gei
= (GuestExecInfo
*)data
;
263 g_debug("guest_exec_child_watch called, pid: %d, status: %u",
264 (int32_t)gpid_to_int64(pid
), (uint32_t)status
);
266 gei
->status
= status
;
267 gei
->finished
= true;
269 g_spawn_close_pid(pid
);
272 /** Reset ignored signals back to default. */
273 static void guest_exec_task_setup(gpointer data
)
275 #if !defined(G_OS_WIN32)
276 struct sigaction sigact
;
278 memset(&sigact
, 0, sizeof(struct sigaction
));
279 sigact
.sa_handler
= SIG_DFL
;
281 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0) {
282 slog("sigaction() failed to reset child process's SIGPIPE: %s",
288 static gboolean
guest_exec_input_watch(GIOChannel
*ch
,
289 GIOCondition cond
, gpointer p_
)
291 GuestExecIOData
*p
= (GuestExecIOData
*)p_
;
292 gsize bytes_written
= 0;
296 /* nothing left to write */
297 if (p
->size
== p
->length
) {
301 status
= g_io_channel_write_chars(ch
, (gchar
*)p
->data
+ p
->length
,
302 p
->size
- p
->length
, &bytes_written
, &gerr
);
304 /* can be not 0 even if not G_IO_STATUS_NORMAL */
305 if (bytes_written
!= 0) {
306 p
->length
+= bytes_written
;
309 /* continue write, our callback will be called again */
310 if (status
== G_IO_STATUS_NORMAL
|| status
== G_IO_STATUS_AGAIN
) {
315 g_warning("qga: i/o error writing to input_data channel: %s",
321 g_io_channel_shutdown(ch
, true, NULL
);
322 g_io_channel_unref(ch
);
323 g_atomic_int_set(&p
->closed
, 1);
329 static gboolean
guest_exec_output_watch(GIOChannel
*ch
,
330 GIOCondition cond
, gpointer p_
)
332 GuestExecIOData
*p
= (GuestExecIOData
*)p_
;
336 if (cond
== G_IO_HUP
|| cond
== G_IO_ERR
) {
340 if (p
->size
== p
->length
) {
342 if (!p
->truncated
&& p
->size
< GUEST_EXEC_MAX_OUTPUT
) {
343 t
= g_try_realloc(p
->data
, p
->size
+ GUEST_EXEC_IO_SIZE
);
346 /* ignore truncated output */
347 gchar buf
[GUEST_EXEC_IO_SIZE
];
350 gstatus
= g_io_channel_read_chars(ch
, buf
, sizeof(buf
),
352 if (gstatus
== G_IO_STATUS_EOF
|| gstatus
== G_IO_STATUS_ERROR
) {
358 p
->size
+= GUEST_EXEC_IO_SIZE
;
362 /* Calling read API once.
363 * On next available data our callback will be called again */
364 gstatus
= g_io_channel_read_chars(ch
, (gchar
*)p
->data
+ p
->length
,
365 p
->size
- p
->length
, &bytes_read
, NULL
);
366 if (gstatus
== G_IO_STATUS_EOF
|| gstatus
== G_IO_STATUS_ERROR
) {
370 p
->length
+= bytes_read
;
375 g_io_channel_shutdown(ch
, true, NULL
);
376 g_io_channel_unref(ch
);
377 g_atomic_int_set(&p
->closed
, 1);
381 GuestExec
*qmp_guest_exec(const char *path
,
382 bool has_arg
, strList
*arg
,
383 bool has_env
, strList
*env
,
384 bool has_input_data
, const char *input_data
,
385 bool has_capture_output
, bool capture_output
,
389 GuestExec
*ge
= NULL
;
395 gint in_fd
, out_fd
, err_fd
;
396 GIOChannel
*in_ch
, *out_ch
, *err_ch
;
398 bool has_output
= (has_capture_output
&& capture_output
);
399 uint8_t *input
= NULL
;
402 arglist
.value
= (char *)path
;
403 arglist
.next
= has_arg
? arg
: NULL
;
405 if (has_input_data
) {
406 input
= qbase64_decode(input_data
, -1, &ninput
, err
);
412 argv
= guest_exec_get_args(&arglist
, true);
413 envp
= has_env
? guest_exec_get_args(env
, false) : NULL
;
415 flags
= G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
;
416 #if GLIB_CHECK_VERSION(2, 33, 2)
417 flags
|= G_SPAWN_SEARCH_PATH_FROM_ENVP
;
420 flags
|= G_SPAWN_STDOUT_TO_DEV_NULL
| G_SPAWN_STDERR_TO_DEV_NULL
;
423 ret
= g_spawn_async_with_pipes(NULL
, argv
, envp
, flags
,
424 guest_exec_task_setup
, NULL
, &pid
, has_input_data
? &in_fd
: NULL
,
425 has_output
? &out_fd
: NULL
, has_output
? &err_fd
: NULL
, &gerr
);
427 error_setg(err
, QERR_QGA_COMMAND_FAILED
, gerr
->message
);
432 ge
= g_new0(GuestExec
, 1);
433 ge
->pid
= gpid_to_int64(pid
);
435 gei
= guest_exec_info_add(pid
);
436 gei
->has_output
= has_output
;
437 g_child_watch_add(pid
, guest_exec_child_watch
, gei
);
439 if (has_input_data
) {
440 gei
->in
.data
= input
;
441 gei
->in
.size
= ninput
;
443 in_ch
= g_io_channel_win32_new_fd(in_fd
);
445 in_ch
= g_io_channel_unix_new(in_fd
);
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
);
450 g_io_channel_set_close_on_unref(in_ch
, true);
451 g_io_add_watch(in_ch
, G_IO_OUT
, guest_exec_input_watch
, &gei
->in
);
456 out_ch
= g_io_channel_win32_new_fd(out_fd
);
457 err_ch
= g_io_channel_win32_new_fd(err_fd
);
459 out_ch
= g_io_channel_unix_new(out_fd
);
460 err_ch
= g_io_channel_unix_new(err_fd
);
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);
466 g_io_channel_set_close_on_unref(out_ch
, true);
467 g_io_channel_set_close_on_unref(err_ch
, true);
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
);
481 /* Convert GuestFileWhence (either a raw integer or an enum value) into
482 * the guest's SEEK_ constants. */
483 int ga_parse_whence(GuestFileWhence
*whence
, Error
**errp
)
485 /* Exploit the fact that we picked values to match QGA_SEEK_*. */
486 if (whence
->type
== QTYPE_QSTRING
) {
487 whence
->type
= QTYPE_QINT
;
488 whence
->u
.value
= whence
->u
.name
;
490 switch (whence
->u
.value
) {
498 error_setg(errp
, "invalid whence code %"PRId64
, whence
->u
.value
);