diff options
author | Gabor Juhos <juhosg@openwrt.org> | 2013-09-10 12:59:21 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2013-09-10 13:25:35 +0200 |
commit | 070bb98651b7e38d953eeffc5b0435e909e603b9 (patch) | |
tree | 7164097f2cac2d10b8d759f264c2d749530160d3 | |
parent | ee92d6fa240e28d8031d7204939ffec61090d7e2 (diff) | |
download | unitd-070bb98651b7e38d953eeffc5b0435e909e603b9.tar unitd-070bb98651b7e38d953eeffc5b0435e909e603b9.zip |
udevtrigger: add device_has_attribute helper
Move the code from device_list_insert() into
a separate function. This makes it possible
to reuse the code for other attribute files.
Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
-rw-r--r-- | udevtrigger.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/udevtrigger.c b/udevtrigger.c index efea6b2..c5628bd 100644 --- a/udevtrigger.c +++ b/udevtrigger.c @@ -21,6 +21,7 @@ #include <stddef.h> #include <stdarg.h> #include <stdio.h> +#include <stdbool.h> #include <unistd.h> #include <getopt.h> #include <errno.h> @@ -135,21 +136,33 @@ static int sysfs_resolve_link(char *devpath, size_t size) return 0; } +static bool device_has_attribute(const char *path, const char *attr, + mode_t mode) +{ + char filename[PATH_SIZE]; + struct stat statbuf; + + strlcpy(filename, path, sizeof(filename)); + strlcat(filename, attr, sizeof(filename)); + + if (stat(filename, &statbuf) < 0) + return false; + + if (!(statbuf.st_mode & mode)) + return false; + + return true; +} static int device_list_insert(const char *path) { - char filename[PATH_SIZE]; char devpath[PATH_SIZE]; struct stat statbuf; dbg("add '%s'" , path); /* we only have a device, if we have an uevent file */ - strlcpy(filename, path, sizeof(filename)); - strlcat(filename, "/uevent", sizeof(filename)); - if (stat(filename, &statbuf) < 0) - return -1; - if (!(statbuf.st_mode & S_IWUSR)) + if (!device_has_attribute(path, "/uevent", S_IWUSR)) return -1; strlcpy(devpath, &path[4], sizeof(devpath)); |