Dynamically obtain the maximum possible value for local file descriptors

This commit is contained in:
Pierre Pronchery 2014-06-01 18:36:56 +02:00
parent 43e114f99a
commit 01ec3edeb0

View File

@ -15,6 +15,7 @@
#include <sys/resource.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -36,12 +37,13 @@ typedef struct _VPNSocket
/* constants */ /* constants */
#define PROGNAME "libVPN" #define PROGNAME "libVPN"
#define VPN_OFFSET 1024
/* variables */ /* variables */
static AppClient * _appclient = NULL; static AppClient * _appclient = NULL;
static int _vpn_offset = 1024;
/* local functions */ /* local functions */
static int (*old_connect)(int fd, const struct sockaddr * name, static int (*old_connect)(int fd, const struct sockaddr * name,
socklen_t namelen); socklen_t namelen);
@ -58,6 +60,9 @@ static void _libvpn_init(void)
/* FIXME some symbols may be in libsocket.so instead */ /* FIXME some symbols may be in libsocket.so instead */
static char * libc[] = { "/lib/libc.so", "/lib/libc.so.6" }; static char * libc[] = { "/lib/libc.so", "/lib/libc.so.6" };
size_t i; size_t i;
#ifdef RLIMIT_NOFILE
struct rlimit r;
#endif
if(hdl != NULL) if(hdl != NULL)
return; return;
@ -81,6 +86,13 @@ static void _libvpn_init(void)
error_print(PROGNAME); error_print(PROGNAME);
exit(1); 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; int ret;
_libvpn_init(); _libvpn_init();
if(fd < VPN_OFFSET) if(fd < _vpn_offset)
return old_connect(fd, name, namelen); 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) != 0)
return -1; return -1;
#ifdef DEBUG #ifdef DEBUG