fixes cgi fd leak

Sat, 31 Dec 2016 18:05:40 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2016 18:05:40 +0100
changeset 144
4b546c4f25ed
parent 143
6bf5d2f37425
child 145
1c93281ca4bf

fixes cgi fd leak

src/server/safs/cgi.c file | annotate | diff | comparison | revisions
src/server/safs/cgi.h file | annotate | diff | comparison | revisions
--- a/src/server/safs/cgi.c	Fri Dec 30 19:43:41 2016 +0100
+++ b/src/server/safs/cgi.c	Sat Dec 31 18:05:40 2016 +0100
@@ -97,6 +97,7 @@
             r = netbuf_getbytes(sn->inbuf, buf, 4096);
             if(r <= 0) {
                 // TODO: handleerror
+                cgi_close(&cgip);
                 return REQ_ABORTED; 
             }
             write(cgip.in[1], buf, r);
@@ -104,6 +105,7 @@
         }
     }
     close(cgip.in[1]);
+    cgip.in[1] = -1;
     
     // read from child
     CGIResponseParser *parser = cgi_parser_new(sn, rq);
@@ -142,6 +144,8 @@
             }
         }
     }
+    
+    cgi_close(&cgip);
       
     cgi_parser_free(parser);
     return result;
@@ -196,11 +200,27 @@
     } else {
         // parent  
         close(p->out[1]);
+        p->out[1] = -1;
     }
     
     return REQ_PROCEED;
 }
 
+void cgi_close(CGIProcess *p) {
+    if(p->in[0] != -1) {
+        close(p->in[0]);
+    }
+    if(p->in[1] != -1) {
+        close(p->in[1]);
+    }
+    if(p->out[0] != -1) {
+        close(p->out[0]);
+    }
+    if(p->out[1] != -1) {
+        close(p->out[1]);
+    }
+}
+
 CGIResponseParser* cgi_parser_new(Session *sn, Request *rq) {
     CGIResponseParser* parser = pool_malloc(sn->pool, sizeof(CGIResponseParser));
     parser->sn = sn;
--- a/src/server/safs/cgi.h	Fri Dec 30 19:43:41 2016 +0100
+++ b/src/server/safs/cgi.h	Sat Dec 31 18:05:40 2016 +0100
@@ -56,6 +56,8 @@
 
 int cgi_start(CGIProcess *p, char *path, char *const argv[], char *const envp[]);
 
+void cgi_close(CGIProcess *p);
+
 CGIResponseParser* cgi_parser_new(Session *sn, Request *rq);
 void cgi_parser_free(CGIResponseParser *parser);
 int cgi_parse_response(CGIResponseParser *parser, char *buf, size_t len, size_t *bpos);

mercurial