public inbox for astral-devel@lists.astral-os.org
 help / color / mirror / Atom feed
From: Matheus <matheus@astral-os.org>
To: astral-devel@lists.astral-os.org
Subject: [PATCH 4/5] devfs: add anonymous variants to register/remove
Date: Mon,  5 Oct 2026 15:01:51 -0300	[thread overview]
Message-ID: <20261005180152.53359-5-matheus@astral-os.org> (raw)
In-Reply-To: <20261005180152.53359-1-matheus@astral-os.org>

From: Matheus <matheusuky@gmail.com>

The current devfs code requires registered devices to have a name on
creation. This makes temporary/hidden devices like pty master ends
appear for a split second on the devfs tree.

Add a way of registering/removing devfs nodes which do not have a name
on the tree.
---
 kernel-src/fs/devfs.c             | 47 ++++++++++++++++++++++++++++++-
 kernel-src/include/kernel/devfs.h |  7 ++++-
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/kernel-src/fs/devfs.c b/kernel-src/fs/devfs.c
index e71dd3f3..d172aa10 100644
--- a/kernel-src/fs/devfs.c
+++ b/kernel-src/fs/devfs.c
@@ -527,8 +527,41 @@ int devfs_getbyname(char *name, vnode_t **ret) {
 	cleanup:
 	return error;
 }
+
+int devfs_register_anonymous(devops_t *devops, int type, int major, int minor, mode_t mode,
+			     cred_t *cred, vnode_t **out) {
+	__assert(type == V_TYPE_CHDEV || type == V_TYPE_BLKDEV);
+	devnode_t *master = slab_allocate(nodecache);
+	if (master == NULL)
+		return ENOMEM;
+
+	master->vnode.type = type;
+	master->devops = devops;
+	master->attr.mode = mode;
+	master->attr.rdevmajor = major;
+	master->attr.rdevminor = minor;
+	master->attr.uid = cred == NULL ? 0 : cred->euid;
+	master->attr.gid = cred == NULL ? 0 : cred->egid;
+
+	int key[2] = {major, minor};
+
+	MUTEX_ACQUIRE(&tablelock);
+	int error = hashtable_set(&devtable, master, key, sizeof(key), true);
+	MUTEX_RELEASE(&tablelock);
+	if (error) {
+		destroy_node(master);
+		return error;
+	}
+
+	VOP_HOLD(&master->vnode);
+	*out = &master->vnode;
+
+	return 0;
+}
+
 // register new device 
-int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode, cred_t *cred) {
+int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode,
+		   cred_t *cred) {
 	__assert(type == V_TYPE_CHDEV || type == V_TYPE_BLKDEV);
 	devnode_t *master = slab_allocate(nodecache);
 	if (master == NULL)
@@ -613,6 +646,18 @@ int devfs_getnode(vnode_t *physical, int major, int minor, vnode_t **node) {
 	return 0;
 }
 
+void devfs_remove_anonymous(int major, int minor) {
+	int key[2] = {major, minor};
+	void *master;
+	MUTEX_ACQUIRE(&tablelock);
+	__assert(hashtable_get(&devtable, &master, key, sizeof(key)) == 0);
+	__assert(hashtable_remove(&devtable, key, sizeof(key)) == 0);
+	MUTEX_RELEASE(&tablelock);
+
+	vnode_t *vnode = master;
+	VOP_RELEASE(vnode);
+}
+
 void devfs_remove(char *name, int major, int minor) {
 	int key[2] = {major, minor};
 	// remove devtable reference
diff --git a/kernel-src/include/kernel/devfs.h b/kernel-src/include/kernel/devfs.h
index d83b9dee..cfcb2068 100644
--- a/kernel-src/include/kernel/devfs.h
+++ b/kernel-src/include/kernel/devfs.h
@@ -23,6 +23,7 @@
 #define DEV_MAJOR_PROF 14
 #define DEV_MAJOR_INPUT 15
 #define DEV_MAJOR_AUDIO 16
+#define DEV_MAJOR_KCOV 17
 
 typedef struct {
 	int (*open)(int minor, vnode_t **vnode, int flags);
@@ -49,9 +50,13 @@ typedef struct devnode_t {
 
 void devfs_init();
 int devfs_getnode(vnode_t *physical, int major, int minor, vnode_t **node);
-int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode, cred_t *cred);
+int devfs_register_anonymous(devops_t *devops, int type, int major, int minor, mode_t mode,
+			     cred_t *cred, vnode_t **out);
+int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode,
+		   cred_t *cred);
 int devfs_getbyname(char *name, vnode_t **ret);
 int devfs_createdir(char *name);
+void devfs_remove_anonymous(int major, int minor);
 void devfs_remove(char *name, int major, int minor);
 
 #endif
-- 
2.43.0


  parent reply	other threads:[~2026-10-05 18:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
2026-10-05 18:01 ` [PATCH 1/5] statvfs: handle vnodes without a vfs Matheus
2026-10-05 18:01 ` [PATCH 2/5] vfs: fix UAF on VOP_RELEASE Matheus
2026-10-05 18:01 ` [PATCH 3/5] tty: fix lifetime issue in tty_unregister() Matheus
2026-10-05 18:01 ` Matheus [this message]
2026-10-05 18:01 ` [PATCH 5/5] pty: use new anonymous devfs operations Matheus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261005180152.53359-5-matheus@astral-os.org \
    --to=matheus@astral-os.org \
    --cc=astral-devel@lists.astral-os.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox