Fix deletion of unopenable spool files.

If scan_service() cannot open the spool file, it logs a message saying
that it will delete the file and calls remove_from_queue() to do it.
However, remove_from_queue() fails to delete the spool file because struct
outgoing has not yet been fully initialized.

* Merged allocating a new struct outgoing and init_outgoing() into
new_outgoing().  Allocation is initialization.

* Made apply_outgoing() not initialize the spool filename in struct
outgoing.

* Made apply_outgoing() call ast_trim_blanks() and ast_skip_blanks()
rather than manually inlining them.

* Reduced indentation levels in apply_outgoing().

* Fixed a garbled comment in remove_from_queue().

* Reworked scan_service() to simplify it.

(closes issue ASTERISK-17231)
Reported by: David Chappell
Patches:
      spool_open_failure.diff (license #4997) patch uploaded by David Chappell
      Started with this patch.
........

Merged revisions 374686 from http://svn.asterisk.org/svn/asterisk/branches/1.8

* Fixed some memory leaks on off nominal paths in init_outgoing() when
merging into the new_outgoing() function dealing with o->capabilities.


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/10@374695 65c4cc65-6c06-0410-ace0-fbb531ad65f3
10
Richard Mudgett 13 years ago
parent dd0b2a2355
commit 4d3e8636b9

@ -101,36 +101,58 @@ struct outgoing {
static void queue_file(const char *filename, time_t when); static void queue_file(const char *filename, time_t when);
#endif #endif
static int init_outgoing(struct outgoing *o) static void free_outgoing(struct outgoing *o)
{
if (o->vars) {
ast_variables_destroy(o->vars);
}
o->capabilities = ast_format_cap_destroy(o->capabilities);
ast_string_field_free_memory(o);
ast_free(o);
}
static struct outgoing *new_outgoing(const char *fn)
{ {
struct outgoing *o;
struct ast_format tmpfmt; struct ast_format tmpfmt;
o->priority = 1;
o->retrytime = 300;
o->waittime = 45;
if (!(o->capabilities = ast_format_cap_alloc_nolock())) { o = ast_calloc(1, sizeof(*o));
return -1; if (!o) {
return NULL;
} }
ast_format_cap_add(o->capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
/* Initialize the new object. */
o->priority = 1;
o->retrytime = 300;
o->waittime = 45;
ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE); ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
if (ast_string_field_init(o, 128)) { if (ast_string_field_init(o, 128)) {
return -1; /*
* No need to call free_outgoing here since the failure was to
* allocate string fields and no variables have been allocated
* yet.
*/
ast_free(o);
return NULL;
} }
return 0; ast_string_field_set(o, fn, fn);
if (ast_strlen_zero(o->fn)) {
/* String field set failed. Since this string is important we must fail. */
free_outgoing(o);
return NULL;
} }
static void free_outgoing(struct outgoing *o) o->capabilities = ast_format_cap_alloc_nolock();
{ if (!o->capabilities) {
if (o->vars) { free_outgoing(o);
ast_variables_destroy(o->vars); return NULL;
} }
ast_string_field_free_memory(o); ast_format_cap_add(o->capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
o->capabilities = ast_format_cap_destroy(o->capabilities);
ast_free(o); return o;
} }
static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f) static int apply_outgoing(struct outgoing *o, FILE *f)
{ {
char buf[256]; char buf[256];
char *c, *c2; char *c, *c2;
@ -164,15 +186,17 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
} }
/* Trim trailing white space */ /* Trim trailing white space */
while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33) ast_trim_blanks(buf);
buf[strlen(buf) - 1] = '\0'; if (ast_strlen_zero(buf)) {
if (!ast_strlen_zero(buf)) { continue;
}
c = strchr(buf, ':'); c = strchr(buf, ':');
if (c) { if (!c) {
ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, o->fn);
continue;
}
*c = '\0'; *c = '\0';
c++; c = ast_skip_blanks(c + 1);
while ((*c) && (*c < 33))
c++;
#if 0 #if 0
printf("'%s' is '%s' at line %d\n", buf, c, lineno); printf("'%s' is '%s' at line %d\n", buf, c, lineno);
#endif #endif
@ -183,7 +207,7 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
ast_string_field_set(o, tech, c); ast_string_field_set(o, tech, c);
ast_string_field_set(o, dest, c2); ast_string_field_set(o, dest, c2);
} else { } else {
ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn); ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, o->fn);
} }
} else if (!strcasecmp(buf, "callerid")) { } else if (!strcasecmp(buf, "callerid")) {
char cid_name[80] = {0}, cid_num[80] = {0}; char cid_name[80] = {0}, cid_num[80] = {0};
@ -196,7 +220,7 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
ast_string_field_set(o, data, c); ast_string_field_set(o, data, c);
} else if (!strcasecmp(buf, "maxretries")) { } else if (!strcasecmp(buf, "maxretries")) {
if (sscanf(c, "%30d", &o->maxretries) != 1) { if (sscanf(c, "%30d", &o->maxretries) != 1) {
ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn); ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, o->fn);
o->maxretries = 0; o->maxretries = 0;
} }
} else if (!strcasecmp(buf, "codecs")) { } else if (!strcasecmp(buf, "codecs")) {
@ -207,17 +231,17 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
ast_string_field_set(o, exten, c); ast_string_field_set(o, exten, c);
} else if (!strcasecmp(buf, "priority")) { } else if (!strcasecmp(buf, "priority")) {
if ((sscanf(c, "%30d", &o->priority) != 1) || (o->priority < 1)) { if ((sscanf(c, "%30d", &o->priority) != 1) || (o->priority < 1)) {
ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn); ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, o->fn);
o->priority = 1; o->priority = 1;
} }
} else if (!strcasecmp(buf, "retrytime")) { } else if (!strcasecmp(buf, "retrytime")) {
if ((sscanf(c, "%30d", &o->retrytime) != 1) || (o->retrytime < 1)) { if ((sscanf(c, "%30d", &o->retrytime) != 1) || (o->retrytime < 1)) {
ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn); ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, o->fn);
o->retrytime = 300; o->retrytime = 300;
} }
} else if (!strcasecmp(buf, "waittime")) { } else if (!strcasecmp(buf, "waittime")) {
if ((sscanf(c, "%30d", &o->waittime) != 1) || (o->waittime < 1)) { if ((sscanf(c, "%30d", &o->waittime) != 1) || (o->waittime < 1)) {
ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, fn); ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, o->fn);
o->waittime = 45; o->waittime = 45;
} }
} else if (!strcasecmp(buf, "retry")) { } else if (!strcasecmp(buf, "retry")) {
@ -235,7 +259,7 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
c2 = c; c2 = c;
strsep(&c2, "="); strsep(&c2, "=");
if (c2) { if (c2) {
var = ast_variable_new(c, c2, fn); var = ast_variable_new(c, c2, o->fn);
if (var) { if (var) {
/* Always insert at the end, because some people want to treat the spool file as a script */ /* Always insert at the end, because some people want to treat the spool file as a script */
if (last) { if (last) {
@ -254,15 +278,11 @@ static int apply_outgoing(struct outgoing *o, const char *fn, FILE *f)
} else if (!strcasecmp(buf, "archive")) { } else if (!strcasecmp(buf, "archive")) {
ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE); ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE);
} else { } else {
ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn); ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, o->fn);
} }
} else
ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
} }
}
ast_string_field_set(o, fn, fn);
if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) { if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn); 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);
return -1; return -1;
} }
return 0; return 0;
@ -326,7 +346,7 @@ static int remove_from_queue(struct outgoing *o, const char *status)
} }
snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname); snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
/* a existing call file the archive dir is overwritten */ /* If there is already a call file with the name in the archive dir, it will be overwritten. */
unlink(newfn); unlink(newfn);
if (rename(o->fn, newfn) != 0) { if (rename(o->fn, newfn) != 0) {
unlink(o->fn); unlink(o->fn);
@ -391,47 +411,46 @@ static void launch_service(struct outgoing *o)
/* Called from scan_thread or queue_file */ /* Called from scan_thread or queue_file */
static int scan_service(const char *fn, time_t now) static int scan_service(const char *fn, time_t now)
{ {
struct outgoing *o = NULL; struct outgoing *o;
FILE *f; FILE *f;
int res = 0; int res;
if (!(o = ast_calloc(1, sizeof(*o)))) { o = new_outgoing(fn);
ast_log(LOG_WARNING, "Out of memory ;(\n"); if (!o) {
return -1; return -1;
} }
if (init_outgoing(o)) { /* Attempt to open the file */
/* No need to call free_outgoing here since we know the failure f = fopen(o->fn, "r");
* was to allocate string fields and no variables have been allocated if (!f) {
* yet. #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)
/*!
* \todo XXX There is some odd delayed duplicate servicing of
* call files going on. We need to suppress the error message
* if the file does not exist as a result.
*/ */
ast_free(o); if (errno != ENOENT)
return -1; #endif
{
ast_log(LOG_WARNING, "Unable to open %s: '%s'(%d), deleting\n",
o->fn, strerror(errno), (int) errno);
} }
/* Attempt to open the file */
if (!(f = fopen(fn, "r"))) {
remove_from_queue(o, "Failed"); remove_from_queue(o, "Failed");
free_outgoing(o); 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; return -1;
} }
/* Read in and verify the contents */ /* Read in and verify the contents */
if (apply_outgoing(o, fn, f)) { res = apply_outgoing(o, f);
fclose(f);
if (res) {
ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", o->fn);
remove_from_queue(o, "Failed"); remove_from_queue(o, "Failed");
free_outgoing(o); free_outgoing(o);
ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
fclose(f);
return -1; return -1;
} }
#if 0 ast_debug(1, "Filename: %s, Retries: %d, max: %d\n", o->fn, o->retries, o->maxretries);
printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
#endif
fclose(f);
if (o->retries <= o->maxretries) { if (o->retries <= o->maxretries) {
now += o->retrytime; now += o->retrytime;
if (o->callingpid && (o->callingpid == ast_mainpid)) { if (o->callingpid && (o->callingpid == ast_mainpid)) {
@ -449,14 +468,14 @@ static int scan_service(const char *fn, time_t now)
safe_append(o, now, "StartRetry"); safe_append(o, now, "StartRetry");
launch_service(o); launch_service(o);
} }
res = now; return now;
} else {
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" : "");
remove_from_queue(o, "Expired");
free_outgoing(o);
} }
return res; 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" : "");
remove_from_queue(o, "Expired");
free_outgoing(o);
return 0;
} }
#if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE) #if defined(HAVE_INOTIFY) || defined(HAVE_KQUEUE)

Loading…
Cancel
Save