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