mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-27 18:04:07 +00:00
bump version
This commit is contained in:
+84
-89
@@ -42,10 +42,10 @@
|
||||
#include <sys/resource.h>
|
||||
#endif
|
||||
|
||||
#define INT_DIGITS 19 /* enough for 64 bit integer */
|
||||
#define INT_DIGITS 19 /* enough for 64 bit integer */
|
||||
|
||||
#ifdef LIB_ONLY
|
||||
FILE* logfile;
|
||||
FILE * logfile;
|
||||
#endif
|
||||
|
||||
#ifdef HAS_SYSLOG
|
||||
@@ -69,25 +69,18 @@ char *itoa(int i)
|
||||
{
|
||||
/* Room for INT_DIGITS digits, - and '\0' */
|
||||
static char buf[INT_DIGITS + 2];
|
||||
char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */
|
||||
if (i >= 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */
|
||||
if (i >= 0) {
|
||||
do {
|
||||
*--p = '0' + (i % 10);
|
||||
i /= 10;
|
||||
}
|
||||
while (i != 0);
|
||||
} while (i != 0);
|
||||
return p;
|
||||
}
|
||||
else /* i < 0 */
|
||||
{
|
||||
do
|
||||
{
|
||||
} else { /* i < 0 */
|
||||
do {
|
||||
*--p = '0' - (i % 10);
|
||||
i /= 10;
|
||||
}
|
||||
while (i != 0);
|
||||
} while (i != 0);
|
||||
*--p = '-';
|
||||
}
|
||||
return p;
|
||||
@@ -99,52 +92,49 @@ char *itoa(int i)
|
||||
int run_as(const char *user)
|
||||
{
|
||||
#ifndef __MINGW32__
|
||||
if (user[0])
|
||||
{
|
||||
if (user[0]) {
|
||||
#ifdef HAVE_GETPWNAM_R
|
||||
struct passwd pwdbuf, *pwd;
|
||||
size_t buflen;
|
||||
int err;
|
||||
|
||||
for (buflen = 128;; buflen *= 2)
|
||||
{
|
||||
for (buflen = 128;; buflen *= 2) {
|
||||
char buf[buflen]; /* variable length array */
|
||||
|
||||
/* Note that we use getpwnam_r() instead of getpwnam(),
|
||||
which returns its result in a statically allocated buffer and
|
||||
cannot be considered thread safe. */
|
||||
err = getpwnam_r(user, &pwdbuf, buf, buflen, &pwd);
|
||||
if(err == 0 && pwd)
|
||||
{
|
||||
if (err == 0 && pwd) {
|
||||
/* setgid first, because we may not be allowed to do it anymore after setuid */
|
||||
if (setgid(pwd->pw_gid) != 0)
|
||||
{
|
||||
LOGE("Could not change group id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
if (setgid(pwd->pw_gid) != 0) {
|
||||
LOGE(
|
||||
"Could not change group id to that of run_as user '%s': %s",
|
||||
user, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (setuid(pwd->pw_uid) != 0)
|
||||
{
|
||||
LOGE("Could not change user id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
if (setuid(pwd->pw_uid) != 0) {
|
||||
LOGE(
|
||||
"Could not change user id to that of run_as user '%s': %s",
|
||||
user, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (err != ERANGE)
|
||||
{
|
||||
if(err)
|
||||
LOGE("run_as user '%s' could not be found: %s",user,strerror(err));
|
||||
else
|
||||
LOGE("run_as user '%s' could not be found.",user);
|
||||
} else if (err != ERANGE) {
|
||||
if (err) {
|
||||
LOGE("run_as user '%s' could not be found: %s", user, strerror(
|
||||
err));
|
||||
} else {
|
||||
LOGE("run_as user '%s' could not be found.", user);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (buflen >= 16*1024)
|
||||
{
|
||||
} else if (buflen >= 16 * 1024) {
|
||||
/* If getpwnam_r() seems defective, call it quits rather than
|
||||
keep on allocating ever larger buffers until we crash. */
|
||||
LOGE("getpwnam_r() requires more than %u bytes of buffer space.",(unsigned)buflen);
|
||||
LOGE(
|
||||
"getpwnam_r() requires more than %u bytes of buffer space.",
|
||||
(unsigned)buflen);
|
||||
return 0;
|
||||
}
|
||||
/* Else try again with larger buffer. */
|
||||
@@ -153,22 +143,19 @@ int run_as(const char *user)
|
||||
/* No getpwnam_r() :-( We'll use getpwnam() and hope for the best. */
|
||||
struct passwd *pwd;
|
||||
|
||||
if (!(pwd=getpwnam(user)))
|
||||
{
|
||||
LOGE("run_as user %s could not be found.",user);
|
||||
if (!(pwd = getpwnam(user))) {
|
||||
LOGE("run_as user %s could not be found.", user);
|
||||
return 0;
|
||||
}
|
||||
/* setgid first, because we may not allowed to do it anymore after setuid */
|
||||
if (setgid(pwd->pw_gid) != 0)
|
||||
{
|
||||
if (setgid(pwd->pw_gid) != 0) {
|
||||
LOGE("Could not change group id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
user, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
if (setuid(pwd->pw_uid) != 0)
|
||||
{
|
||||
if (setuid(pwd->pw_uid) != 0) {
|
||||
LOGE("Could not change user id to that of run_as user '%s': %s",
|
||||
user,strerror(errno));
|
||||
user, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -185,7 +172,9 @@ char *ss_strndup(const char *s, size_t n)
|
||||
size_t len = strlen(s);
|
||||
char *ret;
|
||||
|
||||
if (len <= n) return strdup(s);
|
||||
if (len <= n) {
|
||||
return strdup(s);
|
||||
}
|
||||
|
||||
ret = malloc(n + 1);
|
||||
strncpy(ret, s, n);
|
||||
@@ -203,21 +192,31 @@ void usage()
|
||||
{
|
||||
printf("\n");
|
||||
printf("shadowsocks-libev %s\n\n", VERSION);
|
||||
printf(" maintained by Max Lv <max.c.lv@gmail.com> and Linus Yang <laokongzi@gmail.com>\n\n");
|
||||
printf(
|
||||
" maintained by Max Lv <max.c.lv@gmail.com> and Linus Yang <laokongzi@gmail.com>\n\n");
|
||||
printf(" usage:\n\n");
|
||||
printf(" ss-[local|redir|server|tunnel]\n");
|
||||
printf("\n");
|
||||
printf(" -s <server_host> host name or ip address of your remote server\n");
|
||||
printf(" -p <server_port> port number of your remote server\n");
|
||||
printf(" -l <local_port> port number of your local server\n");
|
||||
printf(" -k <password> password of your remote server\n");
|
||||
printf(
|
||||
" -s <server_host> host name or ip address of your remote server\n");
|
||||
printf(
|
||||
" -p <server_port> port number of your remote server\n");
|
||||
printf(
|
||||
" -l <local_port> port number of your local server\n");
|
||||
printf(
|
||||
" -k <password> password of your remote server\n");
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
printf(" [-m <encrypt_method>] encrypt method: table, rc4, rc4-md5,\n");
|
||||
printf(" aes-128-cfb, aes-192-cfb, aes-256-cfb,\n");
|
||||
printf(" bf-cfb, camellia-128-cfb, camellia-192-cfb,\n");
|
||||
printf(" camellia-256-cfb, cast5-cfb, des-cfb,\n");
|
||||
printf(" idea-cfb, rc2-cfb and seed-cfb\n");
|
||||
printf(
|
||||
" [-m <encrypt_method>] encrypt method: table, rc4, rc4-md5,\n");
|
||||
printf(
|
||||
" aes-128-cfb, aes-192-cfb, aes-256-cfb,\n");
|
||||
printf(
|
||||
" bf-cfb, camellia-128-cfb, camellia-192-cfb,\n");
|
||||
printf(
|
||||
" camellia-256-cfb, cast5-cfb, des-cfb,\n");
|
||||
printf(
|
||||
" idea-cfb, rc2-cfb and seed-cfb\n");
|
||||
printf(" [-f <pid_file>] file to store the pid\n");
|
||||
printf(" [-t <timeout>] socket timeout in seconds\n");
|
||||
printf(" [-c <config_file>] config file in json\n");
|
||||
@@ -229,18 +228,22 @@ void usage()
|
||||
printf(" not available in server mode\n");
|
||||
printf(" [-u] enable udprelay mode\n");
|
||||
printf(" not available in redir mode\n");
|
||||
printf(" [-L <addr>:<port>] setup a local port forwarding tunnel,\n");
|
||||
printf(" only available in tunnel mode\n");
|
||||
printf(
|
||||
" [-L <addr>:<port>] setup a local port forwarding tunnel,\n");
|
||||
printf(
|
||||
" only available in tunnel mode\n");
|
||||
printf(" [-v] verbose mode\n");
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
printf(" [--fast-open] enable TCP fast open,\n");
|
||||
printf(" only available on Linux kernel > 3.7.0\n");
|
||||
printf(" [--acl <acl_file>] config file of ACL (Access Control List)\n");
|
||||
printf(
|
||||
" only available on Linux kernel > 3.7.0\n");
|
||||
printf(
|
||||
" [--acl <acl_file>] config file of ACL (Access Control List)\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void daemonize(const char* path)
|
||||
void daemonize(const char * path)
|
||||
{
|
||||
#ifndef __MINGW32__
|
||||
/* Our process ID and Session ID */
|
||||
@@ -248,17 +251,17 @@ void daemonize(const char* path)
|
||||
|
||||
/* Fork off the parent process */
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
if (pid < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* If we got a good PID, then
|
||||
we can exit the parent process. */
|
||||
if (pid > 0)
|
||||
{
|
||||
if (pid > 0) {
|
||||
FILE *file = fopen(path, "w");
|
||||
if (file == NULL) FATAL("Invalid pid file\n");
|
||||
if (file == NULL) {
|
||||
FATAL("Invalid pid file\n");
|
||||
}
|
||||
|
||||
fprintf(file, "%d", pid);
|
||||
fclose(file);
|
||||
@@ -272,15 +275,13 @@ void daemonize(const char* path)
|
||||
|
||||
/* Create a new SID for the child process */
|
||||
sid = setsid();
|
||||
if (sid < 0)
|
||||
{
|
||||
if (sid < 0) {
|
||||
/* Log the failure */
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Change the current working directory */
|
||||
if ((chdir("/")) < 0)
|
||||
{
|
||||
if ((chdir("/")) < 0) {
|
||||
/* Log the failure */
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -295,27 +296,21 @@ void daemonize(const char* path)
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
int set_nofile(int nofile)
|
||||
{
|
||||
struct rlimit limit = {nofile, nofile}; /* set both soft and hard limit */
|
||||
struct rlimit limit = { nofile, nofile }; /* set both soft and hard limit */
|
||||
|
||||
if (nofile <= 0)
|
||||
{
|
||||
if (nofile <= 0) {
|
||||
FATAL("nofile must be greater than 0\n");
|
||||
}
|
||||
|
||||
if (setrlimit(RLIMIT_NOFILE, &limit) < 0)
|
||||
{
|
||||
if (errno == EPERM)
|
||||
{
|
||||
LOGE("insufficient permission to change NOFILE, not starting as root?");
|
||||
if (setrlimit(RLIMIT_NOFILE, &limit) < 0) {
|
||||
if (errno == EPERM) {
|
||||
LOGE(
|
||||
"insufficient permission to change NOFILE, not starting as root?");
|
||||
return -1;
|
||||
}
|
||||
else if (errno == EINVAL)
|
||||
{
|
||||
} else if (errno == EINVAL) {
|
||||
LOGE("invalid nofile, decrease nofile and try again");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
LOGE("setrlimit failed: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user