/src/qemu/io/channel-socket.c
Line | Count | Source |
1 | | /* |
2 | | * QEMU I/O channels sockets driver |
3 | | * |
4 | | * Copyright (c) 2015 Red Hat, Inc. |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "qemu/osdep.h" |
21 | | #include "qapi/error.h" |
22 | | #include "qapi/qapi-visit-sockets.h" |
23 | | #include "qemu/module.h" |
24 | | #include "io/channel-socket.h" |
25 | | #include "io/channel-util.h" |
26 | | #include "io/channel-watch.h" |
27 | | #include "trace.h" |
28 | | #include "qapi/clone-visitor.h" |
29 | | #ifdef CONFIG_LINUX |
30 | | #include <linux/errqueue.h> |
31 | | #include <sys/socket.h> |
32 | | |
33 | | #if (defined(MSG_ZEROCOPY) && defined(SO_ZEROCOPY)) |
34 | | #define QEMU_MSG_ZEROCOPY |
35 | | #endif |
36 | | #endif |
37 | | |
38 | 0 | #define SOCKET_MAX_FDS 16 |
39 | | |
40 | | #ifdef QEMU_MSG_ZEROCOPY |
41 | | static int qio_channel_socket_flush_internal(QIOChannel *ioc, |
42 | | bool block, |
43 | | Error **errp); |
44 | | #endif |
45 | | |
46 | | SocketAddress * |
47 | | qio_channel_socket_get_local_address(QIOChannelSocket *ioc, |
48 | | Error **errp) |
49 | 0 | { |
50 | 0 | return socket_sockaddr_to_address(&ioc->localAddr, |
51 | 0 | ioc->localAddrLen, |
52 | 0 | errp); |
53 | 0 | } |
54 | | |
55 | | SocketAddress * |
56 | | qio_channel_socket_get_remote_address(QIOChannelSocket *ioc, |
57 | | Error **errp) |
58 | 0 | { |
59 | 0 | return socket_sockaddr_to_address(&ioc->remoteAddr, |
60 | 0 | ioc->remoteAddrLen, |
61 | 0 | errp); |
62 | 0 | } |
63 | | |
64 | | QIOChannelSocket * |
65 | | qio_channel_socket_new(void) |
66 | 0 | { |
67 | 0 | QIOChannelSocket *sioc; |
68 | 0 | QIOChannel *ioc; |
69 | |
|
70 | 0 | sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET)); |
71 | 0 | sioc->fd = -1; |
72 | 0 | sioc->zero_copy_queued = 0; |
73 | 0 | sioc->zero_copy_sent = 0; |
74 | 0 | sioc->blocking = false; |
75 | 0 | sioc->zero_copy_fallback = false; |
76 | |
|
77 | 0 | ioc = QIO_CHANNEL(sioc); |
78 | 0 | qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN); |
79 | |
|
80 | | #ifdef WIN32 |
81 | | ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL); |
82 | | #endif |
83 | |
|
84 | 0 | trace_qio_channel_socket_new(sioc); |
85 | |
|
86 | 0 | return sioc; |
87 | 0 | } |
88 | | |
89 | | int qio_channel_socket_set_send_buffer(QIOChannelSocket *ioc, |
90 | | size_t size, |
91 | | Error **errp) |
92 | 0 | { |
93 | 0 | if (setsockopt(ioc->fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0) { |
94 | 0 | error_setg_errno(errp, errno, "Unable to set socket send buffer size"); |
95 | 0 | return -1; |
96 | 0 | } |
97 | | |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | static int |
102 | | qio_channel_socket_set_fd(QIOChannelSocket *sioc, |
103 | | int fd, |
104 | | Error **errp) |
105 | 0 | { |
106 | 0 | if (sioc->fd != -1) { |
107 | 0 | error_setg(errp, "Socket is already open"); |
108 | 0 | return -1; |
109 | 0 | } |
110 | | |
111 | 0 | sioc->fd = fd; |
112 | 0 | sioc->remoteAddrLen = sizeof(sioc->remoteAddr); |
113 | 0 | sioc->localAddrLen = sizeof(sioc->localAddr); |
114 | | |
115 | |
|
116 | 0 | if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr, |
117 | 0 | &sioc->remoteAddrLen) < 0) { |
118 | 0 | if (errno == ENOTCONN) { |
119 | 0 | memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr)); |
120 | 0 | sioc->remoteAddrLen = sizeof(sioc->remoteAddr); |
121 | 0 | } else { |
122 | 0 | error_setg_errno(errp, errno, |
123 | 0 | "Unable to query remote socket address"); |
124 | 0 | goto error; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | if (getsockname(fd, (struct sockaddr *)&sioc->localAddr, |
129 | 0 | &sioc->localAddrLen) < 0) { |
130 | 0 | error_setg_errno(errp, errno, |
131 | 0 | "Unable to query local socket address"); |
132 | 0 | goto error; |
133 | 0 | } |
134 | | |
135 | 0 | #ifndef WIN32 |
136 | 0 | if (sioc->localAddr.ss_family == AF_UNIX) { |
137 | 0 | QIOChannel *ioc = QIO_CHANNEL(sioc); |
138 | 0 | qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS); |
139 | 0 | } |
140 | 0 | #endif /* WIN32 */ |
141 | |
|
142 | 0 | return 0; |
143 | | |
144 | 0 | error: |
145 | 0 | sioc->fd = -1; /* Let the caller close FD on failure */ |
146 | 0 | return -1; |
147 | 0 | } |
148 | | |
149 | | QIOChannelSocket * |
150 | | qio_channel_socket_new_fd(int fd, |
151 | | Error **errp) |
152 | 0 | { |
153 | 0 | QIOChannelSocket *ioc; |
154 | |
|
155 | 0 | ioc = qio_channel_socket_new(); |
156 | 0 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { |
157 | 0 | object_unref(OBJECT(ioc)); |
158 | 0 | return NULL; |
159 | 0 | } |
160 | | |
161 | 0 | trace_qio_channel_socket_new_fd(ioc, fd); |
162 | |
|
163 | 0 | return ioc; |
164 | 0 | } |
165 | | |
166 | | |
167 | | int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, |
168 | | SocketAddress *addr, |
169 | | Error **errp) |
170 | 0 | { |
171 | 0 | int fd; |
172 | |
|
173 | 0 | trace_qio_channel_socket_connect_sync(ioc, addr); |
174 | 0 | fd = socket_connect(addr, errp); |
175 | 0 | if (fd < 0) { |
176 | 0 | trace_qio_channel_socket_connect_fail(ioc); |
177 | 0 | return -1; |
178 | 0 | } |
179 | | |
180 | 0 | trace_qio_channel_socket_connect_complete(ioc, fd); |
181 | 0 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { |
182 | 0 | close(fd); |
183 | 0 | return -1; |
184 | 0 | } |
185 | | |
186 | 0 | #ifdef QEMU_MSG_ZEROCOPY |
187 | 0 | int ret, v = 1; |
188 | 0 | ret = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v)); |
189 | 0 | if (ret == 0) { |
190 | | /* Zero copy available on host */ |
191 | 0 | qio_channel_set_feature(QIO_CHANNEL(ioc), |
192 | 0 | QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY); |
193 | 0 | } |
194 | 0 | #endif |
195 | |
|
196 | 0 | qio_channel_set_feature(QIO_CHANNEL(ioc), |
197 | 0 | QIO_CHANNEL_FEATURE_READ_MSG_PEEK); |
198 | |
|
199 | 0 | return 0; |
200 | 0 | } |
201 | | |
202 | | |
203 | | static void qio_channel_socket_connect_worker(QIOTask *task, |
204 | | gpointer opaque) |
205 | 0 | { |
206 | 0 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); |
207 | 0 | SocketAddress *addr = opaque; |
208 | 0 | Error *err = NULL; |
209 | |
|
210 | 0 | qio_channel_socket_connect_sync(ioc, addr, &err); |
211 | |
|
212 | 0 | qio_task_set_error(task, err); |
213 | 0 | } |
214 | | |
215 | | |
216 | | void qio_channel_socket_connect_async(QIOChannelSocket *ioc, |
217 | | SocketAddress *addr, |
218 | | QIOTaskFunc callback, |
219 | | gpointer opaque, |
220 | | GDestroyNotify destroy, |
221 | | GMainContext *context) |
222 | 0 | { |
223 | 0 | QIOTask *task = qio_task_new( |
224 | 0 | OBJECT(ioc), callback, opaque, destroy); |
225 | 0 | SocketAddress *addrCopy; |
226 | |
|
227 | 0 | addrCopy = QAPI_CLONE(SocketAddress, addr); |
228 | | |
229 | | /* socket_connect() does a non-blocking connect(), but it |
230 | | * still blocks in DNS lookups, so we must use a thread */ |
231 | 0 | trace_qio_channel_socket_connect_async(ioc, addr); |
232 | 0 | qio_task_run_in_thread(task, |
233 | 0 | qio_channel_socket_connect_worker, |
234 | 0 | addrCopy, |
235 | 0 | (GDestroyNotify)qapi_free_SocketAddress, |
236 | 0 | context); |
237 | 0 | } |
238 | | |
239 | | |
240 | | int qio_channel_socket_listen_sync(QIOChannelSocket *ioc, |
241 | | SocketAddress *addr, |
242 | | int num, |
243 | | Error **errp) |
244 | 0 | { |
245 | 0 | int fd; |
246 | |
|
247 | 0 | trace_qio_channel_socket_listen_sync(ioc, addr, num); |
248 | 0 | fd = socket_listen(addr, num, errp); |
249 | 0 | if (fd < 0) { |
250 | 0 | trace_qio_channel_socket_listen_fail(ioc); |
251 | 0 | return -1; |
252 | 0 | } |
253 | | |
254 | 0 | trace_qio_channel_socket_listen_complete(ioc, fd); |
255 | 0 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { |
256 | 0 | close(fd); |
257 | 0 | return -1; |
258 | 0 | } |
259 | 0 | qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_LISTEN); |
260 | |
|
261 | 0 | return 0; |
262 | 0 | } |
263 | | |
264 | | |
265 | | struct QIOChannelListenWorkerData { |
266 | | SocketAddress *addr; |
267 | | int num; /* amount of expected connections */ |
268 | | }; |
269 | | |
270 | | static void qio_channel_listen_worker_free(gpointer opaque) |
271 | 0 | { |
272 | 0 | struct QIOChannelListenWorkerData *data = opaque; |
273 | |
|
274 | 0 | qapi_free_SocketAddress(data->addr); |
275 | 0 | g_free(data); |
276 | 0 | } |
277 | | |
278 | | static void qio_channel_socket_listen_worker(QIOTask *task, |
279 | | gpointer opaque) |
280 | 0 | { |
281 | 0 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); |
282 | 0 | struct QIOChannelListenWorkerData *data = opaque; |
283 | 0 | Error *err = NULL; |
284 | |
|
285 | 0 | qio_channel_socket_listen_sync(ioc, data->addr, data->num, &err); |
286 | |
|
287 | 0 | qio_task_set_error(task, err); |
288 | 0 | } |
289 | | |
290 | | |
291 | | void qio_channel_socket_listen_async(QIOChannelSocket *ioc, |
292 | | SocketAddress *addr, |
293 | | int num, |
294 | | QIOTaskFunc callback, |
295 | | gpointer opaque, |
296 | | GDestroyNotify destroy, |
297 | | GMainContext *context) |
298 | 0 | { |
299 | 0 | QIOTask *task = qio_task_new( |
300 | 0 | OBJECT(ioc), callback, opaque, destroy); |
301 | 0 | struct QIOChannelListenWorkerData *data; |
302 | |
|
303 | 0 | data = g_new0(struct QIOChannelListenWorkerData, 1); |
304 | 0 | data->addr = QAPI_CLONE(SocketAddress, addr); |
305 | 0 | data->num = num; |
306 | | |
307 | | /* socket_listen() blocks in DNS lookups, so we must use a thread */ |
308 | 0 | trace_qio_channel_socket_listen_async(ioc, addr, num); |
309 | 0 | qio_task_run_in_thread(task, |
310 | 0 | qio_channel_socket_listen_worker, |
311 | 0 | data, |
312 | 0 | qio_channel_listen_worker_free, |
313 | 0 | context); |
314 | 0 | } |
315 | | |
316 | | |
317 | | int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc, |
318 | | SocketAddress *localAddr, |
319 | | SocketAddress *remoteAddr, |
320 | | Error **errp) |
321 | 0 | { |
322 | 0 | int fd; |
323 | |
|
324 | 0 | trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr); |
325 | 0 | fd = socket_dgram(remoteAddr, localAddr, errp); |
326 | 0 | if (fd < 0) { |
327 | 0 | trace_qio_channel_socket_dgram_fail(ioc); |
328 | 0 | return -1; |
329 | 0 | } |
330 | | |
331 | 0 | trace_qio_channel_socket_dgram_complete(ioc, fd); |
332 | 0 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { |
333 | 0 | close(fd); |
334 | 0 | return -1; |
335 | 0 | } |
336 | | |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | | |
341 | | struct QIOChannelSocketDGramWorkerData { |
342 | | SocketAddress *localAddr; |
343 | | SocketAddress *remoteAddr; |
344 | | }; |
345 | | |
346 | | |
347 | | static void qio_channel_socket_dgram_worker_free(gpointer opaque) |
348 | 0 | { |
349 | 0 | struct QIOChannelSocketDGramWorkerData *data = opaque; |
350 | 0 | qapi_free_SocketAddress(data->localAddr); |
351 | 0 | qapi_free_SocketAddress(data->remoteAddr); |
352 | 0 | g_free(data); |
353 | 0 | } |
354 | | |
355 | | static void qio_channel_socket_dgram_worker(QIOTask *task, |
356 | | gpointer opaque) |
357 | 0 | { |
358 | 0 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); |
359 | 0 | struct QIOChannelSocketDGramWorkerData *data = opaque; |
360 | 0 | Error *err = NULL; |
361 | | |
362 | | /* socket_dgram() blocks in DNS lookups, so we must use a thread */ |
363 | 0 | qio_channel_socket_dgram_sync(ioc, data->localAddr, |
364 | 0 | data->remoteAddr, &err); |
365 | |
|
366 | 0 | qio_task_set_error(task, err); |
367 | 0 | } |
368 | | |
369 | | |
370 | | void qio_channel_socket_dgram_async(QIOChannelSocket *ioc, |
371 | | SocketAddress *localAddr, |
372 | | SocketAddress *remoteAddr, |
373 | | QIOTaskFunc callback, |
374 | | gpointer opaque, |
375 | | GDestroyNotify destroy, |
376 | | GMainContext *context) |
377 | 0 | { |
378 | 0 | QIOTask *task = qio_task_new( |
379 | 0 | OBJECT(ioc), callback, opaque, destroy); |
380 | 0 | struct QIOChannelSocketDGramWorkerData *data = g_new0( |
381 | 0 | struct QIOChannelSocketDGramWorkerData, 1); |
382 | |
|
383 | 0 | data->localAddr = QAPI_CLONE(SocketAddress, localAddr); |
384 | 0 | data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr); |
385 | |
|
386 | 0 | trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr); |
387 | 0 | qio_task_run_in_thread(task, |
388 | 0 | qio_channel_socket_dgram_worker, |
389 | 0 | data, |
390 | 0 | qio_channel_socket_dgram_worker_free, |
391 | 0 | context); |
392 | 0 | } |
393 | | |
394 | | |
395 | | QIOChannelSocket * |
396 | | qio_channel_socket_accept(QIOChannelSocket *ioc, |
397 | | Error **errp) |
398 | 0 | { |
399 | 0 | QIOChannelSocket *cioc; |
400 | |
|
401 | 0 | cioc = qio_channel_socket_new(); |
402 | 0 | cioc->remoteAddrLen = sizeof(ioc->remoteAddr); |
403 | 0 | cioc->localAddrLen = sizeof(ioc->localAddr); |
404 | |
|
405 | 0 | retry: |
406 | 0 | trace_qio_channel_socket_accept(ioc); |
407 | 0 | cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr, |
408 | 0 | &cioc->remoteAddrLen); |
409 | 0 | if (cioc->fd < 0) { |
410 | 0 | if (errno == EINTR) { |
411 | 0 | goto retry; |
412 | 0 | } |
413 | 0 | error_setg_errno(errp, errno, "Unable to accept connection"); |
414 | 0 | trace_qio_channel_socket_accept_fail(ioc); |
415 | 0 | goto error; |
416 | 0 | } |
417 | | |
418 | 0 | if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr, |
419 | 0 | &cioc->localAddrLen) < 0) { |
420 | 0 | error_setg_errno(errp, errno, |
421 | 0 | "Unable to query local socket address"); |
422 | 0 | goto error; |
423 | 0 | } |
424 | | |
425 | 0 | #ifndef WIN32 |
426 | 0 | if (cioc->localAddr.ss_family == AF_UNIX) { |
427 | 0 | QIOChannel *ioc_local = QIO_CHANNEL(cioc); |
428 | 0 | qio_channel_set_feature(ioc_local, QIO_CHANNEL_FEATURE_FD_PASS); |
429 | 0 | } |
430 | 0 | #endif /* WIN32 */ |
431 | |
|
432 | 0 | qio_channel_set_feature(QIO_CHANNEL(cioc), |
433 | 0 | QIO_CHANNEL_FEATURE_READ_MSG_PEEK); |
434 | |
|
435 | 0 | trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd); |
436 | 0 | return cioc; |
437 | | |
438 | 0 | error: |
439 | 0 | object_unref(OBJECT(cioc)); |
440 | 0 | return NULL; |
441 | 0 | } |
442 | | |
443 | | static void qio_channel_socket_init(Object *obj) |
444 | 0 | { |
445 | 0 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj); |
446 | 0 | ioc->fd = -1; |
447 | 0 | } |
448 | | |
449 | | static void qio_channel_socket_finalize(Object *obj) |
450 | 0 | { |
451 | 0 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj); |
452 | |
|
453 | 0 | if (ioc->fd != -1) { |
454 | 0 | QIOChannel *ioc_local = QIO_CHANNEL(ioc); |
455 | 0 | if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) { |
456 | 0 | Error *err = NULL; |
457 | |
|
458 | 0 | socket_listen_cleanup(ioc->fd, &err); |
459 | 0 | if (err) { |
460 | 0 | error_report_err(err); |
461 | 0 | err = NULL; |
462 | 0 | } |
463 | 0 | } |
464 | | #ifdef WIN32 |
465 | | qemu_socket_unselect_nofail(ioc->fd); |
466 | | #endif |
467 | 0 | close(ioc->fd); |
468 | 0 | ioc->fd = -1; |
469 | 0 | } |
470 | 0 | } |
471 | | |
472 | | |
473 | | #ifndef WIN32 |
474 | | static void qio_channel_socket_copy_fds(struct msghdr *msg, |
475 | | int **fds, size_t *nfds) |
476 | 0 | { |
477 | 0 | struct cmsghdr *cmsg; |
478 | |
|
479 | 0 | *nfds = 0; |
480 | 0 | *fds = NULL; |
481 | |
|
482 | 0 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
483 | 0 | int fd_size; |
484 | 0 | int gotfds; |
485 | |
|
486 | 0 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) || |
487 | 0 | cmsg->cmsg_level != SOL_SOCKET || |
488 | 0 | cmsg->cmsg_type != SCM_RIGHTS) { |
489 | 0 | continue; |
490 | 0 | } |
491 | | |
492 | 0 | fd_size = cmsg->cmsg_len - CMSG_LEN(0); |
493 | |
|
494 | 0 | if (!fd_size) { |
495 | 0 | continue; |
496 | 0 | } |
497 | | |
498 | 0 | gotfds = fd_size / sizeof(int); |
499 | 0 | *fds = g_renew(int, *fds, *nfds + gotfds); |
500 | 0 | memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size); |
501 | 0 | *nfds += gotfds; |
502 | 0 | } |
503 | 0 | } |
504 | | |
505 | | static bool qio_channel_handle_fds(int *fds, size_t nfds, |
506 | | bool preserve_blocking, Error **errp) |
507 | 0 | { |
508 | 0 | int *end = fds + nfds, *fd; |
509 | |
|
510 | 0 | #ifdef MSG_CMSG_CLOEXEC |
511 | 0 | if (preserve_blocking) { |
512 | | /* Nothing to do */ |
513 | 0 | return true; |
514 | 0 | } |
515 | 0 | #endif |
516 | | |
517 | 0 | for (fd = fds; fd != end; fd++) { |
518 | 0 | if (*fd < 0) { |
519 | 0 | continue; |
520 | 0 | } |
521 | | |
522 | 0 | if (!preserve_blocking) { |
523 | | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ |
524 | 0 | if (!qemu_set_blocking(*fd, true, errp)) { |
525 | 0 | return false; |
526 | 0 | } |
527 | 0 | } |
528 | |
|
529 | | #ifndef MSG_CMSG_CLOEXEC |
530 | | qemu_set_cloexec(*fd); |
531 | | #endif |
532 | 0 | } |
533 | | |
534 | 0 | return true; |
535 | 0 | } |
536 | | |
537 | | static void qio_channel_cleanup_fds(int **fds, size_t *nfds) |
538 | 0 | { |
539 | 0 | for (size_t i = 0; i < *nfds; i++) { |
540 | 0 | if ((*fds)[i] < 0) { |
541 | 0 | continue; |
542 | 0 | } |
543 | 0 | close((*fds)[i]); |
544 | 0 | } |
545 | |
|
546 | 0 | g_clear_pointer(fds, g_free); |
547 | 0 | *nfds = 0; |
548 | 0 | } |
549 | | |
550 | | |
551 | | static ssize_t qio_channel_socket_readv(QIOChannel *ioc, |
552 | | const struct iovec *iov, |
553 | | size_t niov, |
554 | | int **fds, |
555 | | size_t *nfds, |
556 | | int flags, |
557 | | Error **errp) |
558 | 0 | { |
559 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
560 | 0 | ssize_t ret; |
561 | 0 | struct msghdr msg = { NULL, }; |
562 | 0 | char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)]; |
563 | 0 | int sflags = 0; |
564 | |
|
565 | 0 | memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)); |
566 | |
|
567 | 0 | msg.msg_iov = (struct iovec *)iov; |
568 | 0 | msg.msg_iovlen = niov; |
569 | 0 | if (fds && nfds) { |
570 | 0 | msg.msg_control = control; |
571 | 0 | msg.msg_controllen = sizeof(control); |
572 | 0 | #ifdef MSG_CMSG_CLOEXEC |
573 | 0 | sflags |= MSG_CMSG_CLOEXEC; |
574 | 0 | #endif |
575 | |
|
576 | 0 | } |
577 | |
|
578 | 0 | if (flags & QIO_CHANNEL_READ_FLAG_MSG_PEEK) { |
579 | 0 | sflags |= MSG_PEEK; |
580 | 0 | } |
581 | |
|
582 | 0 | retry: |
583 | 0 | ret = recvmsg(sioc->fd, &msg, sflags); |
584 | 0 | if (ret < 0) { |
585 | 0 | if (errno == EAGAIN) { |
586 | 0 | return QIO_CHANNEL_ERR_BLOCK; |
587 | 0 | } |
588 | 0 | if (errno == EINTR) { |
589 | 0 | goto retry; |
590 | 0 | } |
591 | | |
592 | 0 | error_setg_errno(errp, errno, |
593 | 0 | "Unable to read from socket"); |
594 | 0 | return -1; |
595 | 0 | } |
596 | | |
597 | 0 | if (fds && nfds) { |
598 | 0 | bool preserve_blocking = |
599 | 0 | flags & QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING; |
600 | |
|
601 | 0 | qio_channel_socket_copy_fds(&msg, fds, nfds); |
602 | |
|
603 | 0 | if (!qio_channel_handle_fds(*fds, *nfds, |
604 | 0 | preserve_blocking, errp)) { |
605 | 0 | qio_channel_cleanup_fds(fds, nfds); |
606 | 0 | return -1; |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | 0 | return ret; |
611 | 0 | } |
612 | | |
613 | | static ssize_t qio_channel_socket_writev(QIOChannel *ioc, |
614 | | const struct iovec *iov, |
615 | | size_t niov, |
616 | | int *fds, |
617 | | size_t nfds, |
618 | | int flags, |
619 | | Error **errp) |
620 | 0 | { |
621 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
622 | 0 | ssize_t ret; |
623 | 0 | struct msghdr msg = { NULL, }; |
624 | 0 | char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)]; |
625 | 0 | size_t fdsize = sizeof(int) * nfds; |
626 | 0 | struct cmsghdr *cmsg; |
627 | 0 | int sflags = 0; |
628 | 0 | #ifdef QEMU_MSG_ZEROCOPY |
629 | 0 | bool blocking = sioc->blocking; |
630 | 0 | bool zerocopy_flushed_once = false; |
631 | 0 | #endif |
632 | |
|
633 | 0 | memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)); |
634 | |
|
635 | 0 | msg.msg_iov = (struct iovec *)iov; |
636 | 0 | msg.msg_iovlen = niov; |
637 | |
|
638 | 0 | if (nfds) { |
639 | 0 | if (nfds > SOCKET_MAX_FDS) { |
640 | 0 | error_setg_errno(errp, EINVAL, |
641 | 0 | "Only %d FDs can be sent, got %zu", |
642 | 0 | SOCKET_MAX_FDS, nfds); |
643 | 0 | return -1; |
644 | 0 | } |
645 | | |
646 | 0 | msg.msg_control = control; |
647 | 0 | msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds); |
648 | |
|
649 | 0 | cmsg = CMSG_FIRSTHDR(&msg); |
650 | 0 | cmsg->cmsg_len = CMSG_LEN(fdsize); |
651 | 0 | cmsg->cmsg_level = SOL_SOCKET; |
652 | 0 | cmsg->cmsg_type = SCM_RIGHTS; |
653 | 0 | memcpy(CMSG_DATA(cmsg), fds, fdsize); |
654 | 0 | } |
655 | | |
656 | 0 | if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) { |
657 | 0 | #ifdef QEMU_MSG_ZEROCOPY |
658 | 0 | sflags = MSG_ZEROCOPY; |
659 | | #else |
660 | | /* |
661 | | * We expect QIOChannel class entry point to have |
662 | | * blocked this code path already |
663 | | */ |
664 | | g_assert_not_reached(); |
665 | | #endif |
666 | 0 | } |
667 | |
|
668 | 0 | retry: |
669 | 0 | ret = sendmsg(sioc->fd, &msg, sflags); |
670 | 0 | if (ret < 0) { |
671 | 0 | switch (errno) { |
672 | 0 | case EAGAIN: |
673 | 0 | return QIO_CHANNEL_ERR_BLOCK; |
674 | 0 | case EINTR: |
675 | 0 | goto retry; |
676 | 0 | #ifdef QEMU_MSG_ZEROCOPY |
677 | 0 | case ENOBUFS: |
678 | 0 | if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) { |
679 | | /** |
680 | | * Socket error queueing may exhaust the OPTMEM limit. Try |
681 | | * flushing the error queue once. |
682 | | */ |
683 | 0 | if (!zerocopy_flushed_once) { |
684 | 0 | ret = qio_channel_socket_flush_internal(ioc, blocking, |
685 | 0 | errp); |
686 | 0 | if (ret < 0) { |
687 | 0 | return -1; |
688 | 0 | } |
689 | 0 | zerocopy_flushed_once = true; |
690 | 0 | goto retry; |
691 | 0 | } else { |
692 | 0 | error_setg_errno(errp, errno, |
693 | 0 | "Process can't lock enough memory for " |
694 | 0 | "using MSG_ZEROCOPY"); |
695 | 0 | return -1; |
696 | 0 | } |
697 | 0 | } |
698 | 0 | break; |
699 | 0 | #endif |
700 | 0 | } |
701 | | |
702 | 0 | error_setg_errno(errp, errno, |
703 | 0 | "Unable to write to socket"); |
704 | 0 | return -1; |
705 | 0 | } |
706 | | |
707 | 0 | if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) { |
708 | 0 | sioc->zero_copy_queued++; |
709 | 0 | } |
710 | |
|
711 | 0 | return ret; |
712 | 0 | } |
713 | | #else /* WIN32 */ |
714 | | static ssize_t qio_channel_socket_readv(QIOChannel *ioc, |
715 | | const struct iovec *iov, |
716 | | size_t niov, |
717 | | int **fds, |
718 | | size_t *nfds, |
719 | | int flags, |
720 | | Error **errp) |
721 | | { |
722 | | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
723 | | ssize_t done = 0; |
724 | | ssize_t i; |
725 | | int sflags = 0; |
726 | | |
727 | | if (flags & QIO_CHANNEL_READ_FLAG_MSG_PEEK) { |
728 | | sflags |= MSG_PEEK; |
729 | | } |
730 | | |
731 | | for (i = 0; i < niov; i++) { |
732 | | ssize_t ret; |
733 | | retry: |
734 | | ret = recv(sioc->fd, |
735 | | iov[i].iov_base, |
736 | | iov[i].iov_len, |
737 | | sflags); |
738 | | if (ret < 0) { |
739 | | if (errno == EAGAIN) { |
740 | | if (done) { |
741 | | return done; |
742 | | } else { |
743 | | return QIO_CHANNEL_ERR_BLOCK; |
744 | | } |
745 | | } else if (errno == EINTR) { |
746 | | goto retry; |
747 | | } else { |
748 | | error_setg_errno(errp, errno, |
749 | | "Unable to read from socket"); |
750 | | return -1; |
751 | | } |
752 | | } |
753 | | done += ret; |
754 | | if (ret < iov[i].iov_len) { |
755 | | return done; |
756 | | } |
757 | | } |
758 | | |
759 | | return done; |
760 | | } |
761 | | |
762 | | static ssize_t qio_channel_socket_writev(QIOChannel *ioc, |
763 | | const struct iovec *iov, |
764 | | size_t niov, |
765 | | int *fds, |
766 | | size_t nfds, |
767 | | int flags, |
768 | | Error **errp) |
769 | | { |
770 | | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
771 | | ssize_t done = 0; |
772 | | ssize_t i; |
773 | | |
774 | | for (i = 0; i < niov; i++) { |
775 | | ssize_t ret; |
776 | | retry: |
777 | | ret = send(sioc->fd, |
778 | | iov[i].iov_base, |
779 | | iov[i].iov_len, |
780 | | 0); |
781 | | if (ret < 0) { |
782 | | if (errno == EAGAIN) { |
783 | | if (done) { |
784 | | return done; |
785 | | } else { |
786 | | return QIO_CHANNEL_ERR_BLOCK; |
787 | | } |
788 | | } else if (errno == EINTR) { |
789 | | goto retry; |
790 | | } else { |
791 | | error_setg_errno(errp, errno, |
792 | | "Unable to write to socket"); |
793 | | return -1; |
794 | | } |
795 | | } |
796 | | done += ret; |
797 | | if (ret < iov[i].iov_len) { |
798 | | return done; |
799 | | } |
800 | | } |
801 | | |
802 | | return done; |
803 | | } |
804 | | #endif /* WIN32 */ |
805 | | |
806 | | |
807 | | #ifdef QEMU_MSG_ZEROCOPY |
808 | | static int qio_channel_socket_flush_internal(QIOChannel *ioc, |
809 | | bool block, |
810 | | Error **errp) |
811 | 0 | { |
812 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
813 | 0 | struct msghdr msg = {}; |
814 | 0 | struct sock_extended_err *serr; |
815 | 0 | struct cmsghdr *cm; |
816 | 0 | char control[CMSG_SPACE(sizeof(*serr))]; |
817 | 0 | int received; |
818 | |
|
819 | 0 | if (sioc->zero_copy_queued == sioc->zero_copy_sent) { |
820 | 0 | return 0; |
821 | 0 | } |
822 | | |
823 | 0 | msg.msg_control = control; |
824 | 0 | msg.msg_controllen = sizeof(control); |
825 | 0 | memset(control, 0, sizeof(control)); |
826 | |
|
827 | 0 | while (sioc->zero_copy_sent < sioc->zero_copy_queued) { |
828 | 0 | received = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE); |
829 | 0 | if (received < 0) { |
830 | 0 | switch (errno) { |
831 | 0 | case EAGAIN: |
832 | 0 | if (block) { |
833 | | /* |
834 | | * Nothing on errqueue, wait until something is |
835 | | * available. |
836 | | * |
837 | | * Use G_IO_ERR instead of G_IO_IN since MSG_ERRQUEUE reads |
838 | | * are signaled via POLLERR, not POLLIN, as the kernel |
839 | | * sets POLLERR when zero-copy notificatons appear on the |
840 | | * socket error queue. |
841 | | */ |
842 | 0 | qio_channel_wait(ioc, G_IO_ERR); |
843 | 0 | continue; |
844 | 0 | } |
845 | 0 | return 0; |
846 | 0 | case EINTR: |
847 | 0 | continue; |
848 | 0 | default: |
849 | 0 | error_setg_errno(errp, errno, |
850 | 0 | "Unable to read errqueue"); |
851 | 0 | return -1; |
852 | 0 | } |
853 | 0 | } |
854 | | |
855 | 0 | cm = CMSG_FIRSTHDR(&msg); |
856 | 0 | if (cm->cmsg_level != SOL_IP && cm->cmsg_type != IP_RECVERR && |
857 | 0 | cm->cmsg_level != SOL_IPV6 && cm->cmsg_type != IPV6_RECVERR) { |
858 | 0 | error_setg_errno(errp, EPROTOTYPE, |
859 | 0 | "Wrong cmsg in errqueue"); |
860 | 0 | return -1; |
861 | 0 | } |
862 | | |
863 | 0 | serr = (void *) CMSG_DATA(cm); |
864 | 0 | if (serr->ee_errno != SO_EE_ORIGIN_NONE) { |
865 | 0 | error_setg_errno(errp, serr->ee_errno, |
866 | 0 | "Error on socket"); |
867 | 0 | return -1; |
868 | 0 | } |
869 | 0 | if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) { |
870 | 0 | error_setg_errno(errp, serr->ee_origin, |
871 | 0 | "Error not from zero copy"); |
872 | 0 | return -1; |
873 | 0 | } |
874 | 0 | if (serr->ee_data < serr->ee_info) { |
875 | 0 | error_setg_errno(errp, serr->ee_origin, |
876 | 0 | "Wrong notification bounds"); |
877 | 0 | return -1; |
878 | 0 | } |
879 | | |
880 | | /* No errors, count successfully finished sendmsg()*/ |
881 | 0 | sioc->zero_copy_sent += serr->ee_data - serr->ee_info + 1; |
882 | |
|
883 | 0 | if (serr->ee_code == SO_EE_CODE_ZEROCOPY_COPIED) { |
884 | | /* If any sendmsg() fell back to a copy, mark fallback as true */ |
885 | 0 | sioc->zero_copy_fallback = true; |
886 | 0 | } |
887 | 0 | } |
888 | | |
889 | 0 | return 0; |
890 | 0 | } |
891 | | |
892 | | static int qio_channel_socket_flush(QIOChannel *ioc, |
893 | | Error **errp) |
894 | 0 | { |
895 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
896 | 0 | int ret; |
897 | |
|
898 | 0 | ret = qio_channel_socket_flush_internal(ioc, true, errp); |
899 | 0 | if (ret < 0) { |
900 | 0 | return ret; |
901 | 0 | } |
902 | | |
903 | 0 | if (sioc->zero_copy_fallback) { |
904 | 0 | sioc->zero_copy_fallback = false; |
905 | 0 | return 1; |
906 | 0 | } |
907 | | |
908 | 0 | return 0; |
909 | 0 | } |
910 | | |
911 | | #endif /* QEMU_MSG_ZEROCOPY */ |
912 | | |
913 | | static int |
914 | | qio_channel_socket_set_blocking(QIOChannel *ioc, |
915 | | bool enabled, |
916 | | Error **errp) |
917 | 0 | { |
918 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
919 | 0 | sioc->blocking = enabled; |
920 | |
|
921 | 0 | if (!qemu_set_blocking(sioc->fd, enabled, errp)) { |
922 | 0 | return -1; |
923 | 0 | } |
924 | | |
925 | 0 | return 0; |
926 | 0 | } |
927 | | |
928 | | |
929 | | static void |
930 | | qio_channel_socket_set_delay(QIOChannel *ioc, |
931 | | bool enabled) |
932 | 0 | { |
933 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
934 | 0 | int v = enabled ? 0 : 1; |
935 | |
|
936 | 0 | setsockopt(sioc->fd, |
937 | 0 | IPPROTO_TCP, TCP_NODELAY, |
938 | 0 | &v, sizeof(v)); |
939 | 0 | } |
940 | | |
941 | | |
942 | | static void |
943 | | qio_channel_socket_set_cork(QIOChannel *ioc, |
944 | | bool enabled) |
945 | 0 | { |
946 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
947 | 0 | int v = enabled ? 1 : 0; |
948 | | |
949 | | /* |
950 | | * We can ignore the error return from socket_set_cork() because |
951 | | * at the QIO API level set_cork is only a hint, and so |
952 | | * qio_channel_set_cork() can never fail even if it didn't |
953 | | * actually do anything. |
954 | | */ |
955 | 0 | socket_set_cork(sioc->fd, v); |
956 | 0 | } |
957 | | |
958 | | static int |
959 | | qio_channel_socket_get_peerpid(QIOChannel *ioc, |
960 | | unsigned int *pid, |
961 | | Error **errp) |
962 | 0 | { |
963 | 0 | #ifdef CONFIG_LINUX |
964 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
965 | 0 | Error *err = NULL; |
966 | 0 | socklen_t len = sizeof(struct ucred); |
967 | |
|
968 | 0 | struct ucred cred; |
969 | 0 | if (getsockopt(sioc->fd, |
970 | 0 | SOL_SOCKET, SO_PEERCRED, |
971 | 0 | &cred, &len) == -1) { |
972 | 0 | error_setg_errno(&err, errno, "Unable to get peer credentials"); |
973 | 0 | error_propagate(errp, err); |
974 | 0 | *pid = -1; |
975 | 0 | return -1; |
976 | 0 | } |
977 | 0 | *pid = (unsigned int)cred.pid; |
978 | 0 | return 0; |
979 | | #else |
980 | | error_setg(errp, "Unsupported feature"); |
981 | | *pid = -1; |
982 | | return -1; |
983 | | #endif |
984 | 0 | } |
985 | | |
986 | | static int |
987 | | qio_channel_socket_close(QIOChannel *ioc, |
988 | | Error **errp) |
989 | 0 | { |
990 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
991 | 0 | int rc = 0; |
992 | 0 | Error *err = NULL; |
993 | |
|
994 | 0 | if (sioc->fd != -1) { |
995 | | #ifdef WIN32 |
996 | | qemu_socket_unselect_nofail(sioc->fd); |
997 | | #endif |
998 | 0 | if (qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_LISTEN)) { |
999 | 0 | socket_listen_cleanup(sioc->fd, errp); |
1000 | 0 | } |
1001 | |
|
1002 | 0 | if (close(sioc->fd) < 0) { |
1003 | 0 | sioc->fd = -1; |
1004 | 0 | error_setg_errno(&err, errno, "Unable to close socket"); |
1005 | 0 | error_propagate(errp, err); |
1006 | 0 | return -1; |
1007 | 0 | } |
1008 | 0 | sioc->fd = -1; |
1009 | 0 | } |
1010 | 0 | return rc; |
1011 | 0 | } |
1012 | | |
1013 | | static int |
1014 | | qio_channel_socket_shutdown(QIOChannel *ioc, |
1015 | | QIOChannelShutdown how, |
1016 | | Error **errp) |
1017 | 0 | { |
1018 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
1019 | 0 | int sockhow; |
1020 | |
|
1021 | 0 | switch (how) { |
1022 | 0 | case QIO_CHANNEL_SHUTDOWN_READ: |
1023 | 0 | sockhow = SHUT_RD; |
1024 | 0 | break; |
1025 | 0 | case QIO_CHANNEL_SHUTDOWN_WRITE: |
1026 | 0 | sockhow = SHUT_WR; |
1027 | 0 | break; |
1028 | 0 | case QIO_CHANNEL_SHUTDOWN_BOTH: |
1029 | 0 | default: |
1030 | 0 | sockhow = SHUT_RDWR; |
1031 | 0 | break; |
1032 | 0 | } |
1033 | | |
1034 | 0 | if (shutdown(sioc->fd, sockhow) < 0) { |
1035 | 0 | error_setg_errno(errp, errno, |
1036 | 0 | "Unable to shutdown socket"); |
1037 | 0 | return -1; |
1038 | 0 | } |
1039 | 0 | return 0; |
1040 | 0 | } |
1041 | | |
1042 | | static void qio_channel_socket_set_aio_fd_handler(QIOChannel *ioc, |
1043 | | AioContext *read_ctx, |
1044 | | IOHandler *io_read, |
1045 | | AioContext *write_ctx, |
1046 | | IOHandler *io_write, |
1047 | | void *opaque) |
1048 | 0 | { |
1049 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
1050 | |
|
1051 | 0 | qio_channel_util_set_aio_fd_handler(sioc->fd, read_ctx, io_read, |
1052 | 0 | sioc->fd, write_ctx, io_write, |
1053 | 0 | opaque); |
1054 | 0 | } |
1055 | | |
1056 | | static GSource *qio_channel_socket_create_watch(QIOChannel *ioc, |
1057 | | GIOCondition condition) |
1058 | 0 | { |
1059 | 0 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); |
1060 | 0 | return qio_channel_create_socket_watch(ioc, |
1061 | 0 | sioc->fd, |
1062 | 0 | condition); |
1063 | 0 | } |
1064 | | |
1065 | | static void qio_channel_socket_class_init(ObjectClass *klass, |
1066 | | const void *class_data G_GNUC_UNUSED) |
1067 | 61 | { |
1068 | 61 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); |
1069 | | |
1070 | 61 | ioc_klass->io_writev = qio_channel_socket_writev; |
1071 | 61 | ioc_klass->io_readv = qio_channel_socket_readv; |
1072 | 61 | ioc_klass->io_set_blocking = qio_channel_socket_set_blocking; |
1073 | 61 | ioc_klass->io_close = qio_channel_socket_close; |
1074 | 61 | ioc_klass->io_shutdown = qio_channel_socket_shutdown; |
1075 | 61 | ioc_klass->io_set_cork = qio_channel_socket_set_cork; |
1076 | 61 | ioc_klass->io_set_delay = qio_channel_socket_set_delay; |
1077 | 61 | ioc_klass->io_create_watch = qio_channel_socket_create_watch; |
1078 | 61 | ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler; |
1079 | 61 | #ifdef QEMU_MSG_ZEROCOPY |
1080 | 61 | ioc_klass->io_flush = qio_channel_socket_flush; |
1081 | 61 | #endif |
1082 | 61 | ioc_klass->io_peerpid = qio_channel_socket_get_peerpid; |
1083 | 61 | } |
1084 | | |
1085 | | static const TypeInfo qio_channel_socket_info = { |
1086 | | .parent = TYPE_QIO_CHANNEL, |
1087 | | .name = TYPE_QIO_CHANNEL_SOCKET, |
1088 | | .instance_size = sizeof(QIOChannelSocket), |
1089 | | .instance_init = qio_channel_socket_init, |
1090 | | .instance_finalize = qio_channel_socket_finalize, |
1091 | | .class_init = qio_channel_socket_class_init, |
1092 | | }; |
1093 | | |
1094 | | static void qio_channel_socket_register_types(void) |
1095 | 61 | { |
1096 | 61 | type_register_static(&qio_channel_socket_info); |
1097 | 61 | } |
1098 | | |
1099 | | type_init(qio_channel_socket_register_types); |