Browse Source

Move netlink work to separate file

Serj Kalichev 10 years ago
parent
commit
c2276422c4
5 changed files with 87 additions and 68 deletions
  1. 9 7
      Makefile.am
  2. 5 60
      birq.c
  3. 1 1
      lub/module.am
  4. 64 0
      nl.c
  5. 8 0
      nl.h

+ 9 - 7
Makefile.am

@@ -1,22 +1,24 @@
 ## Process this file with automake to generate Makefile.in
-AUTOMAKE_OPTIONS = foreign nostdinc
-ACLOCAL_AMFLAGS = -I m4
-
-AM_CPPFLAGS = -I. -I$(top_srcdir)
-AM_LD = $(CC)
 
 if DEBUG
   DEBUG_CFLAGS = -DDEBUG
 endif
 
+AUTOMAKE_OPTIONS = foreign nostdinc
+ACLOCAL_AMFLAGS = -I m4
+AM_CPPFLAGS = -I. -I$(top_srcdir)
+AM_LD = $(CC)
 AM_CFLAGS = -Wall $(DEBUG_CFLAGS)
 
 sbin_PROGRAMS = birq
 lib_LIBRARIES =
-nobase_include_HEADERS =
+
+noinst_HEADERS = \
+	nl.h
 
 birq_SOURCES = \
-	birq.c
+	birq.c \
+	nl.c
 
 birq_LDADD = liblub.a
 birq_DEPENDENCIES = liblub.a

+ 5 - 60
birq.c

@@ -29,6 +29,7 @@
 #include <linux/netlink.h>
 
 #include "lub/log.h"
+#include "nl.h"
 
 #ifndef VERSION
 #define VERSION 1.0.0
@@ -43,14 +44,10 @@ static volatile int sigterm = 0;
 static void sighandler(int signo);
 
 static void help(int status, const char *argv0);
-struct options *opts_init(void);
-void opts_free(struct options *opts);
+static struct options *opts_init(void);
+static void opts_free(struct options *opts);
 static int opts_parse(int argc, char *argv[], struct options *opts);
 
-static int nl_init(void);
-static void nl_close(int nl);
-static int nl_poll(int nl, int timeout);
-
 /* Command line options */
 struct options {
 	char *pidfile;
@@ -164,58 +161,6 @@ err:
 	return retval;
 }
 
-static int nl_init(void)
-{
-	struct sockaddr_nl nl_addr;
-	int nl;
-
-	memset(&nl_addr, 0, sizeof(nl_addr));
-	nl_addr.nl_family = AF_NETLINK;
-	nl_addr.nl_pad = 0;
-	nl_addr.nl_pid = 0; /* Let kernel to assign id */
-	nl_addr.nl_groups = -1; /* Listen all multicast */
-
-	if ((nl = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT)) < 0) {
-		fprintf(stderr, "Error: Can't create socket\n");
-		return -1;
-	}
-	if (bind(nl, (void *)&nl_addr, sizeof(nl_addr))) {
-		fprintf(stderr, "Error: Can't bind NetLink\n");
-		return -1;
-	}
-
-	return nl;
-}
-
-static void nl_close(int nl)
-{
-	if (nl >= 0)
-		close(nl);
-}
-
-static int nl_poll(int nl, int timeout)
-{
-	struct pollfd pfd;
-	char buf[10];
-	int n;
-
-	pfd.events = POLLIN;
-	pfd.fd = nl;
-
-	n = poll(&pfd, 1, (timeout * 1000));
-	if (n < 0) {
-		if (EINTR == errno)
-			return -2;
-		return -1;
-	}
-	/* Some device-related event */
-	/* Read all messages. We don't need a message content. */
-	if (n > 0)
-		while (recv(nl, buf, sizeof(buf), MSG_DONTWAIT) > 0);
-
-	return n;
-}
-
 /*--------------------------------------------------------- */
 /*
  * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
@@ -227,7 +172,7 @@ static void sighandler(int signo)
 
 /*--------------------------------------------------------- */
 /* Initialize option structure by defaults */
-struct options *opts_init(void)
+static struct options *opts_init(void)
 {
 	struct options *opts = NULL;
 
@@ -242,7 +187,7 @@ struct options *opts_init(void)
 
 /*--------------------------------------------------------- */
 /* Free option structure */
-void opts_free(struct options *opts)
+static void opts_free(struct options *opts)
 {
 	if (opts->pidfile)
 		free(opts->pidfile);

+ 1 - 1
lub/module.am

@@ -2,7 +2,7 @@
 noinst_LIBRARIES = liblub.a
 liblub_a_SOURCES =
 
-nobase_include_HEADERS += \
+noinst_HEADERS += \
     lub/list.h \
     lub/c_decl.h \
     lub/log.h

+ 64 - 0
nl.c

@@ -0,0 +1,64 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <asm/types.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <poll.h>
+
+#include "nl.h"
+
+int nl_init(void)
+{
+	struct sockaddr_nl nl_addr;
+	int nl;
+
+	memset(&nl_addr, 0, sizeof(nl_addr));
+	nl_addr.nl_family = AF_NETLINK;
+	nl_addr.nl_pad = 0;
+	nl_addr.nl_pid = 0; /* Let kernel to assign id */
+	nl_addr.nl_groups = -1; /* Listen all multicast */
+
+	if ((nl = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT)) < 0) {
+		fprintf(stderr, "Error: Can't create socket\n");
+		return -1;
+	}
+	if (bind(nl, (void *)&nl_addr, sizeof(nl_addr))) {
+		fprintf(stderr, "Error: Can't bind NetLink\n");
+		return -1;
+	}
+
+	return nl;
+}
+
+void nl_close(int nl)
+{
+	if (nl >= 0)
+		close(nl);
+}
+
+int nl_poll(int nl, int timeout)
+{
+	struct pollfd pfd;
+	char buf[10];
+	int n;
+
+	pfd.events = POLLIN;
+	pfd.fd = nl;
+
+	n = poll(&pfd, 1, (timeout * 1000));
+	if (n < 0) {
+		if (EINTR == errno)
+			return -2;
+		return -1;
+	}
+	/* Some device-related event */
+	/* Read all messages. We don't need a message content. */
+	if (n > 0)
+		while (recv(nl, buf, sizeof(buf), MSG_DONTWAIT) > 0);
+
+	return n;
+}

+ 8 - 0
nl.h

@@ -0,0 +1,8 @@
+#ifndef _nl_h
+#define _nl_h
+
+int nl_init(void);
+void nl_close(int nl);
+int nl_poll(int nl, int timeout);
+
+#endif