Page MenuHomeFreeBSD

fcntl(2): add F_DUP_OPATH to dup vnode-backed file to O_PATH file
AbandonedPublic

Authored by kib on May 24 2025, 2:35 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Oct 14, 7:11 PM
Unknown Object (File)
Fri, Oct 10, 1:29 PM
Unknown Object (File)
Thu, Oct 2, 1:14 PM
Unknown Object (File)
Thu, Oct 2, 12:32 PM
Unknown Object (File)
Fri, Sep 26, 11:05 AM
Unknown Object (File)
Sep 1 2025, 6:25 AM
Unknown Object (File)
Aug 27 2025, 9:10 PM
Unknown Object (File)
Aug 26 2025, 10:18 AM
Subscribers

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

sys/kern/kern_descrip.c
917

fp may be shared among multiple FDs. Why is it safe to mutate the file handle this way, without any synchronization?

sys/kern/kern_descrip.c
917

I do not see why it is unsafe, to begin with. I change the fileops. If any thread is already in the previous method, nothing is changed for it.

sys/kern/kern_descrip.c
917

It might be indeed unsafe for tmpfs. Then either I can keep f_data, or perhaps better, implement the dup-like behavior.

kib retitled this revision from fcntl(2): add F_TO_O_PATH to convert existing vnode-backed file to O_PATH to fcntl(2): add F_DUP_OPATH to dup vnode-backed file to O_PATH file.
sys/kern/kern_descrip.c
918

I wonder if we should just silently accept fp->f_ops == &path_fileops here as the work is already done.

923

Double break

sys/kern/kern_descrip.c
918

oh, the type is already right; sorry.

This LGTM and works well for what I need in conjunction with O_EMPTY_PATH -- thanks!

This revision is now accepted and ready to land.May 25 2025, 1:02 AM

For posterity: @kib noted that we can probably accomplish the same with openat(fd, "", O_EMPTY_PATH | O_PATH);, which seems to work in my testing (also, just re-opening my tty fd with O_EMPTY_PATH | O_RDONLY seems to work as well) -- so we probably don't need this with that kind of flexibility already functional today.

Should dup3() also permit O_PATH? There, I believe it should behave like F_DUP_OPATH but also allows the caller to choose the target fd.

This is missing a man page entry and ideally some simple tests in tests/sys/file/path_test.c.

sys/kern/kern_descrip.c
916

Should we do anything special if the fp is already an O_PATH file?

923

In openflags() we clear the mode flags for O_PATH files, but here we are preserving FREAD and FWRITE, which is inconsistent.

I do not thin that dup3 flag is very useful. Also dup3 is implemented in a way that makes adding flags a pain.
Instead I propose to add a simple recipe how to convert any fd to O_PATH-like, see D50519. From there, dup3 can put it into desired index.

sys/kern/kern_descrip.c
916

I do not think so. At least, I do not see what useful could we do differently.