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