qemu-option: Reject anti-social IDs
[qemu/cris-port.git] / migration-exec.c
bloba8813b4fbf7a0e5ac63be2c9c352b1216d01a5bf
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
5 * Copyright Dell MessageOne 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Charles Duffy <charles_duffy@messageone.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include "qemu-common.h"
17 #include "qemu_socket.h"
18 #include "migration.h"
19 #include "qemu-char.h"
20 #include "sysemu.h"
21 #include "buffered_file.h"
22 #include "block.h"
23 #include <sys/types.h>
24 #include <sys/wait.h>
26 //#define DEBUG_MIGRATION_EXEC
28 #ifdef DEBUG_MIGRATION_EXEC
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
36 static int file_errno(FdMigrationState *s)
38 return errno;
41 static int file_write(FdMigrationState *s, const void * buf, size_t size)
43 return write(s->fd, buf, size);
46 static int exec_close(FdMigrationState *s)
48 int ret = 0;
49 DPRINTF("exec_close\n");
50 if (s->opaque) {
51 ret = qemu_fclose(s->opaque);
52 s->opaque = NULL;
53 s->fd = -1;
54 if (ret != -1 &&
55 WIFEXITED(ret)
56 && WEXITSTATUS(ret) == 0) {
57 ret = 0;
58 } else {
59 ret = -1;
62 return ret;
65 MigrationState *exec_start_outgoing_migration(Monitor *mon,
66 const char *command,
67 int64_t bandwidth_limit,
68 int detach,
69 int blk,
70 int inc)
72 FdMigrationState *s;
73 FILE *f;
75 s = qemu_mallocz(sizeof(*s));
77 f = popen(command, "w");
78 if (f == NULL) {
79 DPRINTF("Unable to popen exec target\n");
80 goto err_after_alloc;
83 s->fd = fileno(f);
84 if (s->fd == -1) {
85 DPRINTF("Unable to retrieve file descriptor for popen'd handle\n");
86 goto err_after_open;
89 socket_set_nonblock(s->fd);
91 s->opaque = qemu_popen(f, "w");
93 s->close = exec_close;
94 s->get_error = file_errno;
95 s->write = file_write;
96 s->mig_state.cancel = migrate_fd_cancel;
97 s->mig_state.get_status = migrate_fd_get_status;
98 s->mig_state.release = migrate_fd_release;
100 s->mig_state.blk = blk;
101 s->mig_state.shared = inc;
103 s->state = MIG_STATE_ACTIVE;
104 s->mon = NULL;
105 s->bandwidth_limit = bandwidth_limit;
107 if (!detach) {
108 migrate_fd_monitor_suspend(s, mon);
111 migrate_fd_connect(s);
112 return &s->mig_state;
114 err_after_open:
115 pclose(f);
116 err_after_alloc:
117 qemu_free(s);
118 return NULL;
121 static void exec_accept_incoming_migration(void *opaque)
123 QEMUFile *f = opaque;
124 int ret;
126 ret = qemu_loadvm_state(f);
127 if (ret < 0) {
128 fprintf(stderr, "load of migration failed\n");
129 goto err;
131 qemu_announce_self();
132 DPRINTF("successfully loaded vm state\n");
134 if (autostart)
135 vm_start();
137 err:
138 qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
139 qemu_fclose(f);
142 int exec_start_incoming_migration(const char *command)
144 QEMUFile *f;
146 DPRINTF("Attempting to start an incoming migration\n");
147 f = qemu_popen_cmd(command, "r");
148 if(f == NULL) {
149 DPRINTF("Unable to apply qemu wrapper to popen file\n");
150 return -errno;
153 qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
154 exec_accept_incoming_migration, NULL, f);
156 return 0;