AF_UNIX SEQPACKET/DGRAM, writable /dev overlay, uevent netlink socket; eudev packages
udevd now starts, reads its rules and listens. Still to come: the sysfs device tree and writable uevent files so a trigger has something to replay. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EVRhxi9zpdW2uDUN4FVcao
This commit is contained in:
parent
ed81f9a07a
commit
a9f8cb7e0f
7 changed files with 457 additions and 40 deletions
|
|
@ -39,6 +39,7 @@
|
|||
#include "uapi/errno.h"
|
||||
#include "fs/blkdev.h"
|
||||
#include "dev/pty.h"
|
||||
#include "core/uaccess.h"
|
||||
|
||||
// Inode numbering. Two directories and a flat table of nodes, so the number is
|
||||
// either a directory id or an index into devnodes[] offset past them.
|
||||
|
|
@ -57,6 +58,52 @@
|
|||
#define DEVFS_PTS0 8192ULL
|
||||
#define DEVFS_PTM0 12288ULL
|
||||
|
||||
// What udev adds to /dev: directories (/dev/input/by-path, /dev/disk/...) and
|
||||
// symlinks (/dev/fd, /dev/stdin, /dev/char/13:64, /dev/disk/by-id/...). The
|
||||
// device nodes themselves stay the kernel's; this is a small table of names
|
||||
// that live only in memory, as they would in a tmpfs, listed after the real
|
||||
// entries of whichever directory they are in. Nothing here can hold data: a
|
||||
// regular file in /dev is refused.
|
||||
#define DEVFS_EXTRA0 32768ULL
|
||||
#define NEXTRA 192
|
||||
#define EXTRA_NAME 48
|
||||
#define EXTRA_TARGET 112
|
||||
|
||||
struct extra {
|
||||
uchar used; // allocated (an inode exists or is about to)
|
||||
uchar linked; // has a name in a directory
|
||||
short type; // T_DIR or T_SYMLINK
|
||||
uint64 parent; // inum of the directory it is in
|
||||
ushort tlen;
|
||||
char name[EXTRA_NAME];
|
||||
char target[EXTRA_TARGET];
|
||||
};
|
||||
|
||||
static struct {
|
||||
struct spinlock lock;
|
||||
struct extra e[NEXTRA];
|
||||
} extras;
|
||||
|
||||
static int extras_ready;
|
||||
|
||||
static void
|
||||
extras_init(void)
|
||||
{
|
||||
if (!extras_ready) {
|
||||
initlock(&extras.lock, "devfsx");
|
||||
extras_ready = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
extra_of(uint64 inum)
|
||||
{
|
||||
if (inum < DEVFS_EXTRA0)
|
||||
return -1;
|
||||
uint64 i = inum - DEVFS_EXTRA0;
|
||||
return i < NEXTRA ? (int)i : -1;
|
||||
}
|
||||
|
||||
// Which directory a node lives in. Only two exist and a third would want a real
|
||||
// tree rather than a flag, which is the point at which this file should grow
|
||||
// one instead of adding DEV_DIR_THIRD.
|
||||
|
|
@ -158,7 +205,21 @@ devfs_iload(struct inode *ip)
|
|||
ip->major = ip->minor = 0;
|
||||
ip->size = 0;
|
||||
|
||||
if (ip->inum == DEVFS_ROOT || ip->inum == DEVFS_INPUT ||
|
||||
int x = extra_of(ip->inum);
|
||||
if (x >= 0) {
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
if (extras.e[x].used) {
|
||||
ip->type = extras.e[x].type;
|
||||
ip->nlink = extras.e[x].type == T_DIR ? 2 : 1;
|
||||
ip->size = extras.e[x].tlen;
|
||||
ip->mode = extras.e[x].type == T_DIR ? 0755 : 0777;
|
||||
} else {
|
||||
ip->type = 0;
|
||||
ip->nlink = 0;
|
||||
}
|
||||
release(&extras.lock);
|
||||
} else if (ip->inum == DEVFS_ROOT || ip->inum == DEVFS_INPUT ||
|
||||
ip->inum == DEVFS_PTS) {
|
||||
ip->type = T_DIR;
|
||||
ip->nlink = 2;
|
||||
|
|
@ -193,16 +254,69 @@ devfs_iload(struct inode *ip)
|
|||
}
|
||||
|
||||
static void devfs_istore(struct inode *ip) { (void)ip; }
|
||||
static void devfs_ifree(struct inode *ip) { (void)ip; }
|
||||
static void
|
||||
devfs_ifree(struct inode *ip)
|
||||
{
|
||||
int x = extra_of(ip->inum);
|
||||
if (x < 0)
|
||||
return;
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
extras.e[x].used = 0;
|
||||
release(&extras.lock);
|
||||
}
|
||||
static void devfs_truncate(struct inode *ip, uint64 n) { (void)ip; (void)n; }
|
||||
|
||||
// Never reached for a device node: fileread and filewrite see T_DEV and go
|
||||
// through devsw. Present because fs_ops has the slots, and answering -EIO is
|
||||
// better than a null pointer if something ever does arrive here.
|
||||
static int devfs_read(struct inode *ip, int u, uint64 d, uint64 o, uint n)
|
||||
{ (void)ip; (void)u; (void)d; (void)o; (void)n; return -1; }
|
||||
static int devfs_write(struct inode *ip, int u, uint64 s, uint64 o, uint n)
|
||||
{ (void)ip; (void)u; (void)s; (void)o; (void)n; return -1; }
|
||||
static int
|
||||
devfs_read(struct inode *ip, int u, uint64 d, uint64 o, uint n)
|
||||
{
|
||||
int x = extra_of(ip->inum);
|
||||
if (x < 0 || ip->type != T_SYMLINK)
|
||||
return -1;
|
||||
extras_init();
|
||||
char buf[EXTRA_TARGET];
|
||||
acquire(&extras.lock);
|
||||
uint len = extras.e[x].tlen;
|
||||
memmove(buf, extras.e[x].target, len);
|
||||
release(&extras.lock);
|
||||
if (o >= len)
|
||||
return 0;
|
||||
if (o + n > len)
|
||||
n = (uint)(len - o);
|
||||
if (u) {
|
||||
if (copy_to_user(d, buf + o, n) < 0)
|
||||
return -EFAULT;
|
||||
} else {
|
||||
memmove((char *)d, buf + o, n);
|
||||
}
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
// Only a symlink's target can be written, once, from offset 0.
|
||||
static int
|
||||
devfs_write(struct inode *ip, int u, uint64 s, uint64 o, uint n)
|
||||
{
|
||||
int x = extra_of(ip->inum);
|
||||
if (x < 0 || ip->type != T_SYMLINK || o != 0 || n >= EXTRA_TARGET)
|
||||
return -1;
|
||||
extras_init();
|
||||
char buf[EXTRA_TARGET];
|
||||
if (u) {
|
||||
if (copy_from_user(buf, s, n) < 0)
|
||||
return -EFAULT;
|
||||
} else {
|
||||
memmove(buf, (char *)s, n);
|
||||
}
|
||||
acquire(&extras.lock);
|
||||
memmove(extras.e[x].target, buf, n);
|
||||
extras.e[x].tlen = (ushort)n;
|
||||
release(&extras.lock);
|
||||
ip->size = n;
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
static int
|
||||
streq(const char *a, const char *b)
|
||||
|
|
@ -211,12 +325,41 @@ streq(const char *a, const char *b)
|
|||
return *a == *b;
|
||||
}
|
||||
|
||||
static struct inode *
|
||||
extra_find(uint64 parent, const char *name)
|
||||
{
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
for (int i = 0; i < NEXTRA; i++)
|
||||
if (extras.e[i].used && extras.e[i].linked && extras.e[i].parent == parent &&
|
||||
streq(name, extras.e[i].name)) {
|
||||
release(&extras.lock);
|
||||
return iget(DEVFS_DEV, DEVFS_EXTRA0 + (uint64)i);
|
||||
}
|
||||
release(&extras.lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct inode *
|
||||
devfs_dirlookup(struct inode *dp, char *name, uint *poff)
|
||||
{
|
||||
if (poff)
|
||||
*poff = 0;
|
||||
|
||||
int dx = extra_of(dp->inum);
|
||||
if (dx >= 0) {
|
||||
extras_init();
|
||||
if (streq(name, "."))
|
||||
return iget(DEVFS_DEV, dp->inum);
|
||||
if (streq(name, "..")) {
|
||||
acquire(&extras.lock);
|
||||
uint64 up = extras.e[dx].parent;
|
||||
release(&extras.lock);
|
||||
return iget(DEVFS_DEV, up ? up : DEVFS_ROOT);
|
||||
}
|
||||
return extra_find(dp->inum, name);
|
||||
}
|
||||
|
||||
if (streq(name, ".") || (streq(name, "..") && dp->inum == DEVFS_ROOT))
|
||||
return iget(DEVFS_DEV, dp->inum);
|
||||
if (streq(name, ".."))
|
||||
|
|
@ -261,19 +404,85 @@ devfs_dirlookup(struct inode *dp, char *name, uint *poff)
|
|||
if (streq(name, blkdev_name((uint)b)))
|
||||
return iget(DEVFS_DEV, DEVFS_BLK0 + (uint64)b);
|
||||
|
||||
return extra_find(dp->inum, name);
|
||||
}
|
||||
|
||||
// What can be created here: a directory or a symlink, by ialloc and then
|
||||
// dirlink -- the order every filesystem is driven in. The device nodes
|
||||
// themselves are the kernel's: mknod answers EROFS by never getting an inode.
|
||||
static struct inode *
|
||||
devfs_ialloc(uint dev, short type)
|
||||
{
|
||||
(void)dev;
|
||||
if (type != T_DIR && type != T_SYMLINK)
|
||||
return 0;
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
for (int i = 0; i < NEXTRA; i++)
|
||||
if (!extras.e[i].used) {
|
||||
memset(&extras.e[i], 0, sizeof(extras.e[i]));
|
||||
extras.e[i].used = 1;
|
||||
extras.e[i].type = type;
|
||||
release(&extras.lock);
|
||||
return iget(DEVFS_DEV, DEVFS_EXTRA0 + (uint64)i);
|
||||
}
|
||||
release(&extras.lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A read-only filesystem, and these say so rather than pretending. mknod on
|
||||
// /dev returning EROFS is the correct answer: the node either exists because
|
||||
// the kernel has the device, or it does not exist because it has not.
|
||||
static int devfs_dirlink(struct inode *dp, char *name, uint64 inum)
|
||||
{ (void)dp; (void)name; (void)inum; return -EROFS; }
|
||||
static int devfs_dirunlink(struct inode *dp, char *name, uint off)
|
||||
{ (void)dp; (void)name; (void)off; return -EROFS; }
|
||||
static int devfs_dirempty(struct inode *dp) { (void)dp; return 0; }
|
||||
static struct inode *devfs_ialloc(uint dev, short type)
|
||||
{ (void)dev; (void)type; return 0; }
|
||||
static int
|
||||
devfs_dirlink(struct inode *dp, char *name, uint64 inum)
|
||||
{
|
||||
int x = extra_of(inum);
|
||||
if (streq(name, ".") || streq(name, ".."))
|
||||
return 0; /* a new directory's own dots */
|
||||
if (x < 0 || strlen(name) >= EXTRA_NAME)
|
||||
return -EINVAL;
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
if (!extras.e[x].used || extras.e[x].linked) {
|
||||
release(&extras.lock);
|
||||
return -EINVAL;
|
||||
}
|
||||
extras.e[x].parent = dp->inum;
|
||||
extras.e[x].linked = 1;
|
||||
memmove(extras.e[x].name, name, strlen(name) + 1);
|
||||
release(&extras.lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
devfs_dirempty(struct inode *dp)
|
||||
{
|
||||
if (extra_of(dp->inum) < 0)
|
||||
return 0;
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
for (int i = 0; i < NEXTRA; i++)
|
||||
if (extras.e[i].used && extras.e[i].linked && extras.e[i].parent == dp->inum) {
|
||||
release(&extras.lock);
|
||||
return 0;
|
||||
}
|
||||
release(&extras.lock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
devfs_dirunlink(struct inode *dp, char *name, uint off)
|
||||
{
|
||||
(void)off;
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
for (int i = 0; i < NEXTRA; i++)
|
||||
if (extras.e[i].used && extras.e[i].linked && extras.e[i].parent == dp->inum &&
|
||||
streq(name, extras.e[i].name)) {
|
||||
extras.e[i].linked = 0; /* the inode goes when nobody holds it */
|
||||
release(&extras.lock);
|
||||
return 0;
|
||||
}
|
||||
release(&extras.lock);
|
||||
return -EROFS; /* a device node is the kernel's */
|
||||
}
|
||||
|
||||
struct linux_dirent64 {
|
||||
uint64 d_ino;
|
||||
|
|
@ -312,8 +521,9 @@ devfs_getdents(struct inode *dp, char *out, int cap, uint64 *poff)
|
|||
int used = 0;
|
||||
uint64 n = *poff;
|
||||
|
||||
int isx = extra_of(dp->inum) >= 0;
|
||||
if (dp->inum != DEVFS_ROOT && dp->inum != DEVFS_INPUT &&
|
||||
dp->inum != DEVFS_PTS)
|
||||
dp->inum != DEVFS_PTS && !isx)
|
||||
return -1;
|
||||
|
||||
if (n == 0) {
|
||||
|
|
@ -326,6 +536,25 @@ devfs_getdents(struct inode *dp, char *out, int cap, uint64 *poff)
|
|||
n++;
|
||||
}
|
||||
|
||||
// A directory udev made: the dots, then what is in it.
|
||||
if (isx) {
|
||||
uint64 base = 2;
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
for (int i = 0; i < NEXTRA; i++) {
|
||||
if (!extras.e[i].used || !extras.e[i].linked || extras.e[i].parent != dp->inum)
|
||||
continue;
|
||||
if (n > base) { base++; continue; }
|
||||
if (emit(out, cap, &used, DEVFS_EXTRA0 + (uint64)i,
|
||||
extras.e[i].type == T_DIR ? 4 : 10 /* DT_LNK */, extras.e[i].name) < 0)
|
||||
break;
|
||||
n++;
|
||||
base++;
|
||||
}
|
||||
release(&extras.lock);
|
||||
goto out;
|
||||
}
|
||||
|
||||
// /dev/pts: the dots, then the pairs that exist. Ordinal 2 + N is pair N,
|
||||
// whether or not the ones before it exist, so a pair opened between two
|
||||
// calls does not shift the rest.
|
||||
|
|
@ -378,6 +607,21 @@ devfs_getdents(struct inode *dp, char *out, int cap, uint64 *poff)
|
|||
base++;
|
||||
}
|
||||
|
||||
// What udev added to this directory, after everything the kernel put there.
|
||||
extras_init();
|
||||
acquire(&extras.lock);
|
||||
for (int i = 0; i < NEXTRA; i++) {
|
||||
if (!extras.e[i].used || !extras.e[i].linked || extras.e[i].parent != dp->inum)
|
||||
continue;
|
||||
if (n > base) { base++; continue; }
|
||||
if (emit(out, cap, &used, DEVFS_EXTRA0 + (uint64)i,
|
||||
extras.e[i].type == T_DIR ? 4 : 10, extras.e[i].name) < 0)
|
||||
break;
|
||||
n++;
|
||||
base++;
|
||||
}
|
||||
release(&extras.lock);
|
||||
|
||||
out:
|
||||
*poff = n;
|
||||
return used;
|
||||
|
|
|
|||
|
|
@ -113,6 +113,62 @@ sockinit(void)
|
|||
"the socket table, with buffered bytes and descriptors in flight");
|
||||
}
|
||||
|
||||
// SOCK_SEQPACKET and SOCK_DGRAM keep message boundaries. They ride in the same
|
||||
// ring as a stream, each message behind a four-byte length, so a write is one
|
||||
// message and a read takes exactly one -- the rest of a message that does not
|
||||
// fit the buffer is dropped, as Linux drops it. A message is never split, so
|
||||
// one has to fit the ring whole.
|
||||
static int
|
||||
is_packet(struct socket *s)
|
||||
{
|
||||
if(s->domain == AF_NETLINK)
|
||||
return s->nlproto == NETLINK_KOBJECT_UEVENT;
|
||||
return s->domain == AF_UNIX && (s->type == SOCK_DGRAM || s->type == SOCK_SEQPACKET);
|
||||
}
|
||||
|
||||
int
|
||||
sock_is_packet(struct socket *s)
|
||||
{
|
||||
return is_packet(s);
|
||||
}
|
||||
|
||||
// bind(2) on a netlink socket: only the multicast groups mean anything here.
|
||||
int
|
||||
sock_netlink_bind(struct socket *s, uint groups)
|
||||
{
|
||||
acquire(&stab.lock);
|
||||
s->nlgroups = groups;
|
||||
release(&stab.lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Queue one uevent datagram on every uevent socket listening to any of
|
||||
// `groups`, except `from`. A full queue drops the message, as Linux does:
|
||||
// udev recovers with a trigger, and the sender must never wait on a reader.
|
||||
void
|
||||
uevent_send(uint groups, const char *msg, int len, struct socket *from)
|
||||
{
|
||||
struct socket *s;
|
||||
|
||||
if(len <= 0 || len + 4 > SOCKBUF)
|
||||
return;
|
||||
acquire(&stab.lock);
|
||||
for(s = stab.sock; s < stab.sock + NSOCK; s++){
|
||||
if(s->state == SS_FREE || s == from || s->domain != AF_NETLINK ||
|
||||
s->nlproto != NETLINK_KOBJECT_UEVENT || !(s->nlgroups & groups))
|
||||
continue;
|
||||
if(s->nwrite - s->nread + (uint)len + 4 > SOCKBUF)
|
||||
continue;
|
||||
for(int i = 0; i < 4; i++)
|
||||
s->data[s->nwrite++ % SOCKBUF] = (char)((uint)len >> (8 * i));
|
||||
for(int i = 0; i < len; i++)
|
||||
s->data[s->nwrite++ % SOCKBUF] = msg[i];
|
||||
wakeup(&s->nread);
|
||||
}
|
||||
release(&stab.lock);
|
||||
poll_kick();
|
||||
}
|
||||
|
||||
// Caller holds stab.lock.
|
||||
static struct socket *
|
||||
alloc_locked(int type)
|
||||
|
|
@ -415,7 +471,7 @@ sockread_fds(struct socket *s, char *addr, int n, struct file **fout, int *nfout
|
|||
// so an empty buffer means the caller has already drained this dump rather
|
||||
// than that more is coming. Blocking here would hang a getifaddrs() that
|
||||
// asked one question too many.
|
||||
if(s->domain == AF_NETLINK){
|
||||
if(s->domain == AF_NETLINK && !is_packet(s)){
|
||||
acquire(&stab.lock);
|
||||
uint have = s->nwrite - s->nread;
|
||||
if(have == 0){
|
||||
|
|
@ -439,6 +495,39 @@ sockread_fds(struct socket *s, char *addr, int n, struct file **fout, int *nfout
|
|||
sleep(&s->nread, &stab.lock);
|
||||
}
|
||||
|
||||
// One message, whole or truncated; descriptors ride with the header.
|
||||
if(is_packet(s) && s->nread != s->nwrite){
|
||||
uint len = 0;
|
||||
for(i = 0; i < 4; i++)
|
||||
len |= (uint)(uchar)s->data[(s->nread + i) % SOCKBUF] << (8 * i);
|
||||
if(s->nscm > 0 && s->scm[0].pos == s->nread){
|
||||
got = s->scm[0].n;
|
||||
for(i = 0; i < got; i++)
|
||||
taken[i] = s->scm[0].f[i];
|
||||
for(i = 1; i < s->nscm; i++)
|
||||
s->scm[i - 1] = s->scm[i];
|
||||
s->nscm--;
|
||||
}
|
||||
s->nread += 4;
|
||||
uint take = len < (uint)n ? len : (uint)n;
|
||||
for(i = 0; i < (int)take; i++)
|
||||
addr[i] = s->data[(s->nread + i) % SOCKBUF];
|
||||
s->nread += len;
|
||||
wakeup(&s->nwrite);
|
||||
release(&stab.lock);
|
||||
poll_kick();
|
||||
if(fout){
|
||||
for(int j = 0; j < got; j++)
|
||||
fout[j] = taken[j];
|
||||
if(nfout)
|
||||
*nfout = got;
|
||||
} else {
|
||||
for(int j = 0; j < got; j++)
|
||||
fileclose(taken[j]);
|
||||
}
|
||||
return (int)take;
|
||||
}
|
||||
|
||||
// A message carrying descriptors is a boundary. This read either starts
|
||||
// exactly on one -- and takes them -- or is cut short so that it stops
|
||||
// before it, leaving the next read to start there.
|
||||
|
|
@ -519,6 +608,11 @@ sockwrite_fds(struct socket *s, char *addr, int n, struct file **f, int nf)
|
|||
// rather than a peer's -- there is no peer. Doing the work here is what lets
|
||||
// the read that follows never block, which matters because musl reads with
|
||||
// MSG_DONTWAIT and treats a short read as the end of the dump.
|
||||
if(s->domain == AF_NETLINK && is_packet(s)){
|
||||
// udevd announces what it did on group 2; the sender does not hear itself.
|
||||
uevent_send(UEVENT_GROUP_UDEV, addr, n, s);
|
||||
return n;
|
||||
}
|
||||
if(s->domain == AF_NETLINK){
|
||||
acquire(&stab.lock);
|
||||
s->nread = s->nwrite = 0;
|
||||
|
|
@ -548,6 +642,48 @@ sockwrite_fds(struct socket *s, char *addr, int n, struct file **f, int nf)
|
|||
return -ENOBUFS;
|
||||
}
|
||||
|
||||
if(is_packet(s)){
|
||||
if(n + 4 > SOCKBUF){
|
||||
release(&stab.lock);
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
for(;;){
|
||||
dst = s->peer;
|
||||
if(dst == 0){
|
||||
release(&stab.lock);
|
||||
sigsend(myproc()->pid, SIGPIPE);
|
||||
return -EPIPE;
|
||||
}
|
||||
if(dst->nwrite - dst->nread + (uint)n + 4 <= SOCKBUF)
|
||||
break;
|
||||
if(myproc()->killed){
|
||||
release(&stab.lock);
|
||||
return -EINTR;
|
||||
}
|
||||
wakeup(&dst->nread);
|
||||
sleep(&dst->nwrite, &stab.lock);
|
||||
}
|
||||
if(nf > 0){
|
||||
if(dst->nscm >= NSCM){
|
||||
release(&stab.lock);
|
||||
return -ENOBUFS;
|
||||
}
|
||||
struct scm_fds *b = &dst->scm[dst->nscm++];
|
||||
b->pos = dst->nwrite; // the header, where a packet read starts
|
||||
b->n = nf;
|
||||
for(int j = 0; j < nf; j++)
|
||||
b->f[j] = f[j];
|
||||
}
|
||||
for(i = 0; i < 4; i++)
|
||||
dst->data[dst->nwrite++ % SOCKBUF] = (char)((uint)n >> (8 * i));
|
||||
for(i = 0; i < n; i++)
|
||||
dst->data[dst->nwrite++ % SOCKBUF] = addr[i];
|
||||
wakeup(&dst->nread);
|
||||
release(&stab.lock);
|
||||
poll_kick();
|
||||
return n;
|
||||
}
|
||||
|
||||
for(i = 0; i < n; i++){
|
||||
// Re-read every iteration: the peer can go away while we sleep below.
|
||||
dst = s->peer;
|
||||
|
|
@ -607,9 +743,11 @@ socknew(int domain, int type, int proto)
|
|||
|
||||
if(domain != AF_UNIX && domain != AF_INET && domain != AF_NETLINK)
|
||||
return 0;
|
||||
// AF_UNIX is stream-only here; AF_INET has datagrams too, and inet_new is
|
||||
// AF_UNIX: streams, and message-preserving SEQPACKET and DGRAM (connected
|
||||
// pairs and listeners; an unconnected DGRAM has no address to send to and
|
||||
// will be refused when it tries). AF_INET has datagrams too, and inet_new is
|
||||
// what decides whether it can serve the type asked for.
|
||||
if(domain == AF_UNIX && type != SOCK_STREAM)
|
||||
if(domain == AF_UNIX && type != SOCK_STREAM && type != SOCK_DGRAM && type != SOCK_SEQPACKET)
|
||||
return 0;
|
||||
// AF_NETLINK carries whole messages and musl asks for SOCK_RAW; SOCK_DGRAM is
|
||||
// accepted too because Linux does. NETLINK_ROUTE is the only protocol served
|
||||
|
|
@ -618,14 +756,16 @@ socknew(int domain, int type, int proto)
|
|||
if(domain == AF_NETLINK){
|
||||
if(type != SOCK_RAW && type != SOCK_DGRAM)
|
||||
return 0;
|
||||
if(proto != NETLINK_ROUTE)
|
||||
if(proto != NETLINK_ROUTE && proto != NETLINK_KOBJECT_UEVENT)
|
||||
return 0;
|
||||
}
|
||||
|
||||
acquire(&stab.lock);
|
||||
s = alloc_locked(type);
|
||||
if(s)
|
||||
if(s){
|
||||
s->domain = domain;
|
||||
s->nlproto = domain == AF_NETLINK ? proto : 0;
|
||||
}
|
||||
release(&stab.lock);
|
||||
if(s == 0)
|
||||
return 0;
|
||||
|
|
@ -644,16 +784,16 @@ socknew(int domain, int type, int proto)
|
|||
|
||||
// A connected pair with no name and no listener, for socketpair(2).
|
||||
int
|
||||
sockpair(struct socket **pa, struct socket **pb)
|
||||
sockpair_type(struct socket **pa, struct socket **pb, int type)
|
||||
{
|
||||
struct socket *a, *b;
|
||||
|
||||
acquire(&stab.lock);
|
||||
if((a = alloc_locked(SOCK_STREAM)) == 0){
|
||||
if((a = alloc_locked(type)) == 0){
|
||||
release(&stab.lock);
|
||||
return -ENFILE;
|
||||
}
|
||||
if((b = alloc_locked(SOCK_STREAM)) == 0){
|
||||
if((b = alloc_locked(type)) == 0){
|
||||
a->state = SS_FREE;
|
||||
a->ref = 0;
|
||||
release(&stab.lock);
|
||||
|
|
@ -674,6 +814,12 @@ sockpair(struct socket **pa, struct socket **pb)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sockpair(struct socket **pa, struct socket **pb)
|
||||
{
|
||||
return sockpair_type(pa, pb, SOCK_STREAM);
|
||||
}
|
||||
|
||||
// bind(2) for AF_UNIX. The caller has already created the filesystem node and
|
||||
// hands over its identity; see sys_bind() in kernel/sys/sys_net.c for why the
|
||||
// creation lives there and not here.
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ SYSCALL_DEFINE(socket)
|
|||
if (type & ~(0xFF | SOCK_CLOEXEC | SOCK_NONBLOCK))
|
||||
return -EINVAL;
|
||||
type &= 0xFF;
|
||||
if (domain == AF_UNIX && type != SOCK_STREAM)
|
||||
if (domain == AF_UNIX && type != SOCK_STREAM && type != SOCK_DGRAM && type != SOCK_SEQPACKET)
|
||||
return -EPROTOTYPE;
|
||||
if (domain == AF_INET && type != SOCK_STREAM && type != SOCK_DGRAM &&
|
||||
type != SOCK_RAW)
|
||||
|
|
@ -185,7 +185,7 @@ SYSCALL_DEFINE(socket)
|
|||
// default rather than departing from it, and programs do write them out:
|
||||
// toybox's `nc` passes IPPROTO_TCP, and refusing it meant "Protocol not
|
||||
// supported" for TCP on a system whose whole network is TCP.
|
||||
if (protocol != 0) {
|
||||
if (protocol != 0 && domain != AF_NETLINK) {
|
||||
int redundant =
|
||||
(domain == AF_INET && type == SOCK_STREAM && protocol == IPPROTO_TCP) ||
|
||||
(domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP);
|
||||
|
|
@ -270,6 +270,16 @@ SYSCALL_DEFINE(bind)
|
|||
return sockbind_in(s, ip, port_from_net(port));
|
||||
}
|
||||
|
||||
// sockaddr_nl: family, pad, port id, groups. Only the groups matter here.
|
||||
if (sock_domain(s) == AF_NETLINK) {
|
||||
uint groups = 0;
|
||||
if (ulen < 12)
|
||||
return -EINVAL;
|
||||
if (copy_from_user(&groups, uaddr + 8, 4) < 0)
|
||||
return -EFAULT;
|
||||
return sock_netlink_bind(s, groups);
|
||||
}
|
||||
|
||||
uint64 key;
|
||||
if (sun_abstract(uaddr, ulen, &key))
|
||||
return sockbind_abstract(s, key);
|
||||
|
|
@ -459,15 +469,11 @@ SYSCALL_DEFINE(socketpair)
|
|||
return -EAFNOSUPPORT;
|
||||
int sflags = type & (SOCK_CLOEXEC | SOCK_NONBLOCK);
|
||||
type &= 0xFF;
|
||||
// Datagram and sequenced-packet pairs are carried as a stream. What they
|
||||
// are used for between two processes is passing a descriptor or a status
|
||||
// word -- Rust's process spawning asks for SOCK_SEQPACKET to hear back
|
||||
// from its child -- and one message at a time is all of that. A reader
|
||||
// that sends two and wants them kept apart would see them joined.
|
||||
if (type != SOCK_STREAM && type != 2 /* DGRAM */ && type != 5 /* SEQPACKET */)
|
||||
// Datagram and sequenced-packet pairs keep message boundaries (kernel/net/unix.c).
|
||||
if (type != SOCK_STREAM && type != SOCK_DGRAM && type != SOCK_SEQPACKET)
|
||||
return -EPROTOTYPE;
|
||||
|
||||
if ((r = sockpair(&a, &b)) < 0)
|
||||
if ((r = sockpair_type(&a, &b, type)) < 0)
|
||||
return r;
|
||||
|
||||
if ((fd0 = sock_to_fd(a, sflags)) < 0) {
|
||||
|
|
@ -1076,7 +1082,7 @@ SYSCALL_DEFINE(recvmsg)
|
|||
// one rather than discarding it as Linux would: musl's getifaddrs reads
|
||||
// in chunks, and this whole queue is one "datagram" only by convention.
|
||||
int nlret = -1;
|
||||
if (sock_domain(s) == AF_NETLINK) {
|
||||
if (sock_domain(s) == AF_NETLINK && !sock_is_packet(s)) {
|
||||
uint have = socknl_pending(s);
|
||||
if (have == 0)
|
||||
return -EAGAIN; // never blocks; see sockread
|
||||
|
|
@ -1127,8 +1133,8 @@ SYSCALL_DEFINE(recvmsg)
|
|||
return rd;
|
||||
}
|
||||
got += rd;
|
||||
if (rd < len)
|
||||
break;
|
||||
if (rd < len || sock_is_packet(s))
|
||||
break; // one message per call
|
||||
}
|
||||
|
||||
if (nf > 0) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue