#include "asterisk.h"
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <utime.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/callerid.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
Go to the source code of this file.
Data Structures | |
struct | outgoing |
Enumerations | |
enum | { SPOOL_FLAG_ALWAYS_DELETE = (1 << 0), SPOOL_FLAG_ARCHIVE = (1 << 1) } |
Functions | |
static int | apply_outgoing (struct outgoing *o, char *fn, FILE *f) |
AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Outgoing Spool Support") | |
static void * | attempt_thread (void *data) |
static void | free_outgoing (struct outgoing *o) |
static void | init_outgoing (struct outgoing *o) |
static void | launch_service (struct outgoing *o) |
static int | load_module (void) |
static int | remove_from_queue (struct outgoing *o, const char *status) |
Remove a call file from the outgoing queue optionally moving it in the archive dir. | |
static void | safe_append (struct outgoing *o, time_t now, char *s) |
static int | scan_service (char *fn, time_t now, time_t atime) |
static void * | scan_thread (void *unused) |
static int | unload_module (void) |
Variables | |
static char | qdir [255] |
static char | qdonedir [255] |
Definition in file pbx_spool.c.
anonymous enum |
Definition at line 56 of file pbx_spool.c.
00056 { 00057 /*! Always delete the call file after a call succeeds or the 00058 * maximum number of retries is exceeded, even if the 00059 * modification time of the call file is in the future. 00060 */ 00061 SPOOL_FLAG_ALWAYS_DELETE = (1 << 0), 00062 /* Don't unlink the call file after processing, move in qdonedir */ 00063 SPOOL_FLAG_ARCHIVE = (1 << 1) 00064 };
static int apply_outgoing | ( | struct outgoing * | o, | |
char * | fn, | |||
FILE * | f | |||
) | [static] |
Definition at line 129 of file pbx_spool.c.
References outgoing::account, outgoing::app, ast_callerid_split(), ast_log(), ast_parse_allow_disallow(), ast_set2_flag, ast_strlen_zero(), ast_true(), ast_variable_new(), outgoing::callingpid, outgoing::cid_name, outgoing::cid_num, outgoing::context, outgoing::data, outgoing::dest, outgoing::exten, outgoing::fn, outgoing::format, last, lineno, LOG_NOTICE, LOG_WARNING, outgoing::maxretries, outgoing::options, outgoing::priority, outgoing::retries, outgoing::retrytime, SPOOL_FLAG_ALWAYS_DELETE, SPOOL_FLAG_ARCHIVE, strsep(), outgoing::tech, var, outgoing::vars, and outgoing::waittime.
Referenced by scan_service().
00130 { 00131 char buf[256]; 00132 char *c, *c2; 00133 int lineno = 0; 00134 struct ast_variable *var, *last = o->vars; 00135 00136 while (last && last->next) { 00137 last = last->next; 00138 } 00139 00140 while(fgets(buf, sizeof(buf), f)) { 00141 lineno++; 00142 /* Trim comments */ 00143 c = buf; 00144 while ((c = strchr(c, '#'))) { 00145 if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t')) 00146 *c = '\0'; 00147 else 00148 c++; 00149 } 00150 00151 c = buf; 00152 while ((c = strchr(c, ';'))) { 00153 if ((c > buf) && (c[-1] == '\\')) { 00154 memmove(c - 1, c, strlen(c) + 1); 00155 c++; 00156 } else { 00157 *c = '\0'; 00158 break; 00159 } 00160 } 00161 00162 /* Trim trailing white space */ 00163 while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33) 00164 buf[strlen(buf) - 1] = '\0'; 00165 if (!ast_strlen_zero(buf)) { 00166 c = strchr(buf, ':'); 00167 if (c) { 00168 *c = '\0'; 00169 c++; 00170 while ((*c) && (*c < 33)) 00171 c++; 00172 #if 0 00173 printf("'%s' is '%s' at line %d\n", buf, c, lineno); 00174 #endif 00175 if (!strcasecmp(buf, "channel")) { 00176 ast_copy_string(o->tech, c, sizeof(o->tech)); 00177 if ((c2 = strchr(o->tech, '/'))) { 00178 *c2 = '\0'; 00179 c2++; 00180 ast_copy_string(o->dest, c2, sizeof(o->dest)); 00181 } else { 00182 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn); 00183 o->tech[0] = '\0'; 00184 } 00185 } else if (!strcasecmp(buf, "callerid")) { 00186 ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num)); 00187 } else if (!strcasecmp(buf, "application")) { 00188 ast_copy_string(o->app, c, sizeof(o->app)); 00189 } else if (!strcasecmp(buf, "data")) { 00190 ast_copy_string(o->data, c, sizeof(o->data)); 00191 } else if (!strcasecmp(buf, "maxretries")) { 00192 if (sscanf(c, "%d", &o->maxretries) != 1) { 00193 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn); 00194 o->maxretries = 0; 00195 } 00196 } else if (!strcasecmp(buf, "codecs")) { 00197 ast_parse_allow_disallow(NULL, &o->format, c, 1); 00198 } else if (!strcasecmp(buf, "context")) { 00199 ast_copy_string(o->context, c, sizeof(o->context)); 00200 } else if (!strcasecmp(buf, "extension")) { 00201 ast_copy_string(o->exten, c, sizeof(o->exten)); 00202 } else if (!strcasecmp(buf, "priority")) { 00203 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) { 00204 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn); 00205 o->priority = 1; 00206 } 00207 } else if (!strcasecmp(buf, "retrytime")) { 00208 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) { 00209 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn); 00210 o->retrytime = 300; 00211 } 00212 } else if (!strcasecmp(buf, "waittime")) { 00213 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) { 00214 ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, fn); 00215 o->waittime = 45; 00216 } 00217 } else if (!strcasecmp(buf, "retry")) { 00218 o->retries++; 00219 } else if (!strcasecmp(buf, "startretry")) { 00220 if (sscanf(c, "%ld", &o->callingpid) != 1) { 00221 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n"); 00222 o->callingpid = 0; 00223 } 00224 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) { 00225 o->callingpid = 0; 00226 o->retries++; 00227 } else if (!strcasecmp(buf, "delayedretry")) { 00228 } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) { 00229 c2 = c; 00230 strsep(&c2, "="); 00231 if (c2) { 00232 var = ast_variable_new(c, c2); 00233 if (var) { 00234 /* Always insert at the end, because some people want to treat the spool file as a script */ 00235 if (last) { 00236 last->next = var; 00237 } else { 00238 o->vars = var; 00239 } 00240 last = var; 00241 } 00242 } else 00243 ast_log(LOG_WARNING, "Malformed \"%s\" argument. Should be \"%s: variable=value\"\n", buf, buf); 00244 } else if (!strcasecmp(buf, "account")) { 00245 ast_copy_string(o->account, c, sizeof(o->account)); 00246 } else if (!strcasecmp(buf, "alwaysdelete")) { 00247 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ALWAYS_DELETE); 00248 } else if (!strcasecmp(buf, "archive")) { 00249 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE); 00250 } else { 00251 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn); 00252 } 00253 } else 00254 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn); 00255 } 00256 } 00257 ast_copy_string(o->fn, fn, sizeof(o->fn)); 00258 if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) { 00259 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn); 00260 return -1; 00261 } 00262 return 0; 00263 }
AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
"Outgoing Spool Support" | ||||
) |
static void* attempt_thread | ( | void * | data | ) | [static] |
Definition at line 341 of file pbx_spool.c.
References outgoing::account, outgoing::app, ast_channel_reason2str(), ast_log(), ast_pbx_outgoing_app(), ast_pbx_outgoing_exten(), ast_strlen_zero(), ast_verbose(), outgoing::cid_name, outgoing::cid_num, outgoing::context, outgoing::data, outgoing::dest, outgoing::exten, outgoing::format, free_outgoing(), LOG_EVENT, LOG_NOTICE, outgoing::maxretries, option_verbose, outgoing::priority, remove_from_queue(), outgoing::retries, safe_append(), outgoing::tech, outgoing::vars, VERBOSE_PREFIX_3, and outgoing::waittime.
Referenced by launch_service().
00342 { 00343 struct outgoing *o = data; 00344 int res, reason; 00345 if (!ast_strlen_zero(o->app)) { 00346 if (option_verbose > 2) 00347 ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for application %s(%s) (Retry %d)\n", o->tech, o->dest, o->app, o->data, o->retries); 00348 res = ast_pbx_outgoing_app(o->tech, o->format, o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL); 00349 } else { 00350 if (option_verbose > 2) 00351 ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for %s@%s:%d (Retry %d)\n", o->tech, o->dest, o->exten, o->context,o->priority, o->retries); 00352 res = ast_pbx_outgoing_exten(o->tech, o->format, o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL); 00353 } 00354 if (res) { 00355 ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason)); 00356 if (o->retries >= o->maxretries + 1) { 00357 /* Max retries exceeded */ 00358 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt%s\n", o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : ""); 00359 remove_from_queue(o, "Expired"); 00360 } else { 00361 /* Notate that the call is still active */ 00362 safe_append(o, time(NULL), "EndRetry"); 00363 } 00364 } else { 00365 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest); 00366 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest); 00367 remove_from_queue(o, "Completed"); 00368 } 00369 free_outgoing(o); 00370 return NULL; 00371 }
static void free_outgoing | ( | struct outgoing * | o | ) | [static] |
Definition at line 124 of file pbx_spool.c.
References free.
Referenced by attempt_thread(), launch_service(), and scan_service().
00125 { 00126 free(o); 00127 }
static void init_outgoing | ( | struct outgoing * | o | ) | [static] |
Definition at line 114 of file pbx_spool.c.
References AST_FORMAT_SLINEAR, ast_set_flag, outgoing::format, outgoing::options, outgoing::priority, outgoing::retrytime, SPOOL_FLAG_ALWAYS_DELETE, and outgoing::waittime.
Referenced by scan_service().
00115 { 00116 memset(o, 0, sizeof(struct outgoing)); 00117 o->priority = 1; 00118 o->retrytime = 300; 00119 o->waittime = 45; 00120 o->format = AST_FORMAT_SLINEAR; 00121 ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE); 00122 }
static void launch_service | ( | struct outgoing * | o | ) | [static] |
Definition at line 373 of file pbx_spool.c.
References ast_log(), ast_pthread_create, attempt_thread(), free_outgoing(), LOG_WARNING, and t.
Referenced by scan_service().
00374 { 00375 pthread_t t; 00376 pthread_attr_t attr; 00377 int ret; 00378 pthread_attr_init(&attr); 00379 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 00380 if ((ret = ast_pthread_create(&t,&attr,attempt_thread, o)) != 0) { 00381 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret); 00382 free_outgoing(o); 00383 } 00384 pthread_attr_destroy(&attr); 00385 }
static int load_module | ( | void | ) | [static] |
Definition at line 500 of file pbx_spool.c.
References ast_config_AST_SPOOL_DIR, ast_log(), ast_pthread_create_background, errno, LOG_WARNING, scan_thread(), and thread.
00501 { 00502 pthread_t thread; 00503 pthread_attr_t attr; 00504 int ret; 00505 snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing"); 00506 if (mkdir(qdir, 0700) && (errno != EEXIST)) { 00507 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir); 00508 return 0; 00509 } 00510 snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done"); 00511 pthread_attr_init(&attr); 00512 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 00513 if ((ret = ast_pthread_create_background(&thread,&attr,scan_thread, NULL)) != 0) { 00514 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret); 00515 return -1; 00516 } 00517 pthread_attr_destroy(&attr); 00518 return 0; 00519 }
static int remove_from_queue | ( | struct outgoing * | o, | |
const char * | status | |||
) | [static] |
Remove a call file from the outgoing queue optionally moving it in the archive dir.
o | the pointer to outgoing struct | |
status | the exit status of the call. Can be "Completed", "Failed" or "Expired" |
Definition at line 292 of file pbx_spool.c.
References ast_log(), ast_test_flag, errno, f, outgoing::fn, LOG_WARNING, outgoing::options, SPOOL_FLAG_ALWAYS_DELETE, and SPOOL_FLAG_ARCHIVE.
00293 { 00294 int fd; 00295 FILE *f; 00296 char newfn[256]; 00297 const char *bname; 00298 00299 if (!ast_test_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE)) { 00300 struct stat current_file_status; 00301 00302 if (!stat(o->fn, ¤t_file_status)) 00303 if (time(NULL) < current_file_status.st_mtime) 00304 return 0; 00305 } 00306 00307 if (!ast_test_flag(&o->options, SPOOL_FLAG_ARCHIVE)) { 00308 unlink(o->fn); 00309 return 0; 00310 } 00311 if (mkdir(qdonedir, 0700) && (errno != EEXIST)) { 00312 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir); 00313 unlink(o->fn); 00314 return -1; 00315 } 00316 fd = open(o->fn, O_WRONLY|O_APPEND); 00317 if (fd > -1) { 00318 f = fdopen(fd, "a"); 00319 if (f) { 00320 fprintf(f, "Status: %s\n", status); 00321 fclose(f); 00322 } else 00323 close(fd); 00324 } 00325 00326 bname = strrchr(o->fn,'/'); 00327 if (bname == NULL) 00328 bname = o->fn; 00329 else 00330 bname++; 00331 snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname); 00332 /* a existing call file the archive dir is overwritten */ 00333 unlink(newfn); 00334 if (rename(o->fn, newfn) != 0) { 00335 unlink(o->fn); 00336 return -1; 00337 } else 00338 return 0; 00339 }
static void safe_append | ( | struct outgoing * | o, | |
time_t | now, | |||
char * | s | |||
) | [static] |
Definition at line 265 of file pbx_spool.c.
References ast_log(), ast_mainpid, errno, f, outgoing::fn, LOG_WARNING, outgoing::retries, and outgoing::retrytime.
Referenced by attempt_thread(), and scan_service().
00266 { 00267 int fd; 00268 FILE *f; 00269 struct utimbuf tbuf; 00270 fd = open(o->fn, O_WRONLY|O_APPEND); 00271 if (fd > -1) { 00272 f = fdopen(fd, "a"); 00273 if (f) { 00274 fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now); 00275 fclose(f); 00276 } else 00277 close(fd); 00278 /* Update the file time */ 00279 tbuf.actime = now; 00280 tbuf.modtime = now + o->retrytime; 00281 if (utime(o->fn, &tbuf)) 00282 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno)); 00283 } 00284 }
static int scan_service | ( | char * | fn, | |
time_t | now, | |||
time_t | atime | |||
) | [static] |
Definition at line 387 of file pbx_spool.c.
References apply_outgoing(), ast_log(), ast_mainpid, errno, f, free_outgoing(), init_outgoing(), launch_service(), LOG_DEBUG, LOG_EVENT, LOG_WARNING, malloc, remove_from_queue(), and safe_append().
Referenced by scan_thread().
00388 { 00389 struct outgoing *o; 00390 FILE *f; 00391 o = malloc(sizeof(struct outgoing)); 00392 if (o) { 00393 init_outgoing(o); 00394 f = fopen(fn, "r+"); 00395 if (f) { 00396 if (!apply_outgoing(o, fn, f)) { 00397 #if 0 00398 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries); 00399 #endif 00400 fclose(f); 00401 if (o->retries <= o->maxretries) { 00402 now += o->retrytime; 00403 if (o->callingpid && (o->callingpid == ast_mainpid)) { 00404 safe_append(o, time(NULL), "DelayedRetry"); 00405 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn); 00406 free_outgoing(o); 00407 } else { 00408 /* Increment retries */ 00409 o->retries++; 00410 /* If someone else was calling, they're presumably gone now 00411 so abort their retry and continue as we were... */ 00412 if (o->callingpid) 00413 safe_append(o, time(NULL), "AbortRetry"); 00414 00415 safe_append(o, now, "StartRetry"); 00416 launch_service(o); 00417 } 00418 return now; 00419 } else { 00420 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt%s\n", o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : ""); 00421 free_outgoing(o); 00422 remove_from_queue(o, "Expired"); 00423 return 0; 00424 } 00425 } else { 00426 free_outgoing(o); 00427 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn); 00428 fclose(f); 00429 remove_from_queue(o, "Failed"); 00430 } 00431 } else { 00432 free_outgoing(o); 00433 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno)); 00434 remove_from_queue(o, "Failed"); 00435 } 00436 } else 00437 ast_log(LOG_WARNING, "Out of memory :(\n"); 00438 return -1; 00439 }
static void* scan_thread | ( | void * | unused | ) | [static] |
Definition at line 441 of file pbx_spool.c.
References ast_log(), errno, last, LOG_WARNING, and scan_service().
Referenced by load_module().
00442 { 00443 struct stat st; 00444 DIR *dir; 00445 struct dirent *de; 00446 char fn[256]; 00447 int res; 00448 time_t last = 0, next = 0, now; 00449 for(;;) { 00450 /* Wait a sec */ 00451 sleep(1); 00452 time(&now); 00453 if (!stat(qdir, &st)) { 00454 if ((st.st_mtime != last) || (next && (now > next))) { 00455 #if 0 00456 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime); 00457 printf("Ooh, something changed / timeout\n"); 00458 #endif 00459 next = 0; 00460 last = st.st_mtime; 00461 dir = opendir(qdir); 00462 if (dir) { 00463 while((de = readdir(dir))) { 00464 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name); 00465 if (!stat(fn, &st)) { 00466 if (S_ISREG(st.st_mode)) { 00467 if (st.st_mtime <= now) { 00468 res = scan_service(fn, now, st.st_atime); 00469 if (res > 0) { 00470 /* Update next service time */ 00471 if (!next || (res < next)) { 00472 next = res; 00473 } 00474 } else if (res) 00475 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn); 00476 } else { 00477 /* Update "next" update if necessary */ 00478 if (!next || (st.st_mtime < next)) 00479 next = st.st_mtime; 00480 } 00481 } 00482 } else 00483 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno)); 00484 } 00485 closedir(dir); 00486 } else 00487 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno)); 00488 } 00489 } else 00490 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir); 00491 } 00492 return NULL; 00493 }
static int unload_module | ( | void | ) | [static] |
char qdir[255] [static] |
Definition at line 66 of file pbx_spool.c.
char qdonedir[255] [static] |
Definition at line 67 of file pbx_spool.c.