linux-user: add open() hijack infrastructure
[qemu/agraf.git] / qga / guest-agent-commands.c
bloba09c8ca2307bf65add6588d4258ae8fbd2957640
1 /*
2 * QEMU Guest Agent commands
4 * Copyright IBM Corp. 2011
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 <glib.h>
15 #if defined(__linux__)
16 #include <mntent.h>
17 #include <linux/fs.h>
19 #if defined(__linux__) && defined(FIFREEZE)
20 #define CONFIG_FSFREEZE
21 #endif
22 #endif
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include "qga/guest-agent-core.h"
27 #include "qga-qmp-commands.h"
28 #include "qerror.h"
29 #include "qemu-queue.h"
31 static GAState *ga_state;
33 /* Note: in some situations, like with the fsfreeze, logging may be
34 * temporarilly disabled. if it is necessary that a command be able
35 * to log for accounting purposes, check ga_logging_enabled() beforehand,
36 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
38 static void slog(const char *fmt, ...)
40 va_list ap;
42 va_start(ap, fmt);
43 g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
44 va_end(ap);
47 int64_t qmp_guest_sync(int64_t id, Error **errp)
49 return id;
52 void qmp_guest_ping(Error **err)
54 slog("guest-ping called");
57 struct GuestAgentInfo *qmp_guest_info(Error **err)
59 GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
60 GuestAgentCommandInfo *cmd_info;
61 GuestAgentCommandInfoList *cmd_info_list;
62 char **cmd_list_head, **cmd_list;
64 info->version = g_strdup(QGA_VERSION);
66 cmd_list_head = cmd_list = qmp_get_command_list();
67 if (*cmd_list_head == NULL) {
68 goto out;
71 while (*cmd_list) {
72 cmd_info = g_malloc0(sizeof(GuestAgentCommandInfo));
73 cmd_info->name = strdup(*cmd_list);
74 cmd_info->enabled = qmp_command_is_enabled(cmd_info->name);
76 cmd_info_list = g_malloc0(sizeof(GuestAgentCommandInfoList));
77 cmd_info_list->value = cmd_info;
78 cmd_info_list->next = info->supported_commands;
79 info->supported_commands = cmd_info_list;
81 g_free(*cmd_list);
82 cmd_list++;
85 out:
86 g_free(cmd_list_head);
87 return info;
90 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
92 int ret;
93 const char *shutdown_flag;
95 slog("guest-shutdown called, mode: %s", mode);
96 if (!has_mode || strcmp(mode, "powerdown") == 0) {
97 shutdown_flag = "-P";
98 } else if (strcmp(mode, "halt") == 0) {
99 shutdown_flag = "-H";
100 } else if (strcmp(mode, "reboot") == 0) {
101 shutdown_flag = "-r";
102 } else {
103 error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
104 "halt|powerdown|reboot");
105 return;
108 ret = fork();
109 if (ret == 0) {
110 /* child, start the shutdown */
111 setsid();
112 fclose(stdin);
113 fclose(stdout);
114 fclose(stderr);
116 ret = execl("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
117 "hypervisor initiated shutdown", (char*)NULL);
118 if (ret) {
119 slog("guest-shutdown failed: %s", strerror(errno));
121 exit(!!ret);
122 } else if (ret < 0) {
123 error_set(err, QERR_UNDEFINED_ERROR);
127 typedef struct GuestFileHandle {
128 uint64_t id;
129 FILE *fh;
130 QTAILQ_ENTRY(GuestFileHandle) next;
131 } GuestFileHandle;
133 static struct {
134 QTAILQ_HEAD(, GuestFileHandle) filehandles;
135 } guest_file_state;
137 static void guest_file_handle_add(FILE *fh)
139 GuestFileHandle *gfh;
141 gfh = g_malloc0(sizeof(GuestFileHandle));
142 gfh->id = fileno(fh);
143 gfh->fh = fh;
144 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
147 static GuestFileHandle *guest_file_handle_find(int64_t id)
149 GuestFileHandle *gfh;
151 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
153 if (gfh->id == id) {
154 return gfh;
158 return NULL;
161 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
163 FILE *fh;
164 int fd;
165 int64_t ret = -1;
167 if (!has_mode) {
168 mode = "r";
170 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
171 fh = fopen(path, mode);
172 if (!fh) {
173 error_set(err, QERR_OPEN_FILE_FAILED, path);
174 return -1;
177 /* set fd non-blocking to avoid common use cases (like reading from a
178 * named pipe) from hanging the agent
180 fd = fileno(fh);
181 ret = fcntl(fd, F_GETFL);
182 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
183 if (ret == -1) {
184 error_set(err, QERR_QGA_COMMAND_FAILED, "fcntl() failed");
185 fclose(fh);
186 return -1;
189 guest_file_handle_add(fh);
190 slog("guest-file-open, handle: %d", fd);
191 return fd;
194 void qmp_guest_file_close(int64_t handle, Error **err)
196 GuestFileHandle *gfh = guest_file_handle_find(handle);
197 int ret;
199 slog("guest-file-close called, handle: %ld", handle);
200 if (!gfh) {
201 error_set(err, QERR_FD_NOT_FOUND, "handle");
202 return;
205 ret = fclose(gfh->fh);
206 if (ret == -1) {
207 error_set(err, QERR_QGA_COMMAND_FAILED, "fclose() failed");
208 return;
211 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
212 g_free(gfh);
215 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
216 int64_t count, Error **err)
218 GuestFileHandle *gfh = guest_file_handle_find(handle);
219 GuestFileRead *read_data = NULL;
220 guchar *buf;
221 FILE *fh;
222 size_t read_count;
224 if (!gfh) {
225 error_set(err, QERR_FD_NOT_FOUND, "handle");
226 return NULL;
229 if (!has_count) {
230 count = QGA_READ_COUNT_DEFAULT;
231 } else if (count < 0) {
232 error_set(err, QERR_INVALID_PARAMETER, "count");
233 return NULL;
236 fh = gfh->fh;
237 buf = g_malloc0(count+1);
238 read_count = fread(buf, 1, count, fh);
239 if (ferror(fh)) {
240 slog("guest-file-read failed, handle: %ld", handle);
241 error_set(err, QERR_QGA_COMMAND_FAILED, "fread() failed");
242 } else {
243 buf[read_count] = 0;
244 read_data = g_malloc0(sizeof(GuestFileRead));
245 read_data->count = read_count;
246 read_data->eof = feof(fh);
247 if (read_count) {
248 read_data->buf_b64 = g_base64_encode(buf, read_count);
251 g_free(buf);
252 clearerr(fh);
254 return read_data;
257 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
258 bool has_count, int64_t count, Error **err)
260 GuestFileWrite *write_data = NULL;
261 guchar *buf;
262 gsize buf_len;
263 int write_count;
264 GuestFileHandle *gfh = guest_file_handle_find(handle);
265 FILE *fh;
267 if (!gfh) {
268 error_set(err, QERR_FD_NOT_FOUND, "handle");
269 return NULL;
272 fh = gfh->fh;
273 buf = g_base64_decode(buf_b64, &buf_len);
275 if (!has_count) {
276 count = buf_len;
277 } else if (count < 0 || count > buf_len) {
278 g_free(buf);
279 error_set(err, QERR_INVALID_PARAMETER, "count");
280 return NULL;
283 write_count = fwrite(buf, 1, count, fh);
284 if (ferror(fh)) {
285 slog("guest-file-write failed, handle: %ld", handle);
286 error_set(err, QERR_QGA_COMMAND_FAILED, "fwrite() error");
287 } else {
288 write_data = g_malloc0(sizeof(GuestFileWrite));
289 write_data->count = write_count;
290 write_data->eof = feof(fh);
292 g_free(buf);
293 clearerr(fh);
295 return write_data;
298 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
299 int64_t whence, Error **err)
301 GuestFileHandle *gfh = guest_file_handle_find(handle);
302 GuestFileSeek *seek_data = NULL;
303 FILE *fh;
304 int ret;
306 if (!gfh) {
307 error_set(err, QERR_FD_NOT_FOUND, "handle");
308 return NULL;
311 fh = gfh->fh;
312 ret = fseek(fh, offset, whence);
313 if (ret == -1) {
314 error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
315 } else {
316 seek_data = g_malloc0(sizeof(GuestFileRead));
317 seek_data->position = ftell(fh);
318 seek_data->eof = feof(fh);
320 clearerr(fh);
322 return seek_data;
325 void qmp_guest_file_flush(int64_t handle, Error **err)
327 GuestFileHandle *gfh = guest_file_handle_find(handle);
328 FILE *fh;
329 int ret;
331 if (!gfh) {
332 error_set(err, QERR_FD_NOT_FOUND, "handle");
333 return;
336 fh = gfh->fh;
337 ret = fflush(fh);
338 if (ret == EOF) {
339 error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
343 static void guest_file_init(void)
345 QTAILQ_INIT(&guest_file_state.filehandles);
348 #if defined(CONFIG_FSFREEZE)
349 static void disable_logging(void)
351 ga_disable_logging(ga_state);
354 static void enable_logging(void)
356 ga_enable_logging(ga_state);
359 typedef struct GuestFsfreezeMount {
360 char *dirname;
361 char *devtype;
362 QTAILQ_ENTRY(GuestFsfreezeMount) next;
363 } GuestFsfreezeMount;
365 struct {
366 GuestFsfreezeStatus status;
367 QTAILQ_HEAD(, GuestFsfreezeMount) mount_list;
368 } guest_fsfreeze_state;
371 * Walk the mount table and build a list of local file systems
373 static int guest_fsfreeze_build_mount_list(void)
375 struct mntent *ment;
376 GuestFsfreezeMount *mount, *temp;
377 char const *mtab = MOUNTED;
378 FILE *fp;
380 QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
381 QTAILQ_REMOVE(&guest_fsfreeze_state.mount_list, mount, next);
382 g_free(mount->dirname);
383 g_free(mount->devtype);
384 g_free(mount);
387 fp = setmntent(mtab, "r");
388 if (!fp) {
389 g_warning("fsfreeze: unable to read mtab");
390 return -1;
393 while ((ment = getmntent(fp))) {
395 * An entry which device name doesn't start with a '/' is
396 * either a dummy file system or a network file system.
397 * Add special handling for smbfs and cifs as is done by
398 * coreutils as well.
400 if ((ment->mnt_fsname[0] != '/') ||
401 (strcmp(ment->mnt_type, "smbfs") == 0) ||
402 (strcmp(ment->mnt_type, "cifs") == 0)) {
403 continue;
406 mount = g_malloc0(sizeof(GuestFsfreezeMount));
407 mount->dirname = g_strdup(ment->mnt_dir);
408 mount->devtype = g_strdup(ment->mnt_type);
410 QTAILQ_INSERT_TAIL(&guest_fsfreeze_state.mount_list, mount, next);
413 endmntent(fp);
415 return 0;
419 * Return status of freeze/thaw
421 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
423 return guest_fsfreeze_state.status;
427 * Walk list of mounted file systems in the guest, and freeze the ones which
428 * are real local file systems.
430 int64_t qmp_guest_fsfreeze_freeze(Error **err)
432 int ret = 0, i = 0;
433 struct GuestFsfreezeMount *mount, *temp;
434 int fd;
435 char err_msg[512];
437 slog("guest-fsfreeze called");
439 if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
440 return 0;
443 ret = guest_fsfreeze_build_mount_list();
444 if (ret < 0) {
445 return ret;
448 /* cannot risk guest agent blocking itself on a write in this state */
449 disable_logging();
451 QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
452 fd = qemu_open(mount->dirname, O_RDONLY);
453 if (fd == -1) {
454 sprintf(err_msg, "failed to open %s, %s", mount->dirname, strerror(errno));
455 error_set(err, QERR_QGA_COMMAND_FAILED, err_msg);
456 goto error;
459 /* we try to cull filesytems we know won't work in advance, but other
460 * filesytems may not implement fsfreeze for less obvious reasons.
461 * these will report EOPNOTSUPP, so we simply ignore them. when
462 * thawing, these filesystems will return an EINVAL instead, due to
463 * not being in a frozen state. Other filesystem-specific
464 * errors may result in EINVAL, however, so the user should check the
465 * number * of filesystems returned here against those returned by the
466 * thaw operation to determine whether everything completed
467 * successfully
469 ret = ioctl(fd, FIFREEZE);
470 if (ret < 0 && errno != EOPNOTSUPP) {
471 sprintf(err_msg, "failed to freeze %s, %s", mount->dirname, strerror(errno));
472 error_set(err, QERR_QGA_COMMAND_FAILED, err_msg);
473 close(fd);
474 goto error;
476 close(fd);
478 i++;
481 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_FROZEN;
482 return i;
484 error:
485 if (i > 0) {
486 qmp_guest_fsfreeze_thaw(NULL);
488 return 0;
492 * Walk list of frozen file systems in the guest, and thaw them.
494 int64_t qmp_guest_fsfreeze_thaw(Error **err)
496 int ret;
497 GuestFsfreezeMount *mount, *temp;
498 int fd, i = 0;
499 bool has_error = false;
501 QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
502 fd = qemu_open(mount->dirname, O_RDONLY);
503 if (fd == -1) {
504 has_error = true;
505 continue;
507 ret = ioctl(fd, FITHAW);
508 if (ret < 0 && errno != EOPNOTSUPP && errno != EINVAL) {
509 has_error = true;
510 close(fd);
511 continue;
513 close(fd);
514 i++;
517 if (has_error) {
518 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_ERROR;
519 } else {
520 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
522 enable_logging();
523 return i;
526 static void guest_fsfreeze_init(void)
528 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
529 QTAILQ_INIT(&guest_fsfreeze_state.mount_list);
532 static void guest_fsfreeze_cleanup(void)
534 int64_t ret;
535 Error *err = NULL;
537 if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
538 ret = qmp_guest_fsfreeze_thaw(&err);
539 if (ret < 0 || err) {
540 slog("failed to clean up frozen filesystems");
544 #else
546 * Return status of freeze/thaw
548 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
550 error_set(err, QERR_UNSUPPORTED);
552 return 0;
556 * Walk list of mounted file systems in the guest, and freeze the ones which
557 * are real local file systems.
559 int64_t qmp_guest_fsfreeze_freeze(Error **err)
561 error_set(err, QERR_UNSUPPORTED);
563 return 0;
567 * Walk list of frozen file systems in the guest, and thaw them.
569 int64_t qmp_guest_fsfreeze_thaw(Error **err)
571 error_set(err, QERR_UNSUPPORTED);
573 return 0;
575 #endif
577 /* register init/cleanup routines for stateful command groups */
578 void ga_command_state_init(GAState *s, GACommandState *cs)
580 ga_state = s;
581 #if defined(CONFIG_FSFREEZE)
582 ga_command_state_add(cs, guest_fsfreeze_init, guest_fsfreeze_cleanup);
583 #endif
584 ga_command_state_add(cs, guest_file_init, NULL);