Make the AEL load process less chatty.
[asterisk/asterisk.git] / pbx / pbx_spool.c
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$")
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          */
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),
72 };
73
74 static char qdir[255];
75 static char qdonedir[255];
76
77 struct outgoing {
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 */
95         );
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 */
100 };
101
102 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
103 static void queue_file(const char *filename, time_t when);
104 #endif
105
106 static void free_outgoing(struct outgoing *o)
107 {
108         if (o->vars) {
109                 ast_variables_destroy(o->vars);
110         }
111         o->capabilities = ast_format_cap_destroy(o->capabilities);
112         ast_string_field_free_memory(o);
113         ast_free(o);
114 }
115
116 static struct outgoing *new_outgoing(const char *fn)
117 {
118         struct outgoing *o;
119         struct ast_format tmpfmt;
120
121         o = ast_calloc(1, sizeof(*o));
122         if (!o) {
123                 return NULL;
124         }
125
126         /* Initialize the new object. */
127         o->priority = 1;
128         o->retrytime = 300;
129         o->waittime = 45;
130         ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
131         if (ast_string_field_init(o, 128)) {
132                 /*
133                  * No need to call free_outgoing here since the failure was to
134                  * allocate string fields and no variables have been allocated
135                  * yet.
136                  */
137                 ast_free(o);
138                 return NULL;
139         }
140         ast_string_field_set(o, fn, fn);
141         if (ast_strlen_zero(o->fn)) {
142                 /* String field set failed.  Since this string is important we must fail. */
143                 free_outgoing(o);
144                 return NULL;
145         }
146
147         o->capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
148         if (!o->capabilities) {
149                 free_outgoing(o);
150                 return NULL;
151         }
152         ast_format_cap_add(o->capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
153
154         return o;
155 }
156
157 static int apply_outgoing(struct outgoing *o, FILE *f)
158 {
159         char buf[256];
160         char *c, *c2;
161         int lineno = 0;
162         struct ast_variable *var, *last = o->vars;
163
164         while (last && last->next) {
165                 last = last->next;
166         }
167
168         while(fgets(buf, sizeof(buf), f)) {
169                 lineno++;
170                 /* Trim comments */
171                 c = buf;
172                 while ((c = strchr(c, '#'))) {
173                         if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
174                                 *c = '\0';
175                         else
176                                 c++;
177                 }
178
179                 c = buf;
180                 while ((c = strchr(c, ';'))) {
181                         if ((c > buf) && (c[-1] == '\\')) {
182                                 memmove(c - 1, c, strlen(c) + 1);
183                                 c++;
184                         } else {
185                                 *c = '\0';
186                                 break;
187                         }
188                 }
189
190                 /* Trim trailing white space */
191                 ast_trim_blanks(buf);
192                 if (ast_strlen_zero(buf)) {
193                         continue;
194                 }
195                 c = strchr(buf, ':');
196                 if (!c) {
197                         ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, o->fn);
198                         continue;
199                 }
200                 *c = '\0';
201                 c = ast_skip_blanks(c + 1);
202 #if 0
203                 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
204 #endif
205                 if (!strcasecmp(buf, "channel")) {
206                         if ((c2 = strchr(c, '/'))) {
207                                 *c2 = '\0';
208                                 c2++;
209                                 ast_string_field_set(o, tech, c);
210                                 ast_string_field_set(o, dest, c2);
211                         } else {
212                                 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, o->fn);
213                         }
214                 } else if (!strcasecmp(buf, "callerid")) {
215                         char cid_name[80] = {0}, cid_num[80] = {0};
216                         ast_callerid_split(c, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
217                         ast_string_field_set(o, cid_num, cid_num);
218                         ast_string_field_set(o, cid_name, cid_name);
219                 } else if (!strcasecmp(buf, "application")) {
220                         ast_string_field_set(o, app, c);
221                 } else if (!strcasecmp(buf, "data")) {
222                         ast_string_field_set(o, data, c);
223                 } else if (!strcasecmp(buf, "maxretries")) {
224                         if (sscanf(c, "%30d", &o->maxretries) != 1) {
225                                 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, o->fn);
226                                 o->maxretries = 0;
227                         }
228                 } else if (!strcasecmp(buf, "codecs")) {
229                         ast_parse_allow_disallow(NULL, o->capabilities, c, 1);
230                 } else if (!strcasecmp(buf, "context")) {
231                         ast_string_field_set(o, context, c);
232                 } else if (!strcasecmp(buf, "extension")) {
233                         ast_string_field_set(o, exten, c);
234                 } else if (!strcasecmp(buf, "priority")) {
235                         if ((sscanf(c, "%30d", &o->priority) != 1) || (o->priority < 1)) {
236                                 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, o->fn);
237                                 o->priority = 1;
238                         }
239                 } else if (!strcasecmp(buf, "retrytime")) {
240                         if ((sscanf(c, "%30d", &o->retrytime) != 1) || (o->retrytime < 1)) {
241                                 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, o->fn);
242                                 o->retrytime = 300;
243                         }
244                 } else if (!strcasecmp(buf, "waittime")) {
245                         if ((sscanf(c, "%30d", &o->waittime) != 1) || (o->waittime < 1)) {
246                                 ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, o->fn);
247                                 o->waittime = 45;
248                         }
249                 } else if (!strcasecmp(buf, "retry")) {
250                         o->retries++;
251                 } else if (!strcasecmp(buf, "startretry")) {
252                         if (sscanf(c, "%30ld", &o->callingpid) != 1) {
253                                 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
254                                 o->callingpid = 0;
255                         }
256                 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
257                         o->callingpid = 0;
258                         o->retries++;
259                 } else if (!strcasecmp(buf, "delayedretry")) {
260                 } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
261                         c2 = c;
262                         strsep(&c2, "=");
263                         if (c2) {
264                                 var = ast_variable_new(c, c2, o->fn);
265                                 if (var) {
266                                         /* Always insert at the end, because some people want to treat the spool file as a script */
267                                         if (last) {
268                                                 last->next = var;
269                                         } else {
270                                                 o->vars = var;
271                                         }
272                                         last = var;
273                                 }
274                         } else
275                                 ast_log(LOG_WARNING, "Malformed \"%s\" argument.  Should be \"%s: variable=value\"\n", buf, buf);
276                 } else if (!strcasecmp(buf, "account")) {
277                         ast_string_field_set(o, account, c);
278                 } else if (!strcasecmp(buf, "alwaysdelete")) {
279                         ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ALWAYS_DELETE);
280                 } else if (!strcasecmp(buf, "archive")) {
281                         ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE);
282                 } else if (!strcasecmp(buf, "early_media")) {
283                         ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_EARLY_MEDIA);
284                 } else {
285                         ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, o->fn);
286                 }
287         }
288         if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
289                 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);
290                 return -1;
291         }
292         return 0;
293 }
294
295 static void safe_append(struct outgoing *o, time_t now, char *s)
296 {
297         FILE *f;
298         struct utimbuf tbuf = { .actime = now, .modtime = now + o->retrytime };
299
300         ast_debug(1, "Outgoing %s/%s: %s\n", o->tech, o->dest, s);
301
302         if ((f = fopen(o->fn, "a"))) {
303                 fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
304                 fclose(f);
305         }
306
307         /* Update the file time */
308         if (utime(o->fn, &tbuf)) {
309                 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
310         }
311 }
312
313 /*!
314  * \brief Remove a call file from the outgoing queue optionally moving it in the archive dir
315  *
316  * \param o the pointer to outgoing struct
317  * \param status the exit status of the call. Can be "Completed", "Failed" or "Expired"
318  */
319 static int remove_from_queue(struct outgoing *o, const char *status)
320 {
321         FILE *f;
322         char newfn[256];
323         const char *bname;
324
325         if (!ast_test_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE)) {
326                 struct stat current_file_status;
327
328                 if (!stat(o->fn, &current_file_status)) {
329                         if (time(NULL) < current_file_status.st_mtime) {
330                                 return 0;
331                         }
332                 }
333         }
334
335         if (!ast_test_flag(&o->options, SPOOL_FLAG_ARCHIVE)) {
336                 unlink(o->fn);
337                 return 0;
338         }
339
340         if (ast_mkdir(qdonedir, 0777)) {
341                 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
342                 unlink(o->fn);
343                 return -1;
344         }
345
346         if (!(bname = strrchr(o->fn, '/'))) {
347                 bname = o->fn;
348         } else {
349                 bname++;
350         }
351
352         snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
353         /* If there is already a call file with the name in the archive dir, it will be overwritten. */
354         unlink(newfn);
355         if (rename(o->fn, newfn) != 0) {
356                 unlink(o->fn);
357                 return -1;
358         }
359
360         /* Only append to the file AFTER we move it out of the watched directory,
361          * otherwise the fclose() causes another event for inotify(7) */
362         if ((f = fopen(newfn, "a"))) {
363                 fprintf(f, "Status: %s\n", status);
364                 fclose(f);
365         }
366
367         return 0;
368 }
369
370 static void *attempt_thread(void *data)
371 {
372         struct outgoing *o = data;
373         int res, reason;
374         if (!ast_strlen_zero(o->app)) {
375                 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);
376                 res = ast_pbx_outgoing_app(o->tech, o->capabilities, o->dest, o->waittime * 1000,
377                         o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name,
378                         o->vars, o->account, NULL, NULL);
379                 o->vars = NULL;
380         } else {
381                 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);
382                 res = ast_pbx_outgoing_exten(o->tech, o->capabilities, o->dest,
383                         o->waittime * 1000, o->context, o->exten, o->priority, &reason,
384                         2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL,
385                         ast_test_flag(&o->options, SPOOL_FLAG_EARLY_MEDIA), NULL);
386                 o->vars = NULL;
387         }
388         if (res) {
389                 ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason));
390                 if (o->retries >= o->maxretries + 1) {
391                         /* Max retries exceeded */
392                         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" : "");
393                         remove_from_queue(o, "Expired");
394                 } else {
395                         /* Notate that the call is still active */
396                         safe_append(o, time(NULL), "EndRetry");
397 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
398                         queue_file(o->fn, time(NULL) + o->retrytime);
399 #endif
400                 }
401         } else {
402                 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
403                 remove_from_queue(o, "Completed");
404         }
405         free_outgoing(o);
406         return NULL;
407 }
408
409 static void launch_service(struct outgoing *o)
410 {
411         pthread_t t;
412         int ret;
413
414         if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
415                 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
416                 free_outgoing(o);
417         }
418 }
419
420 /* Called from scan_thread or queue_file */
421 static int scan_service(const char *fn, time_t now)
422 {
423         struct outgoing *o;
424         FILE *f;
425         int res;
426
427         o = new_outgoing(fn);
428         if (!o) {
429                 return -1;
430         }
431
432         /* Attempt to open the file */
433         f = fopen(o->fn, "r");
434         if (!f) {
435 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
436                 /*!
437                  * \todo XXX There is some odd delayed duplicate servicing of
438                  * call files going on.  We need to suppress the error message
439                  * if the file does not exist as a result.
440                  */
441                 if (errno != ENOENT)
442 #endif
443                 {
444                         ast_log(LOG_WARNING, "Unable to open %s: '%s'(%d), deleting\n",
445                                 o->fn, strerror(errno), (int) errno);
446                 }
447                 remove_from_queue(o, "Failed");
448                 free_outgoing(o);
449                 return -1;
450         }
451
452         /* Read in and verify the contents */
453         res = apply_outgoing(o, f);
454         fclose(f);
455         if (res) {
456                 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", o->fn);
457                 remove_from_queue(o, "Failed");
458                 free_outgoing(o);
459                 return -1;
460         }
461
462         ast_debug(1, "Filename: %s, Retries: %d, max: %d\n", o->fn, o->retries, o->maxretries);
463         if (o->retries <= o->maxretries) {
464                 now += o->retrytime;
465                 if (o->callingpid && (o->callingpid == ast_mainpid)) {
466                         safe_append(o, time(NULL), "DelayedRetry");
467                         ast_debug(1, "Delaying retry since we're currently running '%s'\n", o->fn);
468                         free_outgoing(o);
469                 } else {
470                         /* Increment retries */
471                         o->retries++;
472                         /* If someone else was calling, they're presumably gone now
473                            so abort their retry and continue as we were... */
474                         if (o->callingpid)
475                                 safe_append(o, time(NULL), "AbortRetry");
476
477                         safe_append(o, now, "StartRetry");
478                         launch_service(o);
479                 }
480                 return now;
481         }
482
483         ast_log(LOG_NOTICE, "Queued call to %s/%s expired without completion after %d attempt%s\n",
484                 o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
485         remove_from_queue(o, "Expired");
486         free_outgoing(o);
487         return 0;
488 }
489
490 #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
491 struct direntry {
492         AST_LIST_ENTRY(direntry) list;
493         time_t mtime;
494         char name[0];
495 };
496
497 static AST_LIST_HEAD_STATIC(dirlist, direntry);
498
499 #if defined(HAVE_INOTIFY)
500 /* Only one thread is accessing this list, so no lock is necessary */
501 static AST_LIST_HEAD_NOLOCK_STATIC(createlist, direntry);
502 static AST_LIST_HEAD_NOLOCK_STATIC(openlist, direntry);
503 #endif
504
505 static void queue_file(const char *filename, time_t when)
506 {
507         struct stat st;
508         struct direntry *cur, *new;
509         int res;
510         time_t now = time(NULL);
511
512         if (!strchr(filename, '/')) {
513                 char *fn = ast_alloca(strlen(qdir) + strlen(filename) + 2);
514                 sprintf(fn, "%s/%s", qdir, filename); /* SAFE */
515                 filename = fn;
516         }
517
518         if (when == 0) {
519                 if (stat(filename, &st)) {
520                         ast_log(LOG_WARNING, "Unable to stat %s: %s\n", filename, strerror(errno));
521                         return;
522                 }
523
524                 if (!S_ISREG(st.st_mode)) {
525                         return;
526                 }
527
528                 when = st.st_mtime;
529         }
530
531         /* Need to check the existing list in order to avoid duplicates. */
532         AST_LIST_LOCK(&dirlist);
533         AST_LIST_TRAVERSE(&dirlist, cur, list) {
534                 if (cur->mtime == when && !strcmp(filename, cur->name)) {
535                         AST_LIST_UNLOCK(&dirlist);
536                         return;
537                 }
538         }
539
540         if ((res = when) > now || (res = scan_service(filename, now)) > 0) {
541                 if (!(new = ast_calloc(1, sizeof(*new) + strlen(filename) + 1))) {
542                         AST_LIST_UNLOCK(&dirlist);
543                         return;
544                 }
545                 new->mtime = res;
546                 strcpy(new->name, filename);
547                 /* List is ordered by mtime */
548                 if (AST_LIST_EMPTY(&dirlist)) {
549                         AST_LIST_INSERT_HEAD(&dirlist, new, list);
550                 } else {
551                         int found = 0;
552                         AST_LIST_TRAVERSE_SAFE_BEGIN(&dirlist, cur, list) {
553                                 if (cur->mtime > new->mtime) {
554                                         AST_LIST_INSERT_BEFORE_CURRENT(new, list);
555                                         found = 1;
556                                         break;
557                                 }
558                         }
559                         AST_LIST_TRAVERSE_SAFE_END
560                         if (!found) {
561                                 AST_LIST_INSERT_TAIL(&dirlist, new, list);
562                         }
563                 }
564         }
565         AST_LIST_UNLOCK(&dirlist);
566 }
567
568 #ifdef HAVE_INOTIFY
569 static void queue_file_create(const char *filename)
570 {
571         struct direntry *cur;
572
573         AST_LIST_TRAVERSE(&createlist, cur, list) {
574                 if (!strcmp(cur->name, filename)) {
575                         return;
576                 }
577         }
578
579         if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(filename) + 1))) {
580                 return;
581         }
582         strcpy(cur->name, filename);
583         /* We'll handle this file unless an IN_OPEN event occurs within 2 seconds */
584         cur->mtime = time(NULL) + 2;
585         AST_LIST_INSERT_TAIL(&createlist, cur, list);
586 }
587
588 static void queue_file_open(const char *filename)
589 {
590         struct direntry *cur;
591
592         AST_LIST_TRAVERSE_SAFE_BEGIN(&createlist, cur, list) {
593                 if (!strcmp(cur->name, filename)) {
594                         AST_LIST_REMOVE_CURRENT(list);
595                         AST_LIST_INSERT_TAIL(&openlist, cur, list);
596                         break;
597                 }
598         }
599         AST_LIST_TRAVERSE_SAFE_END
600 }
601
602 static void queue_created_files(void)
603 {
604         struct direntry *cur;
605         time_t now = time(NULL);
606
607         AST_LIST_TRAVERSE_SAFE_BEGIN(&createlist, cur, list) {
608                 if (cur->mtime > now) {
609                         break;
610                 }
611
612                 AST_LIST_REMOVE_CURRENT(list);
613                 queue_file(cur->name, 0);
614                 ast_free(cur);
615         }
616         AST_LIST_TRAVERSE_SAFE_END
617 }
618
619 static void queue_file_write(const char *filename)
620 {
621         struct direntry *cur;
622         /* Only queue entries where an IN_CREATE preceded the IN_CLOSE_WRITE */
623         AST_LIST_TRAVERSE_SAFE_BEGIN(&openlist, cur, list) {
624                 if (!strcmp(cur->name, filename)) {
625                         AST_LIST_REMOVE_CURRENT(list);
626                         ast_free(cur);
627                         queue_file(filename, 0);
628                         break;
629                 }
630         }
631         AST_LIST_TRAVERSE_SAFE_END
632 }
633 #endif
634
635 static void *scan_thread(void *unused)
636 {
637         DIR *dir;
638         struct dirent *de;
639         time_t now;
640         struct timespec ts = { .tv_sec = 1 };
641 #ifdef HAVE_INOTIFY
642         ssize_t res;
643         int inotify_fd = inotify_init();
644         struct inotify_event *iev;
645         char buf[8192] __attribute__((aligned (sizeof(int))));
646         struct pollfd pfd = { .fd = inotify_fd, .events = POLLIN };
647 #else
648         struct timespec nowait = { .tv_sec = 0, .tv_nsec = 1 };
649         int inotify_fd = kqueue();
650         struct kevent kev;
651         struct kevent event;
652 #endif
653         struct direntry *cur;
654
655         while (!ast_fully_booted) {
656                 nanosleep(&ts, NULL);
657         }
658
659         if (inotify_fd < 0) {
660                 ast_log(LOG_ERROR, "Unable to initialize "
661 #ifdef HAVE_INOTIFY
662                         "inotify(7)"
663 #else
664                         "kqueue(2)"
665 #endif
666                         "\n");
667                 return NULL;
668         }
669
670 #ifdef HAVE_INOTIFY
671         inotify_add_watch(inotify_fd, qdir, IN_CREATE | IN_OPEN | IN_CLOSE_WRITE | IN_MOVED_TO);
672 #endif
673
674         /* First, run through the directory and clear existing entries */
675         if (!(dir = opendir(qdir))) {
676                 ast_log(LOG_ERROR, "Unable to open directory %s: %s\n", qdir, strerror(errno));
677                 return NULL;
678         }
679
680 #ifndef HAVE_INOTIFY
681         EV_SET(&kev, dirfd(dir), EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_WRITE, 0, NULL);
682         if (kevent(inotify_fd, &kev, 1, &event, 1, &nowait) < 0 && errno != 0) {
683                 ast_log(LOG_ERROR, "Unable to watch directory %s: %s\n", qdir, strerror(errno));
684         }
685 #endif
686         now = time(NULL);
687         while ((de = readdir(dir))) {
688                 queue_file(de->d_name, 0);
689         }
690
691 #ifdef HAVE_INOTIFY
692         /* Directory needs to remain open for kqueue(2) */
693         closedir(dir);
694 #endif
695
696         /* Wait for either a) next timestamp to occur, or b) a change to happen */
697         for (;/* ever */;) {
698                 time_t next = AST_LIST_EMPTY(&dirlist) ? INT_MAX : AST_LIST_FIRST(&dirlist)->mtime;
699
700                 time(&now);
701                 if (next > now) {
702 #ifdef HAVE_INOTIFY
703                         int stage = 0;
704                         /* Convert from seconds to milliseconds, unless there's nothing
705                          * in the queue already, in which case, we wait forever. */
706                         int waittime = next == INT_MAX ? -1 : (next - now) * 1000;
707                         if (!AST_LIST_EMPTY(&createlist)) {
708                                 waittime = 1000;
709                         }
710                         /* When a file arrives, add it to the queue, in mtime order. */
711                         if ((res = poll(&pfd, 1, waittime)) > 0 && (stage = 1) &&
712                                 (res = read(inotify_fd, &buf, sizeof(buf))) >= sizeof(*iev)) {
713                                 ssize_t len = 0;
714                                 /* File(s) added to directory, add them to my list */
715                                 for (iev = (void *) buf; res >= sizeof(*iev); iev = (struct inotify_event *) (((char *) iev) + len)) {
716                                         /* For an IN_MOVED_TO event, simply process the file. However, if
717                                          * we get an IN_CREATE event it *might* be an open(O_CREAT) or it
718                                          * might be a hardlink (like smsq does, since rename() might
719                                          * overwrite an existing file). So we have to see if we get a
720                                          * subsequent IN_OPEN event on the same file. If we do, keep it
721                                          * on the openlist and wait for the corresponding IN_CLOSE_WRITE.
722                                          * If we *don't* see an IN_OPEN event, then it was a hard link so
723                                          * it can be processed immediately.
724                                          *
725                                          * Unfortunately, although open(O_CREAT) is an atomic file system
726                                          * operation, the inotify subsystem doesn't give it to us in a
727                                          * single event with both IN_CREATE|IN_OPEN set. It's two separate
728                                          * events, and the kernel doesn't even give them to us at the same
729                                          * time. We can read() from inotify_fd after the IN_CREATE event,
730                                          * and get *nothing* from it. The IN_OPEN arrives only later! So
731                                          * we have a very short timeout of 2 seconds. */
732                                         if (iev->mask & IN_CREATE) {
733                                                 queue_file_create(iev->name);
734                                         } else if (iev->mask & IN_OPEN) {
735                                                 queue_file_open(iev->name);
736                                         } else if (iev->mask & IN_CLOSE_WRITE) {
737                                                 queue_file_write(iev->name);
738                                         } else if (iev->mask & IN_MOVED_TO) {
739                                                 queue_file(iev->name, 0);
740                                         } else {
741                                                 ast_log(LOG_ERROR, "Unexpected event %d for file '%s'\n", (int) iev->mask, iev->name);
742                                         }
743
744                                         len = sizeof(*iev) + iev->len;
745                                         res -= len;
746                                 }
747                         } else if (res < 0 && errno != EINTR && errno != EAGAIN) {
748                                 ast_debug(1, "Got an error back from %s(2): %s\n", stage ? "read" : "poll", strerror(errno));
749                         }
750                         time(&now);
751                 }
752                 queue_created_files();
753 #else
754                         int num_events;
755                         /* If queue empty then wait forever */
756                         if (next == INT_MAX) {
757                                 num_events = kevent(inotify_fd, &kev, 1, &event, 1, NULL);
758                         } else {
759                                 struct timespec ts2 = { .tv_sec = (unsigned long int)(next - now), .tv_nsec = 0 };
760                                 num_events = kevent(inotify_fd, &kev, 1, &event, 1, &ts2);
761                         }
762                         if ((num_events < 0) || (event.flags == EV_ERROR)) {
763                                 ast_debug(10, "KEvent error %s\n", strerror(errno));
764                                 continue;
765                         } else if (num_events == 0) {
766                                 /* Interrupt or timeout, restart calculations */
767                                 continue;
768                         } else {
769                                 /* Directory changed, rescan */
770                                 rewinddir(dir);
771                                 while ((de = readdir(dir))) {
772                                         queue_file(de->d_name, 0);
773                                 }
774                         }
775                         time(&now);
776                 }
777 #endif
778
779                 /* Empty the list of all entries ready to be processed */
780                 AST_LIST_LOCK(&dirlist);
781                 while (!AST_LIST_EMPTY(&dirlist) && AST_LIST_FIRST(&dirlist)->mtime <= now) {
782                         cur = AST_LIST_REMOVE_HEAD(&dirlist, list);
783                         queue_file(cur->name, cur->mtime);
784                         ast_free(cur);
785                 }
786                 AST_LIST_UNLOCK(&dirlist);
787         }
788         return NULL;
789 }
790
791 #else
792 static void *scan_thread(void *unused)
793 {
794         struct stat st;
795         DIR *dir;
796         struct dirent *de;
797         char fn[256];
798         int res;
799         int force_poll = 1;
800         time_t last = 0;
801         time_t next = 0;
802         time_t now;
803         struct timespec ts = { .tv_sec = 1 };
804
805         while (!ast_fully_booted) {
806                 nanosleep(&ts, NULL);
807         }
808
809         for (;;) {
810                 /* Wait a sec */
811                 nanosleep(&ts, NULL);
812                 time(&now);
813
814                 if (stat(qdir, &st)) {
815                         ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
816                         continue;
817                 }
818
819                 /* Make sure it is time for us to execute our check */
820                 if (!force_poll && st.st_mtime == last && (!next || now < next)) {
821                         /*
822                          * The directory timestamp did not change and any delayed
823                          * call-file is not ready to be executed.
824                          */
825                         continue;
826                 }
827
828 #if 0
829                 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
830                 printf("Ooh, something changed / timeout\n");
831 #endif
832
833                 if (!(dir = opendir(qdir))) {
834                         ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
835                         continue;
836                 }
837
838                 /*
839                  * Since the dir timestamp is available at one second
840                  * resolution, we cannot know if it was updated within the same
841                  * second after we scanned it.  Therefore, we will force another
842                  * scan if the dir was just modified.
843                  */
844                 force_poll = (st.st_mtime == now);
845
846                 next = 0;
847                 last = st.st_mtime;
848                 while ((de = readdir(dir))) {
849                         snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
850                         if (stat(fn, &st)) {
851                                 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
852                                 continue;
853                         }
854                         if (!S_ISREG(st.st_mode)) {
855                                 /* Not a regular file. */
856                                 continue;
857                         }
858                         if (st.st_mtime <= now) {
859                                 res = scan_service(fn, now);
860                                 if (res > 0) {
861                                         /* The call-file is delayed or to be retried later. */
862                                         if (!next || res < next) {
863                                                 /* This delayed call file expires earlier. */
864                                                 next = res;
865                                         }
866                                 } else if (res) {
867                                         ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
868                                 } else if (!next) {
869                                         /* Expired entry: must recheck on the next go-around */
870                                         next = st.st_mtime;
871                                 }
872                         } else {
873                                 /* The file's timestamp is in the future. */
874                                 if (!next || st.st_mtime < next) {
875                                         /* This call-file's timestamp expires earlier. */
876                                         next = st.st_mtime;
877                                 }
878                         }
879                 }
880                 closedir(dir);
881         }
882         return NULL;
883 }
884 #endif
885
886 static int unload_module(void)
887 {
888         return -1;
889 }
890
891 static int load_module(void)
892 {
893         pthread_t thread;
894         int ret;
895         snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
896         if (ast_mkdir(qdir, 0777)) {
897                 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
898                 return AST_MODULE_LOAD_DECLINE;
899         }
900         snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
901
902         if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
903                 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
904                 return AST_MODULE_LOAD_FAILURE;
905         }
906
907         return AST_MODULE_LOAD_SUCCESS;
908 }
909
910 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Outgoing Spool Support");