Merged revisions 123546 via svnmerge from

https://origsvn.digium.com/svn/asterisk/trunk

........
r123546 | bbryant | 2008-06-17 16:46:57 -0500 (Tue, 17 Jun 2008) | 5 lines

Updates all usages of ast_tcptls_session_instance to be managed by reference counts so that they only get destroyed when all threads are done using 
them, and memory does not get free'd causing strange issues with SIP. 

This code was originally written by russellb in the team/group/issue_11972/ branch.

........


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.6.0@123547 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Brett Bryant
2008-06-17 21:57:15 +00:00
parent ce6304e162
commit 6891a31c38
5 changed files with 98 additions and 68 deletions

View File

@@ -868,7 +868,8 @@ static void *httpd_helper_thread(void *data)
done:
fclose(ser->f);
ser = ast_tcptls_session_instance_destroy(ser);
ao2_ref(ser, -1);
ser = NULL;
return NULL;
}

View File

@@ -2937,7 +2937,8 @@ static void *session_do(void *data)
destroy_session(s);
done:
ser = ast_tcptls_session_instance_destroy(ser);
ao2_ref(ser, -1);
ser = NULL;
return NULL;
}

View File

@@ -83,6 +83,12 @@ static int ssl_close(void *cookie)
HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *ser, void *buf, size_t count)
{
if (ser->fd == -1) {
ast_log(LOG_ERROR, "server_read called with an fd of -1\n");
errno = EIO;
return -1;
}
#ifdef DO_SSL
if (ser->ssl)
return ssl_read(ser->ssl, buf, count);
@@ -92,6 +98,12 @@ HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *ser, void *buf
HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *ser, void *buf, size_t count)
{
if (ser->fd == -1) {
ast_log(LOG_ERROR, "server_write called with an fd of -1\n");
errno = EIO;
return -1;
}
#ifdef DO_SSL
if (ser->ssl)
return ssl_write(ser->ssl, buf, count);
@@ -99,6 +111,12 @@ HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *ser, void *bu
return write(ser->fd, buf, count);
}
static void session_instance_destructor(void *obj)
{
struct ast_tcptls_session_instance *i = obj;
ast_mutex_destroy(&i->lock);
}
void *ast_tcptls_server_root(void *data)
{
struct server_args *desc = data;
@@ -123,12 +141,15 @@ void *ast_tcptls_server_root(void *data)
ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
continue;
}
ser = ast_calloc(1, sizeof(*ser));
ser = ao2_alloc(sizeof(*ser), session_instance_destructor);
if (!ser) {
ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
close(fd);
continue;
}
ast_mutex_init(&ser->lock);
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
ser->fd = fd;
@@ -140,7 +161,7 @@ void *ast_tcptls_server_root(void *data)
if (ast_pthread_create_detached_background(&launched, NULL, ast_make_file_from_fd, ser)) {
ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
close(ser->fd);
ast_free(ser);
ao2_ref(ser, -1);
}
}
return NULL;
@@ -235,9 +256,11 @@ struct ast_tcptls_session_instance *ast_tcptls_client_start(struct server_args *
goto error;
}
if (!(ser = ast_calloc(1, sizeof(*ser))))
if (!(ser = ao2_alloc(sizeof(*ser), session_instance_destructor)))
goto error;
ast_mutex_init(&ser->lock);
flags = fcntl(desc->accept_fd, F_GETFL);
fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
@@ -262,7 +285,7 @@ error:
close(desc->accept_fd);
desc->accept_fd = -1;
if (ser)
ast_free(ser);
ao2_ref(ser, -1);
return NULL;
}
@@ -447,8 +470,3 @@ void *ast_make_file_from_fd(void *data)
return ser;
}
struct ast_tcptls_session_instance *ast_tcptls_session_instance_destroy(struct ast_tcptls_session_instance *i)
{
ast_free(i);
return NULL;
}