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"
15 #include "qga/guest-agent-core.h"
16 #include "qga-qmp-commands.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/base64.h"
19 #include "qemu/cutils.h"
21 /* Maximum captured guest-exec out_data/err_data - 16MB */
22 #define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)
23 /* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */
24 #define GUEST_EXEC_IO_SIZE (4*1024)
26 /* Note: in some situations, like with the fsfreeze, logging may be
27 * temporarilly disabled. if it is necessary that a command be able
28 * to log for accounting purposes, check ga_logging_enabled() beforehand,
29 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
31 void slog(const gchar
*fmt
, ...)
36 g_logv("syslog", G_LOG_LEVEL_INFO
, fmt
, ap
);
40 int64_t qmp_guest_sync_delimited(int64_t id
, Error
**errp
)
42 ga_set_response_delimited(ga_state
);
46 int64_t qmp_guest_sync(int64_t id
, Error
**errp
)
51 void qmp_guest_ping(Error
**errp
)
53 slog("guest-ping called");
56 static void qmp_command_info(QmpCommand
*cmd
, void *opaque
)
58 GuestAgentInfo
*info
= opaque
;
59 GuestAgentCommandInfo
*cmd_info
;
60 GuestAgentCommandInfoList
*cmd_info_list
;
62 cmd_info
= g_new0(GuestAgentCommandInfo
, 1);
63 cmd_info
->name
= g_strdup(qmp_command_name(cmd
));
64 cmd_info
->enabled
= qmp_command_is_enabled(cmd
);
65 cmd_info
->success_response
= qmp_has_success_response(cmd
);
67 cmd_info_list
= g_new0(GuestAgentCommandInfoList
, 1);
68 cmd_info_list
->value
= cmd_info
;
69 cmd_info_list
->next
= info
->supported_commands
;
70 info
->supported_commands
= cmd_info_list
;
73 struct GuestAgentInfo
*qmp_guest_info(Error
**errp
)
75 GuestAgentInfo
*info
= g_new0(GuestAgentInfo
, 1);
77 info
->version
= g_strdup(QEMU_VERSION
);
78 qmp_for_each_command(qmp_command_info
, info
);
82 struct GuestExecIOData
{
90 typedef struct GuestExecIOData GuestExecIOData
;
92 struct GuestExecInfo
{
101 QTAILQ_ENTRY(GuestExecInfo
) next
;
103 typedef struct GuestExecInfo GuestExecInfo
;
106 QTAILQ_HEAD(, GuestExecInfo
) processes
;
107 } guest_exec_state
= {
108 .processes
= QTAILQ_HEAD_INITIALIZER(guest_exec_state
.processes
),
111 static int64_t gpid_to_int64(GPid pid
)
114 return GetProcessId(pid
);
120 static GuestExecInfo
*guest_exec_info_add(GPid pid
)
124 gei
= g_new0(GuestExecInfo
, 1);
126 gei
->pid_numeric
= gpid_to_int64(pid
);
127 QTAILQ_INSERT_TAIL(&guest_exec_state
.processes
, gei
, next
);
132 static GuestExecInfo
*guest_exec_info_find(int64_t pid_numeric
)
136 QTAILQ_FOREACH(gei
, &guest_exec_state
.processes
, next
) {
137 if (gei
->pid_numeric
== pid_numeric
) {
145 GuestExecStatus
*qmp_guest_exec_status(int64_t pid
, Error
**err
)
148 GuestExecStatus
*ges
;
150 slog("guest-exec-status called, pid: %u", (uint32_t)pid
);
152 gei
= guest_exec_info_find(pid
);
154 error_setg(err
, QERR_INVALID_PARAMETER
, "pid");
158 ges
= g_new0(GuestExecStatus
, 1);
160 bool finished
= g_atomic_int_get(&gei
->finished
);
162 /* need to wait till output channels are closed
163 * to be sure we captured all output at this point */
164 if (gei
->has_output
) {
165 finished
= finished
&& g_atomic_int_get(&gei
->out
.closed
);
166 finished
= finished
&& g_atomic_int_get(&gei
->err
.closed
);
169 ges
->exited
= finished
;
171 /* Glib has no portable way to parse exit status.
172 * On UNIX, we can get either exit code from normal termination
174 * On Windows, it is either the same exit code or the exception
175 * value for an unhandled exception that caused the process
177 * See MSDN for GetExitCodeProcess() and ntstatus.h for possible
178 * well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV
180 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
181 * https://msdn.microsoft.com/en-us/library/aa260331(v=vs.60).aspx
184 /* Additionally WIN32 does not provide any additional information
185 * on whetherthe child exited or terminated via signal.
186 * We use this simple range check to distingish application exit code
187 * (usually value less then 256) and unhandled exception code with
188 * ntstatus (always value greater then 0xC0000005). */
189 if ((uint32_t)gei
->status
< 0xC0000000U
) {
190 ges
->has_exitcode
= true;
191 ges
->exitcode
= gei
->status
;
193 ges
->has_signal
= true;
194 ges
->signal
= gei
->status
;
197 if (WIFEXITED(gei
->status
)) {
198 ges
->has_exitcode
= true;
199 ges
->exitcode
= WEXITSTATUS(gei
->status
);
200 } else if (WIFSIGNALED(gei
->status
)) {
201 ges
->has_signal
= true;
202 ges
->signal
= WTERMSIG(gei
->status
);
205 if (gei
->out
.length
> 0) {
206 ges
->has_out_data
= true;
207 ges
->out_data
= g_base64_encode(gei
->out
.data
, gei
->out
.length
);
208 g_free(gei
->out
.data
);
209 ges
->has_out_truncated
= gei
->out
.truncated
;
212 if (gei
->err
.length
> 0) {
213 ges
->has_err_data
= true;
214 ges
->err_data
= g_base64_encode(gei
->err
.data
, gei
->err
.length
);
215 g_free(gei
->err
.data
);
216 ges
->has_err_truncated
= gei
->err
.truncated
;
219 QTAILQ_REMOVE(&guest_exec_state
.processes
, gei
, next
);
226 /* Get environment variables or arguments array for execve(). */
227 static char **guest_exec_get_args(const strList
*entry
, bool log
)
230 int count
= 1, i
= 0; /* reserve for NULL terminator */
232 char *str
; /* for logging array of arguments */
235 for (it
= entry
; it
!= NULL
; it
= it
->next
) {
237 str_size
+= 1 + strlen(it
->value
);
240 str
= g_malloc(str_size
);
242 args
= g_malloc(count
* sizeof(char *));
243 for (it
= entry
; it
!= NULL
; it
= it
->next
) {
244 args
[i
++] = it
->value
;
245 pstrcat(str
, str_size
, it
->value
);
247 pstrcat(str
, str_size
, " ");
253 slog("guest-exec called: \"%s\"", str
);
260 static void guest_exec_child_watch(GPid pid
, gint status
, gpointer data
)
262 GuestExecInfo
*gei
= (GuestExecInfo
*)data
;
264 g_debug("guest_exec_child_watch called, pid: %d, status: %u",
265 (int32_t)gpid_to_int64(pid
), (uint32_t)status
);
267 gei
->status
= status
;
268 gei
->finished
= true;
270 g_spawn_close_pid(pid
);
273 /** Reset ignored signals back to default. */
274 static void guest_exec_task_setup(gpointer data
)
276 #if !defined(G_OS_WIN32)
277 struct sigaction sigact
;
279 memset(&sigact
, 0, sizeof(struct sigaction
));
280 sigact
.sa_handler
= SIG_DFL
;
282 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0) {
283 slog("sigaction() failed to reset child process's SIGPIPE: %s",
289 static gboolean
guest_exec_input_watch(GIOChannel
*ch
,
290 GIOCondition cond
, gpointer p_
)
292 GuestExecIOData
*p
= (GuestExecIOData
*)p_
;
293 gsize bytes_written
= 0;
297 /* nothing left to write */
298 if (p
->size
== p
->length
) {
302 status
= g_io_channel_write_chars(ch
, (gchar
*)p
->data
+ p
->length
,
303 p
->size
- p
->length
, &bytes_written
, &gerr
);
305 /* can be not 0 even if not G_IO_STATUS_NORMAL */
306 if (bytes_written
!= 0) {
307 p
->length
+= bytes_written
;
310 /* continue write, our callback will be called again */
311 if (status
== G_IO_STATUS_NORMAL
|| status
== G_IO_STATUS_AGAIN
) {
316 g_warning("qga: i/o error writing to input_data channel: %s",
322 g_io_channel_shutdown(ch
, true, NULL
);
323 g_io_channel_unref(ch
);
324 g_atomic_int_set(&p
->closed
, 1);
330 static gboolean
guest_exec_output_watch(GIOChannel
*ch
,
331 GIOCondition cond
, gpointer p_
)
333 GuestExecIOData
*p
= (GuestExecIOData
*)p_
;
337 if (cond
== G_IO_HUP
|| cond
== G_IO_ERR
) {
341 if (p
->size
== p
->length
) {
343 if (!p
->truncated
&& p
->size
< GUEST_EXEC_MAX_OUTPUT
) {
344 t
= g_try_realloc(p
->data
, p
->size
+ GUEST_EXEC_IO_SIZE
);
347 /* ignore truncated output */
348 gchar buf
[GUEST_EXEC_IO_SIZE
];
351 gstatus
= g_io_channel_read_chars(ch
, buf
, sizeof(buf
),
353 if (gstatus
== G_IO_STATUS_EOF
|| gstatus
== G_IO_STATUS_ERROR
) {
359 p
->size
+= GUEST_EXEC_IO_SIZE
;
363 /* Calling read API once.
364 * On next available data our callback will be called again */
365 gstatus
= g_io_channel_read_chars(ch
, (gchar
*)p
->data
+ p
->length
,
366 p
->size
- p
->length
, &bytes_read
, NULL
);
367 if (gstatus
== G_IO_STATUS_EOF
|| gstatus
== G_IO_STATUS_ERROR
) {
371 p
->length
+= bytes_read
;
376 g_io_channel_shutdown(ch
, true, NULL
);
377 g_io_channel_unref(ch
);
378 g_atomic_int_set(&p
->closed
, 1);
382 GuestExec
*qmp_guest_exec(const char *path
,
383 bool has_arg
, strList
*arg
,
384 bool has_env
, strList
*env
,
385 bool has_input_data
, const char *input_data
,
386 bool has_capture_output
, bool capture_output
,
390 GuestExec
*ge
= NULL
;
396 gint in_fd
, out_fd
, err_fd
;
397 GIOChannel
*in_ch
, *out_ch
, *err_ch
;
399 bool has_output
= (has_capture_output
&& capture_output
);
400 uint8_t *input
= NULL
;
403 arglist
.value
= (char *)path
;
404 arglist
.next
= has_arg
? arg
: NULL
;
406 if (has_input_data
) {
407 input
= qbase64_decode(input_data
, -1, &ninput
, err
);
413 argv
= guest_exec_get_args(&arglist
, true);
414 envp
= has_env
? guest_exec_get_args(env
, false) : NULL
;
416 flags
= G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
;
417 #if GLIB_CHECK_VERSION(2, 33, 2)
418 flags
|= G_SPAWN_SEARCH_PATH_FROM_ENVP
;
421 flags
|= G_SPAWN_STDOUT_TO_DEV_NULL
| G_SPAWN_STDERR_TO_DEV_NULL
;
424 ret
= g_spawn_async_with_pipes(NULL
, argv
, envp
, flags
,
425 guest_exec_task_setup
, NULL
, &pid
, has_input_data
? &in_fd
: NULL
,
426 has_output
? &out_fd
: NULL
, has_output
? &err_fd
: NULL
, &gerr
);
428 error_setg(err
, QERR_QGA_COMMAND_FAILED
, gerr
->message
);
433 ge
= g_new0(GuestExec
, 1);
434 ge
->pid
= gpid_to_int64(pid
);
436 gei
= guest_exec_info_add(pid
);
437 gei
->has_output
= has_output
;
438 g_child_watch_add(pid
, guest_exec_child_watch
, gei
);
440 if (has_input_data
) {
441 gei
->in
.data
= input
;
442 gei
->in
.size
= ninput
;
444 in_ch
= g_io_channel_win32_new_fd(in_fd
);
446 in_ch
= g_io_channel_unix_new(in_fd
);
448 g_io_channel_set_encoding(in_ch
, NULL
, NULL
);
449 g_io_channel_set_buffered(in_ch
, false);
450 g_io_channel_set_flags(in_ch
, G_IO_FLAG_NONBLOCK
, NULL
);
451 g_io_channel_set_close_on_unref(in_ch
, true);
452 g_io_add_watch(in_ch
, G_IO_OUT
, guest_exec_input_watch
, &gei
->in
);
457 out_ch
= g_io_channel_win32_new_fd(out_fd
);
458 err_ch
= g_io_channel_win32_new_fd(err_fd
);
460 out_ch
= g_io_channel_unix_new(out_fd
);
461 err_ch
= g_io_channel_unix_new(err_fd
);
463 g_io_channel_set_encoding(out_ch
, NULL
, NULL
);
464 g_io_channel_set_encoding(err_ch
, NULL
, NULL
);
465 g_io_channel_set_buffered(out_ch
, false);
466 g_io_channel_set_buffered(err_ch
, false);
467 g_io_channel_set_close_on_unref(out_ch
, true);
468 g_io_channel_set_close_on_unref(err_ch
, true);
469 g_io_add_watch(out_ch
, G_IO_IN
| G_IO_HUP
,
470 guest_exec_output_watch
, &gei
->out
);
471 g_io_add_watch(err_ch
, G_IO_IN
| G_IO_HUP
,
472 guest_exec_output_watch
, &gei
->err
);
482 /* Convert GuestFileWhence (either a raw integer or an enum value) into
483 * the guest's SEEK_ constants. */
484 int ga_parse_whence(GuestFileWhence
*whence
, Error
**errp
)
486 /* Exploit the fact that we picked values to match QGA_SEEK_*. */
487 if (whence
->type
== QTYPE_QSTRING
) {
488 whence
->type
= QTYPE_QINT
;
489 whence
->u
.value
= whence
->u
.name
;
491 switch (whence
->u
.value
) {
499 error_setg(errp
, "invalid whence code %"PRId64
, whence
->u
.value
);