Browse code

ctl: close opened sockets in case of errors

Daniel-Constantin Mierla authored on 27/07/2017 07:15:00
Showing 7 changed files
... ...
@@ -705,7 +705,7 @@ inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx,
705 705
 					ctx->in_struct=0; /* hack to parse a normal record */
706 706
 					v->type=type; /* hack */
707 707
 					p=binrpc_read_record(ctx, p, end, v, smode, err);
708
-					if (err<0){
708
+					if (*err<0){
709 709
 						ctx->in_struct=tmp;
710 710
 						goto error;
711 711
 					}else{
... ...
@@ -735,6 +735,7 @@ inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx,
735 735
 			v->u.strval.s=(char*)p;
736 736
 			v->u.strval.len=len;
737 737
 			p+=len;
738
+			break;
738 739
 		case BINRPC_T_ARRAY:
739 740
 			if (ctx->in_struct && smode==0) goto error_record;
740 741
 			if (end_tag){
... ...
@@ -881,7 +881,7 @@ static int rpc_scan(struct binrpc_ctx* ctx, char* fmt, ...)
881 881
 				v.type=autoconv?BINRPC_T_ALL:BINRPC_T_STR;
882 882
 				ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, 
883 883
 												ctx->in.end, &v, 0, &err);
884
-				if (err<0 || ((s=binrpc_val_conv_str(ctx, &v, &err))==0 &&
884
+				if (err<0 || ((s=binrpc_val_conv_str(ctx, &v, &err))==0 ||
885 885
 							err<0)){
886 886
 					v.u.strval.s="if you get this string, you don't"
887 887
 								"check rpc_scan return code !!! (very bad)";
... ...
@@ -344,7 +344,7 @@ static int mod_child(int rank)
344 344
 	if (rank!=PROC_RPC || !rpc_handler){
345 345
 		/* close all the opened fds, we don't need them here */
346 346
 		for (cs=ctrl_sock_lst; cs; cs=cs->next){
347
-			close(cs->fd);
347
+			if(cs->fd>=0) close(cs->fd);
348 348
 			cs->fd=-1;
349 349
 			if (cs->write_fd!=-1){
350 350
 				close(cs->write_fd);
... ...
@@ -396,7 +396,7 @@ static void mod_destroy(void)
396 396
 				break;
397 397
 #endif
398 398
 			default:
399
-				close(cs->fd);
399
+				if(cs->fd>=0) close(cs->fd);
400 400
 				cs->fd=-1;
401 401
 				if (cs->write_fd!=-1){
402 402
 					close(cs->write_fd);
... ...
@@ -226,7 +226,7 @@ int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst,
226 226
 						int def_port, int perm, int uid, int gid)
227 227
 {
228 228
 	struct id_list* l;
229
-	int s;
229
+	int s = -1;
230 230
 	struct ctrl_socket* cs;
231 231
 	int extra_fd;
232 232
 	union sockaddr_u su;
... ...
@@ -271,6 +271,7 @@ int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst,
271 271
 		cs->transport=l->proto;
272 272
 		cs->p_proto=l->data_proto;
273 273
 		cs->fd=s;
274
+		s = -1;
274 275
 		cs->write_fd=extra_fd; /* needed for fifo write */
275 276
 		cs->name=l->name;
276 277
 		cs->port=l->port;
... ...
@@ -281,6 +282,8 @@ int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst,
281 282
 	}
282 283
 	return 0;
283 284
 error:
285
+	if(s>=0) close(s);
286
+	if(extra_fd>=0) close(extra_fd);
284 287
 	return -1;
285 288
 }
286 289
 
... ...
@@ -811,7 +811,7 @@ int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid,
811 811
 	struct stat filestat;
812 812
 	int n;
813 813
 	long opt;
814
-	int fifo_read;
814
+	int fifo_read = -1;
815 815
 	
816 816
 	if (fifo == NULL) {
817 817
 		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,
823 823
 		return -1;
824 824
 	}
825 825
 	
826
-	
827 826
 	DBG("Opening fifo...\n");
828 827
 	n = stat(fifo, &filestat);
829 828
 	if (n == 0) {
... ...
@@ -862,26 +861,29 @@ int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid,
862 861
 	
863 862
 	fifo_read = open(fifo, O_RDONLY | O_NONBLOCK, 0);
864 863
 	if (fifo_read < 0) {
865
-		ERR("fifo_read did not open: %s\n",
866
-		    strerror(errno));
864
+		ERR("fifo_read did not open: %s\n", strerror(errno));
867 865
 		return -1;
868 866
 	}
869 867
 	/* make sure the read fifo will not close */
870 868
 	*fifo_write = open(fifo, O_WRONLY | O_NONBLOCK, 0);
871 869
 	if (*fifo_write < 0) {
872
-		ERR("fifo_write did not open: %s\n",
873
-		    strerror(errno));
870
+		ERR("fifo_write did not open: %s\n", strerror(errno));
871
+		close(fifo_read);
874 872
 		return -1;
875 873
 	}
876 874
 	/* set read fifo blocking mode */
877 875
 	if ((opt = fcntl(fifo_read, F_GETFL)) == -1) {
878
-		ERR("fcntl(F_GETFL) failed: %s [%d]\n",
879
-		    strerror(errno), errno);
876
+		ERR("fcntl(F_GETFL) failed: %s [%d]\n", strerror(errno), errno);
877
+		close(fifo_read);
878
+		close(*fifo_write);
879
+		*fifo_write = -1;
880 880
 		return -1;
881 881
 	}
882 882
 	if (fcntl(fifo_read, F_SETFL, opt & (~O_NONBLOCK)) == -1) {
883
-		ERR("fcntl(F_SETFL) failed: %s [%d]\n",
884
-		    strerror(errno), errno);
883
+		ERR("fcntl(F_SETFL) failed: %s [%d]\n", strerror(errno), errno);
884
+		close(fifo_read);
885
+		close(*fifo_write);
886
+		*fifo_write = -1;
885 887
 		return -1;
886 888
 	}
887 889
 	return fifo_read;
... ...
@@ -125,7 +125,7 @@ int init_unix_sock(struct sockaddr_un* su, char* name, int type, int perm,
125 125
 		}
126 126
 	}
127 127
 	/* try to change ownership */
128
-	if ((uid!=-1) || (gid!=-1)){
128
+	if ((uid!=-1) && (gid!=-1)){
129 129
 		if (chown(name, uid, gid)<0){
130 130
 			LOG(L_ERR, "ERROR: init_unix_sock: failed to change the"
131 131
 					" owner/group for %s to %d.%d: %s[%d]\n",
... ...
@@ -181,7 +181,7 @@ void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst)
181 181
 	int type;
182 182
 	
183 183
 	clist_init(&stream_conn_lst, next, prev);
184
-	type=UNKNOWN_SOCK;
184
+	type=F_T_RESERVED;
185 185
 #if 0
186 186
 	/* estimate used fd numbers -- FIXME: broken, make it a function in pt.h */
187 187
 	max_fd_no=get_max_procs()*3 -1 /* timer */ +3; /* stdin/out/err*/;