diff --git a/src/main.c b/src/main.c index 31b377d3..0723096e 100644 --- a/src/main.c +++ b/src/main.c @@ -66,7 +66,7 @@ const char* k_db_path_default = "db.sqlite"; break; \ } while (false) -#if !TARGET_OS_IPHONE +#if !TARGET_OS_IPHONE && !defined(__ANDROID__) static int _tf_command_test(const char* file, int argc, char* argv[]); static int _tf_command_import(const char* file, int argc, char* argv[]); static int _tf_command_export(const char* file, int argc, char* argv[]); @@ -90,54 +90,6 @@ const command_t k_commands[] = { { "check", _tf_command_check, "Validate messages in the SSB database." }, }; -void shedPrivileges() -{ -#if !defined(_WIN32) - struct rlimit zeroLimit; - zeroLimit.rlim_cur = 0; - zeroLimit.rlim_max = 0; - - // RLIMIT_AS - // RLIMIT_CORE - // RLIMIT_CPU - // RLIMIT_DATA - // RLIMIT_FSIZE - // RLIMIT_RSS - // RLIMIT_RTPRIO - // RLIMIT_RTTIME - // RLIMIT_SIGPENDING - // RLIMIT_STACK - - if (setrlimit(RLIMIT_FSIZE, &zeroLimit) != 0) - { - perror("setrlimit(RLIMIT_FSIZE, {0, 0})"); - exit(-1); - } - if (setrlimit(RLIMIT_NOFILE, &zeroLimit) != 0) - { - perror("setrlimit(RLIMIT_NOFILE, {0, 0})"); - exit(-1); - } - if (setrlimit(RLIMIT_NPROC, &zeroLimit) != 0) - { - perror("setrlimit(RLIMIT_NPROC, {0, 0})"); - exit(-1); - } -#if !defined(__MACH__) - if (setrlimit(RLIMIT_LOCKS, &zeroLimit) != 0) - { - perror("setrlimit(RLIMIT_LOCKS, {0, 0})"); - exit(-1); - } - if (setrlimit(RLIMIT_MSGQUEUE, &zeroLimit) != 0) - { - perror("setrlimit(RLIMIT_MSGQUEUE, {0, 0})"); - exit(-1); - } -#endif -#endif -} - static int _tf_command_test(const char* file, int argc, char* argv[]) { #if !defined(__ANDROID__) @@ -377,6 +329,54 @@ static void _tf_run_task_thread(void* data) } #if !TARGET_OS_IPHONE +static void _shed_privileges() +{ +#if !defined(_WIN32) + struct rlimit zeroLimit; + zeroLimit.rlim_cur = 0; + zeroLimit.rlim_max = 0; + + // RLIMIT_AS + // RLIMIT_CORE + // RLIMIT_CPU + // RLIMIT_DATA + // RLIMIT_FSIZE + // RLIMIT_RSS + // RLIMIT_RTPRIO + // RLIMIT_RTTIME + // RLIMIT_SIGPENDING + // RLIMIT_STACK + + if (setrlimit(RLIMIT_FSIZE, &zeroLimit) != 0) + { + perror("setrlimit(RLIMIT_FSIZE, {0, 0})"); + exit(-1); + } + if (setrlimit(RLIMIT_NOFILE, &zeroLimit) != 0) + { + perror("setrlimit(RLIMIT_NOFILE, {0, 0})"); + exit(-1); + } + if (setrlimit(RLIMIT_NPROC, &zeroLimit) != 0) + { + perror("setrlimit(RLIMIT_NPROC, {0, 0})"); + exit(-1); + } +#if !defined(__MACH__) + if (setrlimit(RLIMIT_LOCKS, &zeroLimit) != 0) + { + perror("setrlimit(RLIMIT_LOCKS, {0, 0})"); + exit(-1); + } + if (setrlimit(RLIMIT_MSGQUEUE, &zeroLimit) != 0) + { + perror("setrlimit(RLIMIT_MSGQUEUE, {0, 0})"); + exit(-1); + } +#endif +#endif +} + static int _tf_command_run(const char* file, int argc, char* argv[]) { xoptOption options[] = { @@ -489,7 +489,7 @@ static int _tf_command_sandbox(const char* file, int argc, char* argv[]) #endif tf_task_t* task = tf_task_create(); tf_task_configure_from_fd(task, STDIN_FILENO); - shedPrivileges(); + _shed_privileges(); /* The caller will trigger tf_task_activate with a message. */ tf_task_run(task); tf_task_destroy(task); @@ -507,6 +507,7 @@ xopt_help: return 1; } +#if !defined(__ANDROID__) static int _tf_command_check(const char* file, int argc, char* argv[]) { typedef struct args_t { @@ -569,6 +570,7 @@ static int _tf_command_usage(const char* file, int argc, char* argv[]) return 0; } #endif +#endif static void _backtrace_error(void* data, const char* message, int errnum) { @@ -638,36 +640,26 @@ static void _startup(int argc, char* argv[]) } } -#if !TARGET_OS_IPHONE +#if defined(__ANDROID__) int main(int argc, char* argv[]) { _startup(argc, argv); - - int result = 0; - if (argc >= 2) + int result = -1; + if (argc > 1) { - for (int i = 0; i < (int)_countof(k_commands); i++) + if (strcmp(argv[1], "run") == 0) { - const command_t* command = &k_commands[i]; - if (strcmp(argv[1], command->name) == 0) - { - result = command->callback(argv[0], argc - 2, argv + 2); - goto done; - } + result = _tf_command_run(argv[0], argc - 2, argv + 2); + } + else if (strcmp(argv[1], "sandbox") == 0) + { + result = _tf_command_sandbox(argv[0], argc - 2, argv + 2); } - result = _tf_command_usage(argv[0], argc, argv); } - else - { - result = _tf_command_run(argv[0], argc - 1, argv + 1); - } -done: tf_mem_shutdown(); return result; } -#endif - -#if TARGET_OS_IPHONE +#elif TARGET_OS_IPHONE && !defined(__ANDROID__) void tf_run_thread_start(const char* zip_path) { _startup(0, NULL); @@ -699,4 +691,31 @@ void tf_run_thread_start(const char* zip_path) tf_free(threads); #endif } +#else +int main(int argc, char* argv[]) +{ + _startup(argc, argv); + + int result = 0; + if (argc >= 2) + { + for (int i = 0; i < (int)_countof(k_commands); i++) + { + const command_t* command = &k_commands[i]; + if (strcmp(argv[1], command->name) == 0) + { + result = command->callback(argv[0], argc - 2, argv + 2); + goto done; + } + } + result = _tf_command_usage(argv[0], argc, argv); + } + else + { + result = _tf_command_run(argv[0], argc - 1, argv + 1); + } +done: + tf_mem_shutdown(); + return result; +} #endif