From 01ec3edeb0c20b7b3278d5022b6715c5629cea0f Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sun, 1 Jun 2014 18:36:56 +0200 Subject: [PATCH] Dynamically obtain the maximum possible value for local file descriptors --- tools/libvpn.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/libvpn.c b/tools/libvpn.c index d7c9e42..adebbec 100644 --- a/tools/libvpn.c +++ b/tools/libvpn.c @@ -15,6 +15,7 @@ +#include #include #include #include @@ -36,12 +37,13 @@ typedef struct _VPNSocket /* constants */ #define PROGNAME "libVPN" -#define VPN_OFFSET 1024 /* variables */ static AppClient * _appclient = NULL; +static int _vpn_offset = 1024; + /* local functions */ static int (*old_connect)(int fd, const struct sockaddr * name, socklen_t namelen); @@ -58,6 +60,9 @@ static void _libvpn_init(void) /* FIXME some symbols may be in libsocket.so instead */ static char * libc[] = { "/lib/libc.so", "/lib/libc.so.6" }; size_t i; +#ifdef RLIMIT_NOFILE + struct rlimit r; +#endif if(hdl != NULL) return; @@ -81,6 +86,13 @@ static void _libvpn_init(void) error_print(PROGNAME); exit(1); } +#ifdef RLIMIT_NOFILE + if(getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_max > _vpn_offset) + _vpn_offset = r.rlim_max; +# ifdef DEBUG + fprintf(stderr, "DEBUG: %s() %u\n", __func__, _vpn_offset); +# endif +#endif } @@ -92,9 +104,10 @@ int connect(int fd, const struct sockaddr * name, socklen_t namelen) int ret; _libvpn_init(); - if(fd < VPN_OFFSET) + if(fd < _vpn_offset) return old_connect(fd, name, namelen); - if(appclient_call(_appclient, (void **)&ret, "connect", fd - VPN_OFFSET) + if(appclient_call(_appclient, (void **)&ret, "connect", + fd - _vpn_offset) != 0) return -1; #ifdef DEBUG