Merge remote-tracking branch 'origin/master' into staging
[qemu.git] / qga / guest-agent-commands.c
blob8c0d67ee283b09692752abeb3bf961cb0969c9bc
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>
14 #include <mntent.h>
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <linux/fs.h>
18 #include "qga/guest-agent-core.h"
19 #include "qga-qmp-commands.h"
20 #include "qerror.h"
21 #include "qemu-queue.h"
23 static GAState *ga_state;
25 static void disable_logging(void)
27 ga_disable_logging(ga_state);
30 static void enable_logging(void)
32 ga_enable_logging(ga_state);
35 /* Note: in some situations, like with the fsfreeze, logging may be
36 * temporarilly disabled. if it is necessary that a command be able
37 * to log for accounting purposes, check ga_logging_enabled() beforehand,
38 * and use the QERR_QGA_LOGGING_DISABLED to generate an error
40 static void slog(const char *fmt, ...)
42 va_list ap;
44 va_start(ap, fmt);
45 g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
46 va_end(ap);
49 int64_t qmp_guest_sync(int64_t id, Error **errp)
51 return id;
54 void qmp_guest_ping(Error **err)
56 slog("guest-ping called");
59 struct GuestAgentInfo *qmp_guest_info(Error **err)
61 GuestAgentInfo *info = qemu_mallocz(sizeof(GuestAgentInfo));
63 info->version = g_strdup(QGA_VERSION);
65 return info;
68 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
70 int ret;
71 const char *shutdown_flag;
73 slog("guest-shutdown called, mode: %s", mode);
74 if (!has_mode || strcmp(mode, "powerdown") == 0) {
75 shutdown_flag = "-P";
76 } else if (strcmp(mode, "halt") == 0) {
77 shutdown_flag = "-H";
78 } else if (strcmp(mode, "reboot") == 0) {
79 shutdown_flag = "-r";
80 } else {
81 error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
82 "halt|powerdown|reboot");
83 return;
86 ret = fork();
87 if (ret == 0) {
88 /* child, start the shutdown */
89 setsid();
90 fclose(stdin);
91 fclose(stdout);
92 fclose(stderr);
94 ret = execl("/sbin/shutdown", "shutdown", shutdown_flag, "+0",
95 "hypervisor initiated shutdown", (char*)NULL);
96 if (ret) {
97 slog("guest-shutdown failed: %s", strerror(errno));
99 exit(!!ret);
100 } else if (ret < 0) {
101 error_set(err, QERR_UNDEFINED_ERROR);
105 typedef struct GuestFileHandle {
106 uint64_t id;
107 FILE *fh;
108 QTAILQ_ENTRY(GuestFileHandle) next;
109 } GuestFileHandle;
111 static struct {
112 QTAILQ_HEAD(, GuestFileHandle) filehandles;
113 } guest_file_state;
115 static void guest_file_handle_add(FILE *fh)
117 GuestFileHandle *gfh;
119 gfh = qemu_mallocz(sizeof(GuestFileHandle));
120 gfh->id = fileno(fh);
121 gfh->fh = fh;
122 QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
125 static GuestFileHandle *guest_file_handle_find(int64_t id)
127 GuestFileHandle *gfh;
129 QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
131 if (gfh->id == id) {
132 return gfh;
136 return NULL;
139 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
141 FILE *fh;
142 int fd;
143 int64_t ret = -1;
145 if (!has_mode) {
146 mode = "r";
148 slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
149 fh = fopen(path, mode);
150 if (!fh) {
151 error_set(err, QERR_OPEN_FILE_FAILED, path);
152 return -1;
155 /* set fd non-blocking to avoid common use cases (like reading from a
156 * named pipe) from hanging the agent
158 fd = fileno(fh);
159 ret = fcntl(fd, F_GETFL);
160 ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
161 if (ret == -1) {
162 error_set(err, QERR_QGA_COMMAND_FAILED, "fcntl() failed");
163 fclose(fh);
164 return -1;
167 guest_file_handle_add(fh);
168 slog("guest-file-open, handle: %d", fd);
169 return fd;
172 void qmp_guest_file_close(int64_t handle, Error **err)
174 GuestFileHandle *gfh = guest_file_handle_find(handle);
175 int ret;
177 slog("guest-file-close called, handle: %ld", handle);
178 if (!gfh) {
179 error_set(err, QERR_FD_NOT_FOUND, "handle");
180 return;
183 ret = fclose(gfh->fh);
184 if (ret == -1) {
185 error_set(err, QERR_QGA_COMMAND_FAILED, "fclose() failed");
186 return;
189 QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
190 qemu_free(gfh);
193 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
194 int64_t count, Error **err)
196 GuestFileHandle *gfh = guest_file_handle_find(handle);
197 GuestFileRead *read_data = NULL;
198 guchar *buf;
199 FILE *fh;
200 size_t read_count;
202 if (!gfh) {
203 error_set(err, QERR_FD_NOT_FOUND, "handle");
204 return NULL;
207 if (!has_count) {
208 count = QGA_READ_COUNT_DEFAULT;
209 } else if (count < 0) {
210 error_set(err, QERR_INVALID_PARAMETER, "count");
211 return NULL;
214 fh = gfh->fh;
215 buf = qemu_mallocz(count+1);
216 read_count = fread(buf, 1, count, fh);
217 if (ferror(fh)) {
218 slog("guest-file-read failed, handle: %ld", handle);
219 error_set(err, QERR_QGA_COMMAND_FAILED, "fread() failed");
220 } else {
221 buf[read_count] = 0;
222 read_data = qemu_mallocz(sizeof(GuestFileRead));
223 read_data->count = read_count;
224 read_data->eof = feof(fh);
225 if (read_count) {
226 read_data->buf_b64 = g_base64_encode(buf, read_count);
229 qemu_free(buf);
230 clearerr(fh);
232 return read_data;
235 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
236 bool has_count, int64_t count, Error **err)
238 GuestFileWrite *write_data = NULL;
239 guchar *buf;
240 gsize buf_len;
241 int write_count;
242 GuestFileHandle *gfh = guest_file_handle_find(handle);
243 FILE *fh;
245 if (!gfh) {
246 error_set(err, QERR_FD_NOT_FOUND, "handle");
247 return NULL;
250 fh = gfh->fh;
251 buf = g_base64_decode(buf_b64, &buf_len);
253 if (!has_count) {
254 count = buf_len;
255 } else if (count < 0 || count > buf_len) {
256 qemu_free(buf);
257 error_set(err, QERR_INVALID_PARAMETER, "count");
258 return NULL;
261 write_count = fwrite(buf, 1, count, fh);
262 if (ferror(fh)) {
263 slog("guest-file-write failed, handle: %ld", handle);
264 error_set(err, QERR_QGA_COMMAND_FAILED, "fwrite() error");
265 } else {
266 write_data = qemu_mallocz(sizeof(GuestFileWrite));
267 write_data->count = write_count;
268 write_data->eof = feof(fh);
270 qemu_free(buf);
271 clearerr(fh);
273 return write_data;
276 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
277 int64_t whence, Error **err)
279 GuestFileHandle *gfh = guest_file_handle_find(handle);
280 GuestFileSeek *seek_data = NULL;
281 FILE *fh;
282 int ret;
284 if (!gfh) {
285 error_set(err, QERR_FD_NOT_FOUND, "handle");
286 return NULL;
289 fh = gfh->fh;
290 ret = fseek(fh, offset, whence);
291 if (ret == -1) {
292 error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
293 } else {
294 seek_data = qemu_mallocz(sizeof(GuestFileRead));
295 seek_data->position = ftell(fh);
296 seek_data->eof = feof(fh);
298 clearerr(fh);
300 return seek_data;
303 void qmp_guest_file_flush(int64_t handle, Error **err)
305 GuestFileHandle *gfh = guest_file_handle_find(handle);
306 FILE *fh;
307 int ret;
309 if (!gfh) {
310 error_set(err, QERR_FD_NOT_FOUND, "handle");
311 return;
314 fh = gfh->fh;
315 ret = fflush(fh);
316 if (ret == EOF) {
317 error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
321 static void guest_file_init(void)
323 QTAILQ_INIT(&guest_file_state.filehandles);
326 typedef struct GuestFsfreezeMount {
327 char *dirname;
328 char *devtype;
329 QTAILQ_ENTRY(GuestFsfreezeMount) next;
330 } GuestFsfreezeMount;
332 struct {
333 GuestFsfreezeStatus status;
334 QTAILQ_HEAD(, GuestFsfreezeMount) mount_list;
335 } guest_fsfreeze_state;
338 * Walk the mount table and build a list of local file systems
340 static int guest_fsfreeze_build_mount_list(void)
342 struct mntent *ment;
343 GuestFsfreezeMount *mount, *temp;
344 char const *mtab = MOUNTED;
345 FILE *fp;
347 QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
348 QTAILQ_REMOVE(&guest_fsfreeze_state.mount_list, mount, next);
349 qemu_free(mount->dirname);
350 qemu_free(mount->devtype);
351 qemu_free(mount);
354 fp = setmntent(mtab, "r");
355 if (!fp) {
356 g_warning("fsfreeze: unable to read mtab");
357 return -1;
360 while ((ment = getmntent(fp))) {
362 * An entry which device name doesn't start with a '/' is
363 * either a dummy file system or a network file system.
364 * Add special handling for smbfs and cifs as is done by
365 * coreutils as well.
367 if ((ment->mnt_fsname[0] != '/') ||
368 (strcmp(ment->mnt_type, "smbfs") == 0) ||
369 (strcmp(ment->mnt_type, "cifs") == 0)) {
370 continue;
373 mount = qemu_mallocz(sizeof(GuestFsfreezeMount));
374 mount->dirname = qemu_strdup(ment->mnt_dir);
375 mount->devtype = qemu_strdup(ment->mnt_type);
377 QTAILQ_INSERT_TAIL(&guest_fsfreeze_state.mount_list, mount, next);
380 endmntent(fp);
382 return 0;
386 * Return status of freeze/thaw
388 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
390 return guest_fsfreeze_state.status;
394 * Walk list of mounted file systems in the guest, and freeze the ones which
395 * are real local file systems.
397 int64_t qmp_guest_fsfreeze_freeze(Error **err)
399 int ret = 0, i = 0;
400 struct GuestFsfreezeMount *mount, *temp;
401 int fd;
402 char err_msg[512];
404 slog("guest-fsfreeze called");
406 if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
407 return 0;
410 ret = guest_fsfreeze_build_mount_list();
411 if (ret < 0) {
412 return ret;
415 /* cannot risk guest agent blocking itself on a write in this state */
416 disable_logging();
418 QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
419 fd = qemu_open(mount->dirname, O_RDONLY);
420 if (fd == -1) {
421 sprintf(err_msg, "failed to open %s, %s", mount->dirname, strerror(errno));
422 error_set(err, QERR_QGA_COMMAND_FAILED, err_msg);
423 goto error;
426 /* we try to cull filesytems we know won't work in advance, but other
427 * filesytems may not implement fsfreeze for less obvious reasons.
428 * these will report EOPNOTSUPP, so we simply ignore them. when
429 * thawing, these filesystems will return an EINVAL instead, due to
430 * not being in a frozen state. Other filesystem-specific
431 * errors may result in EINVAL, however, so the user should check the
432 * number * of filesystems returned here against those returned by the
433 * thaw operation to determine whether everything completed
434 * successfully
436 ret = ioctl(fd, FIFREEZE);
437 if (ret < 0 && errno != EOPNOTSUPP) {
438 sprintf(err_msg, "failed to freeze %s, %s", mount->dirname, strerror(errno));
439 error_set(err, QERR_QGA_COMMAND_FAILED, err_msg);
440 close(fd);
441 goto error;
443 close(fd);
445 i++;
448 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_FROZEN;
449 return i;
451 error:
452 if (i > 0) {
453 qmp_guest_fsfreeze_thaw(NULL);
455 return 0;
459 * Walk list of frozen file systems in the guest, and thaw them.
461 int64_t qmp_guest_fsfreeze_thaw(Error **err)
463 int ret;
464 GuestFsfreezeMount *mount, *temp;
465 int fd, i = 0;
466 bool has_error = false;
468 QTAILQ_FOREACH_SAFE(mount, &guest_fsfreeze_state.mount_list, next, temp) {
469 fd = qemu_open(mount->dirname, O_RDONLY);
470 if (fd == -1) {
471 has_error = true;
472 continue;
474 ret = ioctl(fd, FITHAW);
475 if (ret < 0 && errno != EOPNOTSUPP && errno != EINVAL) {
476 has_error = true;
477 close(fd);
478 continue;
480 close(fd);
481 i++;
484 if (has_error) {
485 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_ERROR;
486 } else {
487 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
489 enable_logging();
490 return i;
493 static void guest_fsfreeze_init(void)
495 guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
496 QTAILQ_INIT(&guest_fsfreeze_state.mount_list);
499 static void guest_fsfreeze_cleanup(void)
501 int64_t ret;
502 Error *err = NULL;
504 if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
505 ret = qmp_guest_fsfreeze_thaw(&err);
506 if (ret < 0 || err) {
507 slog("failed to clean up frozen filesystems");
512 /* register init/cleanup routines for stateful command groups */
513 void ga_command_state_init(GAState *s, GACommandState *cs)
515 ga_state = s;
516 ga_command_state_add(cs, guest_fsfreeze_init, guest_fsfreeze_cleanup);
517 ga_command_state_add(cs, guest_file_init, NULL);