From 2b261897fa07006e8a46003f8448b69691555314 Mon Sep 17 00:00:00 2001 From: Varsha Rao Date: Wed, 19 Jul 2017 18:04:07 +0530 Subject: src: netlink: Remove variable nf_mon_sock. Remove variable nf_mon_sock of type structure mnl_socket to avoid duplicity. Instead variable nf_sock of the same type is passed as argument to netlink_monitor(). Also remove netlink_open_mon_sock() function definition, which is no longer required. Suggested-by: Pablo Neira Ayuso Signed-off-by: Varsha Rao Signed-off-by: Pablo Neira Ayuso --- src/mnl.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'src/mnl.c') diff --git a/src/mnl.c b/src/mnl.c index cf060a40..76393123 100644 --- a/src/mnl.c +++ b/src/mnl.c @@ -1114,30 +1114,41 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock, * message loss due to ENOBUFS. */ unsigned int bufsiz = NFTABLES_NLEVENT_BUFSIZ; + int fd = mnl_socket_get_fd(nf_sock); char buf[NFT_NLMSG_MAXSIZE]; + fd_set readfds; int ret; - ret = setsockopt(mnl_socket_get_fd(nf_sock), SOL_SOCKET, SO_RCVBUFFORCE, - &bufsiz, sizeof(socklen_t)); - if (ret < 0) { + ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &bufsiz, + sizeof(socklen_t)); + if (ret < 0) { /* If this doesn't work, try to reach the system wide maximum * (or whatever the user requested). */ - ret = setsockopt(mnl_socket_get_fd(nf_sock), SOL_SOCKET, - SO_RCVBUF, &bufsiz, sizeof(socklen_t)); + ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsiz, + sizeof(socklen_t)); printf("# Cannot set up netlink socket buffer size to %u bytes, falling back to %u bytes\n", NFTABLES_NLEVENT_BUFSIZ, bufsiz); } while (1) { - ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf)); - if (ret < 0) { - if (errno == ENOBUFS) { - printf("# ERROR: We lost some netlink events!\n"); - continue; + FD_ZERO(&readfds); + FD_SET(fd, &readfds); + + ret = select(fd + 1, &readfds, NULL, NULL, NULL); + if (ret < 0) + return -1; + + if (FD_ISSET(fd, &readfds)) { + ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf)); + if (ret < 0) { + if (errno == ENOBUFS) { + printf("# ERROR: We lost some netlink events!\n"); + continue; + } + fprintf(stdout, "# ERROR: %s\n", strerror(errno)); + break; } - fprintf(stdout, "# ERROR: %s\n", strerror(errno)); - break; } #ifdef DEBUG -- cgit v1.2.3