s390x/cpumodel: generate CPU feature group lists
[qemu.git] / qga / commands.c
blob50fd26a817a94454da138846a9c4c578effba288
1 /*
2 * QEMU Guest Agent common/cross-platform command implementations
4 * Copyright IBM Corp. 2012
6 * Authors:
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, ...)
32 va_list ap;
34 va_start(ap, fmt);
35 g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
36 va_end(ap);
39 int64_t qmp_guest_sync_delimited(int64_t id, Error **errp)
41 ga_set_response_delimited(ga_state);
42 return id;
45 int64_t qmp_guest_sync(int64_t id, Error **errp)
47 return id;
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);
78 return info;
81 struct GuestExecIOData {
82 guchar *data;
83 gsize size;
84 gsize length;
85 gint closed;
86 bool truncated;
87 const char *name;
89 typedef struct GuestExecIOData GuestExecIOData;
91 struct GuestExecInfo {
92 GPid pid;
93 int64_t pid_numeric;
94 gint status;
95 bool has_output;
96 gint finished;
97 GuestExecIOData in;
98 GuestExecIOData out;
99 GuestExecIOData err;
100 QTAILQ_ENTRY(GuestExecInfo) next;
102 typedef struct GuestExecInfo GuestExecInfo;
104 static struct {
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)
112 #ifdef G_OS_WIN32
113 return GetProcessId(pid);
114 #else
115 return (int64_t)pid;
116 #endif
119 static GuestExecInfo *guest_exec_info_add(GPid pid)
121 GuestExecInfo *gei;
123 gei = g_new0(GuestExecInfo, 1);
124 gei->pid = pid;
125 gei->pid_numeric = gpid_to_int64(pid);
126 QTAILQ_INSERT_TAIL(&guest_exec_state.processes, gei, next);
128 return gei;
131 static GuestExecInfo *guest_exec_info_find(int64_t pid_numeric)
133 GuestExecInfo *gei;
135 QTAILQ_FOREACH(gei, &guest_exec_state.processes, next) {
136 if (gei->pid_numeric == pid_numeric) {
137 return gei;
141 return NULL;
144 GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **err)
146 GuestExecInfo *gei;
147 GuestExecStatus *ges;
149 slog("guest-exec-status called, pid: %u", (uint32_t)pid);
151 gei = guest_exec_info_find(pid);
152 if (gei == NULL) {
153 error_setg(err, QERR_INVALID_PARAMETER, "pid");
154 return NULL;
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;
169 if (finished) {
170 /* Glib has no portable way to parse exit status.
171 * On UNIX, we can get either exit code from normal termination
172 * or signal number.
173 * On Windows, it is either the same exit code or the exception
174 * value for an unhandled exception that caused the process
175 * to terminate.
176 * See MSDN for GetExitCodeProcess() and ntstatus.h for possible
177 * well-known codes, e.g. C0000005 ACCESS_DENIED - analog of SIGSEGV
178 * References:
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
182 #ifdef G_OS_WIN32
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;
191 } else {
192 ges->has_signal = true;
193 ges->signal = gei->status;
195 #else
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);
203 #endif
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);
219 g_free(gei);
222 return ges;
225 /* Get environment variables or arguments array for execve(). */
226 static char **guest_exec_get_args(const strList *entry, bool log)
228 const strList *it;
229 int count = 1, i = 0; /* reserve for NULL terminator */
230 char **args;
231 char *str; /* for logging array of arguments */
232 size_t str_size = 1;
234 for (it = entry; it != NULL; it = it->next) {
235 count++;
236 str_size += 1 + strlen(it->value);
239 str = g_malloc(str_size);
240 *str = 0;
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);
245 if (it->next) {
246 pstrcat(str, str_size, " ");
249 args[i] = NULL;
251 if (log) {
252 slog("guest-exec called: \"%s\"", str);
254 g_free(str);
256 return args;
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",
283 strerror(errno));
285 #endif
288 static gboolean guest_exec_input_watch(GIOChannel *ch,
289 GIOCondition cond, gpointer p_)
291 GuestExecIOData *p = (GuestExecIOData *)p_;
292 gsize bytes_written = 0;
293 GIOStatus status;
294 GError *gerr = NULL;
296 /* nothing left to write */
297 if (p->size == p->length) {
298 goto done;
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) {
311 return true;
314 if (gerr) {
315 g_warning("qga: i/o error writing to input_data channel: %s",
316 gerr->message);
317 g_error_free(gerr);
320 done:
321 g_io_channel_shutdown(ch, true, NULL);
322 g_io_channel_unref(ch);
323 g_atomic_int_set(&p->closed, 1);
324 g_free(p->data);
326 return false;
329 static gboolean guest_exec_output_watch(GIOChannel *ch,
330 GIOCondition cond, gpointer p_)
332 GuestExecIOData *p = (GuestExecIOData *)p_;
333 gsize bytes_read;
334 GIOStatus gstatus;
336 if (cond == G_IO_HUP || cond == G_IO_ERR) {
337 goto close;
340 if (p->size == p->length) {
341 gpointer t = NULL;
342 if (!p->truncated && p->size < GUEST_EXEC_MAX_OUTPUT) {
343 t = g_try_realloc(p->data, p->size + GUEST_EXEC_IO_SIZE);
345 if (t == NULL) {
346 /* ignore truncated output */
347 gchar buf[GUEST_EXEC_IO_SIZE];
349 p->truncated = true;
350 gstatus = g_io_channel_read_chars(ch, buf, sizeof(buf),
351 &bytes_read, NULL);
352 if (gstatus == G_IO_STATUS_EOF || gstatus == G_IO_STATUS_ERROR) {
353 goto close;
356 return true;
358 p->size += GUEST_EXEC_IO_SIZE;
359 p->data = t;
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) {
367 goto close;
370 p->length += bytes_read;
372 return true;
374 close:
375 g_io_channel_shutdown(ch, true, NULL);
376 g_io_channel_unref(ch);
377 g_atomic_int_set(&p->closed, 1);
378 return false;
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,
386 Error **err)
388 GPid pid;
389 GuestExec *ge = NULL;
390 GuestExecInfo *gei;
391 char **argv, **envp;
392 strList arglist;
393 gboolean ret;
394 GError *gerr = NULL;
395 gint in_fd, out_fd, err_fd;
396 GIOChannel *in_ch, *out_ch, *err_ch;
397 GSpawnFlags flags;
398 bool has_output = (has_capture_output && capture_output);
399 uint8_t *input = NULL;
400 size_t ninput = 0;
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);
407 if (!input) {
408 return NULL;
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;
418 #endif
419 if (!has_output) {
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);
426 if (!ret) {
427 error_setg(err, QERR_QGA_COMMAND_FAILED, gerr->message);
428 g_error_free(gerr);
429 goto done;
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;
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);
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);
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);
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);
474 done:
475 g_free(argv);
476 g_free(envp);
478 return ge;
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) {
491 case QGA_SEEK_SET:
492 return SEEK_SET;
493 case QGA_SEEK_CUR:
494 return SEEK_CUR;
495 case QGA_SEEK_END:
496 return SEEK_END;
498 error_setg(errp, "invalid whence code %"PRId64, whence->u.value);
499 return -1;