Asterisk - The Open Source Telephony Project GIT-master-a358458
Functions | Variables
app_system.c File Reference

Execute arbitrary system commands. More...

#include "asterisk.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/channel.h"
#include "asterisk/strings.h"
#include "asterisk/threadstorage.h"
Include dependency graph for app_system.c:

Go to the source code of this file.

Functions

static void __init_buf_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int system_exec (struct ast_channel *chan, const char *data)
 
static int system_exec_helper (struct ast_channel *chan, const char *data, int failmode)
 
static int trysystem_exec (struct ast_channel *chan, const char *data)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Generic System() application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static char * app = "System"
 
static char * app2 = "TrySystem"
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_threadstorage buf_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_buf_buf , .custom_init = NULL , }
 
static char * chanvar = "SYSTEMSTATUS"
 

Detailed Description

Execute arbitrary system commands.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file app_system.c.

Function Documentation

◆ __init_buf_buf()

static void __init_buf_buf ( void  )
static

Definition at line 107 of file app_system.c.

116{

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 194 of file app_system.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 194 of file app_system.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 194 of file app_system.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 184 of file app_system.c.

185{
186 int res;
187
190
191 return res;
192}
static int system_exec(struct ast_channel *chan, const char *data)
Definition: app_system.c:164
static char * app2
Definition: app_system.c:111
static char * app
Definition: app_system.c:109
static int trysystem_exec(struct ast_channel *chan, const char *data)
Definition: app_system.c:169
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

References app, app2, ast_register_application_xml, system_exec(), and trysystem_exec().

◆ system_exec()

static int system_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 164 of file app_system.c.

165{
166 return system_exec_helper(chan, data, -1);
167}
static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
Definition: app_system.c:115

References system_exec_helper().

Referenced by load_module().

◆ system_exec_helper()

static int system_exec_helper ( struct ast_channel chan,
const char *  data,
int  failmode 
)
static

Definition at line 115 of file app_system.c.

116{
117 int res = 0;
118 struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
119 char *cbuf;
120
121 if (ast_strlen_zero(data)) {
122 ast_log(LOG_WARNING, "System requires an argument(command)\n");
123 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
124 return failmode;
125 }
126
128
129 /* Do our thing here */
130 ast_str_get_encoded_str(&buf, 0, (char *) data);
131 cbuf = ast_str_buffer(buf);
132
133 if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
134 cbuf[ast_str_strlen(buf) - 1] = '\0';
135 cbuf++;
136 ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
137 }
138
139 res = ast_safe_system(cbuf);
140
141 if ((res < 0) && (errno != ECHILD)) {
142 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
143 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
144 res = failmode;
145 } else if (res == 127) {
146 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
147 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
148 res = failmode;
149 } else {
150 if (res < 0)
151 res = 0;
152 if (res != 0)
153 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
154 else
155 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
156 res = 0;
157 }
158
160
161 return res;
162}
static struct ast_threadstorage buf_buf
Definition: app_system.c:107
#define ast_log
Definition: astobj2.c:42
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream)
Decode a stream of encoded control or extended ASCII characters.
Definition: main/app.c:3165
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: extconf.c:829
#define LOG_NOTICE
#define LOG_WARNING
int errno
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:909
Support for dynamic strings.
Definition: strings.h:623
structure for queuing ARI channel variable setting
Definition: control.c:705

References ast_autoservice_start(), ast_autoservice_stop(), ast_log, ast_safe_system(), ast_str_buffer(), ast_str_get_encoded_str(), ast_str_strlen(), ast_str_thread_get(), ast_strlen_zero(), buf, buf_buf, errno, LOG_NOTICE, LOG_WARNING, and pbx_builtin_setvar_helper().

Referenced by system_exec(), and trysystem_exec().

◆ trysystem_exec()

static int trysystem_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 169 of file app_system.c.

170{
171 return system_exec_helper(chan, data, 0);
172}

References system_exec_helper().

Referenced by load_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 174 of file app_system.c.

175{
176 int res;
177
180
181 return res;
182}
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References app, app2, and ast_unregister_application().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Generic System() application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 194 of file app_system.c.

◆ app

char* app = "System"
static

Definition at line 109 of file app_system.c.

Referenced by load_module(), and unload_module().

◆ app2

char* app2 = "TrySystem"
static

Definition at line 111 of file app_system.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 194 of file app_system.c.

◆ buf_buf

struct ast_threadstorage buf_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_buf_buf , .custom_init = NULL , }
static

Definition at line 107 of file app_system.c.

Referenced by system_exec_helper().

◆ chanvar

char* chanvar = "SYSTEMSTATUS"
static

Definition at line 113 of file app_system.c.

Referenced by load_config().