From patchwork Wed Oct 4 12:47:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 13408718 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B51E3E7B60D for ; Wed, 4 Oct 2023 12:46:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242477AbjJDMqj (ORCPT ); Wed, 4 Oct 2023 08:46:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242500AbjJDMqf (ORCPT ); Wed, 4 Oct 2023 08:46:35 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11AC5C4 for ; Wed, 4 Oct 2023 05:46:31 -0700 (PDT) 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-out1.suse.de (Postfix) with ESMTPS id B603E2183A; Wed, 4 Oct 2023 12:46:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1696423589; 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=Pad8hg3mqiq7KVkuWJoEhHoGMZyhbam01mzRl/lGIqs=; b=LmseKLVyN/nQe/vsWyEixbo+TQsj89cQl+NgsRE2oW1lmP+3kgxfijgl/ES2PXWymSUUmM Y2rH17oDll8LSubfe52/DpiZAEHePWcc973T/C1WH3PDaUg+DI7Xy5ecpVuR2xSuo95ku+ YmMfk+SzFbwwm3XspzmhT/vx55avIB4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1696423589; 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=Pad8hg3mqiq7KVkuWJoEhHoGMZyhbam01mzRl/lGIqs=; b=hR9SEoHneh6On1/FWwrqz3BUHW4/M0mDvGSxLasox/mB+RMzo5ZJdjA325fcsqxbTZn6ZX 5xieGGkHVgqKBdAw== 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 A001113A2E; Wed, 4 Oct 2023 12:46:29 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 772qJaVeHWXbPAAAMHmgww (envelope-from ); Wed, 04 Oct 2023 12:46:29 +0000 From: Cyril Hrubis To: ltp@lists.linux.it Cc: Matthew Wilcox , amir73il@gmail.com, mszeredi@redhat.com, brauner@kernel.org, viro@zeniv.linux.org.uk, Jan Kara , linux-fsdevel@vger.kernel.org Subject: [PATCH 1/3] lib: Add tst_fd_iterate() Date: Wed, 4 Oct 2023 14:47:10 +0200 Message-ID: <20231004124712.3833-2-chrubis@suse.cz> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231004124712.3833-1-chrubis@suse.cz> References: <20231004124712.3833-1-chrubis@suse.cz> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Which allows us to call a function on bunch of different file descriptors. Signed-off-by: Cyril Hrubis --- include/tst_fd.h | 39 +++++++++++++++ include/tst_test.h | 1 + lib/tst_fd.c | 116 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 include/tst_fd.h create mode 100644 lib/tst_fd.c diff --git a/include/tst_fd.h b/include/tst_fd.h new file mode 100644 index 000000000..711e043dd --- /dev/null +++ b/include/tst_fd.h @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * Copyright (C) 2023 Cyril Hrubis + */ + +#ifndef TST_FD_H__ +#define TST_FD_H__ + +enum tst_fd_type { + TST_FD_FILE, + TST_FD_DIR, + TST_FD_DEV_ZERO, + TST_FD_PROC_MAPS, + TST_FD_PIPE_IN, + TST_FD_PIPE_OUT, + TST_FD_UNIX_SOCK, + TST_FD_INET_SOCK, + TST_FD_IO_URING, + TST_FD_BPF_MAP, + TST_FD_MAX, +}; + +struct tst_fd { + enum tst_fd_type type; + int fd; +}; + +/* + * Iterates over all fd types and calls the run_test function for each of them. + */ +void tst_fd_iterate(void (*run_test)(struct tst_fd *fd)); + +/* + * Returns human readable name for the file descriptor type. + */ +const char *tst_fd_desc(struct tst_fd *fd); + +#endif /* TST_FD_H__ */ diff --git a/include/tst_test.h b/include/tst_test.h index 75c2109b9..5eee36bac 100644 --- a/include/tst_test.h +++ b/include/tst_test.h @@ -44,6 +44,7 @@ #include "tst_taint.h" #include "tst_memutils.h" #include "tst_arch.h" +#include "tst_fd.h" /* * Reports testcase result. diff --git a/lib/tst_fd.c b/lib/tst_fd.c new file mode 100644 index 000000000..7b6cb767e --- /dev/null +++ b/lib/tst_fd.c @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * Copyright (C) 2023 Cyril Hrubis + */ + +#define TST_NO_DEFAULT_MAIN + +#include "tst_test.h" +#include "tst_safe_macros.h" +#include "lapi/io_uring.h" +#include "lapi/bpf.h" + +#include "tst_fd.h" + +const char *tst_fd_desc(struct tst_fd *fd) +{ + switch (fd->type) { + case TST_FD_FILE: + return "regular file"; + case TST_FD_DIR: + return "directory"; + case TST_FD_DEV_ZERO: + return "/dev/zero"; + case TST_FD_PROC_MAPS: + return "/proc/self/maps"; + case TST_FD_PIPE_IN: + return "pipe read end"; + case TST_FD_PIPE_OUT: + return "pipe write end"; + case TST_FD_UNIX_SOCK: + return "unix socket"; + case TST_FD_INET_SOCK: + return "inet socket"; + case TST_FD_IO_URING: + return "io_uring"; + case TST_FD_BPF_MAP: + return "bpf map"; + case TST_FD_MAX: + break; + } + + return "invalid"; +} + +void tst_fd_iterate(void (*run_test)(struct tst_fd *fd)) +{ + enum tst_fd_type i; + struct tst_fd fd; + int pipe[2]; + struct io_uring_params uring_params = {}; + union bpf_attr array_attr = { + .map_type = BPF_MAP_TYPE_ARRAY, + .key_size = 4, + .value_size = 8, + .max_entries = 1, + }; + + SAFE_PIPE(pipe); + + for (i = 0; i < TST_FD_MAX; i++) { + fd.type = i; + + switch (i) { + case TST_FD_FILE: + fd.fd = SAFE_OPEN("fd_file", O_RDWR | O_CREAT); + SAFE_UNLINK("fd_file"); + break; + case TST_FD_DIR: + SAFE_MKDIR("fd_dir", 0700); + fd.fd = SAFE_OPEN("fd_dir", O_DIRECTORY); + SAFE_RMDIR("fd_dir"); + break; + case TST_FD_DEV_ZERO: + fd.fd = SAFE_OPEN("/dev/zero", O_RDONLY); + break; + case TST_FD_PROC_MAPS: + fd.fd = SAFE_OPEN("/proc/self/maps", O_RDONLY); + break; + case TST_FD_PIPE_IN: + fd.fd = pipe[0]; + break; + case TST_FD_PIPE_OUT: + fd.fd = pipe[1]; + break; + case TST_FD_UNIX_SOCK: + fd.fd = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0); + break; + case TST_FD_INET_SOCK: + fd.fd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0); + break; + case TST_FD_IO_URING: + fd.fd = io_uring_setup(1, &uring_params); + if (fd.fd < 0) { + tst_res(TCONF | TERRNO, + "Skipping %s", tst_fd_desc(&fd)); + continue; + } + break; + case TST_FD_BPF_MAP: + fd.fd = bpf(BPF_MAP_CREATE, &array_attr, sizeof(array_attr)); + if (fd.fd < 0) { + tst_res(TCONF | TERRNO, + "Skipping %s", tst_fd_desc(&fd)); + continue; + } + break; + case TST_FD_MAX: + break; + } + + run_test(&fd); + + SAFE_CLOSE(fd.fd); + } +} From patchwork Wed Oct 4 12:47:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 13408716 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2E20E7B608 for ; Wed, 4 Oct 2023 12:46:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242486AbjJDMqg (ORCPT ); Wed, 4 Oct 2023 08:46:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242492AbjJDMqf (ORCPT ); Wed, 4 Oct 2023 08:46:35 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AA1AA9 for ; Wed, 4 Oct 2023 05:46:31 -0700 (PDT) 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 472BE1F74D; Wed, 4 Oct 2023 12:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1696423590; 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=VeItCfmREetMZWOyX+YlTVpWpRrFsfEO9h9NMi+hFF4=; b=NjygP4OoL0voA6n/laTXMpQgECPb+aGqkdWHZoxcFzzc/u0oWpperwPsHd1LAUSc6PkNVh 6kD8ppsSz+vYxbMr5oTc0AWj0G+cIZ52sfYWKgGBsA2u9PqFSFswuroWw56kZXI7dQT4OI TaJJ2M5ttQUDL3yH1OXxpzGOpmzP3J8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1696423590; 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=VeItCfmREetMZWOyX+YlTVpWpRrFsfEO9h9NMi+hFF4=; b=RUvomOcmVyLNb54amrb/byVARm9JdWj7NtSqY/lRUYoKIqa6p8etlnDwFVLnx10QIvI4t4 nXkEpGm+0jy5ohDg== 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 328D413A2E; Wed, 4 Oct 2023 12:46:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id vdzuCqZeHWXdPAAAMHmgww (envelope-from ); Wed, 04 Oct 2023 12:46:30 +0000 From: Cyril Hrubis To: ltp@lists.linux.it Cc: Matthew Wilcox , amir73il@gmail.com, mszeredi@redhat.com, brauner@kernel.org, viro@zeniv.linux.org.uk, Jan Kara , linux-fsdevel@vger.kernel.org Subject: [PATCH 2/3] syscalls/readahead01: Make use of tst_fd_iterate() Date: Wed, 4 Oct 2023 14:47:11 +0200 Message-ID: <20231004124712.3833-3-chrubis@suse.cz> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231004124712.3833-1-chrubis@suse.cz> References: <20231004124712.3833-1-chrubis@suse.cz> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org TODO: readahead() on /proc/self/maps seems to succeed is that to be expected? Signed-off-by: Cyril Hrubis --- .../kernel/syscalls/readahead/readahead01.c | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/testcases/kernel/syscalls/readahead/readahead01.c b/testcases/kernel/syscalls/readahead/readahead01.c index bdef7945d..28134d416 100644 --- a/testcases/kernel/syscalls/readahead/readahead01.c +++ b/testcases/kernel/syscalls/readahead/readahead01.c @@ -29,44 +29,38 @@ #if defined(__NR_readahead) static void test_bad_fd(void) -{ - char tempname[PATH_MAX] = "readahead01_XXXXXX"; - int fd; - - tst_res(TINFO, "%s -1", __func__); - TST_EXP_FAIL(readahead(-1, 0, getpagesize()), EBADF); - - tst_res(TINFO, "%s O_WRONLY", __func__); - fd = mkstemp(tempname); - if (fd == -1) - tst_res(TFAIL | TERRNO, "mkstemp failed"); - SAFE_CLOSE(fd); - fd = SAFE_OPEN(tempname, O_WRONLY); - TST_EXP_FAIL(readahead(fd, 0, getpagesize()), EBADF); - SAFE_CLOSE(fd); - unlink(tempname); -} - -static void test_invalid_fd(void) { int fd[2]; - tst_res(TINFO, "%s pipe", __func__); + TST_EXP_FAIL(readahead(-1, 0, getpagesize()), EBADF, + "readahead() with fd = -1"); + SAFE_PIPE(fd); - TST_EXP_FAIL(readahead(fd[0], 0, getpagesize()), EINVAL); SAFE_CLOSE(fd[0]); SAFE_CLOSE(fd[1]); - tst_res(TINFO, "%s socket", __func__); - fd[0] = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0); - TST_EXP_FAIL(readahead(fd[0], 0, getpagesize()), EINVAL); - SAFE_CLOSE(fd[0]); + TST_EXP_FAIL(readahead(fd[0], 0, getpagesize()), EBADF, + "readahead() with invalid fd"); +} + +static void test_invalid_fd(struct tst_fd *fd) +{ + switch (fd->type) { + case TST_FD_FILE: + case TST_FD_PIPE_OUT: + return; + default: + break; + } + + TST_EXP_FAIL(readahead(fd->fd, 0, getpagesize()), EINVAL, + "readahead() on %s", tst_fd_desc(fd)); } static void test_readahead(void) { test_bad_fd(); - test_invalid_fd(); + tst_fd_iterate(test_invalid_fd); } static void setup(void) From patchwork Wed Oct 4 12:47:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 13408717 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEED3E7B60C for ; Wed, 4 Oct 2023 12:46:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242499AbjJDMqg (ORCPT ); Wed, 4 Oct 2023 08:46:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242394AbjJDMqf (ORCPT ); Wed, 4 Oct 2023 08:46:35 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E595C9 for ; Wed, 4 Oct 2023 05:46:32 -0700 (PDT) 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 C32D31F855; Wed, 4 Oct 2023 12:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1696423590; 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=OUoVyxQDPI04NNDmHhtQVcaincSOaQHC43/Y+1GZ/uQ=; b=q+fA0bpyoiDMgg9i3CJ3rwpMwxClR9dIiTSozDPym2lvQDzfmdZyaAK48DoLihanXG4+1/ R7OZ6kf68gDVY2FXJrSjZG5UyBZqDCRkZt92g5u7Dp4Pd3hDFUAJp4Uj9s8Q/f/ix1wrSi R8Vmds5NYYbLM4SlmzKMSQfhdaY2jy8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1696423590; 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=OUoVyxQDPI04NNDmHhtQVcaincSOaQHC43/Y+1GZ/uQ=; b=HO7kkoFSU5Jhhc493Sg2u9zSHCBpRgjdqZ5bfqsGy0n1BEchfSEx1V1LNwL29a7k4Ph7Dq MoGpxm9xlfntKyAA== 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 AD23813A2E; Wed, 4 Oct 2023 12:46:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id f2zvKKZeHWXfPAAAMHmgww (envelope-from ); Wed, 04 Oct 2023 12:46:30 +0000 From: Cyril Hrubis To: ltp@lists.linux.it Cc: Matthew Wilcox , amir73il@gmail.com, mszeredi@redhat.com, brauner@kernel.org, viro@zeniv.linux.org.uk, Jan Kara , linux-fsdevel@vger.kernel.org Subject: [PATCH 3/3] syscalls: accept: Add tst_fd_iterate() test Date: Wed, 4 Oct 2023 14:47:12 +0200 Message-ID: <20231004124712.3833-4-chrubis@suse.cz> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231004124712.3833-1-chrubis@suse.cz> References: <20231004124712.3833-1-chrubis@suse.cz> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Signed-off-by: Cyril Hrubis --- runtest/syscalls | 1 + testcases/kernel/syscalls/accept/.gitignore | 1 + testcases/kernel/syscalls/accept/accept01.c | 8 ---- testcases/kernel/syscalls/accept/accept03.c | 46 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 testcases/kernel/syscalls/accept/accept03.c diff --git a/runtest/syscalls b/runtest/syscalls index 8652e0bd3..25b53a724 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -3,6 +3,7 @@ abort01 abort01 accept01 accept01 accept02 accept02 +accept03 accept03 accept4_01 accept4_01 diff --git a/testcases/kernel/syscalls/accept/.gitignore b/testcases/kernel/syscalls/accept/.gitignore index 5b1462699..f81d4bec9 100644 --- a/testcases/kernel/syscalls/accept/.gitignore +++ b/testcases/kernel/syscalls/accept/.gitignore @@ -1,2 +1,3 @@ /accept01 /accept02 +/accept03 diff --git a/testcases/kernel/syscalls/accept/accept01.c b/testcases/kernel/syscalls/accept/accept01.c index 85af0f8af..e5db1dfec 100644 --- a/testcases/kernel/syscalls/accept/accept01.c +++ b/testcases/kernel/syscalls/accept/accept01.c @@ -26,7 +26,6 @@ struct sockaddr_in sin0, sin1, fsin1; int invalid_socketfd = 400; /* anything that is not an open file */ -int devnull_fd; int socket_fd; int udp_fd; @@ -45,10 +44,6 @@ static struct test_case { (struct sockaddr *)&fsin1, sizeof(fsin1), EBADF, "bad file descriptor" }, - { - PF_INET, SOCK_STREAM, 0, &devnull_fd, (struct sockaddr *)&fsin1, - sizeof(fsin1), ENOTSOCK, "fd is not socket" - }, { PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)3, sizeof(fsin1), EINVAL, "invalid socket buffer" @@ -73,8 +68,6 @@ static void test_setup(void) sin0.sin_port = 0; sin0.sin_addr.s_addr = INADDR_ANY; - devnull_fd = SAFE_OPEN("/dev/null", O_WRONLY); - socket_fd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0); SAFE_BIND(socket_fd, (struct sockaddr *)&sin0, sizeof(sin0)); @@ -88,7 +81,6 @@ static void test_setup(void) static void test_cleanup(void) { - SAFE_CLOSE(devnull_fd); SAFE_CLOSE(socket_fd); SAFE_CLOSE(udp_fd); } diff --git a/testcases/kernel/syscalls/accept/accept03.c b/testcases/kernel/syscalls/accept/accept03.c new file mode 100644 index 000000000..6bced33b6 --- /dev/null +++ b/testcases/kernel/syscalls/accept/accept03.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * Copyright (C) 2023 Cyril Hrubis + */ + +/*\ + * [Description] + * + * Verify that accept() returns ENOTSOCK for non-socket file descriptors. + */ + +#include +#include + +#include "tst_test.h" + +void check_accept(struct tst_fd *fd) +{ + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_port = 0, + .sin_addr = {.s_addr = INADDR_ANY}, + }; + socklen_t size = sizeof(addr); + + switch (fd->type) { + case TST_FD_UNIX_SOCK: + case TST_FD_INET_SOCK: + return; + default: + break; + } + + TST_EXP_FAIL2(accept(fd->fd, (void*)&addr, &size), + ENOTSOCK, "accept() on %s", tst_fd_desc(fd)); +} + +static void verify_accept(void) +{ + tst_fd_iterate(check_accept); +} + +static struct tst_test test = { + .test_all = verify_accept, +};