]> Git Repo - qemu.git/blob - qga/guest-agent-commands.c
kvmvapic: Introduce TPR access optimization for Windows guests
[qemu.git] / qga / guest-agent-commands.c
1 /*
2  * QEMU Guest Agent commands
3  *
4  * Copyright IBM Corp. 2011
5  *
6  * Authors:
7  *  Michael Roth      <[email protected]>
8  *
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.
11  */
12
13 #include <glib.h>
14
15 #if defined(__linux__)
16 #include <mntent.h>
17 #include <linux/fs.h>
18
19 #if defined(__linux__) && defined(FIFREEZE)
20 #define CONFIG_FSFREEZE
21 #endif
22 #endif
23
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"
30
31 static GAState *ga_state;
32
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
37  */
38 static void slog(const char *fmt, ...)
39 {
40     va_list ap;
41
42     va_start(ap, fmt);
43     g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
44     va_end(ap);
45 }
46
47 int64_t qmp_guest_sync(int64_t id, Error **errp)
48 {
49     return id;
50 }
51
52 void qmp_guest_ping(Error **err)
53 {
54     slog("guest-ping called");
55 }
56
57 struct GuestAgentInfo *qmp_guest_info(Error **err)
58 {
59     GuestAgentInfo *info = g_malloc0(sizeof(GuestAgentInfo));
60     GuestAgentCommandInfo *cmd_info;
61     GuestAgentCommandInfoList *cmd_info_list;
62     char **cmd_list_head, **cmd_list;
63
64     info->version = g_strdup(QGA_VERSION);
65
66     cmd_list_head = cmd_list = qmp_get_command_list();
67     if (*cmd_list_head == NULL) {
68         goto out;
69     }
70
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);
75
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;
80
81         g_free(*cmd_list);
82         cmd_list++;
83     }
84
85 out:
86     g_free(cmd_list_head);
87     return info;
88 }
89
90 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
91 {
92     int ret;
93     const char *shutdown_flag;
94
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;
106     }
107
108     ret = fork();
109     if (ret == 0) {
110         /* child, start the shutdown */
111         setsid();
112         fclose(stdin);
113         fclose(stdout);
114         fclose(stderr);
115
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));
120         }
121         exit(!!ret);
122     } else if (ret < 0) {
123         error_set(err, QERR_UNDEFINED_ERROR);
124     }
125 }
126
127 typedef struct GuestFileHandle {
128     uint64_t id;
129     FILE *fh;
130     QTAILQ_ENTRY(GuestFileHandle) next;
131 } GuestFileHandle;
132
133 static struct {
134     QTAILQ_HEAD(, GuestFileHandle) filehandles;
135 } guest_file_state;
136
137 static void guest_file_handle_add(FILE *fh)
138 {
139     GuestFileHandle *gfh;
140
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);
145 }
146
147 static GuestFileHandle *guest_file_handle_find(int64_t id)
148 {
149     GuestFileHandle *gfh;
150
151     QTAILQ_FOREACH(gfh, &guest_file_state.filehandles, next)
152     {
153         if (gfh->id == id) {
154             return gfh;
155         }
156     }
157
158     return NULL;
159 }
160
161 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
162 {
163     FILE *fh;
164     int fd;
165     int64_t ret = -1;
166
167     if (!has_mode) {
168         mode = "r";
169     }
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;
175     }
176
177     /* set fd non-blocking to avoid common use cases (like reading from a
178      * named pipe) from hanging the agent
179      */
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;
187     }
188
189     guest_file_handle_add(fh);
190     slog("guest-file-open, handle: %d", fd);
191     return fd;
192 }
193
194 void qmp_guest_file_close(int64_t handle, Error **err)
195 {
196     GuestFileHandle *gfh = guest_file_handle_find(handle);
197     int ret;
198
199     slog("guest-file-close called, handle: %ld", handle);
200     if (!gfh) {
201         error_set(err, QERR_FD_NOT_FOUND, "handle");
202         return;
203     }
204
205     ret = fclose(gfh->fh);
206     if (ret == -1) {
207         error_set(err, QERR_QGA_COMMAND_FAILED, "fclose() failed");
208         return;
209     }
210
211     QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
212     g_free(gfh);
213 }
214
215 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
216                                           int64_t count, Error **err)
217 {
218     GuestFileHandle *gfh = guest_file_handle_find(handle);
219     GuestFileRead *read_data = NULL;
220     guchar *buf;
221     FILE *fh;
222     size_t read_count;
223
224     if (!gfh) {
225         error_set(err, QERR_FD_NOT_FOUND, "handle");
226         return NULL;
227     }
228
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;
234     }
235
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);
249         }
250     }
251     g_free(buf);
252     clearerr(fh);
253
254     return read_data;
255 }
256
257 GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
258                                      bool has_count, int64_t count, Error **err)
259 {
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;
266
267     if (!gfh) {
268         error_set(err, QERR_FD_NOT_FOUND, "handle");
269         return NULL;
270     }
271
272     fh = gfh->fh;
273     buf = g_base64_decode(buf_b64, &buf_len);
274
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;
281     }
282
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);
291     }
292     g_free(buf);
293     clearerr(fh);
294
295     return write_data;
296 }
297
298 struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
299                                           int64_t whence, Error **err)
300 {
301     GuestFileHandle *gfh = guest_file_handle_find(handle);
302     GuestFileSeek *seek_data = NULL;
303     FILE *fh;
304     int ret;
305
306     if (!gfh) {
307         error_set(err, QERR_FD_NOT_FOUND, "handle");
308         return NULL;
309     }
310
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);
319     }
320     clearerr(fh);
321
322     return seek_data;
323 }
324
325 void qmp_guest_file_flush(int64_t handle, Error **err)
326 {
327     GuestFileHandle *gfh = guest_file_handle_find(handle);
328     FILE *fh;
329     int ret;
330
331     if (!gfh) {
332         error_set(err, QERR_FD_NOT_FOUND, "handle");
333         return;
334     }
335
336     fh = gfh->fh;
337     ret = fflush(fh);
338     if (ret == EOF) {
339         error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
340     }
341 }
342
343 static void guest_file_init(void)
344 {
345     QTAILQ_INIT(&guest_file_state.filehandles);
346 }
347
348 #if defined(CONFIG_FSFREEZE)
349 static void disable_logging(void)
350 {
351     ga_disable_logging(ga_state);
352 }
353
354 static void enable_logging(void)
355 {
356     ga_enable_logging(ga_state);
357 }
358
359 typedef struct GuestFsfreezeMount {
360     char *dirname;
361     char *devtype;
362     QTAILQ_ENTRY(GuestFsfreezeMount) next;
363 } GuestFsfreezeMount;
364
365 struct {
366     GuestFsfreezeStatus status;
367     QTAILQ_HEAD(, GuestFsfreezeMount) mount_list;
368 } guest_fsfreeze_state;
369
370 /*
371  * Walk the mount table and build a list of local file systems
372  */
373 static int guest_fsfreeze_build_mount_list(void)
374 {
375     struct mntent *ment;
376     GuestFsfreezeMount *mount, *temp;
377     char const *mtab = MOUNTED;
378     FILE *fp;
379
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);
385     }
386
387     fp = setmntent(mtab, "r");
388     if (!fp) {
389         g_warning("fsfreeze: unable to read mtab");
390         return -1;
391     }
392
393     while ((ment = getmntent(fp))) {
394         /*
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.
399          */
400         if ((ment->mnt_fsname[0] != '/') ||
401             (strcmp(ment->mnt_type, "smbfs") == 0) ||
402             (strcmp(ment->mnt_type, "cifs") == 0)) {
403             continue;
404         }
405
406         mount = g_malloc0(sizeof(GuestFsfreezeMount));
407         mount->dirname = g_strdup(ment->mnt_dir);
408         mount->devtype = g_strdup(ment->mnt_type);
409
410         QTAILQ_INSERT_TAIL(&guest_fsfreeze_state.mount_list, mount, next);
411     }
412
413     endmntent(fp);
414
415     return 0;
416 }
417
418 /*
419  * Return status of freeze/thaw
420  */
421 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
422 {
423     return guest_fsfreeze_state.status;
424 }
425
426 /*
427  * Walk list of mounted file systems in the guest, and freeze the ones which
428  * are real local file systems.
429  */
430 int64_t qmp_guest_fsfreeze_freeze(Error **err)
431 {
432     int ret = 0, i = 0;
433     struct GuestFsfreezeMount *mount, *temp;
434     int fd;
435     char err_msg[512];
436
437     slog("guest-fsfreeze called");
438
439     if (guest_fsfreeze_state.status == GUEST_FSFREEZE_STATUS_FROZEN) {
440         return 0;
441     }
442
443     ret = guest_fsfreeze_build_mount_list();
444     if (ret < 0) {
445         return ret;
446     }
447
448     /* cannot risk guest agent blocking itself on a write in this state */
449     disable_logging();
450
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;
457         }
458
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
468          */
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;
475         }
476         close(fd);
477
478         i++;
479     }
480
481     guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_FROZEN;
482     return i;
483
484 error:
485     if (i > 0) {
486         qmp_guest_fsfreeze_thaw(NULL);
487     }
488     return 0;
489 }
490
491 /*
492  * Walk list of frozen file systems in the guest, and thaw them.
493  */
494 int64_t qmp_guest_fsfreeze_thaw(Error **err)
495 {
496     int ret;
497     GuestFsfreezeMount *mount, *temp;
498     int fd, i = 0;
499     bool has_error = false;
500
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;
506         }
507         ret = ioctl(fd, FITHAW);
508         if (ret < 0 && errno != EOPNOTSUPP && errno != EINVAL) {
509             has_error = true;
510             close(fd);
511             continue;
512         }
513         close(fd);
514         i++;
515     }
516
517     if (has_error) {
518         guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_ERROR;
519     } else {
520         guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
521     }
522     enable_logging();
523     return i;
524 }
525
526 static void guest_fsfreeze_init(void)
527 {
528     guest_fsfreeze_state.status = GUEST_FSFREEZE_STATUS_THAWED;
529     QTAILQ_INIT(&guest_fsfreeze_state.mount_list);
530 }
531
532 static void guest_fsfreeze_cleanup(void)
533 {
534     int64_t ret;
535     Error *err = NULL;
536
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");
541         }
542     }
543 }
544 #else
545 /*
546  * Return status of freeze/thaw
547  */
548 GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err)
549 {
550     error_set(err, QERR_UNSUPPORTED);
551
552     return 0;
553 }
554
555 /*
556  * Walk list of mounted file systems in the guest, and freeze the ones which
557  * are real local file systems.
558  */
559 int64_t qmp_guest_fsfreeze_freeze(Error **err)
560 {
561     error_set(err, QERR_UNSUPPORTED);
562
563     return 0;
564 }
565
566 /*
567  * Walk list of frozen file systems in the guest, and thaw them.
568  */
569 int64_t qmp_guest_fsfreeze_thaw(Error **err)
570 {
571     error_set(err, QERR_UNSUPPORTED);
572
573     return 0;
574 }
575 #endif
576
577 /* register init/cleanup routines for stateful command groups */
578 void ga_command_state_init(GAState *s, GACommandState *cs)
579 {
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);
585 }
This page took 0.061897 seconds and 4 git commands to generate.