From patchwork Mon Dec 12 11:46:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 61778 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 39B243834856 for ; Mon, 12 Dec 2022 11:47:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 39B243834856 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670845645; bh=sKotTV94o2zJe7R8Dd4eYa0Ppk2Mo6/fMT64fDMc9nQ=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=CEW0A5gyhOQO+C1ZvCVau1U4UF53yQaARKa40MzWxqjkZyN1tUZDn9J+FC8lJN5pK lAlpXPOdIOR4ZPI2Avx8bgyLyzhMpGevEz9F5SzDqWtMrhXRc3C9KDquBDiCU4+P3l VuxqdY9j+v6P7YupEr3T/tTKcfxrps1llG8I77E4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 1DB62385842E for ; Mon, 12 Dec 2022 11:47:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1DB62385842E Received: by mail-lf1-x12b.google.com with SMTP id 1so18101384lfz.4 for ; Mon, 12 Dec 2022 03:47:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sKotTV94o2zJe7R8Dd4eYa0Ppk2Mo6/fMT64fDMc9nQ=; b=kVt1alp6Jd3Vz1WOa9CIsPhAjek5D8Tx5ffpLsEjcIaYQ/A+xp4UAvBoUjHuYijInG 00Xkg2tL0JpjGCv0Qw6f5YlvTETt8Qb5AXnmkraIG9ODCEuK79JsFJ0mX5pNpV+j+zlb plBPVORT6vsMh5USnNk3MGV30krYJF4uOWFc3d4Oyt6mMm8UiG1XYEiyiDdpmDQvPsU6 s2ol0Y71EkeS8gny/g3fY+SXGQepAZ/QyDA8nUyZIlIRKFJzh7IGkn08holgDH46NMre rEK7At8JFzd4n1n/cNr1bjWb9h5buTjW2secZKd0Eti36CrH36ZvbhHgeOkYCWCe7bdD MFxg== X-Gm-Message-State: ANoB5pkIHfXFg4vI1YVXcrjpssdz2EyY2ZFxBn4WtMD37kiEfc9KH3pO KrZHtShU+S0yEFn3iaWm2Za6raB0AK1jnA== X-Google-Smtp-Source: AA0mqf7pyvQZSXqAwhZi02/sDImv/XzHXQJVSmrCa+XXkgzJXC2vGfTZ+zdcxGgzc8cyeY9dRlAScA== X-Received: by 2002:a05:6512:3f1d:b0:4a4:68b7:d621 with SMTP id y29-20020a0565123f1d00b004a468b7d621mr5262374lfa.8.1670845619588; Mon, 12 Dec 2022 03:46:59 -0800 (PST) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id g42-20020a0565123baa00b00497b198987bsm1606514lfv.26.2022.12.12.03.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 03:46:59 -0800 (PST) To: bug-hurd@gnu.org, libc-alpha@sourceware.org, samuel.thibault@gnu.org Cc: Sergey Bugaev Subject: [RFC PATCH 1/3] hurd: Consolidate file_name_lookup implementation Date: Mon, 12 Dec 2022 14:46:34 +0300 Message-Id: <20221212114636.74222-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212114636.74222-1-bugaevc@gmail.com> References: <20221212114636.74222-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Instead of __file_name_lookup_at delegating to __file_name_lookup in simple cases, make __file_name_lookup_at deal with both cases, and have __file_name_lookup simply wrap __file_name_lookup_at. --- hurd/hurdlookup.c | 10 ++-------- hurd/lookup-at.c | 51 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/hurd/hurdlookup.c b/hurd/hurdlookup.c index 7babac7d..08166a2c 100644 --- a/hurd/hurdlookup.c +++ b/hurd/hurdlookup.c @@ -16,6 +16,7 @@ . */ #include +#include #include #include #include @@ -220,14 +221,7 @@ weak_alias (__hurd_directory_name_split, hurd_directory_name_split) file_t __file_name_lookup (const char *file_name, int flags, mode_t mode) { - error_t err; - file_t result; - - err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0, - file_name, flags, mode & ~_hurd_umask, - &result); - - return err ? (__hurd_fail (err), MACH_PORT_NULL) : result; + return __file_name_lookup_at (AT_FDCWD, 0, file_name, flags, mode); } weak_alias (__file_name_lookup, file_name_lookup) diff --git a/hurd/lookup-at.c b/hurd/lookup-at.c index 0c247a64..2b84dfd6 100644 --- a/hurd/lookup-at.c +++ b/hurd/lookup-at.c @@ -36,9 +36,6 @@ __file_name_lookup_at (int fd, int at_flags, if (err) return (__hurd_fail (err), MACH_PORT_NULL); - if (fd == AT_FDCWD || file_name[0] == '/') - return __file_name_lookup (file_name, flags, mode); - if (empty != 0 && file_name[0] == '\0') { enum retry_type doretry; @@ -56,22 +53,44 @@ __file_name_lookup_at (int fd, int at_flags, return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result; } - file_t startdir; - error_t use_init_port (int which, error_t (*operate) (mach_port_t)) + if (fd == AT_FDCWD || file_name[0] == '/') { - return (which == INIT_PORT_CWDIR ? (*operate) (startdir) - : _hurd_ports_use (which, operate)); + err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0, + file_name, flags, mode & ~_hurd_umask, + &result); + if (err) + { + __hurd_fail (err); + return MACH_PORT_NULL; + } + } + else + { + file_t startdir; + /* We need to look the file up relative to the given directory (and + not our cwd). For this to work, we supply our own wrapper for + _hurd_ports_use, which replaces cwd with our startdir. */ + error_t use_init_port (int which, error_t (*operate) (mach_port_t)) + { + return (which == INIT_PORT_CWDIR ? (*operate) (startdir) + : _hurd_ports_use (which, operate)); + } + + err = HURD_DPORT_USE (fd, (startdir = port, + __hurd_file_name_lookup (&use_init_port, + &__getdport, NULL, + file_name, + flags, + mode & ~_hurd_umask, + &result))); + if (err) + { + __hurd_dfail (fd, err); + return MACH_PORT_NULL; + } } - err = HURD_DPORT_USE (fd, (startdir = port, - __hurd_file_name_lookup (&use_init_port, - &__getdport, NULL, - file_name, - flags, - mode & ~_hurd_umask, - &result))); - - return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result; + return result; } file_t From patchwork Mon Dec 12 11:46:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 61779 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3DBC7388FFF1 for ; Mon, 12 Dec 2022 11:47:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DBC7388FFF1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670845653; bh=FT+pAgc2vl5cQ3XwLC+d/mlzPv3rN5gq0MEu4HsT+Y4=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=fhpM33ypRy3HBHj7OS8wctFh6ydT4FAtbzU97cEwTbnCIdKQxHmkL2Lunb5B+WvTz sbPW7Py9UpX4CSrI5+LHMWbSKyObOhrj7h2OSwgL2O9j75tZcY032gHyp2eVVCIGlt K6ZfTROt0y5A0PEHjDMdLSuTr8yBwSV7y34S3+AU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id 2608B385F3E1 for ; Mon, 12 Dec 2022 11:47:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2608B385F3E1 Received: by mail-lf1-x130.google.com with SMTP id x28so18088811lfn.6 for ; Mon, 12 Dec 2022 03:47:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FT+pAgc2vl5cQ3XwLC+d/mlzPv3rN5gq0MEu4HsT+Y4=; b=aAtu7aQ9J333la8+TQTCD8Pij5l5NRACM4JKgJrZYKT9U5zkzLU7DTQhcoMctgOcTJ dyOeAbzzuHkDqU3Vh3uZkc1tKMWAFTj08kEshZqBoAtoDobo+OesuSpShPEKQV5rZmjl Cq3GbzIVF8PHO+/wA6TQ5e8lRN+iwhnH+BGjU3al2tfN5mPJdpJCHrfeAZdA+fDeLp6w ThTmLJMjux/vmY/wMx6wItQEAH+6W3ROrx0DSbpwNkCbZbHiqCztZi33I49ZRyLAsh4V 8ru3ez9Ky4RzuJc0qxaX/JcDsFuLLNS/4DKEbr8+V66XaBuhTxnVQLudsQwOaQo1+ufL qStw== X-Gm-Message-State: ANoB5pmlVBYAwOZ6c9Pb+0lBtRf/HVUbWAMl2OHDMMpu+R+cQpuorqfC 1/yrOnd8Hfzi3smjQYqLfA0= X-Google-Smtp-Source: AA0mqf50eFd3JRoZgNb67xfDc7dPmsbeshhPlsxo9qbth+Sh+ao/wwqKor87RAmABvV0b6fJuFmlYA== X-Received: by 2002:a05:6512:298c:b0:4b5:b22e:9653 with SMTP id du12-20020a056512298c00b004b5b22e9653mr3269591lfb.18.1670845620570; Mon, 12 Dec 2022 03:47:00 -0800 (PST) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id g42-20020a0565123baa00b00497b198987bsm1606514lfv.26.2022.12.12.03.46.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 03:47:00 -0800 (PST) To: bug-hurd@gnu.org, libc-alpha@sourceware.org, samuel.thibault@gnu.org Cc: Sergey Bugaev Subject: [RFC PATCH 2/3] hurd: Implement O_TMPFILE Date: Mon, 12 Dec 2022 14:46:35 +0300 Message-Id: <20221212114636.74222-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212114636.74222-1-bugaevc@gmail.com> References: <20221212114636.74222-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This is a flag that causes open () to create a new, unnamed file in the same filesystem as the given directory. The file descriptor can be simply used in the creating process as a temporary file, or shared with children processes via fork (), or sent over a Unix socket. The file can be left anonymous, in which case it will be deleted from the backing file system once all copies of the file descriptor are closed, or given a permanent name with a linkat () call, such as the following: int fd = open ("/tmp", O_TMPFILE | O_RDWR, 0700); /* Do something with the file... */ linkat (fd, "", AT_FDCWD, "/tmp/filename", AT_EMPTY_PATH); In between creating the file and linking it to the file system, it is possible to set the file content, mode, ownership, author, and other attributes, so that the file visibly appears in the file system (perhaps replacing another file) atomically, with all of its attributes already set up. The Hurd support for O_TMPFILE directly exposes the dir_mkfile RPC to user programs. Previously, dir_mkfile was used by glibc internally, in particular for implementing tmpfile (), but not exposed to user programs through a Unix-level API. O_TMPFILE was initially introduced by Linux. This implementation is intended to be compatible with the Linux implementation, except that the O_EXCL flag is not given the special meaning when used together with O_TMPFILE, unlike on Linux. --- hurd/lookup-at.c | 20 ++++++++++++++++++++ sysdeps/mach/hurd/bits/fcntl.h | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/hurd/lookup-at.c b/hurd/lookup-at.c index 2b84dfd6..42ac8c42 100644 --- a/hurd/lookup-at.c +++ b/hurd/lookup-at.c @@ -29,6 +29,7 @@ __file_name_lookup_at (int fd, int at_flags, error_t err; file_t result; int empty = at_flags & AT_EMPTY_PATH; + int orig_flags; at_flags &= ~AT_EMPTY_PATH; @@ -53,6 +54,10 @@ __file_name_lookup_at (int fd, int at_flags, return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result; } + orig_flags = flags; + if ((flags & __O_TMPFILE) == __O_TMPFILE) + flags = O_DIRECTORY; + if (fd == AT_FDCWD || file_name[0] == '/') { err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0, @@ -90,6 +95,21 @@ __file_name_lookup_at (int fd, int at_flags, } } + if ((orig_flags & __O_TMPFILE) == __O_TMPFILE) + { + /* What we have looked up is not the file iteself, but actually + the directory to create the file in. Do that now. */ + file_t dir = result; + + err = __dir_mkfile (dir, orig_flags & ~O_TMPFILE, mode, &result); + __mach_port_deallocate (__mach_task_self (), dir); + if (err) + { + __hurd_fail (err); + return MACH_PORT_NULL; + } + } + return result; } diff --git a/sysdeps/mach/hurd/bits/fcntl.h b/sysdeps/mach/hurd/bits/fcntl.h index 17dcb384..b898a0c5 100644 --- a/sysdeps/mach/hurd/bits/fcntl.h +++ b/sysdeps/mach/hurd/bits/fcntl.h @@ -123,6 +123,11 @@ # define O_CLOEXEC 0x00400000 /* Set FD_CLOEXEC. */ #endif +#ifdef __USE_GNU +# define __O_TMPFILE 0x00800000 /* Make a new unnamed file. */ +# define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) +#endif + /* Controlling terminal flags. These are understood only by `open', and are not preserved once the file has been opened. */ From patchwork Mon Dec 12 11:46:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 61780 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 99BD9397089E for ; Mon, 12 Dec 2022 11:48:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99BD9397089E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670845697; bh=BYwj4St3AD/N5kylbm1IoK54VFllSlMgmB88IU3J6k0=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=csXI6OzTateAYEw2c+EQ3xzIdwf/cB2XUdoicKOrGiGxrWdYNMmJNbQpXr2nC22Zd Rg7qApRMNINLxo29Yj1ROAhVfwCz6AOWreObtrfT1DutOA8KRPCBxsgcE2RGeCtFhN Wr4KwoYcMukNkFjNTWKSNOGKG8arYdhiK4sPt6nQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id E0C75385F3E2 for ; Mon, 12 Dec 2022 11:47:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E0C75385F3E2 Received: by mail-lj1-x229.google.com with SMTP id g14so8240249ljh.10 for ; Mon, 12 Dec 2022 03:47:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BYwj4St3AD/N5kylbm1IoK54VFllSlMgmB88IU3J6k0=; b=RQJ3PK291uywDTwBlfis/WGk6yShrl++oo+GZZpU6tSxmg1J05d964IpmcxvyMfOyU rJfJuWg0MCnEfi4deuR9PU+5fZuCJlZu1ZclcMDB1EtSQG57eNUToc+X2TZqPx8GUbG2 QnzMzzKWlCGum3Bv6sdrAgBHvAi05L5On9c+9Pr1EsX2tpR7Eew4ErADQg2AuhzKrq69 7h4waLum7dLx7SLqW9IiPwA09dhaCl59TRWwvNSNkA8bYqQx8XaYe1CwSgoau/k6Yql5 rX0PqgJ+1OhrBPaVb7JEIOTzSx8VN9og+2BruDxgf2kEVHZjRK/gm1mewOBEhbIqTIzs H+dg== X-Gm-Message-State: ANoB5pnNO6fEfxqIh1rBo2Sc0tn0DvWxk3sQHjpGfBRuo8MPJo+/M4sj HtRwJDyfYY/7FEJukZw8xog= X-Google-Smtp-Source: AA0mqf5HXFloXZARnFYmn/CnjBXAwsY2lb+DQsU9NS/Romzl1ulPmbi9oDWMw4I46R1j+5fwrM5xSA== X-Received: by 2002:a05:651c:1608:b0:277:c03:72cd with SMTP id f8-20020a05651c160800b002770c0372cdmr3607430ljq.28.1670845621525; Mon, 12 Dec 2022 03:47:01 -0800 (PST) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id g42-20020a0565123baa00b00497b198987bsm1606514lfv.26.2022.12.12.03.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 03:47:01 -0800 (PST) To: bug-hurd@gnu.org, libc-alpha@sourceware.org, samuel.thibault@gnu.org Cc: Sergey Bugaev Subject: [RFC PATCH 3/3] hurd: Implement SHM_ANON Date: Mon, 12 Dec 2022 14:46:36 +0300 Message-Id: <20221212114636.74222-4-bugaevc@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212114636.74222-1-bugaevc@gmail.com> References: <20221212114636.74222-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This adds a special SHM_ANON value that can be passed into shm_open () in place of a name. When called in this way, shm_open () will create a new anonymous shared memory file. The file will be created in the same way that other shared memory files are created (i.e., under /dev/shm/), except that it is not given a name and therefore cannot be reached from the file system, nor by other calls to shm_open (). This is accomplished by utilizing O_TMPFILE. This is intended to be compatible with FreeBSD's API of the same name. --- posix/shm-directory.c | 25 +++++++++++++++++++++---- rt/shm_open.c | 5 +++++ sysdeps/mach/hurd/bits/mman_ext.h | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 sysdeps/mach/hurd/bits/mman_ext.h diff --git a/posix/shm-directory.c b/posix/shm-directory.c index 76ea825b..12cc683e 100644 --- a/posix/shm-directory.c +++ b/posix/shm-directory.c @@ -23,17 +23,34 @@ #include #include #include +#include +#include int __shm_get_name (struct shmdir_name *result, const char *name, bool sem_prefix) { + struct alloc_buffer buffer; + size_t namelen; + + buffer = alloc_buffer_create (result->name, sizeof (result->name)); + alloc_buffer_copy_bytes (&buffer, SHMDIR, strlen (SHMDIR)); + +#if defined (SHM_ANON) && defined (O_TMPFILE) + if (name == SHM_ANON) + { + /* For SHM_ANON, we want shm_open () to pass O_TMPFILE to open (), + with SHMDIR itself as the path. So, leave it at that. */ + alloc_buffer_add_byte (&buffer, 0); + if (alloc_buffer_has_failed (&buffer)) + return -1; + return 0; + } +#endif + while (name[0] == '/') ++name; - size_t namelen = strlen (name); + namelen = strlen (name); - struct alloc_buffer buffer - = alloc_buffer_create (result->name, sizeof (result->name)); - alloc_buffer_copy_bytes (&buffer, SHMDIR, strlen (SHMDIR)); if (sem_prefix) alloc_buffer_copy_bytes (&buffer, "sem.", strlen ("sem.")); alloc_buffer_copy_bytes (&buffer, name, namelen + 1); diff --git a/rt/shm_open.c b/rt/shm_open.c index 11cb7cc7..6524776e 100644 --- a/rt/shm_open.c +++ b/rt/shm_open.c @@ -23,6 +23,7 @@ #include #include #include +#include /* Open shared memory object. */ int @@ -36,6 +37,10 @@ __shm_open (const char *name, int oflag, mode_t mode) } oflag |= O_NOFOLLOW | O_CLOEXEC; +#if defined (SHM_ANON) && defined (O_TMPFILE) + if (name == SHM_ANON) + oflag |= O_TMPFILE; +#endif int fd = __open64_nocancel (dirname.name, oflag, mode); if (fd == -1 && __glibc_unlikely (errno == EISDIR)) diff --git a/sysdeps/mach/hurd/bits/mman_ext.h b/sysdeps/mach/hurd/bits/mman_ext.h new file mode 100644 index 00000000..f022826e --- /dev/null +++ b/sysdeps/mach/hurd/bits/mman_ext.h @@ -0,0 +1,25 @@ +/* System-specific extensions of , Hurd version. + Copyright (C) 2022 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 + . */ + +#ifndef _SYS_MMAN_H +# error "Never include directly; use instead." +#endif + +#ifdef __USE_GNU +# define SHM_ANON ((const char *) 1) +#endif /* __USE_GNU */