|
|
Log in / Subscribe / Register

O_DIRECTORY|O_TMPFILE

O_DIRECTORY|O_TMPFILE

Posted Aug 3, 2026 6:37 UTC (Mon) by neilbrown (subscriber, #359)
In reply to: O_DIRECTORY|O_TMPFILE by jkoolstra
Parent article: Reconsidering O_CREAT|O_DIRECTORY

You cannot do this in this fashion since we have:

#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)

True, but opening with O_CREAT | O_TMPFILE | O_DIRECTORY could work. That combination is currently illegal and has been since O_DIRECTORY was added to O_TMPFILE.

That isn't exactly elegant, but isn't as inelegant as combining O_DIRECTORY with O_TMPFILE.

(I would have added O_EXCL to __O_TMPFILE as old kernels would return -EEXIST if O_TMPFILE we given but not understood).


to post comments

O_DIRECTORY|O_TMPFILE - sample code

Posted Aug 10, 2026 2:48 UTC (Mon) by neilbrown (subscriber, #359) [Link] (3 responses)

I decide to play and wrote some code which implements this idea for tmpfs.
It is in the TMPDIR branch of my "linux" tree on github.

https://github.com/neilbrown/linux/tree/TMPDIR

There is a .c file in the root for testing.

No promises - it might eat your data.

O_DIRECTORY|O_TMPFILE - sample code

Posted Aug 10, 2026 9:07 UTC (Mon) by donald.buczek (subscriber, #112892) [Link] (2 responses)

I decide to play and wrote some code which implements this idea for tmpfs.

Oh, just wow. This alone has so much value that I might include it as an out-of-tree patch if it doesn't go upstream.

buczek@dose:/scratch/local2/linux (x)$ df -k /dev/shm
Filesystem     1K-blocks  Used Available Use% Mounted on
tmpfs            5096620     0   5096620   0% /dev/shm
buczek@dose:/scratch/local2/linux (x)$ ./tmpcd /dev/shm /usr/bin/bash
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
buczek@dose:/scratch/local2/linux$ cp -a /scratch/local2/linux/Documentation .
buczek@dose:/scratch/local2/linux$ df -k /dev/shm
Filesystem     1K-blocks  Used Available Use% Mounted on
tmpfs            5096620 81904   5014716   2% /dev/shm
buczek@dose:/scratch/local2/linux$ exit
exit
buczek@dose:/scratch/local2/linux (x)$ df -k /dev/shm
Filesystem     1K-blocks  Used Available Use% Mounted on
tmpfs            5096620     0   5096620   0% /dev/shm

O_DIRECTORY|O_TMPFILE - sample code

Posted Aug 12, 2026 13:19 UTC (Wed) by donald.buczek (subscriber, #112892) [Link] (1 responses)

You can avoid the bash warnings by setting the PWD environment variable to "/proc/PID/fd/FD" in the wrapper. However git, for example, uses getcwd() from glibc (not the system call) which translates "(unreachable)" names to errors. So my hope, that a script could use any external tool, is not fulfilled. Still very useful, though.

buczek@dose:/scratch/local2/linux (x)$ ./tmpcd /dev/shm /bin/bash
buczek@dose:/proc/837/fd/3$ pwd
/proc/837/fd/3
buczek@dose:/proc/837/fd/3$ /bin/pwd
/bin/pwd: couldn't find directory entry in ‘..’ with matching i-node
buczek@dose:/proc/837/fd/3$ git status
fatal: Unable to read current working directory: No such file or directory
buczek@dose:/proc/837/fd/3$ strace git status 2>&1 | fgrep getcwd
getcwd("(unreachable)/", 129)           = 15
getcwd("(unreachable)/", 129)           = 15
buczek@dose:/proc/837/fd/3$ ltrace git status 2>&1 | fgrep getcwd
getcwd(0x7de8a0, 129 <unfinished ...>
<... getcwd resumed> )                           = nil
getcwd(0x7de8a0, 129 <unfinished ...>
<... getcwd resumed> )                           = nil

O_DIRECTORY|O_TMPFILE - sample code

Posted Aug 13, 2026 1:47 UTC (Thu) by neilbrown (subscriber, #359) [Link]

You can avoid the bash warnings by setting the PWD environment variable to ...

You can also avoid the bash warnings by actually mounting the directory. You would want to do that in a private namespace to keep the directory private. The namespace would be torn down on exit which would clean up the mount and the temp directory.

Where to mount it? I would designate a place which is otherwise unused where anyone can mount a private directory in a private namespace. Maybe /run/scratch ??

I've pushed out some changes to github which you can experiment with.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds