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. */