From patchwork Mon Jan 30 12:52:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 63911 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 67D8E3858407 for ; Mon, 30 Jan 2023 12:53:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67D8E3858407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675083228; bh=onXoadCm2muk7Few/Y08HHZs4ki93txlKdmLX+SeAQc=; 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=nPAek8K6+UglZ7cAhJ1RtfxxbwqF8TJOf6zJ4FpTAQNAEACeq97ffFnWpiREWPh// Z/+Y1y9c3lPOLXm9F/d7SWI22bp8/GqSVN4ynjxCH+D37RLw7KERuJRW8mDWhMUaB6 Uu15aGBDYY2qqfmi/w9Kbm2NBG2FFrPSP81RQlY8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 36BEC3858D1E for ; Mon, 30 Jan 2023 12:52:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 36BEC3858D1E Received: by mail-lf1-x131.google.com with SMTP id d30so18802360lfv.8 for ; Mon, 30 Jan 2023 04:52:46 -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=onXoadCm2muk7Few/Y08HHZs4ki93txlKdmLX+SeAQc=; b=LwDvSSfXDch22ANEyz63NSaKmn8rxvZe+Zi2m2fmSNSp6t0DgMJ1Ryrr34aLpyrsCl uv6rhg8LOIvQeINbxRhFSfAWngu1Rji/A5j7iMSkPOE19cRI049fYkhKFcccnc6aLvIe blfwUjg0PbI1YGdu4t2gMm0sz6FxSJY4C4cMRWBHdPgJxCfO3hpwq9n7vm9XnRz78k5F 35qkduVpoxPmI3h23gCEEdqVus5mr/RRDwxoc9FRS2FDtOcvFYs1hE+ct5xghfkj6M0K ficoz1m6MBXavleKX4E3R51jcOMpEl1EbJ2sks7VUQGHbKWW1/DIuNLIQSMIqeuNauUZ JZGw== X-Gm-Message-State: AFqh2kpIWBQmTEg+Fv4X1SkNA+INrbFgqsM1mHRJlYffPJfaIxvUYutd W2QWgwksnTqkdYzDOPBfAT8= X-Google-Smtp-Source: AMrXdXurCLGggJrys+yeocEPUSKjClBJMIzicI3G1IF4svHu4C4nI5Yc2SV8sOJsBSI+L9plXC9meQ== X-Received: by 2002:a05:6512:1086:b0:4d4:d7fb:d13 with SMTP id j6-20020a056512108600b004d4d7fb0d13mr16336497lfg.35.1675083164793; Mon, 30 Jan 2023 04:52:44 -0800 (PST) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id m4-20020a05651202e400b004d35fea344dsm847233lfq.202.2023.01.30.04.52.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 04:52:44 -0800 (PST) To: bug-hurd@gnu.org, libc-alpha@sourceware.org, samuel.thibault@gnu.org Cc: Sergey Bugaev Subject: [PATCH v2 1/3] hurd: Consolidate file_name_lookup implementation Date: Mon, 30 Jan 2023 15:52:14 +0300 Message-Id: <20230130125216.6254-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230130095907.mlvp3pbmtarkhhql@begin> References: <20230130095907.mlvp3pbmtarkhhql@begin> 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, 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. Signed-off-by: Sergey Bugaev --- 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 49eac443..3cfe444f 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 6f30a067..25dab5a1 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 Jan 30 12:52:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 63910 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 7E4EC3858410 for ; Mon, 30 Jan 2023 12:53:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E4EC3858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675083192; bh=cuFAcM0d5OH5hYW/qtr4y+shyz1WWVC9j3aBE+K5zwc=; 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=ELk8VuUyRoQf4OP1A/GZG9ItFR/Mm9ekkrfOXzEj+r3zR7aSv9lS7aBEeDk/2eosJ n3P8397L4B1Ysn9ohNWmP9cOfc4fpv5OobT7b9IxiKHpWyuDW1gGMrScR2f7k3TIv9 lUMZ6wcDfA0qa/CJ1eqlFJMa+Q3s21wQJKfux9qo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id A36BF3858D28 for ; Mon, 30 Jan 2023 12:52:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A36BF3858D28 Received: by mail-lf1-x12c.google.com with SMTP id v17so12933697lfd.7 for ; Mon, 30 Jan 2023 04:52:47 -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=cuFAcM0d5OH5hYW/qtr4y+shyz1WWVC9j3aBE+K5zwc=; b=D6VEQST3Id8H7gBMyZaoVvrGFEB931X28KLr1rTl2YyTLY1RS6uu1Lu0hSJI+KQYMY ZwPmbqVakszBFmsRM/aG1ihVxiMiogzJhkTkjUwQ5lmF5P+GQqGs3qODjCyTW2fzDbGk jkfPx+HO7MsP5ptazo+GN6YkuMMDaYKgqeIf4Ss1lxdMXmPEjdBHCqbjqD2xa+BZsdk3 BVHtkZctsth+qQTlWxNoMefKLbnoQ89TmLttu2tflJcZDIvpBdPvigxoIquvne0TRCfa VlUxIrhpmfUzTAB64EVM7s2FAsGriz+x0c1NyryzSAciDtXg68iN9Bh7nEcO3uwv0JGT Yj0A== X-Gm-Message-State: AO0yUKWtIB9X+5+BgkTl4C9dvfDIm0/nfu1tAs8KdtcKMF826SyM3nLX 3YvB9Yip9clsnr7ix9gX8jdBbz+ru0c= X-Google-Smtp-Source: AK7set/6LhUTFAHJwPyzp6SCGwBDR7XGoHQ6ZA+HTHs1wzfwK9frG2mgCPvNKdiTkAe6I2rmDwmweQ== X-Received: by 2002:a05:6512:3446:b0:4d8:6d13:b07b with SMTP id j6-20020a056512344600b004d86d13b07bmr664380lfr.22.1675083166002; Mon, 30 Jan 2023 04:52:46 -0800 (PST) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id m4-20020a05651202e400b004d35fea344dsm847233lfq.202.2023.01.30.04.52.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 04:52:45 -0800 (PST) To: bug-hurd@gnu.org, libc-alpha@sourceware.org, samuel.thibault@gnu.org Cc: Sergey Bugaev Subject: [PATCH v2 2/3] hurd: Implement O_TMPFILE Date: Mon, 30 Jan 2023 15:52:15 +0300 Message-Id: <20230130125216.6254-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230130095907.mlvp3pbmtarkhhql@begin> References: <20230130095907.mlvp3pbmtarkhhql@begin> 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, 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. Signed-off-by: Sergey Bugaev --- hurd/lookup-at.c | 21 +++++++++++++++++++++ sysdeps/mach/hurd/bits/fcntl.h | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/hurd/lookup-at.c b/hurd/lookup-at.c index 25dab5a1..88c83779 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) + flags = O_DIRECTORY; + if (fd == AT_FDCWD || file_name[0] == '/') { err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0, @@ -90,6 +95,22 @@ __file_name_lookup_at (int fd, int at_flags, } } + if (orig_flags & O_TMPFILE) + { + /* What we have looked up is not the file itself, but actually + the directory to create the file in. Do that now. */ + file_t dir = result; + + err = __dir_mkfile (dir, orig_flags & ~(O_TMPFILE | O_DIRECTORY), + 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 970f79b8..c24a819e 100644 --- a/sysdeps/mach/hurd/bits/fcntl.h +++ b/sysdeps/mach/hurd/bits/fcntl.h @@ -123,6 +123,10 @@ # define O_CLOEXEC 0x00400000 /* Set FD_CLOEXEC. */ #endif +#ifdef __USE_GNU +# define O_TMPFILE 0x00800000 /* Make a new unnamed file. */ +#endif + /* Controlling terminal flags. These are understood only by `open', and are not preserved once the file has been opened. */ From patchwork Mon Jan 30 12:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 63912 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 641B83858C2C for ; Mon, 30 Jan 2023 12:54:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 641B83858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675083269; bh=TbIU87j+4LnwfJuq6FFSKQBAndPRIeaJpY8k/nFX9Ug=; 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=SKOY3uVuVPjDhwMvMOZppLExm7x/o0NZPHFxgEGjTxftULR0saBn/gR4O4drituGC kNrJVWecXqh78brmLPj2PCtD97UThalL7RBTttYVQiq8Vj6t5+ED/s+NdIfrcCPZ0B 5gU50mk4v8NnIuXVhhi8sHB20AmEw7hE4IfEeKyw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id E75213858C1F for ; Mon, 30 Jan 2023 12:52:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E75213858C1F Received: by mail-lf1-x135.google.com with SMTP id cf42so18861113lfb.1 for ; Mon, 30 Jan 2023 04:52:48 -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=TbIU87j+4LnwfJuq6FFSKQBAndPRIeaJpY8k/nFX9Ug=; b=qL5Zxm1iW1lpt98AzSPafTuOIGTCiW24EwUZDDFGkHWrcXUlKDwlXnk+z3jQcoMPIz gAiY/yZbG6vrtMKI9X5oHbJN0GA0277tYKDp2Y8dLC3S4/Lb2N1OnYUQbAUqmMmobeVy 6sct8xdrk3bS2UpE/MMzR5OOmsilZiCQZdE0tByy9HEznmaAufr7ytqPSDjm6NrOV/Te PougGbkZLnqN/dBcQM+TrefpnKIJM8LprmhR7dxt5Xa1/a4BdgqOap1bqdvyxMizfuux MoES0fa6yvXcv6W3CaHd0i5k2Eu/rawWbI+aJtjIrAT4hGtxcRllnk4QfLAt4pAls1s5 joiA== X-Gm-Message-State: AFqh2krQDNU2wYua29QkAXi7Zn0CaF/Ez7aVVTrEyBRfb7vT8zmOYeNb s8GLzoirwx6iry9DMWu8O0c= X-Google-Smtp-Source: AMrXdXuHTdWcfIYdbmDt04G0CBnGOI7RtocijWsEiRvISoXlnueBUpPZ/obP2e2Xjj1aZ6dqXr2htQ== X-Received: by 2002:a05:6512:358a:b0:4cb:1189:285c with SMTP id m10-20020a056512358a00b004cb1189285cmr11541975lfr.10.1675083167341; Mon, 30 Jan 2023 04:52:47 -0800 (PST) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id m4-20020a05651202e400b004d35fea344dsm847233lfq.202.2023.01.30.04.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Jan 2023 04:52:46 -0800 (PST) To: bug-hurd@gnu.org, libc-alpha@sourceware.org, samuel.thibault@gnu.org Cc: Sergey Bugaev Subject: [PATCH v2 3/3] hurd: Implement SHM_ANON Date: Mon, 30 Jan 2023 15:52:16 +0300 Message-Id: <20230130125216.6254-4-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230130095907.mlvp3pbmtarkhhql@begin> References: <20230130095907.mlvp3pbmtarkhhql@begin> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 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. Signed-off-by: Sergey Bugaev --- 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 47e2355d..86d9fd8e 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 48970bbc..6c1f4d60 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 */