Author: mjg
Date: Sat Nov  7 00:18:14 2015
New Revision: 290473
URL: https://svnweb.freebsd.org/changeset/base/290473

Log:
  fd: implement kern.proc.nfds sysctl
  
  Intended purpose is to provide an equivalent of OpenBSD's getdtablecount
  syscall for the compat library..

Modified:
  head/sys/kern/kern_descrip.c
  head/sys/sys/sysctl.h

Modified: head/sys/kern/kern_descrip.c
==============================================================================
--- head/sys/kern/kern_descrip.c        Fri Nov  6 23:17:00 2015        
(r290472)
+++ head/sys/kern/kern_descrip.c        Sat Nov  7 00:18:14 2015        
(r290473)
@@ -3157,6 +3157,30 @@ filedesc_to_leader_alloc(struct filedesc
        return (fdtol);
 }
 
+static int
+sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS)
+{
+       struct filedesc *fdp;
+       int i, count, slots;
+
+       if (*(int *)arg1 != 0)
+               return (EINVAL);
+
+       fdp = curproc->p_fd;
+       count = 0;
+       FILEDESC_SLOCK(fdp);
+       slots = NDSLOTS(fdp->fd_lastfile + 1);
+       for (i = 0; i < slots; i++)
+               count += bitcountl(fdp->fd_map[i]);
+       FILEDESC_SUNLOCK(fdp);
+
+       return (SYSCTL_OUT(req, &count, sizeof(count)));
+}
+
+static SYSCTL_NODE(_kern_proc, KERN_PROC_NFDS, nfds,
+    CTLFLAG_RD|CTLFLAG_MPSAFE, sysctl_kern_proc_nfds,
+    "Number of open file descriptors");
+
 /*
  * Get file structures globally.
  */

Modified: head/sys/sys/sysctl.h
==============================================================================
--- head/sys/sys/sysctl.h       Fri Nov  6 23:17:00 2015        (r290472)
+++ head/sys/sys/sysctl.h       Sat Nov  7 00:18:14 2015        (r290473)
@@ -703,6 +703,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_e
 #define        KERN_PROC_OSREL         40      /* osreldate for process binary 
*/
 #define        KERN_PROC_SIGTRAMP      41      /* signal trampoline location */
 #define        KERN_PROC_CWD           42      /* process current working 
directory */
+#define        KERN_PROC_NFDS          43      /* number of open file 
descriptors */
 
 /*
  * KERN_IPC identifiers
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "[email protected]"

Reply via email to