Merged revisions 72806 via svnmerge from
[asterisk/asterisk.git] / pbx / pbx_spool.c
index d9b0eac..6c045d3 100644 (file)
@@ -257,7 +257,7 @@ static void safe_append(struct outgoing *o, time_t now, char *s)
        if (fd > -1) {
                f = fdopen(fd, "a");
                if (f) {
-                       fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
+                       fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
                        fclose(f);
                } else
                        close(fd);
@@ -294,7 +294,7 @@ static int remove_from_queue(struct outgoing *o, const char *status)
                unlink(o->fn);
                return 0;
        }
-       if (mkdir(qdonedir, 0700) && (errno != EEXIST)) {
+       if (ast_mkdir(qdonedir, 0777)) {
                ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
                unlink(o->fn);
                return -1;
@@ -359,11 +359,9 @@ static void *attempt_thread(void *data)
 static void launch_service(struct outgoing *o)
 {
        pthread_t t;
-       pthread_attr_t attr;
        int ret;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       if ((ret = ast_pthread_create(&t,&attr,attempt_thread, o)) != 0) {
+
+       if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
                ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
                free_outgoing(o);
        }
@@ -387,8 +385,8 @@ static int scan_service(char *fn, time_t now, time_t atime)
                                        now += o->retrytime;
                                        if (o->callingpid && (o->callingpid == ast_mainpid)) {
                                                safe_append(o, time(NULL), "DelayedRetry");
-                                               free_outgoing(o);
                                                ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
+                                               free_outgoing(o);
                                        } else {
                                                /* Increment retries */
                                                o->retries++;
@@ -403,20 +401,20 @@ static int scan_service(char *fn, time_t now, time_t atime)
                                        return now;
                                } else {
                                        ast_log(LOG_EVENT, "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" : "");
-                                       free_outgoing(o);
                                        remove_from_queue(o, "Expired");
+                                       free_outgoing(o);
                                        return 0;
                                }
                        } else {
+                               remove_from_queue(o, "Failed");
                                free_outgoing(o);
                                ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
                                fclose(f);
-                               remove_from_queue(o, "Failed");
                        }
                } else {
+                       remove_from_queue(o, "Failed");
                        free_outgoing(o);
                        ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
-                       remove_from_queue(o, "Failed");
                }
        } else
                ast_log(LOG_WARNING, "Out of memory :(\n");
@@ -485,20 +483,19 @@ static int unload_module(void)
 static int load_module(void)
 {
        pthread_t thread;
-       pthread_attr_t attr;
        int ret;
        snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
-       if (mkdir(qdir, 0700) && (errno != EEXIST)) {
+       if (ast_mkdir(qdir, 0777)) {
                ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
                return 0;
        }
        snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       if ((ret = ast_pthread_create_background(&thread,&attr,scan_thread, NULL)) != 0) {
+
+       if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
                ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
                return -1;
        }
+
        return 0;
 }