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