fixed some memory leaks

Sat, 19 Jan 2013 20:13:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 19 Jan 2013 20:13:07 +0100
changeset 48
37a512d7b8f6
parent 47
ce9790523346
child 49
1fd94945796e

fixed some memory leaks

make/linux.mk file | annotate | diff | comparison | revisions
make/suncc.mk file | annotate | diff | comparison | revisions
src/server/Makefile file | annotate | diff | comparison | revisions
src/server/daemon/event_linux.c file | annotate | diff | comparison | revisions
src/server/daemon/ldap_auth.c file | annotate | diff | comparison | revisions
src/server/daemon/main.c file | annotate | diff | comparison | revisions
src/server/daemon/protocol.c file | annotate | diff | comparison | revisions
src/server/daemon/sessionhandler.c file | annotate | diff | comparison | revisions
src/server/safs/auth.c file | annotate | diff | comparison | revisions
src/server/safs/service.c file | annotate | diff | comparison | revisions
src/server/util/util.h file | annotate | diff | comparison | revisions
src/server/webdav/davparser.cpp file | annotate | diff | comparison | revisions
src/server/webdav/davparser.h file | annotate | diff | comparison | revisions
src/server/webdav/saxhandler.cpp file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
src/server/webdav/webdav.h file | annotate | diff | comparison | revisions
--- a/make/linux.mk	Sun Jan 13 14:16:45 2013 +0100
+++ b/make/linux.mk	Sat Jan 19 20:13:07 2013 +0100
@@ -30,6 +30,7 @@
 # compiler and linker flags
 
 LDFLAGS = 
+CFLAGS += -DLINUX
 
 
 # platform dependend source files
--- a/make/suncc.mk	Sun Jan 13 14:16:45 2013 +0100
+++ b/make/suncc.mk	Sat Jan 19 20:13:07 2013 +0100
@@ -26,7 +26,7 @@
 # POSSIBILITY OF SUCH DAMAGE.
 #
 
-CFLAGS = -xc99 -D_REENTRANT
+CFLAGS = -xc99 -g -D_REENTRANT
 
 CC  = cc
 CXX = CC
--- a/src/server/Makefile	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/Makefile	Sat Jan 19 20:13:07 2013 +0100
@@ -29,7 +29,7 @@
 BUILD_ROOT = ../..
 include $(BUILD_ROOT)/config.mk
 
-LDFLAGS += -pg -lpthread -ldl -lnsl -lm -lxerces-c -lldap
+LDFLAGS += -lpthread -ldl -lnsl -lm -lxerces-c -lldap
 
 OBJ_DIR = $(BUILD_ROOT)/build/
 
--- a/src/server/daemon/event_linux.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/daemon/event_linux.c	Sat Jan 19 20:13:07 2013 +0100
@@ -95,13 +95,10 @@
     
     //port_event_t events[16];
     struct epoll_event events[16];
-    struct timespec timeout;
-    timeout.tv_nsec = 0;
-    timeout.tv_sec = 600;
     
     for(;;) {
         /* wait for events */
-        int ret = epoll_wait(ep, events, 16, 0);
+        int ret = epoll_wait(ep, events, 16, 100000);
         if(ret == -1) {
             /* TODO: check for error */
             perror("epoll_wait");
--- a/src/server/daemon/ldap_auth.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/daemon/ldap_auth.c	Sat Jan 19 20:13:07 2013 +0100
@@ -128,7 +128,7 @@
 }
 
 void ldap_user_free(User *u) {
-    LDAPUser *user = (LDAPUser*) u;
+    LDAPUser *user = (LDAPUser*)u;
     ldap_memfree(user->userdn);
     // TODO: use connection pool
     ldap_unbind(user->ldap);
--- a/src/server/daemon/main.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/daemon/main.c	Sat Jan 19 20:13:07 2013 +0100
@@ -129,8 +129,8 @@
         //FILE *ws_out = fdopen(std_pipe_fds[1], "w");
         //*stdout = *ws_out;
         //*stderr = *ws_out;
-        dup2(std_pipe_fds[1], 1);
-        dup2(std_pipe_fds[1], 2);
+        //dup2(std_pipe_fds[1], 1);
+        //dup2(std_pipe_fds[1], 2);
 
         pthread_t tid;
         pthread_create(&tid, NULL, log_pipe_thread, NULL);
@@ -155,7 +155,7 @@
         fprintf(stderr, "Cannot run server!\n");
         return EXIT_FAILURE;
     }
-
+     
     /* TODO: join threads (or not?) */
     while(1) {
         if(d) {
--- a/src/server/daemon/protocol.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/daemon/protocol.c	Sat Jan 19 20:13:07 2013 +0100
@@ -296,10 +296,13 @@
     add_http_status_line(out, sn->pool, rq);
 
     /* add server header */
-    sbuf_write(out, "Server: WS uap-dev\r\n", 20);
+    sbuf_write(out, "Server: webserver\r\n", 19);
 
     /* add header from rq->srvhdrs */
     add_http_response_header(out, rq);
+    
+    /* add connection header */
+    sbuf_write(out, "Connection: close\r\n", 19);
 
     /* response header end */
     sbuf_write(out, "\r\n", 2);
--- a/src/server/daemon/sessionhandler.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/daemon/sessionhandler.c	Sat Jan 19 20:13:07 2013 +0100
@@ -195,6 +195,8 @@
         event->finish = evt_request_error;
         return 0;
     }
+    //fwrite(buf->inbuf + buf->pos, 1, r, stdout);
+    //printf("\n");
     
     buf->cursize += r;
     state = http_parser_process(parser);
--- a/src/server/safs/auth.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/safs/auth.c	Sat Jan 19 20:13:07 2013 +0100
@@ -269,6 +269,7 @@
     if(auth_user && !auth_user->verify_password(auth_user, pw)) {
         fprintf(stderr, "authdb user not authenticated: %s\n", user);
         free(user);
+        auth_user->free(auth_user);
         return REQ_NOACTION;
     }
     
@@ -278,5 +279,6 @@
     pblock_nvinsert("auth-db", db, rq->vars);
     
     free(user);
+    auth_user->free(auth_user);
     return REQ_PROCEED;
 }
--- a/src/server/safs/service.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/safs/service.c	Sat Jan 19 20:13:07 2013 +0100
@@ -62,7 +62,7 @@
     /* open the file */
     int fd = open(ppath, O_RDONLY);
     if(fd < 0) {
-        perror("prepare_service_file: open");
+        //perror("prepare_service_file: open");
 
         int status = 500;
         int en = errno;
@@ -83,7 +83,7 @@
     /* get stat */
     struct stat stat;
     if (fstat(fd, &stat) != 0) {
-        perror("prepare_service_file: stat");
+        //perror("prepare_service_file: stat");
 
         protocol_status(sn, rq, 500, NULL);
         return -1;
@@ -143,7 +143,7 @@
     /* open the file */
     int fd = open(ppath, O_RDONLY);
     if(fd < 0) {
-        perror("service_index: open");
+        //perror("service_index: open");
 
         int status = 500;
         switch(errno) {
@@ -157,14 +157,14 @@
             }
         }
         protocol_status(sn, rq, status, NULL);
-        printf("REQ_ABORTED\n");
+        //printf("REQ_ABORTED\n");
         return REQ_ABORTED;
     }
 
     DIR *dir = fdopendir(fd);
     if(dir == NULL) {
         protocol_status(sn, rq, 500, NULL);
-        printf("DIR is null\n");
+        //printf("DIR is null\n");
         return REQ_ABORTED;
     }
 
@@ -206,6 +206,7 @@
 
     /* close */
     closedir(dir);
+    sbuf_free(out);
 
     return REQ_PROCEED;
 }
--- a/src/server/util/util.h	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/util/util.h	Sat Jan 19 20:13:07 2013 +0100
@@ -35,6 +35,7 @@
 #define BASE_UTIL_H
 
 #include "../daemon/netsite.h"
+#include <pwd.h>
 
 #ifndef NOINTNSAPI
 #define INTNSAPI
--- a/src/server/webdav/davparser.cpp	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/webdav/davparser.cpp	Sat Jan 19 20:13:07 2013 +0100
@@ -54,13 +54,16 @@
         XMLPlatformUtils::Initialize();
         xcinit = 1;
     }
-    PropfindRequest *davrq = (PropfindRequest*)pool_malloc(
+    PropfindRequest *davrq = (PropfindRequest*)pool_calloc(
             sn->pool,
+            1,
             sizeof(PropfindRequest));
     davrq->allprop = 0;
     davrq->propname = 0;
     davrq->prop = 0;
     davrq->properties = NULL;
+    davrq->forbiddenProps = NULL;
+    davrq->notFoundProps = NULL;
     // create xml parser
     SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
     parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
@@ -85,12 +88,19 @@
         printf("davaparser Exception\n");
     }
 
-    
+    delete parser;
     
 
     return davrq;
 }
 
+void dav_free_propfind(PropfindRequest *rq) {
+    ucx_dlist_free(rq->forbiddenProps);
+    ucx_dlist_free(rq->notFoundProps);
+    //ucx_dlist_free(rq->properties); // uses pool
+    sbuf_free(rq->out);
+}
+
 ProppatchRequest* dav_parse_proppatch(
         Session *sn,
         Request *rq,
@@ -102,10 +112,11 @@
         XMLPlatformUtils::Initialize();
         xcinit = 1;
     }
-    ProppatchRequest *davrq = (ProppatchRequest*)pool_malloc(
+    ProppatchRequest *davrq = (ProppatchRequest*)pool_calloc(
             sn->pool,
+            1,
             sizeof(PropfindRequest));
-    davrq->nsmap = xmlnsmap_create();
+    davrq->nsmap = xmlnsmap_create(sn->pool);
     
     
     
@@ -133,8 +144,16 @@
         printf("davaparser Exception\n");
     }
 
-    
+    delete parser;
     
 
     return davrq;
 }
+
+void dav_free_proppatch(ProppatchRequest *rq) {
+    ucx_dlist_free(rq->removeProps);
+    ucx_dlist_free(rq->setProps);
+    xmlnsmap_free(rq->nsmap);
+    ucx_map_free(rq->propstat->map);
+    ucx_dlist_free(rq->propstat->okprop);
+}
--- a/src/server/webdav/davparser.h	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/webdav/davparser.h	Sat Jan 19 20:13:07 2013 +0100
@@ -45,12 +45,16 @@
         char *xml,
         size_t len);
 
+void dav_free_propfind(PropfindRequest *rq);
+
 ProppatchRequest* dav_parse_proppatch(
         Session *sn,
         Request *rq,
         char *xml,
         size_t len);
 
+void dav_free_proppatch(ProppatchRequest *rq);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/src/server/webdav/saxhandler.cpp	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/webdav/saxhandler.cpp	Sat Jan 19 20:13:07 2013 +0100
@@ -78,9 +78,10 @@
     if(!strcmp(ns, "DAV:") && !strcmp(name, "prop")) {
         davPropTag = true;
     } else if(davPropTag && property == NULL) {
-        //property = (DavProperty*)pool_malloc(pool, sizeof(DavProperty));
-        property = (DavProperty*)malloc(sizeof(DavProperty));
-        /* Ultra TODO: pool_malloc makes big mistakes!! */
+        property = (DavProperty*)pool_malloc(pool, sizeof(DavProperty));
+        //property = (DavProperty*)malloc(sizeof(DavProperty));
+        /* TODO: pool_malloc makes big mistakes!! */
+        // Fixed or not? It works
 
         size_t nslen = strlen(ns);
         size_t namelen = strlen(name);
@@ -158,7 +159,7 @@
 }
 
 ProppatchHandler::~ProppatchHandler() {
-    
+    ucx_dlist_free(xmlStack);
 }
 
 void ProppatchHandler::startElement(
--- a/src/server/webdav/webdav.c	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/webdav/webdav.c	Sat Jan 19 20:13:07 2013 +0100
@@ -64,6 +64,7 @@
 int webdav_put(pblock *pb, Session *sn, Request *rq) {
     int length = 0;
     
+    char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
     char *ctlen = pblock_findkeyval(pb_key_content_length, rq->headers);
     if(ctlen) {
         length = atoi(ctlen);
@@ -75,10 +76,15 @@
     
     printf("PUT length: %d\n", length);
     
-    int status = 204;
-    if(length >= 0) {
-        char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
-        
+    int status = 201;
+    FILE *out = fopen(ppath, "w");
+    if(out == NULL) {
+        fprintf(stderr, "fopen(%s, \"w\") failed\n", ppath);
+        protocol_status(sn, rq, 500, NULL);
+        return REQ_ABORTED;
+    }
+    
+    if(length > 0) {
         FILE *out = fopen(ppath, "w");
         if(out == NULL) {
             fprintf(stderr, "fopen(%s, \"w\") failed\n", ppath);
@@ -86,13 +92,13 @@
         }
         setvbuf(out, NULL, _IONBF, 0);
         
-        size_t l = (length > 4096) ? (4096) : (length);
-        char *buffer = malloc(l);
+        size_t len = (length > 4096) ? (4096) : (length);
+        char *buffer = malloc(len);
         
         int r;
         int r2 = 0;
         while(r2 < length) {
-            r = netbuf_getbytes(sn->inbuf, buffer, l);
+            r = netbuf_getbytes(sn->inbuf, buffer, len);
             if(r == NETBUF_EOF) {
                 break;
             }
@@ -101,8 +107,11 @@
             r2 += r;
         }
         
-        fclose(out);
+        free(buffer);
+    } else {
+        
     }
+    fclose(out);
     
     protocol_status(sn, rq, status, NULL);
     pblock_removekey(pb_key_content_type, rq->srvhdrs);
@@ -232,7 +241,7 @@
         protocol_status(sn, rq, 404, NULL);
         pblock_removekey(pb_key_content_type, rq->srvhdrs);
         pblock_nninsert("content-length", 0, rq->srvhdrs);
-        http_start_response(sn, rq);
+        //http_start_response(sn, rq);
         
         return REQ_ABORTED;
     } 
@@ -306,13 +315,13 @@
     pblock_nvinsert("content-type", "text/xml", rq->srvhdrs);
     pblock_nninsert("content-length", davrq->out->length, rq->srvhdrs);
     
-    pblock_nvinsert("connection", "close", rq->srvhdrs);
+    //pblock_nvinsert("connection", "close", rq->srvhdrs);
     http_start_response(sn, rq);
     
     net_write(sn->csd, davrq->out->ptr, davrq->out->length);
     
     
-
+    dav_free_propfind(davrq);
     
     return REQ_PROCEED;
 }
@@ -408,11 +417,13 @@
     pblock_nvinsert("content-type", "text/xml", rq->srvhdrs);
     pblock_nninsert("content-length", davrq->out->length, rq->srvhdrs);
     
-    pblock_nvinsert("connection", "close", rq->srvhdrs);
+    //pblock_nvinsert("connection", "close", rq->srvhdrs);
     http_start_response(sn, rq);
     
     net_write(sn->csd, davrq->out->ptr, davrq->out->length);
     
+    dav_free_proppatch(davrq);
+    
     return REQ_PROCEED;
 }
 
@@ -485,6 +496,7 @@
         DavProperty *prop,
         int error)
 {
+    // TODO: different errors
     davrq->notFoundProps = ucx_dlist_append(davrq->notFoundProps, prop);
 }
 
@@ -492,14 +504,13 @@
 
 
 /* WebDAV Default Backend */
+static DAVPropertyBackend dav_file_backend = {
+    dav_rq_propfind,
+    dav_rq_proppatch
+};
+
 DAVPropertyBackend* create_property_backend() {
-    DAVPropertyBackend *pb = malloc(sizeof(DAVPropertyBackend));
-    if(pb == NULL) {
-        //
-    }
-    pb->propfind  = dav_rq_propfind;
-    pb->proppatch = dav_rq_proppatch;
-    return pb;
+    return &dav_file_backend;
 }
 
 void dav_rq_propfind(DAVPropertyBackend *b, PropfindRequest *rq ,char *path) {
@@ -553,18 +564,19 @@
 
 /* XmlNsMap */
 
-XmlNsMap* xmlnsmap_create() {
+XmlNsMap* xmlnsmap_create(pool_handle_t *pool) {
     XmlNsMap *map = malloc(sizeof(XmlNsMap));
     UcxMap *uxm = ucx_map_new(16);
     if(map == NULL || uxm == NULL) {
         return NULL;
     }
     map->map = uxm;
+    map->pool = pool;
     return map;
 }
 
 void xmlnsmap_free(XmlNsMap *map) {
-    /* TODO: implement */
+    ucx_map_free(map->map);
 }
 
 XmlNs* xmlnsmap_put(XmlNsMap *map, char *ns) {
@@ -573,7 +585,7 @@
         return xmlns;
     }
     
-    xmlns = malloc(sizeof(XmlNs));
+    xmlns = pool_malloc(map->pool, sizeof(XmlNs));
     if(xmlns == NULL) {
         return NULL;
     }
--- a/src/server/webdav/webdav.h	Sun Jan 13 14:16:45 2013 +0100
+++ b/src/server/webdav/webdav.h	Sat Jan 19 20:13:07 2013 +0100
@@ -132,6 +132,7 @@
 
 struct XmlNsMap {
     UcxMap *map;
+    pool_handle_t *pool;
 };
 
 /*
@@ -220,7 +221,7 @@
  * value:  XmlNs object, containing namespace uri and the prefix
  */
 
-XmlNsMap* xmlnsmap_create();
+XmlNsMap* xmlnsmap_create(pool_handle_t *pool);
 
 void xmlnsmap_free(XmlNsMap *map);
 

mercurial