瀏覽代碼

core: socket info - set receive timeout for netlink socket

- GH #4098

(cherry picked from commit 6750e2e2f1d6c10f6b5e66897d38657a576a9732)
Daniel-Constantin Mierla 7 月之前
父節點
當前提交
9b26093fa5
共有 1 個文件被更改,包括 18 次插入0 次删除
  1. 18 0
      src/core/socket_info.c

+ 18 - 0
src/core/socket_info.c

@@ -1332,6 +1332,7 @@ static int nl_bound_sock(void)
 {
 	int sock = -1;
 	struct sockaddr_nl la;
+	struct timeval recvtimeout;
 
 	sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
 	if(sock < 0) {
@@ -1350,6 +1351,15 @@ static int nl_bound_sock(void)
 		goto error;
 	}
 
+	recvtimeout.tv_sec = 4;
+	recvtimeout.tv_usec = 0;
+	if(setsockopt(
+			   sock, SOL_SOCKET, SO_RCVTIMEO, &recvtimeout, sizeof(recvtimeout))
+			< 0) {
+		LM_ERR("failed to set receive timeout\n");
+		goto error;
+	}
+
 	return sock;
 error:
 	if(sock >= 0)
@@ -1402,6 +1412,10 @@ static int get_flags(int family)
 			goto error;
 		}
 		rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
+		if(rtn <= 0) {
+			LM_ERR("failed to receive data (%d/%d)\n", rtn, errno);
+			goto error;
+		}
 		nlp = (struct nlmsghdr *)p;
 		if(nlp->nlmsg_type == NLMSG_DONE) {
 			LM_DBG("done\n");
@@ -1504,6 +1518,10 @@ static int build_iface_list(void)
 				goto error;
 			}
 			rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
+			if(rtn <= 0) {
+				LM_ERR("failed to receive data (%d/%d)\n", rtn, errno);
+				goto error;
+			}
 			LM_DBG("received %d byles \n", rtn);
 			nlp = (struct nlmsghdr *)p;
 			if(nlp->nlmsg_type == NLMSG_DONE) {