aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2025-08-19 04:32:43 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2025-10-18 05:12:36 +0000
commit78382caa3edbea4c3d3aad2b47ff9db3d77fc978 (patch)
treee62046f85c978c0cf494add0aeb1a3e31b737154
parent47ed42f3f44ca21210c9d9c56c693248dbca29bb (diff)
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
-rw-r--r--sys/kern/kern_event.c34
1 files 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);