monitor: Split monitor_init in HMP and QMP function
[qemu/ar7.git] / io / channel-socket.c
blobbec3d931d17b0537b0348c18b1368e3ae21ec18b
1 /*
2 * QEMU I/O channels sockets driver
4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qemu/module.h"
25 #include "io/channel-socket.h"
26 #include "io/channel-watch.h"
27 #include "trace.h"
28 #include "qapi/clone-visitor.h"
30 #define SOCKET_MAX_FDS 16
32 SocketAddress *
33 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
34 Error **errp)
36 return socket_sockaddr_to_address(&ioc->localAddr,
37 ioc->localAddrLen,
38 errp);
41 SocketAddress *
42 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
43 Error **errp)
45 return socket_sockaddr_to_address(&ioc->remoteAddr,
46 ioc->remoteAddrLen,
47 errp);
50 QIOChannelSocket *
51 qio_channel_socket_new(void)
53 QIOChannelSocket *sioc;
54 QIOChannel *ioc;
56 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
57 sioc->fd = -1;
59 ioc = QIO_CHANNEL(sioc);
60 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
62 #ifdef WIN32
63 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
64 #endif
66 trace_qio_channel_socket_new(sioc);
68 return sioc;
72 static int
73 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
74 int fd,
75 Error **errp)
77 if (sioc->fd != -1) {
78 error_setg(errp, "Socket is already open");
79 return -1;
82 sioc->fd = fd;
83 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
84 sioc->localAddrLen = sizeof(sioc->localAddr);
87 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
88 &sioc->remoteAddrLen) < 0) {
89 if (errno == ENOTCONN) {
90 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
91 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
92 } else {
93 error_setg_errno(errp, errno,
94 "Unable to query remote socket address");
95 goto error;
99 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
100 &sioc->localAddrLen) < 0) {
101 error_setg_errno(errp, errno,
102 "Unable to query local socket address");
103 goto error;
106 #ifndef WIN32
107 if (sioc->localAddr.ss_family == AF_UNIX) {
108 QIOChannel *ioc = QIO_CHANNEL(sioc);
109 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS);
111 #endif /* WIN32 */
113 return 0;
115 error:
116 sioc->fd = -1; /* Let the caller close FD on failure */
117 return -1;
120 QIOChannelSocket *
121 qio_channel_socket_new_fd(int fd,
122 Error **errp)
124 QIOChannelSocket *ioc;
126 ioc = qio_channel_socket_new();
127 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
128 object_unref(OBJECT(ioc));
129 return NULL;
132 trace_qio_channel_socket_new_fd(ioc, fd);
134 return ioc;
138 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
139 SocketAddress *addr,
140 Error **errp)
142 int fd;
144 trace_qio_channel_socket_connect_sync(ioc, addr);
145 fd = socket_connect(addr, errp);
146 if (fd < 0) {
147 trace_qio_channel_socket_connect_fail(ioc);
148 return -1;
151 trace_qio_channel_socket_connect_complete(ioc, fd);
152 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
153 close(fd);
154 return -1;
157 return 0;
161 static void qio_channel_socket_connect_worker(QIOTask *task,
162 gpointer opaque)
164 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
165 SocketAddress *addr = opaque;
166 Error *err = NULL;
168 qio_channel_socket_connect_sync(ioc, addr, &err);
170 qio_task_set_error(task, err);
174 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
175 SocketAddress *addr,
176 QIOTaskFunc callback,
177 gpointer opaque,
178 GDestroyNotify destroy,
179 GMainContext *context)
181 QIOTask *task = qio_task_new(
182 OBJECT(ioc), callback, opaque, destroy);
183 SocketAddress *addrCopy;
185 addrCopy = QAPI_CLONE(SocketAddress, addr);
187 /* socket_connect() does a non-blocking connect(), but it
188 * still blocks in DNS lookups, so we must use a thread */
189 trace_qio_channel_socket_connect_async(ioc, addr);
190 qio_task_run_in_thread(task,
191 qio_channel_socket_connect_worker,
192 addrCopy,
193 (GDestroyNotify)qapi_free_SocketAddress,
194 context);
198 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
199 SocketAddress *addr,
200 Error **errp)
202 int fd;
204 trace_qio_channel_socket_listen_sync(ioc, addr);
205 fd = socket_listen(addr, errp);
206 if (fd < 0) {
207 trace_qio_channel_socket_listen_fail(ioc);
208 return -1;
211 trace_qio_channel_socket_listen_complete(ioc, fd);
212 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
213 close(fd);
214 return -1;
216 qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_LISTEN);
218 return 0;
222 static void qio_channel_socket_listen_worker(QIOTask *task,
223 gpointer opaque)
225 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
226 SocketAddress *addr = opaque;
227 Error *err = NULL;
229 qio_channel_socket_listen_sync(ioc, addr, &err);
231 qio_task_set_error(task, err);
235 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
236 SocketAddress *addr,
237 QIOTaskFunc callback,
238 gpointer opaque,
239 GDestroyNotify destroy,
240 GMainContext *context)
242 QIOTask *task = qio_task_new(
243 OBJECT(ioc), callback, opaque, destroy);
244 SocketAddress *addrCopy;
246 addrCopy = QAPI_CLONE(SocketAddress, addr);
248 /* socket_listen() blocks in DNS lookups, so we must use a thread */
249 trace_qio_channel_socket_listen_async(ioc, addr);
250 qio_task_run_in_thread(task,
251 qio_channel_socket_listen_worker,
252 addrCopy,
253 (GDestroyNotify)qapi_free_SocketAddress,
254 context);
258 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
259 SocketAddress *localAddr,
260 SocketAddress *remoteAddr,
261 Error **errp)
263 int fd;
265 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
266 fd = socket_dgram(remoteAddr, localAddr, errp);
267 if (fd < 0) {
268 trace_qio_channel_socket_dgram_fail(ioc);
269 return -1;
272 trace_qio_channel_socket_dgram_complete(ioc, fd);
273 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
274 close(fd);
275 return -1;
278 return 0;
282 struct QIOChannelSocketDGramWorkerData {
283 SocketAddress *localAddr;
284 SocketAddress *remoteAddr;
288 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
290 struct QIOChannelSocketDGramWorkerData *data = opaque;
291 qapi_free_SocketAddress(data->localAddr);
292 qapi_free_SocketAddress(data->remoteAddr);
293 g_free(data);
296 static void qio_channel_socket_dgram_worker(QIOTask *task,
297 gpointer opaque)
299 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
300 struct QIOChannelSocketDGramWorkerData *data = opaque;
301 Error *err = NULL;
303 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
304 qio_channel_socket_dgram_sync(ioc, data->localAddr,
305 data->remoteAddr, &err);
307 qio_task_set_error(task, err);
311 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
312 SocketAddress *localAddr,
313 SocketAddress *remoteAddr,
314 QIOTaskFunc callback,
315 gpointer opaque,
316 GDestroyNotify destroy,
317 GMainContext *context)
319 QIOTask *task = qio_task_new(
320 OBJECT(ioc), callback, opaque, destroy);
321 struct QIOChannelSocketDGramWorkerData *data = g_new0(
322 struct QIOChannelSocketDGramWorkerData, 1);
324 data->localAddr = QAPI_CLONE(SocketAddress, localAddr);
325 data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr);
327 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
328 qio_task_run_in_thread(task,
329 qio_channel_socket_dgram_worker,
330 data,
331 qio_channel_socket_dgram_worker_free,
332 context);
336 QIOChannelSocket *
337 qio_channel_socket_accept(QIOChannelSocket *ioc,
338 Error **errp)
340 QIOChannelSocket *cioc;
342 cioc = qio_channel_socket_new();
343 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
344 cioc->localAddrLen = sizeof(ioc->localAddr);
346 retry:
347 trace_qio_channel_socket_accept(ioc);
348 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
349 &cioc->remoteAddrLen);
350 if (cioc->fd < 0) {
351 if (errno == EINTR) {
352 goto retry;
354 error_setg_errno(errp, errno, "Unable to accept connection");
355 trace_qio_channel_socket_accept_fail(ioc);
356 goto error;
359 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
360 &cioc->localAddrLen) < 0) {
361 error_setg_errno(errp, errno,
362 "Unable to query local socket address");
363 goto error;
366 #ifndef WIN32
367 if (cioc->localAddr.ss_family == AF_UNIX) {
368 QIOChannel *ioc_local = QIO_CHANNEL(cioc);
369 qio_channel_set_feature(ioc_local, QIO_CHANNEL_FEATURE_FD_PASS);
371 #endif /* WIN32 */
373 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
374 return cioc;
376 error:
377 object_unref(OBJECT(cioc));
378 return NULL;
381 static void qio_channel_socket_init(Object *obj)
383 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
384 ioc->fd = -1;
387 static void qio_channel_socket_finalize(Object *obj)
389 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
391 if (ioc->fd != -1) {
392 QIOChannel *ioc_local = QIO_CHANNEL(ioc);
393 if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) {
394 Error *err = NULL;
396 socket_listen_cleanup(ioc->fd, &err);
397 if (err) {
398 error_report_err(err);
399 err = NULL;
402 #ifdef WIN32
403 WSAEventSelect(ioc->fd, NULL, 0);
404 #endif
405 closesocket(ioc->fd);
406 ioc->fd = -1;
411 #ifndef WIN32
412 static void qio_channel_socket_copy_fds(struct msghdr *msg,
413 int **fds, size_t *nfds)
415 struct cmsghdr *cmsg;
417 *nfds = 0;
418 *fds = NULL;
420 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
421 int fd_size, i;
422 int gotfds;
424 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
425 cmsg->cmsg_level != SOL_SOCKET ||
426 cmsg->cmsg_type != SCM_RIGHTS) {
427 continue;
430 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
432 if (!fd_size) {
433 continue;
436 gotfds = fd_size / sizeof(int);
437 *fds = g_renew(int, *fds, *nfds + gotfds);
438 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
440 for (i = 0; i < gotfds; i++) {
441 int fd = (*fds)[*nfds + i];
442 if (fd < 0) {
443 continue;
446 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
447 qemu_set_block(fd);
449 #ifndef MSG_CMSG_CLOEXEC
450 qemu_set_cloexec(fd);
451 #endif
453 *nfds += gotfds;
458 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
459 const struct iovec *iov,
460 size_t niov,
461 int **fds,
462 size_t *nfds,
463 Error **errp)
465 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
466 ssize_t ret;
467 struct msghdr msg = { NULL, };
468 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
469 int sflags = 0;
471 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
473 #ifdef MSG_CMSG_CLOEXEC
474 sflags |= MSG_CMSG_CLOEXEC;
475 #endif
477 msg.msg_iov = (struct iovec *)iov;
478 msg.msg_iovlen = niov;
479 if (fds && nfds) {
480 msg.msg_control = control;
481 msg.msg_controllen = sizeof(control);
484 retry:
485 ret = recvmsg(sioc->fd, &msg, sflags);
486 if (ret < 0) {
487 if (errno == EAGAIN) {
488 return QIO_CHANNEL_ERR_BLOCK;
490 if (errno == EINTR) {
491 goto retry;
494 error_setg_errno(errp, errno,
495 "Unable to read from socket");
496 return -1;
499 if (fds && nfds) {
500 qio_channel_socket_copy_fds(&msg, fds, nfds);
503 return ret;
506 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
507 const struct iovec *iov,
508 size_t niov,
509 int *fds,
510 size_t nfds,
511 Error **errp)
513 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
514 ssize_t ret;
515 struct msghdr msg = { NULL, };
516 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
517 size_t fdsize = sizeof(int) * nfds;
518 struct cmsghdr *cmsg;
520 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
522 msg.msg_iov = (struct iovec *)iov;
523 msg.msg_iovlen = niov;
525 if (nfds) {
526 if (nfds > SOCKET_MAX_FDS) {
527 error_setg_errno(errp, EINVAL,
528 "Only %d FDs can be sent, got %zu",
529 SOCKET_MAX_FDS, nfds);
530 return -1;
533 msg.msg_control = control;
534 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
536 cmsg = CMSG_FIRSTHDR(&msg);
537 cmsg->cmsg_len = CMSG_LEN(fdsize);
538 cmsg->cmsg_level = SOL_SOCKET;
539 cmsg->cmsg_type = SCM_RIGHTS;
540 memcpy(CMSG_DATA(cmsg), fds, fdsize);
543 retry:
544 ret = sendmsg(sioc->fd, &msg, 0);
545 if (ret <= 0) {
546 if (errno == EAGAIN) {
547 return QIO_CHANNEL_ERR_BLOCK;
549 if (errno == EINTR) {
550 goto retry;
552 error_setg_errno(errp, errno,
553 "Unable to write to socket");
554 return -1;
556 return ret;
558 #else /* WIN32 */
559 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
560 const struct iovec *iov,
561 size_t niov,
562 int **fds,
563 size_t *nfds,
564 Error **errp)
566 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
567 ssize_t done = 0;
568 ssize_t i;
570 for (i = 0; i < niov; i++) {
571 ssize_t ret;
572 retry:
573 ret = recv(sioc->fd,
574 iov[i].iov_base,
575 iov[i].iov_len,
577 if (ret < 0) {
578 if (errno == EAGAIN) {
579 if (done) {
580 return done;
581 } else {
582 return QIO_CHANNEL_ERR_BLOCK;
584 } else if (errno == EINTR) {
585 goto retry;
586 } else {
587 error_setg_errno(errp, errno,
588 "Unable to read from socket");
589 return -1;
592 done += ret;
593 if (ret < iov[i].iov_len) {
594 return done;
598 return done;
601 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
602 const struct iovec *iov,
603 size_t niov,
604 int *fds,
605 size_t nfds,
606 Error **errp)
608 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
609 ssize_t done = 0;
610 ssize_t i;
612 for (i = 0; i < niov; i++) {
613 ssize_t ret;
614 retry:
615 ret = send(sioc->fd,
616 iov[i].iov_base,
617 iov[i].iov_len,
619 if (ret < 0) {
620 if (errno == EAGAIN) {
621 if (done) {
622 return done;
623 } else {
624 return QIO_CHANNEL_ERR_BLOCK;
626 } else if (errno == EINTR) {
627 goto retry;
628 } else {
629 error_setg_errno(errp, errno,
630 "Unable to write to socket");
631 return -1;
634 done += ret;
635 if (ret < iov[i].iov_len) {
636 return done;
640 return done;
642 #endif /* WIN32 */
644 static int
645 qio_channel_socket_set_blocking(QIOChannel *ioc,
646 bool enabled,
647 Error **errp)
649 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
651 if (enabled) {
652 qemu_set_block(sioc->fd);
653 } else {
654 qemu_set_nonblock(sioc->fd);
656 return 0;
660 static void
661 qio_channel_socket_set_delay(QIOChannel *ioc,
662 bool enabled)
664 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
665 int v = enabled ? 0 : 1;
667 qemu_setsockopt(sioc->fd,
668 IPPROTO_TCP, TCP_NODELAY,
669 &v, sizeof(v));
673 static void
674 qio_channel_socket_set_cork(QIOChannel *ioc,
675 bool enabled)
677 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
678 int v = enabled ? 1 : 0;
680 socket_set_cork(sioc->fd, v);
684 static int
685 qio_channel_socket_close(QIOChannel *ioc,
686 Error **errp)
688 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
689 int rc = 0;
691 if (sioc->fd != -1) {
692 #ifdef WIN32
693 WSAEventSelect(sioc->fd, NULL, 0);
694 #endif
695 if (qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_LISTEN)) {
696 socket_listen_cleanup(sioc->fd, errp);
699 if (closesocket(sioc->fd) < 0) {
700 sioc->fd = -1;
701 error_setg_errno(errp, errno,
702 "Unable to close socket");
703 return -1;
705 sioc->fd = -1;
707 return rc;
710 static int
711 qio_channel_socket_shutdown(QIOChannel *ioc,
712 QIOChannelShutdown how,
713 Error **errp)
715 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
716 int sockhow;
718 switch (how) {
719 case QIO_CHANNEL_SHUTDOWN_READ:
720 sockhow = SHUT_RD;
721 break;
722 case QIO_CHANNEL_SHUTDOWN_WRITE:
723 sockhow = SHUT_WR;
724 break;
725 case QIO_CHANNEL_SHUTDOWN_BOTH:
726 default:
727 sockhow = SHUT_RDWR;
728 break;
731 if (shutdown(sioc->fd, sockhow) < 0) {
732 error_setg_errno(errp, errno,
733 "Unable to shutdown socket");
734 return -1;
736 return 0;
739 static void qio_channel_socket_set_aio_fd_handler(QIOChannel *ioc,
740 AioContext *ctx,
741 IOHandler *io_read,
742 IOHandler *io_write,
743 void *opaque)
745 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
746 aio_set_fd_handler(ctx, sioc->fd, false, io_read, io_write, NULL, opaque);
749 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
750 GIOCondition condition)
752 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
753 return qio_channel_create_socket_watch(ioc,
754 sioc->fd,
755 condition);
758 static void qio_channel_socket_class_init(ObjectClass *klass,
759 void *class_data G_GNUC_UNUSED)
761 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
763 ioc_klass->io_writev = qio_channel_socket_writev;
764 ioc_klass->io_readv = qio_channel_socket_readv;
765 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
766 ioc_klass->io_close = qio_channel_socket_close;
767 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
768 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
769 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
770 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
771 ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
774 static const TypeInfo qio_channel_socket_info = {
775 .parent = TYPE_QIO_CHANNEL,
776 .name = TYPE_QIO_CHANNEL_SOCKET,
777 .instance_size = sizeof(QIOChannelSocket),
778 .instance_init = qio_channel_socket_init,
779 .instance_finalize = qio_channel_socket_finalize,
780 .class_init = qio_channel_socket_class_init,
783 static void qio_channel_socket_register_types(void)
785 type_register_static(&qio_channel_socket_info);
788 type_init(qio_channel_socket_register_types);