The branch stable/14 has been updated by kib:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=add51cc280f3a48abb2f4fc2e2e4f56efc186d54

commit add51cc280f3a48abb2f4fc2e2e4f56efc186d54
Author:     Konstantin Belousov <k...@freebsd.org>
AuthorDate: 2024-01-19 19:49:36 +0000
Commit:     Konstantin Belousov <k...@freebsd.org>
CommitDate: 2024-01-30 20:24:42 +0000

    Add kcmp(2) kernel bits
    
    (cherry picked from commit d8decc9ae31af7ffc77276c89639fb13eb1020cc)
---
 sys/kern/sys_generic.c   | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
 sys/kern/syscalls.master | 10 +++++-
 sys/sys/syscallsubr.h    |  2 ++
 sys/sys/systm.h          |  2 ++
 sys/sys/unistd.h         |  7 ++++
 5 files changed, 107 insertions(+), 1 deletion(-)

diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c
index 961bfeaf2bad..b3aaa2956d6f 100644
--- a/sys/kern/sys_generic.c
+++ b/sys/kern/sys_generic.c
@@ -67,6 +67,7 @@
 #include <sys/sysctl.h>
 #include <sys/sysent.h>
 #include <sys/vnode.h>
+#include <sys/unistd.h>
 #include <sys/bio.h>
 #include <sys/buf.h>
 #include <sys/condvar.h>
@@ -2073,3 +2074,89 @@ kern_posix_error(struct thread *td, int error)
        td->td_retval[0] = error;
        return (0);
 }
+
+int
+kcmp_cmp(uintptr_t a, uintptr_t b)
+{
+       if (a == b)
+               return (0);
+       else if (a < b)
+               return (1);
+       return (2);
+}
+
+static int
+kcmp_pget(struct thread *td, pid_t pid, struct proc **pp)
+{
+       if (pid == td->td_proc->p_pid) {
+               *pp = td->td_proc;
+               return (0);
+       }
+       return (pget(pid, PGET_CANDEBUG | PGET_NOTWEXIT | PGET_HOLD, pp));
+}
+
+int
+kern_kcmp(struct thread *td, pid_t pid1, pid_t pid2, int type,
+    uintptr_t idx1, uintptr_t idx2)
+{
+       struct proc *p1, *p2;
+       struct file *fp1, *fp2;
+       int error, res;
+
+       res = -1;
+       p1 = p2 = NULL;
+       error = kcmp_pget(td, pid1, &p1);
+       if (error == 0)
+               error = kcmp_pget(td, pid2, &p2);
+       if (error != 0)
+               goto out;
+
+       switch (type) {
+       case KCMP_FILE:
+       case KCMP_FILEOBJ:
+               error = fget_remote(td, p1, idx1, &fp1);
+               if (error == 0) {
+                       error = fget_remote(td, p2, idx2, &fp2);
+                       if (error == 0) {
+                               if (type == KCMP_FILEOBJ)
+                                       res = fo_cmp(fp1, fp2, td);
+                               else
+                                       res = kcmp_cmp((uintptr_t)fp1,
+                                           (uintptr_t)fp2);
+                               fdrop(fp2, td);
+                       }
+                       fdrop(fp1, td);
+               }
+               break;
+       case KCMP_FILES:
+               res = kcmp_cmp((uintptr_t)p1->p_fd, (uintptr_t)p2->p_fd);
+               break;
+       case KCMP_SIGHAND:
+               res = kcmp_cmp((uintptr_t)p1->p_sigacts,
+                   (uintptr_t)p2->p_sigacts);
+               break;
+       case KCMP_VM:
+               res = kcmp_cmp((uintptr_t)p1->p_vmspace,
+                   (uintptr_t)p2->p_vmspace);
+               break;
+       default:
+               error = EINVAL;
+               break;
+       }
+
+out:
+       if (p1 != NULL && p1 != td->td_proc)
+               PRELE(p1);
+       if (p2 != NULL && p2 != td->td_proc)
+               PRELE(p2);
+
+       td->td_retval[0] = res;
+       return (error);
+}
+
+int
+sys_kcmp(struct thread *td, struct kcmp_args *uap)
+{
+       return (kern_kcmp(td, uap->pid1, uap->pid2, uap->type,
+           uap->idx1, uap->idx2));
+}
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index f7c235ced26c..a50d7723378c 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -3338,6 +3338,14 @@
                    _Out_opt_ _Contains_long_timet_ struct itimerspec *old_value
                );
        }
-
+588    AUE_NULL        STD {
+               int kcmp(
+                   pid_t pid1,
+                   pid_t pid2,
+                   int type,
+                   uintptr_t idx1,
+                   uintptr_t idx2
+               );
+       }
 
 ; vim: syntax=off
diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h
index 15e05985e75f..29c7c364ccf6 100644
--- a/sys/sys/syscallsubr.h
+++ b/sys/sys/syscallsubr.h
@@ -200,6 +200,8 @@ int kern_ioctl(struct thread *td, int fd, u_long com, 
caddr_t data);
 int    kern_jail(struct thread *td, struct jail *j);
 int    kern_jail_get(struct thread *td, struct uio *options, int flags);
 int    kern_jail_set(struct thread *td, struct uio *options, int flags);
+int    kern_kcmp(struct thread *td, pid_t pid1, pid_t pid2, int type,
+           uintptr_t idx1, uintptr_t idx2);
 int    kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
            struct kevent_copyops *k_ops, const struct timespec *timeout);
 int    kern_kevent_anonymous(struct thread *td, int nevents,
diff --git a/sys/sys/systm.h b/sys/sys/systm.h
index 0e90b6062b72..9a0932e50dc9 100644
--- a/sys/sys/systm.h
+++ b/sys/sys/systm.h
@@ -487,6 +487,8 @@ int poll_no_poll(int events);
 /* XXX: Should be void nanodelay(u_int nsec); */
 void   DELAY(int usec);
 
+int kcmp_cmp(uintptr_t a, uintptr_t b);
+
 /* Root mount holdback API */
 struct root_hold_token {
        int                             flags;
diff --git a/sys/sys/unistd.h b/sys/sys/unistd.h
index 77df90cd26b1..1b9262c75af4 100644
--- a/sys/sys/unistd.h
+++ b/sys/sys/unistd.h
@@ -197,6 +197,13 @@
     RFPROCDESC | RFSPAWN | RFPPWAIT)
 #define        RFKERNELONLY    (RFSTOPPED | RFHIGHPID | RFPROCDESC)
 
+/* kcmp() options. */
+#define        KCMP_FILE       100
+#define        KCMP_FILEOBJ    101
+#define        KCMP_FILES      102
+#define        KCMP_SIGHAND    103
+#define        KCMP_VM         104
+
 #define        SWAPOFF_FORCE   0x00000001
 
 /*

Reply via email to