Index: sys/kern/kern_sysctl.c =================================================================== RCS file: /mount/openbsd/cvs/src/sys/kern/kern_sysctl.c,v diff -u -p -u -p -r1.460 kern_sysctl.c --- sys/kern/kern_sysctl.c 4 Jan 2025 09:26:01 -0000 1.460 +++ sys/kern/kern_sysctl.c 13 Jan 2025 11:35:09 -0000 @@ -1672,9 +1672,10 @@ sysctl_file(int *name, u_int namelen, ch kf = malloc(sizeof(*kf), M_TEMP, M_WAITOK); -#define FILLIT2(fp, fdp, i, vp, pr, so) do { \ +#define FILLIT(fp, fdp, i, vp, pr) do { \ if (buflen >= elem_size && elem_count > 0) { \ - fill_file(kf, fp, fdp, i, vp, pr, p, so, show_pointers);\ + fill_file(kf, fp, fdp, i, vp, pr, p, NULL, \ + show_pointers); \ error = copyout(kf, dp, outsize); \ if (error) \ break; \ @@ -1684,62 +1685,59 @@ sysctl_file(int *name, u_int namelen, ch } \ needed += elem_size; \ } while (0) -#define FILLIT(fp, fdp, i, vp, pr) \ - FILLIT2(fp, fdp, i, vp, pr, NULL) -#define FILLSO(so) \ - FILLIT2(NULL, NULL, 0, NULL, NULL, so) + +#define FILLINPTABLE(table) \ +do { \ + struct inpcb_iterator iter = { .inp_table = NULL }; \ + struct inpcb *inp = NULL; \ + struct socket *so; \ + \ + mtx_enter(&(table)->inpt_mtx); \ + while ((inp = in_pcb_iterator(table, inp, &iter)) != NULL) { \ + if (buflen >= elem_size && elem_count > 0) { \ + mtx_enter(&inp->inp_sofree_mtx); \ + so = soref(inp->inp_socket); \ + mtx_leave(&inp->inp_sofree_mtx); \ + if (so == NULL) \ + continue; \ + mtx_leave(&(table)->inpt_mtx); \ + solock_shared(so); \ + fill_file(kf, NULL, NULL, 0, NULL, NULL, p, \ + so, show_pointers); \ + sounlock_shared(so); \ + sorele(so); \ + error = copyout(kf, dp, outsize); \ + mtx_enter(&(table)->inpt_mtx); \ + if (error) { \ + in_pcb_iterator_abort((table), inp, \ + &iter); \ + break; \ + } \ + dp += elem_size; \ + buflen -= elem_size; \ + elem_count--; \ + } \ + needed += elem_size; \ + } \ + mtx_leave(&(table)->inpt_mtx); \ +} while (0) switch (op) { case KERN_FILE_BYFILE: /* use the inp-tables to pick up closed connections, too */ if (arg == DTYPE_SOCKET) { - struct inpcb *inp; - - NET_LOCK(); - mtx_enter(&tcbtable.inpt_mtx); - TAILQ_FOREACH(inp, &tcbtable.inpt_queue, inp_queue) - FILLSO(inp->inp_socket); - mtx_leave(&tcbtable.inpt_mtx); + FILLINPTABLE(&tcbtable); #ifdef INET6 - mtx_enter(&tcb6table.inpt_mtx); - TAILQ_FOREACH(inp, &tcb6table.inpt_queue, inp_queue) - FILLSO(inp->inp_socket); - mtx_leave(&tcb6table.inpt_mtx); + FILLINPTABLE(&tcb6table); #endif - mtx_enter(&udbtable.inpt_mtx); - TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) { - if (in_pcb_is_iterator(inp)) - continue; - FILLSO(inp->inp_socket); - } - mtx_leave(&udbtable.inpt_mtx); + FILLINPTABLE(&udbtable); #ifdef INET6 - mtx_enter(&udb6table.inpt_mtx); - TAILQ_FOREACH(inp, &udb6table.inpt_queue, inp_queue) { - if (in_pcb_is_iterator(inp)) - continue; - FILLSO(inp->inp_socket); - } - mtx_leave(&udb6table.inpt_mtx); + FILLINPTABLE(&udb6table); #endif - mtx_enter(&rawcbtable.inpt_mtx); - TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) { - if (in_pcb_is_iterator(inp)) - continue; - FILLSO(inp->inp_socket); - } - mtx_leave(&rawcbtable.inpt_mtx); + FILLINPTABLE(&rawcbtable); #ifdef INET6 - mtx_enter(&rawin6pcbtable.inpt_mtx); - TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue, - inp_queue) { - if (in_pcb_is_iterator(inp)) - continue; - FILLSO(inp->inp_socket); - } - mtx_leave(&rawin6pcbtable.inpt_mtx); + FILLINPTABLE(&rawin6pcbtable); #endif - NET_UNLOCK(); } fp = NULL; while ((fp = fd_iterfile(fp, p)) != NULL) {