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>
111 lines
3.3 KiB
C
111 lines
3.3 KiB
C
// SPDX-License-Identifier: Apache-2.0
|
|
// SchOS — first-party userland runtime: _start entry and exit.
|
|
|
|
#include "types.h"
|
|
#include "user.h"
|
|
|
|
// The raw syscall. It takes the status in %rdi, and both callers below must
|
|
// pass one: this declaration used to be `void _xv6_exit(void)`, so the status
|
|
// was whatever the caller happened to leave in that register. Every program
|
|
// that ended with a bare exit() reported a random number, which is why
|
|
// `if socktest; then` could not work for any of them.
|
|
extern void _xv6_exit(int status);
|
|
|
|
// exit(void) in this library means exit(0). A program that wants to report
|
|
// something returns it from main -- lib/start.S passes that through now -- or
|
|
// calls exit_group(n).
|
|
void
|
|
exit(void)
|
|
{
|
|
_xv6_exit(0);
|
|
for (;;);
|
|
}
|
|
|
|
/* _start lives in lib/start.S — it must read incoming registers, which
|
|
* cannot be expressed portably in C. See that file. */
|
|
|
|
// The four the compiler is allowed to call without being asked.
|
|
//
|
|
// A freestanding build gets no libc, but it does not get a compiler that
|
|
// promises never to *call* one: the C standard lets an implementation emit
|
|
// calls to memcpy, memmove, memset and memcmp for things that look nothing
|
|
// like a function call. A struct assignment is the common one --
|
|
//
|
|
// *out = in; /* struct pciuser_info, 96 bytes */
|
|
//
|
|
// -- which gcc unrolled into stores and clang turned into `call memcpy`, so
|
|
// ldlsd linked for months and then did not, on a machine where nothing about
|
|
// it had changed except which compiler ran. The fix is not to avoid struct
|
|
// assignment; it is to provide the four functions, because the compiler is
|
|
// within its rights either way.
|
|
//
|
|
// Deliberately simple. These are not the hot path of anything here, and a
|
|
// clever one risks being turned back into a call to itself.
|
|
void *
|
|
memcpy(void *dst, const void *src, unsigned long n)
|
|
{
|
|
unsigned char *d = dst;
|
|
const unsigned char *s = src;
|
|
while (n--) *d++ = *s++;
|
|
return dst;
|
|
}
|
|
|
|
void *
|
|
memmove(void *dst, const void *src, unsigned long n)
|
|
{
|
|
unsigned char *d = dst;
|
|
const unsigned char *s = src;
|
|
|
|
if (d == s || n == 0)
|
|
return dst;
|
|
// Backwards when they overlap the wrong way, which is the whole reason
|
|
// this is not memcpy.
|
|
if (d < s) {
|
|
while (n--) *d++ = *s++;
|
|
} else {
|
|
d += n; s += n;
|
|
while (n--) *--d = *--s;
|
|
}
|
|
return dst;
|
|
}
|
|
|
|
void *
|
|
memset(void *dst, int c, unsigned long n)
|
|
{
|
|
unsigned char *d = dst;
|
|
while (n--) *d++ = (unsigned char)c;
|
|
return dst;
|
|
}
|
|
|
|
int
|
|
memcmp(const void *a, const void *b, unsigned long n)
|
|
{
|
|
const unsigned char *p = a, *q = b;
|
|
while (n--) {
|
|
if (*p != *q)
|
|
return *p - *q;
|
|
p++; q++;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
__stack_chk_fail(void)
|
|
{
|
|
// A smashed stack is not a successful run. 134 is what a shell reports for
|
|
// SIGABRT, which is the nearest true thing this can say.
|
|
_xv6_exit(134);
|
|
for (;;);
|
|
}
|
|
|
|
// exec() without an environment.
|
|
//
|
|
// The syscall is execve, three arguments, because that is what Linux numbers
|
|
// 59 and what musl calls. The two-argument spelling stays because most of
|
|
// cmd/ has no environment to pass and should not have to say so -- but it
|
|
// says so *here*, explicitly, rather than leaving %rdx to chance.
|
|
int
|
|
exec(char *path, char **argv)
|
|
{
|
|
return execve(path, argv, 0);
|
|
}
|