Merged revisions 324849 via svnmerge from
[asterisk/asterisk.git] / pbx / pbx_spool.c
index 00d6041..b908663 100644 (file)
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-/* Handling of call files using inotify is not functioning correctly currently:
- * Issue 18089 - https://issues.asterisk.org/view.php?id=18089 
- */
-#undef HAVE_INOTIFY
-
 #include <sys/stat.h>
 #include <time.h>
 #include <utime.h>
@@ -67,7 +62,7 @@ enum {
         */
        SPOOL_FLAG_ALWAYS_DELETE = (1 << 0),
        /* Don't unlink the call file after processing, move in qdonedir */
-       SPOOL_FLAG_ARCHIVE = (1 << 1)
+       SPOOL_FLAG_ARCHIVE = (1 << 1),
 };
 
 static char qdir[255];
@@ -79,7 +74,7 @@ struct outgoing {
        int retrytime;                            /*!< How long to wait between retries (in seconds) */
        int waittime;                             /*!< How long to wait for an answer */
        long callingpid;                          /*!< PID which is currently calling */
-       format_t format;                          /*!< Formats (codecs) for this call */
+       struct ast_format_cap *capabilities;                 /*!< Formats (codecs) for this call */
        AST_DECLARE_STRING_FIELDS (
                AST_STRING_FIELD(fn);                 /*!< File name of call file */
                AST_STRING_FIELD(tech);               /*!< Which channel technology to use for outgoing call */
@@ -98,12 +93,22 @@ struct outgoing {
        struct ast_flags options;                 /*!< options */
 };
 
+#if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
+static void queue_file(const char *filename, time_t when);
+#endif
+
 static int init_outgoing(struct outgoing *o)
 {
+       struct ast_format tmpfmt;
        o->priority = 1;
        o->retrytime = 300;
        o->waittime = 45;
-       o->format = AST_FORMAT_SLINEAR;
+
+       if (!(o->capabilities = ast_format_cap_alloc_nolock())) {
+               return -1;
+       }
+       ast_format_cap_add(o->capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
+
        ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
        if (ast_string_field_init(o, 128)) {
                return -1;
@@ -117,6 +122,7 @@ static void free_outgoing(struct outgoing *o)
                ast_variables_destroy(o->vars);
        }
        ast_string_field_free_memory(o);
+       o->capabilities = ast_format_cap_destroy(o->capabilities);
        ast_free(o);
 }
 
@@ -190,7 +196,7 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
                                                o->maxretries = 0;
                                        }
                                } else if (!strcasecmp(buf, "codecs")) {
-                                       ast_parse_allow_disallow(NULL, &o->format, c, 1);
+                                       ast_parse_allow_disallow(NULL, o->capabilities, c, 1);
                                } else if (!strcasecmp(buf, "context")) {
                                        ast_string_field_set(o, context, c);
                                } else if (!strcasecmp(buf, "extension")) {
@@ -260,24 +266,20 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
 
 static void safe_append(struct outgoing *o, time_t now, char *s)
 {
-       int fd;
        FILE *f;
-       struct utimbuf tbuf;
+       struct utimbuf tbuf = { .actime = now, .modtime = now + o->retrytime };
 
-       if ((fd = open(o->fn, O_WRONLY | O_APPEND)) < 0)
-               return;
+       ast_debug(1, "Outgoing %s/%s: %s\n", o->tech, o->dest, s);
 
-       if ((f = fdopen(fd, "a"))) {
+       if ((f = fopen(o->fn, "a"))) {
                fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
                fclose(f);
-       } else
-               close(fd);
+       }
 
        /* Update the file time */
-       tbuf.actime = now;
-       tbuf.modtime = now + o->retrytime;
-       if (utime(o->fn, &tbuf))
+       if (utime(o->fn, &tbuf)) {
                ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
+       }
 }
 
 /*!
@@ -288,7 +290,6 @@ static void safe_append(struct outgoing *o, time_t now, char *s)
  */
 static int remove_from_queue(struct outgoing *o, const char *status)
 {
-       int fd;
        FILE *f;
        char newfn[256];
        const char *bname;
@@ -297,8 +298,9 @@ static int remove_from_queue(struct outgoing *o, const char *status)
                struct stat current_file_status;
 
                if (!stat(o->fn, &current_file_status)) {
-                       if (time(NULL) < current_file_status.st_mtime)
+                       if (time(NULL) < current_file_status.st_mtime) {
                                return 0;
+                       }
                }
        }
 
@@ -313,26 +315,28 @@ static int remove_from_queue(struct outgoing *o, const char *status)
                return -1;
        }
 
-       if ((fd = open(o->fn, O_WRONLY | O_APPEND))) {
-               if ((f = fdopen(fd, "a"))) {
-                       fprintf(f, "Status: %s\n", status);
-                       fclose(f);
-               } else
-                       close(fd);
+       if (!(bname = strrchr(o->fn, '/'))) {
+               bname = o->fn;
+       } else {
+               bname++;
        }
 
-       if (!(bname = strrchr(o->fn, '/')))
-               bname = o->fn;
-       else
-               bname++;        
        snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
        /* a existing call file the archive dir is overwritten */
        unlink(newfn);
        if (rename(o->fn, newfn) != 0) {
                unlink(o->fn);
                return -1;
-       } else
-               return 0;
+       }
+
+       /* Only append to the file AFTER we move it out of the watched directory,
+        * otherwise the fclose() causes another event for inotify(7) */
+       if ((f = fopen(newfn, "a"))) {
+               fprintf(f, "Status: %s\n", status);
+               fclose(f);
+       }
+
+       return 0;
 }
 
 static void *attempt_thread(void *data)
@@ -341,11 +345,11 @@ static void *attempt_thread(void *data)
        int res, reason;
        if (!ast_strlen_zero(o->app)) {
                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);
-               res = ast_pbx_outgoing_app(o->tech, o->format, (void *) o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL);
+               res = ast_pbx_outgoing_app(o->tech, o->capabilities, (void *) o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL);
                o->vars = NULL;
        } else {
                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);
-               res = ast_pbx_outgoing_exten(o->tech, o->format, (void *) o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL);
+               res = ast_pbx_outgoing_exten(o->tech, o->capabilities, (void *) o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL);
                o->vars = NULL;
        }
        if (res) {
@@ -357,6 +361,9 @@ static void *attempt_thread(void *data)
                } else {
                        /* Notate that the call is still active */
                        safe_append(o, time(NULL), "EndRetry");
+#if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
+                       queue_file(o->fn, time(NULL) + o->retrytime);
+#endif
                }
        } else {
                ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
@@ -388,7 +395,7 @@ static int scan_service(const char *fn, time_t now)
                ast_log(LOG_WARNING, "Out of memory ;(\n");
                return -1;
        }
-       
+
        if (init_outgoing(o)) {
                /* No need to call free_outgoing here since we know the failure
                 * was to allocate string fields and no variables have been allocated
@@ -399,10 +406,12 @@ static int scan_service(const char *fn, time_t now)
        }
 
        /* Attempt to open the file */
-       if (!(f = fopen(fn, "r+"))) {
+       if (!(f = fopen(fn, "r"))) {
                remove_from_queue(o, "Failed");
                free_outgoing(o);
+#if !defined(HAVE_INOTIFY) && !defined(HAVE_KQUEUE)
                ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
+#endif
                return -1;
        }
 
@@ -414,7 +423,7 @@ static int scan_service(const char *fn, time_t now)
                fclose(f);
                return -1;
        }
-       
+
 #if 0
        printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
 #endif
@@ -423,7 +432,7 @@ static int scan_service(const char *fn, time_t now)
                now += o->retrytime;
                if (o->callingpid && (o->callingpid == ast_mainpid)) {
                        safe_append(o, time(NULL), "DelayedRetry");
-                       ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
+                       ast_debug(1, "Delaying retry since we're currently running '%s'\n", o->fn);
                        free_outgoing(o);
                } else {
                        /* Increment retries */
@@ -432,7 +441,7 @@ static int scan_service(const char *fn, time_t now)
                           so abort their retry and continue as we were... */
                        if (o->callingpid)
                                safe_append(o, time(NULL), "AbortRetry");
-                       
+
                        safe_append(o, now, "StartRetry");
                        launch_service(o);
                }
@@ -453,8 +462,12 @@ struct direntry {
        char name[0];
 };
 
+static AST_LIST_HEAD_STATIC(dirlist, direntry);
+
+#if defined(HAVE_INOTIFY)
 /* Only one thread is accessing this list, so no lock is necessary */
-static AST_LIST_HEAD_NOLOCK_STATIC(dirlist, direntry);
+static AST_LIST_HEAD_NOLOCK_STATIC(createlist, direntry);
+#endif
 
 static void queue_file(const char *filename, time_t when)
 {
@@ -482,17 +495,18 @@ static void queue_file(const char *filename, time_t when)
                when = st.st_mtime;
        }
 
-#ifndef HAVE_INOTIFY
-       /* Need to check the existing list for kqueue(2), in order to avoid duplicates. */
+       /* Need to check the existing list in order to avoid duplicates. */
+       AST_LIST_LOCK(&dirlist);
        AST_LIST_TRAVERSE(&dirlist, cur, list) {
                if (cur->mtime == when && !strcmp(filename, cur->name)) {
+                       AST_LIST_UNLOCK(&dirlist);
                        return;
                }
        }
-#endif
 
        if ((res = when) > now || (res = scan_service(filename, now)) > 0) {
                if (!(new = ast_calloc(1, sizeof(*new) + strlen(filename) + 1))) {
+                       AST_LIST_UNLOCK(&dirlist);
                        return;
                }
                new->mtime = res;
@@ -515,7 +529,42 @@ static void queue_file(const char *filename, time_t when)
                        }
                }
        }
+       AST_LIST_UNLOCK(&dirlist);
+}
+
+#ifdef HAVE_INOTIFY
+static void queue_file_create(const char *filename)
+{
+       struct direntry *cur;
+
+       AST_LIST_TRAVERSE(&createlist, cur, list) {
+               if (!strcmp(cur->name, filename)) {
+                       return;
+               }
+       }
+
+       if (!(cur = ast_calloc(1, sizeof(*cur) + strlen(filename) + 1))) {
+               return;
+       }
+       strcpy(cur->name, filename);
+       AST_LIST_INSERT_TAIL(&createlist, cur, list);
+}
+
+static void queue_file_write(const char *filename)
+{
+       struct direntry *cur;
+       /* Only queue entries where an IN_CREATE preceded the IN_CLOSE_WRITE */
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&createlist, cur, list) {
+               if (!strcmp(cur->name, filename)) {
+                       AST_LIST_REMOVE_CURRENT(list);
+                       ast_free(cur);
+                       queue_file(filename, 0);
+                       break;
+               }
+       }
+       AST_LIST_TRAVERSE_SAFE_END
 }
+#endif
 
 static void *scan_thread(void *unused)
 {
@@ -524,15 +573,10 @@ static void *scan_thread(void *unused)
        time_t now;
        struct timespec ts = { .tv_sec = 1 };
 #ifdef HAVE_INOTIFY
-       int res;
+       ssize_t res;
        int inotify_fd = inotify_init();
-       struct {
-               struct inotify_event iev;
-               /* It may not look like we're using this element, but when we read
-                * from inotify_fd, the event is typically larger than the first
-                * struct, and overflows into this second one. */
-               char name[FILENAME_MAX + 1];
-       } buf;
+       struct inotify_event *iev;
+       char buf[8192] __attribute__((aligned (sizeof(int))));
        struct pollfd pfd = { .fd = inotify_fd, .events = POLLIN };
 #else
        struct timespec nowait = { 0, 1 };
@@ -557,7 +601,7 @@ static void *scan_thread(void *unused)
        }
 
 #ifdef HAVE_INOTIFY
-       inotify_add_watch(inotify_fd, qdir, IN_CLOSE_WRITE | IN_MOVED_TO);
+       inotify_add_watch(inotify_fd, qdir, IN_CREATE | IN_CLOSE_WRITE | IN_MOVED_TO);
 #endif
 
        /* First, run through the directory and clear existing entries */
@@ -567,7 +611,7 @@ static void *scan_thread(void *unused)
        }
 
 #ifndef HAVE_INOTIFY
-       EV_SET(&kev, dirfd(dir), EVFILT_VNODE, EV_ADD | EV_ENABLE, NOTE_WRITE | NOTE_EXTEND | NOTE_DELETE | NOTE_REVOKE | NOTE_ATTRIB, 0, NULL);
+       EV_SET(&kev, dirfd(dir), EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_WRITE, 0, NULL);
        if (kevent(inotify_fd, &kev, 1, NULL, 0, &nowait) < 0 && errno != 0) {
                ast_log(LOG_ERROR, "Unable to watch directory %s: %s\n", qdir, strerror(errno));
        }
@@ -595,17 +639,23 @@ static void *scan_thread(void *unused)
                        int waittime = next == INT_MAX ? -1 : (next - now) * 1000;
                        /* When a file arrives, add it to the queue, in mtime order. */
                        if ((res = poll(&pfd, 1, waittime)) > 0 && (stage = 1) &&
-                               (res = read(inotify_fd, &buf, sizeof(buf))) >= sizeof(buf.iev)) {
+                               (res = read(inotify_fd, &buf, sizeof(buf))) >= sizeof(*iev)) {
+                               ssize_t len = 0;
                                /* File(s) added to directory, add them to my list */
-                               do {
-                                       queue_file(buf.iev.name, 0);
-                                       res -= sizeof(buf.iev) + buf.iev.len;
-                                       if (res >= sizeof(buf.iev)) {
-                                               memmove(&buf.iev, &buf.iev.name[buf.iev.len], res);
-                                               continue;
+                               for (iev = (void *) buf; res >= sizeof(*iev); iev = (struct inotify_event *) (((char *) iev) + len)) {
+                                       if (iev->mask & IN_CREATE) {
+                                               queue_file_create(iev->name);
+                                       } else if (iev->mask & IN_CLOSE_WRITE) {
+                                               queue_file_write(iev->name);
+                                       } else if (iev->mask & IN_MOVED_TO) {
+                                               queue_file(iev->name, 0);
+                                       } else {
+                                               ast_log(LOG_ERROR, "Unexpected event %d for file '%s'\n", (int) iev->mask, iev->name);
                                        }
-                                       break;
-                               } while (1);
+
+                                       len = sizeof(*iev) + iev->len;
+                                       res -= len;
+                               }
                        } else if (res < 0 && errno != EINTR && errno != EAGAIN) {
                                ast_debug(1, "Got an error back from %s(2): %s\n", stage ? "read" : "poll", strerror(errno));
                        }
@@ -626,11 +676,13 @@ static void *scan_thread(void *unused)
                }
 
                /* Empty the list of all entries ready to be processed */
+               AST_LIST_LOCK(&dirlist);
                while (!AST_LIST_EMPTY(&dirlist) && AST_LIST_FIRST(&dirlist)->mtime <= now) {
                        cur = AST_LIST_REMOVE_HEAD(&dirlist, list);
                        queue_file(cur->name, cur->mtime);
                        ast_free(cur);
                }
+               AST_LIST_UNLOCK(&dirlist);
        }
        return NULL;
 }
@@ -645,7 +697,7 @@ static void *scan_thread(void *unused)
        int res;
        time_t last = 0, next = 0, now;
        struct timespec ts = { .tv_sec = 1 };
-  
+
        while (!ast_fully_booted) {
                nanosleep(&ts, NULL);
        }
@@ -663,7 +715,7 @@ static void *scan_thread(void *unused)
                /* Make sure it is time for us to execute our check */
                if ((st.st_mtime == last) && (next && (next > now)))
                        continue;
-               
+
 #if 0
                printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
                printf("Ooh, something changed / timeout\n");