From 6fbf3d49848bd17dd61147bebbd59b0bd2229ee6 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Wed, 12 Sep 2018 11:06:19 +0200 Subject: [PATCH] fix zoombie process & rename plugin handle method --- Makefile | 2 +- http_server.c | 5 +- http_server.h | 2 +- httpd.c | 2 + libs/handle.c | 1 + libs/handle.h | 1 + libs/plugin.h | 2 +- libs/pluginsman/pluginsman.c | 91 ------------------------------------ libs/wsimg/wsimg.c | 59 ----------------------- libs/wterm/wterm.c | 19 ++++---- 10 files changed, 21 insertions(+), 163 deletions(-) delete mode 100644 libs/pluginsman/pluginsman.c delete mode 100644 libs/wsimg/wsimg.c diff --git a/Makefile b/Makefile index 77885d3..3c271ef 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ SERVERLIB= -ldl $(LIB_FLAG) $(DB_LIB) $(SSL_LIB) -lpthread SERVER_O=plugin_manager.o \ http_server.o #-lsocket -PLUGINS= pluginsman.$(EXT) wterm.$(EXT) nodedaemon.$(EXT) wsimg.$(EXT) +PLUGINS= wterm.$(EXT) nodedaemon.$(EXT) LIBOBJS = libs/ini.o \ libs/handle.o \ diff --git a/http_server.c b/http_server.c index e66e304..ea34a3d 100644 --- a/http_server.c +++ b/http_server.c @@ -504,11 +504,12 @@ dictionary decode_request(void* client,const char* method, char* url) int clen = -1; // first real all header -// this for check if web socket is enabled + // this for check if web socket is enabled int ws= 0; char* ws_key = NULL; char buf[BUFFLEN]; - + // ip address + dput(xheader,"REMOTE_ADDR", (void*)strdup(((antd_client_t*)client)->ip )); //while((line = read_line(client)) && strcmp("\r\n",line)) while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf)) { diff --git a/http_server.h b/http_server.h index 07f2292..3636fd9 100644 --- a/http_server.h +++ b/http_server.h @@ -12,7 +12,7 @@ #define FORM_URL_ENCODE "application/x-www-form-urlencoded" #define FORM_MULTI_PART "multipart/form-data" -#define PLUGIN_HANDLER "handler" +#define PLUGIN_HANDLER "handle" #define WS_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" diff --git a/httpd.c b/httpd.c index 0085631..7a5bc15 100644 --- a/httpd.c +++ b/httpd.c @@ -234,9 +234,11 @@ int main(int argc, char* argv[]) /* get the remote IP */ + client->ip = NULL; if (client_name.sin_family == AF_INET) { client_ip = inet_ntoa(client_name.sin_addr); + client->ip = strdup(client_ip); LOG("Client IP: %s\n", client_ip); } //return &(((struct sockaddr_in6*)sa)->sin6_addr); diff --git a/libs/handle.c b/libs/handle.c index a5d1657..d0c26a3 100644 --- a/libs/handle.c +++ b/libs/handle.c @@ -133,6 +133,7 @@ int antd_close(void* src) #endif //printf("Close sock %d\n", source->sock); int ret = close(source->sock); + if(source->ip) free(source->ip); server_config.connection--; LOG("Remaining connection %d\n", server_config.connection); free(src); diff --git a/libs/handle.h b/libs/handle.h index f231ba5..beea497 100644 --- a/libs/handle.h +++ b/libs/handle.h @@ -51,6 +51,7 @@ typedef struct { typedef struct{ int sock; void* ssl; + char* ip; } antd_client_t; int response(void*, const char*); diff --git a/libs/plugin.h b/libs/plugin.h index 969eb23..a51dbf0 100644 --- a/libs/plugin.h +++ b/libs/plugin.h @@ -36,6 +36,6 @@ char* route(const char*); char* htdocs(const char*); char* config_dir(); /*Default function for plugin*/ -void handler(void*, const char*,const char*,dictionary); +void handle(void*, const char*,const char*,dictionary); void __release(); #endif diff --git a/libs/pluginsman/pluginsman.c b/libs/pluginsman/pluginsman.c deleted file mode 100644 index e380167..0000000 --- a/libs/pluginsman/pluginsman.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "../plugin.h" -#define PEXT "dylib" -#define MAXSIZE 500000 - -void execute(void* client,const char* method,dictionary rq) -{ - //all plugin file - DIR *d; - struct dirent *dir; - struct stat st; - d = opendir(__plugin__.pdir); - json(client); - __t(client,"{ \"records\":["); - int nrec = 0; - if (d) - { - while ((dir = readdir(d)) != NULL) - { - if(strcmp(dir->d_name,".") == 0 || - strcmp(dir->d_name,"..")==0 || *(dir->d_name)=='.') continue; - if( stat(__s("%s%s",__plugin__.pdir,dir->d_name), &st) == 0 ) - { - if(nrec != 0) - __t(client,","); - __t(client,"{\"name\":\"%s\",\"size\":%d,\"changed\":\"%s\"}", - dir->d_name, - (int)st.st_size, - __time(st.st_mtime) - ); - nrec++; - } - } - closedir(d); - } - __t(client,"], \"total\":%d}",nrec); - -} - -void install(void* c, const char* m, dictionary rq) -{ - char * result = "{\"result\":%d,\"msg\":\"%s\"}"; - json(c); - if(IS_GET(m)) - { - __t(c,result,0,"Bad request:GET"); - return; - } - if(R_INT(rq,"test") != NULL) - LOG("Test is :%d \n",R_INT(rq,"test")); - char * file_name = R_STR(rq,"pfile.file"); - char* file_ext = R_STR(rq,"pfile.ext"); - if(file_name == NULL) - { - __t(c,result,0,"Cannot send file to server"); - return; - } - - if(strcasecmp(file_ext,PEXT) == 0) - { - int size = R_INT(rq,"pfile.size"); - if(size>MAXSIZE) - { - __t(c,result,0,"Cannot accept file more than 500Kb"); - return; - } - if(!upload(R_STR(rq,"pfile.tmp"),__s("%s/%s",__plugin__.pdir,file_name))) - { - __t(c,result,0,"Cannot move file to plugin dir"); - return; - } - __t(c,result,1,"OK"); - return; - } - - __t(c,result,0,"This is not a plugin file"); -} -void handler(void* client, const char* method, const char* rqpth, dictionary rq) -{ - if(EQU(rqpth,"default")) - { - execute(client,method,rq); - } - else if(EQU(rqpth,"install")) - { - install(client,method,rq); - } - else - { - unknow(client); - } -} \ No newline at end of file diff --git a/libs/wsimg/wsimg.c b/libs/wsimg/wsimg.c deleted file mode 100644 index 1c04161..0000000 --- a/libs/wsimg/wsimg.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include "../plugin.h" - -void pexit() -{ - -} -void handler(void* cl, const char* m, const char* rqp, dictionary rq) -{ - char* path = NULL; - int nimg = 19; - ws_msg_header_t* h = NULL; - char buff[1024]; - if(ws_enable(rq)) - { - while(1) - { - h = ws_read_header(cl); - if(h) - { - if(h->mask == 0) - { - LOG("%s\n","data is not masked"); - ws_close(cl, 1012); - break; - } - if(h->opcode == WS_CLOSE) - { - LOG("%s\n","Websocket: connection closed"); - ws_close(cl, 1011); - break; - } - else if(h->opcode == WS_TEXT) - { - int l; - if((l = ws_read_data(cl,h,sizeof(buff),buff)) > 0) - { - - //path = __s("%s/ws/img%d.jpg",__plugin__.htdocs,buff[0]); - LOG("%s : %s\n", "send back data of", buff); - //ws_f(cl,path); - ws_t(cl,buff); - free(path); - } - else - { - LOG("%s\n","Invalid request"); - ws_close(cl, 1011); - break; - } - } - - free(h); - } - } - } - LOG("%s\n", "EXIT Streaming.."); -} \ No newline at end of file diff --git a/libs/wterm/wterm.c b/libs/wterm/wterm.c index afdf9a4..c20e826 100644 --- a/libs/wterm/wterm.c +++ b/libs/wterm/wterm.c @@ -13,16 +13,16 @@ void pexit() { } -void handler(void* cl, const char* m, const char* rqp, dictionary rq) +void handle(void* cl, const char* m, const char* rqp, dictionary rq) { ws_msg_header_t* h = NULL; if(ws_enable(rq)) { int fdm, fds; - int rc; + int rc, status; char buff[1024]; - + pid_t pid; // Check arguments fdm = posix_openpt(O_RDWR); if (fdm < 0) @@ -52,7 +52,8 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq) fds = open(ptsname(fdm), O_RDWR); // Create the child process - if (fork()) + pid = fork(); + if (pid) { fd_set fd_in; @@ -75,7 +76,7 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq) case -1 : LOG("Error %d on select()\n", errno); ws_close(cl, 1011); - return; + goto wait_for_child; default : { @@ -90,14 +91,14 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq) LOG("%s\n","Data is not mask"); write(fdm, "exit\n", 5); free(h); - return; + goto wait_for_child;; } if(h->opcode == WS_CLOSE) { LOG("%s\n","Websocket: connection closed"); write(fdm, "exit\n", 5); free(h); - return; + goto wait_for_child;; } else if(h->opcode == WS_TEXT) { @@ -183,7 +184,7 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq) LOG("Error %d on read standard input. Exit now\n", errno); write(fdm, "exit\n", 5); ws_close(cl,1011); - return; + goto wait_for_child; } } } @@ -191,6 +192,8 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq) } } // End switch } // End while + wait_for_child: + waitpid(pid, &status, 0); } else {