sendmsg/recvmsg with SCM_RIGHTS. Item 1 of docs/plan/foundations.md and
mechanism 1 of docs/plan/hybrid.md, which are the same primitive seen from two
directions: Wayland's wl_shm_create_pool passes a file descriptor with no
integer-shaped alternative, and handing a driver process the device it is
allowed to touch is the same act. Both plans stopped here.
Linux's structures byte for byte -- msghdr, cmsghdr, the CMSG_ macros -- because
the programs that need this are not ours. libwayland and toybox both call
recvmsg with Linux's headers and would not notice a friendlier door.
The delivery rule is the whole design. A batch of descriptors is pinned to the
stream position of the first byte of the message that carried it, and that has
two halves: a reader arriving at that position is handed them, and a reader that
has not reached it is stopped SHORT of it. Leave out the second half and a
program asking for 4096 bytes swallows three messages and is handed one
message's descriptors, with nothing in the result saying which. socktest checks
exactly that: two one-byte messages, an eight-byte read, one byte back.
The kernel holds a reference to every descriptor in flight, so a sender may
close its own copy the moment sendmsg returns. Giving that reference back is the
part that leaks if it is not written down, and there are three ways a message
ends:
- received with room installed in the receiver's table
- received without room, or by
plain read(2) closed; MSG_CTRUNC for the first case
- never received, socket closed closed by sockclose
The middle row looks like a choice and is not: holding kernel references for a
receiver that has proved it is not looking is a leak with extra steps.
No garbage collector. A socket descriptor sent over that same socket and never
received holds a reference to the socket that holds the reference to it, and
neither close reaches zero. Linux carries several hundred lines for this.
Nothing on either plan passes a socket over a socket -- Wayland passes memory,
the driver work passes devices -- so it is a bounded leak that is documented
rather than a silent one.
Fourteen checks in cmd/socktest, and every one moves data through the descriptor
rather than inspecting it. The descriptor passed is always a pipe end, because a
pipe announces its own lifetime: a read of a pipe whose writers are all gone
returns 0, which turns "was the kernel's reference released" -- otherwise
invisible from userspace -- into a value a test can compare.
Two bugs fell out, neither in the new code:
- sockpair() never set the socket's domain, so every socketpair() socket
claimed domain 0. Invisible while the data paths asked only whether a socket
was AF_INET or AF_NETLINK, since zero answers "neither" correctly.
SCM_RIGHTS asks "is this AF_UNIX", and zero is wrong for that one.
- pipe_poll() reports POLLHUP but not POLLIN at end of file, so a non-blocking
read of a pipe whose writers had all gone returned EAGAIN forever instead of
0 -- a program that polls and reads spins waiting for an end that already
happened. sockreadable() always had the equivalent condition; the pipe path
did not. Found because the test helper for "is this descriptor closed" is a
non-blocking read of a pipe.
projects/schcom/README.md said SCM_RIGHTS was done for a long time while nothing
in the kernel implemented it, and an entire GUI plan was drawn on that line.
It is true now, which is not the same as it having been right, and the README
says so.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
306 lines
8.1 KiB
C
306 lines
8.1 KiB
C
#include "types.h"
|
|
#include "defs.h"
|
|
#include "stat.h"
|
|
#include "param.h"
|
|
#include "fs.h"
|
|
#include "spinlock.h"
|
|
#include "sleeplock.h"
|
|
#include "file.h"
|
|
#include "fsstat.h"
|
|
#include "posix.h"
|
|
#include "errno.h"
|
|
|
|
extern struct devsw devsw[];
|
|
|
|
struct {
|
|
struct spinlock lock;
|
|
struct file file[NFILE];
|
|
} ftable;
|
|
|
|
void
|
|
fileinit(void)
|
|
{
|
|
initlock(&ftable.lock, "ftable");
|
|
}
|
|
|
|
struct file*
|
|
filealloc(void)
|
|
{
|
|
struct file *f;
|
|
|
|
acquire(&ftable.lock);
|
|
for(f = ftable.file; f < ftable.file + NFILE; f++){
|
|
if(f->ref == 0){
|
|
f->ref = 1;
|
|
FSSTAT_TAKE(fsstat.file_used, fsstat.file_peak); // 0 -> 1
|
|
// Cleared here rather than left to the caller: every creator sets
|
|
// readable/writable/off, but only open() sets nonblock, so a struct file
|
|
// recycled from a non-blocking device would hand its flag to the next
|
|
// pipe or socket that happened to land in this slot.
|
|
f->nonblock = 0;
|
|
release(&ftable.lock);
|
|
return f;
|
|
}
|
|
}
|
|
release(&ftable.lock);
|
|
return 0;
|
|
}
|
|
|
|
struct file*
|
|
filedup(struct file *f)
|
|
{
|
|
acquire(&ftable.lock);
|
|
if(f->ref < 1)
|
|
panic("filedup");
|
|
f->ref++;
|
|
release(&ftable.lock);
|
|
return f;
|
|
}
|
|
|
|
void
|
|
fileclose(struct file *f)
|
|
{
|
|
struct file ff;
|
|
|
|
acquire(&ftable.lock);
|
|
if(f->ref < 1)
|
|
panic("fileclose");
|
|
if(--f->ref > 0){
|
|
release(&ftable.lock);
|
|
return;
|
|
}
|
|
ff = *f;
|
|
f->ref = 0;
|
|
f->type = FD_NONE;
|
|
FSSTAT_DROP(fsstat.file_used); // 1 -> 0
|
|
release(&ftable.lock);
|
|
|
|
if(ff.type == FD_PIPE)
|
|
pipeclose(ff.pipe, ff.writable);
|
|
else if(ff.type == FD_SOCKET)
|
|
sockclose(ff.sock);
|
|
else if(ff.type == FD_INODE){
|
|
// Give a device a chance to put the hardware back before the inode goes.
|
|
// Done here rather than in exit() because a program that closes the
|
|
// framebuffer without exiting has finished with it just as thoroughly.
|
|
if(ff.ip && ff.ip->type == T_DEV && ff.ip->major >= 0 &&
|
|
ff.ip->major < NDEV && devsw[ff.ip->major].close)
|
|
devsw[ff.ip->major].close(ff.ip);
|
|
|
|
begin_op();
|
|
iput(ff.ip);
|
|
end_op();
|
|
}
|
|
}
|
|
|
|
int
|
|
filestat(struct file *f, struct stat *st)
|
|
{
|
|
if(f->type == FD_INODE){
|
|
ilock(f->ip);
|
|
stati(f->ip, st);
|
|
iunlock(f->ip);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int
|
|
fileread(struct file *f, char *addr, int n)
|
|
{
|
|
int r;
|
|
|
|
if(f->readable == 0)
|
|
return -1;
|
|
|
|
// O_NONBLOCK on a pipe or a socket. Reported here rather than inside
|
|
// piperead/sockread so that the blocking implementations stay unchanged:
|
|
// this is a check of the same condition their sleep loops wait on, made one
|
|
// level up. Writes still block -- fcntl documents that.
|
|
if(f->type == FD_PIPE){
|
|
// POLLHUP counts as readable here, and leaving it out was a bug: a pipe
|
|
// whose writers have all gone is not "nothing yet", it is end of file, and
|
|
// the answer to a read is 0. Told EAGAIN instead, a caller that polls and
|
|
// reads spins forever waiting for an end that has already happened.
|
|
// sockreadable() has always included the equivalent condition.
|
|
if(f->nonblock &&
|
|
!(pipe_poll(f->pipe, f->readable, f->writable) & (POLLIN | POLLHUP)))
|
|
return -EAGAIN;
|
|
return piperead(f->pipe, addr, n);
|
|
}
|
|
|
|
if(f->type == FD_SOCKET){
|
|
if(f->nonblock && !sockreadable(f->sock))
|
|
return -EAGAIN;
|
|
return sockread(f->sock, addr, n);
|
|
}
|
|
|
|
if(f->type == FD_INODE){
|
|
ilock(f->ip);
|
|
if(f->ip->type == 3){
|
|
if(f->ip->major < 0 || f->ip->major >= NDEV || !devsw[f->ip->major].read){
|
|
iunlock(f->ip);
|
|
return -1;
|
|
}
|
|
|
|
// O_NONBLOCK. Without this a game that opens an input device
|
|
// non-blocking and drains it once per frame stops dead on the first
|
|
// frame -- it blocks in read() waiting for a key, and the result looks
|
|
// exactly like a hung renderer rather than a blocking read.
|
|
if(f->nonblock && devsw[f->ip->major].ready &&
|
|
!devsw[f->ip->major].ready(f->ip)){
|
|
iunlock(f->ip);
|
|
return -EAGAIN;
|
|
}
|
|
|
|
r = devsw[f->ip->major].read(f->ip, f->off, addr, n);
|
|
if(r > 0)
|
|
f->off += r;
|
|
iunlock(f->ip);
|
|
return r;
|
|
}
|
|
|
|
if((f->ip->rwmode & 2) == 0){
|
|
cprintf("SchOS: permission denied (read)\n");
|
|
iunlock(f->ip);
|
|
return -1;
|
|
}
|
|
|
|
// The "recycle bin" check that used to sit here is gone.
|
|
//
|
|
// It made a file that still existed, still had a name and still had blocks
|
|
// refuse every read with a printf -- a state no POSIX program is prepared
|
|
// for. It was reachable only through `delete`, a NO_LINUX syscall from the
|
|
// third-party filesystem this file derives from, which the decision to keep
|
|
// only Linux numbering had already orphaned.
|
|
//
|
|
// The on-disk `showmode` field stays for now: removing it changes the inode
|
|
// layout, and this whole file is being replaced by SchFS. It is simply
|
|
// never consulted.
|
|
|
|
if((r = readi(f->ip, 0, (uint64)addr, f->off, n)) > 0)
|
|
f->off += r;
|
|
iunlock(f->ip);
|
|
return r;
|
|
}
|
|
|
|
panic("fileread");
|
|
}
|
|
|
|
int
|
|
filewrite(struct file *f, char *addr, int n)
|
|
{
|
|
int r;
|
|
|
|
if(f->writable == 0)
|
|
return -1;
|
|
|
|
if(f->type == FD_PIPE)
|
|
return pipewrite(f->pipe, addr, n);
|
|
|
|
if(f->type == FD_SOCKET)
|
|
return sockwrite(f->sock, addr, n);
|
|
|
|
if(f->type == FD_INODE){
|
|
ilock(f->ip);
|
|
|
|
if(f->ip->type == 3){
|
|
if(f->ip->major < 0 || f->ip->major >= NDEV || !devsw[f->ip->major].write){
|
|
iunlock(f->ip);
|
|
return -1;
|
|
}
|
|
r = devsw[f->ip->major].write(f->ip, f->off, addr, n);
|
|
if(r > 0)
|
|
f->off += r;
|
|
iunlock(f->ip);
|
|
return r;
|
|
}
|
|
|
|
if((f->ip->rwmode & 1) == 0){
|
|
cprintf("SchOS: permission denied (write)\n");
|
|
iunlock(f->ip);
|
|
return -1;
|
|
}
|
|
|
|
// Recycle-bin check removed here too; see the note in fileread().
|
|
iunlock(f->ip);
|
|
|
|
// How much may go in one call is the filesystem's business. v1 is bounded
|
|
// by its log; SchFS is not bounded at all and would rather have the lot,
|
|
// because it makes one transaction out of whatever it is given.
|
|
uint max = f->ip->ops->max_write;
|
|
int i = 0;
|
|
while(i < n){
|
|
int n1 = n - i;
|
|
if(max && (uint)n1 > max)
|
|
n1 = (int)max;
|
|
|
|
begin_op();
|
|
ilock(f->ip);
|
|
if ((r = writei(f->ip, 1, (uint64)(addr + i), f->off, n1)) > 0)
|
|
f->off += r;
|
|
|
|
iunlock(f->ip);
|
|
end_op();
|
|
|
|
if(r < 0)
|
|
break;
|
|
if(r != n1)
|
|
panic("short filewrite");
|
|
i += r;
|
|
}
|
|
return i == n ? n : -1;
|
|
}
|
|
panic("filewrite");
|
|
}
|
|
|
|
// poll(2) readiness for one open file. Returns a POLL* bitmask restricted
|
|
// to the requested events (plus error/hangup conditions, which POSIX says
|
|
// are always reported). Pipes and the console tty have real state; other
|
|
// inodes (regular files, block-ish devices) are always ready, per POSIX.
|
|
int
|
|
file_poll(struct file *f, int events)
|
|
{
|
|
if(f->type == FD_PIPE)
|
|
return pipe_poll(f->pipe, f->readable, f->writable) &
|
|
(events | POLLERR | POLLHUP);
|
|
|
|
if(f->type == FD_SOCKET){
|
|
int rev = 0;
|
|
if(sockreadable(f->sock))
|
|
rev |= POLLIN;
|
|
// A stream socket is writable whenever it has a peer. The buffer can
|
|
// still be full, in which case the write blocks -- same latitude poll()
|
|
// has always had, and the same answer a pipe gives.
|
|
if(sock_hup(f->sock))
|
|
rev |= POLLHUP;
|
|
else
|
|
rev |= POLLOUT;
|
|
return rev & (events | POLLERR | POLLHUP);
|
|
}
|
|
|
|
if(f->type == FD_INODE){
|
|
// An input device is only readable when an event is waiting. Reporting it
|
|
// always-ready -- which is what the generic answer below does -- turns a
|
|
// poll loop into a busy loop that blocks in read().
|
|
if(f->ip && f->ip->type == T_DEV && f->ip->major == INPUT_MAJOR){
|
|
int rev = 0;
|
|
if((events & POLLIN) && f->readable && input_ready(f->ip->minor))
|
|
rev |= POLLIN;
|
|
if(events & POLLOUT)
|
|
rev |= POLLOUT;
|
|
return rev;
|
|
}
|
|
|
|
if(f->ip && f->ip->type == T_DEV && f->ip->major == CONSOLE){
|
|
int rev = 0;
|
|
if((events & POLLIN) && f->readable && console_input_ready(f->ip ? f->ip->minor : 1))
|
|
rev |= POLLIN;
|
|
if((events & POLLOUT) && f->writable)
|
|
rev |= POLLOUT;
|
|
return rev;
|
|
}
|
|
return events & (POLLIN | POLLOUT);
|
|
}
|
|
return POLLNVAL;
|
|
}
|