From patchwork Wed Oct 3 14:36:51 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 188806 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 89C812C0098 for ; Thu, 4 Oct 2012 01:26:58 +1000 (EST) Received: from localhost ([::1]:58314 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJQ5t-0006Rz-Fy for incoming@patchwork.ozlabs.org; Wed, 03 Oct 2012 10:38:37 -0400 Received: from eggs.gnu.org ([208.118.235.92]:37872) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJQ4u-0004Ii-PK for qemu-devel@nongnu.org; Wed, 03 Oct 2012 10:37:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TJQ4o-0000FB-Nw for qemu-devel@nongnu.org; Wed, 03 Oct 2012 10:37:36 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:39719) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJQ4o-0000Cn-Hx for qemu-devel@nongnu.org; Wed, 03 Oct 2012 10:37:30 -0400 Received: by mail-pb0-f45.google.com with SMTP id rp2so10061642pbb.4 for ; Wed, 03 Oct 2012 07:37:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=e4/CWeIMjQdkBy3Oys484S5bbY1F/ZZQXHzOI2e/pW0=; b=zwsSg87JpoG2UnfmW0OTHEELt2pw24sibfyMf0PhEfpJgYFM+TEpwhKxHjJVNVVLjQ myFMd8SuEW1BmPpaGH2mQTRgNXHdCpJ3f8Y+RukXI+VYLiLHUg86SavWhBLwPA/fo5F/ xjzWFww4PbMGfrHmtZbi9U1mx8bjNmoGYY6iwC/rYTfK/57S1GpsLYnodejuz2SHGm3e bNlIMq43iea1GpXBkG8LlO1J8WfsSMtl3466zlkSo31jRB5SIy3p2PBFXadxHDTP3Zfw x6JqZiy8dcLHqJHtgac2BRMh8JiTX8+w4JkkJLMj+woi9DKvdfgKAZEPNBf2zkJNWIT9 IWmg== Received: by 10.66.81.202 with SMTP id c10mr5594801pay.31.1349275050116; Wed, 03 Oct 2012 07:37:30 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-169-1.ip50.fastwebnet.it. [93.34.169.1]) by mx.google.com with ESMTPS id i1sm2566228pay.26.2012.10.03.07.37.27 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 03 Oct 2012 07:37:29 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 3 Oct 2012 16:36:51 +0200 Message-Id: <1349275025-5093-5-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1349275025-5093-1-git-send-email-pbonzini@redhat.com> References: <1349275025-5093-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.45 Cc: lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH 04/18] qemu-sockets: add nonblocking connect for Unix sockets X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Paolo Bonzini --- qemu-char.c | 2 +- qemu-sockets.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++--------- qemu_socket.h | 6 ++++- 3 file modificati, 72 inserzioni(+), 14 rimozioni(-) diff --git a/qemu-char.c b/qemu-char.c index 3cc6cb5..8ebd582 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2450,7 +2450,7 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) if (is_listen) { fd = unix_listen_opts(opts, NULL); } else { - fd = unix_connect_opts(opts, NULL); + fd = unix_connect_opts(opts, NULL, NULL, NULL); } } else { if (is_listen) { diff --git a/qemu-sockets.c b/qemu-sockets.c index 79c7b66..341ae21 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -252,16 +252,19 @@ static void wait_for_connect(void *opaque) } /* try to connect to the next address on the list */ - while (s->current_addr->ai_next != NULL && s->fd < 0) { - s->current_addr = s->current_addr->ai_next; - s->fd = inet_connect_addr(s->current_addr, &in_progress, s); - /* connect in progress */ - if (in_progress) { - return; + if (s->current_addr) { + while (s->current_addr->ai_next != NULL && s->fd < 0) { + s->current_addr = s->current_addr->ai_next; + s->fd = inet_connect_addr(s->current_addr, &in_progress, s); + /* connect in progress */ + if (in_progress) { + return; + } } + + freeaddrinfo(s->addr_list); } - freeaddrinfo(s->addr_list); if (s->callback) { s->callback(s->fd, s->opaque); } @@ -702,32 +705,65 @@ err: return -1; } -int unix_connect_opts(QemuOpts *opts, Error **errp) +int unix_connect_opts(QemuOpts *opts, Error **errp, + NonBlockingConnectHandler *callback, void *opaque) { struct sockaddr_un un; const char *path = qemu_opt_get(opts, "path"); - int sock; + ConnectState *connect_state = NULL; + int sock, rc; if (NULL == path) { fprintf(stderr, "unix connect: no path specified\n"); return -1; } + if (callback != NULL) { + connect_state = g_malloc0(sizeof(*connect_state)); + connect_state->callback = callback; + connect_state->opaque = opaque; + } + sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); if (sock < 0) { perror("socket(unix)"); return -1; } + if (connect_state != NULL) { + socket_set_nonblock(sock); + } memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); - if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { + + /* connect to peer */ + do { + rc = 0; + if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) { + rc = -socket_error(); + } + } while (rc == -EINTR); + + if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) { + connect_state->fd = sock; + qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect, + connect_state); + return sock; + } else { + /* non blocking socket immediate success, call callback */ + if (callback != NULL) { + callback(sock, opaque); + } + } + + if (rc < 0) { fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno)); close(sock); return -1; } + g_free(connect_state); return sock; } @@ -740,7 +776,8 @@ int unix_listen_opts(QemuOpts *opts, Error **errp) return -1; } -int unix_connect_opts(QemuOpts *opts, Error **errp) +int unix_connect_opts(QemuOpts *opts, Error **errp, + NonBlockingConnectHandler *callback, void *opaque) { error_setg(errp, "unix sockets are not available on windows"); errno = ENOTSUP; @@ -785,7 +822,24 @@ int unix_connect(const char *path, Error **errp) opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); qemu_opt_set(opts, "path", path); - sock = unix_connect_opts(opts, errp); + sock = unix_connect_opts(opts, errp, NULL, NULL); + qemu_opts_del(opts); + return sock; +} + + +int unix_nonblocking_connect(const char *path, + NonBlockingConnectHandler *callback, + void *opaque, Error **errp) +{ + QemuOpts *opts; + int sock = -1; + + g_assert(callback != NULL); + + opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); + qemu_opt_set(opts, "path", path); + sock = unix_connect_opts(opts, errp, callback, opaque); qemu_opts_del(opts); return sock; } diff --git a/qemu_socket.h b/qemu_socket.h index ff979b5..89a5feb 100644 --- a/qemu_socket.h +++ b/qemu_socket.h @@ -58,8 +58,12 @@ const char *inet_strfamily(int family); int unix_listen_opts(QemuOpts *opts, Error **errp); int unix_listen(const char *path, char *ostr, int olen, Error **errp); -int unix_connect_opts(QemuOpts *opts, Error **errp); +int unix_connect_opts(QemuOpts *opts, Error **errp, + NonBlockingConnectHandler *callback, void *opaque); int unix_connect(const char *path, Error **errp); +int unix_nonblocking_connect(const char *str, + NonBlockingConnectHandler *callback, + void *opaque, Error **errp); /* Old, ipv4 only bits. Don't use for new code. */ int parse_host_port(struct sockaddr_in *saddr, const char *str);