git: 78382caa3edb - main - kern/kern_event.c: extract kern_kqueue_alloc() from kern_kqueue()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sat, 18 Oct 2025 05:14:12 UTC
The branch main has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=78382caa3edbea4c3d3aad2b47ff9db3d77fc978
commit 78382caa3edbea4c3d3aad2b47ff9db3d77fc978
Author: Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-08-19 04:32:43 +0000
Commit: Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-10-18 05:12:36 +0000
kern/kern_event.c: extract kern_kqueue_alloc() from kern_kqueue()
The new helper function allows to allocate a kqueue and its file,
without also allocating file descriptor.
Reviewed by: markj
Tested by: pho
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks
Differential revision: https://reviews.freebsd.org/D52045
---
sys/kern/kern_event.c | 34 +++++++++++++++++++++++++---------
1 file changed, 25 insertions(+), 9 deletions(-)
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index 57b6b75cb848..5435f9702d06 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -1177,28 +1177,28 @@ kqueue_init(struct kqueue *kq)
TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
}
-int
-kern_kqueue(struct thread *td, int flags, struct filecaps *fcaps)
+static int
+kern_kqueue_alloc(struct thread *td, struct filedesc *fdp, int *fdip,
+ struct file **fpp, int flags, struct filecaps *fcaps,
+ struct kqueue **kqp)
{
- struct filedesc *fdp;
- struct kqueue *kq;
- struct file *fp;
struct ucred *cred;
- int fd, error;
+ struct kqueue *kq;
+ int error;
- fdp = td->td_proc->p_fd;
cred = td->td_ucred;
if (!chgkqcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_KQUEUES)))
return (ENOMEM);
- error = falloc_caps(td, &fp, &fd, flags, fcaps);
+ error = fdip != NULL ? falloc_caps(td, fpp, fdip, flags, fcaps) :
+ _falloc_noinstall(td, fpp, 2);
if (error != 0) {
chgkqcnt(cred->cr_ruidinfo, -1, 0);
return (error);
}
/* An extra reference on `fp' has been held for us by falloc(). */
- kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
+ kq = malloc(sizeof(*kq), M_KQUEUE, M_WAITOK | M_ZERO);
kqueue_init(kq);
kq->kq_fdp = fdp;
kq->kq_cred = crhold(cred);
@@ -1207,6 +1207,22 @@ kern_kqueue(struct thread *td, int flags, struct filecaps *fcaps)
TAILQ_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
FILEDESC_XUNLOCK(fdp);
+ *kqp = kq;
+ return (0);
+}
+
+int
+kern_kqueue(struct thread *td, int flags, struct filecaps *fcaps)
+{
+ struct kqueue *kq;
+ struct file *fp;
+ int fd, error;
+
+ error = kern_kqueue_alloc(td, td->td_proc->p_fd, &fd, &fp, flags,
+ fcaps, &kq);
+ if (error != 0)
+ return (error);
+
finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
fdrop(fp, td);