2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2010, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Full-featured outgoing call spool support
26 <support_level>core</support_level>
36 #include <sys/inotify.h>
37 #elif defined(HAVE_KQUEUE)
38 #include <sys/types.h>
40 #include <sys/event.h>
44 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
45 #include "asterisk/lock.h"
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/callerid.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/module.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/options.h"
54 #include "asterisk/format.h"
55 #include "asterisk/format_cache.h"
58 * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
59 * The spool file contains a header
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.
67 SPOOL_FLAG_ALWAYS_DELETE = (1 << 0),
68 /* Don't unlink the call file after processing, move in qdonedir */
69 SPOOL_FLAG_ARCHIVE = (1 << 1),
70 /* Connect the channel with the outgoing extension once early media is received */
71 SPOOL_FLAG_EARLY_MEDIA = (1 << 2),
74 static char qdir[255];
75 static char qdonedir[255];
78 int retries; /*!< Current number of retries */
79 int maxretries; /*!< Maximum number of retries permitted */
80 int retrytime; /*!< How long to wait between retries (in seconds) */
81 int waittime; /*!< How long to wait for an answer */
82 long callingpid; /*!< PID which is currently calling */
83 struct ast_format_cap *capabilities; /*!< Formats (codecs) for this call */
84 AST_DECLARE_STRING_FIELDS (
85 AST_STRING_FIELD(fn); /*!< File name of call file */
86 AST_STRING_FIELD(tech); /*!< Which channel technology to use for outgoing call */
87 AST_STRING_FIELD(dest); /*!< Which device/line to use for outgoing call */
88 AST_STRING_FIELD(app); /*!< If application: Application name */
89 AST_STRING_FIELD(data); /*!< If application: Application data */
90 AST_STRING_FIELD(exten); /*!< If extension/context/priority: Extension in dialplan */
91 AST_STRING_FIELD(context); /*!< If extension/context/priority: Dialplan context */
92 AST_STRING_FIELD(cid_num); /*!< CallerID Information: Number/extension */
93 AST_STRING_FIELD(cid_name); /*!< CallerID Information: Name */
94 AST_STRING_FIELD(account); /*!< account code */
96 int priority; /*!< If extension/context/priority: Dialplan priority */
97 struct ast_variable *vars; /*!< Variables and Functions */
98 int maxlen; /*!< Maximum length of call */
99 struct ast_flags options; /*!< options */
102 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
104 AST_LIST_ENTRY(direntry) list;
109 static AST_LIST_HEAD_STATIC(dirlist, direntry);
111 static void queue_file(const char *filename, time_t when);
114 static void free_outgoing(struct outgoing *o)
117 ast_variables_destroy(o->vars);
119 ao2_cleanup(o->capabilities);
120 ast_string_field_free_memory(o);
124 static struct outgoing *new_outgoing(const char *fn)
128 o = ast_calloc(1, sizeof(*o));
133 /* Initialize the new object. */
137 ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
138 if (ast_string_field_init(o, 128)) {
140 * No need to call free_outgoing here since the failure was to
141 * allocate string fields and no variables have been allocated
147 ast_string_field_set(o, fn, fn);
148 if (ast_strlen_zero(o->fn)) {
149 /* String field set failed. Since this string is important we must fail. */
154 o->capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
155 if (!o->capabilities) {
159 ast_format_cap_append(o->capabilities, ast_format_slin, 0);
164 static void append_variable(struct outgoing *o, const char *name, const char *value)
166 struct ast_variable *var = ast_variable_new(name, value, o->fn);
172 /* Always insert at the end, because some people want to treat the spool
173 * file as a script */
174 ast_variable_list_append(&o->vars, var);
177 static void parse_line(char *line, unsigned int lineno, struct outgoing *o)
183 while ((c = strchr(c, '#'))) {
184 if ((c == line) || (*(c-1) == ' ') || (*(c-1) == '\t')) {
192 while ((c = strchr(c, ';'))) {
193 if ((c > line) && (c[-1] == '\\')) {
194 memmove(c - 1, c, strlen(c) + 1);
201 /* Trim trailing white space */
202 ast_trim_blanks(line);
203 if (ast_strlen_zero(line)) {
206 c = strchr(line, ':');
208 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, o->fn);
212 c = ast_skip_blanks(c + 1);
214 printf("'%s' is '%s' at line %d\n", line, c, lineno);
216 if (!strcasecmp(line, "channel")) {
218 if ((c2 = strchr(c, '/'))) {
221 ast_string_field_set(o, tech, c);
222 ast_string_field_set(o, dest, c2);
224 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, o->fn);
226 } else if (!strcasecmp(line, "callerid")) {
227 char cid_name[80] = {0}, cid_num[80] = {0};
228 ast_callerid_split(c, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
229 ast_string_field_set(o, cid_num, cid_num);
230 ast_string_field_set(o, cid_name, cid_name);
231 } else if (!strcasecmp(line, "application")) {
232 ast_string_field_set(o, app, c);
233 } else if (!strcasecmp(line, "data")) {
234 ast_string_field_set(o, data, c);
235 } else if (!strcasecmp(line, "maxretries")) {
236 if (sscanf(c, "%30d", &o->maxretries) != 1) {
237 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, o->fn);
240 } else if (!strcasecmp(line, "codecs")) {
241 ast_format_cap_update_by_allow_disallow(o->capabilities, c, 1);
242 } else if (!strcasecmp(line, "context")) {
243 ast_string_field_set(o, context, c);
244 } else if (!strcasecmp(line, "extension")) {
245 ast_string_field_set(o, exten, c);
246 } else if (!strcasecmp(line, "priority")) {
247 if ((sscanf(c, "%30d", &o->priority) != 1) || (o->priority < 1)) {
248 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, o->fn);
251 } else if (!strcasecmp(line, "retrytime")) {
252 if ((sscanf(c, "%30d", &o->retrytime) != 1) || (o->retrytime < 1)) {
253 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, o->fn);
256 } else if (!strcasecmp(line, "waittime")) {
257 if ((sscanf(c, "%30d", &o->waittime) != 1) || (o->waittime < 1)) {
258 ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, o->fn);
261 } else if (!strcasecmp(line, "retry")) {
263 } else if (!strcasecmp(line, "startretry")) {
264 if (sscanf(c, "%30ld", &o->callingpid) != 1) {
265 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
268 } else if (!strcasecmp(line, "endretry") || !strcasecmp(line, "abortretry")) {
271 } else if (!strcasecmp(line, "delayedretry")) {
272 } else if (!strcasecmp(line, "setvar") || !strcasecmp(line, "set")) {
277 append_variable(o, c, c2);
279 ast_log(LOG_WARNING, "Malformed \"%s\" argument. Should be \"%s: variable=value\"\n", line, line);
281 } else if (!strcasecmp(line, "account")) {
282 ast_string_field_set(o, account, c);
283 } else if (!strcasecmp(line, "alwaysdelete")) {
284 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ALWAYS_DELETE);
285 } else if (!strcasecmp(line, "archive")) {
286 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE);
287 } else if (!strcasecmp(line, "early_media")) {
288 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_EARLY_MEDIA);
290 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", line, lineno, o->fn);
294 #define LINE_BUFFER_SIZE 1024
296 static int apply_outgoing(struct outgoing *o, FILE *f)
298 char buf[LINE_BUFFER_SIZE];
299 unsigned int lineno = 0;
301 while (fgets(buf, sizeof(buf), f)) {
302 size_t len = strlen(buf);
306 if (buf[len - 1] == '\n' || feof(f)) {
307 /* We have a line, parse it */
308 parse_line(buf, lineno, o);
312 /* Crazy long line, skip it */
313 ast_log(LOG_WARNING, "Skipping extremely long line at line %d of %s\n", lineno, o->fn);
315 /* Consume the rest of the problematic line */
316 while (fgets(buf, sizeof(buf), f)) {
318 if (buf[len - 1] == '\n' || feof(f)) {
324 if (ast_strlen_zero(o->tech)
325 || ast_strlen_zero(o->dest)
326 || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
327 ast_log(LOG_WARNING, "At least one of app or extension must be specified, "
328 "along with tech and dest in file %s\n", o->fn);
332 if (snprintf(buf, sizeof(buf), "%d", o->retries + 1) < sizeof(buf)) {
333 append_variable(o, "AST_OUTGOING_ATTEMPT", buf);
339 static void safe_append(struct outgoing *o, time_t now, char *s)
342 struct utimbuf tbuf = { .actime = now, .modtime = now + o->retrytime };
344 ast_debug(1, "Outgoing %s/%s: %s\n", o->tech, o->dest, s);
346 if ((f = fopen(o->fn, "a"))) {
347 fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
351 /* Update the file time */
352 if (utime(o->fn, &tbuf)) {
353 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
358 * \brief Remove a call file from the outgoing queue optionally moving it in the archive dir
360 * \param o the pointer to outgoing struct
361 * \param status the exit status of the call. Can be "Completed", "Failed" or "Expired"
363 static int remove_from_queue(struct outgoing *o, const char *status)
369 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
370 struct direntry *cur;
373 if (!ast_test_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE)) {
374 struct stat current_file_status;
376 if (!stat(o->fn, ¤t_file_status)) {
377 if (time(NULL) < current_file_status.st_mtime) {
383 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
384 AST_LIST_LOCK(&dirlist);
385 AST_LIST_TRAVERSE_SAFE_BEGIN(&dirlist, cur, list) {
386 if (!strcmp(cur->name, o->fn)) {
387 AST_LIST_REMOVE_CURRENT(list);
392 AST_LIST_TRAVERSE_SAFE_END;
393 AST_LIST_UNLOCK(&dirlist);
396 if (!ast_test_flag(&o->options, SPOOL_FLAG_ARCHIVE)) {
401 if (ast_mkdir(qdonedir, 0777)) {
402 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
407 if (!(bname = strrchr(o->fn, '/'))) {
413 snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
414 /* If there is already a call file with the name in the archive dir, it will be overwritten. */
416 if (rename(o->fn, newfn) != 0) {
421 /* Only append to the file AFTER we move it out of the watched directory,
422 * otherwise the fclose() causes another event for inotify(7) */
423 if ((f = fopen(newfn, "a"))) {
424 fprintf(f, "Status: %s\n", status);
431 static void *attempt_thread(void *data)
433 struct outgoing *o = data;
435 if (!ast_strlen_zero(o->app)) {
436 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);
437 res = ast_pbx_outgoing_app(o->tech, o->capabilities, o->dest,
438 o->waittime * 1000, o->app, o->data, &reason,
439 AST_OUTGOING_WAIT_COMPLETE, o->cid_num, o->cid_name,
440 o->vars, o->account, NULL, NULL);
442 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);
443 res = ast_pbx_outgoing_exten(o->tech, o->capabilities, o->dest,
444 o->waittime * 1000, o->context, o->exten, o->priority, &reason,
445 AST_OUTGOING_WAIT_COMPLETE, o->cid_num, o->cid_name,
446 o->vars, o->account, NULL, ast_test_flag(&o->options, SPOOL_FLAG_EARLY_MEDIA),
450 ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason));
451 if (o->retries >= o->maxretries + 1) {
452 /* Max retries exceeded */
453 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" : "");
454 remove_from_queue(o, "Expired");
456 /* Notate that the call is still active */
457 safe_append(o, time(NULL), "EndRetry");
458 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
459 queue_file(o->fn, time(NULL) + o->retrytime);
463 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
464 remove_from_queue(o, "Completed");
470 static void launch_service(struct outgoing *o)
475 if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
476 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
481 /* Called from scan_thread or queue_file */
482 static int scan_service(const char *fn, time_t now)
488 o = new_outgoing(fn);
493 /* Attempt to open the file */
494 f = fopen(o->fn, "r");
496 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
498 * \todo XXX There is some odd delayed duplicate servicing of
499 * call files going on. We need to suppress the error message
500 * if the file does not exist as a result.
505 ast_log(LOG_WARNING, "Unable to open %s: '%s'(%d), deleting\n",
506 o->fn, strerror(errno), (int) errno);
508 remove_from_queue(o, "Failed");
513 /* Read in and verify the contents */
514 res = apply_outgoing(o, f);
517 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", o->fn);
518 remove_from_queue(o, "Failed");
523 ast_debug(1, "Filename: %s, Retries: %d, max: %d\n", o->fn, o->retries, o->maxretries);
524 if (o->retries <= o->maxretries) {
526 if (o->callingpid && (o->callingpid == ast_mainpid)) {
527 safe_append(o, time(NULL), "DelayedRetry");
528 ast_debug(1, "Delaying retry since we're currently running '%s'\n", o->fn);
531 /* Increment retries */
533 /* If someone else was calling, they're presumably gone now
534 so abort their retry and continue as we were... */
536 safe_append(o, time(NULL), "AbortRetry");
538 safe_append(o, now, "StartRetry");
544 ast_log(LOG_NOTICE, "Queued call to %s/%s expired without completion after %d attempt%s\n",
545 o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
546 remove_from_queue(o, "Expired");
552 #if defined(HAVE_INOTIFY)
553 /* Only one thread is accessing this list, so no lock is necessary */
554 static AST_LIST_HEAD_NOLOCK_STATIC(createlist, direntry);
555 static AST_LIST_HEAD_NOLOCK_STATIC(openlist, direntry);
558 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
560 static void queue_file(const char *filename, time_t when)
563 struct direntry *cur, *new;
565 time_t now = time(NULL);
567 if (!strchr(filename, '/')) {
568 char *fn = ast_alloca(strlen(qdir) + strlen(filename) + 2);
569 sprintf(fn, "%s/%s", qdir, filename); /* SAFE */
574 if (stat(filename, &st)) {
575 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", filename, strerror(errno));
579 if (!S_ISREG(st.st_mode)) {
586 /* Need to check the existing list in order to avoid duplicates. */
587 AST_LIST_LOCK(&dirlist);
588 AST_LIST_TRAVERSE(&dirlist, cur, list) {
589 if (cur->mtime == when && !strcmp(filename, cur->name)) {
590 AST_LIST_UNLOCK(&dirlist);
595 if ((res = when) > now || (res = scan_service(filename, now)) > 0) {
596 if (!(new = ast_calloc(1, sizeof(*new) + strlen(filename) + 1))) {
597 AST_LIST_UNLOCK(&dirlist);
601 strcpy(new->name, filename);
602 /* List is ordered by mtime */
603 if (AST_LIST_EMPTY(&dirlist)) {
604 AST_LIST_INSERT_HEAD(&dirlist, new, list);
607 AST_LIST_TRAVERSE_SAFE_BEGIN(&dirlist, cur, list) {
608 if (cur->mtime > new->mtime) {
609 AST_LIST_INSERT_BEFORE_CURRENT(new, list);
614 AST_LIST_TRAVERSE_SAFE_END
616 AST_LIST_INSERT_TAIL(&dirlist, new, list);
620 AST_LIST_UNLOCK(&dirlist);
624 static void queue_file_create(const char *filename)
626 struct direntry *cur;
628 AST_LIST_TRAVERSE(&createlist, cur, list) {
629 if (!strcmp(cur->name, filename)) {
634 if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(filename) + 1))) {
637 strcpy(cur->name, filename);
638 /* We'll handle this file unless an IN_OPEN event occurs within 2 seconds */
639 cur->mtime = time(NULL) + 2;
640 AST_LIST_INSERT_TAIL(&createlist, cur, list);
643 static void queue_file_open(const char *filename)
645 struct direntry *cur;
647 AST_LIST_TRAVERSE_SAFE_BEGIN(&createlist, cur, list) {
648 if (!strcmp(cur->name, filename)) {
649 AST_LIST_REMOVE_CURRENT(list);
650 AST_LIST_INSERT_TAIL(&openlist, cur, list);
654 AST_LIST_TRAVERSE_SAFE_END
657 static void queue_created_files(void)
659 struct direntry *cur;
660 time_t now = time(NULL);
662 AST_LIST_TRAVERSE_SAFE_BEGIN(&createlist, cur, list) {
663 if (cur->mtime > now) {
667 AST_LIST_REMOVE_CURRENT(list);
668 queue_file(cur->name, 0);
671 AST_LIST_TRAVERSE_SAFE_END
674 static void queue_file_write(const char *filename)
676 struct direntry *cur;
677 /* Only queue entries where an IN_CREATE preceded the IN_CLOSE_WRITE */
678 AST_LIST_TRAVERSE_SAFE_BEGIN(&openlist, cur, list) {
679 if (!strcmp(cur->name, filename)) {
680 AST_LIST_REMOVE_CURRENT(list);
682 queue_file(filename, 0);
686 AST_LIST_TRAVERSE_SAFE_END
690 static void *scan_thread(void *unused)
695 struct timespec ts = { .tv_sec = 1 };
698 int inotify_fd = inotify_init();
699 struct inotify_event *iev;
700 char buf[8192] __attribute__((aligned (sizeof(int))));
701 struct pollfd pfd = { .fd = inotify_fd, .events = POLLIN };
703 struct timespec nowait = { .tv_sec = 0, .tv_nsec = 1 };
704 int inotify_fd = kqueue();
708 struct direntry *cur;
710 while (!ast_fully_booted) {
711 nanosleep(&ts, NULL);
714 if (inotify_fd < 0) {
715 ast_log(LOG_ERROR, "Unable to initialize "
726 inotify_add_watch(inotify_fd, qdir, IN_CREATE | IN_OPEN | IN_CLOSE_WRITE | IN_MOVED_TO);
729 /* First, run through the directory and clear existing entries */
730 if (!(dir = opendir(qdir))) {
731 ast_log(LOG_ERROR, "Unable to open directory %s: %s\n", qdir, strerror(errno));
736 EV_SET(&kev, dirfd(dir), EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_WRITE, 0, NULL);
737 if (kevent(inotify_fd, &kev, 1, &event, 1, &nowait) < 0 && errno != 0) {
738 ast_log(LOG_ERROR, "Unable to watch directory %s: %s\n", qdir, strerror(errno));
742 while ((de = readdir(dir))) {
743 queue_file(de->d_name, 0);
747 /* Directory needs to remain open for kqueue(2) */
751 /* Wait for either a) next timestamp to occur, or b) a change to happen */
753 time_t next = AST_LIST_EMPTY(&dirlist) ? INT_MAX : AST_LIST_FIRST(&dirlist)->mtime;
759 /* Convert from seconds to milliseconds, unless there's nothing
760 * in the queue already, in which case, we wait forever. */
761 int waittime = next == INT_MAX ? -1 : (next - now) * 1000;
762 if (!AST_LIST_EMPTY(&createlist)) {
765 /* When a file arrives, add it to the queue, in mtime order. */
766 if ((res = poll(&pfd, 1, waittime)) > 0 && (stage = 1) &&
767 (res = read(inotify_fd, &buf, sizeof(buf))) >= sizeof(*iev)) {
769 /* File(s) added to directory, add them to my list */
770 for (iev = (void *) buf; res >= sizeof(*iev); iev = (struct inotify_event *) (((char *) iev) + len)) {
771 /* For an IN_MOVED_TO event, simply process the file. However, if
772 * we get an IN_CREATE event it *might* be an open(O_CREAT) or it
773 * might be a hardlink (like smsq does, since rename() might
774 * overwrite an existing file). So we have to see if we get a
775 * subsequent IN_OPEN event on the same file. If we do, keep it
776 * on the openlist and wait for the corresponding IN_CLOSE_WRITE.
777 * If we *don't* see an IN_OPEN event, then it was a hard link so
778 * it can be processed immediately.
780 * Unfortunately, although open(O_CREAT) is an atomic file system
781 * operation, the inotify subsystem doesn't give it to us in a
782 * single event with both IN_CREATE|IN_OPEN set. It's two separate
783 * events, and the kernel doesn't even give them to us at the same
784 * time. We can read() from inotify_fd after the IN_CREATE event,
785 * and get *nothing* from it. The IN_OPEN arrives only later! So
786 * we have a very short timeout of 2 seconds. */
787 if (iev->mask & IN_CREATE) {
788 queue_file_create(iev->name);
789 } else if (iev->mask & IN_OPEN) {
790 queue_file_open(iev->name);
791 } else if (iev->mask & IN_CLOSE_WRITE) {
792 queue_file_write(iev->name);
793 } else if (iev->mask & IN_MOVED_TO) {
794 queue_file(iev->name, 0);
796 ast_log(LOG_ERROR, "Unexpected event %d for file '%s'\n", (int) iev->mask, iev->name);
799 len = sizeof(*iev) + iev->len;
802 } else if (res < 0 && errno != EINTR && errno != EAGAIN) {
803 ast_debug(1, "Got an error back from %s(2): %s\n", stage ? "read" : "poll", strerror(errno));
807 queue_created_files();
810 /* If queue empty then wait forever */
811 if (next == INT_MAX) {
812 num_events = kevent(inotify_fd, &kev, 1, &event, 1, NULL);
814 struct timespec ts2 = { .tv_sec = (unsigned long int)(next - now), .tv_nsec = 0 };
815 num_events = kevent(inotify_fd, &kev, 1, &event, 1, &ts2);
817 if ((num_events < 0) || (event.flags == EV_ERROR)) {
818 ast_debug(10, "KEvent error %s\n", strerror(errno));
820 } else if (num_events == 0) {
821 /* Interrupt or timeout, restart calculations */
824 /* Directory changed, rescan */
826 while ((de = readdir(dir))) {
827 queue_file(de->d_name, 0);
834 /* Empty the list of all entries ready to be processed */
835 AST_LIST_LOCK(&dirlist);
836 while (!AST_LIST_EMPTY(&dirlist) && AST_LIST_FIRST(&dirlist)->mtime <= now) {
837 cur = AST_LIST_REMOVE_HEAD(&dirlist, list);
838 queue_file(cur->name, cur->mtime);
841 AST_LIST_UNLOCK(&dirlist);
847 static void *scan_thread(void *unused)
858 struct timespec ts = { .tv_sec = 1 };
860 while (!ast_fully_booted) {
861 nanosleep(&ts, NULL);
866 nanosleep(&ts, NULL);
869 if (stat(qdir, &st)) {
870 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
874 /* Make sure it is time for us to execute our check */
875 if (!force_poll && st.st_mtime == last && (!next || now < next)) {
877 * The directory timestamp did not change and any delayed
878 * call-file is not ready to be executed.
884 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
885 printf("Ooh, something changed / timeout\n");
888 if (!(dir = opendir(qdir))) {
889 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
894 * Since the dir timestamp is available at one second
895 * resolution, we cannot know if it was updated within the same
896 * second after we scanned it. Therefore, we will force another
897 * scan if the dir was just modified.
899 force_poll = (st.st_mtime == now);
903 while ((de = readdir(dir))) {
904 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
906 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
909 if (!S_ISREG(st.st_mode)) {
910 /* Not a regular file. */
913 if (st.st_mtime <= now) {
914 res = scan_service(fn, now);
916 /* The call-file is delayed or to be retried later. */
917 if (!next || res < next) {
918 /* This delayed call file expires earlier. */
922 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
924 /* Expired entry: must recheck on the next go-around */
928 /* The file's timestamp is in the future. */
929 if (!next || st.st_mtime < next) {
930 /* This call-file's timestamp expires earlier. */
941 static int unload_module(void)
946 static int load_module(void)
950 snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
951 if (ast_mkdir(qdir, 0777)) {
952 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
953 return AST_MODULE_LOAD_DECLINE;
955 snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
957 if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
958 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
959 return AST_MODULE_LOAD_FAILURE;
962 return AST_MODULE_LOAD_SUCCESS;
965 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Outgoing Spool Support");