aboutsummaryrefslogtreecommitdiff
path: root/util-linux/mdev.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-04-13 22:23:02 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-04-13 22:23:02 +0000
commit065c7147914bf545ede2dbdbc07c48386d750c77 (patch)
tree1988354d67aaa044b21f6bb36baeb1edb6956a03 /util-linux/mdev.c
parent0b791d9a976e46b2705ae73046706ab9ac3768be (diff)
downloadbusybox-w32-065c7147914bf545ede2dbdbc07c48386d750c77.tar.gz
busybox-w32-065c7147914bf545ede2dbdbc07c48386d750c77.tar.bz2
busybox-w32-065c7147914bf545ede2dbdbc07c48386d750c77.zip
mdev: provide $SUBSYSTEM (by Vladimir)
function old new delta make_device 1265 1340 +75 dirAction 14 60 +46 mdev_main 676 695 +19 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 140/0) Total: 140 bytes
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r--util-linux/mdev.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index ca5eaca48..3b0ef351e 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -14,10 +14,12 @@
14 14
15struct globals { 15struct globals {
16 int root_major, root_minor; 16 int root_major, root_minor;
17 char *subsystem;
17}; 18};
18#define G (*(struct globals*)&bb_common_bufsiz1) 19#define G (*(struct globals*)&bb_common_bufsiz1)
19#define root_major (G.root_major) 20#define root_major (G.root_major)
20#define root_minor (G.root_minor) 21#define root_minor (G.root_minor)
22#define subsystem (G.subsystem)
21 23
22/* Prevent infinite loops in /sys symlinks */ 24/* Prevent infinite loops in /sys symlinks */
23#define MAX_SYSFS_DEPTH 3 25#define MAX_SYSFS_DEPTH 3
@@ -111,7 +113,7 @@ static void make_device(char *path, int delete)
111 /* If we have config file, look up user settings */ 113 /* If we have config file, look up user settings */
112 while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) { 114 while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) {
113 regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; 115 regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
114 char *val; 116 char *val = tokens[0];
115 117
116 /* Fields: regex uid:gid mode [alias] [cmd] */ 118 /* Fields: regex uid:gid mode [alias] [cmd] */
117 119
@@ -134,10 +136,15 @@ static void make_device(char *path, int delete)
134 } else { /* ... or regex to match device name */ 136 } else { /* ... or regex to match device name */
135 regex_t match; 137 regex_t match;
136 int result; 138 int result;
139 const char *dev_name_or_subsystem = device_name;
140 if ('/' == val[0] && subsystem) {
141 dev_name_or_subsystem = subsystem;
142 val++;
143 }
137 144
138 /* Is this it? */ 145 /* Is this it? */
139 xregcomp(&match, tokens[0], REG_EXTENDED); 146 xregcomp(&match, val, REG_EXTENDED);
140 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0); 147 result = regexec(&match, dev_name_or_subsystem, ARRAY_SIZE(off), off, 0);
141 regfree(&match); 148 regfree(&match);
142 149
143 //bb_error_msg("matches:"); 150 //bb_error_msg("matches:");
@@ -151,7 +158,7 @@ static void make_device(char *path, int delete)
151 /* If not this device, skip rest of line */ 158 /* If not this device, skip rest of line */
152 /* (regexec returns whole pattern as "range" 0) */ 159 /* (regexec returns whole pattern as "range" 0) */
153 if (result || off[0].rm_so 160 if (result || off[0].rm_so
154 || ((int)off[0].rm_eo != (int)strlen(device_name)) 161 || ((int)off[0].rm_eo != (int)strlen(dev_name_or_subsystem))
155 ) { 162 ) {
156 continue; 163 continue;
157 } 164 }
@@ -276,10 +283,14 @@ static void make_device(char *path, int delete)
276#if ENABLE_FEATURE_MDEV_EXEC 283#if ENABLE_FEATURE_MDEV_EXEC
277 if (command) { 284 if (command) {
278 /* setenv will leak memory, use putenv/unsetenv/free */ 285 /* setenv will leak memory, use putenv/unsetenv/free */
279 char *s = xasprintf("MDEV=%s", device_name); 286 char *s = xasprintf("%s=%s", "MDEV", device_name);
287 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", subsystem);
280 putenv(s); 288 putenv(s);
289 putenv(s1);
281 if (system(command) == -1) 290 if (system(command) == -1)
282 bb_perror_msg_and_die("can't run '%s'", command); 291 bb_perror_msg_and_die("can't run '%s'", command);
292 unsetenv("SUBSYSTEM");
293 free(s1);
283 unsetenv("MDEV"); 294 unsetenv("MDEV");
284 free(s); 295 free(s);
285 free(command); 296 free(command);
@@ -326,6 +337,13 @@ static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
326 void *userData UNUSED_PARAM, 337 void *userData UNUSED_PARAM,
327 int depth) 338 int depth)
328{ 339{
340 /* Extract device subsystem -- the name of the directory under /sys/class/ */
341 if (1 == depth) {
342 subsystem = strrchr(fileName, '/');
343 if (subsystem)
344 subsystem++;
345 }
346
329 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); 347 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
330} 348}
331 349
@@ -445,13 +463,14 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
445 char *env_path; 463 char *env_path;
446 464
447 /* Hotplug: 465 /* Hotplug:
448 * env ACTION=... DEVPATH=... [SEQNUM=...] mdev 466 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
449 * ACTION can be "add" or "remove" 467 * ACTION can be "add" or "remove"
450 * DEVPATH is like "/block/sda" or "/class/input/mice" 468 * DEVPATH is like "/block/sda" or "/class/input/mice"
451 */ 469 */
452 action = getenv("ACTION"); 470 action = getenv("ACTION");
453 env_path = getenv("DEVPATH"); 471 env_path = getenv("DEVPATH");
454 if (!action || !env_path) 472 subsystem = getenv("SUBSYSTEM");
473 if (!action || !env_path /*|| !subsystem*/)
455 bb_show_usage(); 474 bb_show_usage();
456 fw = getenv("FIRMWARE"); 475 fw = getenv("FIRMWARE");
457 476