3 #include "qemu_socket.h"
4 #include "qga/channel.h"
10 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
13 GIOChannel
*listen_channel
;
14 GIOChannel
*client_channel
;
15 GAChannelMethod method
;
16 GAChannelCallback event_cb
;
20 static int ga_channel_client_add(GAChannel
*c
, int fd
);
22 static gboolean
ga_channel_listen_accept(GIOChannel
*channel
,
23 GIOCondition condition
, gpointer data
)
27 bool accepted
= false;
28 struct sockaddr_un addr
;
29 socklen_t addrlen
= sizeof(addr
);
31 g_assert(channel
!= NULL
);
33 client_fd
= qemu_accept(g_io_channel_unix_get_fd(channel
),
34 (struct sockaddr
*)&addr
, &addrlen
);
35 if (client_fd
== -1) {
36 g_warning("error converting fd to gsocket: %s", strerror(errno
));
39 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
40 ret
= ga_channel_client_add(c
, client_fd
);
42 g_warning("error setting up connection");
48 /* only accept 1 connection at a time */
52 /* start polling for readable events on listen fd, new==true
53 * indicates we should use the existing s->listen_channel
55 static void ga_channel_listen_add(GAChannel
*c
, int listen_fd
, bool create
)
58 c
->listen_channel
= g_io_channel_unix_new(listen_fd
);
60 g_io_add_watch(c
->listen_channel
, G_IO_IN
, ga_channel_listen_accept
, c
);
63 static void ga_channel_listen_close(GAChannel
*c
)
65 g_assert(c
->method
== GA_CHANNEL_UNIX_LISTEN
);
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
->method
== GA_CHANNEL_UNIX_LISTEN
&& 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
, GAChannelMethod method
)
129 case GA_CHANNEL_VIRTIO_SERIAL
: {
130 int fd
= qemu_open(path
, O_RDWR
| O_NONBLOCK
131 #ifndef CONFIG_SOLARIS
136 g_critical("error opening channel: %s", strerror(errno
));
139 #ifdef CONFIG_SOLARIS
140 ret
= ioctl(fd
, I_SETSIG
, S_OUTPUT
| S_INPUT
| S_HIPRI
);
142 g_critical("error setting event mask for channel: %s",
147 ret
= ga_channel_client_add(c
, fd
);
149 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_error("error adding channel to main loop");
183 case GA_CHANNEL_UNIX_LISTEN
: {
184 int fd
= unix_listen(path
, NULL
, strlen(path
));
186 g_critical("error opening path: %s", strerror(errno
));
189 ga_channel_listen_add(c
, fd
, true);
193 g_critical("error binding/listening to specified socket");
200 GIOStatus
ga_channel_write_all(GAChannel
*c
, const gchar
*buf
, gsize size
)
204 GIOStatus status
= G_IO_STATUS_NORMAL
;
207 status
= g_io_channel_write_chars(c
->client_channel
, buf
, size
,
209 g_debug("sending data, count: %d", (int)size
);
211 g_warning("error writing to channel: %s", err
->message
);
212 return G_IO_STATUS_ERROR
;
214 if (status
!= G_IO_STATUS_NORMAL
) {
220 if (status
== G_IO_STATUS_NORMAL
) {
221 status
= g_io_channel_flush(c
->client_channel
, &err
);
223 g_warning("error flushing channel: %s", err
->message
);
224 return G_IO_STATUS_ERROR
;
231 GIOStatus
ga_channel_read(GAChannel
*c
, gchar
*buf
, gsize size
, gsize
*count
)
233 return g_io_channel_read_chars(c
->client_channel
, buf
, size
, count
, NULL
);
236 GAChannel
*ga_channel_new(GAChannelMethod method
, const gchar
*path
,
237 GAChannelCallback cb
, gpointer opaque
)
239 GAChannel
*c
= g_malloc0(sizeof(GAChannel
));
241 c
->user_data
= opaque
;
243 if (!ga_channel_open(c
, path
, method
)) {
244 g_critical("error opening channel");
252 void ga_channel_free(GAChannel
*c
)
254 if (c
->method
== GA_CHANNEL_UNIX_LISTEN
255 && c
->listen_channel
) {
256 ga_channel_listen_close(c
);
258 if (c
->client_channel
) {
259 ga_channel_client_close(c
);