git: 8c309d48aabf - main - struct kinfo_file changes needed for lsof to work using only usermode APIs`

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 18 Jun 2022 09:34:54 UTC
The branch main has been updated by kib:

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

commit 8c309d48aabf1cb469334c7716033f177a2715c0
Author:     Damjan Jovanovic <damjan.jov@gmail.com>
AuthorDate: 2022-06-17 13:37:40 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-06-18 09:34:25 +0000

    struct kinfo_file changes needed for lsof to work using only usermode APIs`
    
    Add kf_pipe_buffer_[in/out/size] fields to kf_pipe, and populate them.
    
    Add a kf_kqueue struct to the kf_un union, to allow querying kqueue state,
    and populate it.
    
    Populate the kf_sock_rcv_sb_state and kf_sock_snd_sb_state fields in
    kf_sock for INET/INET6 sockets, and populate all other fields for all
    transport layer protocols, not just TCP.
    
    Bump __FreeBSD_version.
    
    Differential revision:  https://reviews.freebsd.org/D34184
    Reviewed by:    jhb, kib, se
    MFC after:      1 week
---
 sys/kern/kern_event.c  |  4 ++++
 sys/kern/sys_eventfd.c |  1 +
 sys/kern/sys_pipe.c    |  3 +++
 sys/kern/sys_socket.c  | 22 ++++++++++++----------
 sys/sys/param.h        |  2 +-
 sys/sys/user.h         | 12 ++++++++++--
 6 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index f1ee8de4e7ee..4b4522704ad0 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -2324,8 +2324,12 @@ kqueue_close(struct file *fp, struct thread *td)
 static int
 kqueue_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
 {
+	struct kqueue *kq = fp->f_data;
 
 	kif->kf_type = KF_TYPE_KQUEUE;
+	kif->kf_un.kf_kqueue.kf_kqueue_addr = (uintptr_t)kq;
+	kif->kf_un.kf_kqueue.kf_kqueue_count = kq->kq_count;
+	kif->kf_un.kf_kqueue.kf_kqueue_state = kq->kq_state;
 	return (0);
 }
 
diff --git a/sys/kern/sys_eventfd.c b/sys/kern/sys_eventfd.c
index 8ee8421296e5..10566b486f30 100644
--- a/sys/kern/sys_eventfd.c
+++ b/sys/kern/sys_eventfd.c
@@ -341,6 +341,7 @@ eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp
 	mtx_lock(&efd->efd_lock);
 	kif->kf_un.kf_eventfd.kf_eventfd_value = efd->efd_count;
 	kif->kf_un.kf_eventfd.kf_eventfd_flags = efd->efd_flags;
+	kif->kf_un.kf_eventfd.kf_eventfd_addr = (uintptr_t)efd;
 	mtx_unlock(&efd->efd_lock);
 	return (0);
 }
diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c
index ed93a2b7f094..d9e7d97d01e1 100644
--- a/sys/kern/sys_pipe.c
+++ b/sys/kern/sys_pipe.c
@@ -1613,6 +1613,9 @@ pipe_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
 	kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
 	kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
 	kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
+	kif->kf_un.kf_pipe.kf_pipe_buffer_in = pi->pipe_buffer.in;
+	kif->kf_un.kf_pipe.kf_pipe_buffer_out = pi->pipe_buffer.out;
+	kif->kf_un.kf_pipe.kf_pipe_buffer_size = pi->pipe_buffer.size;
 	return (0);
 }
 
diff --git a/sys/kern/sys_socket.c b/sys/kern/sys_socket.c
index 859c6d593da0..7f39e58f4ce4 100644
--- a/sys/kern/sys_socket.c
+++ b/sys/kern/sys_socket.c
@@ -382,17 +382,19 @@ soo_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
 	switch (kif->kf_un.kf_sock.kf_sock_domain0) {
 	case AF_INET:
 	case AF_INET6:
-		if (kif->kf_un.kf_sock.kf_sock_protocol0 == IPPROTO_TCP) {
-			if (so->so_pcb != NULL) {
-				inpcb = (struct inpcb *)(so->so_pcb);
-				kif->kf_un.kf_sock.kf_sock_inpcb =
-				    (uintptr_t)inpcb->inp_ppcb;
-				kif->kf_un.kf_sock.kf_sock_sendq =
-				    sbused(&so->so_snd);
-				kif->kf_un.kf_sock.kf_sock_recvq =
-				    sbused(&so->so_rcv);
-			}
+		if (so->so_pcb != NULL) {
+			inpcb = (struct inpcb *)(so->so_pcb);
+			kif->kf_un.kf_sock.kf_sock_inpcb =
+			    (uintptr_t)inpcb->inp_ppcb;
 		}
+		kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
+		    so->so_rcv.sb_state;
+		kif->kf_un.kf_sock.kf_sock_snd_sb_state =
+		    so->so_snd.sb_state;
+		kif->kf_un.kf_sock.kf_sock_sendq =
+		    sbused(&so->so_snd);
+		kif->kf_un.kf_sock.kf_sock_recvq =
+		    sbused(&so->so_rcv);
 		break;
 	case AF_UNIX:
 		if (so->so_pcb != NULL) {
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 963f7f89c930..d8110f08a385 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -76,7 +76,7 @@
  * cannot include sys/param.h and should only be updated here.
  */
 #undef __FreeBSD_version
-#define __FreeBSD_version 1400061
+#define __FreeBSD_version 1400062
 
 /*
  * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,
diff --git a/sys/sys/user.h b/sys/sys/user.h
index cf64f8e8eda4..d3257ffdf0f0 100644
--- a/sys/sys/user.h
+++ b/sys/sys/user.h
@@ -420,8 +420,9 @@ struct kinfo_file {
 				uint64_t	kf_pipe_addr;
 				uint64_t	kf_pipe_peer;
 				uint32_t	kf_pipe_buffer_cnt;
-				/* Round to 64 bit alignment. */
-				uint32_t	kf_pipe_pad0[3];
+				uint32_t	kf_pipe_buffer_in;
+				uint32_t	kf_pipe_buffer_out;
+				uint32_t	kf_pipe_buffer_size;
 			} kf_pipe;
 			struct {
 				uint32_t	kf_spareint[4];
@@ -440,7 +441,14 @@ struct kinfo_file {
 			struct {
 				uint64_t	kf_eventfd_value;
 				uint32_t	kf_eventfd_flags;
+				uint32_t	kf_eventfd_spareint[3];
+				uint64_t	kf_eventfd_addr;
 			} kf_eventfd;
+			struct {
+				uint64_t	kf_kqueue_addr;
+				int32_t		kf_kqueue_count;
+				int32_t		kf_kqueue_state;
+			} kf_kqueue;
 		} kf_un;
 	};
 	uint16_t	kf_status;		/* Status flags. */