Browse Source

Add irq.h and irq_parse.c

Serj Kalichev 10 years ago
parent
commit
2fbbb93f5f
4 changed files with 84 additions and 15 deletions
  1. 3 1
      Makefile.am
  2. 33 14
      birq.c
  3. 14 0
      irq.h
  4. 34 0
      irq_parse.c

+ 3 - 1
Makefile.am

@@ -14,11 +14,13 @@ sbin_PROGRAMS = birq
 lib_LIBRARIES =
 
 noinst_HEADERS = \
+	irq.h \
 	nl.h
 
 birq_SOURCES = \
 	birq.c \
-	nl.c
+	nl.c \
+	irq_parse.c
 
 birq_LDADD = liblub.a
 birq_DEPENDENCIES = liblub.a

+ 33 - 14
birq.c

@@ -29,16 +29,19 @@
 #include <linux/netlink.h>
 
 #include "lub/log.h"
+#include "lub/list.h"
+#include "irq.h"
 #include "nl.h"
 
+#define BIRQ_PIDFILE "/var/run/birq.pid"
+#define BIRQ_INTERVAL 5 /* in seconds */
+
 #ifndef VERSION
 #define VERSION 1.0.0
 #endif
 #define QUOTE(t) #t
 #define version(v) printf("%s\n", v)
 
-#define BIRQ_PIDFILE "/var/run/birq.pid"
-
 /* Global signal vars */
 static volatile int sigterm = 0;
 static void sighandler(int signo);
@@ -61,7 +64,7 @@ int main(int argc, char **argv)
 	int retval = -1;
 	struct options *opts = NULL;
 	int pidfd = -1;
-	int rescan = 0; /* sysfs rescan needed */
+	int rescan = 1; /* sysfs rescan needed */
 
 	/* Signal vars */
 	struct sigaction sig_act;
@@ -70,6 +73,9 @@ int main(int argc, char **argv)
 	/* NetLink vars */
 	int nl = -1; /* NetLink socket */
 
+	/* IRQ list. It contain all found irqs. */
+	lub_list_t *irqs;
+
 	/* Parse command line options */
 	opts = opts_init();
 	if (opts_parse(argc, argv, opts))
@@ -120,26 +126,39 @@ int main(int argc, char **argv)
 	sigaction(SIGINT, &sig_act, NULL);
 	sigaction(SIGQUIT, &sig_act, NULL);
 
+	/* Prepare data structures */
+	irqs = lub_list_new(NULL);
+
 	/* Main loop */
 	while (!sigterm) {
 		int n;
-		n = nl_poll(nl, 5);
-		if (n < 0) {
-			if (-2 == n) /* EINTR */
-				continue;
-			break;
-		}
-		if (n > 0) {
-			rescan = 1;
-			continue;
-		}
 
 		if (rescan) {
-			fprintf(stdout, "Rescanning...\n");
+			fprintf(stdout, "Scanning hardware...\n");
 			rescan = 0;
+			irqs_populate(irqs);
+		}
+
+		/* Timeout and poll for new devices */
+		while ((n = nl_poll(nl, BIRQ_INTERVAL)) != 0) {
+			if (-1 == n) {
+				fprintf(stderr,
+					"Error: Broken NetLink socket.\n");
+				goto end;
+			}
+			if (-2 == n) /* EINTR */
+				break;
+			if (n > 0) {
+				rescan = 1;
+				continue;
+			}
 		}
 	}
 
+end:
+	/* Free data structures */
+	lub_list_free(irqs);
+
 	retval = 0;
 err:
 	/* Close NetLink socket */

+ 14 - 0
irq.h

@@ -0,0 +1,14 @@
+#ifndef _irq_h
+#define _irq_h
+
+struct irq_t {
+	int irq;
+	char *desc; /* IRQ text description */
+};
+
+#define SYSFS_PCI_PATH "/sys/bus/pci/devices"
+
+/* IRQ list functions */
+int irqs_populate(lub_list_t *irqs);
+
+#endif

+ 34 - 0
irq_parse.c

@@ -0,0 +1,34 @@
+/* irq_parse.c
+ * Parse IRQ-related files.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <limits.h>
+
+#include "lub/list.h"
+#include "irq.h"
+
+int irqs_populate(lub_list_t *irqs)
+{
+	DIR *dir;
+	struct dirent *dent;
+	char path[PATH_MAX];
+
+	/* Now we can parse PCI devices only */
+	dir = opendir(SYSFS_PCI_PATH);
+	if (!dir)
+		return -1;
+	while((dent = readdir(dir))) {
+		if (!strcmp(dent->d_name, ".") ||
+			!strcmp(dent->d_name, ".."))
+			continue;
+		printf("entry: %s\n", dent->d_name);
+	}
+
+	return 0;
+}
+