Commit ebb6c9ae authored by 's avatar
Browse files

remove many memory leaks, most of them in DiMAPI over SSL

git-svn-id: file:///home/svn/mapi/trunk@983 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent c89775a3
......@@ -116,13 +116,11 @@ int mapiipc_remote_write(struct dmapiipcbuf *dbuf, struct host *h)
#ifdef DIMAPISSL
if(SSL_write(h->con,dbuf,dbuf->length) == -1){
WARNING_CMD(printf("SSL_write: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
// exit(1);
return -1;
}
#else
if(send(h->sockfd, dbuf, dbuf->length, 0) == -1) {
WARNING_CMD(printf("send: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
// exit(1);
return -1;
}
#endif
......@@ -150,6 +148,12 @@ int mapiipc_remote_write_to_all(remote_flowdescr_t* rflow)
}
return 0;
}
void cleanup_handler(void *arg){ //the cleanup handler
free(arg);
return;
}
void *mapiipc_comm_thread(void *host) {
//Reads an IPC message. Blocking call
......@@ -167,10 +171,19 @@ void *mapiipc_comm_thread(void *host) {
#endif
/* Guarantees that thread resources are deallocated upon return */
pthread_detach(pthread_self());
pthread_detach(pthread_self());
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // enable cancellation
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // changes the type of responses to cancellation requests for the calling thread
// asynchronous (cancel the calling thread as soon as the cancellation request is received)
dbuf = (struct dmapiipcbuf *)malloc(sizeof(struct dmapiipcbuf));
// pthread_cleanup_push() function pushes the specified cancellation cleanup handler onto
// the cancellation cleanup stack of the calling thread. When a thread exits or is cancelled,
// and its cancellation cleanup stack is not empty, the cleanup handlers are invoked with
// the specified argument in last-in-first-out order from the cancellation cleanup stack
pthread_cleanup_push(cleanup_handler, dbuf);
while (1) {
if (host==NULL) break;
......@@ -200,7 +213,7 @@ void *mapiipc_comm_thread(void *host) {
#ifdef DIMAPISSL
recv_bytes = SSL_readn(hostconn,(char *)dbuf + BASIC_SIZE, dbuf->length - BASIC_SIZE );
#else
recv_bytes=readn(sockfd, (char*)dbuf+BASIC_SIZE, dbuf->length-BASIC_SIZE);
recv_bytes=readn(sockfd, (char*)dbuf+BASIC_SIZE, dbuf->length-BASIC_SIZE);
#endif
if (recv_bytes == 0) { // the peer has gone
//printf("Socket closed\n");
......@@ -234,8 +247,9 @@ void *mapiipc_comm_thread(void *host) {
}
}
free(dbuf);
pthread_cleanup_pop(1); // pthread_cleanup_pop() function shall remove the routine at the top of the
// calling thread's cancellation cleanup stack and invoke it
return NULL;
}
......@@ -247,8 +261,8 @@ int mapiipc_remote_init(struct host *h)
struct timeval tv;
#ifdef DIMAPISSL
SSL_library_init();
SSL_load_error_strings();
SSL_library_init(); // registers the available ciphers and digests
SSL_load_error_strings(); // registers the error strings for all libcrypto functions and libssl
if ((ctx=SSL_CTX_new(SSLv3_client_method())) == NULL) {
ERR_print_errors_fp(stderr);
......@@ -322,13 +336,23 @@ int mapiipc_remote_init(struct host *h)
void mapiipc_remote_close(struct host *h)
//Releases socket resources
{
shutdown(h->sockfd, SHUT_RDWR);
close(h->sockfd);
#ifdef DIMAPISSL
if (SSL_shutdown(h->con) == -1) {
if (SSL_shutdown(h->con) == -1) // shut down a TLS/SSL connection
ERR_print_errors_fp(stderr);
}
SSL_free(h->con); // decrements the reference count of ssl, and removes the SSL structure pointed to by ssl
// frees up the allocated memory if the the reference count has reached 0
if(ctx != NULL)
SSL_CTX_free(ctx); // decrements the reference count of ctx, and removes the SSL_CTX object pointed to by ctx
// frees up the allocated memory if the the reference count has reached 0
ERR_remove_state(0); // the current thread will have its error queue removed
ERR_free_strings(); // frees all previously loaded error strings
EVP_cleanup(); // removes all ciphers and digests from the table
CRYPTO_cleanup_all_ex_data(); // clean up all allocated state
#endif
shutdown(h->sockfd, SHUT_RDWR);
close(h->sockfd);
}
#endif /* DIMAPI */
......
......@@ -445,11 +445,11 @@ static void delete_remote_flow(remote_flowdescr_t* rflow)
hflow->rhost->num_flows--;
flist_remove(hflow->rhost->flows, hflow->fd);
for (fnode3=flist_head(hflow->functions); fnode3!=NULL; fnode3=flist_next(fnode3)) {
flist_remove(hflow->rhost->functions, ((function_data*)fnode3->data)->fid);
free( flist_remove(hflow->rhost->functions, ((function_data*)fnode3->data)->fid) );
}
if (hflow->rhost->num_flows==0) {
mapiipc_remote_close(hflow->rhost); //close the socket
pthread_cancel(*hflow->rhost->comm_thread);
mapiipc_remote_close(hflow->rhost); //close the socket
flist_destroy(hflow->rhost->flows);
free(hflow->rhost->flows);
flist_destroy(hflow->rhost->functions);
......
......@@ -28,10 +28,19 @@ extern void set_agent();
int service_count;
int dimapi_port;
#ifdef DIMAPISSL
struct overload *inst = NULL;
SSL_CTX *ctx;
#endif
void *handle_request(void *);
int die(char *msg);
int getfid(struct dmapiipcbuf *dbuf);
#ifdef DIMAPISSL
void mapicommd_shutdown();
#endif
int main() {
int serv_sock = 0;
......@@ -43,17 +52,20 @@ int main() {
struct sockaddr_in clnt_addr;
#ifdef DIMAPISSL
struct overload *inst;
SSL *con=NULL;
SSL_CTX *ctx;
OpenSSL_add_all_algorithms(); /* load & register all cryptos, etc. */
SSL_library_init();
SSL_load_error_strings();
SSL *con = NULL;
OpenSSL_add_all_algorithms(); // adds all algorithms to the table (digests and ciphers)
// OpenSSL keeps an internal table of digest algorithms and ciphers
SSL_library_init(); // registers the available ciphers and digests
SSL_load_error_strings(); // registers the error strings for all libcrypto functions and libssl
#endif
pthread_t chld_thr;
char* mapi_conf;
#ifdef DIMAPISSL
signal (SIGTERM, mapicommd_shutdown);
signal (SIGQUIT, mapicommd_shutdown);
signal (SIGINT, mapicommd_shutdown);
#endif
//mapi_conf = malloc(sizeof(CONF_FILE)-2+strlen(getenv("HOME")));
//sprintf(mapi_conf,CONF_FILE,getenv("HOME"));
mapi_conf = printf_string( CONFDIR"/"CONF_FILE );
......@@ -193,8 +205,14 @@ void *handle_request(void *arg) {
/* Guarantees that thread resources are deallocated upon return */
pthread_detach(pthread_self());
dbuf = (struct dmapiipcbuf *)malloc(sizeof(struct dmapiipcbuf));
#ifdef DIMAPISSL
printf("<+> new thread %d, socket number = %d\n", (int)pthread_self(),(int) con);
if(inst != NULL){
free(inst);
inst = NULL;
}
#else
printf("<+> new thread %d, socket number = %d\n", (int)pthread_self(), sock);
#endif
......@@ -444,15 +462,16 @@ void *handle_request(void *arg) {
free(active_flows);
free(dbuf);
shutdown(sock, SHUT_RDWR);
close(sock);
#ifdef DIMAPISSL
if (SSL_shutdown(con) == -1)
if (SSL_shutdown(con) == -1) // shut down a TLS/SSL connection
ERR_print_errors_fp(stderr);
#endif
SSL_free(con); // decrements the reference count of ssl, and removes the SSL structure pointed to by ssl
// frees up the allocated memory if the the reference count has reached 0
#endif
/* update the global service counter */
//pthread_mutex_lock(&lock); DELETE
......@@ -463,7 +482,6 @@ if (SSL_shutdown(con) == -1)
pthread_exit((void *)0);
}
int die(char *msg) {
perror(msg);
return EXIT_FAILURE;
......@@ -480,7 +498,17 @@ int getfid(struct dmapiipcbuf *dbuf){
return (result);
}
#ifdef DIMAPISSL
void mapicommd_shutdown(){
if(ctx != NULL)
SSL_CTX_free(ctx); // decrements the reference count of ctx, and removes the SSL_CTX object pointed to by ctx
// frees up the allocated memory if the the reference count has reached 0
CRYPTO_cleanup_all_ex_data(); // clean up all allocated state
ERR_free_strings(); // frees all previously loaded error strings
ERR_remove_state(0); // the current thread will have its error queue removed
EVP_cleanup(); // removes all ciphers and digests from the table
exit(0);
}
#endif
......@@ -272,6 +272,8 @@ mapid_shutdown (int data)
free(gflist);
free(mapidsocket);
free(mapid_conf);
/* close error-lookup file */
//close_error ();
running_shutdown = 2;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment