Wed Jan 8 2020 09:49:55

Asterisk developer's documentation


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"

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)
 
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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static char * app = "System"
 
static char * app2 = "TrySystem"
 
static 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

static void __init_buf_buf ( void  )
static

Definition at line 99 of file app_system.c.

108 {
static void __reg_module ( void  )
static

Definition at line 186 of file app_system.c.

static void __unreg_module ( void  )
static

Definition at line 186 of file app_system.c.

static int load_module ( void  )
static

Definition at line 176 of file app_system.c.

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

177 {
178  int res;
179 
182 
183  return res;
184 }
static int system_exec(struct ast_channel *chan, const char *data)
Definition: app_system.c:156
static char * app
Definition: app_system.c:101
static int trysystem_exec(struct ast_channel *chan, const char *data)
Definition: app_system.c:161
static char * app2
Definition: app_system.c:103
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
static int system_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 156 of file app_system.c.

References system_exec_helper().

Referenced by load_module().

157 {
158  return system_exec_helper(chan, data, -1);
159 }
static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
Definition: app_system.c:107
static int system_exec_helper ( struct ast_channel chan,
const char *  data,
int  failmode 
)
static

Definition at line 107 of file app_system.c.

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, errno, LOG_NOTICE, LOG_WARNING, and pbx_builtin_setvar_helper().

Referenced by system_exec(), and trysystem_exec().

108 {
109  int res = 0;
110  struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
111  char *cbuf;
112 
113  if (ast_strlen_zero(data)) {
114  ast_log(LOG_WARNING, "System requires an argument(command)\n");
115  pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
116  return failmode;
117  }
118 
119  ast_autoservice_start(chan);
120 
121  /* Do our thing here */
122  ast_str_get_encoded_str(&buf, 0, (char *) data);
123  cbuf = ast_str_buffer(buf);
124 
125  if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
126  cbuf[ast_str_strlen(buf) - 1] = '\0';
127  cbuf++;
128  ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
129  }
130 
131  res = ast_safe_system(cbuf);
132 
133  if ((res < 0) && (errno != ECHILD)) {
134  ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
135  pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
136  res = failmode;
137  } else if (res == 127) {
138  ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
139  pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
140  res = failmode;
141  } else {
142  if (res < 0)
143  res = 0;
144  if (res != 0)
145  pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
146  else
147  pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
148  res = 0;
149  }
150 
151  ast_autoservice_stop(chan);
152 
153  return res;
154 }
int ast_safe_system(const char *s)
Safely spawn an external program while closing file descriptors.
Definition: asterisk.c:1077
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:179
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
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: app.c:2210
static char * chanvar
Definition: app_system.c:105
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:238
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
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...
Definition: pbx.c:10546
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
static struct ast_threadstorage buf_buf
Definition: app_system.c:99
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:669
static int trysystem_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 161 of file app_system.c.

References system_exec_helper().

Referenced by load_module().

162 {
163  return system_exec_helper(chan, data, 0);
164 }
static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
Definition: app_system.c:107
static int unload_module ( void  )
static

Definition at line 166 of file app_system.c.

References ast_unregister_application().

167 {
168  int res;
169 
172 
173  return res;
174 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
static char * app
Definition: app_system.c:101
static char * app2
Definition: app_system.c:103

Variable Documentation

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 186 of file app_system.c.

char* app = "System"
static

Definition at line 101 of file app_system.c.

char* app2 = "TrySystem"
static

Definition at line 103 of file app_system.c.

Definition at line 186 of file app_system.c.

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

Definition at line 99 of file app_system.c.

Referenced by system_exec_helper().

char* chanvar = "SYSTEMSTATUS"
static

Definition at line 105 of file app_system.c.

Referenced by function_sippeer(), and load_config().