diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 993d02a812..5fefc9b5b6 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -1654,6 +1654,8 @@ SWITCH_DECLARE(uint32_t) switch_core_default_dtmf_duration(uint32_t duration); SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string); SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string); +SWITCH_DECLARE(int) switch_system(char *cmd, switch_bool_t wait); + ///\} /*! diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 832b338d24..488a64865f 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -346,6 +346,7 @@ SWITCH_DECLARE_DATA extern switch_directories SWITCH_GLOBAL_dirs; #define SWITCH_MAX_STACKS 32 #define SWITCH_THREAD_STACKSIZE 240 * 1024 +#define SWITCH_SYSTEM_THREAD_STACKSIZE 8192 * 1024 #define SWITCH_MAX_INTERVAL 120 /* we only do up to 120ms */ #define SWITCH_INTERVAL_PAD 10 /* A little extra buffer space to be safe */ #define SWITCH_MAX_SAMPLE_LEN 32 diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index c6882cb981..139a72ec3f 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -2518,7 +2518,7 @@ SWITCH_STANDARD_API(system_function) } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Executing command: %s\n", cmd); - if (system(cmd) < 0) { + if (switch_system(cmd, SWITCH_TRUE) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Failed to execute command: %s\n", cmd); } stream->write_function(stream, "+OK\n"); diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index 4fdae5e528..2cba22d22f 100644 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -1185,7 +1185,7 @@ SWITCH_STANDARD_APP(fax_detect_session_function) SWITCH_STANDARD_APP(system_session_function) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Executing command: %s\n", data); - if (system(data) < 0) { + if (switch_system(data, SWITCH_TRUE) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Failed to execute command: %s\n", data); } } diff --git a/src/mod/languages/mod_lua/lua/loslib.c b/src/mod/languages/mod_lua/lua/loslib.c index da06a572ac..d29785756c 100644 --- a/src/mod/languages/mod_lua/lua/loslib.c +++ b/src/mod/languages/mod_lua/lua/loslib.c @@ -36,7 +36,7 @@ static int os_pushresult (lua_State *L, int i, const char *filename) { static int os_execute (lua_State *L) { - lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); + lua_pushinteger(L, switch_system(luaL_optstring(L, 1, NULL), SWITCH_TRUE)); return 1; } diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c index 009f7da300..74a388503f 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c @@ -3427,7 +3427,7 @@ static JSBool js_system(JSContext * cx, JSObject * obj, uintN argc, jsval * argv *rval = BOOLEAN_TO_JSVAL(JS_FALSE); if (argc > 0 && (cmd = JS_GetStringBytes(JS_ValueToString(cx, argv[0])))) { - *rval = INT_TO_JSVAL(system(cmd)); + *rval = INT_TO_JSVAL(switch_system(cmd, SWITCH_TRUE)); return JS_TRUE; } diff --git a/src/switch_core.c b/src/switch_core.c index 6fa12b119b..8724e0c6cb 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -40,6 +40,8 @@ #include #endif +#include + SWITCH_DECLARE_DATA switch_directories SWITCH_GLOBAL_dirs = { 0 }; /* The main runtime obj we keep this hidden for ourselves */ @@ -1432,6 +1434,82 @@ SWITCH_DECLARE(void) switch_core_memory_reclaim_all(void) switch_core_memory_reclaim(); } + +struct system_thread_handle { + const char * cmd; + switch_thread_cond_t *cond; + switch_mutex_t *mutex; + switch_memory_pool_t *pool; + int ret; +}; + +static void *SWITCH_THREAD_FUNC system_thread(switch_thread_t *thread, void *obj) +{ + struct system_thread_handle *sth = (struct system_thread_handle *)obj; + +/* struct rlimit rlim; + + getrlimit(RLIMIT_STACK, &rlim); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "rlim_cur: %d rlim_max: %d\n", (int)rlim.rlim_cur, (int)rlim.rlim_max); +*/ + + sth->ret = system(sth->cmd); + + switch_mutex_lock(sth->mutex); + switch_thread_cond_signal(sth->cond); + switch_mutex_unlock(sth->mutex); + + switch_core_destroy_memory_pool(&sth->pool); + + return NULL; +} + +SWITCH_DECLARE(int) switch_system(char *cmd, switch_bool_t wait) +{ + switch_thread_t *thread; + switch_threadattr_t *thd_attr; + int ret = 0; + struct system_thread_handle *sth; + switch_memory_pool_t *pool; +#ifndef __FreeBSD__ + struct rlimit rlim; + + rlim.rlim_cur = SWITCH_SYSTEM_THREAD_STACKSIZE; + rlim.rlim_max = SWITCH_SYSTEM_THREAD_STACKSIZE;; + setrlimit(RLIMIT_STACK, &rlim); +#endif + + if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Pool Failure\n"); + return 1; + } + + if (!(sth = switch_core_alloc(pool, sizeof(struct system_thread_handle)))) { + switch_core_destroy_memory_pool(&pool); + return 1; + } + + sth->pool = pool; + sth->cmd = switch_core_strdup(pool, cmd); + + switch_thread_cond_create(&sth->cond, sth->pool); + switch_mutex_init(&sth->mutex, SWITCH_MUTEX_NESTED, sth->pool); + switch_mutex_lock(sth->mutex); + + switch_threadattr_create(&thd_attr, sth->pool); + switch_threadattr_stacksize_set(thd_attr, SWITCH_SYSTEM_THREAD_STACKSIZE); +// switch_threadattr_priority_increase(thd_attr); + switch_thread_create(&thread, thd_attr, system_thread, sth, sth->pool); + + if (wait) { + switch_thread_cond_wait(sth->cond, sth->mutex); + ret = sth->ret; + } + switch_mutex_unlock(sth->mutex); + + return ret; +} + /* For Emacs: * Local Variables: * mode:c diff --git a/src/switch_utils.c b/src/switch_utils.c index 1ee5fdc230..982a37df17 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -479,7 +479,7 @@ SWITCH_DECLARE(switch_bool_t) switch_simple_email(const char *to, const char *fr #else switch_snprintf(buf, B64BUFFLEN, "/bin/cat %s | %s %s %s", filename, runtime.mailer_app, runtime.mailer_app_args, to); #endif - if (system(buf)) { + if (switch_system(buf, SWITCH_TRUE) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to execute command: %s\n", buf); }