From 47f984e61c093c94af16f07f335d3e5f2edf5918 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Mon, 14 Dec 2015 19:31:12 +0800 Subject: [PATCH] Refine nofile option --- src/local.c | 9 ++++++--- src/redir.c | 23 ++++++++++++++++++++++- src/server.c | 7 +++++-- src/tunnel.c | 25 +++++++++++++++++++++++-- src/utils.c | 6 ++++++ 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/local.c b/src/local.c index eb302d81..551237ad 100644 --- a/src/local.c +++ b/src/local.c @@ -992,10 +992,10 @@ int main(int argc, char **argv) USE_TTY(); #ifdef ANDROID - while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uvVA", + while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:n:uvVA", long_options, &option_index)) != -1) { #else - while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uvA", + while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:n:uvA", long_options, &option_index)) != -1) { #endif switch (c) { @@ -1044,6 +1044,9 @@ int main(int argc, char **argv) case 'a': user = optarg; break; + case 'n': + nofile = atoi(optarg); + break; case 'u': mode = TCP_AND_UDP; break; @@ -1110,7 +1113,7 @@ int main(int argc, char **argv) * no need to check the return value here since we will show * the user an error message if setrlimit(2) fails */ - if (nofile) { + if (nofile > 1024) { if (verbose) { LOGI("setting NOFILE to %d", nofile); } diff --git a/src/redir.c b/src/redir.c index 8e3d0b49..9aabe82b 100644 --- a/src/redir.c +++ b/src/redir.c @@ -84,6 +84,9 @@ int verbose = 0; static int mode = TCP_ONLY; static int auth = 0; +#ifdef HAVE_SETRLIMIT +static int nofile = 0; +#endif int getdestaddr(int fd, struct sockaddr_storage *destaddr) { @@ -636,7 +639,7 @@ int main(int argc, char **argv) opterr = 0; - while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:c:b:a:uUvA")) != -1) + while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:c:b:a:n:uUvA")) != -1) switch (c) { case 's': if (remote_num < MAX_REMOTE_NUM) { @@ -672,6 +675,9 @@ int main(int argc, char **argv) case 'a': user = optarg; break; + case 'n': + nofile = atoi(optarg); + break; case 'u': mode = TCP_AND_UDP; break; @@ -725,6 +731,21 @@ int main(int argc, char **argv) if (auth == 0) { auth = conf->auth; } +#ifdef HAVE_SETRLIMIT + if (nofile == 0) { + nofile = conf->nofile; + } + /* + * no need to check the return value here since we will show + * the user an error message if setrlimit(2) fails + */ + if (nofile > 1024) { + if (verbose) { + LOGI("setting NOFILE to %d", nofile); + } + set_nofile(nofile); + } +#endif } if (remote_num == 0 || remote_port == NULL || diff --git a/src/server.c b/src/server.c index a19f695b..4c8143d6 100644 --- a/src/server.c +++ b/src/server.c @@ -1297,7 +1297,7 @@ int main(int argc, char **argv) USE_TTY(); - while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:c:i:d:a:uUvAw", + while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:c:i:d:a:n:uUvAw", long_options, &option_index)) != -1) switch (c) { case 0: @@ -1346,6 +1346,9 @@ int main(int argc, char **argv) case 'a': user = optarg; break; + case 'n': + nofile = atoi(optarg); + break; case 'u': mode = TCP_AND_UDP; break; @@ -1411,7 +1414,7 @@ int main(int argc, char **argv) * no need to check the return value here since we will show * the user an error message if setrlimit(2) fails */ - if (nofile) { + if (nofile > 1024) { if (verbose) { LOGI("setting NOFILE to %d", nofile); } diff --git a/src/tunnel.c b/src/tunnel.c index efae4638..bc5c0fd3 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -92,6 +92,9 @@ int verbose = 0; static int mode = TCP_ONLY; static int auth = 0; +#ifdef HAVE_SETRLIMIT +static int nofile = 0; +#endif #ifndef __MINGW32__ static int setnonblocking(int fd) @@ -693,9 +696,9 @@ int main(int argc, char **argv) USE_TTY(); #ifdef ANDROID - while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uUvVA")) != -1) { + while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:n:uUvVA")) != -1) { #else - while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uUvA")) != -1) { + while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:n:uUvA")) != -1) { #endif switch (c) { case 's': @@ -744,6 +747,9 @@ int main(int argc, char **argv) case 'a': user = optarg; break; + case 'n': + nofile = atoi(optarg); + break; case 'v': verbose = 1; break; @@ -797,6 +803,21 @@ int main(int argc, char **argv) if (auth == 0) { auth = conf->auth; } +#ifdef HAVE_SETRLIMIT + if (nofile == 0) { + nofile = conf->nofile; + } + /* + * no need to check the return value here since we will show + * the user an error message if setrlimit(2) fails + */ + if (nofile > 1024) { + if (verbose) { + LOGI("setting NOFILE to %d", nofile); + } + set_nofile(nofile); + } +#endif } if (remote_num == 0 || remote_port == NULL || tunnel_addr_str == NULL || diff --git a/src/utils.c b/src/utils.c index f2e45967..3c2aa64a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -223,12 +223,18 @@ void usage() printf( " rc2-cfb, seed-cfb, salsa20 and chacha20.\n"); printf("\n"); + printf( + " [-a ] Run as another user.\n"); printf( " [-f ] The file path to store pid.\n"); printf( " [-t ] Socket timeout in seconds.\n"); printf( " [-c ] The path to config file.\n"); +#ifdef HAVE_SETRLIMIT + printf( + " [-n ] Max number of open files, only available on Linux.\n"); +#endif #ifndef MODULE_REDIR printf( " [-i ] Network interface to bind.\n");