aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncaen <mail@duncano.de>2017-03-06 19:26:58 +0100
committerDuncaen <mail@duncano.de>2017-03-06 20:34:38 +0100
commite6797f725c947f5c02fca20c20e4ab5aba86c251 (patch)
tree42c30ad123eece664b08bae8ec8eaa35889c28ac
parent457ee41c30e286383f21633b45cc99327a00216d (diff)
downloadlobase-e6797f725c947f5c02fca20c20e4ab5aba86c251.tar.gz
lib/libopenbsd: add getdtablecount(2) for linux
-rw-r--r--include/unistd.h1
-rw-r--r--lib/libopenbsd/sys/getdtablecount.238
-rw-r--r--lib/libopenbsd/sys/getdtablecount.c25
3 files changed, 64 insertions, 0 deletions
diff --git a/include/unistd.h b/include/unistd.h
index f2be493..061192e 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -43,6 +43,7 @@
int execvpe(const char *, char *const *, char *const *);
int closefrom(int);
+int getdtablecount(void);
int getentropy(void *, size_t);
mode_t getmode(const void *, mode_t);
int getresgid(gid_t *, gid_t *, gid_t *);
diff --git a/lib/libopenbsd/sys/getdtablecount.2 b/lib/libopenbsd/sys/getdtablecount.2
new file mode 100644
index 0000000..311c52a
--- /dev/null
+++ b/lib/libopenbsd/sys/getdtablecount.2
@@ -0,0 +1,38 @@
+.\" $OpenBSD: getdtablecount.2,v 1.5 2015/05/31 23:54:25 schwarze Exp $
+.\"
+.\" Copyright (c) 2012 Theo de Raadt
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: May 31 2015 $
+.Dt GETDTABLECOUNT 2
+.Os
+.Sh NAME
+.Nm getdtablecount
+.Nd get descriptor table count
+.Sh SYNOPSIS
+.In unistd.h
+.Ft int
+.Fn getdtablecount void
+.Sh DESCRIPTION
+.Fn getdtablecount
+returns the number of file descriptors the process currently has
+open.
+.Sh SEE ALSO
+.Xr getrlimit 2 ,
+.Xr getdtablesize 3
+.Sh HISTORY
+The
+.Fn getdtablecount
+function appeared in
+.Ox 5.2 .
diff --git a/lib/libopenbsd/sys/getdtablecount.c b/lib/libopenbsd/sys/getdtablecount.c
new file mode 100644
index 0000000..1c710af
--- /dev/null
+++ b/lib/libopenbsd/sys/getdtablecount.c
@@ -0,0 +1,25 @@
+#include <sys/types.h>
+#include <dirent.h>
+
+#ifdef __linux__
+int
+getdtablecount(void)
+{
+ struct dirent *dp;
+ DIR *dir;
+ int n;
+
+ /* XXX: return something different? */
+ if (!(dir = opendir("/proc/self/fd")))
+ return 0;
+
+ n = 0;
+ while ((dp = readdir(dir)))
+ n++;
+
+ closedir(dir);
+ return n;
+}
+#else
+#error getdtablecount(2) not supported
+#endif