From nobody Fri Apr 19 14:26:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1641914432; cv=none; d=zohomail.com; s=zohoarc; b=D4iqT6N01L6VtclngNmm9JDmu+ViB8zX+6GJlB/vVPOd2lea1XORuRHgIeDFAa/fUxNaXOWTd1FLg33+6EAAqQAvsDsPN+IeuRX3/mcSGYj+F4zXlmqHPGN3omP+WgOrWHMrIIhQnMPz5o2zfs5ibMQvF92TtBnA+2tIabUhe/o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1641914432; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/J7Pbzy4bEMLs1Zfn02zqrQKoe12Kbt0uIHiCf5rrSk=; b=QejbL1jXWYC45ON3W0EWrsI7WfpYcgEEfWLfOFUqkWXTDkzur9SSbllxnbwRml+SEDMLLXcm7XgP+IlJUt6ftlWvEtQzCN11oxU+fzYVWvQ8VMSKAq3vhwacBZ9K4OJe4WtFoJDzPq2ILx6Lf/VUh6eqsr8GcH93C7OdNcYKq4g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 16419144324093.6342035753658593; Tue, 11 Jan 2022 07:20:32 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.255905.438894 (Exim 4.92) (envelope-from ) id 1n7IwT-00035h-HU; Tue, 11 Jan 2022 15:20:09 +0000 Received: by outflank-mailman (output) from mailman id 255905.438894; Tue, 11 Jan 2022 15:20:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7IwT-00035a-EC; Tue, 11 Jan 2022 15:20:09 +0000 Received: by outflank-mailman (input) for mailman id 255905; Tue, 11 Jan 2022 15:20:08 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7Ip5-0002Fw-FD for xen-devel@lists.xenproject.org; Tue, 11 Jan 2022 15:12:31 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id de4d45f5-72f0-11ec-81c1-a30af7de8005; Tue, 11 Jan 2022 16:12:19 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id DBC7B1F3B8; Tue, 11 Jan 2022 15:12:18 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B2E1813DDD; Tue, 11 Jan 2022 15:12:18 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id SOJ9KlKe3WG8AwAAMHmgww (envelope-from ); Tue, 11 Jan 2022 15:12:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: de4d45f5-72f0-11ec-81c1-a30af7de8005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1641913938; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/J7Pbzy4bEMLs1Zfn02zqrQKoe12Kbt0uIHiCf5rrSk=; b=jRefBonpoL7Y3znlLNWa9M4TFpzlRZgE9MInSr2UJlJjav+cOsfGIENNqmwfxbIwkiCE0f grByPAHUcFc8WpgtefmOUQnilaHoq3aZTTO/yd163GJ7UfkwpckSX0VxO6mgoSJ/mNwO0A b4Tc4nZMc0y/ZgNUEgjRMXOdYxelxYU= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, wl@xen.org, Juergen Gross Subject: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket Date: Tue, 11 Jan 2022 16:12:13 +0100 Message-Id: <20220111151215.22955-11-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20220111151215.22955-1-jgross@suse.com> References: <20220111151215.22955-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1641914434985100001 Content-Type: text/plain; charset="utf-8" Even with some special handling needed in select_poll(), add a struct file_ops for FTYPE_SOCKET. Due to the need of the special handling it isn't possible to use a dynamically allocated file type. Most functions calling the file_ops methods can be simplified a lot now that no file type specific handling is left. Same applies to the file type name printing in debug/verbose mode. Signed-off-by: Juergen Gross Reviewed-by: Samuel Thibault --- lib/sys.c | 153 +++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 83 deletions(-) diff --git a/lib/sys.c b/lib/sys.c index 3a8aa68..12deaed 100644 --- a/lib/sys.c +++ b/lib/sys.c @@ -99,11 +99,70 @@ static struct file_ops file_ops_none =3D { .name =3D "none", }; =20 +#ifdef HAVE_LWIP +static int socket_read(int fd, void *buf, size_t nbytes) +{ + return lwip_read(fd, buf, nbytes); +} + +static int socket_write(int fd, const void *buf, size_t nbytes) +{ + return lwip_write(fd, (void *)buf, nbytes); +} + +static int close_socket_fd(int fd) +{ + struct file *file =3D get_file_from_fd(fd); + + return lwip_close(file->fd); +} + +static int socket_fstat(int fd, struct stat *buf) +{ + buf->st_mode =3D S_IFSOCK | S_IRUSR | S_IWUSR; + buf->st_atime =3D buf->st_mtime =3D buf->st_ctime =3D time(NULL); + + return 0; +} + +static int socket_fcntl(int fd, int cmd, va_list args) +{ + long arg; + struct file *file =3D get_file_from_fd(fd); + + arg =3D va_arg(args, long); + + if ( cmd =3D=3D F_SETFL && !(arg & ~O_NONBLOCK) ) + { + /* Only flag supported: non-blocking mode */ + uint32_t nblock =3D !!(arg & O_NONBLOCK); + + return lwip_ioctl(file->fd, FIONBIO, &nblock); + } + + printk("fcntl(%d, %d, %lx/%lo)\n", fd, cmd, arg, arg); + errno =3D ENOSYS; + return -1; +} + +static struct file_ops socket_ops =3D { + .name =3D "socket", + .read =3D socket_read, + .write =3D socket_write, + .close =3D close_socket_fd, + .fstat =3D socket_fstat, + .fcntl =3D socket_fcntl, +}; +#endif + static struct file_ops *file_ops[FTYPE_N + FTYPE_SPARE] =3D { [FTYPE_NONE] =3D &file_ops_none, #ifdef CONFIG_CONSFRONT [FTYPE_CONSOLE] =3D &console_ops, #endif +#ifdef HAVE_LWIP + [FTYPE_SOCKET] =3D &socket_ops, +#endif }; =20 unsigned int alloc_file_type(struct file_ops *ops) @@ -282,14 +341,6 @@ int read(int fd, void *buf, size_t nbytes) if ( ops->read ) return ops->read(fd, buf, nbytes); =20 - switch (files[fd].type) { -#ifdef HAVE_LWIP - case FTYPE_SOCKET: - return lwip_read(files[fd].fd, buf, nbytes); -#endif - default: - break; - } printk("read(%d): Bad descriptor\n", fd); errno =3D EBADF; return -1; @@ -302,14 +353,6 @@ int write(int fd, const void *buf, size_t nbytes) if ( ops->write ) return ops->write(fd, buf, nbytes); =20 - switch (files[fd].type) { -#ifdef HAVE_LWIP - case FTYPE_SOCKET: - return lwip_write(files[fd].fd, (void*) buf, nbytes); -#endif - default: - break; - } printk("write(%d): Bad descriptor\n", fd); errno =3D EBADF; return -1; @@ -378,26 +421,14 @@ int close(int fd) =20 printk("close(%d)\n", fd); if ( ops->close ) - { res =3D ops->close(fd); - goto out; - } - - switch (files[fd].type) { - default: - break; -#ifdef HAVE_LWIP - case FTYPE_SOCKET: - res =3D lwip_close(files[fd].fd); - break; -#endif - case FTYPE_NONE: - printk("close(%d): Bad descriptor\n", fd); - errno =3D EBADF; - return -1; + else if ( files[fd].type =3D=3D FTYPE_NONE ) + { + printk("close(%d): Bad descriptor\n", fd); + errno =3D EBADF; + return -1; } =20 - out: memset(files + fd, 0, sizeof(struct file)); files[fd].type =3D FTYPE_NONE; return res; @@ -429,21 +460,6 @@ int fstat(int fd, struct stat *buf) if ( ops->fstat ) return ops->fstat(fd, buf); =20 - switch (files[fd].type) { - case FTYPE_SOCKET: { - buf->st_mode =3D S_IFSOCK|S_IRUSR|S_IWUSR; - buf->st_uid =3D 0; - buf->st_gid =3D 0; - buf->st_size =3D 0; - buf->st_atime =3D=20 - buf->st_mtime =3D=20 - buf->st_ctime =3D time(NULL); - return 0; - } - default: - break; - } - printk("statf(%d): Bad descriptor\n", fd); errno =3D EBADF; return -1; @@ -491,21 +507,9 @@ int fcntl(int fd, int cmd, ...) arg =3D va_arg(ap, long); va_end(ap); =20 - switch (cmd) { -#ifdef HAVE_LWIP - case F_SETFL: - if (files[fd].type =3D=3D FTYPE_SOCKET && !(arg & ~O_NONBLOCK)) { - /* Only flag supported: non-blocking mode */ - uint32_t nblock =3D !!(arg & O_NONBLOCK); - return lwip_ioctl(files[fd].fd, FIONBIO, &nblock); - } - /* Fallthrough */ -#endif - default: - printk("fcntl(%d, %d, %lx/%lo)\n", fd, cmd, arg, arg); - errno =3D ENOSYS; - return -1; - } + printk("fcntl(%d, %d, %lx/%lo)\n", fd, cmd, arg, arg); + errno =3D ENOSYS; + return -1; } =20 DIR *opendir(const char *name) @@ -539,23 +543,6 @@ int closedir(DIR *dir) =20 /* We assume that only the main thread calls select(). */ =20 -#if defined(LIBC_DEBUG) || defined(LIBC_VERBOSE) -static const char *file_types[] =3D { - [FTYPE_NONE] =3D "none", - [FTYPE_SOCKET] =3D "socket", -}; - -static char *get_type_name(unsigned int type) -{ - if ( type < ARRAY_SIZE(file_ops) && file_ops[type] ) - return file_ops[type]->name; - - if ( type < ARRAY_SIZE(file_types) && file_types[type] ) - return file_types[type]; - - return "none"; -} -#endif #ifdef LIBC_DEBUG static void dump_set(int nfds, fd_set *readfds, fd_set *writefds, fd_set *= exceptfds, struct timeval *timeout) { @@ -566,7 +553,7 @@ static void dump_set(int nfds, fd_set *readfds, fd_set = *writefds, fd_set *except if (FD_ISSET(i, set)) { \ if (comma) \ printk(", "); \ - printk("%d(%s)", i, get_type_name(files[i].type)); \ + printk("%d(%s)", i, get_file_ops(files[i].type)->name); \ comma =3D 1; \ } \ } \ @@ -600,7 +587,7 @@ static void dump_pollfds(struct pollfd *pfd, int nfds, = int timeout) fd =3D pfd[i].fd; if (comma) printk(", "); - printk("%d(%s)/%02x", fd, get_type_name(files[fd].type), + printk("%d(%s)/%02x", fd, get_file_ops(files[fd].type)->name, pfd[i].events); comma =3D 1; } @@ -754,7 +741,7 @@ static int select_poll(int nfds, fd_set *readfds, fd_se= t *writefds, fd_set *exce printk("%d(%d): ", nb, sock_n); for (i =3D 0; i < nfds; i++) { if (nbread[i] || nbwrite[i] || nbexcept[i]) - printk(" %d(%c):", i, get_type_name(files[i].type)); + printk(" %d(%c):", i, get_file_ops(files[i].type)->name); if (nbread[i]) printk(" %dR", nbread[i]); if (nbwrite[i]) --=20 2.26.2