From 750f9da42a4d6e09b068ca959977707b20c98f36 Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Thu, 4 Mar 2010 17:46:46 -0500 Subject: [PATCH] ustd: destroy buffer struct after usage --- libustcomm/ustcomm.c | 11 +++++++++- ustd/ustd.c | 49 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 60428df..259931d 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -425,7 +425,9 @@ int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustco list_for_each_entry(conn, &server->connections, list) { if(conn->fd == fds[idx].fd) { + ustcomm_close_app(conn); list_del(&conn->list); + free(conn); break; } } @@ -636,7 +638,10 @@ int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn) return ustcomm_connect_path(path, conn, pid); } -/* Close a connection to a traceable app. */ +/* Close a connection to a traceable app. It frees the + * resources. It however does not free the + * ustcomm_connection itself. + */ int ustcomm_close_app(struct ustcomm_connection *conn) { @@ -784,11 +789,15 @@ static void ustcomm_fini_server(struct ustcomm_server *server, int keep_socket_f } } +/* Free a traceable application server */ + void ustcomm_fini_app(struct ustcomm_app *handle, int keep_socket_file) { ustcomm_fini_server(&handle->server, keep_socket_file); } +/* Free a ustd server */ + void ustcomm_fini_ustd(struct ustcomm_ustd *handle) { ustcomm_fini_server(&handle->server, 0); diff --git a/ustd/ustd.c b/ustd/ustd.c index 4db5502..8eeb62b 100644 --- a/ustd/ustd.c +++ b/ustd/ustd.c @@ -86,11 +86,10 @@ int get_subbuffer(struct buffer_info *buf) if(result != 2 && result != 1) { ERR("unable to parse response to get_subbuffer"); retval = -1; + free(received_msg); goto end_rep; } - DBG("received msg is %s", received_msg); - if(!strcmp(rep_code, "OK")) { DBG("got subbuffer %s", buf->name); retval = GET_SUBBUF_OK; @@ -400,6 +399,33 @@ error: return NULL; } +static void destroy_buffer(struct buffer_info *buf) +{ + int result; + + result = ustcomm_close_app(&buf->conn); + if(result == -1) { + WARN("problem calling ustcomm_close_app"); + } + + result = shmdt(buf->mem); + if(result == -1) { + PERROR("shmdt"); + } + + result = shmdt(buf->bufstruct_mem); + if(result == -1) { + PERROR("shmdt"); + } + + result = close(buf->file_fd); + if(result == -1) { + PERROR("close"); + } + + free(buf); +} + int unwrite_last_subbuffer(struct buffer_info *buf) { int result; @@ -513,10 +539,6 @@ int consumer_loop(struct buffer_info *buf) return 0; } -void free_buffer(struct buffer_info *buf) -{ -} - struct consumer_thread_args { pid_t pid; const char *bufname; @@ -537,7 +559,8 @@ void *consumer_thread(void *arg) consumer_loop(buf); - free_buffer(buf); + free(args->bufname); + destroy_buffer(buf); end: /* bufname is free'd in free_buffer() */ @@ -549,6 +572,7 @@ int start_consuming_buffer(pid_t pid, const char *bufname) { pthread_t thr; struct consumer_thread_args *args; + int result; DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid, bufname); @@ -558,7 +582,16 @@ int start_consuming_buffer(pid_t pid, const char *bufname) args->bufname = strdup(bufname); DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args->pid, args->bufname); - pthread_create(&thr, NULL, consumer_thread, args); + result = pthread_create(&thr, NULL, consumer_thread, args); + if(result == -1) { + ERR("pthread_create failed"); + return -1; + } + result = pthread_detach(thr); + if(result == -1) { + ERR("pthread_detach failed"); + return -1; + } DBG("end of start_consuming_buffer: args: pid %d bufname %s", args->pid, args->bufname); return 0; -- 2.34.1