]>
Commit | Line | Data |
---|---|---|
559607ea DB |
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 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 | ||
21 | #include "io/channel-socket.h" | |
22 | #include "io/channel-watch.h" | |
23 | #include "trace.h" | |
24 | ||
25 | #define SOCKET_MAX_FDS 16 | |
26 | ||
27 | SocketAddress * | |
28 | qio_channel_socket_get_local_address(QIOChannelSocket *ioc, | |
29 | Error **errp) | |
30 | { | |
31 | return socket_sockaddr_to_address(&ioc->localAddr, | |
32 | ioc->localAddrLen, | |
33 | errp); | |
34 | } | |
35 | ||
36 | SocketAddress * | |
37 | qio_channel_socket_get_remote_address(QIOChannelSocket *ioc, | |
38 | Error **errp) | |
39 | { | |
40 | return socket_sockaddr_to_address(&ioc->remoteAddr, | |
41 | ioc->remoteAddrLen, | |
42 | errp); | |
43 | } | |
44 | ||
45 | QIOChannelSocket * | |
46 | qio_channel_socket_new(void) | |
47 | { | |
48 | QIOChannelSocket *sioc; | |
49 | QIOChannel *ioc; | |
50 | ||
51 | sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET)); | |
52 | sioc->fd = -1; | |
53 | ||
54 | ioc = QIO_CHANNEL(sioc); | |
55 | ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN); | |
56 | ||
57 | trace_qio_channel_socket_new(sioc); | |
58 | ||
59 | return sioc; | |
60 | } | |
61 | ||
62 | ||
63 | static int | |
64 | qio_channel_socket_set_fd(QIOChannelSocket *sioc, | |
65 | int fd, | |
66 | Error **errp) | |
67 | { | |
68 | if (sioc->fd != -1) { | |
69 | error_setg(errp, "Socket is already open"); | |
70 | return -1; | |
71 | } | |
72 | ||
73 | sioc->fd = fd; | |
74 | sioc->remoteAddrLen = sizeof(sioc->remoteAddr); | |
75 | sioc->localAddrLen = sizeof(sioc->localAddr); | |
76 | ||
77 | ||
78 | if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr, | |
79 | &sioc->remoteAddrLen) < 0) { | |
80 | if (socket_error() == ENOTCONN) { | |
81 | memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr)); | |
82 | sioc->remoteAddrLen = sizeof(sioc->remoteAddr); | |
83 | } else { | |
84 | error_setg_errno(errp, socket_error(), | |
85 | "Unable to query remote socket address"); | |
86 | goto error; | |
87 | } | |
88 | } | |
89 | ||
90 | if (getsockname(fd, (struct sockaddr *)&sioc->localAddr, | |
91 | &sioc->localAddrLen) < 0) { | |
92 | error_setg_errno(errp, socket_error(), | |
93 | "Unable to query local socket address"); | |
94 | goto error; | |
95 | } | |
96 | ||
97 | #ifndef WIN32 | |
98 | if (sioc->localAddr.ss_family == AF_UNIX) { | |
99 | QIOChannel *ioc = QIO_CHANNEL(sioc); | |
100 | ioc->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS); | |
101 | } | |
102 | #endif /* WIN32 */ | |
103 | ||
104 | return 0; | |
105 | ||
106 | error: | |
107 | sioc->fd = -1; /* Let the caller close FD on failure */ | |
108 | return -1; | |
109 | } | |
110 | ||
111 | QIOChannelSocket * | |
112 | qio_channel_socket_new_fd(int fd, | |
113 | Error **errp) | |
114 | { | |
115 | QIOChannelSocket *ioc; | |
116 | ||
117 | ioc = qio_channel_socket_new(); | |
118 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { | |
119 | object_unref(OBJECT(ioc)); | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | trace_qio_channel_socket_new_fd(ioc, fd); | |
124 | ||
125 | return ioc; | |
126 | } | |
127 | ||
128 | ||
129 | int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, | |
130 | SocketAddress *addr, | |
131 | Error **errp) | |
132 | { | |
133 | int fd; | |
134 | ||
135 | trace_qio_channel_socket_connect_sync(ioc, addr); | |
136 | fd = socket_connect(addr, errp, NULL, NULL); | |
137 | if (fd < 0) { | |
138 | trace_qio_channel_socket_connect_fail(ioc); | |
139 | return -1; | |
140 | } | |
141 | ||
142 | trace_qio_channel_socket_connect_complete(ioc, fd); | |
143 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { | |
144 | close(fd); | |
145 | return -1; | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | ||
152 | static int qio_channel_socket_connect_worker(QIOTask *task, | |
153 | Error **errp, | |
154 | gpointer opaque) | |
155 | { | |
156 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); | |
157 | SocketAddress *addr = opaque; | |
158 | int ret; | |
159 | ||
160 | ret = qio_channel_socket_connect_sync(ioc, | |
161 | addr, | |
162 | errp); | |
163 | ||
164 | object_unref(OBJECT(ioc)); | |
165 | return ret; | |
166 | } | |
167 | ||
168 | ||
169 | void qio_channel_socket_connect_async(QIOChannelSocket *ioc, | |
170 | SocketAddress *addr, | |
171 | QIOTaskFunc callback, | |
172 | gpointer opaque, | |
173 | GDestroyNotify destroy) | |
174 | { | |
175 | QIOTask *task = qio_task_new( | |
176 | OBJECT(ioc), callback, opaque, destroy); | |
177 | SocketAddress *addrCopy; | |
178 | ||
179 | qapi_copy_SocketAddress(&addrCopy, addr); | |
180 | ||
181 | /* socket_connect() does a non-blocking connect(), but it | |
182 | * still blocks in DNS lookups, so we must use a thread */ | |
183 | trace_qio_channel_socket_connect_async(ioc, addr); | |
184 | qio_task_run_in_thread(task, | |
185 | qio_channel_socket_connect_worker, | |
186 | addrCopy, | |
187 | (GDestroyNotify)qapi_free_SocketAddress); | |
188 | } | |
189 | ||
190 | ||
191 | int qio_channel_socket_listen_sync(QIOChannelSocket *ioc, | |
192 | SocketAddress *addr, | |
193 | Error **errp) | |
194 | { | |
195 | int fd; | |
196 | ||
197 | trace_qio_channel_socket_listen_sync(ioc, addr); | |
198 | fd = socket_listen(addr, errp); | |
199 | if (fd < 0) { | |
200 | trace_qio_channel_socket_listen_fail(ioc); | |
201 | return -1; | |
202 | } | |
203 | ||
204 | trace_qio_channel_socket_listen_complete(ioc, fd); | |
205 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { | |
206 | close(fd); | |
207 | return -1; | |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | ||
214 | static int qio_channel_socket_listen_worker(QIOTask *task, | |
215 | Error **errp, | |
216 | gpointer opaque) | |
217 | { | |
218 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); | |
219 | SocketAddress *addr = opaque; | |
220 | int ret; | |
221 | ||
222 | ret = qio_channel_socket_listen_sync(ioc, | |
223 | addr, | |
224 | errp); | |
225 | ||
226 | object_unref(OBJECT(ioc)); | |
227 | return ret; | |
228 | } | |
229 | ||
230 | ||
231 | void qio_channel_socket_listen_async(QIOChannelSocket *ioc, | |
232 | SocketAddress *addr, | |
233 | QIOTaskFunc callback, | |
234 | gpointer opaque, | |
235 | GDestroyNotify destroy) | |
236 | { | |
237 | QIOTask *task = qio_task_new( | |
238 | OBJECT(ioc), callback, opaque, destroy); | |
239 | SocketAddress *addrCopy; | |
240 | ||
241 | qapi_copy_SocketAddress(&addrCopy, addr); | |
242 | ||
243 | /* socket_listen() blocks in DNS lookups, so we must use a thread */ | |
244 | trace_qio_channel_socket_listen_async(ioc, addr); | |
245 | qio_task_run_in_thread(task, | |
246 | qio_channel_socket_listen_worker, | |
247 | addrCopy, | |
248 | (GDestroyNotify)qapi_free_SocketAddress); | |
249 | } | |
250 | ||
251 | ||
252 | int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc, | |
253 | SocketAddress *localAddr, | |
254 | SocketAddress *remoteAddr, | |
255 | Error **errp) | |
256 | { | |
257 | int fd; | |
258 | ||
259 | trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr); | |
260 | fd = socket_dgram(localAddr, remoteAddr, errp); | |
261 | if (fd < 0) { | |
262 | trace_qio_channel_socket_dgram_fail(ioc); | |
263 | return -1; | |
264 | } | |
265 | ||
266 | trace_qio_channel_socket_dgram_complete(ioc, fd); | |
267 | if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) { | |
268 | close(fd); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
275 | ||
276 | struct QIOChannelSocketDGramWorkerData { | |
277 | SocketAddress *localAddr; | |
278 | SocketAddress *remoteAddr; | |
279 | }; | |
280 | ||
281 | ||
282 | static void qio_channel_socket_dgram_worker_free(gpointer opaque) | |
283 | { | |
284 | struct QIOChannelSocketDGramWorkerData *data = opaque; | |
285 | qapi_free_SocketAddress(data->localAddr); | |
286 | qapi_free_SocketAddress(data->remoteAddr); | |
287 | g_free(data); | |
288 | } | |
289 | ||
290 | static int qio_channel_socket_dgram_worker(QIOTask *task, | |
291 | Error **errp, | |
292 | gpointer opaque) | |
293 | { | |
294 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); | |
295 | struct QIOChannelSocketDGramWorkerData *data = opaque; | |
296 | int ret; | |
297 | ||
298 | /* socket_dgram() blocks in DNS lookups, so we must use a thread */ | |
299 | ret = qio_channel_socket_dgram_sync(ioc, | |
300 | data->localAddr, | |
301 | data->remoteAddr, | |
302 | errp); | |
303 | ||
304 | object_unref(OBJECT(ioc)); | |
305 | return ret; | |
306 | } | |
307 | ||
308 | ||
309 | void qio_channel_socket_dgram_async(QIOChannelSocket *ioc, | |
310 | SocketAddress *localAddr, | |
311 | SocketAddress *remoteAddr, | |
312 | QIOTaskFunc callback, | |
313 | gpointer opaque, | |
314 | GDestroyNotify destroy) | |
315 | { | |
316 | QIOTask *task = qio_task_new( | |
317 | OBJECT(ioc), callback, opaque, destroy); | |
318 | struct QIOChannelSocketDGramWorkerData *data = g_new0( | |
319 | struct QIOChannelSocketDGramWorkerData, 1); | |
320 | ||
321 | qapi_copy_SocketAddress(&data->localAddr, localAddr); | |
322 | qapi_copy_SocketAddress(&data->remoteAddr, remoteAddr); | |
323 | ||
324 | trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr); | |
325 | qio_task_run_in_thread(task, | |
326 | qio_channel_socket_dgram_worker, | |
327 | data, | |
328 | qio_channel_socket_dgram_worker_free); | |
329 | } | |
330 | ||
331 | ||
332 | QIOChannelSocket * | |
333 | qio_channel_socket_accept(QIOChannelSocket *ioc, | |
334 | Error **errp) | |
335 | { | |
336 | QIOChannelSocket *cioc; | |
337 | ||
338 | cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET)); | |
339 | cioc->fd = -1; | |
340 | cioc->remoteAddrLen = sizeof(ioc->remoteAddr); | |
341 | cioc->localAddrLen = sizeof(ioc->localAddr); | |
342 | ||
343 | retry: | |
344 | trace_qio_channel_socket_accept(ioc); | |
345 | cioc->fd = accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr, | |
346 | &cioc->remoteAddrLen); | |
347 | if (cioc->fd < 0) { | |
348 | trace_qio_channel_socket_accept_fail(ioc); | |
349 | if (socket_error() == EINTR) { | |
350 | goto retry; | |
351 | } | |
352 | goto error; | |
353 | } | |
354 | ||
355 | if (getsockname(cioc->fd, (struct sockaddr *)&ioc->localAddr, | |
356 | &ioc->localAddrLen) < 0) { | |
357 | error_setg_errno(errp, socket_error(), | |
358 | "Unable to query local socket address"); | |
359 | goto error; | |
360 | } | |
361 | ||
362 | trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd); | |
363 | return cioc; | |
364 | ||
365 | error: | |
366 | object_unref(OBJECT(cioc)); | |
367 | return NULL; | |
368 | } | |
369 | ||
370 | static void qio_channel_socket_init(Object *obj) | |
371 | { | |
372 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj); | |
373 | ioc->fd = -1; | |
374 | } | |
375 | ||
376 | static void qio_channel_socket_finalize(Object *obj) | |
377 | { | |
378 | QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj); | |
379 | if (ioc->fd != -1) { | |
380 | close(ioc->fd); | |
381 | ioc->fd = -1; | |
382 | } | |
383 | } | |
384 | ||
385 | ||
386 | #ifndef WIN32 | |
387 | static void qio_channel_socket_copy_fds(struct msghdr *msg, | |
388 | int **fds, size_t *nfds) | |
389 | { | |
390 | struct cmsghdr *cmsg; | |
391 | ||
392 | *nfds = 0; | |
393 | *fds = NULL; | |
394 | ||
395 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
396 | int fd_size, i; | |
397 | int gotfds; | |
398 | ||
399 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) || | |
400 | cmsg->cmsg_level != SOL_SOCKET || | |
401 | cmsg->cmsg_type != SCM_RIGHTS) { | |
402 | continue; | |
403 | } | |
404 | ||
405 | fd_size = cmsg->cmsg_len - CMSG_LEN(0); | |
406 | ||
407 | if (!fd_size) { | |
408 | continue; | |
409 | } | |
410 | ||
411 | gotfds = fd_size / sizeof(int); | |
412 | *fds = g_renew(int, *fds, *nfds + gotfds); | |
413 | memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size); | |
414 | ||
415 | for (i = 0; i < gotfds; i++) { | |
416 | int fd = (*fds)[*nfds + i]; | |
417 | if (fd < 0) { | |
418 | continue; | |
419 | } | |
420 | ||
421 | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ | |
422 | qemu_set_block(fd); | |
423 | ||
424 | #ifndef MSG_CMSG_CLOEXEC | |
425 | qemu_set_cloexec(fd); | |
426 | #endif | |
427 | } | |
428 | *nfds += gotfds; | |
429 | } | |
430 | } | |
431 | ||
432 | ||
433 | static ssize_t qio_channel_socket_readv(QIOChannel *ioc, | |
434 | const struct iovec *iov, | |
435 | size_t niov, | |
436 | int **fds, | |
437 | size_t *nfds, | |
438 | Error **errp) | |
439 | { | |
440 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
441 | ssize_t ret; | |
442 | struct msghdr msg = { NULL, }; | |
443 | char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)]; | |
444 | int sflags = 0; | |
445 | ||
446 | #ifdef MSG_CMSG_CLOEXEC | |
447 | sflags |= MSG_CMSG_CLOEXEC; | |
448 | #endif | |
449 | ||
450 | msg.msg_iov = (struct iovec *)iov; | |
451 | msg.msg_iovlen = niov; | |
452 | if (fds && nfds) { | |
453 | msg.msg_control = control; | |
454 | msg.msg_controllen = sizeof(control); | |
455 | } | |
456 | ||
457 | retry: | |
458 | ret = recvmsg(sioc->fd, &msg, sflags); | |
459 | if (ret < 0) { | |
460 | if (socket_error() == EAGAIN || | |
461 | socket_error() == EWOULDBLOCK) { | |
462 | return QIO_CHANNEL_ERR_BLOCK; | |
463 | } | |
464 | if (socket_error() == EINTR) { | |
465 | goto retry; | |
466 | } | |
467 | ||
468 | error_setg_errno(errp, socket_error(), | |
469 | "Unable to read from socket"); | |
470 | return -1; | |
471 | } | |
472 | ||
473 | if (fds && nfds) { | |
474 | qio_channel_socket_copy_fds(&msg, fds, nfds); | |
475 | } | |
476 | ||
477 | return ret; | |
478 | } | |
479 | ||
480 | static ssize_t qio_channel_socket_writev(QIOChannel *ioc, | |
481 | const struct iovec *iov, | |
482 | size_t niov, | |
483 | int *fds, | |
484 | size_t nfds, | |
485 | Error **errp) | |
486 | { | |
487 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
488 | ssize_t ret; | |
489 | struct msghdr msg = { NULL, }; | |
490 | ||
491 | msg.msg_iov = (struct iovec *)iov; | |
492 | msg.msg_iovlen = niov; | |
493 | ||
494 | if (nfds) { | |
495 | char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)]; | |
496 | size_t fdsize = sizeof(int) * nfds; | |
497 | struct cmsghdr *cmsg; | |
498 | ||
499 | if (nfds > SOCKET_MAX_FDS) { | |
500 | error_setg_errno(errp, -EINVAL, | |
501 | "Only %d FDs can be sent, got %zu", | |
502 | SOCKET_MAX_FDS, nfds); | |
503 | return -1; | |
504 | } | |
505 | ||
506 | msg.msg_control = control; | |
507 | msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds); | |
508 | ||
509 | cmsg = CMSG_FIRSTHDR(&msg); | |
510 | cmsg->cmsg_len = CMSG_LEN(fdsize); | |
511 | cmsg->cmsg_level = SOL_SOCKET; | |
512 | cmsg->cmsg_type = SCM_RIGHTS; | |
513 | memcpy(CMSG_DATA(cmsg), fds, fdsize); | |
514 | } | |
515 | ||
516 | retry: | |
517 | ret = sendmsg(sioc->fd, &msg, 0); | |
518 | if (ret <= 0) { | |
519 | if (socket_error() == EAGAIN || | |
520 | socket_error() == EWOULDBLOCK) { | |
521 | return QIO_CHANNEL_ERR_BLOCK; | |
522 | } | |
523 | if (socket_error() == EINTR) { | |
524 | goto retry; | |
525 | } | |
526 | error_setg_errno(errp, socket_error(), | |
527 | "Unable to write to socket"); | |
528 | return -1; | |
529 | } | |
530 | return ret; | |
531 | } | |
532 | #else /* WIN32 */ | |
533 | static ssize_t qio_channel_socket_readv(QIOChannel *ioc, | |
534 | const struct iovec *iov, | |
535 | size_t niov, | |
536 | int **fds, | |
537 | size_t *nfds, | |
538 | Error **errp) | |
539 | { | |
540 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
541 | ssize_t done = 0; | |
542 | ssize_t i; | |
543 | ||
544 | for (i = 0; i < niov; i++) { | |
545 | ssize_t ret; | |
546 | retry: | |
547 | ret = recv(sioc->fd, | |
548 | iov[i].iov_base, | |
549 | iov[i].iov_len, | |
550 | 0); | |
551 | if (ret < 0) { | |
552 | if (socket_error() == EAGAIN) { | |
553 | if (done) { | |
554 | return done; | |
555 | } else { | |
556 | return QIO_CHANNEL_ERR_BLOCK; | |
557 | } | |
558 | } else if (socket_error() == EINTR) { | |
559 | goto retry; | |
560 | } else { | |
561 | error_setg_errno(errp, socket_error(), | |
562 | "Unable to write to socket"); | |
563 | return -1; | |
564 | } | |
565 | } | |
566 | done += ret; | |
567 | if (ret < iov[i].iov_len) { | |
568 | return done; | |
569 | } | |
570 | } | |
571 | ||
572 | return done; | |
573 | } | |
574 | ||
575 | static ssize_t qio_channel_socket_writev(QIOChannel *ioc, | |
576 | const struct iovec *iov, | |
577 | size_t niov, | |
578 | int *fds, | |
579 | size_t nfds, | |
580 | Error **errp) | |
581 | { | |
582 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
583 | ssize_t done = 0; | |
584 | ssize_t i; | |
585 | ||
586 | for (i = 0; i < niov; i++) { | |
587 | ssize_t ret; | |
588 | retry: | |
589 | ret = send(sioc->fd, | |
590 | iov[i].iov_base, | |
591 | iov[i].iov_len, | |
592 | 0); | |
593 | if (ret < 0) { | |
594 | if (socket_error() == EAGAIN) { | |
595 | if (done) { | |
596 | return done; | |
597 | } else { | |
598 | return QIO_CHANNEL_ERR_BLOCK; | |
599 | } | |
600 | } else if (socket_error() == EINTR) { | |
601 | goto retry; | |
602 | } else { | |
603 | error_setg_errno(errp, socket_error(), | |
604 | "Unable to write to socket"); | |
605 | return -1; | |
606 | } | |
607 | } | |
608 | done += ret; | |
609 | if (ret < iov[i].iov_len) { | |
610 | return done; | |
611 | } | |
612 | } | |
613 | ||
614 | return done; | |
615 | } | |
616 | #endif /* WIN32 */ | |
617 | ||
618 | static int | |
619 | qio_channel_socket_set_blocking(QIOChannel *ioc, | |
620 | bool enabled, | |
621 | Error **errp) | |
622 | { | |
623 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
624 | ||
625 | if (enabled) { | |
626 | qemu_set_block(sioc->fd); | |
627 | } else { | |
628 | qemu_set_nonblock(sioc->fd); | |
629 | } | |
630 | return 0; | |
631 | } | |
632 | ||
633 | ||
634 | static void | |
635 | qio_channel_socket_set_delay(QIOChannel *ioc, | |
636 | bool enabled) | |
637 | { | |
638 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
639 | int v = enabled ? 0 : 1; | |
640 | ||
641 | qemu_setsockopt(sioc->fd, | |
642 | IPPROTO_TCP, TCP_NODELAY, | |
643 | &v, sizeof(v)); | |
644 | } | |
645 | ||
646 | ||
647 | static void | |
648 | qio_channel_socket_set_cork(QIOChannel *ioc, | |
649 | bool enabled) | |
650 | { | |
651 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
652 | int v = enabled ? 1 : 0; | |
653 | ||
654 | socket_set_cork(sioc->fd, v); | |
655 | } | |
656 | ||
657 | ||
658 | static int | |
659 | qio_channel_socket_close(QIOChannel *ioc, | |
660 | Error **errp) | |
661 | { | |
662 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
663 | ||
664 | if (closesocket(sioc->fd) < 0) { | |
665 | sioc->fd = -1; | |
666 | error_setg_errno(errp, socket_error(), | |
667 | "Unable to close socket"); | |
668 | return -1; | |
669 | } | |
670 | sioc->fd = -1; | |
671 | return 0; | |
672 | } | |
673 | ||
674 | static int | |
675 | qio_channel_socket_shutdown(QIOChannel *ioc, | |
676 | QIOChannelShutdown how, | |
677 | Error **errp) | |
678 | { | |
679 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
680 | int sockhow; | |
681 | ||
682 | switch (how) { | |
683 | case QIO_CHANNEL_SHUTDOWN_READ: | |
684 | sockhow = SHUT_RD; | |
685 | break; | |
686 | case QIO_CHANNEL_SHUTDOWN_WRITE: | |
687 | sockhow = SHUT_WR; | |
688 | break; | |
689 | case QIO_CHANNEL_SHUTDOWN_BOTH: | |
690 | default: | |
691 | sockhow = SHUT_RDWR; | |
692 | break; | |
693 | } | |
694 | ||
695 | if (shutdown(sioc->fd, sockhow) < 0) { | |
696 | error_setg_errno(errp, socket_error(), | |
697 | "Unable to shutdown socket"); | |
698 | return -1; | |
699 | } | |
700 | return 0; | |
701 | } | |
702 | ||
703 | static GSource *qio_channel_socket_create_watch(QIOChannel *ioc, | |
704 | GIOCondition condition) | |
705 | { | |
706 | QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc); | |
707 | return qio_channel_create_fd_watch(ioc, | |
708 | sioc->fd, | |
709 | condition); | |
710 | } | |
711 | ||
712 | static void qio_channel_socket_class_init(ObjectClass *klass, | |
713 | void *class_data G_GNUC_UNUSED) | |
714 | { | |
715 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); | |
716 | ||
717 | ioc_klass->io_writev = qio_channel_socket_writev; | |
718 | ioc_klass->io_readv = qio_channel_socket_readv; | |
719 | ioc_klass->io_set_blocking = qio_channel_socket_set_blocking; | |
720 | ioc_klass->io_close = qio_channel_socket_close; | |
721 | ioc_klass->io_shutdown = qio_channel_socket_shutdown; | |
722 | ioc_klass->io_set_cork = qio_channel_socket_set_cork; | |
723 | ioc_klass->io_set_delay = qio_channel_socket_set_delay; | |
724 | ioc_klass->io_create_watch = qio_channel_socket_create_watch; | |
725 | } | |
726 | ||
727 | static const TypeInfo qio_channel_socket_info = { | |
728 | .parent = TYPE_QIO_CHANNEL, | |
729 | .name = TYPE_QIO_CHANNEL_SOCKET, | |
730 | .instance_size = sizeof(QIOChannelSocket), | |
731 | .instance_init = qio_channel_socket_init, | |
732 | .instance_finalize = qio_channel_socket_finalize, | |
733 | .class_init = qio_channel_socket_class_init, | |
734 | }; | |
735 | ||
736 | static void qio_channel_socket_register_types(void) | |
737 | { | |
738 | type_register_static(&qio_channel_socket_info); | |
739 | } | |
740 | ||
741 | type_init(qio_channel_socket_register_types); |