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.
14 #include "qga/guest-agent-core.h"
15 #include "qga-qmp-commands.h"
16 #include "qapi/qmp/qerror.h"
18 /* Maximum captured guest-exec out_data/err_data - 16MB */
19 #define GUEST_EXEC_MAX_OUTPUT (16*1024*1024)
20 /* Allocation and I/O buffer for reading guest-exec out_data/err_data - 4KB */
21 #define GUEST_EXEC_IO_SIZE (4*1024)
23 /* Note: in some situations, like with the fsfreeze, logging may be
24 * temporarilly disabled. if it is necessary that a command be able
25 * to log for accounting purposes, check ga_logging_enabled() beforehand,
26 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
28 void slog(const gchar
*fmt
, ...)
33 g_logv("syslog", G_LOG_LEVEL_INFO
, fmt
, ap
);
37 int64_t qmp_guest_sync_delimited(int64_t id
, Error
**errp
)
39 ga_set_response_delimited(ga_state
);
43 int64_t qmp_guest_sync(int64_t id
, Error
**errp
)
48 void qmp_guest_ping(Error
**errp
)
50 slog("guest-ping called");
53 static void qmp_command_info(QmpCommand
*cmd
, void *opaque
)
55 GuestAgentInfo
*info
= opaque
;
56 GuestAgentCommandInfo
*cmd_info
;
57 GuestAgentCommandInfoList
*cmd_info_list
;
59 cmd_info
= g_new0(GuestAgentCommandInfo
, 1);
60 cmd_info
->name
= g_strdup(qmp_command_name(cmd
));
61 cmd_info
->enabled
= qmp_command_is_enabled(cmd
);
62 cmd_info
->success_response
= qmp_has_success_response(cmd
);
64 cmd_info_list
= g_new0(GuestAgentCommandInfoList
, 1);
65 cmd_info_list
->value
= cmd_info
;
66 cmd_info_list
->next
= info
->supported_commands
;
67 info
->supported_commands
= cmd_info_list
;
70 struct GuestAgentInfo
*qmp_guest_info(Error
**errp
)
72 GuestAgentInfo
*info
= g_new0(GuestAgentInfo
, 1);
74 info
->version
= g_strdup(QEMU_VERSION
);
75 qmp_for_each_command(qmp_command_info
, info
);
79 struct GuestExecIOData
{
87 typedef struct GuestExecIOData GuestExecIOData
;
89 struct GuestExecInfo
{
98 QTAILQ_ENTRY(GuestExecInfo
) next
;
100 typedef struct GuestExecInfo GuestExecInfo
;
103 QTAILQ_HEAD(, GuestExecInfo
) processes
;
104 } guest_exec_state
= {
105 .processes
= QTAILQ_HEAD_INITIALIZER(guest_exec_state
.processes
),
108 static int64_t gpid_to_int64(GPid pid
)
111 return GetProcessId(pid
);
117 static GuestExecInfo
*guest_exec_info_add(GPid pid
)
121 gei
= g_new0(GuestExecInfo
, 1);
123 gei
->pid_numeric
= gpid_to_int64(pid
);
124 QTAILQ_INSERT_TAIL(&guest_exec_state
.processes
, gei
, next
);
129 static GuestExecInfo
*guest_exec_info_find(int64_t pid_numeric
)
133 QTAILQ_FOREACH(gei
, &guest_exec_state
.processes
, next
) {
134 if (gei
->pid_numeric
== pid_numeric
) {
142 GuestExecStatus
*qmp_guest_exec_status(int64_t pid
, Error
**err
)
145 GuestExecStatus
*ges
;
147 slog("guest-exec-status called, pid: %u", (uint32_t)pid
);
149 gei
= guest_exec_info_find(pid
);
151 error_setg(err
, QERR_INVALID_PARAMETER
, "pid");
155 ges
= g_new0(GuestExecStatus
, 1);
157 bool finished
= g_atomic_int_get(&gei
->finished
);
159 /* need to wait till output channels are closed
160 * to be sure we captured all output at this point */
161 if (gei
->has_output
) {
162 finished
= finished
&& g_atomic_int_get(&gei
->out
.closed
);
163 finished
= finished
&& g_atomic_int_get(&gei
->err
.closed
);
166 ges
->exited
= finished
;
168 /* Glib has no portable way to parse exit status.
169 * On UNIX, we can get either exit code from normal termination
171 * On Windows, it is either the same exit code or the exception
172 * value for an unhandled exception that caused the process
174 * See MSDN for GetExitCodeProcess() and ntstatus.h for possible
175 * well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV
177 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
178 * https://msdn.microsoft.com/en-us/library/aa260331(v=vs.60).aspx
181 /* Additionally WIN32 does not provide any additional information
182 * on whetherthe child exited or terminated via signal.
183 * We use this simple range check to distingish application exit code
184 * (usually value less then 256) and unhandled exception code with
185 * ntstatus (always value greater then 0xC0000005). */
186 if ((uint32_t)gei
->status
< 0xC0000000U
) {
187 ges
->has_exitcode
= true;
188 ges
->exitcode
= gei
->status
;
190 ges
->has_signal
= true;
191 ges
->signal
= gei
->status
;
194 if (WIFEXITED(gei
->status
)) {
195 ges
->has_exitcode
= true;
196 ges
->exitcode
= WEXITSTATUS(gei
->status
);
197 } else if (WIFSIGNALED(gei
->status
)) {
198 ges
->has_signal
= true;
199 ges
->signal
= WTERMSIG(gei
->status
);
202 if (gei
->out
.length
> 0) {
203 ges
->has_out_data
= true;
204 ges
->out_data
= g_base64_encode(gei
->out
.data
, gei
->out
.length
);
205 g_free(gei
->out
.data
);
206 ges
->has_out_truncated
= gei
->out
.truncated
;
209 if (gei
->err
.length
> 0) {
210 ges
->has_err_data
= true;
211 ges
->err_data
= g_base64_encode(gei
->err
.data
, gei
->err
.length
);
212 g_free(gei
->err
.data
);
213 ges
->has_err_truncated
= gei
->err
.truncated
;
216 QTAILQ_REMOVE(&guest_exec_state
.processes
, gei
, next
);
223 /* Get environment variables or arguments array for execve(). */
224 static char **guest_exec_get_args(const strList
*entry
, bool log
)
227 int count
= 1, i
= 0; /* reserve for NULL terminator */
229 char *str
; /* for logging array of arguments */
232 for (it
= entry
; it
!= NULL
; it
= it
->next
) {
234 str_size
+= 1 + strlen(it
->value
);
237 str
= g_malloc(str_size
);
239 args
= g_malloc(count
* sizeof(char *));
240 for (it
= entry
; it
!= NULL
; it
= it
->next
) {
241 args
[i
++] = it
->value
;
242 pstrcat(str
, str_size
, it
->value
);
244 pstrcat(str
, str_size
, " ");
250 slog("guest-exec called: \"%s\"", str
);
257 static void guest_exec_child_watch(GPid pid
, gint status
, gpointer data
)
259 GuestExecInfo
*gei
= (GuestExecInfo
*)data
;
261 g_debug("guest_exec_child_watch called, pid: %d, status: %u",
262 (int32_t)gpid_to_int64(pid
), (uint32_t)status
);
264 gei
->status
= status
;
265 gei
->finished
= true;
267 g_spawn_close_pid(pid
);
270 /** Reset ignored signals back to default. */
271 static void guest_exec_task_setup(gpointer data
)
273 #if !defined(G_OS_WIN32)
274 struct sigaction sigact
;
276 memset(&sigact
, 0, sizeof(struct sigaction
));
277 sigact
.sa_handler
= SIG_DFL
;
279 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0) {
280 slog("sigaction() failed to reset child process's SIGPIPE: %s",
286 static gboolean
guest_exec_input_watch(GIOChannel
*ch
,
287 GIOCondition cond
, gpointer p_
)
289 GuestExecIOData
*p
= (GuestExecIOData
*)p_
;
290 gsize bytes_written
= 0;
294 /* nothing left to write */
295 if (p
->size
== p
->length
) {
299 status
= g_io_channel_write_chars(ch
, (gchar
*)p
->data
+ p
->length
,
300 p
->size
- p
->length
, &bytes_written
, &gerr
);
302 /* can be not 0 even if not G_IO_STATUS_NORMAL */
303 if (bytes_written
!= 0) {
304 p
->length
+= bytes_written
;
307 /* continue write, our callback will be called again */
308 if (status
== G_IO_STATUS_NORMAL
|| status
== G_IO_STATUS_AGAIN
) {
313 g_warning("qga: i/o error writing to input_data channel: %s",
319 g_io_channel_shutdown(ch
, true, NULL
);
320 g_io_channel_unref(ch
);
321 g_atomic_int_set(&p
->closed
, 1);
327 static gboolean
guest_exec_output_watch(GIOChannel
*ch
,
328 GIOCondition cond
, gpointer p_
)
330 GuestExecIOData
*p
= (GuestExecIOData
*)p_
;
334 if (cond
== G_IO_HUP
|| cond
== G_IO_ERR
) {
338 if (p
->size
== p
->length
) {
340 if (!p
->truncated
&& p
->size
< GUEST_EXEC_MAX_OUTPUT
) {
341 t
= g_try_realloc(p
->data
, p
->size
+ GUEST_EXEC_IO_SIZE
);
344 /* ignore truncated output */
345 gchar buf
[GUEST_EXEC_IO_SIZE
];
348 gstatus
= g_io_channel_read_chars(ch
, buf
, sizeof(buf
),
350 if (gstatus
== G_IO_STATUS_EOF
|| gstatus
== G_IO_STATUS_ERROR
) {
356 p
->size
+= GUEST_EXEC_IO_SIZE
;
360 /* Calling read API once.
361 * On next available data our callback will be called again */
362 gstatus
= g_io_channel_read_chars(ch
, (gchar
*)p
->data
+ p
->length
,
363 p
->size
- p
->length
, &bytes_read
, NULL
);
364 if (gstatus
== G_IO_STATUS_EOF
|| gstatus
== G_IO_STATUS_ERROR
) {
368 p
->length
+= bytes_read
;
373 g_io_channel_unref(ch
);
374 g_atomic_int_set(&p
->closed
, 1);
378 GuestExec
*qmp_guest_exec(const char *path
,
379 bool has_arg
, strList
*arg
,
380 bool has_env
, strList
*env
,
381 bool has_input_data
, const char *input_data
,
382 bool has_capture_output
, bool capture_output
,
386 GuestExec
*ge
= NULL
;
392 gint in_fd
, out_fd
, err_fd
;
393 GIOChannel
*in_ch
, *out_ch
, *err_ch
;
395 bool has_output
= (has_capture_output
&& capture_output
);
397 arglist
.value
= (char *)path
;
398 arglist
.next
= has_arg
? arg
: NULL
;
400 argv
= guest_exec_get_args(&arglist
, true);
401 envp
= has_env
? guest_exec_get_args(env
, false) : NULL
;
403 flags
= G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
;
404 #if GLIB_CHECK_VERSION(2, 33, 2)
405 flags
|= G_SPAWN_SEARCH_PATH_FROM_ENVP
;
408 flags
|= G_SPAWN_STDOUT_TO_DEV_NULL
| G_SPAWN_STDERR_TO_DEV_NULL
;
411 ret
= g_spawn_async_with_pipes(NULL
, argv
, envp
, flags
,
412 guest_exec_task_setup
, NULL
, &pid
, has_input_data
? &in_fd
: NULL
,
413 has_output
? &out_fd
: NULL
, has_output
? &err_fd
: NULL
, &gerr
);
415 error_setg(err
, QERR_QGA_COMMAND_FAILED
, gerr
->message
);
420 ge
= g_new0(GuestExec
, 1);
421 ge
->pid
= gpid_to_int64(pid
);
423 gei
= guest_exec_info_add(pid
);
424 gei
->has_output
= has_output
;
425 g_child_watch_add(pid
, guest_exec_child_watch
, gei
);
427 if (has_input_data
) {
428 gei
->in
.data
= g_base64_decode(input_data
, &gei
->in
.size
);
430 in_ch
= g_io_channel_win32_new_fd(in_fd
);
432 in_ch
= g_io_channel_unix_new(in_fd
);
434 g_io_channel_set_encoding(in_ch
, NULL
, NULL
);
435 g_io_channel_set_buffered(in_ch
, false);
436 g_io_channel_set_flags(in_ch
, G_IO_FLAG_NONBLOCK
, NULL
);
437 g_io_add_watch(in_ch
, G_IO_OUT
, guest_exec_input_watch
, &gei
->in
);
442 out_ch
= g_io_channel_win32_new_fd(out_fd
);
443 err_ch
= g_io_channel_win32_new_fd(err_fd
);
445 out_ch
= g_io_channel_unix_new(out_fd
);
446 err_ch
= g_io_channel_unix_new(err_fd
);
448 g_io_channel_set_encoding(out_ch
, NULL
, NULL
);
449 g_io_channel_set_encoding(err_ch
, NULL
, NULL
);
450 g_io_channel_set_buffered(out_ch
, false);
451 g_io_channel_set_buffered(err_ch
, false);
452 g_io_add_watch(out_ch
, G_IO_IN
| G_IO_HUP
,
453 guest_exec_output_watch
, &gei
->out
);
454 g_io_add_watch(err_ch
, G_IO_IN
| G_IO_HUP
,
455 guest_exec_output_watch
, &gei
->err
);