8 #include "qemu/osdep.h"
9 #include "qemu/sockets.h"
10 #include "qga/channel.h"
16 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
19 GIOChannel
*listen_channel
;
20 GIOChannel
*client_channel
;
21 GAChannelMethod method
;
22 GAChannelCallback event_cb
;
26 static int ga_channel_client_add(GAChannel
*c
, int fd
);
28 static gboolean
ga_channel_listen_accept(GIOChannel
*channel
,
29 GIOCondition condition
, gpointer data
)
33 bool accepted
= false;
34 struct sockaddr_un addr
;
35 socklen_t addrlen
= sizeof(addr
);
37 g_assert(channel
!= NULL
);
39 client_fd
= qemu_accept(g_io_channel_unix_get_fd(channel
),
40 (struct sockaddr
*)&addr
, &addrlen
);
41 if (client_fd
== -1) {
42 g_warning("error converting fd to gsocket: %s", strerror(errno
));
45 qemu_set_nonblock(client_fd
);
46 ret
= ga_channel_client_add(c
, client_fd
);
48 g_warning("error setting up connection");
55 /* only accept 1 connection at a time */
59 /* start polling for readable events on listen fd, new==true
60 * indicates we should use the existing s->listen_channel
62 static void ga_channel_listen_add(GAChannel
*c
, int listen_fd
, bool create
)
65 c
->listen_channel
= g_io_channel_unix_new(listen_fd
);
67 g_io_add_watch(c
->listen_channel
, G_IO_IN
, ga_channel_listen_accept
, c
);
70 static void ga_channel_listen_close(GAChannel
*c
)
72 g_assert(c
->method
== GA_CHANNEL_UNIX_LISTEN
);
73 g_assert(c
->listen_channel
);
74 g_io_channel_shutdown(c
->listen_channel
, true, NULL
);
75 g_io_channel_unref(c
->listen_channel
);
76 c
->listen_channel
= NULL
;
79 /* cleanup state for closed connection/session, start accepting new
80 * connections if we're in listening mode
82 static void ga_channel_client_close(GAChannel
*c
)
84 g_assert(c
->client_channel
);
85 g_io_channel_shutdown(c
->client_channel
, true, NULL
);
86 g_io_channel_unref(c
->client_channel
);
87 c
->client_channel
= NULL
;
88 if (c
->method
== GA_CHANNEL_UNIX_LISTEN
&& c
->listen_channel
) {
89 ga_channel_listen_add(c
, 0, false);
93 static gboolean
ga_channel_client_event(GIOChannel
*channel
,
94 GIOCondition condition
, gpointer data
)
101 client_cont
= c
->event_cb(condition
, c
->user_data
);
103 ga_channel_client_close(c
);
110 static int ga_channel_client_add(GAChannel
*c
, int fd
)
112 GIOChannel
*client_channel
;
115 g_assert(c
&& !c
->client_channel
);
116 client_channel
= g_io_channel_unix_new(fd
);
117 g_assert(client_channel
);
118 g_io_channel_set_encoding(client_channel
, NULL
, &err
);
120 g_warning("error setting channel encoding to binary");
124 g_io_add_watch(client_channel
, G_IO_IN
| G_IO_HUP
,
125 ga_channel_client_event
, c
);
126 c
->client_channel
= client_channel
;
130 static gboolean
ga_channel_open(GAChannel
*c
, const gchar
*path
, GAChannelMethod method
)
136 case GA_CHANNEL_VIRTIO_SERIAL
: {
137 int fd
= qemu_open(path
, O_RDWR
| O_NONBLOCK
138 #ifndef CONFIG_SOLARIS
143 g_critical("error opening channel: %s", strerror(errno
));
146 #ifdef CONFIG_SOLARIS
147 ret
= ioctl(fd
, I_SETSIG
, S_OUTPUT
| S_INPUT
| S_HIPRI
);
149 g_critical("error setting event mask for channel: %s",
155 ret
= ga_channel_client_add(c
, fd
);
157 g_critical("error adding channel to main loop");
163 case GA_CHANNEL_ISA_SERIAL
: {
165 int fd
= qemu_open(path
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
);
167 g_critical("error opening channel: %s", strerror(errno
));
171 /* set up serial port for non-canonical, dumb byte streaming */
172 tio
.c_iflag
&= ~(IGNBRK
| BRKINT
| IGNPAR
| PARMRK
| INPCK
| ISTRIP
|
173 INLCR
| IGNCR
| ICRNL
| IXON
| IXOFF
| IXANY
|
177 tio
.c_cflag
|= GA_CHANNEL_BAUDRATE_DEFAULT
;
178 /* 1 available byte min or reads will block (we'll set non-blocking
179 * elsewhere, else we have to deal with read()=0 instead)
183 /* flush everything waiting for read/xmit, it's garbage at this point */
184 tcflush(fd
, TCIFLUSH
);
185 tcsetattr(fd
, TCSANOW
, &tio
);
186 ret
= ga_channel_client_add(c
, fd
);
188 g_critical("error adding channel to main loop");
194 case GA_CHANNEL_UNIX_LISTEN
: {
195 Error
*local_err
= NULL
;
196 int fd
= unix_listen(path
, NULL
, strlen(path
), &local_err
);
197 if (local_err
!= NULL
) {
198 g_critical("%s", error_get_pretty(local_err
));
199 error_free(local_err
);
202 ga_channel_listen_add(c
, fd
, true);
206 g_critical("error binding/listening to specified socket");
213 GIOStatus
ga_channel_write_all(GAChannel
*c
, const gchar
*buf
, gsize size
)
217 GIOStatus status
= G_IO_STATUS_NORMAL
;
220 g_debug("sending data, count: %d", (int)size
);
221 status
= g_io_channel_write_chars(c
->client_channel
, buf
, size
,
223 if (status
== G_IO_STATUS_NORMAL
) {
226 } else if (status
!= G_IO_STATUS_AGAIN
) {
227 g_warning("error writing to channel: %s", err
->message
);
233 status
= g_io_channel_flush(c
->client_channel
, &err
);
234 } while (status
== G_IO_STATUS_AGAIN
);
236 if (status
!= G_IO_STATUS_NORMAL
) {
237 g_warning("error flushing channel: %s", err
->message
);
243 GIOStatus
ga_channel_read(GAChannel
*c
, gchar
*buf
, gsize size
, gsize
*count
)
245 return g_io_channel_read_chars(c
->client_channel
, buf
, size
, count
, NULL
);
248 GAChannel
*ga_channel_new(GAChannelMethod method
, const gchar
*path
,
249 GAChannelCallback cb
, gpointer opaque
)
251 GAChannel
*c
= g_new0(GAChannel
, 1);
253 c
->user_data
= opaque
;
255 if (!ga_channel_open(c
, path
, method
)) {
256 g_critical("error opening channel");
264 void ga_channel_free(GAChannel
*c
)
266 if (c
->method
== GA_CHANNEL_UNIX_LISTEN
267 && c
->listen_channel
) {
268 ga_channel_listen_close(c
);
270 if (c
->client_channel
) {
271 ga_channel_client_close(c
);