1290835Srodrigc/*-
2290835Srodrigc * Copyright (c) 2015 Craig Rodrigues
3290835Srodrigc * All rights reserved.
4290835Srodrigc *
5290835Srodrigc * Redistribution and use in source and binary forms, with or without
6290835Srodrigc * modification, are permitted provided that the following conditions
7290835Srodrigc * are met:
8290835Srodrigc * 1. Redistributions of source code must retain the above copyright
9290835Srodrigc *    notice, this list of conditions and the following disclaimer.
10290835Srodrigc * 2. Redistributions in binary form must reproduce the above copyright
11290835Srodrigc *    notice, this list of conditions and the following disclaimer in the
12290835Srodrigc *    documentation and/or other materials provided with the distribution.
13290835Srodrigc *
14290835Srodrigc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15290835Srodrigc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16290835Srodrigc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17290835Srodrigc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18290835Srodrigc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19290835Srodrigc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20290835Srodrigc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21290835Srodrigc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22290835Srodrigc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23290835Srodrigc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24290835Srodrigc * SUCH DAMAGE.
25290835Srodrigc */
26290835Srodrigc
27290835Srodrigc#include <sys/cdefs.h>
28290835Srodrigc__FBSDID("$FreeBSD$");
29290835Srodrigc
30290835Srodrigc#include <sys/types.h>
31290835Srodrigc#include <sys/sysctl.h>
32290835Srodrigc#include <stddef.h>
33290835Srodrigc
34290835Srodrigcint getdtablecount(void);
35290835Srodrigc
36290835Srodrigc/*
37290835Srodrigc * Return the count of open file descriptors for this process.
38290835Srodrigc *
39290835Srodrigc */
40290835Srodrigcint
41290835Srodrigcgetdtablecount(void)
42290835Srodrigc{
43290835Srodrigc	int mib[4];
44290835Srodrigc	int error;
45290835Srodrigc	int nfds;
46290835Srodrigc	size_t len;
47290835Srodrigc
48290835Srodrigc	len = sizeof(nfds);
49290835Srodrigc	mib[0] = CTL_KERN;
50290835Srodrigc	mib[1] = KERN_PROC;
51290835Srodrigc	mib[2] = KERN_PROC_NFDS;
52290835Srodrigc	mib[3] = 0;
53290835Srodrigc
54290835Srodrigc	error = sysctl(mib, 4, &nfds, &len, NULL, 0);
55290835Srodrigc	if (error)
56290835Srodrigc		return (-1);
57290835Srodrigc	return (nfds);
58290835Srodrigc}
59