]> git.proxmox.com Git - qemu.git/blame - migration-exec.c
vnc: rename vnc-encoding-* vnc-enc-*
[qemu.git] / migration-exec.c
CommitLineData
065e2813
AL
1/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 * Copyright Dell MessageOne 2008
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Charles Duffy <charles_duffy@messageone.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 */
15
16#include "qemu-common.h"
17#include "qemu_socket.h"
18#include "migration.h"
19#include "qemu-char.h"
20#include "sysemu.h"
065e2813
AL
21#include "buffered_file.h"
22#include "block.h"
0ffbba35
BS
23#include <sys/types.h>
24#include <sys/wait.h>
065e2813
AL
25
26//#define DEBUG_MIGRATION_EXEC
27
28#ifdef DEBUG_MIGRATION_EXEC
d0f2c4c6 29#define DPRINTF(fmt, ...) \
065e2813
AL
30 do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
31#else
d0f2c4c6 32#define DPRINTF(fmt, ...) \
065e2813
AL
33 do { } while (0)
34#endif
35
36static int file_errno(FdMigrationState *s)
37{
38 return errno;
39}
40
41static int file_write(FdMigrationState *s, const void * buf, size_t size)
42{
43 return write(s->fd, buf, size);
44}
45
46static int exec_close(FdMigrationState *s)
47{
41ef56e6 48 int ret = 0;
d0f2c4c6 49 DPRINTF("exec_close\n");
065e2813 50 if (s->opaque) {
41ef56e6 51 ret = qemu_fclose(s->opaque);
065e2813
AL
52 s->opaque = NULL;
53 s->fd = -1;
41ef56e6
AL
54 if (ret != -1 &&
55 WIFEXITED(ret)
56 && WEXITSTATUS(ret) == 0) {
57 ret = 0;
58 } else {
59 ret = -1;
60 }
065e2813 61 }
41ef56e6 62 return ret;
065e2813
AL
63}
64
f327aa0c
JK
65MigrationState *exec_start_outgoing_migration(Monitor *mon,
66 const char *command,
c163b5ca 67 int64_t bandwidth_limit,
68 int detach,
69 int blk,
70 int inc)
065e2813
AL
71{
72 FdMigrationState *s;
73 FILE *f;
74
75 s = qemu_mallocz(sizeof(*s));
065e2813
AL
76
77 f = popen(command, "w");
78 if (f == NULL) {
d0f2c4c6 79 DPRINTF("Unable to popen exec target\n");
065e2813
AL
80 goto err_after_alloc;
81 }
82
83 s->fd = fileno(f);
84 if (s->fd == -1) {
d0f2c4c6 85 DPRINTF("Unable to retrieve file descriptor for popen'd handle\n");
065e2813
AL
86 goto err_after_open;
87 }
88
90750009 89 socket_set_nonblock(s->fd);
065e2813
AL
90
91 s->opaque = qemu_popen(f, "w");
92
8ad9fa5d 93 s->close = exec_close;
065e2813
AL
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;
99
c163b5ca 100 s->mig_state.blk = blk;
101 s->mig_state.shared = inc;
f327aa0c 102
065e2813 103 s->state = MIG_STATE_ACTIVE;
f327aa0c 104 s->mon = NULL;
065e2813
AL
105 s->bandwidth_limit = bandwidth_limit;
106
f327aa0c
JK
107 if (!detach) {
108 migrate_fd_monitor_suspend(s, mon);
109 }
065e2813
AL
110
111 migrate_fd_connect(s);
112 return &s->mig_state;
113
114err_after_open:
115 pclose(f);
116err_after_alloc:
117 qemu_free(s);
065e2813
AL
118 return NULL;
119}
120
8a43b1ea 121static void exec_accept_incoming_migration(void *opaque)
065e2813 122{
8a43b1ea 123 QEMUFile *f = opaque;
065e2813 124
511c0231 125 process_incoming_migration(f);
cfaf6d36 126 qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
065e2813 127 qemu_fclose(f);
8a43b1ea
CL
128}
129
130int exec_start_incoming_migration(const char *command)
131{
132 QEMUFile *f;
133
d0f2c4c6 134 DPRINTF("Attempting to start an incoming migration\n");
8a43b1ea
CL
135 f = qemu_popen_cmd(command, "r");
136 if(f == NULL) {
d0f2c4c6 137 DPRINTF("Unable to apply qemu wrapper to popen file\n");
8a43b1ea
CL
138 return -errno;
139 }
140
7f79dd28 141 qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
1c39e2a2 142 exec_accept_incoming_migration, NULL, f);
8a43b1ea
CL
143
144 return 0;
065e2813 145}