]> git.ipfire.org Git - thirdparty/qemu.git/blame - qga/channel-posix.c
qemu-ga: Plug fd leak on ga_channel_listen_accept() error path
[thirdparty/qemu.git] / qga / channel-posix.c
CommitLineData
125b310e
MR
1#include <glib.h>
2#include <termios.h>
4d4922c3
EH
3#include <errno.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <stdlib.h>
1d57db19 7#include <string.h>
1de7afc9
PB
8#include "qemu/osdep.h"
9#include "qemu/sockets.h"
125b310e
MR
10#include "qga/channel.h"
11
e61ab1da
AF
12#ifdef CONFIG_SOLARIS
13#include <stropts.h>
14#endif
15
125b310e
MR
16#define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
17
18struct GAChannel {
19 GIOChannel *listen_channel;
20 GIOChannel *client_channel;
21 GAChannelMethod method;
22 GAChannelCallback event_cb;
23 gpointer user_data;
24};
25
26static int ga_channel_client_add(GAChannel *c, int fd);
27
28static gboolean ga_channel_listen_accept(GIOChannel *channel,
29 GIOCondition condition, gpointer data)
30{
31 GAChannel *c = data;
32 int ret, client_fd;
33 bool accepted = false;
34 struct sockaddr_un addr;
35 socklen_t addrlen = sizeof(addr);
36
37 g_assert(channel != NULL);
38
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));
43 goto out;
44 }
45 fcntl(client_fd, F_SETFL, O_NONBLOCK);
46 ret = ga_channel_client_add(c, client_fd);
47 if (ret) {
48 g_warning("error setting up connection");
32c16620 49 close(client_fd);
125b310e
MR
50 goto out;
51 }
52 accepted = true;
53
54out:
55 /* only accept 1 connection at a time */
56 return !accepted;
57}
58
59/* start polling for readable events on listen fd, new==true
60 * indicates we should use the existing s->listen_channel
61 */
62static void ga_channel_listen_add(GAChannel *c, int listen_fd, bool create)
63{
64 if (create) {
65 c->listen_channel = g_io_channel_unix_new(listen_fd);
66 }
67 g_io_add_watch(c->listen_channel, G_IO_IN, ga_channel_listen_accept, c);
68}
69
70static void ga_channel_listen_close(GAChannel *c)
71{
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;
77}
78
79/* cleanup state for closed connection/session, start accepting new
80 * connections if we're in listening mode
81 */
82static void ga_channel_client_close(GAChannel *c)
83{
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);
90 }
91}
92
93static gboolean ga_channel_client_event(GIOChannel *channel,
94 GIOCondition condition, gpointer data)
95{
96 GAChannel *c = data;
97 gboolean client_cont;
98
99 g_assert(c);
100 if (c->event_cb) {
101 client_cont = c->event_cb(condition, c->user_data);
102 if (!client_cont) {
103 ga_channel_client_close(c);
104 return false;
105 }
106 }
107 return true;
108}
109
110static int ga_channel_client_add(GAChannel *c, int fd)
111{
112 GIOChannel *client_channel;
113 GError *err = NULL;
114
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);
119 if (err != NULL) {
120 g_warning("error setting channel encoding to binary");
121 g_error_free(err);
122 return -1;
123 }
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;
127 return 0;
128}
129
130static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod method)
131{
132 int ret;
133 c->method = method;
134
135 switch (c->method) {
136 case GA_CHANNEL_VIRTIO_SERIAL: {
e61ab1da
AF
137 int fd = qemu_open(path, O_RDWR | O_NONBLOCK
138#ifndef CONFIG_SOLARIS
139 | O_ASYNC
140#endif
141 );
125b310e
MR
142 if (fd == -1) {
143 g_critical("error opening channel: %s", strerror(errno));
144 exit(EXIT_FAILURE);
145 }
e61ab1da
AF
146#ifdef CONFIG_SOLARIS
147 ret = ioctl(fd, I_SETSIG, S_OUTPUT | S_INPUT | S_HIPRI);
148 if (ret == -1) {
149 g_critical("error setting event mask for channel: %s",
150 strerror(errno));
151 exit(EXIT_FAILURE);
152 }
153#endif
125b310e
MR
154 ret = ga_channel_client_add(c, fd);
155 if (ret) {
156 g_critical("error adding channel to main loop");
157 return false;
158 }
159 break;
160 }
161 case GA_CHANNEL_ISA_SERIAL: {
162 struct termios tio;
163 int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
164 if (fd == -1) {
165 g_critical("error opening channel: %s", strerror(errno));
166 exit(EXIT_FAILURE);
167 }
168 tcgetattr(fd, &tio);
169 /* set up serial port for non-canonical, dumb byte streaming */
170 tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
171 INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
172 IMAXBEL);
173 tio.c_oflag = 0;
174 tio.c_lflag = 0;
175 tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
176 /* 1 available byte min or reads will block (we'll set non-blocking
177 * elsewhere, else we have to deal with read()=0 instead)
178 */
179 tio.c_cc[VMIN] = 1;
180 tio.c_cc[VTIME] = 0;
181 /* flush everything waiting for read/xmit, it's garbage at this point */
182 tcflush(fd, TCIFLUSH);
183 tcsetattr(fd, TCSANOW, &tio);
184 ret = ga_channel_client_add(c, fd);
185 if (ret) {
186 g_error("error adding channel to main loop");
187 }
188 break;
189 }
190 case GA_CHANNEL_UNIX_LISTEN: {
90119816
PB
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);
125b310e
MR
196 return false;
197 }
198 ga_channel_listen_add(c, fd, true);
199 break;
200 }
201 default:
202 g_critical("error binding/listening to specified socket");
203 return false;
204 }
205
206 return true;
207}
208
209GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size)
210{
211 GError *err = NULL;
212 gsize written = 0;
213 GIOStatus status = G_IO_STATUS_NORMAL;
214
215 while (size) {
216 status = g_io_channel_write_chars(c->client_channel, buf, size,
217 &written, &err);
218 g_debug("sending data, count: %d", (int)size);
219 if (err != NULL) {
220 g_warning("error writing to channel: %s", err->message);
221 return G_IO_STATUS_ERROR;
222 }
223 if (status != G_IO_STATUS_NORMAL) {
224 break;
225 }
226 size -= written;
227 }
228
229 if (status == G_IO_STATUS_NORMAL) {
230 status = g_io_channel_flush(c->client_channel, &err);
231 if (err != NULL) {
232 g_warning("error flushing channel: %s", err->message);
233 return G_IO_STATUS_ERROR;
234 }
235 }
236
237 return status;
238}
239
240GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
241{
242 return g_io_channel_read_chars(c->client_channel, buf, size, count, NULL);
243}
244
245GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
246 GAChannelCallback cb, gpointer opaque)
247{
248 GAChannel *c = g_malloc0(sizeof(GAChannel));
249 c->event_cb = cb;
250 c->user_data = opaque;
251
252 if (!ga_channel_open(c, path, method)) {
253 g_critical("error opening channel");
254 ga_channel_free(c);
255 return NULL;
256 }
257
258 return c;
259}
260
261void ga_channel_free(GAChannel *c)
262{
263 if (c->method == GA_CHANNEL_UNIX_LISTEN
264 && c->listen_channel) {
265 ga_channel_listen_close(c);
266 }
267 if (c->client_channel) {
268 ga_channel_client_close(c);
269 }
270 g_free(c);
271}