Asterisk - The Open Source Telephony Project GIT-master-1f1c5bb
Data Structures | Functions | Variables
app_signal.c File Reference

Channel signaling applications. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
Include dependency graph for app_signal.c:

Go to the source code of this file.

Data Structures

struct  signalitem
 
struct  signals
 

Functions

static struct signalitemalloc_signal (const char *sname)
 
 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Channel Signaling Applications")
 
static int dealloc_signal (struct signalitem *s)
 
static struct signalitemget_signal (char *sname, int addnew)
 
static int load_module (void)
 
static int remove_signal (char *sname)
 
static int send_signal (char *signame, char *payload)
 
static int signal_exec (struct ast_channel *chan, const char *data)
 
static int unload_module (void)
 
static int wait_for_signal_or_hangup (struct ast_channel *chan, char *signame, int timeout)
 
static int waitsignal_exec (struct ast_channel *chan, const char *data)
 

Variables

static const char *const app = "Signal"
 
static const char *const app2 = "WaitForSignal"
 
static struct signals signals = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 

Detailed Description

Channel signaling applications.

Author
Naveen Albert aster.nosp@m.isk@.nosp@m.phrea.nosp@m.knet.nosp@m..org

Definition in file app_signal.c.

Function Documentation

◆ alloc_signal()

static struct signalitem * alloc_signal ( const char *  sname)
static

Definition at line 137 of file app_signal.c.

138{
139 struct signalitem *s;
140
141 if (!(s = ast_calloc(1, sizeof(*s)))) {
142 return NULL;
143 }
144
145 ast_mutex_init(&s->lock);
146 ast_copy_string(s->name, sname, sizeof(s->name));
147
148 s->sig_alert_pipe[0] = -1;
149 s->sig_alert_pipe[1] = -1;
150 s->watchers = 0;
151 s->payload = NULL;
153
154 return s;
155}
int ast_alertpipe_init(int alert_pipe[2])
Initialize an alert pipe.
Definition: alertpipe.c:38
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_mutex_init(pmutex)
Definition: lock.h:186
#define NULL
Definition: resample.c:96
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
char * payload
Definition: app_signal.c:131
int sig_alert_pipe[2]
Definition: app_signal.c:128
int watchers
Definition: app_signal.c:129
char name[AST_MAX_CONTEXT]
Definition: app_signal.c:127
ast_mutex_t lock
Definition: app_signal.c:126

References ast_alertpipe_init(), ast_calloc, ast_copy_string(), ast_mutex_init, signalitem::lock, signalitem::name, NULL, signalitem::payload, signalitem::sig_alert_pipe, and signalitem::watchers.

Referenced by get_signal().

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Channel Signaling Applications"   
)

◆ dealloc_signal()

static int dealloc_signal ( struct signalitem s)
static

Definition at line 157 of file app_signal.c.

158{
159 if (s->watchers) { /* somebody is still using us... refuse to go away */
160 ast_debug(1, "Signal '%s' is still being used by %d listener(s)\n", s->name, s->watchers);
161 return -1;
162 }
165 if (s->payload) {
166 ast_free(s->payload);
167 s->payload = NULL;
168 }
169 ast_free(s);
170 s = NULL;
171 return 0;
172}
void ast_alertpipe_close(int alert_pipe[2])
Close an alert pipe.
Definition: alertpipe.c:79
#define ast_free(a)
Definition: astmm.h:180
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_mutex_destroy(a)
Definition: lock.h:188

References ast_alertpipe_close(), ast_debug, ast_free, ast_mutex_destroy, signalitem::lock, signalitem::name, NULL, signalitem::payload, signalitem::sig_alert_pipe, and signalitem::watchers.

Referenced by remove_signal(), and unload_module().

◆ get_signal()

static struct signalitem * get_signal ( char *  sname,
int  addnew 
)
static

Definition at line 191 of file app_signal.c.

192{
193 struct signalitem *s = NULL;
196 if (!strcasecmp(s->name, sname)) {
197 ast_debug(1, "Using existing signal item '%s'\n", sname);
198 break;
199 }
200 }
201 if (!s) {
202 if (addnew) { /* signal doesn't exist, so create it */
203 s = alloc_signal(sname);
204 /* Totally fail if we fail to find/create an entry */
205 if (s) {
206 ast_debug(1, "Created new signal item '%s'\n", sname);
208 } else {
209 ast_log(LOG_WARNING, "Failed to create signal item for '%s'\n", sname);
210 }
211 } else {
212 ast_debug(1, "Signal '%s' doesn't exist, and not creating it\n", sname);
213 }
214 }
216 return s;
217}
static struct signalitem * alloc_signal(const char *sname)
Definition: app_signal.c:137
#define ast_log
Definition: astobj2.c:42
#define LOG_WARNING
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:718
Definition: search.h:40

References alloc_signal(), ast_debug, AST_LIST_TRAVERSE, ast_log, AST_RWLIST_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, LOG_WARNING, signalitem::name, and NULL.

Referenced by send_signal(), and wait_for_signal_or_hangup().

◆ load_module()

static int load_module ( void  )
static

Definition at line 461 of file app_signal.c.

462{
463 int res;
464
467
468 return res;
469}
static int signal_exec(struct ast_channel *chan, const char *data)
Definition: app_signal.c:394
static int waitsignal_exec(struct ast_channel *chan, const char *data)
Definition: app_signal.c:336
static const char *const app
Definition: app_signal.c:122
static const char *const app2
Definition: app_signal.c:123
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640

References app, app2, ast_register_application_xml, signal_exec(), and waitsignal_exec().

◆ remove_signal()

static int remove_signal ( char *  sname)
static

Definition at line 174 of file app_signal.c.

175{
176 int res = -1;
177 struct signalitem *s;
178
180 if (!strcmp(s->name, sname)) {
182 res = dealloc_signal(s);
183 ast_debug(1, "Removed signal '%s'\n", sname);
184 }
185 }
187
188 return res;
189}
static int dealloc_signal(struct signalitem *s)
Definition: app_signal.c:157
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557

References ast_debug, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, dealloc_signal(), and signalitem::name.

Referenced by wait_for_signal_or_hangup().

◆ send_signal()

static int send_signal ( char *  signame,
char *  payload 
)
static

Definition at line 296 of file app_signal.c.

297{
298 struct signalitem *s;
299 int save_errno = errno;
300 int res = 0;
301
302 s = get_signal(signame, 0); /* if signal doesn't exist already, no point in creating it, because nobody could be waiting for it! */
303
304 if (!s) {
305 return -1; /* this signal didn't exist, so we can't send a signal for it */
306 }
307
308 /* at this point, we know someone is listening, since signals are destroyed when watchers gets down to 0 */
309 ast_mutex_lock(&s->lock);
310 s->signaled = 1;
311 if (payload && *payload) {
312 int len = strlen(payload);
313 if (s->payload) {
314 ast_free(s->payload); /* if there was already a payload, replace it */
315 s->payload = NULL;
316 }
317 s->payload = ast_malloc(len + 1);
318 if (!s->payload) {
319 ast_log(LOG_WARNING, "Failed to allocate signal payload '%s'\n", payload);
320 } else {
322 }
323 }
325 ast_log(LOG_WARNING, "%s: write() failed: %s\n", __FUNCTION__, strerror(errno));
326 s->signaled = 0; /* okay, so we didn't send a signal after all... */
327 res = -1;
328 }
329 errno = save_errno;
330 ast_debug(1, "Sent '%s' signal to %d listeners\n", signame, s->watchers);
332
333 return res;
334}
ssize_t ast_alertpipe_write(int alert_pipe[2])
Write an event to an alert pipe.
Definition: alertpipe.c:120
static struct signalitem * get_signal(char *sname, int addnew)
Definition: app_signal.c:191
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
int errno
unsigned int signaled
Definition: app_signal.c:130

References ast_alertpipe_write(), ast_copy_string(), ast_debug, ast_free, ast_log, ast_malloc, ast_mutex_lock, ast_mutex_unlock, errno, get_signal(), len(), signalitem::lock, LOG_WARNING, NULL, signalitem::payload, signalitem::sig_alert_pipe, signalitem::signaled, and signalitem::watchers.

Referenced by signal_exec().

◆ signal_exec()

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

Definition at line 394 of file app_signal.c.

395{
396 char *argcopy;
398 AST_APP_ARG(signame);
399 AST_APP_ARG(payload);
400 );
401
402 if (ast_strlen_zero(data)) {
403 ast_log(LOG_WARNING, "Signal() requires arguments\n");
404 return -1;
405 }
406
407 argcopy = ast_strdupa(data);
408 AST_STANDARD_APP_ARGS(args, argcopy);
409
410 if (ast_strlen_zero(args.signame)) {
411 ast_log(LOG_WARNING, "Missing signal name\n");
412 return -1;
413 }
414 if (strlen(args.signame) >= AST_MAX_CONTEXT) {
415 ast_log(LOG_WARNING, "Signal name '%s' is too long\n", args.signame);
416 return -1;
417 }
418
419 if (send_signal(args.signame, args.payload)) {
420 pbx_builtin_setvar_helper(chan, "SIGNALSTATUS", "FAILURE");
421 } else {
422 pbx_builtin_setvar_helper(chan, "SIGNALSTATUS", "SUCCESS");
423 }
424
425 return 0;
426}
static int send_signal(char *signame, char *payload)
Definition: app_signal.c:296
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define AST_MAX_CONTEXT
Definition: channel.h:135
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
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.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
const char * args

References args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log, AST_MAX_CONTEXT, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), LOG_WARNING, signalitem::payload, pbx_builtin_setvar_helper(), and send_signal().

Referenced by load_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 428 of file app_signal.c.

429{
430 struct signalitem *s;
431 int res = 0;
432
433 /* To avoid a locking nightmare, and for logistical reasons, this module
434 * will refuse to unload if watchers > 0. That way we know a signal's
435 * pipe won't disappear while it's being used. */
436
438 /* Don't just use AST_RWLIST_REMOVE_HEAD, because if dealloc_signal fails, it should stay in the list. */
440 int mres = dealloc_signal(s);
441 res |= mres;
442 if (!mres) {
444 }
445 }
448
449 /* One or more signals still has watchers. */
450 if (res) {
451 ast_log(LOG_WARNING, "One or more signals is currently in use. Unload failed.\n");
452 return res;
453 }
454
457
458 return res;
459}
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References app, app2, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_log, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_unregister_application(), dealloc_signal(), and LOG_WARNING.

◆ wait_for_signal_or_hangup()

static int wait_for_signal_or_hangup ( struct ast_channel chan,
char *  signame,
int  timeout 
)
static

Definition at line 219 of file app_signal.c.

220{
221 struct signalitem *s = NULL;
222 int ms, remaining_time, res = 1, goaway = 0;
223 struct timeval start;
224 struct ast_frame *frame = NULL;
225
226 remaining_time = timeout;
227 start = ast_tvnow();
228
229 s = get_signal(signame, 1);
230
231 ast_mutex_lock(&s->lock);
232 s->watchers = s->watchers + 1; /* we unlock, because a) other people need to use this and */
233 ast_mutex_unlock(&s->lock); /* b) the signal will be available to us as long as watchers > 0 */
234
235 while (timeout == 0 || remaining_time > 0) {
236 int ofd, exception;
237
238 ms = 1000;
239 errno = 0;
240 if (ast_waitfor_nandfds(&chan, 1, &s->sig_alert_pipe[0], 1, &exception, &ofd, &ms)) { /* channel won */
241 if (!(frame = ast_read(chan))) { /* channel hung up */
242 ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
243 res = -1;
244 break;
245 } else {
246 ast_frfree(frame); /* handle frames */
247 }
248 } else if (ofd == s->sig_alert_pipe[0]) { /* fd won */
250 ast_debug(1, "Alert pipe has data for us\n");
251 res = 0;
252 break;
253 } else {
254 ast_debug(1, "Alert pipe does not have data for us\n");
255 }
256 } else { /* nobody won */
257 if (ms && (ofd < 0)) {
258 if (!((errno == 0) || (errno == EINTR))) {
259 ast_log(LOG_WARNING, "Something bad happened while channel '%s' was polling.\n", ast_channel_name(chan));
260 break;
261 }
262 } /* else, nothing happened */
263 }
264 if (timeout) {
265 remaining_time = ast_remaining_ms(start, timeout);
266 }
267 }
268
269 /* WRLOCK the list so that if we're going to destroy the signal now, nobody else can grab it before that happens. */
271 ast_mutex_lock(&s->lock);
272 if (s->payload) {
273 pbx_builtin_setvar_helper(chan, "WAITFORSIGNALPAYLOAD", s->payload);
274 }
275 s->watchers = s->watchers - 1;
276 if (s->watchers) { /* folks are still waiting for this, pass it on... */
277 int save_errno = errno;
279 ast_log(LOG_WARNING, "%s: write() failed: %s\n", __FUNCTION__, strerror(errno));
280 }
281 errno = save_errno;
282 } else { /* nobody else is waiting for this */
283 goaway = 1; /* we were the last guy using this, so mark signal item for destruction */
284 }
286
287 if (goaway) {
288 /* remove_signal calls ast_mutex_destroy, so don't call it with the mutex itself locked. */
289 remove_signal(signame);
290 }
292
293 return res;
294}
@ AST_ALERT_READ_SUCCESS
Definition: alertpipe.h:25
ast_alert_status_t ast_alertpipe_read(int alert_pipe[2])
Read an event from an alert pipe.
Definition: alertpipe.c:102
static int remove_signal(char *sname)
Definition: app_signal.c:174
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_channel * ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds, int *exception, int *outfd, int *ms)
Waits for activity on a group of channels.
Definition: channel.c:2988
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
#define ast_frfree(fr)
Data structure associated with a single frame of data.
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:2281
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159

References AST_ALERT_READ_SUCCESS, ast_alertpipe_read(), ast_alertpipe_write(), ast_channel_name(), ast_debug, ast_frfree, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_read(), ast_remaining_ms(), AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_tvnow(), ast_waitfor_nandfds(), errno, get_signal(), signalitem::lock, LOG_WARNING, NULL, signalitem::payload, pbx_builtin_setvar_helper(), remove_signal(), signalitem::sig_alert_pipe, and signalitem::watchers.

Referenced by waitsignal_exec().

◆ waitsignal_exec()

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

Definition at line 336 of file app_signal.c.

337{
338 char *argcopy;
339 int r = 0, timeoutms = 0;
340 double timeout = 0;
341
343 AST_APP_ARG(signame);
344 AST_APP_ARG(sigtimeout);
345 );
346
347 if (ast_strlen_zero(data)) {
348 ast_log(LOG_WARNING, "Signal() requires arguments\n");
349 return -1;
350 }
351
352 argcopy = ast_strdupa(data);
353 AST_STANDARD_APP_ARGS(args, argcopy);
354
355 if (ast_strlen_zero(args.signame)) {
356 ast_log(LOG_WARNING, "Missing signal name\n");
357 return -1;
358 }
359 if (strlen(args.signame) >= AST_MAX_CONTEXT) {
360 ast_log(LOG_WARNING, "Signal name '%s' is too long\n", args.signame);
361 return -1;
362 }
363 if (!ast_strlen_zero(args.sigtimeout)) {
364 if (sscanf(args.sigtimeout, "%30lg", &timeout) != 1 || timeout < 0) {
365 ast_log(LOG_WARNING, "Invalid timeout provided: %s. Defaulting to no timeout.\n", args.sigtimeout);
366 } else {
367 timeoutms = timeout * 1000; /* sec to msec */
368 }
369 }
370
371 if (timeout > 0) {
372 ast_debug(1, "Waiting for signal '%s' for %d ms\n", args.signame, timeoutms);
373 } else {
374 ast_debug(1, "Waiting for signal '%s', indefinitely\n", args.signame);
375 }
376
377 r = wait_for_signal_or_hangup(chan, args.signame, timeoutms);
378
379 if (r == 1) {
380 ast_verb(3, "Channel '%s' timed out, waiting for signal '%s'\n", ast_channel_name(chan), args.signame);
381 pbx_builtin_setvar_helper(chan, "WAITFORSIGNALSTATUS", "TIMEOUT");
382 } else if (!r) {
383 ast_verb(3, "Received signal '%s' on channel '%s'\n", args.signame, ast_channel_name(chan));
384 pbx_builtin_setvar_helper(chan, "WAITFORSIGNALSTATUS", "SIGNALED");
385 } else {
386 pbx_builtin_setvar_helper(chan, "WAITFORSIGNALSTATUS", "HANGUP");
387 ast_verb(3, "Channel '%s' hung up\n", ast_channel_name(chan));
388 return -1;
389 }
390
391 return 0;
392}
static int wait_for_signal_or_hangup(struct ast_channel *chan, char *signame, int timeout)
Definition: app_signal.c:219
#define ast_verb(level,...)

References args, AST_APP_ARG, ast_channel_name(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_MAX_CONTEXT, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), ast_verb, LOG_WARNING, pbx_builtin_setvar_helper(), and wait_for_signal_or_hangup().

Referenced by load_module().

Variable Documentation

◆ app

const char* const app = "Signal"
static

Definition at line 122 of file app_signal.c.

Referenced by load_module(), and unload_module().

◆ app2

const char* const app2 = "WaitForSignal"
static

Definition at line 123 of file app_signal.c.

Referenced by load_module(), and unload_module().

◆ signals

struct signals signals = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static