"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 3 | * mdev - Mini udev for busybox |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 5 | * Copyright 2005 Rob Landley <rob@landley.net> |
| 6 | * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 9 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 10 | |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 11 | //config:config MDEV |
| 12 | //config: bool "mdev" |
| 13 | //config: default y |
| 14 | //config: select PLATFORM_LINUX |
| 15 | //config: help |
| 16 | //config: mdev is a mini-udev implementation for dynamically creating device |
| 17 | //config: nodes in the /dev directory. |
| 18 | //config: |
| 19 | //config: For more information, please see docs/mdev.txt |
| 20 | //config: |
| 21 | //config:config FEATURE_MDEV_CONF |
| 22 | //config: bool "Support /etc/mdev.conf" |
| 23 | //config: default y |
| 24 | //config: depends on MDEV |
| 25 | //config: help |
| 26 | //config: Add support for the mdev config file to control ownership and |
| 27 | //config: permissions of the device nodes. |
| 28 | //config: |
| 29 | //config: For more information, please see docs/mdev.txt |
| 30 | //config: |
| 31 | //config:config FEATURE_MDEV_RENAME |
| 32 | //config: bool "Support subdirs/symlinks" |
| 33 | //config: default y |
| 34 | //config: depends on FEATURE_MDEV_CONF |
| 35 | //config: help |
| 36 | //config: Add support for renaming devices and creating symlinks. |
| 37 | //config: |
| 38 | //config: For more information, please see docs/mdev.txt |
| 39 | //config: |
| 40 | //config:config FEATURE_MDEV_RENAME_REGEXP |
| 41 | //config: bool "Support regular expressions substitutions when renaming device" |
| 42 | //config: default y |
| 43 | //config: depends on FEATURE_MDEV_RENAME |
| 44 | //config: help |
| 45 | //config: Add support for regular expressions substitutions when renaming |
| 46 | //config: device. |
| 47 | //config: |
| 48 | //config:config FEATURE_MDEV_EXEC |
| 49 | //config: bool "Support command execution at device addition/removal" |
| 50 | //config: default y |
| 51 | //config: depends on FEATURE_MDEV_CONF |
| 52 | //config: help |
| 53 | //config: This adds support for an optional field to /etc/mdev.conf for |
| 54 | //config: executing commands when devices are created/removed. |
| 55 | //config: |
| 56 | //config: For more information, please see docs/mdev.txt |
| 57 | //config: |
| 58 | //config:config FEATURE_MDEV_LOAD_FIRMWARE |
| 59 | //config: bool "Support loading of firmwares" |
| 60 | //config: default y |
| 61 | //config: depends on MDEV |
| 62 | //config: help |
| 63 | //config: Some devices need to load firmware before they can be usable. |
| 64 | //config: |
| 65 | //config: These devices will request userspace look up the files in |
| 66 | //config: /lib/firmware/ and if it exists, send it to the kernel for |
| 67 | //config: loading into the hardware. |
| 68 | |
| 69 | //applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP)) |
| 70 | |
| 71 | //kbuild:lib-$(CONFIG_MDEV) += mdev.o |
| 72 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 73 | //usage:#define mdev_trivial_usage |
| 74 | //usage: "[-s]" |
| 75 | //usage:#define mdev_full_usage "\n\n" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 76 | //usage: "mdev -s is to be run during boot to scan /sys and populate /dev.\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 77 | //usage: "\n" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 78 | //usage: "Bare mdev is a kernel hotplug helper. To activate it:\n" |
| 79 | //usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 80 | //usage: IF_FEATURE_MDEV_CONF( |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 81 | //usage: "\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 82 | //usage: "It uses /etc/mdev.conf with lines\n" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 83 | //usage: " [-]DEVNAME UID:GID PERM" |
| 84 | //usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 85 | //usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]") |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 86 | //usage: "\n" |
| 87 | //usage: "where DEVNAME is device name regex, @major,minor[-minor2], or\n" |
| 88 | //usage: "environment variable regex. A common use of the latter is\n" |
| 89 | //usage: "to load modules for hotplugged devices:\n" |
| 90 | //usage: " $MODALIAS=.* 0:0 660 @modprobe \"$MODALIAS\"\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 91 | //usage: ) |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 92 | //usage: "\n" |
| 93 | //usage: "If /dev/mdev.seq file exists, mdev will wait for its value\n" |
| 94 | //usage: "to match $SEQNUM variable. This prevents plug/unplug races.\n" |
| 95 | //usage: "To activate this feature, create empty /dev/mdev.seq at boot." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 96 | //usage: |
| 97 | //usage:#define mdev_notes_usage "" |
| 98 | //usage: IF_FEATURE_MDEV_CONFIG( |
| 99 | //usage: "The mdev config file contains lines that look like:\n" |
| 100 | //usage: " hd[a-z][0-9]* 0:3 660\n\n" |
| 101 | //usage: "That's device name (with regex match), uid:gid, and permissions.\n\n" |
| 102 | //usage: IF_FEATURE_MDEV_EXEC( |
| 103 | //usage: "Optionally, that can be followed (on the same line) by a special character\n" |
| 104 | //usage: "and a command line to run after creating/before deleting the corresponding\n" |
| 105 | //usage: "device(s). The environment variable $MDEV indicates the active device node\n" |
| 106 | //usage: "(which is useful if it's a regex match). For example:\n\n" |
| 107 | //usage: " hdc root:cdrom 660 *ln -s $MDEV cdrom\n\n" |
| 108 | //usage: "The special characters are @ (run after creating), $ (run before deleting),\n" |
| 109 | //usage: "and * (run both after creating and before deleting). The commands run in\n" |
| 110 | //usage: "the /dev directory, and use system() which calls /bin/sh.\n\n" |
| 111 | //usage: ) |
| 112 | //usage: "Config file parsing stops on the first matching line. If no config\n" |
| 113 | //usage: "entry is matched, devices are created with default 0:0 660. (Make\n" |
| 114 | //usage: "the last line match .* to override this.)\n\n" |
| 115 | //usage: ) |
| 116 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 117 | #include "libbb.h" |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 118 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 120 | /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev |
| 121 | * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev |
| 122 | * contains "4:0\n". Directory name is taken as device name, path component |
| 123 | * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty". |
| 124 | * Then mdev creates the /dev/device_name node. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 125 | * If /sys/class/.../dev file does not exist, mdev still may act |
| 126 | * on this device: see "@|$|*command args..." parameter in config file. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 127 | * |
| 128 | * mdev w/o parameters is called as hotplug helper. It takes device |
| 129 | * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts |
| 130 | * maj,min from "/sys/$DEVPATH/dev" and also examines |
| 131 | * $ACTION ("add"/"delete") and $FIRMWARE. |
| 132 | * |
| 133 | * If action is "add", mdev creates /dev/device_name similarly to mdev -s. |
| 134 | * (todo: explain "delete" and $FIRMWARE) |
| 135 | * |
| 136 | * If /etc/mdev.conf exists, it may modify /dev/device_name's properties. |
| 137 | * /etc/mdev.conf file format: |
| 138 | * |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 139 | * [-][subsystem/]device user:grp mode [>|=path]|[!] [@|$|*command args...] |
| 140 | * [-]@maj,min[-min2] user:grp mode [>|=path]|[!] [@|$|*command args...] |
| 141 | * [-]$envvar=val user:grp mode [>|=path]|[!] [@|$|*command args...] |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 142 | * |
| 143 | * Leading minus in 1st field means "don't stop on this line", otherwise |
| 144 | * search is stopped after the matching line is encountered. |
| 145 | * |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 146 | * The device name or "subsystem/device" combo is matched against 1st field |
| 147 | * (which is a regex), or maj,min is matched against 1st field, |
| 148 | * or specified environment variable (as regex) is matched against 1st field. |
| 149 | * |
| 150 | * $envvar=val format is useful for loading modules for hot-plugged devices |
| 151 | * which do not have driver loaded yet. In this case /sys/class/.../dev |
| 152 | * does not exist, but $MODALIAS is set to needed module's name |
| 153 | * (actually, an alias to it) by kernel. This rule instructs mdev |
| 154 | * to load the module and exit: |
| 155 | * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS" |
| 156 | * The kernel will generate another hotplug event when /sys/class/.../dev |
| 157 | * file appears. |
| 158 | * |
| 159 | * When line matches, the device node is created, chmod'ed and chown'ed, |
| 160 | * moved to path, and if >path, a symlink to moved node is created, |
| 161 | * all this if /sys/class/.../dev exists. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 162 | * Examples: |
| 163 | * =loop/ - moves to /dev/loop |
| 164 | * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 165 | * |
| 166 | * Then "command args..." is executed (via sh -c 'command args...'). |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 167 | * @:execute on creation, $:on deletion, *:on both. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 168 | * This happens regardless of /sys/class/.../dev existence. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 169 | */ |
| 170 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 171 | struct rule { |
| 172 | bool keep_matching; |
| 173 | bool regex_compiled; |
| 174 | bool regex_has_slash; |
| 175 | mode_t mode; |
| 176 | int maj, min0, min1; |
| 177 | struct bb_uidgid_t ugid; |
| 178 | char *envvar; |
| 179 | char *ren_mov; |
| 180 | IF_FEATURE_MDEV_EXEC(char *r_cmd;) |
| 181 | regex_t match; |
| 182 | }; |
| 183 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 184 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 185 | int root_major, root_minor; |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 186 | char *subsystem; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 187 | #if ENABLE_FEATURE_MDEV_CONF |
| 188 | const char *filename; |
| 189 | parser_t *parser; |
| 190 | struct rule **rule_vec; |
| 191 | unsigned rule_idx; |
| 192 | #endif |
| 193 | struct rule cur_rule; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 194 | } FIX_ALIASING; |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 195 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 196 | #define INIT_G() do { } while (0) |
| 197 | |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 199 | /* Prevent infinite loops in /sys symlinks */ |
| 200 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 202 | /* We use additional 64+ bytes in make_device() */ |
| 203 | #define SCRATCH_SIZE 80 |
| 204 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 205 | #if 0 |
| 206 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 207 | #else |
| 208 | # define dbg(...) ((void)0) |
| 209 | #endif |
| 210 | |
| 211 | |
| 212 | #if ENABLE_FEATURE_MDEV_CONF |
| 213 | |
| 214 | static void make_default_cur_rule(void) |
| 215 | { |
| 216 | memset(&G.cur_rule, 0, sizeof(G.cur_rule)); |
| 217 | G.cur_rule.maj = -1; /* "not a @major,minor rule" */ |
| 218 | G.cur_rule.mode = 0660; |
| 219 | } |
| 220 | |
| 221 | static void clean_up_cur_rule(void) |
| 222 | { |
| 223 | free(G.cur_rule.envvar); |
| 224 | if (G.cur_rule.regex_compiled) |
| 225 | regfree(&G.cur_rule.match); |
| 226 | free(G.cur_rule.ren_mov); |
| 227 | IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);) |
| 228 | make_default_cur_rule(); |
| 229 | } |
| 230 | |
| 231 | static void parse_next_rule(void) |
| 232 | { |
| 233 | /* Note: on entry, G.cur_rule is set to default */ |
| 234 | while (1) { |
| 235 | char *tokens[4]; |
| 236 | char *val; |
| 237 | |
| 238 | if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) |
| 239 | break; |
| 240 | |
| 241 | /* Fields: [-]regex uid:gid mode [alias] [cmd] */ |
| 242 | dbg("token1:'%s'", tokens[1]); |
| 243 | |
| 244 | /* 1st field */ |
| 245 | val = tokens[0]; |
| 246 | G.cur_rule.keep_matching = ('-' == val[0]); |
| 247 | val += G.cur_rule.keep_matching; /* swallow leading dash */ |
| 248 | if (val[0] == '@') { |
| 249 | /* @major,minor[-minor2] */ |
| 250 | /* (useful when name is ambiguous: |
| 251 | * "/sys/class/usb/lp0" and |
| 252 | * "/sys/class/printer/lp0") |
| 253 | */ |
| 254 | int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1); |
| 255 | if (sc < 2 || G.cur_rule.maj < 0) { |
| 256 | bb_error_msg("bad @maj,min on line %d", G.parser->lineno); |
| 257 | goto next_rule; |
| 258 | } |
| 259 | if (sc == 2) |
| 260 | G.cur_rule.min1 = G.cur_rule.min0; |
| 261 | } else { |
| 262 | if (val[0] == '$') { |
| 263 | char *eq = strchr(++val, '='); |
| 264 | if (!eq) { |
| 265 | bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno); |
| 266 | goto next_rule; |
| 267 | } |
| 268 | G.cur_rule.envvar = xstrndup(val, eq - val); |
| 269 | val = eq + 1; |
| 270 | } |
| 271 | xregcomp(&G.cur_rule.match, val, REG_EXTENDED); |
| 272 | G.cur_rule.regex_compiled = 1; |
| 273 | G.cur_rule.regex_has_slash = (strchr(val, '/') != NULL); |
| 274 | } |
| 275 | |
| 276 | /* 2nd field: uid:gid - device ownership */ |
| 277 | if (get_uidgid(&G.cur_rule.ugid, tokens[1], /*allow_numeric:*/ 1) == 0) { |
| 278 | bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno); |
| 279 | goto next_rule; |
| 280 | } |
| 281 | |
| 282 | /* 3rd field: mode - device permissions */ |
| 283 | bb_parse_mode(tokens[2], &G.cur_rule.mode); |
| 284 | |
| 285 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ |
| 286 | val = tokens[3]; |
| 287 | if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) { |
| 288 | char *s = skip_non_whitespace(val); |
| 289 | G.cur_rule.ren_mov = xstrndup(val, s - val); |
| 290 | val = skip_whitespace(s); |
| 291 | } |
| 292 | |
| 293 | if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) { |
| 294 | const char *s = "$@*"; |
| 295 | const char *s2 = strchr(s, val[0]); |
| 296 | if (!s2) { |
| 297 | bb_error_msg("bad line %u", G.parser->lineno); |
| 298 | goto next_rule; |
| 299 | } |
| 300 | IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);) |
| 301 | } |
| 302 | |
| 303 | return; |
| 304 | next_rule: |
| 305 | clean_up_cur_rule(); |
| 306 | } /* while (config_read) */ |
| 307 | |
| 308 | dbg("config_close(G.parser)"); |
| 309 | config_close(G.parser); |
| 310 | G.parser = NULL; |
| 311 | |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | /* If mdev -s, we remember rules in G.rule_vec[]. |
| 316 | * Otherwise, there is no point in doing it, and we just |
| 317 | * save only one parsed rule in G.cur_rule. |
| 318 | */ |
| 319 | static const struct rule *next_rule(void) |
| 320 | { |
| 321 | struct rule *rule; |
| 322 | |
| 323 | /* Open conf file if we didn't do it yet */ |
| 324 | if (!G.parser && G.filename) { |
| 325 | dbg("config_open('%s')", G.filename); |
| 326 | G.parser = config_open2(G.filename, fopen_for_read); |
| 327 | G.filename = NULL; |
| 328 | } |
| 329 | |
| 330 | if (G.rule_vec) { |
| 331 | /* mdev -s */ |
| 332 | /* Do we have rule parsed already? */ |
| 333 | if (G.rule_vec[G.rule_idx]) { |
| 334 | dbg("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]); |
| 335 | return G.rule_vec[G.rule_idx++]; |
| 336 | } |
| 337 | make_default_cur_rule(); |
| 338 | } else { |
| 339 | /* not mdev -s */ |
| 340 | clean_up_cur_rule(); |
| 341 | } |
| 342 | |
| 343 | /* Parse one more rule if file isn't fully read */ |
| 344 | rule = &G.cur_rule; |
| 345 | if (G.parser) { |
| 346 | parse_next_rule(); |
| 347 | if (G.rule_vec) { /* mdev -s */ |
| 348 | rule = memcpy(xmalloc(sizeof(G.cur_rule)), &G.cur_rule, sizeof(G.cur_rule)); |
| 349 | G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx); |
| 350 | G.rule_vec[G.rule_idx++] = rule; |
| 351 | dbg("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return rule; |
| 356 | } |
| 357 | |
| 358 | #else |
| 359 | |
| 360 | # define next_rule() (&G.cur_rule) |
| 361 | |
| 362 | #endif |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 363 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 364 | /* Builds an alias path. |
| 365 | * This function potentionally reallocates the alias parameter. |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 366 | * Only used for ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 367 | */ |
| 368 | static char *build_alias(char *alias, const char *device_name) |
| 369 | { |
| 370 | char *dest; |
| 371 | |
| 372 | /* ">bar/": rename to bar/device_name */ |
| 373 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 374 | dest = strrchr(alias, '/'); |
| 375 | if (dest) { /* ">bar/[baz]" ? */ |
| 376 | *dest = '\0'; /* mkdir bar */ |
| 377 | bb_make_directory(alias, 0755, FILEUTILS_RECUR); |
| 378 | *dest = '/'; |
| 379 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 380 | dest = alias; |
| 381 | alias = concat_path_file(alias, device_name); |
| 382 | free(dest); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return alias; |
| 387 | } |
| 388 | |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 389 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" |
| 390 | * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes |
| 391 | * after NUL, but we promise to not mangle (IOW: to restore if needed) |
| 392 | * path string. |
| 393 | * NB2: "mdev -s" may call us many times, do not leak memory/fds! |
| 394 | */ |
Rob Landley | ef10d52 | 2006-06-26 14:11:33 +0000 | [diff] [blame] | 395 | static void make_device(char *path, int delete) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 396 | { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 397 | char *device_name, *subsystem_slash_devname; |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 398 | int major, minor, type, len; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 399 | |
| 400 | dbg("%s('%s', delete:%d)", __func__, path, delete); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 401 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 402 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 403 | * the data, so we don't need to worry about null terminating the string |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 404 | * because sscanf() will stop at the first nondigit, which \n is. |
| 405 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 406 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 407 | major = -1; |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 408 | if (!delete) { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 409 | char *dev_maj_min = path + strlen(path); |
| 410 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 411 | strcpy(dev_maj_min, "/dev"); |
| 412 | len = open_read_close(path, dev_maj_min + 1, 64); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 413 | *dev_maj_min = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 414 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 415 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 416 | return; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 417 | /* no "dev" file, but we can still run scripts |
| 418 | * based on device name */ |
| 419 | } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 420 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 421 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 422 | } |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 423 | /* else: for delete, -1 still deletes the node, but < -1 suppresses that */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 424 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 425 | /* Determine device name, type, major and minor */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 426 | device_name = (char*) bb_basename(path); |
Denis Vlasenko | aa8a601 | 2008-03-29 16:07:46 +0000 | [diff] [blame] | 427 | /* http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 428 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 429 | * But since 2.6.25 block devices are also in /sys/class/block. |
| 430 | * We use strstr("/block/") to forestall future surprises. */ |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 431 | type = S_IFCHR; |
Denys Vlasenko | 8d4a8d1 | 2010-03-16 18:37:19 +0100 | [diff] [blame] | 432 | if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0)) |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 433 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 434 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 435 | /* Make path point to "subsystem/device_name" */ |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 436 | subsystem_slash_devname = NULL; |
| 437 | /* Check for coldplug invocations first */ |
| 438 | if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */ |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 439 | path += sizeof("/sys/") - 1; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 440 | else if (strncmp(path, "/sys/class/", 11) == 0) |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 441 | path += sizeof("/sys/class/") - 1; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 442 | else { |
| 443 | /* Example of a hotplug invocation: |
| 444 | * SUBSYSTEM="block" |
| 445 | * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3" |
| 446 | * ("/sys" is added by mdev_main) |
| 447 | * - path does not contain subsystem |
| 448 | */ |
| 449 | subsystem_slash_devname = concat_path_file(G.subsystem, device_name); |
| 450 | path = subsystem_slash_devname; |
| 451 | } |
Denis Vlasenko | 6dda5de | 2009-04-16 21:42:12 +0000 | [diff] [blame] | 452 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 453 | #if ENABLE_FEATURE_MDEV_CONF |
| 454 | G.rule_idx = 0; /* restart from the beginning (think mdev -s) */ |
| 455 | #endif |
| 456 | for (;;) { |
| 457 | const char *str_to_match; |
| 458 | regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 459 | char *command; |
| 460 | char *alias; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 461 | char aliaslink = aliaslink; /* for compiler */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 462 | const char *node_name; |
| 463 | const struct rule *rule; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 464 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 465 | str_to_match = ""; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 466 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 467 | rule = next_rule(); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 468 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 469 | #if ENABLE_FEATURE_MDEV_CONF |
| 470 | if (rule->maj >= 0) { /* @maj,min rule */ |
| 471 | if (major != rule->maj) |
| 472 | continue; |
| 473 | if (minor < rule->min0 || minor > rule->min1) |
| 474 | continue; |
| 475 | memset(off, 0, sizeof(off)); |
| 476 | goto rule_matches; |
| 477 | } |
| 478 | if (rule->envvar) { /* $envvar=regex rule */ |
| 479 | str_to_match = getenv(rule->envvar); |
| 480 | dbg("getenv('%s'):'%s'", rule->envvar, str_to_match); |
| 481 | if (!str_to_match) |
| 482 | continue; |
| 483 | } else { |
| 484 | /* regex to match [subsystem/]device_name */ |
| 485 | str_to_match = (rule->regex_has_slash ? path : device_name); |
| 486 | } |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 487 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 488 | if (rule->regex_compiled) { |
| 489 | int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0); |
| 490 | dbg("regex_match for '%s':%d", str_to_match, regex_match); |
| 491 | //bb_error_msg("matches:"); |
| 492 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 493 | // if (off[i].rm_so < 0) continue; |
| 494 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 495 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 496 | // device_name + off[i].rm_so); |
| 497 | //} |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 498 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 499 | if (regex_match != 0 |
| 500 | /* regexec returns whole pattern as "range" 0 */ |
| 501 | || off[0].rm_so != 0 |
| 502 | || (int)off[0].rm_eo != (int)strlen(str_to_match) |
| 503 | ) { |
| 504 | continue; /* this rule doesn't match */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 505 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 506 | } |
| 507 | /* else: it's final implicit "match-all" rule */ |
| 508 | #endif |
| 509 | |
| 510 | rule_matches: |
| 511 | dbg("rule matched"); |
| 512 | |
| 513 | /* Build alias name */ |
| 514 | alias = NULL; |
| 515 | if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) { |
| 516 | aliaslink = rule->ren_mov[0]; |
| 517 | if (aliaslink == '!') { |
| 518 | /* "!": suppress node creation/deletion */ |
| 519 | major = -2; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 520 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 521 | else if (aliaslink == '>' || aliaslink == '=') { |
| 522 | if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) { |
| 523 | char *s; |
| 524 | char *p; |
| 525 | unsigned n; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 526 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 527 | /* substitute %1..9 with off[1..9], if any */ |
| 528 | n = 0; |
| 529 | s = rule->ren_mov; |
| 530 | while (*s) |
| 531 | if (*s++ == '%') |
| 532 | n++; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 533 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 534 | p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match)); |
| 535 | s = rule->ren_mov + 1; |
| 536 | while (*s) { |
| 537 | *p = *s; |
| 538 | if ('%' == *s) { |
| 539 | unsigned i = (s[1] - '0'); |
| 540 | if (i <= 9 && off[i].rm_so >= 0) { |
| 541 | n = off[i].rm_eo - off[i].rm_so; |
| 542 | strncpy(p, str_to_match + off[i].rm_so, n); |
| 543 | p += n - 1; |
| 544 | s++; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 545 | } |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 546 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 547 | p++; |
| 548 | s++; |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 549 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 550 | } else { |
| 551 | alias = xstrdup(rule->ren_mov + 1); |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 552 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 553 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 554 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 555 | dbg("alias:'%s'", alias); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 556 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 557 | command = NULL; |
| 558 | IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;) |
| 559 | if (command) { |
| 560 | const char *s = "$@*"; |
| 561 | const char *s2 = strchr(s, command[0]); |
| 562 | |
| 563 | /* Are we running this command now? |
| 564 | * Run $cmd on delete, @cmd on create, *cmd on both |
| 565 | */ |
| 566 | if (s2 - s != delete) { |
| 567 | /* We are here if: '*', |
| 568 | * or: '@' and delete = 0, |
| 569 | * or: '$' and delete = 1 |
| 570 | */ |
| 571 | command++; |
| 572 | } else { |
| 573 | command = NULL; |
| 574 | } |
| 575 | } |
| 576 | dbg("command:'%s'", command); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 577 | |
| 578 | /* "Execute" the line we found */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 579 | node_name = device_name; |
| 580 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 581 | node_name = alias = build_alias(alias, device_name); |
| 582 | dbg("alias2:'%s'", alias); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 585 | if (!delete && major >= 0) { |
| 586 | dbg("mknod('%s',%o,(%d,%d))", node_name, rule->mode | type, major, minor); |
| 587 | if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST) |
| 588 | bb_perror_msg("can't create '%s'", node_name); |
| 589 | if (major == G.root_major && minor == G.root_minor) |
| 590 | symlink(node_name, "root"); |
| 591 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 592 | chmod(node_name, rule->mode); |
| 593 | chown(node_name, rule->ugid.uid, rule->ugid.gid); |
| 594 | } |
| 595 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 596 | if (aliaslink == '>') |
| 597 | symlink(node_name, device_name); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (ENABLE_FEATURE_MDEV_EXEC && command) { |
| 602 | /* setenv will leak memory, use putenv/unsetenv/free */ |
| 603 | char *s = xasprintf("%s=%s", "MDEV", node_name); |
| 604 | char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem); |
| 605 | putenv(s); |
| 606 | putenv(s1); |
| 607 | if (system(command) == -1) |
| 608 | bb_perror_msg("can't run '%s'", command); |
| 609 | bb_unsetenv_and_free(s1); |
| 610 | bb_unsetenv_and_free(s); |
| 611 | } |
| 612 | |
| 613 | if (delete && major >= -1) { |
| 614 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 615 | if (aliaslink == '>') |
| 616 | unlink(device_name); |
| 617 | } |
| 618 | unlink(node_name); |
| 619 | } |
| 620 | |
| 621 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 622 | free(alias); |
| 623 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 624 | /* We found matching line. |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 625 | * Stop unless it was prefixed with '-' |
| 626 | */ |
| 627 | if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching) |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 628 | break; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 629 | } /* for (;;) */ |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 630 | |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 631 | free(subsystem_slash_devname); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 634 | /* File callback for /sys/ traversal */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 635 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 636 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 637 | void *userData, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 638 | int depth UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 639 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 640 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 641 | char *scratch = userData; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 642 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 643 | /* len check is for paranoid reasons */ |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 644 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 645 | return FALSE; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 646 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 647 | strcpy(scratch, fileName); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 648 | scratch[len] = '\0'; |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 649 | make_device(scratch, /*delete:*/ 0); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 650 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 651 | return TRUE; |
| 652 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 653 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 654 | /* Directory callback for /sys/ traversal */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 655 | static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM, |
| 656 | struct stat *statbuf UNUSED_PARAM, |
| 657 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 658 | int depth) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 659 | { |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 660 | /* Extract device subsystem -- the name of the directory |
| 661 | * under /sys/class/ */ |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 662 | if (1 == depth) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 663 | free(G.subsystem); |
| 664 | G.subsystem = strrchr(fileName, '/'); |
| 665 | if (G.subsystem) |
| 666 | G.subsystem = xstrdup(G.subsystem + 1); |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 669 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 672 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 673 | * |
| 674 | * Firmware loading works like this: |
| 675 | * - kernel sets FIRMWARE env var |
| 676 | * - userspace checks /lib/firmware/$FIRMWARE |
| 677 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 678 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 679 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 680 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 681 | * - kernel loads firmware into device |
| 682 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 683 | static void load_firmware(const char *firmware, const char *sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 684 | { |
| 685 | int cnt; |
| 686 | int firmware_fd, loading_fd, data_fd; |
| 687 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 688 | /* check for /lib/firmware/$FIRMWARE */ |
| 689 | xchdir("/lib/firmware"); |
Mike Frysinger | 0e0639b | 2007-06-14 09:29:48 +0000 | [diff] [blame] | 690 | firmware_fd = xopen(firmware, O_RDONLY); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 691 | |
| 692 | /* in case we goto out ... */ |
| 693 | data_fd = -1; |
| 694 | |
| 695 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 696 | xchdir(sysfs_path); |
| 697 | for (cnt = 0; cnt < 30; ++cnt) { |
| 698 | loading_fd = open("loading", O_WRONLY); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 699 | if (loading_fd != -1) |
| 700 | goto loading; |
| 701 | sleep(1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 702 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 703 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 704 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 705 | loading: |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 706 | /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 707 | if (full_write(loading_fd, "1", 1) != 1) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 708 | goto out; |
| 709 | |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 710 | /* load firmware into /sys/$DEVPATH/data */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 711 | data_fd = open("data", O_WRONLY); |
| 712 | if (data_fd == -1) |
| 713 | goto out; |
| 714 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 715 | |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 716 | /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 717 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 718 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 719 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 720 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 721 | |
| 722 | out: |
| 723 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 724 | close(firmware_fd); |
| 725 | close(loading_fd); |
| 726 | close(data_fd); |
| 727 | } |
| 728 | } |
| 729 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 730 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 731 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 732 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 733 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 734 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 735 | INIT_G(); |
| 736 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 737 | #if ENABLE_FEATURE_MDEV_CONF |
| 738 | G.filename = "/etc/mdev.conf"; |
| 739 | #endif |
| 740 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 741 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 742 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 743 | bb_sanitize_stdio(); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 744 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 745 | /* Force the configuration file settings exactly */ |
| 746 | umask(0); |
| 747 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 748 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 749 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 750 | if (argv[1] && strcmp(argv[1], "-s") == 0) { |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 751 | /* Scan: |
| 752 | * mdev -s |
| 753 | */ |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 754 | struct stat st; |
| 755 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame^] | 756 | #if ENABLE_FEATURE_MDEV_CONF |
| 757 | /* Same as xrealloc_vector(NULL, 4, 0): */ |
| 758 | G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec)); |
| 759 | #endif |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 760 | xstat("/", &st); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 761 | G.root_major = major(st.st_dev); |
| 762 | G.root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 763 | |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 764 | /* ACTION_FOLLOWLINKS is needed since in newer kernels |
| 765 | * /sys/block/loop* (for example) are symlinks to dirs, |
| 766 | * not real directories. |
| 767 | * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 768 | * but we can't enforce that on users) |
| 769 | */ |
| 770 | if (access("/sys/class/block", F_OK) != 0) { |
| 771 | /* Scan obsolete /sys/block only if /sys/class/block |
| 772 | * doesn't exist. Otherwise we'll have dupes. |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 773 | * Also, do not complain if it doesn't exist. |
| 774 | * Some people configure kernel to have no blockdevs. |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 775 | */ |
| 776 | recursive_action("/sys/block", |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 777 | ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 778 | fileAction, dirAction, temp, 0); |
| 779 | } |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 780 | recursive_action("/sys/class", |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 781 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 782 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 783 | } else { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 784 | char *fw; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 785 | char *seq; |
| 786 | char *action; |
| 787 | char *env_path; |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 788 | static const char keywords[] ALIGN1 = "remove\0add\0"; |
| 789 | enum { OP_remove = 0, OP_add }; |
| 790 | smalluint op; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 791 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 792 | /* Hotplug: |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 793 | * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 794 | * ACTION can be "add" or "remove" |
| 795 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 796 | */ |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 797 | action = getenv("ACTION"); |
| 798 | env_path = getenv("DEVPATH"); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 799 | G.subsystem = getenv("SUBSYSTEM"); |
| 800 | if (!action || !env_path /*|| !G.subsystem*/) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 801 | bb_show_usage(); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 802 | fw = getenv("FIRMWARE"); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 803 | op = index_in_strings(keywords, action); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 804 | /* If it exists, does /dev/mdev.seq match $SEQNUM? |
| 805 | * If it does not match, earlier mdev is running |
| 806 | * in parallel, and we need to wait */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 807 | seq = getenv("SEQNUM"); |
| 808 | if (seq) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 809 | int timeout = 2000 / 32; /* 2000 msec */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 810 | do { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 811 | int seqlen; |
| 812 | char seqbuf[sizeof(int)*3 + 2]; |
| 813 | |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 814 | seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1)); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 815 | if (seqlen < 0) { |
| 816 | seq = NULL; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 817 | break; |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 818 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 819 | seqbuf[seqlen] = '\0'; |
| 820 | if (seqbuf[0] == '\n' /* seed file? */ |
| 821 | || strcmp(seq, seqbuf) == 0 /* correct idx? */ |
| 822 | ) { |
| 823 | break; |
| 824 | } |
| 825 | usleep(32*1000); |
| 826 | } while (--timeout); |
| 827 | } |
| 828 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 829 | snprintf(temp, PATH_MAX, "/sys%s", env_path); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 830 | if (op == OP_remove) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 831 | /* Ignoring "remove firmware". It was reported |
| 832 | * to happen and to cause erroneous deletion |
| 833 | * of device nodes. */ |
| 834 | if (!fw) |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 835 | make_device(temp, /*delete:*/ 1); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 836 | } |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 837 | else if (op == OP_add) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 838 | make_device(temp, /*delete:*/ 0); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 839 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 840 | if (fw) |
| 841 | load_firmware(fw, temp); |
| 842 | } |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 843 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 844 | |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 845 | if (seq) { |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 846 | xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1)); |
| 847 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 850 | if (ENABLE_FEATURE_CLEAN_UP) |
| 851 | RELEASE_CONFIG_BUFFER(temp); |
| 852 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 853 | return EXIT_SUCCESS; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 854 | } |