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