]> git.proxmox.com Git - qemu.git/blame - qga/channel-posix.c
qemu-ga: Plug fd leak on ga_channel_open() error paths
[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");
d4f4a3ef 157 close(fd);
125b310e
MR
158 return false;
159 }
160 break;
161 }
162 case GA_CHANNEL_ISA_SERIAL: {
163 struct termios tio;
164 int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
165 if (fd == -1) {
166 g_critical("error opening channel: %s", strerror(errno));
167 exit(EXIT_FAILURE);
168 }
169 tcgetattr(fd, &tio);
170 /* set up serial port for non-canonical, dumb byte streaming */
171 tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
172 INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
173 IMAXBEL);
174 tio.c_oflag = 0;
175 tio.c_lflag = 0;
176 tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
177 /* 1 available byte min or reads will block (we'll set non-blocking
178 * elsewhere, else we have to deal with read()=0 instead)
179 */
180 tio.c_cc[VMIN] = 1;
181 tio.c_cc[VTIME] = 0;
182 /* flush everything waiting for read/xmit, it's garbage at this point */
183 tcflush(fd, TCIFLUSH);
184 tcsetattr(fd, TCSANOW, &tio);
185 ret = ga_channel_client_add(c, fd);
186 if (ret) {
187 g_error("error adding channel to main loop");
188 }
189 break;
190 }
191 case GA_CHANNEL_UNIX_LISTEN: {
90119816
PB
192 Error *local_err = NULL;
193 int fd = unix_listen(path, NULL, strlen(path), &local_err);
194 if (local_err != NULL) {
195 g_critical("%s", error_get_pretty(local_err));
196 error_free(local_err);
125b310e
MR
197 return false;
198 }
199 ga_channel_listen_add(c, fd, true);
200 break;
201 }
202 default:
203 g_critical("error binding/listening to specified socket");
204 return false;
205 }
206
207 return true;
208}
209
210GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size)
211{
212 GError *err = NULL;
213 gsize written = 0;
214 GIOStatus status = G_IO_STATUS_NORMAL;
215
216 while (size) {
217 status = g_io_channel_write_chars(c->client_channel, buf, size,
218 &written, &err);
219 g_debug("sending data, count: %d", (int)size);
220 if (err != NULL) {
221 g_warning("error writing to channel: %s", err->message);
222 return G_IO_STATUS_ERROR;
223 }
224 if (status != G_IO_STATUS_NORMAL) {
225 break;
226 }
227 size -= written;
228 }
229
230 if (status == G_IO_STATUS_NORMAL) {
231 status = g_io_channel_flush(c->client_channel, &err);
232 if (err != NULL) {
233 g_warning("error flushing channel: %s", err->message);
234 return G_IO_STATUS_ERROR;
235 }
236 }
237
238 return status;
239}
240
241GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
242{
243 return g_io_channel_read_chars(c->client_channel, buf, size, count, NULL);
244}
245
246GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
247 GAChannelCallback cb, gpointer opaque)
248{
249 GAChannel *c = g_malloc0(sizeof(GAChannel));
250 c->event_cb = cb;
251 c->user_data = opaque;
252
253 if (!ga_channel_open(c, path, method)) {
254 g_critical("error opening channel");
255 ga_channel_free(c);
256 return NULL;
257 }
258
259 return c;
260}
261
262void ga_channel_free(GAChannel *c)
263{
264 if (c->method == GA_CHANNEL_UNIX_LISTEN
265 && c->listen_channel) {
266 ga_channel_listen_close(c);
267 }
268 if (c->client_channel) {
269 ga_channel_client_close(c);
270 }
271 g_free(c);
272}