1 #include "qemu/osdep.h"
3 #include "qapi/error.h"
4 #include "qemu/sockets.h"
5 #include "qga/channel.h"
11 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
14 GIOChannel
*listen_channel
;
15 GIOChannel
*client_channel
;
16 GAChannelMethod method
;
17 GAChannelCallback event_cb
;
21 static int ga_channel_client_add(GAChannel
*c
, int fd
);
23 static gboolean
ga_channel_listen_accept(GIOChannel
*channel
,
24 GIOCondition condition
, gpointer data
)
28 bool accepted
= false;
30 g_assert(channel
!= NULL
);
32 client_fd
= qemu_accept(g_io_channel_unix_get_fd(channel
), NULL
, NULL
);
33 if (client_fd
== -1) {
34 g_warning("error converting fd to gsocket: %s", strerror(errno
));
37 qemu_set_nonblock(client_fd
);
38 ret
= ga_channel_client_add(c
, client_fd
);
40 g_warning("error setting up connection");
47 /* only accept 1 connection at a time */
51 /* start polling for readable events on listen fd, new==true
52 * indicates we should use the existing s->listen_channel
54 static void ga_channel_listen_add(GAChannel
*c
, int listen_fd
, bool create
)
57 c
->listen_channel
= g_io_channel_unix_new(listen_fd
);
59 g_io_add_watch(c
->listen_channel
, G_IO_IN
, ga_channel_listen_accept
, c
);
62 static void ga_channel_listen_close(GAChannel
*c
)
64 g_assert(c
->listen_channel
);
65 g_io_channel_shutdown(c
->listen_channel
, true, NULL
);
66 g_io_channel_unref(c
->listen_channel
);
67 c
->listen_channel
= NULL
;
70 /* cleanup state for closed connection/session, start accepting new
71 * connections if we're in listening mode
73 static void ga_channel_client_close(GAChannel
*c
)
75 g_assert(c
->client_channel
);
76 g_io_channel_shutdown(c
->client_channel
, true, NULL
);
77 g_io_channel_unref(c
->client_channel
);
78 c
->client_channel
= NULL
;
79 if (c
->listen_channel
) {
80 ga_channel_listen_add(c
, 0, false);
84 static gboolean
ga_channel_client_event(GIOChannel
*channel
,
85 GIOCondition condition
, gpointer data
)
92 client_cont
= c
->event_cb(condition
, c
->user_data
);
94 ga_channel_client_close(c
);
101 static int ga_channel_client_add(GAChannel
*c
, int fd
)
103 GIOChannel
*client_channel
;
106 g_assert(c
&& !c
->client_channel
);
107 client_channel
= g_io_channel_unix_new(fd
);
108 g_assert(client_channel
);
109 g_io_channel_set_encoding(client_channel
, NULL
, &err
);
111 g_warning("error setting channel encoding to binary");
115 g_io_add_watch(client_channel
, G_IO_IN
| G_IO_HUP
,
116 ga_channel_client_event
, c
);
117 c
->client_channel
= client_channel
;
121 static gboolean
ga_channel_open(GAChannel
*c
, const gchar
*path
, GAChannelMethod method
)
127 case GA_CHANNEL_VIRTIO_SERIAL
: {
128 int fd
= qemu_open(path
, O_RDWR
| O_NONBLOCK
129 #ifndef CONFIG_SOLARIS
134 g_critical("error opening channel: %s", strerror(errno
));
137 #ifdef CONFIG_SOLARIS
138 ret
= ioctl(fd
, I_SETSIG
, S_OUTPUT
| S_INPUT
| S_HIPRI
);
140 g_critical("error setting event mask for channel: %s",
146 ret
= ga_channel_client_add(c
, fd
);
148 g_critical("error adding channel to main loop");
154 case GA_CHANNEL_ISA_SERIAL
: {
156 int fd
= qemu_open(path
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
158 g_critical("error opening channel: %s", strerror(errno
));
162 /* set up serial port for non-canonical, dumb byte streaming */
163 tio
.c_iflag
&= ~(IGNBRK
| BRKINT
| IGNPAR
| PARMRK
| INPCK
| ISTRIP
|
164 INLCR
| IGNCR
| ICRNL
| IXON
| IXOFF
| IXANY
|
168 tio
.c_cflag
|= GA_CHANNEL_BAUDRATE_DEFAULT
;
169 /* 1 available byte min or reads will block (we'll set non-blocking
170 * elsewhere, else we have to deal with read()=0 instead)
174 /* flush everything waiting for read/xmit, it's garbage at this point */
175 tcflush(fd
, TCIFLUSH
);
176 tcsetattr(fd
, TCSANOW
, &tio
);
177 ret
= ga_channel_client_add(c
, fd
);
179 g_critical("error adding channel to main loop");
185 case GA_CHANNEL_UNIX_LISTEN
: {
186 Error
*local_err
= NULL
;
187 int fd
= unix_listen(path
, NULL
, strlen(path
), &local_err
);
188 if (local_err
!= NULL
) {
189 g_critical("%s", error_get_pretty(local_err
));
190 error_free(local_err
);
193 ga_channel_listen_add(c
, fd
, true);
196 case GA_CHANNEL_VSOCK_LISTEN
: {
197 Error
*local_err
= NULL
;
202 addr_str
= g_strdup_printf("vsock:%s", path
);
203 addr
= socket_parse(addr_str
, &local_err
);
205 if (local_err
!= NULL
) {
206 g_critical("%s", error_get_pretty(local_err
));
207 error_free(local_err
);
211 fd
= socket_listen(addr
, &local_err
);
212 qapi_free_SocketAddress(addr
);
213 if (local_err
!= NULL
) {
214 g_critical("%s", error_get_pretty(local_err
));
215 error_free(local_err
);
218 ga_channel_listen_add(c
, fd
, true);
222 g_critical("error binding/listening to specified socket");
229 GIOStatus
ga_channel_write_all(GAChannel
*c
, const gchar
*buf
, gsize size
)
233 GIOStatus status
= G_IO_STATUS_NORMAL
;
236 g_debug("sending data, count: %d", (int)size
);
237 status
= g_io_channel_write_chars(c
->client_channel
, buf
, size
,
239 if (status
== G_IO_STATUS_NORMAL
) {
242 } else if (status
!= G_IO_STATUS_AGAIN
) {
243 g_warning("error writing to channel: %s", err
->message
);
249 status
= g_io_channel_flush(c
->client_channel
, &err
);
250 } while (status
== G_IO_STATUS_AGAIN
);
252 if (status
!= G_IO_STATUS_NORMAL
) {
253 g_warning("error flushing channel: %s", err
->message
);
259 GIOStatus
ga_channel_read(GAChannel
*c
, gchar
*buf
, gsize size
, gsize
*count
)
261 return g_io_channel_read_chars(c
->client_channel
, buf
, size
, count
, NULL
);
264 GAChannel
*ga_channel_new(GAChannelMethod method
, const gchar
*path
,
265 GAChannelCallback cb
, gpointer opaque
)
267 GAChannel
*c
= g_new0(GAChannel
, 1);
269 c
->user_data
= opaque
;
271 if (!ga_channel_open(c
, path
, method
)) {
272 g_critical("error opening channel");
280 void ga_channel_free(GAChannel
*c
)
282 if (c
->listen_channel
) {
283 ga_channel_listen_close(c
);
285 if (c
->client_channel
) {
286 ga_channel_client_close(c
);