From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x130.google.com with SMTP id x28so18088811lfn.6 for ; Mon, 12 Dec 2022 03:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FT+pAgc2vl5cQ3XwLC+d/mlzPv3rN5gq0MEu4HsT+Y4=; b=ARRIFyrljbqzXcV9pl7JhCCC2e/nOBSkiTU9OYAw4lkapFI2FSuTW8gLfM+1aWbFXc jnQNito0cMyMWfuNjNAI00zubFwIZyRSMfmjjkwrpLDADD2BUB5CoiZsa5gtG5qUYoXT eRO2qlASX7ThMJLRfmPRJPD8eqWCm9bdjn25Rd+2/M95LzU1tG1PgAtzYGMZSW87j65Y FpKW7w5jJR8/iVauOMiWBKFw/56d/VtAtDSuMupFwk9km6E6TOfGinJ2qihwSMvX1+MT hvmDiIfLJweHgNd9zMZxlF02jZuHSunjPTbRrylIkz+dv+HE7Xp+uJWOIVm694SIzV4D j3ww== 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) From: Sergey Bugaev 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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. */ -- 2.38.1