diff options
author | blogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2012-10-05 10:12:53 +0000 |
---|---|---|
committer | blogic <blogic@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2012-10-05 10:12:53 +0000 |
commit | 5c105d9f3fd086aff195d3849dcf847d6b0bd927 (patch) | |
tree | 1229a11f725bfa58aa7c57a76898553bb5f6654a /package/hotplug2 | |
download | openwrt-5c105d9f3fd086aff195d3849dcf847d6b0bd927.tar.gz openwrt-5c105d9f3fd086aff195d3849dcf847d6b0bd927.zip |
branch Attitude Adjustment
git-svn-id: svn://svn.openwrt.org/openwrt/branches/attitude_adjustment@33625 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/hotplug2')
-rw-r--r-- | package/hotplug2/Makefile | 65 | ||||
-rw-r--r-- | package/hotplug2/files/hotplug2.rules | 10 | ||||
-rw-r--r-- | package/hotplug2/patches/100-env_memleak.patch | 63 | ||||
-rw-r--r-- | package/hotplug2/patches/110-static_worker.patch | 20 | ||||
-rw-r--r-- | package/hotplug2/patches/120-sysfs_path_fix.patch | 14 | ||||
-rw-r--r-- | package/hotplug2/patches/130-cancel_download_fix.patch | 18 | ||||
-rw-r--r-- | package/hotplug2/patches/140-worker_fork_fix.patch | 56 | ||||
-rw-r--r-- | package/hotplug2/patches/150-force_fork_slow.patch | 21 | ||||
-rw-r--r-- | package/hotplug2/patches/160-event_block_fix.patch | 200 | ||||
-rw-r--r-- | package/hotplug2/patches/170-non_fatal_include.patch | 21 | ||||
-rw-r--r-- | package/hotplug2/src/udevtrigger.c | 345 |
11 files changed, 833 insertions, 0 deletions
diff --git a/package/hotplug2/Makefile b/package/hotplug2/Makefile new file mode 100644 index 000000000..e8483ddc7 --- /dev/null +++ b/package/hotplug2/Makefile @@ -0,0 +1,65 @@ +# +# Copyright (C) 2006-2011 OpenWrt.org +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=hotplug2 +PKG_REV:=201 +PKG_VERSION:=$(PKG_REV) +PKG_RELEASE:=4 + +PKG_SOURCE_PROTO:=svn +PKG_SOURCE_VERSION:=$(PKG_REV) +PKG_SOURCE_SUBDIR:=hotplug2-$(PKG_VERSION) +PKG_SOURCE_URL:=http://svn.nomi.cz/svn/isteve/hotplug2 +PKG_SOURCE:=$(PKG_SOURCE_SUBDIR).tar.gz +#PKG_SOURCE_URL:=http://isteve.bofh.cz/~isteve/hotplug2 +#PKG_MD5SUM:=ea2c01d027b4002e4e6b0ff266f51a51 + +include $(INCLUDE_DIR)/package.mk + +define Package/hotplug2 + SECTION:=base + CATEGORY:=Base system + VERSION:=1.0-beta-$(PKG_RELEASE) + TITLE:=Version 1.0 Dynamic device management subsystem for embedded systems + URL:=http://isteve.bofh.cz/~isteve/hotplug2/ + DEPENDS:=+!USE_UCLIBC:libbsd +endef + +define Package/hotplug2/description + Hotplug2 is a trivial replacement of some of the UDev functionality + in a tiny pack, intended for Linux early userspace: Init RAM FS and InitRD. + This is an implementation of Hotplug2-1.0-beta +endef + +define Package/hotplug2/conffiles +/etc/hotplug2.rules +endef + +MAKE_FLAGS += \ + COPTS="$(TARGET_CFLAGS)" \ + STATIC_WORKER="fork" + +ifneq ($(CONFIG_USE_EGLIBC),) + TARGET_LDFLAGS += -lbsd +endif + +define Build/Compile + $(call Build/Compile/Default) + $(TARGET_CC) $(TARGET_CFLAGS) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/udevtrigger src/udevtrigger.c +endef + +define Package/hotplug2/install + $(INSTALL_DIR) $(1)/etc + $(INSTALL_DATA) ./files/hotplug2.rules $(1)/etc/ + $(INSTALL_DIR) $(1)/sbin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/udevtrigger $(1)/sbin/ + $(INSTALL_BIN) $(PKG_BUILD_DIR)/hotplug2 $(1)/sbin/ +endef + +$(eval $(call BuildPackage,hotplug2)) diff --git a/package/hotplug2/files/hotplug2.rules b/package/hotplug2/files/hotplug2.rules new file mode 100644 index 000000000..246cc3ab4 --- /dev/null +++ b/package/hotplug2/files/hotplug2.rules @@ -0,0 +1,10 @@ +$include /etc/hotplug2-common.rules + +SUBSYSTEM ~~ (^net$|^input$|^button$|^usb$|^ieee1394$|^block$|^atm$|^zaptel$|^tty$) { + exec /sbin/hotplug-call %SUBSYSTEM% +} + +DEVICENAME == watchdog { + exec /sbin/watchdog -t 5 /dev/watchdog + next-event +} diff --git a/package/hotplug2/patches/100-env_memleak.patch b/package/hotplug2/patches/100-env_memleak.patch new file mode 100644 index 000000000..28a3e2510 --- /dev/null +++ b/package/hotplug2/patches/100-env_memleak.patch @@ -0,0 +1,63 @@ +--- a/action.c ++++ b/action.c +@@ -31,6 +31,30 @@ static void action_dumb(const struct set + } + + /** ++ * Creates a "key=value" string from the given key and value ++ * ++ * @1 Key ++ * @2 Value ++ * ++ * Returns: Newly allocated string in "key=value" form ++ * ++ */ ++static char* alloc_env(const char *key, const char *value) { ++ size_t keylen, vallen; ++ char *combined; ++ ++ keylen = strlen(key); ++ vallen = strlen(value) + 1; ++ ++ combined = xmalloc(keylen + vallen + 1); ++ memcpy(combined, key, keylen); ++ combined[keylen] = '='; ++ memcpy(&combined[keylen + 1], value, vallen); ++ ++ return combined; ++} ++ ++/** + * Choose what action should be taken according to passed settings. + * + * @1 Hotplug settings +@@ -41,16 +65,25 @@ static void action_dumb(const struct set + */ + void action_perform(struct settings_t *settings, struct uevent_t *event) { + int i; ++ char **env; ++ ++ env = xmalloc(sizeof(char *) * event->env_vars_c); ++ ++ for (i = 0; i < event->env_vars_c; i++) { ++ env[i] = alloc_env(event->env_vars[i].key, event->env_vars[i].value); ++ putenv(env[i]); ++ } + +- for (i = 0; i < event->env_vars_c; i++) +- setenv(event->env_vars[i].key, event->env_vars[i].value, 1); +- + if (settings->dumb == 0) { + ruleset_execute(&settings->rules, event, settings); + } else { + action_dumb(settings, event); + } + +- for (i = 0; i < event->env_vars_c; i++) ++ for (i = 0; i < event->env_vars_c; i++) { + unsetenv(event->env_vars[i].key); ++ free(env[i]); ++ } ++ ++ free(env); + } diff --git a/package/hotplug2/patches/110-static_worker.patch b/package/hotplug2/patches/110-static_worker.patch new file mode 100644 index 000000000..3a5181f74 --- /dev/null +++ b/package/hotplug2/patches/110-static_worker.patch @@ -0,0 +1,20 @@ +--- a/common.mak ++++ b/common.mak +@@ -1,7 +1,7 @@ + # vim:set sw=8 nosta: + + COPTS=-Os -Wall -g +-LDFLAGS=-g -ldl ++LDFLAGS=-g + + CFLAGS=$(COPTS) + FPIC=-fPIC +--- a/Makefile ++++ b/Makefile +@@ -40,5 +40,6 @@ ifdef STATIC_WORKER + CFLAGS += -DSTATIC_WORKER=1 + else + CFLAGS += $(FPIC) ++ LDFLAGS += -ldl + endif + diff --git a/package/hotplug2/patches/120-sysfs_path_fix.patch b/package/hotplug2/patches/120-sysfs_path_fix.patch new file mode 100644 index 000000000..0903057c7 --- /dev/null +++ b/package/hotplug2/patches/120-sysfs_path_fix.patch @@ -0,0 +1,14 @@ +--- a/rules/command.c ++++ b/rules/command.c +@@ -374,9 +374,9 @@ RULES_COMMAND_F(cmd_firmware) { + if (firmware == NULL) + return -1; + +- if (snprintf(sysfs_path_loading, PATH_MAX, "/sysfs%s/loading", devpath) >= PATH_MAX) ++ if (snprintf(sysfs_path_loading, PATH_MAX, "/sys%s/loading", devpath) >= PATH_MAX) + return -1; +- if (snprintf(sysfs_path_data, PATH_MAX, "/sysfs%s/data", devpath) >= PATH_MAX) ++ if (snprintf(sysfs_path_data, PATH_MAX, "/sys%s/data", devpath) >= PATH_MAX) + return -1; + if (snprintf(firmware_path, PATH_MAX, "%s/%s", argv[0], firmware) >= PATH_MAX) + return -1; diff --git a/package/hotplug2/patches/130-cancel_download_fix.patch b/package/hotplug2/patches/130-cancel_download_fix.patch new file mode 100644 index 000000000..c94000dcc --- /dev/null +++ b/package/hotplug2/patches/130-cancel_download_fix.patch @@ -0,0 +1,18 @@ +--- a/rules/command.c ++++ b/rules/command.c +@@ -385,13 +385,13 @@ RULES_COMMAND_F(cmd_firmware) { + + infp = fopen(firmware_path, "r"); + if (infp == NULL) { +- echo_to_file(sysfs_path_loading, "0\n", 2); ++ echo_to_file(sysfs_path_loading, "-1\n", 2); + return -1; + } + outfp = fopen(sysfs_path_data, "w"); + if (outfp == NULL) { + fclose(infp); +- echo_to_file(sysfs_path_loading, "0\n", 2); ++ echo_to_file(sysfs_path_loading, "-1\n", 2); + return -1; + } + diff --git a/package/hotplug2/patches/140-worker_fork_fix.patch b/package/hotplug2/patches/140-worker_fork_fix.patch new file mode 100644 index 000000000..eaf3c5c36 --- /dev/null +++ b/package/hotplug2/patches/140-worker_fork_fix.patch @@ -0,0 +1,56 @@ +--- a/action.c ++++ b/action.c +@@ -39,7 +39,7 @@ static void action_dumb(const struct set + * Returns: Newly allocated string in "key=value" form + * + */ +-static char* alloc_env(const char *key, const char *value) { ++char* alloc_env(const char *key, const char *value) { + size_t keylen, vallen; + char *combined; + +--- a/action.h ++++ b/action.h +@@ -12,5 +12,6 @@ + #include "settings.h" + + void action_perform(struct settings_t *, struct uevent_t *); ++char* alloc_env(const char *, const char *); + #endif /* ifndef ACTION_H */ + +--- a/workers/worker_fork.c ++++ b/workers/worker_fork.c +@@ -380,6 +380,7 @@ static void worker_fork_deinit(void *in_ + + + static int worker_fork_process(void *in_ctx, struct uevent_t *uevent) { ++ char **env; + int i; + struct worker_fork_child_t *child; + struct worker_fork_ctx_t *ctx = in_ctx; +@@ -406,6 +407,12 @@ static int worker_fork_process(void *in_ + * No child process is currently available. + */ + if (child == NULL) { ++ env = xmalloc(sizeof(char *) * uevent->env_vars_c); ++ for (i = 0; i < uevent->env_vars_c; i++) { ++ env[i] = alloc_env(uevent->env_vars[i].key, uevent->env_vars[i].value); ++ putenv(env[i]); ++ } ++ + /* + * Are the matching rules trivial enough that we + * can execute them in the main process? +@@ -421,6 +428,12 @@ static int worker_fork_process(void *in_ + */ + if (ctx->children_count < ctx->max_children) + child = worker_fork_spawn(ctx); ++ ++ for (i = 0; i < uevent->env_vars_c; i++) { ++ unsetenv(uevent->env_vars[i].key); ++ free(env[i]); ++ } ++ free(env); + } + + /* diff --git a/package/hotplug2/patches/150-force_fork_slow.patch b/package/hotplug2/patches/150-force_fork_slow.patch new file mode 100644 index 000000000..47101f56b --- /dev/null +++ b/package/hotplug2/patches/150-force_fork_slow.patch @@ -0,0 +1,21 @@ +--- a/workers/worker_fork.c ++++ b/workers/worker_fork.c +@@ -396,7 +396,7 @@ static int worker_fork_process(void *in_ + worker_fork_update_children(ctx); + + child = NULL; +- for (i = 0; i < ctx->children_count; i++) { ++ for (i = 0; i < ctx->children_count && i < ctx->max_children; i++) { + if (ctx->children[i]->busy == 0) { + child = ctx->children[i]; + break; +@@ -426,7 +426,8 @@ static int worker_fork_process(void *in_ + /* + * We have to fork off a new child. + */ +- if (ctx->children_count < ctx->max_children) ++ if (ctx->children_count < ctx->max_children || ++ (ruleset_flags(&ctx->settings->rules, uevent) & FLAG_SLOW)) + child = worker_fork_spawn(ctx); + + for (i = 0; i < uevent->env_vars_c; i++) { diff --git a/package/hotplug2/patches/160-event_block_fix.patch b/package/hotplug2/patches/160-event_block_fix.patch new file mode 100644 index 000000000..ac7e79ab1 --- /dev/null +++ b/package/hotplug2/patches/160-event_block_fix.patch @@ -0,0 +1,200 @@ +--- a/uevent.c ++++ b/uevent.c +@@ -132,6 +132,8 @@ struct uevent_t *uevent_dup(const struct + + dest = xmalloc(sizeof(struct uevent_t)); + dest->action = src->action; ++ dest->seqnum = src->seqnum; ++ dest->action_str = strdup(src->action_str); + dest->env_vars_c = src->env_vars_c; + dest->env_vars = xmalloc(sizeof(struct env_var_t) * dest->env_vars_c); + dest->plain_s = src->plain_s; +--- a/workers/worker_fork.c ++++ b/workers/worker_fork.c +@@ -1,6 +1,69 @@ + #include "worker_fork.h" + + static struct worker_fork_ctx_t *global_ctx; ++static struct worker_fork_uevent_t *uevent_list; ++ ++static void worker_fork_uevent_free(struct worker_fork_uevent_t *node) { ++ uevent_free(node->uevent); ++ free(node); ++} ++ ++static void worker_fork_uevent_add(void *in_ctx, struct uevent_t *uevent) { ++ char **env; ++ int i; ++ struct worker_fork_ctx_t *ctx = in_ctx; ++ struct worker_fork_uevent_t *node, *walker; ++ ++ node = malloc(sizeof (struct worker_fork_uevent_t)); ++ node->uevent = uevent_dup(uevent); ++ node->next = NULL; ++ ++ if (!uevent_list) uevent_list = node; ++ else { ++ /* ++ * Put events that need to fork first and in reverse order ++ */ ++ env = xmalloc(sizeof(char *) * node->uevent->env_vars_c); ++ for (i = 0; i < node->uevent->env_vars_c; i++) { ++ env[i] = alloc_env(node->uevent->env_vars[i].key, node->uevent->env_vars[i].value); ++ putenv(env[i]); ++ } ++ if (ruleset_flags(&ctx->settings->rules, uevent) & FLAG_SLOW) { ++ node->next = uevent_list; ++ uevent_list = node; ++ } ++ else { ++ for (walker = uevent_list; walker->next; walker = walker->next); ++ walker->next = node; ++ } ++ for (i = 0; i < node->uevent->env_vars_c; i++) { ++ unsetenv(node->uevent->env_vars[i].key); ++ free(env[i]); ++ } ++ free(env); ++ } ++} ++ ++static void worker_fork_uevent_del(struct worker_fork_uevent_t *node) { ++ struct worker_fork_uevent_t *walker; ++ ++ if (node == uevent_list) { ++ uevent_list = node->next; ++ } ++ else { ++ for (walker = uevent_list; walker->next; walker = walker->next) ++ if (walker->next == node) walker->next = node->next; ++ } ++ worker_fork_uevent_free(node); ++} ++ ++static void worker_fork_uevent_empty(void) { ++ struct worker_fork_uevent_t *walker; ++ ++ if (!uevent_list) return; ++ for (walker = uevent_list; walker->next; walker = walker->next) worker_fork_uevent_free(walker); ++ uevent_list = NULL; ++} + + /** + * Destroys data structures related to the given child ID (not PID). +@@ -315,6 +378,8 @@ static void *worker_fork_init(struct set + struct worker_fork_ctx_t *ctx; + PRINTFUNC(); + ++ uevent_list = NULL; ++ + ctx = malloc(sizeof(struct worker_fork_ctx_t)); + ctx->children = NULL; + ctx->children_count = 0; +@@ -376,6 +441,7 @@ static void worker_fork_deinit(void *in_ + free(ctx->children); + free(ctx); + global_ctx = NULL; ++ worker_fork_uevent_empty(); + } + + +@@ -384,15 +450,26 @@ static int worker_fork_process(void *in_ + int i; + struct worker_fork_child_t *child; + struct worker_fork_ctx_t *ctx = in_ctx; ++ struct worker_fork_uevent_t *node, *walker; ++ event_seqnum_t seqnum; ++ ++ worker_fork_uevent_add(ctx, uevent); ++ walker = uevent_list; + + /* +- * A big loop, because if we fail to process the event, ++ * A big loop, because if we fail to process the events, + * we don't want to give up. + * + * TODO: Decide if we want to limit the number of attempts + * or set a time limit before reporting terminal failure. + */ + do { ++ /* ++ * If more events are waiting, return to receive them ++ */ ++ if (!seqnum_get(&seqnum) && seqnum > uevent->seqnum) break; ++ ++ node = walker; + worker_fork_update_children(ctx); + + child = NULL; +@@ -407,9 +484,9 @@ static int worker_fork_process(void *in_ + * No child process is currently available. + */ + if (child == NULL) { +- env = xmalloc(sizeof(char *) * uevent->env_vars_c); +- for (i = 0; i < uevent->env_vars_c; i++) { +- env[i] = alloc_env(uevent->env_vars[i].key, uevent->env_vars[i].value); ++ env = xmalloc(sizeof(char *) * node->uevent->env_vars_c); ++ for (i = 0; i < node->uevent->env_vars_c; i++) { ++ env[i] = alloc_env(node->uevent->env_vars[i].key, node->uevent->env_vars[i].value); + putenv(env[i]); + } + +@@ -418,8 +495,11 @@ static int worker_fork_process(void *in_ + * can execute them in the main process? + */ + if (ctx->always_fork == 0 && ctx->settings->dumb == 0 && +- (ruleset_flags(&ctx->settings->rules, uevent) & FLAG_MASK_SLOW) == 0) { +- action_perform(ctx->settings, uevent); ++ (ruleset_flags(&ctx->settings->rules, node->uevent) & FLAG_MASK_SLOW) == 0) { ++ action_perform(ctx->settings, node->uevent); ++ walker = walker->next; ++ worker_fork_uevent_del(node); ++ if (walker) continue; + break; + } + +@@ -427,11 +507,11 @@ static int worker_fork_process(void *in_ + * We have to fork off a new child. + */ + if (ctx->children_count < ctx->max_children || +- (ruleset_flags(&ctx->settings->rules, uevent) & FLAG_SLOW)) ++ (ruleset_flags(&ctx->settings->rules, node->uevent) & FLAG_SLOW)) + child = worker_fork_spawn(ctx); + +- for (i = 0; i < uevent->env_vars_c; i++) { +- unsetenv(uevent->env_vars[i].key); ++ for (i = 0; i < node->uevent->env_vars_c; i++) { ++ unsetenv(node->uevent->env_vars[i].key); + free(env[i]); + } + free(env); +@@ -442,9 +522,14 @@ static int worker_fork_process(void *in_ + */ + if (child != NULL) { + child->busy = 1; +- if (!worker_fork_relay_event(child->event_fd, uevent)); +- break; +- child->busy = 0; ++ if (worker_fork_relay_event(child->event_fd, node->uevent)) { ++ child->busy = 0; ++ continue; ++ } ++ walker = walker->next; ++ worker_fork_uevent_del(node); ++ if (walker) continue; ++ break; + } + + /* +--- a/workers/worker_fork.h ++++ b/workers/worker_fork.h +@@ -35,4 +35,9 @@ struct worker_fork_ctx_t { + struct settings_t *settings; + }; + ++struct worker_fork_uevent_t { ++ struct uevent_t *uevent; ++ struct worker_fork_uevent_t *next; ++}; ++ + #endif diff --git a/package/hotplug2/patches/170-non_fatal_include.patch b/package/hotplug2/patches/170-non_fatal_include.patch new file mode 100644 index 000000000..0b8e85283 --- /dev/null +++ b/package/hotplug2/patches/170-non_fatal_include.patch @@ -0,0 +1,21 @@ +Index: hotplug2-201/parser/parser.c +=================================================================== +--- hotplug2-201.orig/parser/parser.c 2009-12-09 19:44:13.000000000 +0100 ++++ hotplug2-201/parser/parser.c 2011-02-08 18:06:44.681213713 +0100 +@@ -1,5 +1,7 @@ + #include "parser.h" + ++#include <errno.h> ++ + /* + * Grammar: + * input => directive // TOKEN_ROOTKW +@@ -578,7 +580,7 @@ + ctx.lexer.fp = fopen(filename, "r"); + if (ctx.lexer.fp == NULL) { + parser_clear(&ctx); +- return -1; ++ return (errno == ENOENT) ? 0 : -1; + } + ctx.lexer.filename = strdup(filename); + diff --git a/package/hotplug2/src/udevtrigger.c b/package/hotplug2/src/udevtrigger.c new file mode 100644 index 000000000..297e64cc9 --- /dev/null +++ b/package/hotplug2/src/udevtrigger.c @@ -0,0 +1,345 @@ +/* + * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org> + * Copyright (C) 2006 Hannes Reinecke <hare@suse.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <stdlib.h> +#include <stddef.h> +#include <stdarg.h> +#include <string.h> +#include <stdio.h> +#include <unistd.h> +#include <getopt.h> +#include <errno.h> +#include <dirent.h> +#include <fcntl.h> +#include <syslog.h> +#include <sys/stat.h> +#include <sys/types.h> + +#define PATH_SIZE 512 + +static int verbose; +static int dry_run; + +void log_message(int priority, const char *format, ...) +{ + va_list args; + + va_start(args, format); + vsyslog(priority, format, args); + va_end(args); +} + +#undef err +#define err(format, arg...) \ + do { \ + log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \ + } while (0) + +#undef info +#define info(format, arg...) \ + do { \ + log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \ + } while (0) + +#ifdef DEBUG +#undef dbg +#define dbg(format, arg...) \ + do { \ + log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \ + } while (0) +#else +#define dbg(...) do {} while(0) +#endif + + +static void trigger_uevent(const char *devpath) +{ + char filename[PATH_SIZE]; + int fd; + + strlcpy(filename, "/sys", sizeof(filename)); + strlcat(filename, devpath, sizeof(filename)); + strlcat(filename, "/uevent", sizeof(filename)); + + if (verbose) + printf("%s\n", devpath); + + if (dry_run) + return; + + fd = open(filename, O_WRONLY); + if (fd < 0) { + dbg("error on opening %s: %s\n", filename, strerror(errno)); + return; + } + + if (write(fd, "add", 3) < 0) + info("error on triggering %s: %s\n", filename, strerror(errno)); + + close(fd); +} + +static int sysfs_resolve_link(char *devpath, size_t size) +{ + char link_path[PATH_SIZE]; + char link_target[PATH_SIZE]; + int len; + int i; + int back; + + strlcpy(link_path, "/sys", sizeof(link_path)); + strlcat(link_path, devpath, sizeof(link_path)); + len = readlink(link_path, link_target, sizeof(link_target)); + if (len <= 0) + return -1; + link_target[len] = '\0'; + dbg("path link '%s' points to '%s'", devpath, link_target); + + for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++) + ; + dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back); + for (i = 0; i <= back; i++) { + char *pos = strrchr(devpath, '/'); + + if (pos == NULL) + return -1; + pos[0] = '\0'; + } + dbg("after moving back '%s'", devpath); + strlcat(devpath, "/", size); + strlcat(devpath, &link_target[back * 3], size); + return 0; +} + + +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)) + return -1; + + strlcpy(devpath, &path[4], sizeof(devpath)); + + /* resolve possible link to real target */ + if (lstat(path, &statbuf) < 0) + return -1; + if (S_ISLNK(statbuf.st_mode)) + if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0) + return -1; + + trigger_uevent(devpath); + return 0; +} + + +static void scan_subsystem(const char *subsys) +{ + char base[PATH_SIZE]; + DIR *dir; + struct dirent *dent; + + strlcpy(base, "/sys/", sizeof(base)); + strlcat(base, subsys, sizeof(base)); + + dir = opendir(base); + if (dir != NULL) { + for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { + char dirname[PATH_SIZE]; + DIR *dir2; + struct dirent *dent2; + + if (dent->d_name[0] == '.') + continue; + + strlcpy(dirname, base, sizeof(dirname)); + strlcat(dirname, "/", sizeof(dirname)); + strlcat(dirname, dent->d_name, sizeof(dirname)); + strlcat(dirname, "/devices", sizeof(dirname)); + + /* look for devices */ + dir2 = opendir(dirname); + if (dir2 != NULL) { + for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { + char dirname2[PATH_SIZE]; + + if (dent2->d_name[0] == '.') + continue; + + strlcpy(dirname2, dirname, sizeof(dirname2)); + strlcat(dirname2, "/", sizeof(dirname2)); + strlcat(dirname2, dent2->d_name, sizeof(dirname2)); + device_list_insert(dirname2); + } + closedir(dir2); + } + } + closedir(dir); + } +} + +static void scan_block(void) +{ + char base[PATH_SIZE]; + DIR *dir; + struct dirent *dent; + + strlcpy(base, "/sys/block", sizeof(base)); + + dir = opendir(base); + if (dir != NULL) { + for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { + char dirname[PATH_SIZE]; + DIR *dir2; + struct dirent *dent2; + + if (dent->d_name[0] == '.') + continue; + + strlcpy(dirname, base, sizeof(dirname)); + strlcat(dirname, "/", sizeof(dirname)); + strlcat(dirname, dent->d_name, sizeof(dirname)); + if (device_list_insert(dirname) != 0) + continue; + + /* look for partitions */ + dir2 = opendir(dirname); + if (dir2 != NULL) { + for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { + char dirname2[PATH_SIZE]; + + if (dent2->d_name[0] == '.') + continue; + + if (!strcmp(dent2->d_name,"device")) + continue; + + strlcpy(dirname2, dirname, sizeof(dirname2)); + strlcat(dirname2, "/", sizeof(dirname2)); + strlcat(dirname2, dent2->d_name, sizeof(dirname2)); + device_list_insert(dirname2); + } + closedir(dir2); + } + } + closedir(dir); + } +} + +static void scan_class(void) +{ + char base[PATH_SIZE]; + DIR *dir; + struct dirent *dent; + + strlcpy(base, "/sys/class", sizeof(base)); + + dir = opendir(base); + if (dir != NULL) { + for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { + char dirname[PATH_SIZE]; + DIR *dir2; + struct dirent *dent2; + + if (dent->d_name[0] == '.') + continue; + + strlcpy(dirname, base, sizeof(dirname)); + strlcat(dirname, "/", sizeof(dirname)); + strlcat(dirname, dent->d_name, sizeof(dirname)); + dir2 = opendir(dirname); + if (dir2 != NULL) { + for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { + char dirname2[PATH_SIZE]; + + if (dent2->d_name[0] == '.') + continue; + + if (!strcmp(dent2->d_name, "device")) + continue; + + strlcpy(dirname2, dirname, sizeof(dirname2)); + strlcat(dirname2, "/", sizeof(dirname2)); + strlcat(dirname2, dent2->d_name, sizeof(dirname2)); + device_list_insert(dirname2); + } + closedir(dir2); + } + } + closedir(dir); + } +} + +int main(int argc, char *argv[], char *envp[]) +{ + char base[PATH_SIZE]; + struct stat statbuf; + int failed = 0; + int option; + + openlog("udevtrigger", LOG_PID | LOG_CONS, LOG_DAEMON); + + while (1) { + option = getopt(argc, argv, "vnh"); + if (option == -1) + break; + + switch (option) { + case 'v': + verbose = 1; + break; + case 'n': + dry_run = 1; + break; + case 'h': + printf("Usage: udevtrigger OPTIONS\n" + " -v print the list of devices while running\n" + " -n do not actually trigger the events\n" + " -h print this text\n" + "\n"); + goto exit; + default: + goto exit; + } + } + + + /* if we have /sys/subsystem, forget all the old stuff */ + scan_subsystem("bus"); + scan_class(); + + /* scan "block" if it isn't a "class" */ + strlcpy(base, "/sys/class/block", sizeof(base)); + if (stat(base, &statbuf) != 0) + scan_block(); + +exit: + + closelog(); + return 0; +} |