summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2015-09-15 16:29:36 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2015-09-15 16:29:36 +0200
commit30f48528f60ee29c917533026a0facffe530bdc0 (patch)
treebf895e754ae2ceffe564d3cd6dcd024db4ef532a
parent327add8c702edd5515b79fbfc29d9ef0b07084f4 (diff)
downloadnlplug-30f48528f60ee29c917533026a0facffe530bdc0.tar.bz2
nlplug-30f48528f60ee29c917533026a0facffe530bdc0.tar.xz
nlplug: add -s for searchig for a given device
Add -s option so we can specify a specific block device. Once it is found we just exit.
-rw-r--r--nlplug.c55
1 files changed, 41 insertions, 14 deletions
diff --git a/nlplug.c b/nlplug.c
index fda8a84..3ed8dbd 100644
--- a/nlplug.c
+++ b/nlplug.c
@@ -114,9 +114,24 @@ int load_kmod(const char *modalias)
return count;
}
-int process_kbuf(char *buf, const size_t len, char **argv, const char *subsys)
+int is_searchdev(const char *devname, const char *searchdev)
{
- char *modalias = NULL, *action = NULL;
+ if (searchdev == NULL)
+ return 0;
+
+ if (strcmp(devname, searchdev) == 0) {
+ dbg("FOUND %s", searchdev);
+ return 1;
+ }
+
+ return 0;
+}
+
+int process_kbuf(char *buf, const size_t len, char **argv,
+ const char *subsys_filter,
+ const char *searchdev)
+{
+ char *modalias = NULL, *action = NULL, *subsystem = NULL;
int i, slen = 0;
char *key, *value;
@@ -140,19 +155,22 @@ int process_kbuf(char *buf, const size_t len, char **argv, const char *subsys)
continue;
}
- if (subsys && strncmp(key, "SUBSYSTEM=", 10) == 0
- && !strstr(key+10, subsys)) {
- dbg("subsystem filter '%s' applied.", subsys);
- break;
- }
-
value[0] = '\0';
value++;
- if (strncmp(key, "MODALIAS", 9) == 0) {
+ if (strcmp(key, "MODALIAS") == 0) {
modalias = value;
- } else if (strncmp(key, "ACTION", 6) == 0) {
+ } else if (strcmp(key, "ACTION") == 0) {
action = value;
+ } else if (strcmp(key, "SUBSYSTEM") == 0) {
+ subsystem = value;
+ if (subsys_filter
+ && strcmp(subsystem, subsys_filter) != 0) {
+ dbg("subsystem '%s' filtered out ('%s').",
+ subsystem, subsys_filter);
+ return 0;
+ }
+
}
/*
@@ -169,6 +187,10 @@ int process_kbuf(char *buf, const size_t len, char **argv, const char *subsys)
load_kmod(modalias);
} else if (getenv("DEVNAME") != NULL) {
run_child(argv);
+ if (subsystem && strcmp(subsystem, "block") == 0
+ && strcmp(action, "add") == 0
+ && is_searchdev(getenv("DEVNAME"), searchdev))
+ exit(0);
}
}
return modalias != NULL ? 1 : 0;
@@ -177,14 +199,15 @@ int process_kbuf(char *buf, const size_t len, char **argv, const char *subsys)
void
usage(void)
{
- errx(1, "usage: %s [-dku] [-f subsystem] PATH\n", argv0);
+ errx(1, "usage: %s [-dku] [-f subsystem] [-b device] PATH\n", argv0);
}
int main(int argc, char *argv[])
{
struct pollfd fds;
int r;
- char *subsystem = NULL;
+ char *subsys_filter = NULL;
+ char *searchdev = NULL;
int showudev, showkernel;
@@ -202,7 +225,10 @@ int main(int argc, char *argv[])
showkernel = 0;
break;
case 'f':
- subsystem = EARGF(usage());
+ subsys_filter = EARGF(usage());
+ break;
+ case 's':
+ searchdev = EARGF(usage());
break;
default:
usage();
@@ -277,7 +303,8 @@ int main(int argc, char *argv[])
continue;
}
- modalias_count += process_kbuf(buf, len, argv, subsystem);
+ modalias_count += process_kbuf(buf, len, argv, subsys_filter,
+ searchdev);
event_count++;
dbg("-------------------------\n");
if (fds.revents & POLLHUP) {