3 #include "qemu_socket.h"
4 #include "qga/channel.h"
6 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
9 GIOChannel *listen_channel;
10 GIOChannel *client_channel;
11 GAChannelMethod method;
12 GAChannelCallback event_cb;
16 static int ga_channel_client_add(GAChannel *c, int fd);
18 static gboolean ga_channel_listen_accept(GIOChannel *channel,
19 GIOCondition condition, gpointer data)
23 bool accepted = false;
24 struct sockaddr_un addr;
25 socklen_t addrlen = sizeof(addr);
27 g_assert(channel != NULL);
29 client_fd = qemu_accept(g_io_channel_unix_get_fd(channel),
30 (struct sockaddr *)&addr, &addrlen);
31 if (client_fd == -1) {
32 g_warning("error converting fd to gsocket: %s", strerror(errno));
35 fcntl(client_fd, F_SETFL, O_NONBLOCK);
36 ret = ga_channel_client_add(c, client_fd);
38 g_warning("error setting up connection");
44 /* only accept 1 connection at a time */
48 /* start polling for readable events on listen fd, new==true
49 * indicates we should use the existing s->listen_channel
51 static void ga_channel_listen_add(GAChannel *c, int listen_fd, bool create)
54 c->listen_channel = g_io_channel_unix_new(listen_fd);
56 g_io_add_watch(c->listen_channel, G_IO_IN, ga_channel_listen_accept, c);
59 static void ga_channel_listen_close(GAChannel *c)
61 g_assert(c->method == GA_CHANNEL_UNIX_LISTEN);
62 g_assert(c->listen_channel);
63 g_io_channel_shutdown(c->listen_channel, true, NULL);
64 g_io_channel_unref(c->listen_channel);
65 c->listen_channel = NULL;
68 /* cleanup state for closed connection/session, start accepting new
69 * connections if we're in listening mode
71 static void ga_channel_client_close(GAChannel *c)
73 g_assert(c->client_channel);
74 g_io_channel_shutdown(c->client_channel, true, NULL);
75 g_io_channel_unref(c->client_channel);
76 c->client_channel = NULL;
77 if (c->method == GA_CHANNEL_UNIX_LISTEN && c->listen_channel) {
78 ga_channel_listen_add(c, 0, false);
82 static gboolean ga_channel_client_event(GIOChannel *channel,
83 GIOCondition condition, gpointer data)
90 client_cont = c->event_cb(condition, c->user_data);
92 ga_channel_client_close(c);
99 static int ga_channel_client_add(GAChannel *c, int fd)
101 GIOChannel *client_channel;
104 g_assert(c && !c->client_channel);
105 client_channel = g_io_channel_unix_new(fd);
106 g_assert(client_channel);
107 g_io_channel_set_encoding(client_channel, NULL, &err);
109 g_warning("error setting channel encoding to binary");
113 g_io_add_watch(client_channel, G_IO_IN | G_IO_HUP,
114 ga_channel_client_event, c);
115 c->client_channel = client_channel;
119 static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod method)
125 case GA_CHANNEL_VIRTIO_SERIAL: {
126 int fd = qemu_open(path, O_RDWR | O_NONBLOCK | O_ASYNC);
128 g_critical("error opening channel: %s", strerror(errno));
131 ret = ga_channel_client_add(c, fd);
133 g_critical("error adding channel to main loop");
138 case GA_CHANNEL_ISA_SERIAL: {
140 int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
142 g_critical("error opening channel: %s", strerror(errno));
146 /* set up serial port for non-canonical, dumb byte streaming */
147 tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
148 INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
152 tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
153 /* 1 available byte min or reads will block (we'll set non-blocking
154 * elsewhere, else we have to deal with read()=0 instead)
158 /* flush everything waiting for read/xmit, it's garbage at this point */
159 tcflush(fd, TCIFLUSH);
160 tcsetattr(fd, TCSANOW, &tio);
161 ret = ga_channel_client_add(c, fd);
163 g_error("error adding channel to main loop");
167 case GA_CHANNEL_UNIX_LISTEN: {
168 int fd = unix_listen(path, NULL, strlen(path));
170 g_critical("error opening path: %s", strerror(errno));
173 ga_channel_listen_add(c, fd, true);
177 g_critical("error binding/listening to specified socket");
184 GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size)
188 GIOStatus status = G_IO_STATUS_NORMAL;
191 status = g_io_channel_write_chars(c->client_channel, buf, size,
193 g_debug("sending data, count: %d", (int)size);
195 g_warning("error writing to channel: %s", err->message);
196 return G_IO_STATUS_ERROR;
198 if (status != G_IO_STATUS_NORMAL) {
204 if (status == G_IO_STATUS_NORMAL) {
205 status = g_io_channel_flush(c->client_channel, &err);
207 g_warning("error flushing channel: %s", err->message);
208 return G_IO_STATUS_ERROR;
215 GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
217 return g_io_channel_read_chars(c->client_channel, buf, size, count, NULL);
220 GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
221 GAChannelCallback cb, gpointer opaque)
223 GAChannel *c = g_malloc0(sizeof(GAChannel));
225 c->user_data = opaque;
227 if (!ga_channel_open(c, path, method)) {
228 g_critical("error opening channel");
236 void ga_channel_free(GAChannel *c)
238 if (c->method == GA_CHANNEL_UNIX_LISTEN
239 && c->listen_channel) {
240 ga_channel_listen_close(c);
242 if (c->client_channel) {
243 ga_channel_client_close(c);