lobase

Linux port of OpenBSDs userland.
Log | Files | Refs | README

commit e6797f725c947f5c02fca20c20e4ab5aba86c251
parent 457ee41c30e286383f21633b45cc99327a00216d
Author: Duncaen <mail@duncano.de>
Date:   Mon,  6 Mar 2017 19:26:58 +0100

lib/libopenbsd: add getdtablecount(2) for linux

Diffstat:
include/unistd.h | 1+
lib/libopenbsd/sys/getdtablecount.2 | 38++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/sys/getdtablecount.c | 25+++++++++++++++++++++++++
3 files changed, 64 insertions(+), 0 deletions(-)

diff --git 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 @@ -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 @@ -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