1 #include "qemu/osdep.h"
2 #include "qemu/cutils.h"
4 #include "qapi/error.h"
5 #include "qemu/sockets.h"
13 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
16 GIOChannel
*listen_channel
;
17 GIOChannel
*client_channel
;
18 GAChannelMethod method
;
19 GAChannelCallback event_cb
;
23 static int ga_channel_client_add(GAChannel
*c
, int fd
);
25 static gboolean
ga_channel_listen_accept(GIOChannel
*channel
,
26 GIOCondition condition
, gpointer data
)
30 bool accepted
= false;
32 g_assert(channel
!= NULL
);
34 client_fd
= qemu_accept(g_io_channel_unix_get_fd(channel
), NULL
, NULL
);
35 if (client_fd
== -1) {
36 g_warning("error converting fd to gsocket: %s", strerror(errno
));
39 qemu_socket_set_nonblock(client_fd
);
40 ret
= ga_channel_client_add(c
, client_fd
);
42 g_warning("error setting up connection");
49 /* only accept 1 connection at a time */
53 /* start polling for readable events on listen fd, new==true
54 * indicates we should use the existing s->listen_channel
56 static void ga_channel_listen_add(GAChannel
*c
, int listen_fd
, bool create
)
59 c
->listen_channel
= g_io_channel_unix_new(listen_fd
);
61 g_io_add_watch(c
->listen_channel
, G_IO_IN
, ga_channel_listen_accept
, c
);
64 static void ga_channel_listen_close(GAChannel
*c
)
66 g_assert(c
->listen_channel
);
67 g_io_channel_shutdown(c
->listen_channel
, true, NULL
);
68 g_io_channel_unref(c
->listen_channel
);
69 c
->listen_channel
= NULL
;
72 /* cleanup state for closed connection/session, start accepting new
73 * connections if we're in listening mode
75 static void ga_channel_client_close(GAChannel
*c
)
77 g_assert(c
->client_channel
);
78 g_io_channel_shutdown(c
->client_channel
, true, NULL
);
79 g_io_channel_unref(c
->client_channel
);
80 c
->client_channel
= NULL
;
81 if (c
->listen_channel
) {
82 ga_channel_listen_add(c
, 0, false);
86 static gboolean
ga_channel_client_event(GIOChannel
*channel
,
87 GIOCondition condition
, gpointer data
)
94 client_cont
= c
->event_cb(condition
, c
->user_data
);
96 ga_channel_client_close(c
);
103 static int ga_channel_client_add(GAChannel
*c
, int fd
)
105 GIOChannel
*client_channel
;
108 g_assert(c
&& !c
->client_channel
);
109 client_channel
= g_io_channel_unix_new(fd
);
110 g_assert(client_channel
);
111 g_io_channel_set_encoding(client_channel
, NULL
, &err
);
113 g_warning("error setting channel encoding to binary");
117 g_io_add_watch(client_channel
, G_IO_IN
| G_IO_HUP
,
118 ga_channel_client_event
, c
);
119 c
->client_channel
= client_channel
;
123 static gboolean
ga_channel_open(GAChannel
*c
, const gchar
*path
,
124 GAChannelMethod method
, int fd
, Error
**errp
)
130 case GA_CHANNEL_VIRTIO_SERIAL
: {
132 fd
= qga_open_cloexec(
134 #ifndef CONFIG_SOLARIS
141 error_setg_errno(errp
, errno
, "error opening channel '%s'", path
);
144 #ifdef CONFIG_SOLARIS
145 ret
= ioctl(fd
, I_SETSIG
, S_OUTPUT
| S_INPUT
| S_HIPRI
);
147 error_setg_errno(errp
, errno
, "error setting event mask for channel");
154 * In the default state channel sends echo of every command to a
155 * client. The client programm doesn't expect this and raises an
156 * error. Suppress echo by resetting ECHO terminal flag.
159 if (tcgetattr(fd
, &tio
) < 0) {
160 error_setg_errno(errp
, errno
, "error getting channel termios attrs");
164 tio
.c_lflag
&= ~ECHO
;
165 if (tcsetattr(fd
, TCSAFLUSH
, &tio
) < 0) {
166 error_setg_errno(errp
, errno
, "error setting channel termios attrs");
170 #endif /* __FreeBSD__ */
171 ret
= ga_channel_client_add(c
, fd
);
173 error_setg(errp
, "error adding channel to main loop");
179 case GA_CHANNEL_ISA_SERIAL
: {
183 fd
= qga_open_cloexec(path
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
, 0);
185 error_setg_errno(errp
, errno
, "error opening channel '%s'", path
);
189 /* set up serial port for non-canonical, dumb byte streaming */
190 tio
.c_iflag
&= ~(IGNBRK
| BRKINT
| IGNPAR
| PARMRK
| INPCK
| ISTRIP
|
191 INLCR
| IGNCR
| ICRNL
| IXON
| IXOFF
| IXANY
|
195 tio
.c_cflag
|= GA_CHANNEL_BAUDRATE_DEFAULT
;
196 /* 1 available byte min or reads will block (we'll set non-blocking
197 * elsewhere, else we have to deal with read()=0 instead)
201 /* flush everything waiting for read/xmit, it's garbage at this point */
202 tcflush(fd
, TCIFLUSH
);
203 tcsetattr(fd
, TCSANOW
, &tio
);
204 ret
= ga_channel_client_add(c
, fd
);
206 error_setg(errp
, "error adding channel to main loop");
212 case GA_CHANNEL_UNIX_LISTEN
: {
214 fd
= unix_listen(path
, errp
);
219 ga_channel_listen_add(c
, fd
, true);
222 case GA_CHANNEL_VSOCK_LISTEN
: {
227 addr_str
= g_strdup_printf("vsock:%s", path
);
228 addr
= socket_parse(addr_str
, errp
);
234 fd
= socket_listen(addr
, 1, errp
);
235 qapi_free_SocketAddress(addr
);
240 ga_channel_listen_add(c
, fd
, true);
244 error_setg(errp
, "error binding/listening to specified socket");
251 GIOStatus
ga_channel_write_all(GAChannel
*c
, const gchar
*buf
, gsize size
)
255 GIOStatus status
= G_IO_STATUS_NORMAL
;
258 g_debug("sending data, count: %d", (int)size
);
259 status
= g_io_channel_write_chars(c
->client_channel
, buf
, size
,
261 if (status
== G_IO_STATUS_NORMAL
) {
264 } else if (status
!= G_IO_STATUS_AGAIN
) {
265 g_warning("error writing to channel: %s", err
->message
);
271 status
= g_io_channel_flush(c
->client_channel
, &err
);
272 } while (status
== G_IO_STATUS_AGAIN
);
274 if (status
!= G_IO_STATUS_NORMAL
) {
275 g_warning("error flushing channel: %s", err
->message
);
281 GIOStatus
ga_channel_read(GAChannel
*c
, gchar
*buf
, gsize size
, gsize
*count
)
283 return g_io_channel_read_chars(c
->client_channel
, buf
, size
, count
, NULL
);
286 GAChannel
*ga_channel_new(GAChannelMethod method
, const gchar
*path
,
287 int listen_fd
, GAChannelCallback cb
, gpointer opaque
)
290 GAChannel
*c
= g_new0(GAChannel
, 1);
292 c
->user_data
= opaque
;
294 if (!ga_channel_open(c
, path
, method
, listen_fd
, &err
)) {
295 g_critical("%s", error_get_pretty(err
));
304 void ga_channel_free(GAChannel
*c
)
306 if (c
->listen_channel
) {
307 ga_channel_listen_close(c
);
309 if (c
->client_channel
) {
310 ga_channel_client_close(c
);