1 #include "qemu/osdep.h"
4 #include "qapi/error.h"
5 #include "qemu/sockets.h"
6 #include "qga/channel.h"
12 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
15 GIOChannel
*listen_channel
;
16 GIOChannel
*client_channel
;
17 GAChannelMethod method
;
18 GAChannelCallback event_cb
;
22 static int ga_channel_client_add(GAChannel
*c
, int fd
);
24 static gboolean
ga_channel_listen_accept(GIOChannel
*channel
,
25 GIOCondition condition
, gpointer data
)
29 bool accepted
= false;
30 struct sockaddr_un addr
;
31 socklen_t addrlen
= sizeof(addr
);
33 g_assert(channel
!= NULL
);
35 client_fd
= qemu_accept(g_io_channel_unix_get_fd(channel
),
36 (struct sockaddr
*)&addr
, &addrlen
);
37 if (client_fd
== -1) {
38 g_warning("error converting fd to gsocket: %s", strerror(errno
));
41 qemu_set_nonblock(client_fd
);
42 ret
= ga_channel_client_add(c
, client_fd
);
44 g_warning("error setting up connection");
51 /* only accept 1 connection at a time */
55 /* start polling for readable events on listen fd, new==true
56 * indicates we should use the existing s->listen_channel
58 static void ga_channel_listen_add(GAChannel
*c
, int listen_fd
, bool create
)
61 c
->listen_channel
= g_io_channel_unix_new(listen_fd
);
63 g_io_add_watch(c
->listen_channel
, G_IO_IN
, ga_channel_listen_accept
, c
);
66 static void ga_channel_listen_close(GAChannel
*c
)
68 g_assert(c
->method
== GA_CHANNEL_UNIX_LISTEN
);
69 g_assert(c
->listen_channel
);
70 g_io_channel_shutdown(c
->listen_channel
, true, NULL
);
71 g_io_channel_unref(c
->listen_channel
);
72 c
->listen_channel
= NULL
;
75 /* cleanup state for closed connection/session, start accepting new
76 * connections if we're in listening mode
78 static void ga_channel_client_close(GAChannel
*c
)
80 g_assert(c
->client_channel
);
81 g_io_channel_shutdown(c
->client_channel
, true, NULL
);
82 g_io_channel_unref(c
->client_channel
);
83 c
->client_channel
= NULL
;
84 if (c
->method
== GA_CHANNEL_UNIX_LISTEN
&& c
->listen_channel
) {
85 ga_channel_listen_add(c
, 0, false);
89 static gboolean
ga_channel_client_event(GIOChannel
*channel
,
90 GIOCondition condition
, gpointer data
)
97 client_cont
= c
->event_cb(condition
, c
->user_data
);
99 ga_channel_client_close(c
);
106 static int ga_channel_client_add(GAChannel
*c
, int fd
)
108 GIOChannel
*client_channel
;
111 g_assert(c
&& !c
->client_channel
);
112 client_channel
= g_io_channel_unix_new(fd
);
113 g_assert(client_channel
);
114 g_io_channel_set_encoding(client_channel
, NULL
, &err
);
116 g_warning("error setting channel encoding to binary");
120 g_io_add_watch(client_channel
, G_IO_IN
| G_IO_HUP
,
121 ga_channel_client_event
, c
);
122 c
->client_channel
= client_channel
;
126 static gboolean
ga_channel_open(GAChannel
*c
, const gchar
*path
, GAChannelMethod method
)
132 case GA_CHANNEL_VIRTIO_SERIAL
: {
133 int fd
= qemu_open(path
, O_RDWR
| O_NONBLOCK
134 #ifndef CONFIG_SOLARIS
139 g_critical("error opening channel: %s", strerror(errno
));
142 #ifdef CONFIG_SOLARIS
143 ret
= ioctl(fd
, I_SETSIG
, S_OUTPUT
| S_INPUT
| S_HIPRI
);
145 g_critical("error setting event mask for channel: %s",
151 ret
= ga_channel_client_add(c
, fd
);
153 g_critical("error adding channel to main loop");
159 case GA_CHANNEL_ISA_SERIAL
: {
161 int fd
= qemu_open(path
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
163 g_critical("error opening channel: %s", strerror(errno
));
167 /* set up serial port for non-canonical, dumb byte streaming */
168 tio
.c_iflag
&= ~(IGNBRK
| BRKINT
| IGNPAR
| PARMRK
| INPCK
| ISTRIP
|
169 INLCR
| IGNCR
| ICRNL
| IXON
| IXOFF
| IXANY
|
173 tio
.c_cflag
|= GA_CHANNEL_BAUDRATE_DEFAULT
;
174 /* 1 available byte min or reads will block (we'll set non-blocking
175 * elsewhere, else we have to deal with read()=0 instead)
179 /* flush everything waiting for read/xmit, it's garbage at this point */
180 tcflush(fd
, TCIFLUSH
);
181 tcsetattr(fd
, TCSANOW
, &tio
);
182 ret
= ga_channel_client_add(c
, fd
);
184 g_critical("error adding channel to main loop");
190 case GA_CHANNEL_UNIX_LISTEN
: {
191 Error
*local_err
= NULL
;
192 int fd
= unix_listen(path
, NULL
, strlen(path
), &local_err
);
193 if (local_err
!= NULL
) {
194 g_critical("%s", error_get_pretty(local_err
));
195 error_free(local_err
);
198 ga_channel_listen_add(c
, fd
, true);
202 g_critical("error binding/listening to specified socket");
209 GIOStatus
ga_channel_write_all(GAChannel
*c
, const gchar
*buf
, gsize size
)
213 GIOStatus status
= G_IO_STATUS_NORMAL
;
216 g_debug("sending data, count: %d", (int)size
);
217 status
= g_io_channel_write_chars(c
->client_channel
, buf
, size
,
219 if (status
== G_IO_STATUS_NORMAL
) {
222 } else if (status
!= G_IO_STATUS_AGAIN
) {
223 g_warning("error writing to channel: %s", err
->message
);
229 status
= g_io_channel_flush(c
->client_channel
, &err
);
230 } while (status
== G_IO_STATUS_AGAIN
);
232 if (status
!= G_IO_STATUS_NORMAL
) {
233 g_warning("error flushing channel: %s", err
->message
);
239 GIOStatus
ga_channel_read(GAChannel
*c
, gchar
*buf
, gsize size
, gsize
*count
)
241 return g_io_channel_read_chars(c
->client_channel
, buf
, size
, count
, NULL
);
244 GAChannel
*ga_channel_new(GAChannelMethod method
, const gchar
*path
,
245 GAChannelCallback cb
, gpointer opaque
)
247 GAChannel
*c
= g_new0(GAChannel
, 1);
249 c
->user_data
= opaque
;
251 if (!ga_channel_open(c
, path
, method
)) {
252 g_critical("error opening channel");
260 void ga_channel_free(GAChannel
*c
)
262 if (c
->method
== GA_CHANNEL_UNIX_LISTEN
263 && c
->listen_channel
) {
264 ga_channel_listen_close(c
);
266 if (c
->client_channel
) {
267 ga_channel_client_close(c
);