Toward race-free process signaling
Toward race-free process signaling
Posted Dec 12, 2018 5:43 UTC (Wed) by kmeyer (subscriber, #50720)In reply to: Toward race-free process signaling by nybble41
Parent article: Toward race-free process signaling
Ok — this might just be a terminology difference between FreeBSD and Linux. In FreeBSD, file descriptors are definitely integers. They index a per-process table of 'struct filedescent's named the 'fdescenttbl.' Each filedescent has a 'struct file' pointer, 'struct filecaps', flags, and a sequence number associated with it.
The 'struct file' tracks things like 'f_type' (DTYPE_VNODE for regular files, DTYPE_SOCKET, DTYPE_PIPE, DTYPE_KQUEUE, etc); 'fileops' (file-level vtable of operations); file-associated credentials; associated 'struct vnode' (inode in Linux terminology), if any; the file offset (for operations like read(2) or write(2) that don't take an explicit offset); etc.
So you can understand my confusion — we would call what you're talking about a 'filedescent' or just 'file'.
> all file descriptors have unique integer identifiers within a process
Ok, definitely 'filedescent' in our terminology, rather than 'file' (which would be shared by a dup(2)ed fd).
> Outside the process, however, you need some other way to identify the state that a file descriptor stands for; the index alone, without context, isn't enough.
Sure. That state can be passed between processes using unix domain sockets and control messages, though. That's not as frictionless as copying and pasting some pid number between arbitrary processes, but it's not like the credential is locked to the original process exclusively.
> How exactly does one pass a file descriptor to or from a shell script using Unix-domain sockets, for example?
It could readily be done with an extension builtin, or a helper program. (Or something crazy like ctypes.sh.) Yeah, it's higher friction than just passing around some integer. On the flip side, it works without increasing pid space to 64 bits.
> It also doesn't address the use case of serializing the identifier to a file, where there may not be any process running to hold the file descriptor open.
I'm not sure it's reasonable to serialize a process handle of any kind to a file and expect it to be meaningful later. Say your file is on NFS, or backed up to a remote system. Or even a local filesystem, but the machine has been rebooted. I'm not overly concerned with not handling that case.
Thanks for the discussion, it's been interesting and given me some food for thought.