From patchwork Sat Jun 4 21:33:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 9154979 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3D52E60574 for ; Sat, 4 Jun 2016 21:42:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D99D27F2B for ; Sat, 4 Jun 2016 21:42:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 214F828327; Sat, 4 Jun 2016 21:42:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B5BA427F2B for ; Sat, 4 Jun 2016 21:42:49 +0000 (UTC) Received: from localhost ([::1]:34239 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JLE-0005jV-P8 for patchwork-qemu-devel@patchwork.kernel.org; Sat, 04 Jun 2016 17:42:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44719) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JCM-0005pZ-VX for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:33:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9JCC-0005g5-Tc for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:33:37 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:35253) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JCC-0005g0-MX for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:33:28 -0400 Received: by mail-wm0-x243.google.com with SMTP id k184so894128wme.2 for ; Sat, 04 Jun 2016 14:33:28 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=cC12WaS8Q0uRg6KbEnZKYgXu9TcmbXZqmKZ6+O4mwr4=; b=KlV2EVliAmMrwxKpfG1q8kI24FgLtPWOXotkQ5IviXWbmFFR/NvNNwu88Rlx6CHy7k z9aHUBycyOkO1ABnCQuDfqpMoJZyxso4Tw6ZNS82IUzaiRYUJxngcnhO1thrnpWJHx3d umSTsiOgzemnV0htTQUk2QkCnX8j3+zEESKh3aqaVgKoaMxdQRvtlHEazugow51mzu9c MKCl5AV4VtnYyqsJv2d7YMRyc5O53ywZDZowiOgp3UCXaapEBQ/oszJ+nLjd9sSmrRa1 Rl8yf+FauydzOGdsPbb8k8WrxJEanAqM2vS/ijng1tEu+fpCxX7JlwE578mLGxjkNAOF gBew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=cC12WaS8Q0uRg6KbEnZKYgXu9TcmbXZqmKZ6+O4mwr4=; b=BBFVI9ga2nCzdg6bqrb964kyCr1TkV5Sv1ZeFrfVCRvRRAga54Ka8LVTylXJQfhoHT AsaNrZQvcCP4OlTACpzCCE3LMlOBua3AFPBrUQpQhOESQEqJWOUnB+nkUo7oNHMT6G4F iYqe4SaKBYpgyR12znVHmDwlwqwm5onj7OGfhCWKwUgGkxe0E/0jGXeptxXxO6lmig+T R7EdeqxGqeUNBXCduo6EW0+Cw1nVbPE7BcwNStAHND/1zRLvrih+GPjmJLemrKL1QGtd 15Rl+myGbSSjVsYKGUweiZ8hOKm+gI+QBNkyBaWURsJ+drKm9fxF85uod4Bsm+sfzJs6 2nvg== X-Gm-Message-State: ALyK8tLQg10VUnNc7neY/OxhULJURFq2ysIg1C4bV6ALtRTArA7XQspbzqVO11W8UeWSlw== X-Received: by 10.194.82.194 with SMTP id k2mr7967213wjy.73.1465076008004; Sat, 04 Jun 2016 14:33:28 -0700 (PDT) Received: from localhost (APoitiers-110-1-1-235.w193-248.abo.wanadoo.fr. [193.248.103.235]) by smtp.gmail.com with ESMTPSA id q189sm6240699wmd.19.2016.06.04.14.33.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Jun 2016 14:33:27 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Sat, 4 Jun 2016 23:33:10 +0200 Message-Id: <1465076003-26291-2-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1465076003-26291-1-git-send-email-marcandre.lureau@redhat.com> References: <1465076003-26291-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [RFC 01/14] Add qemu_chr_open_socket() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau Create a CharDriver from an existing socket fd. Is there a better way to do that? Signed-off-by: Marc-André Lureau --- include/sysemu/char.h | 2 ++ qemu-char.c | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/sysemu/char.h b/include/sysemu/char.h index 307fd8f..bd97031 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -106,6 +106,8 @@ struct CharDriverState { */ CharDriverState *qemu_chr_alloc(ChardevCommon *backend, Error **errp); +CharDriverState *qemu_chr_open_socket(int fd, Error **errp); + /** * @qemu_chr_new_from_opts: * diff --git a/qemu-char.c b/qemu-char.c index b597ee1..caa737d 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -96,6 +96,10 @@ static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr, bool is_listen, bool is_telnet) { + if (!addr) { + return g_strdup(prefix); + } + switch (addr->type) { case SOCKET_ADDRESS_KIND_INET: return g_strdup_printf("%s%s:%s:%s%s", prefix, @@ -166,7 +170,7 @@ CharDriverState *qemu_chr_alloc(ChardevCommon *backend, Error **errp) CharDriverState *chr = g_malloc0(sizeof(CharDriverState)); qemu_mutex_init(&chr->chr_write_lock); - if (backend->has_logfile) { + if (backend && backend->has_logfile) { int flags = O_WRONLY | O_CREAT; if (backend->has_logappend && backend->logappend) { @@ -4466,6 +4470,37 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, return NULL; } +CharDriverState *qemu_chr_open_socket(int fd, Error **errp) +{ + CharDriverState *chr; + TCPCharDriver *s; + + chr = qemu_chr_alloc(NULL, errp); + if (!chr) { + return NULL; + } + + qemu_set_nonblock(fd); + + s = g_new0(TCPCharDriver, 1); + chr->opaque = s; + chr->chr_write = tcp_chr_write; + chr->chr_sync_read = tcp_chr_sync_read; + chr->chr_close = tcp_chr_close; + chr->get_msgfds = tcp_get_msgfds; + chr->set_msgfds = tcp_set_msgfds; + chr->chr_add_watch = tcp_chr_add_watch; + chr->chr_update_read_handler = tcp_chr_update_read_handler; + + if (tcp_chr_add_client(chr, fd) < -1) { + free(s); + qemu_chr_free_common(chr); + return NULL; + } + + return chr; +} + static CharDriverState *qmp_chardev_open_udp(const char *id, ChardevBackend *backend, ChardevReturn *ret,