remove some more code duplication
authorFelix Fietkau <nbd@openwrt.org>
Sun, 23 Oct 2011 14:02:18 +0000 (16:02 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 23 Oct 2011 14:02:18 +0000 (16:02 +0200)
system-linux.c

index 11126fa4f23badf4f50d366605a6d5ef910f7f83..0308e6f3e6ed5b08af95a0a0845d05a2edda977a 100644 (file)
@@ -42,6 +42,21 @@ handler_nl_event(struct uloop_fd *u, unsigned int events)
        nl_recvmsgs(ev->sock, ev->cb);
 }
 
+static struct nl_sock *
+create_socket(int protocol)
+{
+       struct nl_sock *sock;
+
+       sock = nl_socket_alloc();
+       if (!sock)
+               return NULL;
+
+       if (nl_connect(sock, protocol))
+               return NULL;
+
+       return sock;
+}
+
 static bool
 create_event_socket(struct event_socket *ev, int protocol,
                    int (*cb)(struct nl_msg *msg, void *arg))
@@ -53,13 +68,10 @@ create_event_socket(struct event_socket *ev, int protocol,
 
        nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
 
-       ev->sock = nl_socket_alloc();
+       ev->sock = create_socket(protocol);
        if (!ev->sock)
                return false;
 
-       if (nl_connect(ev->sock, protocol))
-               return false;
-
        ev->uloop.fd = nl_socket_get_fd(ev->sock);
        ev->uloop.cb = handler_nl_event;
        uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
@@ -74,20 +86,16 @@ int system_init(void)
        fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
 
        // Prepare socket for routing / address control
-       sock_rtnl = nl_socket_alloc();
+       sock_rtnl = create_socket(NETLINK_ROUTE);
        if (!sock_rtnl)
                return -1;
 
-       if (nl_connect(sock_rtnl, NETLINK_ROUTE))
-               return -1;
-
        if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
                return -1;
 
        // Receive network link events form kernel
        nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
 
-
        return 0;
 }