nldev

NetLink DEVice manager; a lightweight netlink frontend for mdev.
git clone git://r-36.net/nldev
Log | Files | Refs | LICENSE

commit 0c6abf3133abf0100fe18d4a0e9fb0df48c6c0ba
parent 3926fcc07fb8171017902702050d473e05948c26
Author: Natanael Copa <ncopa@alpinelinux.org>
Date:   Mon, 29 Dec 2014 22:01:31 +0100

move netlink socket initialization to its own function

improves readability

Signed-off-by: Christoph Lohmann <20h@r-36.net>

Diffstat:
nldev.c | 58+++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 35 insertions(+), 23 deletions(-)

diff --git a/nldev.c b/nldev.c @@ -148,6 +148,38 @@ initsignals(void) signal(SIGPIPE, SIG_IGN); } +int +init_netlink_socket(void) +{ + struct sockaddr_nl nls; + int fd, slen; + + memset(&nls, 0, sizeof(nls)); + nls.nl_family = AF_NETLINK; + nls.nl_pid = getpid(); + nls.nl_groups = -1; + + fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); + if (fd < 0) + edie("socket"); + + slen = 128*1024*1024; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &slen, + sizeof(slen)) < 0) { + edie("setsockopt"); + } + slen = 1; + if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &slen, + sizeof(slen)) < 0) { + edie("setsockopt"); + } + + if (bind(fd, (void *)&nls, sizeof(nls))) + edie("bind"); + + return fd; +} + void usage(void) { @@ -157,7 +189,7 @@ usage(void) int main(int argc, char *argv[]) { - struct sockaddr_nl nls, cnls; + struct sockaddr_nl cnls; struct pollfd fds; struct msghdr hdr; struct iovec iov; @@ -195,30 +227,9 @@ main(int argc, char *argv[]) usage(); } ARGEND; - memset(&nls, 0, sizeof(nls)); - nls.nl_family = AF_NETLINK; - nls.nl_pid = getpid(); - nls.nl_groups = -1; - fds.events = POLLIN; - fds.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); + fds.fd = init_netlink_socket(); listfd = fds.fd; - if (fds.fd < 0) - edie("socket"); - - slen = 128*1024*1024; - if (setsockopt(fds.fd, SOL_SOCKET, SO_RCVBUFFORCE, &slen, - sizeof(slen)) < 0) { - edie("setsockopt"); - } - slen = 1; - if (setsockopt(fds.fd, SOL_SOCKET, SO_PASSCRED, &slen, - sizeof(slen)) < 0) { - edie("setsockopt"); - } - - if (bind(fds.fd, (void *)&nls, sizeof(nls))) - edie("bind"); if (dofork) { if (daemon(0, 0) < 0) @@ -285,6 +296,7 @@ main(int argc, char *argv[]) continue; } + slen = 0; for (i = 0; i < len; i += slen + 1) { key = buf + i; value = strchr(key, '=');