migration-exec.c 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * QEMU live migration
 *
 * Copyright IBM, Corp. 2008
 * Copyright Dell MessageOne 2008
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *  Charles Duffy     <charles_duffy@messageone.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include "qemu-common.h"
#include "qemu_socket.h"
#include "migration.h"
#include "qemu-char.h"
#include "buffered_file.h"
#include "block.h"
22 23
#include <sys/types.h>
#include <sys/wait.h>
24 25 26 27

//#define DEBUG_MIGRATION_EXEC

#ifdef DEBUG_MIGRATION_EXEC
M
malc 已提交
28
#define DPRINTF(fmt, ...) \
29 30
    do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0)
#else
M
malc 已提交
31
#define DPRINTF(fmt, ...) \
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    do { } while (0)
#endif

static int file_errno(FdMigrationState *s)
{
    return errno;
}

static int file_write(FdMigrationState *s, const void * buf, size_t size)
{
    return write(s->fd, buf, size);
}

static int exec_close(FdMigrationState *s)
{
47
    int ret = 0;
M
malc 已提交
48
    DPRINTF("exec_close\n");
49
    if (s->opaque) {
50
        ret = qemu_fclose(s->opaque);
51 52
        s->opaque = NULL;
        s->fd = -1;
53 54 55 56 57 58 59
        if (ret != -1 &&
            WIFEXITED(ret)
            && WEXITSTATUS(ret) == 0) {
            ret = 0;
        } else {
            ret = -1;
        }
60
    }
61
    return ret;
62 63
}

64
FdMigrationState *exec_start_outgoing_migration(Monitor *mon,
65
                                              const char *command,
L
lirans@il.ibm.com 已提交
66 67 68 69
					      int64_t bandwidth_limit,
					      int detach,
					      int blk,
					      int inc)
70 71 72 73
{
    FdMigrationState *s;
    FILE *f;

74
    s = g_malloc0(sizeof(*s));
75 76 77

    f = popen(command, "w");
    if (f == NULL) {
M
malc 已提交
78
        DPRINTF("Unable to popen exec target\n");
79 80 81 82 83
        goto err_after_alloc;
    }

    s->fd = fileno(f);
    if (s->fd == -1) {
M
malc 已提交
84
        DPRINTF("Unable to retrieve file descriptor for popen'd handle\n");
85 86 87
        goto err_after_open;
    }

88
    socket_set_nonblock(s->fd);
89 90 91

    s->opaque = qemu_popen(f, "w");

92
    s->close = exec_close;
93 94 95 96 97 98
    s->get_error = file_errno;
    s->write = file_write;
    s->mig_state.cancel = migrate_fd_cancel;
    s->mig_state.get_status = migrate_fd_get_status;
    s->mig_state.release = migrate_fd_release;

L
lirans@il.ibm.com 已提交
99 100
    s->mig_state.blk = blk;
    s->mig_state.shared = inc;
101

102
    s->state = MIG_STATE_ACTIVE;
103
    s->mon = NULL;
104 105
    s->bandwidth_limit = bandwidth_limit;

106 107 108
    if (!detach) {
        migrate_fd_monitor_suspend(s, mon);
    }
109 110

    migrate_fd_connect(s);
111
    return s;
112 113 114 115

err_after_open:
    pclose(f);
err_after_alloc:
116
    g_free(s);
117 118 119
    return NULL;
}

120
static void exec_accept_incoming_migration(void *opaque)
121
{
122
    QEMUFile *f = opaque;
123

124
    process_incoming_migration(f);
125
    qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
126
    qemu_fclose(f);
127 128 129 130 131 132
}

int exec_start_incoming_migration(const char *command)
{
    QEMUFile *f;

M
malc 已提交
133
    DPRINTF("Attempting to start an incoming migration\n");
134 135
    f = qemu_popen_cmd(command, "r");
    if(f == NULL) {
M
malc 已提交
136
        DPRINTF("Unable to apply qemu wrapper to popen file\n");
137 138 139
        return -errno;
    }

P
Paolo Bonzini 已提交
140
    qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
J
Juan Quintela 已提交
141
			 exec_accept_incoming_migration, NULL, f);
142 143

    return 0;
144
}