From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1792) id C30B93858D3C; Wed, 1 Feb 2023 22:33:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C30B93858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675290810; bh=n88xkVmhH6QET7bXHa+dakFahT/vMbfyIBjOj6evbdI=; h=From:To:Subject:Date:From; b=NF6vigYi+rD4xbnWzLzMu+yeEbDJHMZVP+Csz3MAr6scUm3D62EokAy2sWeH44oRH hyzQRc8XGb/kkzpqRae9+R77vKOu31J0dT//gaiqV/zqpYCMhVoo3KWl49qGwEVLDd QJkkwSCqzN1z17iF7kO2BjbAtid26axsAFB4kCVg= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Samuel Thibault To: glibc-cvs@sourceware.org Subject: [glibc] hurd: Implement O_TMPFILE X-Act-Checkin: glibc X-Git-Author: Sergey Bugaev X-Git-Refname: refs/heads/master X-Git-Oldrev: d011ab5708c2be4e2cc7eb8851c9e2c614410bd3 X-Git-Newrev: 65392c84782a53b0d7705ca0207c95c3da41c7dc Message-Id: <20230201223330.C30B93858D3C@sourceware.org> Date: Wed, 1 Feb 2023 22:33:30 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=65392c84782a53b0d7705ca0207c95c3da41c7dc commit 65392c84782a53b0d7705ca0207c95c3da41c7dc Author: Sergey Bugaev Date: Mon Jan 30 15:52:15 2023 +0300 hurd: Implement O_TMPFILE 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 Message-Id: <20230130125216.6254-3-bugaevc@gmail.com> Diff: --- 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 25dab5a16b..88c8377941 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 970f79b82d..c24a819e02 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. */