From bd0933a19b0da1ec6d70ba842c9f036792c40f8d Mon Sep 17 00:00:00 2001 From: Erik van der Kouwe Date: Mon, 7 Dec 2009 19:56:40 +0000 Subject: [PATCH] Implementation of getrlimit and getdtablesize --- include/sys/resource.h | 28 ++++++++++++++++++++ include/unistd.h | 4 +++ lib/other/Makefile.in | 1 + lib/other/rlimit.c | 55 ++++++++++++++++++++++++++++++++++++++++ man/man3/getdtablesize.3 | 16 ++++++++++++ man/man3/getrlimit.3 | 23 +++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 lib/other/rlimit.c create mode 100644 man/man3/getdtablesize.3 create mode 100644 man/man3/getrlimit.3 diff --git a/include/sys/resource.h b/include/sys/resource.h index 6dc5bd869..465b6ea95 100644 --- a/include/sys/resource.h +++ b/include/sys/resource.h @@ -15,4 +15,32 @@ int getpriority(int, int); int setpriority(int, int, int); +#ifdef _POSIX_SOURCE + +#include + +typedef unsigned long rlim_t; + +#define RLIM_INFINITY ((rlim_t) -1) +#define RLIM_SAVED_CUR RLIM_INFINITY +#define RLIM_SAVED_MAX RLIM_INFINITY + +struct rlimit +{ + rlim_t rlim_cur; + rlim_t rlim_max; +}; + +#define RLIMIT_CORE 1 +#define RLIMIT_CPU 2 +#define RLIMIT_DATA 3 +#define RLIMIT_FSIZE 4 +#define RLIMIT_NOFILE 5 +#define RLIMIT_STACK 6 +#define RLIMIT_AS 7 + +int getrlimit(int resource, struct rlimit *rlp); + +#endif /* defined(_POSIX_SOURCE) */ + #endif diff --git a/include/unistd.h b/include/unistd.h index 156742430..3d129bfeb 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -216,4 +216,8 @@ _PROTOTYPE( int initgroups, (const char *name, gid_t basegid) ); #endif +#ifdef _POSIX_SOURCE +_PROTOTYPE( int getdtablesize, (void) ); +#endif + #endif /* _UNISTD_H */ diff --git a/lib/other/Makefile.in b/lib/other/Makefile.in index 6d84c8570..6f191121b 100644 --- a/lib/other/Makefile.in +++ b/lib/other/Makefile.in @@ -77,6 +77,7 @@ libc_FILES=" \ random.c \ realpath.c \ rindex.c \ + rlimit.c \ setenv.c \ setgroups.c \ settimeofday.c \ diff --git a/lib/other/rlimit.c b/lib/other/rlimit.c new file mode 100644 index 000000000..77fd8c2de --- /dev/null +++ b/lib/other/rlimit.c @@ -0,0 +1,55 @@ +/* getdtablesize, getrlimit Author: Erik van der Kouwe + * query resource consumtion limits 4 December 2009 + * + * Based on these specifications: + * http://www.opengroup.org/onlinepubs/007908775/xsh/getdtablesize.html + * http://www.opengroup.org/onlinepubs/007908775/xsh/getrlimit.html + */ + +#include +#include +#include +#include + +int getdtablesize(void) +{ + return OPEN_MAX; +} + +int getrlimit(int resource, struct rlimit *rlp) +{ + rlim_t limit; + + switch (resource) + { + case RLIMIT_CORE: + /* no core currently produced */ + limit = 0; + break; + + case RLIMIT_CPU: + case RLIMIT_DATA: + case RLIMIT_FSIZE: + case RLIMIT_STACK: + case RLIMIT_AS: + /* no limit enforced (however architectural limits + * may apply) + */ + limit = RLIM_INFINITY; + break; + + case RLIMIT_NOFILE: + limit = OPEN_MAX; + break; + + default: + errno = EINVAL; + return -1; + } + + /* return limit */ + rlp->rlim_cur = limit; + rlp->rlim_max = limit; + return 0; +} + diff --git a/man/man3/getdtablesize.3 b/man/man3/getdtablesize.3 new file mode 100644 index 000000000..9e5d24bd6 --- /dev/null +++ b/man/man3/getdtablesize.3 @@ -0,0 +1,16 @@ +.TH GETDTABLESIZE 3 "December 4, 2009" +.UC 4 +.SH NAME +getdtablesize \- query maximum number of open files +.SH SYNOPSIS +.nf +.ft B +#include + +int getdtablesize(void); +.fi +.SH DESCRIPTION +getdtablesize returns the number of files that may be open at the same time +in a process. +.SH "RETURN VALUE +The number of files that may be open at the same time in a process is returned. diff --git a/man/man3/getrlimit.3 b/man/man3/getrlimit.3 new file mode 100644 index 000000000..c2e1e483d --- /dev/null +++ b/man/man3/getrlimit.3 @@ -0,0 +1,23 @@ +.TH GETRLIMIT 3 "December 4, 2009" +.UC 4 +.SH NAME +getrlimit \- query resource consumption limits +.SH SYNOPSIS +.nf +.ft B +#include + +int getrlimit(int \fIresource\fP, struct rlimit *\fIrlp\fP); +.fi +.SH DESCRIPTION +getrlimit queries the current resource limit regarding the specified +\fIresource\fP. This can be one of the following: RLIMIT_CORE, RLIMIT_CPU, +RLIMIT_DATA, RLIMIT_FSIZE, RLIMIT_NOFILE, RLIMIT_STACK or RLIMIT_AS. +The soft limit is specified in \fIrlp\fP\->rlim_cur and the hard limit in +\fIrlp\fP\->rlim_max. On MINIX, these are currently always the same. +A value of RLIM_INFINITY specifies that no limit is enforced, although +architectural limits may still apply. +.SH "RETURN VALUE +If the function succeeds, 0 is returned. +If the function fails, -1 is returned and errno is set to indicate the +cause of the failure.