ctl: close opened sockets in case of errors
authorDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 27 Jul 2017 07:15:00 +0000 (09:15 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 27 Jul 2017 07:15:00 +0000 (09:15 +0200)
src/modules/ctl/binrpc.h
src/modules/ctl/binrpc_run.c
src/modules/ctl/ctl.c
src/modules/ctl/ctrl_socks.c
src/modules/ctl/fifo_server.c
src/modules/ctl/init_socks.c
src/modules/ctl/io_listener.c

index 2e33a39..56a8e26 100644 (file)
@@ -705,7 +705,7 @@ inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx,
                                        ctx->in_struct=0; /* hack to parse a normal record */
                                        v->type=type; /* hack */
                                        p=binrpc_read_record(ctx, p, end, v, smode, err);
-                                       if (err<0){
+                                       if (*err<0){
                                                ctx->in_struct=tmp;
                                                goto error;
                                        }else{
@@ -735,6 +735,7 @@ inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx,
                        v->u.strval.s=(char*)p;
                        v->u.strval.len=len;
                        p+=len;
+                       break;
                case BINRPC_T_ARRAY:
                        if (ctx->in_struct && smode==0) goto error_record;
                        if (end_tag){
index 0a27278..74d71dc 100644 (file)
@@ -881,7 +881,7 @@ static int rpc_scan(struct binrpc_ctx* ctx, char* fmt, ...)
                                v.type=autoconv?BINRPC_T_ALL:BINRPC_T_STR;
                                ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, 
                                                                                                ctx->in.end, &v, 0, &err);
-                               if (err<0 || ((s=binrpc_val_conv_str(ctx, &v, &err))==0 &&
+                               if (err<0 || ((s=binrpc_val_conv_str(ctx, &v, &err))==0 ||
                                                        err<0)){
                                        v.u.strval.s="if you get this string, you don't"
                                                                "check rpc_scan return code !!! (very bad)";
index f626a82..2126a00 100644 (file)
@@ -344,7 +344,7 @@ static int mod_child(int rank)
        if (rank!=PROC_RPC || !rpc_handler){
                /* close all the opened fds, we don't need them here */
                for (cs=ctrl_sock_lst; cs; cs=cs->next){
-                       close(cs->fd);
+                       if(cs->fd>=0) close(cs->fd);
                        cs->fd=-1;
                        if (cs->write_fd!=-1){
                                close(cs->write_fd);
@@ -396,7 +396,7 @@ static void mod_destroy(void)
                                break;
 #endif
                        default:
-                               close(cs->fd);
+                               if(cs->fd>=0) close(cs->fd);
                                cs->fd=-1;
                                if (cs->write_fd!=-1){
                                        close(cs->write_fd);
index 7d4010e..4449ff7 100644 (file)
@@ -226,7 +226,7 @@ int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst,
                                                int def_port, int perm, int uid, int gid)
 {
        struct id_list* l;
-       int s;
+       int s = -1;
        struct ctrl_socket* cs;
        int extra_fd;
        union sockaddr_u su;
@@ -271,6 +271,7 @@ int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst,
                cs->transport=l->proto;
                cs->p_proto=l->data_proto;
                cs->fd=s;
+               s = -1;
                cs->write_fd=extra_fd; /* needed for fifo write */
                cs->name=l->name;
                cs->port=l->port;
@@ -281,6 +282,8 @@ int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst,
        }
        return 0;
 error:
+       if(s>=0) close(s);
+       if(extra_fd>=0) close(extra_fd);
        return -1;
 }
 
index 76d8287..a84073a 100644 (file)
@@ -811,7 +811,7 @@ int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid,
        struct stat filestat;
        int n;
        long opt;
-       int fifo_read;
+       int fifo_read = -1;
        
        if (fifo == NULL) {
                ERR("null fifo: no fifo will be opened\n");
@@ -823,7 +823,6 @@ int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid,
                return -1;
        }
        
-       
        DBG("Opening fifo...\n");
        n = stat(fifo, &filestat);
        if (n == 0) {
@@ -862,26 +861,29 @@ int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid,
        
        fifo_read = open(fifo, O_RDONLY | O_NONBLOCK, 0);
        if (fifo_read < 0) {
-               ERR("fifo_read did not open: %s\n",
-                   strerror(errno));
+               ERR("fifo_read did not open: %s\n", strerror(errno));
                return -1;
        }
        /* make sure the read fifo will not close */
        *fifo_write = open(fifo, O_WRONLY | O_NONBLOCK, 0);
        if (*fifo_write < 0) {
-               ERR("fifo_write did not open: %s\n",
-                   strerror(errno));
+               ERR("fifo_write did not open: %s\n", strerror(errno));
+               close(fifo_read);
                return -1;
        }
        /* set read fifo blocking mode */
        if ((opt = fcntl(fifo_read, F_GETFL)) == -1) {
-               ERR("fcntl(F_GETFL) failed: %s [%d]\n",
-                   strerror(errno), errno);
+               ERR("fcntl(F_GETFL) failed: %s [%d]\n", strerror(errno), errno);
+               close(fifo_read);
+               close(*fifo_write);
+               *fifo_write = -1;
                return -1;
        }
        if (fcntl(fifo_read, F_SETFL, opt & (~O_NONBLOCK)) == -1) {
-               ERR("fcntl(F_SETFL) failed: %s [%d]\n",
-                   strerror(errno), errno);
+               ERR("fcntl(F_SETFL) failed: %s [%d]\n", strerror(errno), errno);
+               close(fifo_read);
+               close(*fifo_write);
+               *fifo_write = -1;
                return -1;
        }
        return fifo_read;
index 1ec555a..269bfd8 100644 (file)
@@ -125,7 +125,7 @@ int init_unix_sock(struct sockaddr_un* su, char* name, int type, int perm,
                }
        }
        /* try to change ownership */
-       if ((uid!=-1) || (gid!=-1)){
+       if ((uid!=-1) && (gid!=-1)){
                if (chown(name, uid, gid)<0){
                        LOG(L_ERR, "ERROR: init_unix_sock: failed to change the"
                                        " owner/group for %s to %d.%d: %s[%d]\n",
index 399f1fd..8da2ba2 100644 (file)
@@ -181,7 +181,7 @@ void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst)
        int type;
        
        clist_init(&stream_conn_lst, next, prev);
-       type=UNKNOWN_SOCK;
+       type=F_T_RESERVED;
 #if 0
        /* estimate used fd numbers -- FIXME: broken, make it a function in pt.h */
        max_fd_no=get_max_procs()*3 -1 /* timer */ +3; /* stdin/out/err*/;