| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/file.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/app.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static const char * const app = "Signal"; |
| 131 |
static const char * const app2 = "WaitForSignal"; |
| 132 |
|
| 133 |
struct signalitem { |
| 134 |
ast_mutex_t lock; |
| 135 |
char name[AST_MAX_CONTEXT]; |
| 136 |
int sig_alert_pipe[2]; |
| 137 |
int watchers; |
| 138 |
unsigned int signaled:1; |
| 139 |
char *payload; |
| 140 |
AST_LIST_ENTRY(signalitem) entry; |
| 141 |
}; |
| 142 |
|
| 143 |
static AST_RWLIST_HEAD_STATIC(signals, signalitem); |
| 144 |
|
| 145 |
static struct signalitem *alloc_signal(const char *sname) |
| 146 |
{ |
| 147 |
struct signalitem *s; |
| 148 |
|
| 149 |
if (!(s = ast_calloc(1, sizeof(*s)))) { |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
ast_mutex_init(&s->lock); |
| 154 |
ast_copy_string(s->name, sname, sizeof(s->name)); |
| 155 |
|
| 156 |
s->sig_alert_pipe[0] = -1; |
| 157 |
s->sig_alert_pipe[1] = -1; |
| 158 |
s->watchers = 0; |
| 159 |
s->payload = NULL; |
| 160 |
ast_alertpipe_init(s->sig_alert_pipe); |
| 161 |
|
| 162 |
return s; |
| 163 |
} |
| 164 |
|
| 165 |
static int dealloc_signal(struct signalitem *s) |
| 166 |
{ |
| 167 |
if (s->watchers) { |
| 168 |
ast_debug(1, "Signal '%s' is still being used by %d listener(s)\n", s->name, s->watchers); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
ast_alertpipe_close(s->sig_alert_pipe); |
| 172 |
ast_mutex_destroy(&s->lock); |
| 173 |
if (s->payload) { |
| 174 |
ast_free(s->payload); |
| 175 |
s->payload = NULL; |
| 176 |
} |
| 177 |
ast_free(s); |
| 178 |
s = NULL; |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
static int remove_signal(char *sname) |
| 183 |
{ |
| 184 |
int res = -1; |
| 185 |
struct signalitem *s; |
| 186 |
|
| 187 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&signals, s, entry) { |
| 188 |
if (!strcmp(s->name, sname)) { |
| 189 |
AST_LIST_REMOVE_CURRENT(entry); |
| 190 |
res = dealloc_signal(s); |
| 191 |
ast_debug(1, "Removed signal '%s'\n", sname); |
| 192 |
} |
| 193 |
} |
| 194 |
AST_LIST_TRAVERSE_SAFE_END; |
| 195 |
|
| 196 |
return res; |
| 197 |
} |
| 198 |
|
| 199 |
static struct signalitem *get_signal(char *sname, int addnew) |
| 200 |
{ |
| 201 |
struct signalitem *s = NULL; |
| 202 |
AST_RWLIST_WRLOCK(&signals); |
| 203 |
AST_LIST_TRAVERSE(&signals, s, entry) { |
| 204 |
if (!strcasecmp(s->name, sname)) { |
| 205 |
ast_debug(1, "Using existing signal item '%s'\n", sname); |
| 206 |
break; |
| 207 |
} |
| 208 |
} |
| 209 |
if (!s) { |
| 210 |
if (addnew) { |
| 211 |
s = alloc_signal(sname); |
| 212 |
|
| 213 |
if (s) { |
| 214 |
ast_debug(1, "Created new signal item '%s'\n", sname); |
| 215 |
AST_RWLIST_INSERT_HEAD(&signals, s, entry); |
| 216 |
} else { |
| 217 |
ast_log(LOG_WARNING, "Failed to create signal item for '%s'\n", sname); |
| 218 |
} |
| 219 |
} else { |
| 220 |
ast_debug(1, "Signal '%s' doesn't exist, and not creating it\n", sname); |
| 221 |
} |
| 222 |
} |
| 223 |
AST_RWLIST_UNLOCK(&signals); |
| 224 |
return s; |
| 225 |
} |
| 226 |
|
| 227 |
static int wait_for_signal_or_hangup(struct ast_channel *chan, char *signame, int timeout) |
| 228 |
{ |
| 229 |
struct signalitem *s = NULL; |
| 230 |
int ms, remaining_time, res = 1, goaway = 0; |
| 231 |
struct timeval start; |
| 232 |
struct ast_frame *frame = NULL; |
| 233 |
|
| 234 |
remaining_time = timeout; |
| 235 |
start = ast_tvnow(); |
| 236 |
|
| 237 |
s = get_signal(signame, 1); |
| 238 |
|
| 239 |
ast_mutex_lock(&s->lock); |
| 240 |
s->watchers = s->watchers + 1; |
| 241 |
ast_mutex_unlock(&s->lock); |
| 242 |
|
| 243 |
while (timeout == 0 || remaining_time > 0) { |
| 244 |
int ofd, exception; |
| 245 |
|
| 246 |
ms = 1000; |
| 247 |
errno = 0; |
| 248 |
if (ast_waitfor_nandfds(&chan, 1, &s->sig_alert_pipe[0], 1, &exception, &ofd, &ms)) { |
| 249 |
if (!(frame = ast_read(chan))) { |
| 250 |
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan)); |
| 251 |
res = -1; |
| 252 |
break; |
| 253 |
} else { |
| 254 |
ast_frfree(frame); |
| 255 |
} |
| 256 |
} else if (ofd == s->sig_alert_pipe[0]) { |
| 257 |
if (ast_alertpipe_read(s->sig_alert_pipe) == AST_ALERT_READ_SUCCESS) { |
| 258 |
ast_debug(1, "Alert pipe has data for us\n"); |
| 259 |
res = 0; |
| 260 |
break; |
| 261 |
} else { |
| 262 |
ast_debug(1, "Alert pipe does not have data for us\n"); |
| 263 |
} |
| 264 |
} else { |
| 265 |
if (ms && (ofd < 0)) { |
| 266 |
if (!((errno == 0) || (errno == EINTR))) { |
| 267 |
ast_log(LOG_WARNING, "Something bad happened while channel '%s' was polling.\n", ast_channel_name(chan)); |
| 268 |
break; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
if (timeout) { |
| 273 |
remaining_time = ast_remaining_ms(start, timeout); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
AST_RWLIST_WRLOCK(&signals); |
| 279 |
ast_mutex_lock(&s->lock); |
| 280 |
if (s->payload) { |
| 281 |
pbx_builtin_setvar_helper(chan, "WAITFORSIGNALPAYLOAD", s->payload); |
| 282 |
} |
| 283 |
s->watchers = s->watchers - 1; |
| 284 |
if (s->watchers) { |
| 285 |
int save_errno = errno; |
| 286 |
if (ast_alertpipe_write(s->sig_alert_pipe)) { |
| 287 |
ast_log(LOG_WARNING, "%s: write() failed: %s\n", __FUNCTION__, strerror(errno)); |
| 288 |
} |
| 289 |
errno = save_errno; |
| 290 |
} else { |
| 291 |
goaway = 1; |
| 292 |
} |
| 293 |
ast_mutex_unlock(&s->lock); |
| 294 |
|
| 295 |
if (goaway) { |
| 296 |
|
| 297 |
remove_signal(signame); |
| 298 |
} |
| 299 |
AST_RWLIST_UNLOCK(&signals); |
| 300 |
|
| 301 |
return res; |
| 302 |
} |
| 303 |
|
| 304 |
static int send_signal(char *signame, char *payload) |
| 305 |
{ |
| 306 |
struct signalitem *s; |
| 307 |
int save_errno = errno; |
| 308 |
int res = 0; |
| 309 |
|
| 310 |
s = get_signal(signame, 0); |
| 311 |
|
| 312 |
if (!s) { |
| 313 |
return -1; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
ast_mutex_lock(&s->lock); |
| 318 |
s->signaled = 1; |
| 319 |
if (payload && *payload) { |
| 320 |
int len = strlen(payload); |
| 321 |
if (s->payload) { |
| 322 |
ast_free(s->payload); |
| 323 |
s->payload = NULL; |
| 324 |
} |
| 325 |
s->payload = ast_malloc(len + 1); |
| 326 |
if (!s->payload) { |
| 327 |
ast_log(LOG_WARNING, "Failed to allocate signal payload '%s'\n", payload); |
| 328 |
} else { |
| 329 |
ast_copy_string(s->payload, payload, len + 1); |
| 330 |
} |
| 331 |
} |
| 332 |
if (ast_alertpipe_write(s->sig_alert_pipe)) { |
| 333 |
ast_log(LOG_WARNING, "%s: write() failed: %s\n", __FUNCTION__, strerror(errno)); |
| 334 |
s->signaled = 0; |
| 335 |
res = -1; |
| 336 |
} |
| 337 |
errno = save_errno; |
| 338 |
ast_debug(1, "Sent '%s' signal to %d listeners\n", signame, s->watchers); |
| 339 |
ast_mutex_unlock(&s->lock); |
| 340 |
|
| 341 |
return res; |
| 342 |
} |
| 343 |
|
| 344 |
static int waitsignal_exec(struct ast_channel *chan, const char *data) |
| 345 |
{ |
| 346 |
char *argcopy; |
| 347 |
int r = 0, timeoutms = 0; |
| 348 |
double timeout = 0; |
| 349 |
|
| 350 |
AST_DECLARE_APP_ARGS(args, |
| 351 |
AST_APP_ARG(signame); |
| 352 |
AST_APP_ARG(sigtimeout); |
| 353 |
); |
| 354 |
|
| 355 |
if (ast_strlen_zero(data)) { |
| 356 |
ast_log(LOG_WARNING, "Signal() requires arguments\n"); |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
argcopy = ast_strdupa(data); |
| 361 |
AST_STANDARD_APP_ARGS(args, argcopy); |
| 362 |
|
| 363 |
if (ast_strlen_zero(args.signame)) { |
| 364 |
ast_log(LOG_WARNING, "Missing signal name\n"); |
| 365 |
return -1; |
| 366 |
} |
| 367 |
if (strlen(args.signame) >= AST_MAX_CONTEXT) { |
| 368 |
ast_log(LOG_WARNING, "Signal name '%s' is too long\n", args.signame); |
| 369 |
return -1; |
| 370 |
} |
| 371 |
if (!ast_strlen_zero(args.sigtimeout)) { |
| 372 |
if (sscanf(args.sigtimeout, "%30lg", &timeout) != 1 || timeout < 0) { |
| 373 |
ast_log(LOG_WARNING, "Invalid timeout provided: %s. Defaulting to no timeout.\n", args.sigtimeout); |
| 374 |
} else { |
| 375 |
timeoutms = timeout * 1000; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if (timeout > 0) { |
| 380 |
ast_debug(1, "Waiting for signal '%s' for %d ms\n", args.signame, timeoutms); |
| 381 |
} else { |
| 382 |
ast_debug(1, "Waiting for signal '%s', indefinitely\n", args.signame); |
| 383 |
} |
| 384 |
|
| 385 |
r = wait_for_signal_or_hangup(chan, args.signame, timeoutms); |
| 386 |
|
| 387 |
if (r == 1) { |
| 388 |
ast_verb(3, "Channel '%s' timed out, waiting for signal '%s'\n", ast_channel_name(chan), args.signame); |
| 389 |
pbx_builtin_setvar_helper(chan, "WAITFORSIGNALSTATUS", "TIMEOUT"); |
| 390 |
} else if (!r) { |
| 391 |
ast_verb(3, "Received signal '%s' on channel '%s'\n", args.signame, ast_channel_name(chan)); |
| 392 |
pbx_builtin_setvar_helper(chan, "WAITFORSIGNALSTATUS", "SIGNALED"); |
| 393 |
} else { |
| 394 |
pbx_builtin_setvar_helper(chan, "WAITFORSIGNALSTATUS", "HANGUP"); |
| 395 |
ast_verb(3, "Channel '%s' hung up\n", ast_channel_name(chan)); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
|
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
static int signal_exec(struct ast_channel *chan, const char *data) |
| 403 |
{ |
| 404 |
char *argcopy; |
| 405 |
AST_DECLARE_APP_ARGS(args, |
| 406 |
AST_APP_ARG(signame); |
| 407 |
AST_APP_ARG(payload); |
| 408 |
); |
| 409 |
|
| 410 |
if (ast_strlen_zero(data)) { |
| 411 |
ast_log(LOG_WARNING, "Signal() requires arguments\n"); |
| 412 |
return -1; |
| 413 |
} |
| 414 |
|
| 415 |
argcopy = ast_strdupa(data); |
| 416 |
AST_STANDARD_APP_ARGS(args, argcopy); |
| 417 |
|
| 418 |
if (ast_strlen_zero(args.signame)) { |
| 419 |
ast_log(LOG_WARNING, "Missing signal name\n"); |
| 420 |
return -1; |
| 421 |
} |
| 422 |
if (strlen(args.signame) >= AST_MAX_CONTEXT) { |
| 423 |
ast_log(LOG_WARNING, "Signal name '%s' is too long\n", args.signame); |
| 424 |
return -1; |
| 425 |
} |
| 426 |
|
| 427 |
if (send_signal(args.signame, args.payload)) { |
| 428 |
pbx_builtin_setvar_helper(chan, "SIGNALSTATUS", "FAILURE"); |
| 429 |
} else { |
| 430 |
pbx_builtin_setvar_helper(chan, "SIGNALSTATUS", "SUCCESS"); |
| 431 |
} |
| 432 |
|
| 433 |
return 0; |
| 434 |
} |
| 435 |
|
| 436 |
static int unload_module(void) |
| 437 |
{ |
| 438 |
struct signalitem *s; |
| 439 |
int res = 0; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
AST_RWLIST_WRLOCK(&signals); |
| 446 |
|
| 447 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&signals, s, entry) { |
| 448 |
int mres = dealloc_signal(s); |
| 449 |
res |= mres; |
| 450 |
if (!mres) { |
| 451 |
AST_LIST_REMOVE_CURRENT(entry); |
| 452 |
} |
| 453 |
} |
| 454 |
AST_LIST_TRAVERSE_SAFE_END; |
| 455 |
AST_RWLIST_UNLOCK(&signals); |
| 456 |
|
| 457 |
|
| 458 |
if (res) { |
| 459 |
ast_log(LOG_WARNING, "One or more signals is currently in use. Unload failed.\n"); |
| 460 |
return res; |
| 461 |
} |
| 462 |
|
| 463 |
res |= ast_unregister_application(app); |
| 464 |
res |= ast_unregister_application(app2); |
| 465 |
|
| 466 |
return res; |
| 467 |
} |
| 468 |
|
| 469 |
static int load_module(void) |
| 470 |
{ |
| 471 |
int res; |
| 472 |
|
| 473 |
res = ast_register_application_xml(app, signal_exec); |
| 474 |
res |= ast_register_application_xml(app2, waitsignal_exec); |
| 475 |
|
| 476 |
return res; |
| 477 |
} |
| 478 |
|
| 479 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Channel Signaling Applications"); |