migration: convert unix socket protocol to use QIOChannel
[qemu/ar7.git] / migration / unix.c
blob75205d42eada3c739ec8caf433b416942c5d1a32
1 /*
2 * QEMU live migration via Unix Domain Sockets
4 * Copyright Red Hat, Inc. 2009-2016
6 * Authors:
7 * Chris Lalancette <clalance@redhat.com>
8 * Daniel P. Berrange <berrange@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
13 * Contributions after 2012-01-13 are licensed under the terms of the
14 * GNU GPL, version 2 or (at your option) any later version.
17 #include "qemu/osdep.h"
19 #include "qemu-common.h"
20 #include "qemu/error-report.h"
21 #include "qapi/error.h"
22 #include "migration/migration.h"
23 #include "migration/qemu-file.h"
24 #include "io/channel-socket.h"
25 #include "trace.h"
28 static SocketAddress *unix_build_address(const char *path)
30 SocketAddress *saddr;
32 saddr = g_new0(SocketAddress, 1);
33 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
34 saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
35 saddr->u.q_unix.data->path = g_strdup(path);
37 return saddr;
41 static void unix_outgoing_migration(Object *src,
42 Error *err,
43 gpointer opaque)
45 MigrationState *s = opaque;
46 QIOChannel *sioc = QIO_CHANNEL(src);
48 if (err) {
49 trace_migration_unix_outgoing_error(error_get_pretty(err));
50 s->to_dst_file = NULL;
51 migrate_fd_error(s, err);
52 } else {
53 trace_migration_unix_outgoing_connected();
54 migration_set_outgoing_channel(s, sioc);
56 object_unref(src);
60 void unix_start_outgoing_migration(MigrationState *s, const char *path, Error **errp)
62 SocketAddress *saddr = unix_build_address(path);
63 QIOChannelSocket *sioc;
64 sioc = qio_channel_socket_new();
65 qio_channel_socket_connect_async(sioc,
66 saddr,
67 unix_outgoing_migration,
69 NULL);
70 qapi_free_SocketAddress(saddr);
74 static gboolean unix_accept_incoming_migration(QIOChannel *ioc,
75 GIOCondition condition,
76 gpointer opaque)
78 QIOChannelSocket *sioc;
79 Error *err = NULL;
81 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
82 &err);
83 if (!sioc) {
84 error_report("could not accept migration connection (%s)",
85 error_get_pretty(err));
86 goto out;
89 trace_migration_unix_incoming_accepted();
91 migration_set_incoming_channel(migrate_get_current(),
92 QIO_CHANNEL(sioc));
93 object_unref(OBJECT(sioc));
95 out:
96 /* Close listening socket as its no longer needed */
97 qio_channel_close(ioc, NULL);
98 return FALSE; /* unregister */
102 void unix_start_incoming_migration(const char *path, Error **errp)
104 SocketAddress *saddr = unix_build_address(path);
105 QIOChannelSocket *listen_ioc;
107 listen_ioc = qio_channel_socket_new();
108 if (qio_channel_socket_listen_sync(listen_ioc, saddr, errp) < 0) {
109 object_unref(OBJECT(listen_ioc));
110 qapi_free_SocketAddress(saddr);
111 return;
114 qio_channel_add_watch(QIO_CHANNEL(listen_ioc),
115 G_IO_IN,
116 unix_accept_incoming_migration,
117 listen_ioc,
118 (GDestroyNotify)object_unref);
120 qapi_free_SocketAddress(saddr);