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