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