Commit c18e3b0a authored by Matthieu Boutier's avatar Matthieu Boutier Committed by Juliusz Chroboczek

Simplify netlink_read.

parent 72a62643
...@@ -261,7 +261,7 @@ netlink_socket(struct netlink *nl, uint32_t groups) ...@@ -261,7 +261,7 @@ netlink_socket(struct netlink *nl, uint32_t groups)
static int static int
netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
int (*fn)(struct nlmsghdr *nh, void *data), void *data) struct kernel_filter *filter)
{ {
/* 'answer' must be true when we just have send a request on 'nl_socket' */ /* 'answer' must be true when we just have send a request on 'nl_socket' */
...@@ -271,9 +271,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, ...@@ -271,9 +271,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
/* Return code : */ /* Return code : */
/* -1 : error */ /* -1 : error */
/* 0 : if(fn) found_interesting; else found_ack; */ /* 0 : success */
/* 1 : only if(fn) nothing interesting has been found */
/* 2 : nothing found, retry */
int err; int err;
struct msghdr msg; struct msghdr msg;
...@@ -281,8 +279,8 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, ...@@ -281,8 +279,8 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
struct iovec iov; struct iovec iov;
struct nlmsghdr *nh; struct nlmsghdr *nh;
int len; int len;
int interesting = 0;
int done = 0; int done = 0;
int skip = 0;
char buf[8192]; char buf[8192];
...@@ -314,7 +312,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, ...@@ -314,7 +312,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
if(len < 0) { if(len < 0) {
perror("netlink_read: recvmsg()"); perror("netlink_read: recvmsg()");
return 2; return -1;
} else if(len == 0) { } else if(len == 0) {
fprintf(stderr, "netlink_read: EOF\n"); fprintf(stderr, "netlink_read: EOF\n");
goto socket_error; goto socket_error;
...@@ -325,7 +323,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, ...@@ -325,7 +323,7 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
goto socket_error; goto socket_error;
} else if(nladdr.nl_pid != 0) { } else if(nladdr.nl_pid != 0) {
kdebugf("netlink_read: message not sent by kernel.\n"); kdebugf("netlink_read: message not sent by kernel.\n");
return 2; return -1;
} }
kdebugf("Netlink message: "); kdebugf("Netlink message: ");
...@@ -359,12 +357,13 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, ...@@ -359,12 +357,13 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
errno = -err->error; errno = -err->error;
return -1; return -1;
} }
} else if(fn) { } else if(skip) {
kdebugf("(skip)");
} if(filter) {
kdebugf("(msg -> \""); kdebugf("(msg -> \"");
err = fn(nh,data); err = filter_netlink(nh, filter);
kdebugf("\" %d), ", err); kdebugf("\" %d), ", err);
if(err < 0) return err; if(err < 0) skip = 1;
interesting = interesting || err;
continue; continue;
} }
kdebugf(", "); kdebugf(", ");
...@@ -376,14 +375,14 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer, ...@@ -376,14 +375,14 @@ netlink_read(struct netlink *nl, struct netlink *nl_ignore, int answer,
} while(!done); } while(!done);
return interesting; return 0;
socket_error: socket_error:
close(nl->sock); close(nl->sock);
nl->sock = -1; nl->sock = -1;
errno = EIO; errno = EIO;
return -1; return -1;
} }
static int static int
netlink_talk(struct nlmsghdr *nh) netlink_talk(struct nlmsghdr *nh)
...@@ -428,7 +427,7 @@ netlink_talk(struct nlmsghdr *nh) ...@@ -428,7 +427,7 @@ netlink_talk(struct nlmsghdr *nh)
return -1; return -1;
} }
rc = netlink_read(&nl_command, NULL, 1, NULL, NULL); /* ACK */ rc = netlink_read(&nl_command, NULL, 1, NULL); /* ACK */
return rc; return rc;
} }
...@@ -1224,8 +1223,7 @@ kernel_dump(int operation, struct kernel_filter *filter) ...@@ -1224,8 +1223,7 @@ kernel_dump(int operation, struct kernel_filter *filter)
if(rc < 0) if(rc < 0)
return -1; return -1;
rc = netlink_read(&nl_command, NULL, 1, rc = netlink_read(&nl_command, NULL, 1, filter);
filter_netlink, (void *)filter);
if(rc < 0) if(rc < 0)
return -1; return -1;
} }
...@@ -1237,8 +1235,7 @@ kernel_dump(int operation, struct kernel_filter *filter) ...@@ -1237,8 +1235,7 @@ kernel_dump(int operation, struct kernel_filter *filter)
if(rc < 0) if(rc < 0)
return -1; return -1;
rc = netlink_read(&nl_command, NULL, 1, rc = netlink_read(&nl_command, NULL, 1, filter);
filter_netlink, (void*) filter);
if(rc < 0) if(rc < 0)
return -1; return -1;
} }
...@@ -1251,7 +1248,7 @@ kernel_dump(int operation, struct kernel_filter *filter) ...@@ -1251,7 +1248,7 @@ kernel_dump(int operation, struct kernel_filter *filter)
if(rc < 0) if(rc < 0)
return -1; return -1;
rc = netlink_read(&nl_command, NULL, 1, filter_netlink, (void*)filter); rc = netlink_read(&nl_command, NULL, 1, filter);
if(rc < 0) if(rc < 0)
return -1; return -1;
} }
...@@ -1512,8 +1509,7 @@ kernel_callback(struct kernel_filter *filter) ...@@ -1512,8 +1509,7 @@ kernel_callback(struct kernel_filter *filter)
return -1; return -1;
} }
} }
rc = netlink_read(&nl_listen, &nl_command, 0, filter_netlink, rc = netlink_read(&nl_listen, &nl_command, 0, filter);
(void*) filter);
if(rc < 0 && nl_listen.sock < 0) if(rc < 0 && nl_listen.sock < 0)
kernel_setup_socket(1); kernel_setup_socket(1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment