"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" |
Denys Vlasenko | 8d11854 | 2012-05-18 04:47:18 +0200 | [diff] [blame] | 95 | //usage: "To activate this feature, create empty /dev/mdev.seq at boot.\n" |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 96 | //usage: "\n" |
Denys Vlasenko | 8d11854 | 2012-05-18 04:47:18 +0200 | [diff] [blame] | 97 | //usage: "If /dev/mdev.log file exists, debug log will be appended to it." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 98 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 99 | #include "libbb.h" |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 100 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 102 | /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev |
| 103 | * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev |
| 104 | * contains "4:0\n". Directory name is taken as device name, path component |
| 105 | * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty". |
| 106 | * Then mdev creates the /dev/device_name node. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 107 | * If /sys/class/.../dev file does not exist, mdev still may act |
| 108 | * on this device: see "@|$|*command args..." parameter in config file. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 109 | * |
| 110 | * mdev w/o parameters is called as hotplug helper. It takes device |
| 111 | * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts |
| 112 | * maj,min from "/sys/$DEVPATH/dev" and also examines |
| 113 | * $ACTION ("add"/"delete") and $FIRMWARE. |
| 114 | * |
| 115 | * If action is "add", mdev creates /dev/device_name similarly to mdev -s. |
| 116 | * (todo: explain "delete" and $FIRMWARE) |
| 117 | * |
| 118 | * If /etc/mdev.conf exists, it may modify /dev/device_name's properties. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 119 | * |
| 120 | * Leading minus in 1st field means "don't stop on this line", otherwise |
| 121 | * search is stopped after the matching line is encountered. |
| 122 | * |
Denys Vlasenko | 45f0a11 | 2011-09-22 04:55:47 +0200 | [diff] [blame] | 123 | * $envvar=regex format is useful for loading modules for hot-plugged devices |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 124 | * which do not have driver loaded yet. In this case /sys/class/.../dev |
| 125 | * does not exist, but $MODALIAS is set to needed module's name |
| 126 | * (actually, an alias to it) by kernel. This rule instructs mdev |
| 127 | * to load the module and exit: |
| 128 | * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS" |
| 129 | * The kernel will generate another hotplug event when /sys/class/.../dev |
| 130 | * file appears. |
| 131 | * |
| 132 | * When line matches, the device node is created, chmod'ed and chown'ed, |
| 133 | * moved to path, and if >path, a symlink to moved node is created, |
| 134 | * all this if /sys/class/.../dev exists. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 135 | * Examples: |
| 136 | * =loop/ - moves to /dev/loop |
| 137 | * >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] | 138 | * |
| 139 | * Then "command args..." is executed (via sh -c 'command args...'). |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 140 | * @:execute on creation, $:on deletion, *:on both. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 141 | * This happens regardless of /sys/class/.../dev existence. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 142 | */ |
| 143 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 144 | /* Kernel's hotplug environment constantly changes. |
| 145 | * Here are new cases I observed on 3.1.0: |
| 146 | * |
| 147 | * Case with $DEVNAME and $DEVICE, not just $DEVPATH: |
| 148 | * ACTION=add |
| 149 | * BUSNUM=001 |
| 150 | * DEVICE=/proc/bus/usb/001/003 |
| 151 | * DEVNAME=bus/usb/001/003 |
| 152 | * DEVNUM=003 |
| 153 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5 |
| 154 | * DEVTYPE=usb_device |
| 155 | * MAJOR=189 |
| 156 | * MINOR=2 |
| 157 | * PRODUCT=18d1/4e12/227 |
| 158 | * SUBSYSTEM=usb |
| 159 | * TYPE=0/0/0 |
| 160 | * |
| 161 | * Case with $DEVICE, but no $DEVNAME - apparenty, usb iface notification? |
| 162 | * "Please load me a module" thing? |
| 163 | * ACTION=add |
| 164 | * DEVICE=/proc/bus/usb/001/003 |
| 165 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0 |
| 166 | * DEVTYPE=usb_interface |
| 167 | * INTERFACE=8/6/80 |
| 168 | * MODALIAS=usb:v18D1p4E12d0227dc00dsc00dp00ic08isc06ip50 |
| 169 | * PRODUCT=18d1/4e12/227 |
| 170 | * SUBSYSTEM=usb |
| 171 | * TYPE=0/0/0 |
| 172 | * |
| 173 | * ACTION=add |
| 174 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5 |
| 175 | * DEVTYPE=scsi_host |
| 176 | * SUBSYSTEM=scsi |
| 177 | * |
| 178 | * ACTION=add |
| 179 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/scsi_host/host5 |
| 180 | * SUBSYSTEM=scsi_host |
| 181 | * |
| 182 | * ACTION=add |
| 183 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0 |
| 184 | * DEVTYPE=scsi_target |
| 185 | * SUBSYSTEM=scsi |
| 186 | * |
| 187 | * Case with strange $MODALIAS: |
| 188 | * ACTION=add |
| 189 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0 |
| 190 | * DEVTYPE=scsi_device |
| 191 | * MODALIAS=scsi:t-0x00 |
| 192 | * SUBSYSTEM=scsi |
| 193 | * |
| 194 | * ACTION=add |
| 195 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0/scsi_disk/5:0:0:0 |
| 196 | * SUBSYSTEM=scsi_disk |
| 197 | * |
| 198 | * ACTION=add |
| 199 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0/scsi_device/5:0:0:0 |
| 200 | * SUBSYSTEM=scsi_device |
| 201 | * |
| 202 | * Case with explicit $MAJOR/$MINOR (no need to read /sys/$DEVPATH/dev?): |
| 203 | * ACTION=add |
| 204 | * DEVNAME=bsg/5:0:0:0 |
| 205 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0/bsg/5:0:0:0 |
| 206 | * MAJOR=253 |
| 207 | * MINOR=1 |
| 208 | * SUBSYSTEM=bsg |
| 209 | * |
| 210 | * ACTION=add |
| 211 | * DEVPATH=/devices/virtual/bdi/8:16 |
| 212 | * SUBSYSTEM=bdi |
| 213 | * |
| 214 | * ACTION=add |
| 215 | * DEVNAME=sdb |
| 216 | * DEVPATH=/block/sdb |
| 217 | * DEVTYPE=disk |
| 218 | * MAJOR=8 |
| 219 | * MINOR=16 |
| 220 | * SUBSYSTEM=block |
| 221 | * |
| 222 | * Case with ACTION=change: |
| 223 | * ACTION=change |
| 224 | * DEVNAME=sdb |
| 225 | * DEVPATH=/block/sdb |
| 226 | * DEVTYPE=disk |
| 227 | * DISK_MEDIA_CHANGE=1 |
| 228 | * MAJOR=8 |
| 229 | * MINOR=16 |
| 230 | * SUBSYSTEM=block |
| 231 | */ |
| 232 | |
| 233 | static const char keywords[] ALIGN1 = "add\0remove\0change\0"; |
| 234 | enum { OP_add, OP_remove }; |
| 235 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 236 | struct rule { |
| 237 | bool keep_matching; |
| 238 | bool regex_compiled; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 239 | mode_t mode; |
| 240 | int maj, min0, min1; |
| 241 | struct bb_uidgid_t ugid; |
| 242 | char *envvar; |
| 243 | char *ren_mov; |
| 244 | IF_FEATURE_MDEV_EXEC(char *r_cmd;) |
| 245 | regex_t match; |
| 246 | }; |
| 247 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 248 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 249 | int root_major, root_minor; |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 250 | smallint verbose; |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 251 | char *subsystem; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 252 | #if ENABLE_FEATURE_MDEV_CONF |
| 253 | const char *filename; |
| 254 | parser_t *parser; |
| 255 | struct rule **rule_vec; |
| 256 | unsigned rule_idx; |
| 257 | #endif |
| 258 | struct rule cur_rule; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 259 | } FIX_ALIASING; |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 260 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denys Vlasenko | c966563 | 2012-04-21 14:35:32 +0200 | [diff] [blame] | 261 | #define INIT_G() do { \ |
| 262 | IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \ |
| 263 | IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \ |
| 264 | } while (0) |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 265 | |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 266 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 267 | /* Prevent infinite loops in /sys symlinks */ |
| 268 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 270 | /* We use additional 64+ bytes in make_device() */ |
| 271 | #define SCRATCH_SIZE 80 |
| 272 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 273 | #if 0 |
| 274 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 275 | #else |
| 276 | # define dbg(...) ((void)0) |
| 277 | #endif |
| 278 | |
| 279 | |
| 280 | #if ENABLE_FEATURE_MDEV_CONF |
| 281 | |
| 282 | static void make_default_cur_rule(void) |
| 283 | { |
| 284 | memset(&G.cur_rule, 0, sizeof(G.cur_rule)); |
| 285 | G.cur_rule.maj = -1; /* "not a @major,minor rule" */ |
| 286 | G.cur_rule.mode = 0660; |
| 287 | } |
| 288 | |
| 289 | static void clean_up_cur_rule(void) |
| 290 | { |
| 291 | free(G.cur_rule.envvar); |
| 292 | if (G.cur_rule.regex_compiled) |
| 293 | regfree(&G.cur_rule.match); |
| 294 | free(G.cur_rule.ren_mov); |
| 295 | IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);) |
| 296 | make_default_cur_rule(); |
| 297 | } |
| 298 | |
| 299 | static void parse_next_rule(void) |
| 300 | { |
| 301 | /* Note: on entry, G.cur_rule is set to default */ |
| 302 | while (1) { |
| 303 | char *tokens[4]; |
| 304 | char *val; |
| 305 | |
Denys Vlasenko | 499597d | 2012-01-11 00:37:17 +0100 | [diff] [blame] | 306 | /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */ |
| 307 | if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL & ~PARSE_EOL_COMMENTS)) |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 308 | break; |
| 309 | |
| 310 | /* Fields: [-]regex uid:gid mode [alias] [cmd] */ |
| 311 | dbg("token1:'%s'", tokens[1]); |
| 312 | |
| 313 | /* 1st field */ |
| 314 | val = tokens[0]; |
| 315 | G.cur_rule.keep_matching = ('-' == val[0]); |
| 316 | val += G.cur_rule.keep_matching; /* swallow leading dash */ |
| 317 | if (val[0] == '@') { |
| 318 | /* @major,minor[-minor2] */ |
| 319 | /* (useful when name is ambiguous: |
| 320 | * "/sys/class/usb/lp0" and |
| 321 | * "/sys/class/printer/lp0") |
| 322 | */ |
| 323 | int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1); |
| 324 | if (sc < 2 || G.cur_rule.maj < 0) { |
| 325 | bb_error_msg("bad @maj,min on line %d", G.parser->lineno); |
| 326 | goto next_rule; |
| 327 | } |
| 328 | if (sc == 2) |
| 329 | G.cur_rule.min1 = G.cur_rule.min0; |
| 330 | } else { |
| 331 | if (val[0] == '$') { |
| 332 | char *eq = strchr(++val, '='); |
| 333 | if (!eq) { |
| 334 | bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno); |
| 335 | goto next_rule; |
| 336 | } |
| 337 | G.cur_rule.envvar = xstrndup(val, eq - val); |
| 338 | val = eq + 1; |
| 339 | } |
| 340 | xregcomp(&G.cur_rule.match, val, REG_EXTENDED); |
| 341 | G.cur_rule.regex_compiled = 1; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* 2nd field: uid:gid - device ownership */ |
| 345 | if (get_uidgid(&G.cur_rule.ugid, tokens[1], /*allow_numeric:*/ 1) == 0) { |
| 346 | bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno); |
| 347 | goto next_rule; |
| 348 | } |
| 349 | |
| 350 | /* 3rd field: mode - device permissions */ |
| 351 | bb_parse_mode(tokens[2], &G.cur_rule.mode); |
| 352 | |
| 353 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ |
| 354 | val = tokens[3]; |
| 355 | if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) { |
| 356 | char *s = skip_non_whitespace(val); |
| 357 | G.cur_rule.ren_mov = xstrndup(val, s - val); |
| 358 | val = skip_whitespace(s); |
| 359 | } |
| 360 | |
| 361 | if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) { |
| 362 | const char *s = "$@*"; |
| 363 | const char *s2 = strchr(s, val[0]); |
| 364 | if (!s2) { |
| 365 | bb_error_msg("bad line %u", G.parser->lineno); |
| 366 | goto next_rule; |
| 367 | } |
| 368 | IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);) |
| 369 | } |
| 370 | |
| 371 | return; |
| 372 | next_rule: |
| 373 | clean_up_cur_rule(); |
| 374 | } /* while (config_read) */ |
| 375 | |
| 376 | dbg("config_close(G.parser)"); |
| 377 | config_close(G.parser); |
| 378 | G.parser = NULL; |
| 379 | |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | /* If mdev -s, we remember rules in G.rule_vec[]. |
| 384 | * Otherwise, there is no point in doing it, and we just |
| 385 | * save only one parsed rule in G.cur_rule. |
| 386 | */ |
| 387 | static const struct rule *next_rule(void) |
| 388 | { |
| 389 | struct rule *rule; |
| 390 | |
| 391 | /* Open conf file if we didn't do it yet */ |
| 392 | if (!G.parser && G.filename) { |
| 393 | dbg("config_open('%s')", G.filename); |
| 394 | G.parser = config_open2(G.filename, fopen_for_read); |
| 395 | G.filename = NULL; |
| 396 | } |
| 397 | |
| 398 | if (G.rule_vec) { |
| 399 | /* mdev -s */ |
| 400 | /* Do we have rule parsed already? */ |
| 401 | if (G.rule_vec[G.rule_idx]) { |
| 402 | dbg("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]); |
| 403 | return G.rule_vec[G.rule_idx++]; |
| 404 | } |
| 405 | make_default_cur_rule(); |
| 406 | } else { |
| 407 | /* not mdev -s */ |
| 408 | clean_up_cur_rule(); |
| 409 | } |
| 410 | |
| 411 | /* Parse one more rule if file isn't fully read */ |
| 412 | rule = &G.cur_rule; |
| 413 | if (G.parser) { |
| 414 | parse_next_rule(); |
| 415 | if (G.rule_vec) { /* mdev -s */ |
| 416 | rule = memcpy(xmalloc(sizeof(G.cur_rule)), &G.cur_rule, sizeof(G.cur_rule)); |
| 417 | G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx); |
| 418 | G.rule_vec[G.rule_idx++] = rule; |
| 419 | dbg("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return rule; |
| 424 | } |
| 425 | |
| 426 | #else |
| 427 | |
| 428 | # define next_rule() (&G.cur_rule) |
| 429 | |
| 430 | #endif |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 431 | |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 432 | static void mkdir_recursive(char *name) |
| 433 | { |
| 434 | /* if name has many levels ("dir1/dir2"), |
| 435 | * bb_make_directory() will create dir1 according to umask, |
| 436 | * not according to its "mode" parameter. |
| 437 | * Since we run with umask=0, need to temporarily switch it. |
| 438 | */ |
| 439 | umask(022); /* "dir1" (if any) will be 0755 too */ |
| 440 | bb_make_directory(name, 0755, FILEUTILS_RECUR); |
| 441 | umask(0); |
| 442 | } |
| 443 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 444 | /* Builds an alias path. |
| 445 | * This function potentionally reallocates the alias parameter. |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 446 | * Only used for ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 447 | */ |
| 448 | static char *build_alias(char *alias, const char *device_name) |
| 449 | { |
| 450 | char *dest; |
| 451 | |
| 452 | /* ">bar/": rename to bar/device_name */ |
| 453 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 454 | dest = strrchr(alias, '/'); |
| 455 | if (dest) { /* ">bar/[baz]" ? */ |
| 456 | *dest = '\0'; /* mkdir bar */ |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 457 | mkdir_recursive(alias); |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 458 | *dest = '/'; |
| 459 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 460 | dest = alias; |
| 461 | alias = concat_path_file(alias, device_name); |
| 462 | free(dest); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return alias; |
| 467 | } |
| 468 | |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 469 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" |
| 470 | * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes |
| 471 | * after NUL, but we promise to not mangle (IOW: to restore if needed) |
| 472 | * path string. |
| 473 | * NB2: "mdev -s" may call us many times, do not leak memory/fds! |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 474 | * |
| 475 | * device_name = $DEVNAME (may be NULL) |
| 476 | * path = /sys/$DEVPATH |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 477 | */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 478 | static void make_device(char *device_name, char *path, int operation) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 479 | { |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 480 | int major, minor, type, len; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 481 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 482 | if (G.verbose) |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 483 | bb_error_msg("device: %s, %s", device_name, path); |
Mike Frysinger | f0044c4 | 2008-02-01 06:53:50 +0000 | [diff] [blame] | 484 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 485 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 486 | * 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] | 487 | * because sscanf() will stop at the first nondigit, which \n is. |
| 488 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 489 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 490 | major = -1; |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 491 | if (operation == OP_add) { |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 492 | char *dev_maj_min = path + strlen(path); |
| 493 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 494 | strcpy(dev_maj_min, "/dev"); |
| 495 | len = open_read_close(path, dev_maj_min + 1, 64); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 496 | *dev_maj_min = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 497 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 498 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 499 | return; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 500 | /* no "dev" file, but we can still run scripts |
| 501 | * based on device name */ |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 502 | } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) == 2) { |
| 503 | if (G.verbose) |
| 504 | bb_error_msg("maj,min: %u,%u", major, minor); |
| 505 | } else { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 506 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 507 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 508 | } |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 509 | /* else: for delete, -1 still deletes the node, but < -1 suppresses that */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 510 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 511 | /* Determine device name, type, major and minor */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 512 | if (!device_name) |
| 513 | device_name = (char*) bb_basename(path); |
Denis Vlasenko | aa8a601 | 2008-03-29 16:07:46 +0000 | [diff] [blame] | 514 | /* http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 515 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 516 | * But since 2.6.25 block devices are also in /sys/class/block. |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 517 | * We use strstr("/block/") to forestall future surprises. |
| 518 | */ |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 519 | type = S_IFCHR; |
Denys Vlasenko | 8d4a8d1 | 2010-03-16 18:37:19 +0100 | [diff] [blame] | 520 | if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0)) |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 521 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 522 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 523 | #if ENABLE_FEATURE_MDEV_CONF |
| 524 | G.rule_idx = 0; /* restart from the beginning (think mdev -s) */ |
| 525 | #endif |
| 526 | for (;;) { |
| 527 | const char *str_to_match; |
| 528 | regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 529 | char *command; |
| 530 | char *alias; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 531 | char aliaslink = aliaslink; /* for compiler */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 532 | char *node_name; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 533 | const struct rule *rule; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 534 | |
Denys Vlasenko | b6beada | 2012-05-18 15:11:16 +0200 | [diff] [blame] | 535 | str_to_match = device_name; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 536 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 537 | rule = next_rule(); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 538 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 539 | #if ENABLE_FEATURE_MDEV_CONF |
| 540 | if (rule->maj >= 0) { /* @maj,min rule */ |
| 541 | if (major != rule->maj) |
| 542 | continue; |
| 543 | if (minor < rule->min0 || minor > rule->min1) |
| 544 | continue; |
| 545 | memset(off, 0, sizeof(off)); |
| 546 | goto rule_matches; |
| 547 | } |
| 548 | if (rule->envvar) { /* $envvar=regex rule */ |
| 549 | str_to_match = getenv(rule->envvar); |
| 550 | dbg("getenv('%s'):'%s'", rule->envvar, str_to_match); |
| 551 | if (!str_to_match) |
| 552 | continue; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 553 | } |
Denys Vlasenko | b6beada | 2012-05-18 15:11:16 +0200 | [diff] [blame] | 554 | /* else: str_to_match = device_name */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 555 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 556 | if (rule->regex_compiled) { |
| 557 | int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0); |
| 558 | dbg("regex_match for '%s':%d", str_to_match, regex_match); |
| 559 | //bb_error_msg("matches:"); |
| 560 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 561 | // if (off[i].rm_so < 0) continue; |
| 562 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 563 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 564 | // device_name + off[i].rm_so); |
| 565 | //} |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 566 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 567 | if (regex_match != 0 |
| 568 | /* regexec returns whole pattern as "range" 0 */ |
| 569 | || off[0].rm_so != 0 |
| 570 | || (int)off[0].rm_eo != (int)strlen(str_to_match) |
| 571 | ) { |
| 572 | continue; /* this rule doesn't match */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 573 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 574 | } |
| 575 | /* else: it's final implicit "match-all" rule */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 576 | rule_matches: |
Denys Vlasenko | c966563 | 2012-04-21 14:35:32 +0200 | [diff] [blame] | 577 | #endif |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 578 | dbg("rule matched"); |
| 579 | |
| 580 | /* Build alias name */ |
| 581 | alias = NULL; |
| 582 | if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) { |
| 583 | aliaslink = rule->ren_mov[0]; |
| 584 | if (aliaslink == '!') { |
| 585 | /* "!": suppress node creation/deletion */ |
| 586 | major = -2; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 587 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 588 | else if (aliaslink == '>' || aliaslink == '=') { |
| 589 | if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) { |
| 590 | char *s; |
| 591 | char *p; |
| 592 | unsigned n; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 593 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 594 | /* substitute %1..9 with off[1..9], if any */ |
| 595 | n = 0; |
| 596 | s = rule->ren_mov; |
| 597 | while (*s) |
| 598 | if (*s++ == '%') |
| 599 | n++; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 600 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 601 | p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match)); |
| 602 | s = rule->ren_mov + 1; |
| 603 | while (*s) { |
| 604 | *p = *s; |
| 605 | if ('%' == *s) { |
| 606 | unsigned i = (s[1] - '0'); |
| 607 | if (i <= 9 && off[i].rm_so >= 0) { |
| 608 | n = off[i].rm_eo - off[i].rm_so; |
| 609 | strncpy(p, str_to_match + off[i].rm_so, n); |
| 610 | p += n - 1; |
| 611 | s++; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 612 | } |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 613 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 614 | p++; |
| 615 | s++; |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 616 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 617 | } else { |
| 618 | alias = xstrdup(rule->ren_mov + 1); |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 619 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 620 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 621 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 622 | dbg("alias:'%s'", alias); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 623 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 624 | command = NULL; |
| 625 | IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;) |
| 626 | if (command) { |
| 627 | const char *s = "$@*"; |
| 628 | const char *s2 = strchr(s, command[0]); |
| 629 | |
| 630 | /* Are we running this command now? |
| 631 | * Run $cmd on delete, @cmd on create, *cmd on both |
| 632 | */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 633 | if (s2 - s != (operation == OP_remove) || *s2 == '*') { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 634 | /* We are here if: '*', |
| 635 | * or: '@' and delete = 0, |
| 636 | * or: '$' and delete = 1 |
| 637 | */ |
| 638 | command++; |
| 639 | } else { |
| 640 | command = NULL; |
| 641 | } |
| 642 | } |
| 643 | dbg("command:'%s'", command); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 644 | |
| 645 | /* "Execute" the line we found */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 646 | node_name = device_name; |
| 647 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 648 | node_name = alias = build_alias(alias, device_name); |
| 649 | dbg("alias2:'%s'", alias); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 652 | if (operation == OP_add && major >= 0) { |
| 653 | char *slash = strrchr(node_name, '/'); |
| 654 | if (slash) { |
| 655 | *slash = '\0'; |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 656 | mkdir_recursive(node_name); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 657 | *slash = '/'; |
| 658 | } |
| 659 | if (G.verbose) |
| 660 | bb_error_msg("mknod: %s (%d,%d) %o", node_name, major, minor, rule->mode | type); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 661 | if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST) |
| 662 | bb_perror_msg("can't create '%s'", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 663 | if (ENABLE_FEATURE_MDEV_CONF) { |
Denys Vlasenko | 6e29d07 | 2013-01-30 16:51:22 +0100 | [diff] [blame] | 664 | if (G.verbose) |
| 665 | bb_error_msg("chmod: %o chown: %u:%u", rule->mode, rule->ugid.uid, rule->ugid.gid); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 666 | chmod(node_name, rule->mode); |
| 667 | chown(node_name, rule->ugid.uid, rule->ugid.gid); |
| 668 | } |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 669 | if (major == G.root_major && minor == G.root_minor) |
| 670 | symlink(node_name, "root"); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 671 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
Denys Vlasenko | 638dbc3 | 2012-02-06 01:00:00 +0100 | [diff] [blame] | 672 | if (aliaslink == '>') { |
| 673 | //TODO: on devtmpfs, device_name already exists and symlink() fails. |
| 674 | //End result is that instead of symlink, we have two nodes. |
| 675 | //What should be done? |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 676 | if (G.verbose) |
| 677 | bb_error_msg("symlink: %s", device_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 678 | symlink(node_name, device_name); |
Denys Vlasenko | 638dbc3 | 2012-02-06 01:00:00 +0100 | [diff] [blame] | 679 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | if (ENABLE_FEATURE_MDEV_EXEC && command) { |
| 684 | /* setenv will leak memory, use putenv/unsetenv/free */ |
| 685 | char *s = xasprintf("%s=%s", "MDEV", node_name); |
| 686 | char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem); |
| 687 | putenv(s); |
| 688 | putenv(s1); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 689 | if (G.verbose) |
| 690 | bb_error_msg("running: %s", command); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 691 | if (system(command) == -1) |
| 692 | bb_perror_msg("can't run '%s'", command); |
| 693 | bb_unsetenv_and_free(s1); |
| 694 | bb_unsetenv_and_free(s); |
| 695 | } |
| 696 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 697 | if (operation == OP_remove && major >= -1) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 698 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 699 | if (aliaslink == '>') { |
| 700 | if (G.verbose) |
| 701 | bb_error_msg("unlink: %s", device_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 702 | unlink(device_name); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 703 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 704 | } |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 705 | if (G.verbose) |
| 706 | bb_error_msg("unlink: %s", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 707 | unlink(node_name); |
| 708 | } |
| 709 | |
| 710 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 711 | free(alias); |
| 712 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 713 | /* We found matching line. |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 714 | * Stop unless it was prefixed with '-' |
| 715 | */ |
| 716 | if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching) |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 717 | break; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 718 | } /* for (;;) */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 721 | /* File callback for /sys/ traversal */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 722 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 723 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 724 | void *userData, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 725 | int depth UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 726 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 727 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 728 | char *scratch = userData; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 729 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 730 | /* len check is for paranoid reasons */ |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 731 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 732 | return FALSE; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 733 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 734 | strcpy(scratch, fileName); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 735 | scratch[len] = '\0'; |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 736 | make_device(/*DEVNAME:*/ NULL, scratch, OP_add); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 737 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 738 | return TRUE; |
| 739 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 740 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 741 | /* Directory callback for /sys/ traversal */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 742 | static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM, |
| 743 | struct stat *statbuf UNUSED_PARAM, |
| 744 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 745 | int depth) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 746 | { |
Denis Vlasenko | 32a3d08 | 2009-04-14 21:23:33 +0000 | [diff] [blame] | 747 | /* Extract device subsystem -- the name of the directory |
| 748 | * under /sys/class/ */ |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 749 | if (1 == depth) { |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 750 | free(G.subsystem); |
| 751 | G.subsystem = strrchr(fileName, '/'); |
| 752 | if (G.subsystem) |
| 753 | G.subsystem = xstrdup(G.subsystem + 1); |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 756 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 759 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 760 | * |
| 761 | * Firmware loading works like this: |
| 762 | * - kernel sets FIRMWARE env var |
| 763 | * - userspace checks /lib/firmware/$FIRMWARE |
| 764 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 765 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 766 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 767 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 768 | * - kernel loads firmware into device |
| 769 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 770 | static void load_firmware(const char *firmware, const char *sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 771 | { |
| 772 | int cnt; |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 773 | int firmware_fd, loading_fd; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 774 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 775 | /* check for /lib/firmware/$FIRMWARE */ |
| 776 | xchdir("/lib/firmware"); |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 777 | firmware_fd = open(firmware, O_RDONLY); /* can fail */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 778 | |
| 779 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 780 | xchdir(sysfs_path); |
| 781 | for (cnt = 0; cnt < 30; ++cnt) { |
| 782 | loading_fd = open("loading", O_WRONLY); |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 783 | if (loading_fd >= 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 784 | goto loading; |
| 785 | sleep(1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 786 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 787 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 788 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 789 | loading: |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 790 | cnt = 0; |
| 791 | if (firmware_fd >= 0) { |
| 792 | int data_fd; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 793 | |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 794 | /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */ |
| 795 | if (full_write(loading_fd, "1", 1) != 1) |
| 796 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 797 | |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 798 | /* load firmware into /sys/$DEVPATH/data */ |
| 799 | data_fd = open("data", O_WRONLY); |
| 800 | if (data_fd < 0) |
| 801 | goto out; |
| 802 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 803 | if (ENABLE_FEATURE_CLEAN_UP) |
| 804 | close(data_fd); |
| 805 | } |
| 806 | |
| 807 | /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" |
| 808 | * Note: we emit -1 also if firmware file wasn't found. |
| 809 | * There are cases when otherwise kernel would wait for minutes |
| 810 | * before timing out. |
| 811 | */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 812 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 813 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 814 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 815 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 816 | |
| 817 | out: |
Denys Vlasenko | ba76b7a | 2013-02-04 23:35:27 +0100 | [diff] [blame^] | 818 | xchdir("/dev"); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 819 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 820 | close(firmware_fd); |
| 821 | close(loading_fd); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 825 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 826 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 827 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 828 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 829 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 830 | INIT_G(); |
| 831 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 832 | #if ENABLE_FEATURE_MDEV_CONF |
| 833 | G.filename = "/etc/mdev.conf"; |
| 834 | #endif |
| 835 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 836 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 837 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 838 | bb_sanitize_stdio(); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 839 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 840 | /* Force the configuration file settings exactly */ |
| 841 | umask(0); |
| 842 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 843 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 844 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 845 | if (argv[1] && strcmp(argv[1], "-s") == 0) { |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 846 | /* Scan: |
| 847 | * mdev -s |
| 848 | */ |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 849 | struct stat st; |
| 850 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 851 | #if ENABLE_FEATURE_MDEV_CONF |
| 852 | /* Same as xrealloc_vector(NULL, 4, 0): */ |
| 853 | G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec)); |
| 854 | #endif |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 855 | xstat("/", &st); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 856 | G.root_major = major(st.st_dev); |
| 857 | G.root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 858 | |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 859 | /* ACTION_FOLLOWLINKS is needed since in newer kernels |
| 860 | * /sys/block/loop* (for example) are symlinks to dirs, |
| 861 | * not real directories. |
| 862 | * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 863 | * but we can't enforce that on users) |
| 864 | */ |
| 865 | if (access("/sys/class/block", F_OK) != 0) { |
| 866 | /* Scan obsolete /sys/block only if /sys/class/block |
| 867 | * doesn't exist. Otherwise we'll have dupes. |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 868 | * Also, do not complain if it doesn't exist. |
| 869 | * Some people configure kernel to have no blockdevs. |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 870 | */ |
| 871 | recursive_action("/sys/block", |
Denis Vlasenko | f1df35c | 2009-04-12 14:00:12 +0000 | [diff] [blame] | 872 | ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET, |
Denis Vlasenko | d63cd1b | 2009-02-13 00:02:54 +0000 | [diff] [blame] | 873 | fileAction, dirAction, temp, 0); |
| 874 | } |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 875 | recursive_action("/sys/class", |
Denis Vlasenko | 67075bb | 2008-07-06 17:00:49 +0000 | [diff] [blame] | 876 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 877 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 878 | } else { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 879 | char *fw; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 880 | char *seq; |
| 881 | char *action; |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 882 | char *env_devname; |
| 883 | char *env_devpath; |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 884 | smalluint op; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 885 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 886 | /* Hotplug: |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 887 | * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 888 | * ACTION can be "add" or "remove" |
| 889 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 890 | */ |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 891 | action = getenv("ACTION"); |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 892 | op = index_in_strings(keywords, action); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 893 | env_devname = getenv("DEVNAME"); /* can be NULL */ |
| 894 | env_devpath = getenv("DEVPATH"); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 895 | G.subsystem = getenv("SUBSYSTEM"); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 896 | if (!action || !env_devpath /*|| !G.subsystem*/) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 897 | bb_show_usage(); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 898 | fw = getenv("FIRMWARE"); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 899 | /* If it exists, does /dev/mdev.seq match $SEQNUM? |
| 900 | * If it does not match, earlier mdev is running |
| 901 | * in parallel, and we need to wait */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 902 | seq = getenv("SEQNUM"); |
| 903 | if (seq) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 904 | int timeout = 2000 / 32; /* 2000 msec */ |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 905 | do { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 906 | int seqlen; |
| 907 | char seqbuf[sizeof(int)*3 + 2]; |
| 908 | |
Javier Viguera | e8758ea | 2012-01-27 18:30:20 +0100 | [diff] [blame] | 909 | seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf) - 1); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 910 | if (seqlen < 0) { |
| 911 | seq = NULL; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 912 | break; |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 913 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 914 | seqbuf[seqlen] = '\0'; |
| 915 | if (seqbuf[0] == '\n' /* seed file? */ |
| 916 | || strcmp(seq, seqbuf) == 0 /* correct idx? */ |
| 917 | ) { |
| 918 | break; |
| 919 | } |
| 920 | usleep(32*1000); |
| 921 | } while (--timeout); |
| 922 | } |
| 923 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 924 | { |
Denys Vlasenko | ba76b7a | 2013-02-04 23:35:27 +0100 | [diff] [blame^] | 925 | int logfd = open("mdev.log", O_WRONLY | O_APPEND); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 926 | if (logfd >= 0) { |
| 927 | xmove_fd(logfd, STDERR_FILENO); |
| 928 | G.verbose = 1; |
Denys Vlasenko | 6e29d07 | 2013-01-30 16:51:22 +0100 | [diff] [blame] | 929 | if (seq) |
| 930 | applet_name = xasprintf("%s[%s]", applet_name, seq); |
| 931 | bb_error_msg("action: %s", action); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | snprintf(temp, PATH_MAX, "/sys%s", env_devpath); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 936 | if (op == OP_remove) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 937 | /* Ignoring "remove firmware". It was reported |
| 938 | * to happen and to cause erroneous deletion |
| 939 | * of device nodes. */ |
| 940 | if (!fw) |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 941 | make_device(env_devname, temp, op); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 942 | } |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 943 | else if (op == OP_add) { |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 944 | make_device(env_devname, temp, op); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 945 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 946 | if (fw) |
| 947 | load_firmware(fw, temp); |
| 948 | } |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 949 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 950 | |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 951 | if (seq) { |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 952 | xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1)); |
| 953 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 956 | if (ENABLE_FEATURE_CLEAN_UP) |
| 957 | RELEASE_CONFIG_BUFFER(temp); |
| 958 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 959 | return EXIT_SUCCESS; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 960 | } |