Wed Jan 8 2020 09:49:49

Asterisk developer's documentation


pbx_spool.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2010, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Full-featured outgoing call spool support
22  *
23  */
24 
25 /*** MODULEINFO
26  <support_level>core</support_level>
27  ***/
28 
29 #include "asterisk.h"
30 
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 383120 $")
32 
33 #include <sys/stat.h>
34 #include <time.h>
35 #include <utime.h>
36 #include <dirent.h>
37 #ifdef HAVE_INOTIFY
38 #include <sys/inotify.h>
39 #elif defined(HAVE_KQUEUE)
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <sys/event.h>
43 #include <fcntl.h>
44 #endif
45 
46 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
47 #include "asterisk/lock.h"
48 #include "asterisk/file.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/callerid.h"
52 #include "asterisk/pbx.h"
53 #include "asterisk/module.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/options.h"
56 
57 /*
58  * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
59  * The spool file contains a header
60  */
61 
62 enum {
63  /*! Always delete the call file after a call succeeds or the
64  * maximum number of retries is exceeded, even if the
65  * modification time of the call file is in the future.
66  */
68  /* Don't unlink the call file after processing, move in qdonedir */
69  SPOOL_FLAG_ARCHIVE = (1 << 1),
70 };
71 
72 static char qdir[255];
73 static char qdonedir[255];
74 
75 struct outgoing {
76  int retries; /*!< Current number of retries */
77  int maxretries; /*!< Maximum number of retries permitted */
78  int retrytime; /*!< How long to wait between retries (in seconds) */
79  int waittime; /*!< How long to wait for an answer */
80  long callingpid; /*!< PID which is currently calling */
81  format_t format; /*!< Formats (codecs) for this call */
83  AST_STRING_FIELD(fn); /*!< File name of call file */
84  AST_STRING_FIELD(tech); /*!< Which channel technology to use for outgoing call */
85  AST_STRING_FIELD(dest); /*!< Which device/line to use for outgoing call */
86  AST_STRING_FIELD(app); /*!< If application: Application name */
87  AST_STRING_FIELD(data); /*!< If application: Application data */
88  AST_STRING_FIELD(exten); /*!< If extension/context/priority: Extension in dialplan */
89  AST_STRING_FIELD(context); /*!< If extension/context/priority: Dialplan context */
90  AST_STRING_FIELD(cid_num); /*!< CallerID Information: Number/extension */
91  AST_STRING_FIELD(cid_name); /*!< CallerID Information: Name */
92  AST_STRING_FIELD(account); /*!< account code */
93  );
94  int priority; /*!< If extension/context/priority: Dialplan priority */
95  struct ast_variable *vars; /*!< Variables and Functions */
96  int maxlen; /*!< Maximum length of call */
97  struct ast_flags options; /*!< options */
98 };
99 
100 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
101 static void queue_file(const char *filename, time_t when);
102 #endif
103 
104 static void free_outgoing(struct outgoing *o)
105 {
106  if (o->vars) {
108  }
110  ast_free(o);
111 }
112 
113 static struct outgoing *new_outgoing(const char *fn)
114 {
115  struct outgoing *o;
116 
117  o = ast_calloc(1, sizeof(*o));
118  if (!o) {
119  return NULL;
120  }
121 
122  /* Initialize the new object. */
123  o->priority = 1;
124  o->retrytime = 300;
125  o->waittime = 45;
128  if (ast_string_field_init(o, 128)) {
129  /*
130  * No need to call free_outgoing here since the failure was to
131  * allocate string fields and no variables have been allocated
132  * yet.
133  */
134  ast_free(o);
135  return NULL;
136  }
137  ast_string_field_set(o, fn, fn);
138  if (ast_strlen_zero(o->fn)) {
139  /* String field set failed. Since this string is important we must fail. */
140  free_outgoing(o);
141  return NULL;
142  }
143 
144  return o;
145 }
146 
147 static int apply_outgoing(struct outgoing *o, FILE *f)
148 {
149  char buf[256];
150  char *c, *c2;
151  int lineno = 0;
152  struct ast_variable *var, *last = o->vars;
153 
154  while (last && last->next) {
155  last = last->next;
156  }
157 
158  while(fgets(buf, sizeof(buf), f)) {
159  lineno++;
160  /* Trim comments */
161  c = buf;
162  while ((c = strchr(c, '#'))) {
163  if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
164  *c = '\0';
165  else
166  c++;
167  }
168 
169  c = buf;
170  while ((c = strchr(c, ';'))) {
171  if ((c > buf) && (c[-1] == '\\')) {
172  memmove(c - 1, c, strlen(c) + 1);
173  c++;
174  } else {
175  *c = '\0';
176  break;
177  }
178  }
179 
180  /* Trim trailing white space */
181  ast_trim_blanks(buf);
182  if (ast_strlen_zero(buf)) {
183  continue;
184  }
185  c = strchr(buf, ':');
186  if (!c) {
187  ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, o->fn);
188  continue;
189  }
190  *c = '\0';
191  c = ast_skip_blanks(c + 1);
192 #if 0
193  printf("'%s' is '%s' at line %d\n", buf, c, lineno);
194 #endif
195  if (!strcasecmp(buf, "channel")) {
196  if ((c2 = strchr(c, '/'))) {
197  *c2 = '\0';
198  c2++;
199  ast_string_field_set(o, tech, c);
200  ast_string_field_set(o, dest, c2);
201  } else {
202  ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, o->fn);
203  }
204  } else if (!strcasecmp(buf, "callerid")) {
205  char cid_name[80] = {0}, cid_num[80] = {0};
206  ast_callerid_split(c, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
208  ast_string_field_set(o, cid_name, cid_name);
209  } else if (!strcasecmp(buf, "application")) {
210  ast_string_field_set(o, app, c);
211  } else if (!strcasecmp(buf, "data")) {
212  ast_string_field_set(o, data, c);
213  } else if (!strcasecmp(buf, "maxretries")) {
214  if (sscanf(c, "%30d", &o->maxretries) != 1) {
215  ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, o->fn);
216  o->maxretries = 0;
217  }
218  } else if (!strcasecmp(buf, "codecs")) {
219  ast_parse_allow_disallow(NULL, &o->format, c, 1);
220  } else if (!strcasecmp(buf, "context")) {
222  } else if (!strcasecmp(buf, "extension")) {
224  } else if (!strcasecmp(buf, "priority")) {
225  if ((sscanf(c, "%30d", &o->priority) != 1) || (o->priority < 1)) {
226  ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, o->fn);
227  o->priority = 1;
228  }
229  } else if (!strcasecmp(buf, "retrytime")) {
230  if ((sscanf(c, "%30d", &o->retrytime) != 1) || (o->retrytime < 1)) {
231  ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, o->fn);
232  o->retrytime = 300;
233  }
234  } else if (!strcasecmp(buf, "waittime")) {
235  if ((sscanf(c, "%30d", &o->waittime) != 1) || (o->waittime < 1)) {
236  ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, o->fn);
237  o->waittime = 45;
238  }
239  } else if (!strcasecmp(buf, "retry")) {
240  o->retries++;
241  } else if (!strcasecmp(buf, "startretry")) {
242  if (sscanf(c, "%30ld", &o->callingpid) != 1) {
243  ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
244  o->callingpid = 0;
245  }
246  } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
247  o->callingpid = 0;
248  o->retries++;
249  } else if (!strcasecmp(buf, "delayedretry")) {
250  } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
251  c2 = c;
252  strsep(&c2, "=");
253  if (c2) {
254  var = ast_variable_new(c, c2, o->fn);
255  if (var) {
256  /* Always insert at the end, because some people want to treat the spool file as a script */
257  if (last) {
258  last->next = var;
259  } else {
260  o->vars = var;
261  }
262  last = var;
263  }
264  } else
265  ast_log(LOG_WARNING, "Malformed \"%s\" argument. Should be \"%s: variable=value\"\n", buf, buf);
266  } else if (!strcasecmp(buf, "account")) {
267  ast_string_field_set(o, account, c);
268  } else if (!strcasecmp(buf, "alwaysdelete")) {
270  } else if (!strcasecmp(buf, "archive")) {
272  } else {
273  ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, o->fn);
274  }
275  }
277  ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", o->fn);
278  return -1;
279  }
280  return 0;
281 }
282 
283 static void safe_append(struct outgoing *o, time_t now, char *s)
284 {
285  FILE *f;
286  struct utimbuf tbuf = { .actime = now, .modtime = now + o->retrytime };
287 
288  ast_debug(1, "Outgoing %s/%s: %s\n", o->tech, o->dest, s);
289 
290  if ((f = fopen(o->fn, "a"))) {
291  fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
292  fclose(f);
293  }
294 
295  /* Update the file time */
296  if (utime(o->fn, &tbuf)) {
297  ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
298  }
299 }
300 
301 /*!
302  * \brief Remove a call file from the outgoing queue optionally moving it in the archive dir
303  *
304  * \param o the pointer to outgoing struct
305  * \param status the exit status of the call. Can be "Completed", "Failed" or "Expired"
306  */
307 static int remove_from_queue(struct outgoing *o, const char *status)
308 {
309  FILE *f;
310  char newfn[256];
311  const char *bname;
312 
314  struct stat current_file_status;
315 
316  if (!stat(o->fn, &current_file_status)) {
317  if (time(NULL) < current_file_status.st_mtime) {
318  return 0;
319  }
320  }
321  }
322 
324  unlink(o->fn);
325  return 0;
326  }
327 
328  if (ast_mkdir(qdonedir, 0777)) {
329  ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
330  unlink(o->fn);
331  return -1;
332  }
333 
334  if (!(bname = strrchr(o->fn, '/'))) {
335  bname = o->fn;
336  } else {
337  bname++;
338  }
339 
340  snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
341  /* If there is already a call file with the name in the archive dir, it will be overwritten. */
342  unlink(newfn);
343  if (rename(o->fn, newfn) != 0) {
344  unlink(o->fn);
345  return -1;
346  }
347 
348  /* Only append to the file AFTER we move it out of the watched directory,
349  * otherwise the fclose() causes another event for inotify(7) */
350  if ((f = fopen(newfn, "a"))) {
351  fprintf(f, "Status: %s\n", status);
352  fclose(f);
353  }
354 
355  return 0;
356 }
357 
358 static void *attempt_thread(void *data)
359 {
360  struct outgoing *o = data;
361  int res, reason;
362  if (!ast_strlen_zero(o->app)) {
363  ast_verb(3, "Attempting call on %s/%s for application %s(%s) (Retry %d)\n", o->tech, o->dest, o->app, o->data, o->retries);
364  res = ast_pbx_outgoing_app(o->tech, o->format, (void *) 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);
365  o->vars = NULL;
366  } else {
367  ast_verb(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);
368  res = ast_pbx_outgoing_exten(o->tech, o->format, (void *) 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);
369  o->vars = NULL;
370  }
371  if (res) {
372  ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason));
373  if (o->retries >= o->maxretries + 1) {
374  /* Max retries exceeded */
375  ast_log(LOG_NOTICE, "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" : "");
376  remove_from_queue(o, "Expired");
377  } else {
378  /* Notate that the call is still active */
379  safe_append(o, time(NULL), "EndRetry");
380 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
381  queue_file(o->fn, time(NULL) + o->retrytime);
382 #endif
383  }
384  } else {
385  ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
386  remove_from_queue(o, "Completed");
387  }
388  free_outgoing(o);
389  return NULL;
390 }
391 
392 static void launch_service(struct outgoing *o)
393 {
394  pthread_t t;
395  int ret;
396 
397  if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
398  ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
399  free_outgoing(o);
400  }
401 }
402 
403 /* Called from scan_thread or queue_file */
404 static int scan_service(const char *fn, time_t now)
405 {
406  struct outgoing *o;
407  FILE *f;
408  int res;
409 
410  o = new_outgoing(fn);
411  if (!o) {
412  return -1;
413  }
414 
415  /* Attempt to open the file */
416  f = fopen(o->fn, "r");
417  if (!f) {
418 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
419  /*!
420  * \todo XXX There is some odd delayed duplicate servicing of
421  * call files going on. We need to suppress the error message
422  * if the file does not exist as a result.
423  */
424  if (errno != ENOENT)
425 #endif
426  {
427  ast_log(LOG_WARNING, "Unable to open %s: '%s'(%d), deleting\n",
428  o->fn, strerror(errno), (int) errno);
429  }
430  remove_from_queue(o, "Failed");
431  free_outgoing(o);
432  return -1;
433  }
434 
435  /* Read in and verify the contents */
436  res = apply_outgoing(o, f);
437  fclose(f);
438  if (res) {
439  ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", o->fn);
440  remove_from_queue(o, "Failed");
441  free_outgoing(o);
442  return -1;
443  }
444 
445  ast_debug(1, "Filename: %s, Retries: %d, max: %d\n", o->fn, o->retries, o->maxretries);
446  if (o->retries <= o->maxretries) {
447  now += o->retrytime;
448  if (o->callingpid && (o->callingpid == ast_mainpid)) {
449  safe_append(o, time(NULL), "DelayedRetry");
450  ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
451  free_outgoing(o);
452  } else {
453  /* Increment retries */
454  o->retries++;
455  /* If someone else was calling, they're presumably gone now
456  so abort their retry and continue as we were... */
457  if (o->callingpid)
458  safe_append(o, time(NULL), "AbortRetry");
459 
460  safe_append(o, now, "StartRetry");
461  launch_service(o);
462  }
463  return now;
464  }
465 
466  ast_log(LOG_NOTICE, "Queued call to %s/%s expired without completion after %d attempt%s\n",
467  o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
468  remove_from_queue(o, "Expired");
469  free_outgoing(o);
470  return 0;
471 }
472 
473 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
474 struct direntry {
476  time_t mtime;
477  char name[0];
478 };
479 
481 
482 #if defined(HAVE_INOTIFY)
483 /* Only one thread is accessing this list, so no lock is necessary */
486 #endif
487 
488 static void queue_file(const char *filename, time_t when)
489 {
490  struct stat st;
491  struct direntry *cur, *new;
492  int res;
493  time_t now = time(NULL);
494 
495  if (!strchr(filename, '/')) {
496  char *fn = ast_alloca(strlen(qdir) + strlen(filename) + 2);
497  sprintf(fn, "%s/%s", qdir, filename); /* SAFE */
498  filename = fn;
499  }
500 
501  if (when == 0) {
502  if (stat(filename, &st)) {
503  ast_log(LOG_WARNING, "Unable to stat %s: %s\n", filename, strerror(errno));
504  return;
505  }
506 
507  if (!S_ISREG(st.st_mode)) {
508  return;
509  }
510 
511  when = st.st_mtime;
512  }
513 
514  /* Need to check the existing list in order to avoid duplicates. */
515  AST_LIST_LOCK(&dirlist);
516  AST_LIST_TRAVERSE(&dirlist, cur, list) {
517  if (cur->mtime == when && !strcmp(filename, cur->name)) {
518  AST_LIST_UNLOCK(&dirlist);
519  return;
520  }
521  }
522 
523  if ((res = when) > now || (res = scan_service(filename, now)) > 0) {
524  if (!(new = ast_calloc(1, sizeof(*new) + strlen(filename) + 1))) {
525  AST_LIST_UNLOCK(&dirlist);
526  return;
527  }
528  new->mtime = res;
529  strcpy(new->name, filename);
530  /* List is ordered by mtime */
531  if (AST_LIST_EMPTY(&dirlist)) {
532  AST_LIST_INSERT_HEAD(&dirlist, new, list);
533  } else {
534  int found = 0;
535  AST_LIST_TRAVERSE_SAFE_BEGIN(&dirlist, cur, list) {
536  if (cur->mtime > new->mtime) {
538  found = 1;
539  break;
540  }
541  }
543  if (!found) {
544  AST_LIST_INSERT_TAIL(&dirlist, new, list);
545  }
546  }
547  }
548  AST_LIST_UNLOCK(&dirlist);
549 }
550 
551 #ifdef HAVE_INOTIFY
552 static void queue_file_create(const char *filename)
553 {
554  struct direntry *cur;
555 
556  AST_LIST_TRAVERSE(&createlist, cur, list) {
557  if (!strcmp(cur->name, filename)) {
558  return;
559  }
560  }
561 
562  if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(filename) + 1))) {
563  return;
564  }
565  strcpy(cur->name, filename);
566  /* We'll handle this file unless an IN_OPEN event occurs within 2 seconds */
567  cur->mtime = time(NULL) + 2;
568  AST_LIST_INSERT_TAIL(&createlist, cur, list);
569 }
570 
571 static void queue_file_open(const char *filename)
572 {
573  struct direntry *cur;
574 
576  if (!strcmp(cur->name, filename)) {
578  AST_LIST_INSERT_TAIL(&openlist, cur, list);
579  break;
580  }
581  }
583 }
584 
585 static void queue_created_files(void)
586 {
587  struct direntry *cur;
588  time_t now = time(NULL);
589 
591  if (cur->mtime > now) {
592  break;
593  }
594 
596  queue_file(cur->name, 0);
597  ast_free(cur);
598  }
600 }
601 
602 static void queue_file_write(const char *filename)
603 {
604  struct direntry *cur;
605  /* Only queue entries where an IN_CREATE preceded the IN_CLOSE_WRITE */
607  if (!strcmp(cur->name, filename)) {
609  ast_free(cur);
610  queue_file(filename, 0);
611  break;
612  }
613  }
615 }
616 #endif
617 
618 static void *scan_thread(void *unused)
619 {
620  DIR *dir;
621  struct dirent *de;
622  time_t now;
623  struct timespec ts = { .tv_sec = 1 };
624 #ifdef HAVE_INOTIFY
625  ssize_t res;
626  int inotify_fd = inotify_init();
627  struct inotify_event *iev;
628  char buf[8192] __attribute__((aligned (sizeof(int))));
629  struct pollfd pfd = { .fd = inotify_fd, .events = POLLIN };
630 #else
631  struct timespec nowait = { .tv_sec = 0, .tv_nsec = 1 };
632  int inotify_fd = kqueue();
633  struct kevent kev;
634  struct kevent event;
635 #endif
636  struct direntry *cur;
637 
638  while (!ast_fully_booted) {
639  nanosleep(&ts, NULL);
640  }
641 
642  if (inotify_fd < 0) {
643  ast_log(LOG_ERROR, "Unable to initialize "
644 #ifdef HAVE_INOTIFY
645  "inotify(7)"
646 #else
647  "kqueue(2)"
648 #endif
649  "\n");
650  return NULL;
651  }
652 
653 #ifdef HAVE_INOTIFY
654  inotify_add_watch(inotify_fd, qdir, IN_CREATE | IN_OPEN | IN_CLOSE_WRITE | IN_MOVED_TO);
655 #endif
656 
657  /* First, run through the directory and clear existing entries */
658  if (!(dir = opendir(qdir))) {
659  ast_log(LOG_ERROR, "Unable to open directory %s: %s\n", qdir, strerror(errno));
660  return NULL;
661  }
662 
663 #ifndef HAVE_INOTIFY
664  EV_SET(&kev, dirfd(dir), EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_WRITE, 0, NULL);
665  if (kevent(inotify_fd, &kev, 1, &event, 1, &nowait) < 0 && errno != 0) {
666  ast_log(LOG_ERROR, "Unable to watch directory %s: %s\n", qdir, strerror(errno));
667  }
668 #endif
669  now = time(NULL);
670  while ((de = readdir(dir))) {
671  queue_file(de->d_name, 0);
672  }
673 
674 #ifdef HAVE_INOTIFY
675  /* Directory needs to remain open for kqueue(2) */
676  closedir(dir);
677 #endif
678 
679  /* Wait for either a) next timestamp to occur, or b) a change to happen */
680  for (;/* ever */;) {
681  time_t next = AST_LIST_EMPTY(&dirlist) ? INT_MAX : AST_LIST_FIRST(&dirlist)->mtime;
682 
683  time(&now);
684  if (next > now) {
685 #ifdef HAVE_INOTIFY
686  int stage = 0;
687  /* Convert from seconds to milliseconds, unless there's nothing
688  * in the queue already, in which case, we wait forever. */
689  int waittime = next == INT_MAX ? -1 : (next - now) * 1000;
690  if (!AST_LIST_EMPTY(&createlist)) {
691  waittime = 1000;
692  }
693  /* When a file arrives, add it to the queue, in mtime order. */
694  if ((res = poll(&pfd, 1, waittime)) > 0 && (stage = 1) &&
695  (res = read(inotify_fd, &buf, sizeof(buf))) >= sizeof(*iev)) {
696  ssize_t len = 0;
697  /* File(s) added to directory, add them to my list */
698  for (iev = (void *) buf; res >= sizeof(*iev); iev = (struct inotify_event *) (((char *) iev) + len)) {
699  /* For an IN_MOVED_TO event, simply process the file. However, if
700  * we get an IN_CREATE event it *might* be an open(O_CREAT) or it
701  * might be a hardlink (like smsq does, since rename() might
702  * overwrite an existing file). So we have to see if we get a
703  * subsequent IN_OPEN event on the same file. If we do, keep it
704  * on the openlist and wait for the corresponding IN_CLOSE_WRITE.
705  * If we *don't* see an IN_OPEN event, then it was a hard link so
706  * it can be processed immediately.
707  *
708  * Unfortunately, although open(O_CREAT) is an atomic file system
709  * operation, the inotify subsystem doesn't give it to us in a
710  * single event with both IN_CREATE|IN_OPEN set. It's two separate
711  * events, and the kernel doesn't even give them to us at the same
712  * time. We can read() from inotify_fd after the IN_CREATE event,
713  * and get *nothing* from it. The IN_OPEN arrives only later! So
714  * we have a very short timeout of 2 seconds. */
715  if (iev->mask & IN_CREATE) {
716  queue_file_create(iev->name);
717  } else if (iev->mask & IN_OPEN) {
718  queue_file_open(iev->name);
719  } else if (iev->mask & IN_CLOSE_WRITE) {
720  queue_file_write(iev->name);
721  } else if (iev->mask & IN_MOVED_TO) {
722  queue_file(iev->name, 0);
723  } else {
724  ast_log(LOG_ERROR, "Unexpected event %d for file '%s'\n", (int) iev->mask, iev->name);
725  }
726 
727  len = sizeof(*iev) + iev->len;
728  res -= len;
729  }
730  } else if (res < 0 && errno != EINTR && errno != EAGAIN) {
731  ast_debug(1, "Got an error back from %s(2): %s\n", stage ? "read" : "poll", strerror(errno));
732  }
733  time(&now);
734  }
736 #else
737  int num_events;
738  /* If queue empty then wait forever */
739  if (next == INT_MAX) {
740  num_events = kevent(inotify_fd, &kev, 1, &event, 1, NULL);
741  } else {
742  struct timespec ts2 = { .tv_sec = (unsigned long int)(next - now), .tv_nsec = 0 };
743  num_events = kevent(inotify_fd, &kev, 1, &event, 1, &ts2);
744  }
745  if ((num_events < 0) || (event.flags == EV_ERROR)) {
746  ast_debug(10, "KEvent error %s\n", strerror(errno));
747  continue;
748  } else if (num_events == 0) {
749  /* Interrupt or timeout, restart calculations */
750  continue;
751  } else {
752  /* Directory changed, rescan */
753  rewinddir(dir);
754  while ((de = readdir(dir))) {
755  queue_file(de->d_name, 0);
756  }
757  }
758  time(&now);
759  }
760 #endif
761 
762  /* Empty the list of all entries ready to be processed */
763  AST_LIST_LOCK(&dirlist);
764  while (!AST_LIST_EMPTY(&dirlist) && AST_LIST_FIRST(&dirlist)->mtime <= now) {
765  cur = AST_LIST_REMOVE_HEAD(&dirlist, list);
766  queue_file(cur->name, cur->mtime);
767  ast_free(cur);
768  }
769  AST_LIST_UNLOCK(&dirlist);
770  }
771  return NULL;
772 }
773 
774 #else
775 static void *scan_thread(void *unused)
776 {
777  struct stat st;
778  DIR *dir;
779  struct dirent *de;
780  char fn[256];
781  int res;
782  int force_poll = 1;
783  time_t last = 0;
784  time_t next = 0;
785  time_t now;
786  struct timespec ts = { .tv_sec = 1 };
787 
788  while (!ast_fully_booted) {
789  nanosleep(&ts, NULL);
790  }
791 
792  for (;;) {
793  /* Wait a sec */
794  nanosleep(&ts, NULL);
795  time(&now);
796 
797  if (stat(qdir, &st)) {
798  ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
799  continue;
800  }
801 
802  /* Make sure it is time for us to execute our check */
803  if (!force_poll && st.st_mtime == last && (!next || now < next)) {
804  /*
805  * The directory timestamp did not change and any delayed
806  * call-file is not ready to be executed.
807  */
808  continue;
809  }
810 
811 #if 0
812  printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
813  printf("Ooh, something changed / timeout\n");
814 #endif
815 
816  if (!(dir = opendir(qdir))) {
817  ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
818  continue;
819  }
820 
821  /*
822  * Since the dir timestamp is available at one second
823  * resolution, we cannot know if it was updated within the same
824  * second after we scanned it. Therefore, we will force another
825  * scan if the dir was just modified.
826  */
827  force_poll = (st.st_mtime == now);
828 
829  next = 0;
830  last = st.st_mtime;
831  while ((de = readdir(dir))) {
832  snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
833  if (stat(fn, &st)) {
834  ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
835  continue;
836  }
837  if (!S_ISREG(st.st_mode)) {
838  /* Not a regular file. */
839  continue;
840  }
841  if (st.st_mtime <= now) {
842  res = scan_service(fn, now);
843  if (res > 0) {
844  /* The call-file is delayed or to be retried later. */
845  if (!next || res < next) {
846  /* This delayed call file expires earlier. */
847  next = res;
848  }
849  } else if (res) {
850  ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
851  } else if (!next) {
852  /* Expired entry: must recheck on the next go-around */
853  next = st.st_mtime;
854  }
855  } else {
856  /* The file's timestamp is in the future. */
857  if (!next || st.st_mtime < next) {
858  /* This call-file's timestamp expires earlier. */
859  next = st.st_mtime;
860  }
861  }
862  }
863  closedir(dir);
864  }
865  return NULL;
866 }
867 #endif
868 
869 static int unload_module(void)
870 {
871  return -1;
872 }
873 
874 static int load_module(void)
875 {
876  pthread_t thread;
877  int ret;
878  snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
879  if (ast_mkdir(qdir, 0777)) {
880  ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
882  }
883  snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
884 
885  if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
886  ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
888  }
889 
891 }
892 
893 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Outgoing Spool Support");
static struct outgoing * new_outgoing(const char *fn)
Definition: pbx_spool.c:113
static int scan_service(const char *fn, time_t now)
Definition: pbx_spool.c:404
pthread_t thread
Definition: app_meetme.c:962
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:109
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:39
static void queue_created_files(void)
Definition: pbx_spool.c:585
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:420
static int apply_outgoing(struct outgoing *o, FILE *f)
Definition: pbx_spool.c:147
char * strsep(char **str, const char *delims)
int ast_callerid_split(const char *src, char *name, int namelen, char *num, int numlen)
Definition: callerid.c:1093
static char qdonedir[255]
Definition: pbx_spool.c:73
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
const ast_string_field cid_name
Definition: pbx_spool.c:93
#define ast_pthread_create_detached(a, b, c, d)
Definition: utils.h:422
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
#define ast_test_flag(p, flag)
Definition: utils.h:63
Time-related functions and macros.
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define LOG_WARNING
Definition: logger.h:144
int maxretries
Definition: pbx_spool.c:77
static char qdir[255]
Definition: pbx_spool.c:72
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:139
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.
Definition: pbx_spool.c:307
int priority
Definition: pbx_spool.c:94
int waittime
Definition: pbx_spool.c:79
#define HAVE_INOTIFY
Definition: autoconfig.h:342
const ast_string_field cid_num
Definition: pbx_spool.c:93
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
const ast_string_field app
Definition: pbx_spool.c:93
static void * attempt_thread(void *data)
Definition: pbx_spool.c:358
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:235
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:449
int retries
Definition: pbx_spool.c:76
static void free_outgoing(struct outgoing *o)
Definition: pbx_spool.c:104
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: config.c:586
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
#define LOG_DEBUG
Definition: logger.h:122
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:600
static char cid_num[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:157
const ast_string_field context
Definition: pbx_spool.c:93
#define ast_pthread_create_detached_background(a, b, c, d)
Definition: utils.h:431
#define ast_verb(level,...)
Definition: logger.h:243
Utility functions.
static void queue_file_write(const char *filename)
Definition: pbx_spool.c:602
const ast_string_field exten
Definition: pbx_spool.c:93
static void queue_file(const char *filename, time_t when)
Definition: pbx_spool.c:488
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
const ast_string_field data
Definition: pbx_spool.c:93
static const char app[]
Definition: app_adsiprog.c:49
General Asterisk PBX channel definitions.
Asterisk file paths, configured in asterisk.conf.
static int inotify_fd
Definition: localtime.c:270
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:249
#define ast_fully_booted
Definition: options.h:113
struct ast_variable * vars
Definition: pbx_spool.c:95
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const ast_string_field account
Definition: pbx_spool.c:93
struct sla_ringing_trunk * last
Definition: app_meetme.c:965
static void launch_service(struct outgoing *o)
Definition: pbx_spool.c:392
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:220
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:554
static int load_module(void)
Definition: pbx_spool.c:874
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
Core PBX routines and definitions.
struct direntry * next
Definition: pbx_spool.c:475
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:290
struct ast_flags options
Definition: pbx_spool.c:97
#define AST_LIST_HEAD_NOLOCK_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:345
#define LOG_ERROR
Definition: logger.h:155
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
int ast_pbx_outgoing_exten(const char *type, format_t format, void *data, int timeout, const char *context, const char *exten, int priority, int *reason, int sync, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel)
Definition: pbx.c:9375
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
int retrytime
Definition: pbx_spool.c:78
int64_t format_t
Definition: frame_defs.h:32
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
char name[0]
Definition: pbx_spool.c:477
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:97
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
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
Definition: strings.h:122
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:696
int errno
#define ast_free(a)
Definition: astmm.h:97
struct direntry::@320 list
int maxlen
Definition: pbx_spool.c:96
static struct ast_format f[]
Definition: format_g726.c:181
const ast_string_field fn
Definition: pbx_spool.c:93
time_t mtime
Definition: pbx_spool.c:476
const char * ast_config_AST_SPOOL_DIR
Definition: asterisk.c:259
Structure used to handle boolean flags.
Definition: utils.h:200
static void queue_file_create(const char *filename)
Definition: pbx_spool.c:552
Support for logging to various files, console and syslog Configuration in file logger.conf.
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:158
const ast_string_field dest
Definition: pbx_spool.c:93
const ast_string_field tech
Definition: pbx_spool.c:93
long callingpid
Definition: pbx_spool.c:80
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
pid_t ast_mainpid
Definition: asterisk.c:199
static void safe_append(struct outgoing *o, time_t now, char *s)
Definition: pbx_spool.c:283
#define ast_calloc(a, b)
Definition: astmm.h:82
Options provided by main asterisk program.
static void * scan_thread(void *unused)
Definition: pbx_spool.c:618
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
struct ast_variable * next
Definition: config.h:82
const char * ast_channel_reason2str(int reason)
return an english explanation of the code returned thru __ast_request_and_dial&#39;s &#39;outstate&#39; argument ...
Definition: channel.c:5313
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
int ast_pbx_outgoing_app(const char *type, format_t format, void *data, int timeout, const char *app, const char *appdata, int *reason, int sync, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel)
Definition: pbx.c:9544
#define AST_LIST_INSERT_BEFORE_CURRENT(elm, field)
Inserts a list entry before the current entry during a traversal.
Definition: linkedlists.h:584
static void queue_file_open(const char *filename)
Definition: pbx_spool.c:571
format_t format
Definition: pbx_spool.c:81
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
static int unload_module(void)
Definition: pbx_spool.c:869
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:253
struct ast_variable * ast_variable_new(const char *name, const char *value, const char *filename)
Definition: config.c:278
int ast_parse_allow_disallow(struct ast_codec_pref *pref, format_t *mask, const char *list, int allowing)
Parse an &quot;allow&quot; or &quot;deny&quot; line in a channel or device configuration and update the capabilities mask...
Definition: frame.c:1272
jack_status_t status
Definition: app_jack.c:143
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2151
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:344