From patchwork Thu Aug 22 10:39:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1975433 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DoL/L3t8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WqKWB1BSmz1ybW for ; Thu, 22 Aug 2024 20:41:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E371E3871012 for ; Thu, 22 Aug 2024 10:41:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id ED7BA3858283 for ; Thu, 22 Aug 2024 10:40:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED7BA3858283 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ED7BA3858283 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724323208; cv=none; b=kjTMxuaehemkAOSMOjd3gBPU1DNm7PA1PiBCs1xW0xaTg824cZ2Ax+4wepvfzijTBEgecNLuxi57YVxJIsRcC7Oy0WpIOqfUya2vovzsokmwt8gOCWcmbAJMB9RkKTNFU/Q+8vlrbMvfBkzrpeQE1fHtELTK+dylvE2Rdgr9VgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724323208; c=relaxed/simple; bh=0CMflc6uQ6TMCHwEQefqlrow5dWOSvp+9oWRLabsWxg=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=JtfnkkoFduHJVIPNbT0sfxakfEBMVWCiIq7YxsTVPDKQkvKs4ckOsK2QE8FSXsdk6QubUUak+TJCHWjX0II9he4uMzuVJEqsl7trZtWAAljzQUVIBG+GwiGnt+4aWOMlPS1Qt4quak5vWkuuffIRRTWodbAd8TbzY7Zs0gkalRU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724323202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=f3yeAIQw/X9MJhlKBOWd9J9WlM6GhdoF08MI3srjJv8=; b=DoL/L3t8UuNW/t3XQm7k0ajvV3UcosBPnchaejHUIaa02UBcma7y77BxmelTn7WEfsHgtr wclov1KzKsg5ngeFuo4TeaT7Hm1/VtMwmqAUHxvmZ8AyyHU67/sC/LrsdbtTth6I6MfGSx k7XWeZxPRdviGbZm7chXb1fSp0vC8GY= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-548-VJglPNmONuOsZ8SVKSDhVw-1; Thu, 22 Aug 2024 06:40:01 -0400 X-MC-Unique: VJglPNmONuOsZ8SVKSDhVw-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6DF0D1955F43 for ; Thu, 22 Aug 2024 10:40:00 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.22]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9950A300019C for ; Thu, 22 Aug 2024 10:39:58 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 1/3] Linux: Add FUSE-based file system test framework to support/ In-Reply-To: Message-ID: References: X-From-Line: aba0806fdd8c7ab06ab88fd7441b7a07e8f61f5a Mon Sep 17 00:00:00 2001 Date: Thu, 22 Aug 2024 12:39:55 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org This allows to monitor the exact file system operations performed by glibc and inject errors. --- support/Makefile | 1 + support/fuse.h | 180 ++++++ support/support_fuse.c | 21 + sysdeps/unix/sysv/linux/Makefile | 6 + sysdeps/unix/sysv/linux/support_fuse.c | 601 +++++++++++++++++++++ sysdeps/unix/sysv/linux/tst-support_fuse.c | 315 +++++++++++ 6 files changed, 1124 insertions(+) create mode 100644 support/fuse.h create mode 100644 support/support_fuse.c create mode 100644 sysdeps/unix/sysv/linux/support_fuse.c create mode 100644 sysdeps/unix/sysv/linux/tst-support_fuse.c diff --git a/support/Makefile b/support/Makefile index 6e3c55394f..d5039ddeb2 100644 --- a/support/Makefile +++ b/support/Makefile @@ -62,6 +62,7 @@ libsupport-routines = \ support_format_herrno \ support_format_hostent \ support_format_netent \ + support_fuse \ support_isolate_in_subprocess \ support_mutex_pi_monotonic \ support_need_proc \ diff --git a/support/fuse.h b/support/fuse.h new file mode 100644 index 0000000000..7a049cfaca --- /dev/null +++ b/support/fuse.h @@ -0,0 +1,180 @@ +/* Facilities for FUSE-backed file system tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Before using this functionality, use support_enter_mount_namespace + to ensure that mounts do not impact the overall system. */ + +#ifndef SUPPORT_FUSE_H +#define SUPPORT_FUSE_H + +#include +#include +#include +#include + +/* Defined in . */ +struct fuse_attr_out; +struct fuse_entry_out; +struct fuse_in_header; +struct fuse_open_out; + +/* Opaque type for tracking FUSE mount state. */ +struct support_fuse; + +/* This function disables a mount point created using + support_fuse_mount. */ +void support_fuse_unmount (struct support_fuse *) __nonnull ((1)); + +/* This function is called on a separate thread after calling + support_fuse_mount. F is the mount state, and CLOSURE the argument + that was passed to support_fuse_mount. The callback function is + expected to call support_fuse_read to read packets from the kernel + and handle them according to the test's need. */ +typedef void (*support_fuse_callback) (struct support_fuse *f, void *closure); + +/* This function creates a new mount point, implemented by CALLBACK. + CLOSURE is passed to CALLBACK as the second argument. */ +struct support_fuse *support_fuse_mount (support_fuse_callback callback, + void *closure) + __nonnull ((1)) __attr_dealloc (support_fuse_unmount, 1); + +/* This function returns the path to the mount point for F. The + returned string is valid until support_fuse_unmount (F) is called. */ +const char * support_fuse_mountpoint (struct support_fuse *f) __nonnull ((1)); + + +/* Renders the OPCODE as a string (FUSE_* constant. The caller must + free the returned string. */ +char * support_fuse_opcode (uint32_t opcode) __attr_dealloc_free; + +/* Use to provide a checked cast facility. Use the + support_fuse_in_cast macro below. */ +void *support_fuse_cast_internal (struct fuse_in_header *, uint32_t) + __nonnull ((1)); +void *support_fuse_cast_name_internal (struct fuse_in_header *, uint32_t, + size_t skip, char **name) + __nonnull ((1)); + +/* The macro expansion support_fuse_in_cast (P, TYPE) casts the + pointer INH to the appropriate type corresponding to the FUSE_TYPE + opcode. It fails (terminates the process) if INH->opcode does not + match FUSE_TYPE. The type of the returned pointer matches that of + the FUSE_* constant. */ +#define support_fuse_payload_type_INIT struct fuse_init_in +#define support_fuse_payload_type_LOOKUP char +#define support_fuse_payload_type_OPEN struct fuse_open_in +#define support_fuse_payload_type_READ struct fuse_read_in +#define support_fuse_payload_type_SETATTR struct fuse_setattr_in +#define support_fuse_payload_type_WRITE struct fuse_write_in +#define support_fuse_cast(typ, inh) \ + ((support_fuse_payload_type_##typ *) \ + support_fuse_cast_internal ((inh), FUSE_##typ)) + +/* Same as support_fuse_cast, but also writes the passed name to *NAMEP. */ +#define support_fuse_payload_name_type_CREATE struct fuse_create_in +#define support_fuse_payload_name_type_MKDIR struct fuse_mkdir_in +#define support_fuse_cast_name(typ, inh, namep) \ + ((support_fuse_payload_name_type_##typ *) \ + support_fuse_cast_name_internal \ + ((inh), FUSE_##typ, sizeof (support_fuse_payload_name_type_##typ), \ + (namep))) + +/* This function should be called from the callback function. It + returns NULL if the mount point has been unmounted. The result can + be cast using support_fuse_in_cast. The pointer is invalidated + with the next call to support_fuse_read. + + Typical use involves handling some basics using the + support_fuse_handle_* building blocks, following by a switch + statement on the result member of the returned struct, to implement + what a particular test needs. Casts to payload data should be made + using support_fuse_in_cast. + + By default, FUSE_FORGET responses are filtered. See + support_fuse_filter_forget for turning that off. */ +struct fuse_in_header *support_fuse_read (struct support_fuse *f) + __nonnull ((1)); + +/* This function can be called from a callback function to handle + basic aspects of directories (OPENDIR, GETATTR, RELEASEDIR). + inh->nodeid is used as the inode number for the directory. This + function must be called after support_fuse_read. */ +bool support_fuse_handle_directory (struct support_fuse *f) __nonnull ((1)); + +/* This function can be called from a callback function to handle + access to the mount point itself, after call support_fuse_read. */ +bool support_fuse_handle_mountpoint (struct support_fuse *f) __nonnull ((1)); + +/* If FILTER_ENABLED, future support_fuse_read calls will not return + FUSE_FORGET events (and simply discared them, as they require no + reply). If !FILTER_ENABLED, the callback needs to handled + FUSE_FORGET events and call support_fuse_no_reply. */ +void support_fuse_filter_forget (struct support_fuse *f, bool filter_enabled) + __nonnull ((1)); + +/* This function should be called from the callback function after + support_fuse_read returned a non-null pointer. It sends out a + response packet on the FUSE device with the supplied payload data. */ +void support_fuse_reply (struct support_fuse *f, + const void *payload, size_t payload_size) + __nonnull ((1)) __attr_access ((__read_only__, 2, 3)); + +/* This function should be called from the callback function. It + replies to a request with an error indicator. ERROR must be positive. */ +void support_fuse_reply_error (struct support_fuse *f, uint32_t error) + __nonnull ((1)); + +/* This function should be called from the callback function. It + sends out an empty (but success-indicating) reply packet. */ +void support_fuse_reply_empty (struct support_fuse *f) __nonnull ((1)); + +/* Do not send a reply. Only to be used after a support_fuse_read + call that returned a FUSE_FORGET event. */ +void support_fuse_no_reply (struct support_fuse *f) __nonnull ((1)); + +/* Specific reponse preparation functions. The returned object can be + updated as needed. If a NODEID argument is present, it will be + used to set the inode and FUSE nodeid fields. Without such an + argument, it is initialized from the current request (if the reply + requires this field). This function must be called after + support_fuse_read. The actual response must be sent using + support_fuse_reply_prepared (or a support_fuse_reply_error call can + be used to cancel the response). */ +struct fuse_entry_out *support_fuse_prepare_entry (struct support_fuse *f, + uint64_t nodeid) + __nonnull ((1)); +struct fuse_attr_out *support_fuse_prepare_attr (struct support_fuse *f) + __nonnull ((1)); + +/* Similar to the other support_fuse_prepare_* functions, but it + prepares for two response packets. They can be updated through the + pointers written to *OUT_ENTRY and *OUT_OPEN prior to calling + support_fuse_reply_prepared. */ +void support_fuse_prepare_create (struct support_fuse *f, + uint64_t nodeid, + struct fuse_entry_out **out_entry, + struct fuse_open_out **out_open) + __nonnull ((1, 3, 4)); + + +/* Send a prepared response. Must be called after one of the + support_fuse_prepare_* functions and before the next + support_fuse_read call. */ +void support_fuse_reply_prepared (struct support_fuse *f) __nonnull ((1)); + +#endif /* SUPPORT_FUSE_H */ diff --git a/support/support_fuse.c b/support/support_fuse.c new file mode 100644 index 0000000000..a399421f7a --- /dev/null +++ b/support/support_fuse.c @@ -0,0 +1,21 @@ +/* Facilities for FUSE-backed file system tests. Stub version. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* Not supported. */ diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 59998c7af4..7ef986bdd9 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -662,3 +662,9 @@ tests-internal += \ tst-rseq-nptl \ # tests-internal endif + +ifeq ($(subdir),support) +tests += \ + tst-support_fuse \ + # tests +endif # ($(subdir),support) diff --git a/sysdeps/unix/sysv/linux/support_fuse.c b/sysdeps/unix/sysv/linux/support_fuse.c new file mode 100644 index 0000000000..4e01ad2863 --- /dev/null +++ b/sysdeps/unix/sysv/linux/support_fuse.c @@ -0,0 +1,601 @@ +/* Facilities for FUSE-backed file system tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +struct support_fuse +{ + char *mountpoint; + void *buffer_start; /* Begin of allocation. */ + void *buffer_next; /* Next read position. */ + void *buffer_limit; /* End of buffered data. */ + void *buffer_end; /* End of allocation. */ + struct fuse_in_header *inh; /* Most recent request (support_fuse_read). */ + union /* Space for prepared responses. */ + { + struct fuse_attr_out attr; + struct fuse_entry_out entry; + struct + { + struct fuse_entry_out entry; + struct fuse_open_out open; + } create; + } prepared; + size_t prepared_size; /* 0 if inactive. */ + pthread_t handler; /* Thread handling requests. */ + uid_t uid; /* Cached value for the current process. */ + uid_t gid; /* Cached value for the current process. */ + int fd; /* FUSE file descriptor. */ + int connection; /* Entry under /sys/fs/fuse/connections. */ + bool filter_forget; /* Controls FUSE_FORGET event dropping. */ +}; + +struct fuse_thread_wrapper_args +{ + struct support_fuse *f; + support_fuse_callback callback; + void *closure; +}; + +/* Allocate the read buffer in F with SIZE bytes capacity. Does not + free the previously allocated buffer. */ +static void support_fuse_allocate (struct support_fuse *f, size_t size) + __nonnull ((1)); + +/* Internal mkdtemp replacement */ +static char * support_fuse_mkdir (const char *prefix) __nonnull ((1)); + +/* Low-level allocation function for support_fuse_mount. Does not + perform the mount. */ +static struct support_fuse *support_fuse_open (void); + +/* Thread wrapper function for use with pthread_create. Uses struct + fuse_thread_wrapper_args. */ +static void *support_fuse_thread_wrapper (void *closure) __nonnull ((1)); + +/* Initial step before preparing a reply. SIZE must be the size of + the F->prepared member that is going to be used. */ +static void support_fuse_prepare_1 (struct support_fuse *f, size_t size); + +/* Similar to support_fuse_reply_error, but not check that ERROR is + not zero. */ +static void support_fuse_reply_error_1 (struct support_fuse *f, + uint32_t error) __nonnull ((1)); + +/* Path to the directory containing mount points. Initialized by an + ELF constructor. All mountpoints are collected there so that the + test wrapper can clean them up without keeping track of them + individually. */ +static char *support_fuse_mountpoints; + +/* PID of the process that should clean up the mount points in the ELF + destructor. */ +static pid_t support_fuse_cleanup_pid; + +static void +support_fuse_allocate (struct support_fuse *f, size_t size) +{ + f->buffer_start = xmalloc (size); + f->buffer_end = f->buffer_start + size; + f->buffer_limit = f->buffer_start; + f->buffer_next = f->buffer_limit; +} + +void +support_fuse_filter_forget (struct support_fuse *f, bool filter) +{ + f->filter_forget = filter; +} + +void * +support_fuse_cast_internal (struct fuse_in_header *p, uint32_t expected) +{ + if (expected != p->opcode + && !(expected == FUSE_READ && p->opcode == FUSE_READDIR)) + { + char *expected1 = support_fuse_opcode (expected); + char *actual = support_fuse_opcode (p->opcode); + FAIL_EXIT1 ("attempt to cast %s to %s", actual, expected1); + } + return p + 1; +} + +void * +support_fuse_cast_name_internal (struct fuse_in_header *p, uint32_t expected, + size_t skip, char **name) +{ + char *result = support_fuse_cast_internal (p, expected); + *name = result + skip; + return result; +} + +bool +support_fuse_handle_directory (struct support_fuse *f) +{ + TEST_VERIFY (f->inh != NULL); + switch (f->inh->opcode) + { + case FUSE_OPENDIR: + { + struct fuse_open_out out = + { + }; + support_fuse_reply (f, &out, sizeof (out)); + } + return true; + case FUSE_RELEASEDIR: + support_fuse_reply_empty (f); + return true; + case FUSE_GETATTR: + { + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFDIR | 0700; + support_fuse_reply_prepared (f); + } + return true; + default: + return false; + } +} + +bool +support_fuse_handle_mountpoint (struct support_fuse *f) +{ + TEST_VERIFY (f->inh != NULL); + /* 1 is the root node. */ + if (f->inh->opcode == FUSE_GETATTR && f->inh->nodeid == 1) + return support_fuse_handle_directory (f); + return false; +} + +static char * +support_fuse_mkdir (const char *prefix) +{ + /* Do not use mkdtemp to avoid interfering with its tests. */ + unsigned int counter = 1; + unsigned int pid = getpid (); + while (true) + { + char *path = xasprintf ("%s%u.%u/", prefix, pid, counter); + if (mkdir (path, 0700) == 0) + return path; + if (errno != EEXIST) + FAIL_EXIT1 ("mkdir (\"%s\"): %m", path); + free (path); + ++counter; + } +} + +struct support_fuse * +support_fuse_mount (support_fuse_callback callback, void *closure) +{ + /* Request at least minor version 12 because it changed struct sizes. */ + enum { min_version = 12 }; + + struct support_fuse *f = support_fuse_open (); + char *mount_options + = xasprintf ("fd=%d,rootmode=040700,user_id=%u,group_id=%u", + f->fd, f->uid, f->gid); + if (mount ("fuse", f->mountpoint, "fuse.glibc", + MS_NOSUID|MS_NODEV, mount_options) + != 0) + FAIL_EXIT1 ("FUSE mount on %s: %m", f->mountpoint); + free (mount_options); + + /* Retry with an older FUSE version. */ + while (true) + { + struct fuse_in_header *inh = support_fuse_read (f); + struct fuse_init_in *init_in = support_fuse_cast (INIT, inh); + if (init_in->major < 7 + || (init_in->major == 7 && init_in->minor < min_version)) + FAIL_UNSUPPORTED ("kernel FUSE version is %u.%u, too old", + init_in->major, init_in->minor); + if (init_in->major > 7) + { + uint32_t major = 7; + support_fuse_reply (f, &major, sizeof (major)); + continue; + } + TEST_VERIFY (init_in->flags & FUSE_DONT_MASK); + struct fuse_init_out out = + { + .major = 7, + .minor = min_version, + /* Request that the kernel does not apply umask. */ + .flags = FUSE_DONT_MASK, + }; + support_fuse_reply (f, &out, sizeof (out)); + + { + struct fuse_thread_wrapper_args args = + { + .f = f, + .callback = callback, + .closure = closure, + }; + f->handler = xpthread_create (NULL, + support_fuse_thread_wrapper, &args); + struct stat64 st; + xstat64 (f->mountpoint, &st); + f->connection = minor (st.st_dev); + /* Got a reply from the thread, safe to deallocate args. */ + } + + return f; + } +} + +const char * +support_fuse_mountpoint (struct support_fuse *f) +{ + return f->mountpoint; +} + +void +support_fuse_no_reply (struct support_fuse *f) +{ + TEST_VERIFY (f->inh != NULL); + TEST_COMPARE (f->inh->opcode, FUSE_FORGET); + f->inh = NULL; +} + +char * +support_fuse_opcode (uint32_t op) +{ + const char *result; + switch (op) + { +#define X(n) case n: result = #n; break + X(FUSE_LOOKUP); + X(FUSE_FORGET); + X(FUSE_GETATTR); + X(FUSE_SETATTR); + X(FUSE_READLINK); + X(FUSE_SYMLINK); + X(FUSE_MKNOD); + X(FUSE_MKDIR); + X(FUSE_UNLINK); + X(FUSE_RMDIR); + X(FUSE_RENAME); + X(FUSE_LINK); + X(FUSE_OPEN); + X(FUSE_READ); + X(FUSE_WRITE); + X(FUSE_STATFS); + X(FUSE_RELEASE); + X(FUSE_FSYNC); + X(FUSE_SETXATTR); + X(FUSE_GETXATTR); + X(FUSE_LISTXATTR); + X(FUSE_REMOVEXATTR); + X(FUSE_FLUSH); + X(FUSE_INIT); + X(FUSE_OPENDIR); + X(FUSE_READDIR); + X(FUSE_RELEASEDIR); + X(FUSE_FSYNCDIR); + X(FUSE_GETLK); + X(FUSE_SETLK); + X(FUSE_SETLKW); + X(FUSE_ACCESS); + X(FUSE_CREATE); + X(FUSE_INTERRUPT); + X(FUSE_BMAP); + X(FUSE_DESTROY); + X(FUSE_IOCTL); + X(FUSE_POLL); +/* These are not present in Linux 3.2. + X(FUSE_NOTIFY_REPLY); + X(FUSE_BATCH_FORGET); + X(FUSE_FALLOCATE); + X(FUSE_READDIRPLUS); + X(FUSE_RENAME2); + X(FUSE_LSEEK); + X(FUSE_COPY_FILE_RANGE); + X(FUSE_SETUPMAPPING); + X(FUSE_REMOVEMAPPING); + X(FUSE_SYNCFS); + X(FUSE_TMPFILE); + X(FUSE_STATX); */ +#undef X + default: + return xasprintf ("FUSE_unknown_%u", op); + } + return xstrdup (result); +} + +static struct support_fuse * +support_fuse_open (void) +{ + struct support_fuse *result = xmalloc (sizeof (*result)); + result->mountpoint = support_fuse_mkdir (support_fuse_mountpoints); + result->inh = NULL; + result->prepared_size = 0; + result->uid = getuid (); + result->gid = getgid (); + result->fd = open ("/dev/fuse", O_RDWR, 0); + if (result->fd < 0) + { + if (errno == ENOENT || errno == ENODEV || errno == EPERM + || errno == EACCES) + FAIL_UNSUPPORTED ("cannot open /dev/fuse: %m"); + else + FAIL_EXIT1 ("cannot open /dev/fuse: %m"); + } + result->connection = -1; + result->filter_forget = true; + support_fuse_allocate (result, FUSE_MIN_READ_BUFFER); + return result; +} + +static void +support_fuse_prepare_1 (struct support_fuse *f, size_t size) +{ + TEST_VERIFY (f->prepared_size == 0); + f->prepared_size = size; + memset (&f->prepared, 0, size); +} + +struct fuse_attr_out * +support_fuse_prepare_attr (struct support_fuse *f) +{ + support_fuse_prepare_1 (f, sizeof (f->prepared.attr)); + f->prepared.attr.attr.uid = f->uid; + f->prepared.attr.attr.gid = f->gid; + f->prepared.attr.attr.ino = f->inh->nodeid; + return &f->prepared.attr; +} + +void +support_fuse_prepare_create (struct support_fuse *f, + uint64_t nodeid, + struct fuse_entry_out **out_entry, + struct fuse_open_out **out_open) +{ + support_fuse_prepare_1 (f, sizeof (f->prepared.create)); + f->prepared.create.entry.nodeid = nodeid; + f->prepared.create.entry.attr.uid = f->uid; + f->prepared.create.entry.attr.gid = f->gid; + f->prepared.create.entry.attr.ino = nodeid; + *out_entry = &f->prepared.create.entry; + *out_open = &f->prepared.create.open; +} + +struct fuse_entry_out * +support_fuse_prepare_entry (struct support_fuse *f, uint64_t nodeid) +{ + support_fuse_prepare_1 (f, sizeof (f->prepared.entry)); + f->prepared.entry.nodeid = nodeid; + f->prepared.entry.attr.uid = f->uid; + f->prepared.entry.attr.gid = f->gid; + f->prepared.entry.attr.ino = nodeid; + return &f->prepared.entry; +} + +struct fuse_in_header * +support_fuse_read (struct support_fuse *f) +{ + TEST_VERIFY (f->inh == NULL); + while (true) + { + if (f->buffer_next < f->buffer_limit) + { + f->inh = f->buffer_next; + f->buffer_next = (void *) f->buffer_next + f->inh->len; + /* Suppress FUSE_FORGET responses if requested. */ + if (f->filter_forget && f->inh->opcode == FUSE_FORGET) + { + f->inh = NULL; + continue; + } + return f->inh; + } + ssize_t ret = read (f->fd, f->buffer_start, + f->buffer_end - f->buffer_start); + if (ret == 0) + FAIL_EXIT (1, "unexpected EOF on FUSE device"); + if (ret < 0 && errno == EINVAL) + { + /* Increase buffer size. */ + size_t new_size = 2 * (size_t) (f->buffer_end - f->buffer_start); + free (f->buffer_start); + support_fuse_allocate (f, new_size); + continue; + } + if (ret < 0) + { + if (errno == ENODEV) + /* Unmount detected. */ + return NULL; + FAIL_EXIT1 ("read error on FUSE device: %m"); + } + /* Read was successful, make [next, limit) the active buffer area. */ + f->buffer_next = f->buffer_start; + f->buffer_limit = (void *) f->buffer_start + ret; + } +} + +void +support_fuse_reply (struct support_fuse *f, + const void *payload, size_t payload_size) +{ + TEST_VERIFY_EXIT (f->inh != NULL); + TEST_VERIFY (f->prepared_size == 0); + struct fuse_out_header outh = + { + .len = sizeof (outh) + payload_size, + .unique = f->inh->unique, + }; + struct iovec iov[] = + { + { &outh, sizeof (outh) }, + { (void *) payload, payload_size }, + }; + ssize_t ret = writev (f->fd, iov, array_length (iov)); + if (ret < 0) + FAIL_EXIT1 ("FUSE write failed for %s response (%zu bytes payload): %m", + support_fuse_opcode (f->inh->opcode), payload_size); + if (ret != sizeof (outh) + payload_size) + FAIL_EXIT1 ("FUSE write short for %s response (%zu bytes payload):" + " %zd bytes", + support_fuse_opcode (f->inh->opcode), payload_size, ret); + f->inh = NULL; +} + +void +support_fuse_reply_empty (struct support_fuse *f) +{ + support_fuse_reply_error_1 (f, 0); +} + +static void +support_fuse_reply_error_1 (struct support_fuse *f, uint32_t error) +{ + TEST_VERIFY_EXIT (f->inh != NULL); + struct fuse_out_header outh = + { + .len = sizeof (outh), + .error = -error, + .unique = f->inh->unique, + }; + ssize_t ret = write (f->fd, &outh, sizeof (outh)); + if (ret < 0) + FAIL_EXIT1 ("FUSE write failed for %s error response: %m", + support_fuse_opcode (f->inh->opcode)); + if (ret != sizeof (outh)) + FAIL_EXIT1 ("FUSE write short for %s error response: %zd bytes", + support_fuse_opcode (f->inh->opcode), ret); + f->inh = NULL; + f->prepared_size = 0; +} + +void +support_fuse_reply_error (struct support_fuse *f, uint32_t error) +{ + TEST_VERIFY (error > 0); + support_fuse_reply_error_1 (f, error); +} + +void +support_fuse_reply_prepared (struct support_fuse *f) +{ + TEST_VERIFY_EXIT (f->prepared_size != 0); + /* Re-use the non-prepared reply function. It requires + f->prepared_size to be zero, so reset the field before the call. */ + size_t prepared_size = f->prepared_size; + f->prepared_size = 0; + support_fuse_reply (f, &f->prepared, prepared_size); +} + +static void * +support_fuse_thread_wrapper (void *closure) +{ + struct fuse_thread_wrapper_args args + = *(struct fuse_thread_wrapper_args *) closure; + + /* Handle the initial stat call. */ + struct fuse_in_header *inh = support_fuse_read (args.f); + if (inh == NULL || !support_fuse_handle_mountpoint (args.f)) + { + support_fuse_reply_error (args.f, EIO); + return NULL; + } + + args.callback (args.f, args.closure); + return NULL; +} + +void +support_fuse_unmount (struct support_fuse *f) +{ + { + char *path = xasprintf ("/sys/fs/fuse/connections/%d/abort", + f->connection); + /* Some kernels do not support these files under /sys. */ + int fd = open (path, O_RDWR | O_TRUNC); + if (fd >= 0) + { + TEST_COMPARE (write (fd, "1", 1), 1); + xclose (fd); + } + free (path); + } + if (umount (f->mountpoint) != 0) + FAIL ("FUSE: umount (\"%s\"): %m", f->mountpoint); + xpthread_join (f->handler); + if (rmdir (f->mountpoint) != 0) + FAIL ("FUSE: rmdir (\"%s\"): %m", f->mountpoint); + xclose (f->fd); + free (f->mountpoint); + free (f); +} + +static void __attribute__ ((constructor)) +init (void) +{ + /* The test_dir test driver variable is not yet set at this point. */ + const char *tmpdir = getenv ("TMPDIR"); + if (tmpdir == NULL || tmpdir[0] == '\0') + tmpdir = "/tmp"; + + char *prefix = xasprintf ("%s/glibc-tst-fuse.", tmpdir); + support_fuse_mountpoints = support_fuse_mkdir (prefix); + free (prefix); + support_fuse_cleanup_pid = getpid (); +} + +static void __attribute__ ((destructor)) +fini (void) +{ + if (support_fuse_cleanup_pid != getpid () + || support_fuse_mountpoints == NULL) + return; + DIR *dir = opendir (support_fuse_mountpoints); + while (true) + { + struct dirent64 *e = readdir64 (dir); + if (e == NULL) + /* Ignore errors. */ + break; + if (*e->d_name == '.') + /* Skip "." and "..". No hidden files expected. */ + continue; + if (unlinkat (dirfd (dir), e->d_name, AT_REMOVEDIR) != 0) + break; + rewinddir (dir); + } + rmdir (support_fuse_mountpoints); + free (support_fuse_mountpoints); + support_fuse_mountpoints = NULL; +} diff --git a/sysdeps/unix/sysv/linux/tst-support_fuse.c b/sysdeps/unix/sysv/linux/tst-support_fuse.c new file mode 100644 index 0000000000..db9266144e --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-support_fuse.c @@ -0,0 +1,315 @@ +/* Facilities for FUSE-backed file system tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + /* Turn on returning FUSE_FORGET responses. */ + support_fuse_filter_forget (f, false); + + /* Inode and nodeid for "file" and "new". */ + enum { NODE_FILE = 2, NODE_NEW, NODE_SUBDIR }; + struct fuse_in_header *inh; + while ((inh = support_fuse_read (f)) != NULL) + { + { + char *opcode = support_fuse_opcode (inh->opcode); + printf ("info: (T) event %s(%llu) len=%u nodeid=%llu\n", + opcode, (unsigned long long int) inh->unique, inh->len, + (unsigned long long int) inh->nodeid); + free (opcode); + } + + /* Handle mountpoint and basic directory operation for the root (1). */ + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + + switch (inh->opcode) + { + case FUSE_READDIR: + /* Implementation of getdents64. */ + if (inh->nodeid == 1) + { + struct fuse_read_in *p = support_fuse_cast (READ, inh); + if (p->offset == 0) + { + /* Pad the name to a multiple of 8, as required by + the kernel interface. */ + size_t name_size = 8; + size_t e_size = (offsetof (struct fuse_dirent, name) + + name_size); + struct fuse_dirent *e = xmalloc (e_size); + e->ino = NODE_FILE; + e->off = 2; + e->namelen = name_size; + e->type = DT_REG; + memcpy (e->name, "file\0\0\0", 8); + support_fuse_reply (f, e, e_size); + free (e); + } + else + support_fuse_reply_empty (f); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_LOOKUP: + /* Part of the implementation of open. */ + { + char *name = support_fuse_cast (LOOKUP, inh); + printf (" name: %s\n", name); + if (inh->nodeid == 1 && strcmp (name, "file") == 0) + { + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, NODE_FILE); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, ENOENT); + } + break; + case FUSE_OPEN: + /* Implementation of open. */ + { + struct fuse_open_in *p = support_fuse_cast (OPEN, inh); + if (inh->nodeid == NODE_FILE) + { + TEST_VERIFY (!(p->flags & O_EXCL)); + struct fuse_open_out out = { 0, }; + support_fuse_reply (f, &out, sizeof (out)); + } + else + support_fuse_reply_error (f, ENOENT); + } + break; + case FUSE_GETATTR: + /* Happens after open. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFREG | 0600; + out->attr.size = strlen ("Hello, world!"); + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, ENOENT); + break; + case FUSE_READ: + /* Implementation of read. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_read_in *p = support_fuse_cast (READ, inh); + TEST_COMPARE (p->offset, 0); + TEST_VERIFY (p->size >= strlen ("Hello, world!")); + support_fuse_reply (f, + "Hello, world!", strlen ("Hello, world!")); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_FLUSH: + /* Sent in response to close. */ + support_fuse_reply_empty (f); + break; + case FUSE_GETXATTR: + /* This happens as part of a open-for-write operation. + Signal no support for extended attributes. */ + support_fuse_reply_error (f, ENOSYS); + break; + case FUSE_SETATTR: + /* This happens as part of a open-for-write operation to + implement O_TRUNC. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_setattr_in *p = support_fuse_cast (SETATTR, inh); + /* FATTR_LOCKOWNER may also be set. */ + TEST_COMPARE ((p->valid) & ~ FATTR_LOCKOWNER, FATTR_SIZE); + TEST_COMPARE (p->size, 0); + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_WRITE: + /* Implementation of write. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_write_in *p = support_fuse_cast (WRITE, inh); + TEST_COMPARE (p->offset, 0); + /* Write payload follows after struct fuse_write_in. */ + TEST_COMPARE_BLOB (p + 1, p->size, + "Good day to you too.", + strlen ("Good day to you too.")); + struct fuse_write_out out = + { + .size = p->size, + }; + support_fuse_reply (f, &out, sizeof (out)); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_CREATE: + /* Implementation of O_CREAT. */ + if (inh->nodeid == 1) + { + char *name; + struct fuse_create_in *p + = support_fuse_cast_name (CREATE, inh, &name); + TEST_VERIFY (S_ISREG (p->mode)); + TEST_COMPARE (p->mode & 07777, 0600); + TEST_COMPARE_STRING (name, "new"); + struct fuse_entry_out *out_entry; + struct fuse_open_out *out_open; + support_fuse_prepare_create (f, NODE_NEW, &out_entry, &out_open); + out_entry->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_MKDIR: + /* Implementation of mkdir. */ + { + if (inh->nodeid == 1) + { + char *name; + struct fuse_mkdir_in *p + = support_fuse_cast_name (MKDIR, inh, &name); + TEST_COMPARE (p->mode, 01234); + TEST_COMPARE_STRING (name, "subdir"); + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, NODE_SUBDIR); + out->attr.mode = S_IFDIR | p->mode; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + } + break; + case FUSE_FORGET: + support_fuse_no_reply (f); + break; + default: + support_fuse_reply_error (f, EIO); + } + } +} + +static int +do_test (void) +{ + support_become_root (); + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("mount namespaces not supported"); + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + + printf ("info: Attributes of mountpoint/root directory %s\n", + support_fuse_mountpoint (f)); + { + struct statx st; + xstatx (AT_FDCWD, support_fuse_mountpoint (f), 0, STATX_BASIC_STATS, &st); + TEST_COMPARE (st.stx_uid, getuid ()); + TEST_COMPARE (st.stx_gid, getgid ()); + TEST_VERIFY (S_ISDIR (st.stx_mode)); + TEST_COMPARE (st.stx_mode & 07777, 0700); + } + + printf ("info: List directory %s\n", support_fuse_mountpoint (f)); + { + DIR *dir = opendir (support_fuse_mountpoint (f)); + TEST_VERIFY_EXIT (dir != NULL); + struct dirent *e = readdir (dir); + TEST_VERIFY (e != NULL); + TEST_COMPARE (closedir (dir), 0); + } + + char *file_path = xasprintf ("%s/file", support_fuse_mountpoint (f)); + + printf ("info: Attributes of FILE %s\n", file_path); + { + struct statx st; + xstatx (AT_FDCWD, file_path, 0, STATX_BASIC_STATS, &st); + TEST_COMPARE (st.stx_uid, getuid ()); + TEST_COMPARE (st.stx_gid, getgid ()); + TEST_VERIFY (S_ISREG (st.stx_mode)); + TEST_COMPARE (st.stx_mode & 07777, 0600); + TEST_COMPARE (st.stx_size, strlen ("Hello, world!")); + } + + printf ("info: Read from %s\n", file_path); + { + int fd = xopen (file_path, O_RDONLY, 0); + char buf[64]; + ssize_t len = read (fd, buf, sizeof (buf)); + if (len < 0) + FAIL_EXIT1 ("read: %m"); + TEST_COMPARE_BLOB (buf, len, "Hello, world!", strlen ("Hello, world!")); + xclose (fd); + } + + printf ("info: Write to %s\n", file_path); + { + int fd = xopen (file_path, O_WRONLY | O_TRUNC, 0); + xwrite (fd, "Good day to you too.", strlen ("Good day to you too.")); + xclose (fd); + } + + printf ("info: Attempt O_EXCL creation of existing %s\n", file_path); + /* O_EXCL creation shall fail. */ + errno = 0; + TEST_COMPARE (open64 (file_path, O_RDWR | O_EXCL | O_CREAT, 0600), -1); + TEST_COMPARE (errno, EEXIST); + + free (file_path); + + { + char *new_path = xasprintf ("%s/new", support_fuse_mountpoint (f)); + printf ("info: Test successful O_EXCL creation at %s\n", new_path); + int fd = xopen (new_path, O_RDWR | O_EXCL | O_CREAT, 0600); + xclose (fd); + free (new_path); + } + + { + char *subdir_path = xasprintf ("%s/subdir", support_fuse_mountpoint (f)); + xmkdir (subdir_path, 01234); + } + + support_fuse_unmount (f); + return 0; +} + +#include From patchwork Thu Aug 22 10:40:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1975431 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HpQPd6qM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WqKVM3Scgz1ybW for ; Thu, 22 Aug 2024 20:40:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ACA45385DDE0 for ; Thu, 22 Aug 2024 10:40:41 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 8769C386D606 for ; Thu, 22 Aug 2024 10:40:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8769C386D606 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8769C386D606 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724323219; cv=none; b=Cu3wGD4NdVJHc+/KDorAvsN6a5Oyq9vmCxEefIbBB//8lUNwyikL5cp9OrdT4/KdgxZCjy5aStKb8TGiXtrJtWNPHvjfxVmAtSnfjTSJO1kTp5c4iMQBbNoASO8++B5b1YojKBrkgVppAaK0JLQCq2jw8PuTVx1QizwDMZ5YUT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724323219; c=relaxed/simple; bh=LRb/gigIQJee8BDDHr3qkwGcHjzupuiAj+Ga+/5/zdo=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=fkCue/YHUbqggAzIcMdLc2ijlS1PQQvxJSHaM8P3+yRQKGjn+mpvS8apKfM++KgYzvSJkpaIzj/edstXttZMLXpIRCa/x3t9QNwfDq8itiMUV1XfaNS2JJYUfaxIwk6i45N5yy0RIiaw4Cfe6ZrqXjhluy5Dz/1C6KUmhWG2WGY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724323215; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PN19kOaB4o7462j+EXiJPT0Gnnj6rVpdw+fbPAeLdmE=; b=HpQPd6qMfjXZDWvWl/Sw8iUvhEdiOxDKIzsqfdzkYZLY91GepSemx+mqCaUm9BZ3ktKhP5 HS5O+XUwLMfcXskesXr0MKyzK/aPVVffU/atC05Vh+hxmqMXe1EY3iAWDjzToDHrj7ogJ6 OKwGfSYCMcPf97xcnDG3DKFselMzBN8= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-191-W1zQazwkMVe8aG-73xmB7g-1; Thu, 22 Aug 2024 06:40:13 -0400 X-MC-Unique: W1zQazwkMVe8aG-73xmB7g-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D5DD41955D48 for ; Thu, 22 Aug 2024 10:40:12 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.22]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0AF0D19560AA for ; Thu, 22 Aug 2024 10:40:11 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 2/3] Linux: FUSE-based tests for mkstemp In-Reply-To: Message-ID: <9187dd636ddfb6ce777b3726eb2b63a03e7ec906.1724323005.git.fweimer@redhat.com> References: X-From-Line: 9187dd636ddfb6ce777b3726eb2b63a03e7ec906 Mon Sep 17 00:00:00 2001 Date: Thu, 22 Aug 2024 12:40:08 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org The tests check that O_EXCL is used properly, that 0600 is used as the mode, that the characters used are as expected, and that the distribution of names generated is reasonably random. The tests run very slowly on some kernel versions, so make them xtests. --- sysdeps/unix/sysv/linux/Makefile | 6 + .../sysv/linux/tst-mkstemp-fuse-parallel.c | 210 ++++++++++++++++++ sysdeps/unix/sysv/linux/tst-mkstemp-fuse.c | 199 +++++++++++++++++ 3 files changed, 415 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/tst-mkstemp-fuse-parallel.c create mode 100644 sysdeps/unix/sysv/linux/tst-mkstemp-fuse.c diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 7ef986bdd9..941e64d883 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -242,6 +242,12 @@ xtests += \ tst-process_madvise \ # xtests +# Long test times. +xtests += \ + tst-mkstemp-fuse \ + tst-mkstemp-fuse-parallel \ + # xtests + # For +depfiles in Makerules. extra-test-objs += \ tst-sysconf-iov_max-uapi.o \ diff --git a/sysdeps/unix/sysv/linux/tst-mkstemp-fuse-parallel.c b/sysdeps/unix/sysv/linux/tst-mkstemp-fuse-parallel.c new file mode 100644 index 0000000000..03c7689a7f --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-mkstemp-fuse-parallel.c @@ -0,0 +1,210 @@ +/* FUSE-based test for mkstemp. Parallel collision statistics. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Enough space to record 4 times the expected number of replies (62**3). + See the processes array in do_test. */ +enum { results_allocated = 4 * 62 * 62 * 62 + 1000 }; + +/* The thread will store the results there. */ +static uint64_t *results; + +/* Currently used part of the results array. */ +static uint64_t results_used; + +/* Fail with EEXIST, but record observed names. */ +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_read (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + if (inh->opcode != FUSE_LOOKUP || results_used >= results_allocated) + { + support_fuse_reply_error (f, EIO); + continue; + } + + char *name = support_fuse_cast (LOOKUP, inh); + TEST_COMPARE_BLOB (name, 3, "new", 3); + TEST_COMPARE (strlen (name), 9); + /* Extract 8 bytes of the name: 'w', the X replacements, and the + null terminator. */ + memcpy (&results[results_used], name + 2, 8); + ++results_used; + struct fuse_entry_out *out = support_fuse_prepare_entry (f, 2); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } +} + +/* Used to sort the results array, to find duplicates. */ +static int +results_sort (const void *a1, const void *b1) +{ + const uint64_t *a = a1; + const uint64_t *b = b1; + if (*a < *b) + return -1; + if (*a == *b) + return 0; + return 1; +} + +/* Number of occurrences of certain streak lengths. */ +static size_t streak_lengths[6]; + +/* Called for every encountered streak. */ +static inline void +report_streak (uint64_t current, size_t length) +{ + if (length > 1) + { + printf ("info: name \"ne%.8s\" repeats: %zu\n", + (char *) ¤t, length); + TEST_VERIFY_EXIT (length < array_length (streak_lengths)); + } + ++streak_lengths[length]; +} + +static int +do_test (void) +{ + support_become_root (); + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("mount namespaces not supported"); + + results = xmalloc (results_allocated * sizeof (*results)); + + pid_t processes[4]; + + struct shared + { + /* Used to synchronize the start of all subprocesses, to make it + more likely to expose concurrency-related bugs. */ + pthread_barrier_t barrier1; + pthread_barrier_t barrier2; + + /* Filled in after fork. */ + char mountpoint[PATH_MAX]; + }; + + /* Used to synchronize the start of all subprocesses, to make it + more likely to expose concurrency-related bugs. */ + struct shared *pshared = support_shared_allocate (sizeof (*pshared)); + { + pthread_barrierattr_t attr; + xpthread_barrierattr_init (&attr); + xpthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_SHARED); + xpthread_barrierattr_destroy (&attr); + xpthread_barrier_init (&pshared->barrier1, &attr, + array_length (processes) + 1); + xpthread_barrier_init (&pshared->barrier2, &attr, + array_length (processes) + 1); + xpthread_barrierattr_destroy (&attr); + } + + for (int i = 0; i < array_length (processes); ++i) + { + processes[i] = xfork (); + if (processes[i] == 0) + { + /* Wait for mountpoint initialization. */ + xpthread_barrier_wait (&pshared->barrier1); + char *path = xasprintf ("%s/newXXXXXX", pshared->mountpoint); + + /* Park this process until all processes have started. */ + xpthread_barrier_wait (&pshared->barrier2); + errno = 0; + TEST_COMPARE (mkstemp (path), -1); + TEST_COMPARE (errno, EEXIST); + free (path); + _exit (0); + } + } + + /* Do this after the forking, to minimize initialization inteference. */ + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + TEST_VERIFY (strlcpy (pshared->mountpoint, support_fuse_mountpoint (f), + sizeof (pshared->mountpoint)) + < sizeof (pshared->mountpoint)); + xpthread_barrier_wait (&pshared->barrier1); + + puts ("info: performing mkstemp calls"); + xpthread_barrier_wait (&pshared->barrier2); + + for (int i = 0; i < array_length (processes); ++i) + { + int status; + xwaitpid (processes[i], &status, 0); + TEST_COMPARE (status, 0); + } + + support_fuse_unmount (f); + + puts ("info: checking results"); + qsort (results, results_used, sizeof (*results), results_sort); + + size_t current = -1; + size_t streak = 0; + for (size_t i = 0; i < results_used; ++i) + if (results[i] == current) + ++streak; + else + { + report_streak (current, streak); + current = results[i]; + streak = 1; + } + report_streak (current, streak); + + puts ("info: repetition count distribution:"); + for (int i = 1; i < array_length (streak_lengths); ++i) + printf (" length %d: %zu\n", i, streak_lengths[i]); + /* Some arbitrary threshold, hopefully unlikely enough. */ + if (streak_lengths[2] > 20) + FAIL ("unexpectedly common repetition count 2: %zu", streak_lengths[2]); + if (streak_lengths[3] > 1) + FAIL ("unexpectedly common repetition count 3: %zu", streak_lengths[3]); + for (int i = 4; i < array_length (streak_lengths); ++i) + if (streak_lengths[i] > 0) + FAIL ("too many repeats of count %d: %zu", i, streak_lengths[i]); + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/tst-mkstemp-fuse.c b/sysdeps/unix/sysv/linux/tst-mkstemp-fuse.c new file mode 100644 index 0000000000..db3a6fedf1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-mkstemp-fuse.c @@ -0,0 +1,199 @@ +/* FUSE-based test for mkstemp. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Set to true in do_test to cause the first FUSE_CREATE attempt to fail. */ +static _Atomic bool simulate_creat_race; + +/* Basic tests with eventually successful creation. */ +static void +fuse_thread_basic (struct support_fuse *f, void *closure) +{ + char *previous_name = NULL; + int state = 0; + struct fuse_in_header *inh; + while ((inh = support_fuse_read (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + + switch (inh->opcode) + { + case FUSE_LOOKUP: + /* File does not exist initially. */ + TEST_COMPARE (inh->nodeid, 1); + if (simulate_creat_race) + { + if (state < 3) + ++state; + else + FAIL ("invalid state: %d", state); + } + else + { + TEST_COMPARE (state, 0); + state = 3; + } + support_fuse_reply_error (f, ENOENT); + break; + case FUSE_CREATE: + { + TEST_COMPARE (inh->nodeid, 1); + char *name; + struct fuse_create_in *p + = support_fuse_cast_name (CREATE, inh, &name); + /* Name follows after struct fuse_create_in. */ + TEST_COMPARE (p->flags & O_ACCMODE, O_RDWR); + TEST_VERIFY (p->flags & O_EXCL); + TEST_VERIFY (p->flags & O_CREAT); + TEST_COMPARE (p->mode & 07777, 0600); + TEST_VERIFY (S_ISREG (p->mode)); + TEST_COMPARE_BLOB (name, 3, "new", 3); + + if (state != 3 && simulate_creat_race) + { + ++state; + support_fuse_reply_error (f, EEXIST); + } + else + { + if (previous_name != NULL) + /* This test has a very small probability of failure + due to a harmless collision (one in 62**6 tests). */ + TEST_VERIFY (strcmp (name, previous_name) != 0); + TEST_COMPARE (state, 3); + ++state; + struct fuse_entry_out *entry; + struct fuse_open_out *open; + support_fuse_prepare_create (f, 2, &entry, &open); + entry->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + free (previous_name); + previous_name = xstrdup (name); + } + break; + case FUSE_FLUSH: + case FUSE_RELEASE: + TEST_COMPARE (state, 4); + TEST_COMPARE (inh->nodeid, 2); + support_fuse_reply_empty (f); + break; + default: + support_fuse_reply_error (f, EIO); + } + } + free (previous_name); +} + +/* Reply that all files exist. */ +static void +fuse_thread_eexist (struct support_fuse *f, void *closure) +{ + uint64_t counter = 0; + struct fuse_in_header *inh; + while ((inh = support_fuse_read (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + + switch (inh->opcode) + { + case FUSE_LOOKUP: + ++counter; + TEST_COMPARE (inh->nodeid, 1); + char *name = support_fuse_cast (LOOKUP, inh); + TEST_COMPARE_BLOB (name, 3, "new", 3); + TEST_COMPARE (strlen (name), 9); + for (int i = 3; i <= 8; ++i) + { + /* The glibc implementation uses letters and digits only. */ + char ch = name[i]; + TEST_VERIFY (('0' <= ch && ch <= '9') + || ('a' <= ch && ch <= 'z') + || ('A' <= ch && ch <= 'Z')); + } + struct fuse_entry_out out = + { + .nodeid = 2, + .attr = { + .mode = S_IFREG | 0600, + .ino = 2, + }, + }; + support_fuse_reply (f, &out, sizeof (out)); + break; + default: + support_fuse_reply_error (f, EIO); + } + } + TEST_VERIFY (counter >= 200000); +} + +static int +do_test (void) +{ + support_become_root (); + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("mount namespaces not supported"); + + for (int do_simulate_creat_race = 0; do_simulate_creat_race < 2; + ++do_simulate_creat_race) + { + simulate_creat_race = do_simulate_creat_race; + printf ("info: testing with simulate_creat_race == %d\n", + (int) simulate_creat_race); + struct support_fuse *f = support_fuse_mount (fuse_thread_basic, NULL); + char *path = xasprintf ("%s/newXXXXXX", support_fuse_mountpoint (f)); + int fd = mkstemp (path); + TEST_VERIFY (fd > 2); + xclose (fd); + free (path); + support_fuse_unmount (f); + } + + puts ("info: testing EEXIST failure case for mkstemp"); + { + struct support_fuse *f = support_fuse_mount (fuse_thread_eexist, NULL); + char *path = xasprintf ("%s/newXXXXXX", support_fuse_mountpoint (f)); + errno = 0; + TEST_COMPARE (mkstemp (path), -1); + TEST_COMPARE (errno, EEXIST); + free (path); + support_fuse_unmount (f); + } + + return 0; +} + +#include From patchwork Thu Aug 22 10:40:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1975432 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=MvUve0go; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WqKVg3Csjz1ybW for ; Thu, 22 Aug 2024 20:40:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADF52387100E for ; Thu, 22 Aug 2024 10:40:57 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 56ADB3870C24 for ; Thu, 22 Aug 2024 10:40:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56ADB3870C24 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 56ADB3870C24 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724323223; cv=none; b=Z2Cf9OL5vAywDUSWdRNrPbcXs/zMcpRAsWQhgzqNbGNUOPTgD8Wbg8D6g3XasZbTBRNT5nByu1oWU2ggJc4BY2XyEoKOYbAyieT9O8mjyyG+MDbhUl4v/Y/xDpNjkjkDAvmvkM9qGlyn730lh+j9GFz9zXnrBwBf+J9vALLsvJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724323223; c=relaxed/simple; bh=LtCqmHfD7G9VHIjJA9IuMezVRYWFkT9IVf3KEedsmqI=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=xITP+AT2k9MDQj9TQkA1mdDgx2OTckbKJaFDZ54VlhhehBoWDN5nibYkiVNvGbVu5gpMSnlcAwNlvYQyNb5zciKpokB2fOo6M9TXH9AKkUmIIFTY6JMFM5rmDbN1qIeGUrReffECP/uVTmj5MNtBPkoW2OxMnv7w4WrY+uhKfIQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724323221; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gb+bwGMntMHWw7yxlf/ng6qeDRQy6qo76SW493Em6XU=; b=MvUve0gohdNSVAFsrLVWQhE5w7kxR3IWNlx5GL+b+82Apvmvl8tcNrwzefxUIDBJtOTKOU rvblJCzoRs5IJqatYQgAegNvIR5D/5hhhofRVdGdb9ohDAsMEGn3BGJUxC7XLcJuVyXYuE 9Le6JqMXHUL2t6NhYxNovnJTd04+XaU= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-455-PgqCjDSRP1O2G7Ka4PrL1g-1; Thu, 22 Aug 2024 06:40:20 -0400 X-MC-Unique: PgqCjDSRP1O2G7Ka4PrL1g-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3D6961955BF2 for ; Thu, 22 Aug 2024 10:40:19 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.22]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 47D801955DD6 for ; Thu, 22 Aug 2024 10:40:17 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 3/3] Linux: Add FUSE-based test for fchmod In-Reply-To: Message-ID: <5b82db7afebbbc0f0fbe6b0f2931b63c9f61aea9.1724323005.git.fweimer@redhat.com> References: X-From-Line: 5b82db7afebbbc0f0fbe6b0f2931b63c9f61aea9 Mon Sep 17 00:00:00 2001 Date: Thu, 22 Aug 2024 12:40:15 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Test all mode arguments, and that extra bits are ignored as required by POSIX. --- sysdeps/unix/sysv/linux/Makefile | 1 + sysdeps/unix/sysv/linux/tst-fchmod-fuse.c | 124 ++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/tst-fchmod-fuse.c diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 941e64d883..d762f9b476 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -617,6 +617,7 @@ sysdep_headers += \ tests += \ tst-fallocate \ tst-fallocate64 \ + tst-fchmod-fuse \ tst-getcwd-smallbuff \ tst-o_path-locks \ # tests diff --git a/sysdeps/unix/sysv/linux/tst-fchmod-fuse.c b/sysdeps/unix/sysv/linux/tst-fchmod-fuse.c new file mode 100644 index 0000000000..2a377ccd62 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-fchmod-fuse.c @@ -0,0 +1,124 @@ +/* FUSE-based test for fchmod. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +/* Set from do_test to indicate the expected incoming mode change request. */ +static _Atomic int expected_mode; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_read (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_LOOKUP: + { + char *name = support_fuse_cast (LOOKUP, inh); + TEST_COMPARE_STRING (name, "file"); + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, 2); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + break; + case FUSE_OPEN: + { + TEST_COMPARE (inh->nodeid, 2); + struct fuse_open_in *p = support_fuse_cast (OPEN, inh); + TEST_COMPARE (p->flags & O_ACCMODE, O_RDWR); + struct fuse_open_out out = { 0, }; + support_fuse_reply (f, &out, sizeof (out)); + } + break; + case FUSE_SETATTR: + { + TEST_COMPARE (inh->nodeid, 2); + struct fuse_setattr_in *p = support_fuse_cast (SETATTR, inh); + TEST_COMPARE (p->valid , FATTR_MODE); + TEST_COMPARE (p->mode, S_IFREG | expected_mode); + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFREG | p->mode; + support_fuse_reply_prepared (f); + } + break; + case FUSE_FLUSH: + support_fuse_reply_empty (f); + break; + default: + support_fuse_reply_error (f, EIO); + } + } +} + +/* Test all mode values with the specified extra bits. */ +static void +test_with_bits (int fd, unsigned int extra_bits) +{ + for (int do_mode = 0; do_mode <= 07777; ++do_mode) + { + expected_mode = do_mode; + TEST_COMPARE (fchmod (fd, extra_bits | do_mode), 0); + } +} + +static int +do_test (void) +{ + support_become_root (); + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("mount namespaces not supported"); + + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + char *path = xasprintf ("%s/file", support_fuse_mountpoint (f)); + int fd = xopen (path, O_RDWR, 0600); + free (path); + + test_with_bits (fd, 0); + /* POSIX requires that the extra bits are ignored. */ + test_with_bits (fd, S_IFREG); + test_with_bits (fd, S_IFDIR); + test_with_bits (fd, ~07777); + + /* Check that AT_FDCWD produces the expected error (and not + something else from fchmodat). */ + errno = 0; + TEST_COMPARE (fchmod (AT_FDCWD, 0), -1); + TEST_COMPARE (errno, EBADF); + + xclose (fd); + support_fuse_unmount (f); + + return 0; +} + +#include