unix: a bound socket is a file

Gate 1, item 3, and the last of foundations.md.

bind() creates a T_SOCK node at the path; connect() resolves the path and looks
for the socket bound to **that inode**. Matching on the inode rather than on
the path *string* is the part that matters: the name used to live only in the
socket table, so two spellings of one path missed each other, a relative path
never matched an absolute one, and there was nothing to stat() or unlink().
An inode is the thing a path resolves *to*, so connect() can walk the path
however it likes and arrive at the same socket.

    # ls -l /tmp
    srwxr-xr-x 1 root root 0 2026-09-22 08:04 socktest

The node is a marker, not the binding. It holds no data and the live
association stays in the socket table, so a socket file left by a crash is
what Linux leaves behind -- a dead file that connect() refuses until something
binds again -- and unlink()ing a live socket's path does not disturb anyone
already connected.

EADDRINUSE comes from the *directory* rather than from a scan of the table,
deliberately: a stale socket file has to refuse a rebind for the same reason a
running server's does, or two servers both believe they own it. The creation
lives in sys_bind() and not in unix.c, because that is the layer with both --
kernel/net/unix.c knows about sockets and has no business calling create().

T_SOCK is a compat addition to SchFS by the rule in schfs.h: a driver that does
not know SCHFS_T_SOCK maps it to a zero-length regular file, so nothing goes
stale and nothing becomes unreadable.

cmd/socktest section 15 asserts the plan's own wording -- bind, stat, see an
`s`, unlink -- plus the two a name table could never fail: a second bind to a
live path is refused, and connecting to a path that is a *regular file* is
refused rather than matched by name.

**And the semantics being real broke socktest on its second run**, correctly:
the socket files persisted and every bind() hit EADDRINUSE. Every real server
unlinks before binding; socktest now does too, which is the change the new
behaviour demands rather than a workaround for it.

Chasing that turned up something worse. `socktest` reported rc=1 with every
check passing, and so did every other first-party program:

  - **lib/start.S called main and threw %eax away.** sys_exit() reads the
    status from %rdi and nothing put it there, so every program exited with
    whatever that register happened to hold. Every test suite ends in
    `return failures ? 1 : 0` and none of it reached the shell.
  - **exit(void) passed no status either**, for the same reason: the
    declaration takes no argument, so %rdi was the caller's litter.

`if socktest; then` works now, and a failing command reports 1 -- checked both
ways, because a fix that makes everything return 0 would look identical from
the success side.

Also factored: unlink_path() out of sys_unlink, so a failed bind can take its
own node back out without going through the syscall entry, and create_sock()
as a named door rather than exporting create() with an arbitrary type.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-09-22 10:06:10 +02:00
commit 3f13989883
15 changed files with 339 additions and 45 deletions

View file

@ -164,6 +164,10 @@ uint tree_expand(struct inode *ip, uint off);
void tree_search(struct inode* dp, char* name, uint* poff, struct dirent* de,uint add);
void build_dir_tree(struct inode* dp,uint64 pinum);
struct inode* divesymlink(struct inode *);
// A socket node for AF_UNIX bind(2), and unlink by path for the failure that
// follows it. See the notes on each in kernel/sys/sys_fs.c.
struct inode* create_sock(char *path);
int unlink_path(char *path);
void readsb(int dev, struct superblock *sb);
// pci.c -- bus enumeration. Drivers that need to *find* their hardware use
@ -469,10 +473,10 @@ int sockwrite(struct socket*, char*, int);
// The same two, carrying descriptors. See kernel/net/unix.c.
int sockread_fds(struct socket*, char*, int, struct file**, int*);
int sockwrite_fds(struct socket*, char*, int, struct file**, int);
int sockbind(struct socket*, const char *path);
int sockbind(struct socket*, const char *path, uint dev, uint64 ino);
int socklisten(struct socket*, int backlog);
struct socket* sockaccept(struct socket*);
int sockconnect(struct socket*, const char *path);
int sockconnect(struct socket*, uint dev, uint64 ino);
int sockreadable(struct socket*);
int sock_hup(struct socket*);
int sock_shutdown(struct socket*, int how);

View file

@ -293,6 +293,11 @@ _Static_assert(__builtin_offsetof(struct schfs_uberblock, next_objectid) == 72,
#define SCHFS_T_DIR 2
#define SCHFS_T_DEV 3
#define SCHFS_T_SYMLINK 4
// A bound AF_UNIX socket. A *compat* addition by the rule above: a driver that
// does not know this value maps it to SCHFS_T_FILE and sees a zero-length
// regular file, which is harmless -- nothing goes stale and nothing becomes
// unreadable, so it needs no feature flag.
#define SCHFS_T_SOCK 5
struct schfs_inode {
uint64_t size;

View file

@ -309,7 +309,21 @@ struct socket {
// wants a struct socket.
void *inet;
char name[UNIX_PATH_MAX]; // the bound path, for a listener
char name[UNIX_PATH_MAX]; // the bound path, for a listener and getsockname
// The filesystem node bind() created, by identity rather than by name.
//
// Matching on the *path string* was what made these sockets invisible to the
// filesystem: two spellings of one path missed each other, a relative path
// never matched an absolute one, and there was nothing to stat() or
// unlink(). An inode is the thing a path resolves *to*, so connect() can
// walk the path however it likes and still arrive at the same socket.
//
// bind_dev 0 means "no node": a socket bound before this existed, or one
// whose file has been unlinked. The socket stays bound and stays usable by
// anyone already connected; it is simply no longer findable, which is
// exactly what Linux does.
uint bind_dev;
uint64 bind_ino;
// A connected socket's peer. Writing to a socket appends to the *peer's*
// buffer, which is what makes one buffer per socket enough for a

View file

@ -6,6 +6,13 @@
#define T_DEV 3 // Device
#define T_SYMLINK 4 // Divesymlink
#define T_EXTENT 5 // bmap & itrunc
// A bound AF_UNIX socket. The node is a *marker*: it is what makes a socket
// findable by path, stat()able and unlink()able, and it holds no data. The
// live binding lives in kernel/net/unix.c and is matched by (dev, inum), not
// by this node's contents -- so a socket file left behind by a crash is
// exactly what Linux leaves behind, a dead file that connect() refuses until
// something binds to it again.
#define T_SOCK 6
struct stat {
int32 type; // File type (T_DIR, T_FILE, or T_DEV)