"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 | */ |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 10 | //config:config MDEV |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "mdev (16 kb)" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: select PLATFORM_LINUX |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: mdev is a mini-udev implementation for dynamically creating device |
| 16 | //config: nodes in the /dev directory. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 17 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 18 | //config: For more information, please see docs/mdev.txt |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 19 | //config: |
| 20 | //config:config FEATURE_MDEV_CONF |
| 21 | //config: bool "Support /etc/mdev.conf" |
| 22 | //config: default y |
| 23 | //config: depends on MDEV |
| 24 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 25 | //config: Add support for the mdev config file to control ownership and |
| 26 | //config: permissions of the device nodes. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 27 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: For more information, please see docs/mdev.txt |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 29 | //config: |
| 30 | //config:config FEATURE_MDEV_RENAME |
| 31 | //config: bool "Support subdirs/symlinks" |
| 32 | //config: default y |
| 33 | //config: depends on FEATURE_MDEV_CONF |
| 34 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 35 | //config: Add support for renaming devices and creating symlinks. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 36 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 37 | //config: For more information, please see docs/mdev.txt |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 38 | //config: |
| 39 | //config:config FEATURE_MDEV_RENAME_REGEXP |
| 40 | //config: bool "Support regular expressions substitutions when renaming device" |
| 41 | //config: default y |
| 42 | //config: depends on FEATURE_MDEV_RENAME |
| 43 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 44 | //config: Add support for regular expressions substitutions when renaming |
| 45 | //config: device. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 46 | //config: |
| 47 | //config:config FEATURE_MDEV_EXEC |
| 48 | //config: bool "Support command execution at device addition/removal" |
| 49 | //config: default y |
| 50 | //config: depends on FEATURE_MDEV_CONF |
| 51 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 52 | //config: This adds support for an optional field to /etc/mdev.conf for |
| 53 | //config: executing commands when devices are created/removed. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 54 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 55 | //config: For more information, please see docs/mdev.txt |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 56 | //config: |
| 57 | //config:config FEATURE_MDEV_LOAD_FIRMWARE |
Denys Vlasenko | 68b653b | 2017-07-27 10:53:09 +0200 | [diff] [blame] | 58 | //config: bool "Support loading of firmware" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 59 | //config: default y |
| 60 | //config: depends on MDEV |
| 61 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 62 | //config: Some devices need to load firmware before they can be usable. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 63 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 64 | //config: These devices will request userspace look up the files in |
| 65 | //config: /lib/firmware/ and if it exists, send it to the kernel for |
| 66 | //config: loading into the hardware. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 67 | |
| 68 | //applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP)) |
| 69 | |
| 70 | //kbuild:lib-$(CONFIG_MDEV) += mdev.o |
| 71 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 72 | //usage:#define mdev_trivial_usage |
| 73 | //usage: "[-s]" |
| 74 | //usage:#define mdev_full_usage "\n\n" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 75 | //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] | 76 | //usage: "\n" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 77 | //usage: "Bare mdev is a kernel hotplug helper. To activate it:\n" |
| 78 | //usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 79 | //usage: IF_FEATURE_MDEV_CONF( |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 80 | //usage: "\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 81 | //usage: "It uses /etc/mdev.conf with lines\n" |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 82 | //usage: " [-][ENV=regex;]...DEVNAME UID:GID PERM" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 83 | //usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 84 | //usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]") |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 85 | //usage: "\n" |
| 86 | //usage: "where DEVNAME is device name regex, @major,minor[-minor2], or\n" |
| 87 | //usage: "environment variable regex. A common use of the latter is\n" |
| 88 | //usage: "to load modules for hotplugged devices:\n" |
| 89 | //usage: " $MODALIAS=.* 0:0 660 @modprobe \"$MODALIAS\"\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 90 | //usage: ) |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 91 | //usage: "\n" |
| 92 | //usage: "If /dev/mdev.seq file exists, mdev will wait for its value\n" |
| 93 | //usage: "to match $SEQNUM variable. This prevents plug/unplug races.\n" |
Denys Vlasenko | 8d11854 | 2012-05-18 04:47:18 +0200 | [diff] [blame] | 94 | //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] | 95 | //usage: "\n" |
Denys Vlasenko | 8d11854 | 2012-05-18 04:47:18 +0200 | [diff] [blame] | 96 | //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] | 97 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 98 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 99 | #include "common_bufsiz.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 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 233 | #define DEBUG_LVL 2 |
| 234 | |
| 235 | #if DEBUG_LVL >= 1 |
| 236 | # define dbg1(...) do { if (G.verbose) bb_error_msg(__VA_ARGS__); } while(0) |
| 237 | #else |
| 238 | # define dbg1(...) ((void)0) |
| 239 | #endif |
| 240 | #if DEBUG_LVL >= 2 |
| 241 | # define dbg2(...) do { if (G.verbose >= 2) bb_error_msg(__VA_ARGS__); } while(0) |
| 242 | #else |
| 243 | # define dbg2(...) ((void)0) |
| 244 | #endif |
| 245 | #if DEBUG_LVL >= 3 |
| 246 | # define dbg3(...) do { if (G.verbose >= 3) bb_error_msg(__VA_ARGS__); } while(0) |
| 247 | #else |
| 248 | # define dbg3(...) ((void)0) |
| 249 | #endif |
| 250 | |
| 251 | |
| 252 | static const char keywords[] ALIGN1 = "add\0remove\0"; // "change\0" |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 253 | enum { OP_add, OP_remove }; |
| 254 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 255 | struct envmatch { |
| 256 | struct envmatch *next; |
| 257 | char *envname; |
| 258 | regex_t match; |
| 259 | }; |
| 260 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 261 | struct rule { |
| 262 | bool keep_matching; |
| 263 | bool regex_compiled; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 264 | mode_t mode; |
| 265 | int maj, min0, min1; |
| 266 | struct bb_uidgid_t ugid; |
| 267 | char *envvar; |
| 268 | char *ren_mov; |
| 269 | IF_FEATURE_MDEV_EXEC(char *r_cmd;) |
| 270 | regex_t match; |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 271 | struct envmatch *envmatch; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 272 | }; |
| 273 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 274 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 275 | int root_major, root_minor; |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 276 | smallint verbose; |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 277 | char *subsystem; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 278 | char *subsys_env; /* for putenv("SUBSYSTEM=subsystem") */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 279 | #if ENABLE_FEATURE_MDEV_CONF |
| 280 | const char *filename; |
| 281 | parser_t *parser; |
| 282 | struct rule **rule_vec; |
| 283 | unsigned rule_idx; |
| 284 | #endif |
| 285 | struct rule cur_rule; |
Denys Vlasenko | bd77e9d | 2015-04-19 18:54:49 +0200 | [diff] [blame] | 286 | char timestr[sizeof("HH:MM:SS.123456")]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 287 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 288 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | c966563 | 2012-04-21 14:35:32 +0200 | [diff] [blame] | 289 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 290 | setup_common_bufsiz(); \ |
Denys Vlasenko | c966563 | 2012-04-21 14:35:32 +0200 | [diff] [blame] | 291 | IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \ |
| 292 | IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \ |
| 293 | } while (0) |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 294 | |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 295 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 296 | /* Prevent infinite loops in /sys symlinks */ |
| 297 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 298 | |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 299 | /* We use additional bytes in make_device() */ |
| 300 | #define SCRATCH_SIZE 128 |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 301 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 302 | #if ENABLE_FEATURE_MDEV_CONF |
| 303 | |
| 304 | static void make_default_cur_rule(void) |
| 305 | { |
| 306 | memset(&G.cur_rule, 0, sizeof(G.cur_rule)); |
| 307 | G.cur_rule.maj = -1; /* "not a @major,minor rule" */ |
| 308 | G.cur_rule.mode = 0660; |
| 309 | } |
| 310 | |
| 311 | static void clean_up_cur_rule(void) |
| 312 | { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 313 | struct envmatch *e; |
| 314 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 315 | free(G.cur_rule.envvar); |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 316 | free(G.cur_rule.ren_mov); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 317 | if (G.cur_rule.regex_compiled) |
| 318 | regfree(&G.cur_rule.match); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 319 | IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);) |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 320 | e = G.cur_rule.envmatch; |
| 321 | while (e) { |
| 322 | free(e->envname); |
| 323 | regfree(&e->match); |
| 324 | e = e->next; |
| 325 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 326 | make_default_cur_rule(); |
| 327 | } |
| 328 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 329 | static char *parse_envmatch_pfx(char *val) |
| 330 | { |
| 331 | struct envmatch **nextp = &G.cur_rule.envmatch; |
| 332 | |
| 333 | for (;;) { |
| 334 | struct envmatch *e; |
| 335 | char *semicolon; |
| 336 | char *eq = strchr(val, '='); |
| 337 | if (!eq /* || eq == val? */) |
| 338 | return val; |
| 339 | if (endofname(val) != eq) |
| 340 | return val; |
| 341 | semicolon = strchr(eq, ';'); |
| 342 | if (!semicolon) |
| 343 | return val; |
| 344 | /* ENVVAR=regex;... */ |
| 345 | *nextp = e = xzalloc(sizeof(*e)); |
| 346 | nextp = &e->next; |
| 347 | e->envname = xstrndup(val, eq - val); |
| 348 | *semicolon = '\0'; |
| 349 | xregcomp(&e->match, eq + 1, REG_EXTENDED); |
| 350 | *semicolon = ';'; |
| 351 | val = semicolon + 1; |
| 352 | } |
| 353 | } |
| 354 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 355 | static void parse_next_rule(void) |
| 356 | { |
| 357 | /* Note: on entry, G.cur_rule is set to default */ |
| 358 | while (1) { |
| 359 | char *tokens[4]; |
| 360 | char *val; |
| 361 | |
Denys Vlasenko | 499597d | 2012-01-11 00:37:17 +0100 | [diff] [blame] | 362 | /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */ |
| 363 | 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] | 364 | break; |
| 365 | |
| 366 | /* Fields: [-]regex uid:gid mode [alias] [cmd] */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 367 | dbg3("token1:'%s'", tokens[1]); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 368 | |
| 369 | /* 1st field */ |
| 370 | val = tokens[0]; |
| 371 | G.cur_rule.keep_matching = ('-' == val[0]); |
| 372 | val += G.cur_rule.keep_matching; /* swallow leading dash */ |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 373 | val = parse_envmatch_pfx(val); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 374 | if (val[0] == '@') { |
| 375 | /* @major,minor[-minor2] */ |
| 376 | /* (useful when name is ambiguous: |
| 377 | * "/sys/class/usb/lp0" and |
| 378 | * "/sys/class/printer/lp0") |
| 379 | */ |
| 380 | int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1); |
| 381 | if (sc < 2 || G.cur_rule.maj < 0) { |
| 382 | bb_error_msg("bad @maj,min on line %d", G.parser->lineno); |
| 383 | goto next_rule; |
| 384 | } |
| 385 | if (sc == 2) |
| 386 | G.cur_rule.min1 = G.cur_rule.min0; |
| 387 | } else { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 388 | char *eq = strchr(val, '='); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 389 | if (val[0] == '$') { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 390 | /* $ENVVAR=regex ... */ |
| 391 | val++; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 392 | if (!eq) { |
| 393 | bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno); |
| 394 | goto next_rule; |
| 395 | } |
| 396 | G.cur_rule.envvar = xstrndup(val, eq - val); |
| 397 | val = eq + 1; |
| 398 | } |
| 399 | xregcomp(&G.cur_rule.match, val, REG_EXTENDED); |
| 400 | G.cur_rule.regex_compiled = 1; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* 2nd field: uid:gid - device ownership */ |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 404 | if (get_uidgid(&G.cur_rule.ugid, tokens[1]) == 0) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 405 | bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno); |
| 406 | goto next_rule; |
| 407 | } |
| 408 | |
| 409 | /* 3rd field: mode - device permissions */ |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 410 | G.cur_rule.mode = bb_parse_mode(tokens[2], G.cur_rule.mode); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 411 | |
| 412 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ |
| 413 | val = tokens[3]; |
| 414 | if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) { |
| 415 | char *s = skip_non_whitespace(val); |
| 416 | G.cur_rule.ren_mov = xstrndup(val, s - val); |
| 417 | val = skip_whitespace(s); |
| 418 | } |
| 419 | |
| 420 | if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) { |
| 421 | const char *s = "$@*"; |
| 422 | const char *s2 = strchr(s, val[0]); |
| 423 | if (!s2) { |
| 424 | bb_error_msg("bad line %u", G.parser->lineno); |
| 425 | goto next_rule; |
| 426 | } |
| 427 | IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);) |
| 428 | } |
| 429 | |
| 430 | return; |
| 431 | next_rule: |
| 432 | clean_up_cur_rule(); |
| 433 | } /* while (config_read) */ |
| 434 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 435 | dbg3("config_close(G.parser)"); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 436 | config_close(G.parser); |
| 437 | G.parser = NULL; |
| 438 | |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | /* If mdev -s, we remember rules in G.rule_vec[]. |
| 443 | * Otherwise, there is no point in doing it, and we just |
| 444 | * save only one parsed rule in G.cur_rule. |
| 445 | */ |
| 446 | static const struct rule *next_rule(void) |
| 447 | { |
| 448 | struct rule *rule; |
| 449 | |
| 450 | /* Open conf file if we didn't do it yet */ |
| 451 | if (!G.parser && G.filename) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 452 | dbg3("config_open('%s')", G.filename); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 453 | G.parser = config_open2(G.filename, fopen_for_read); |
| 454 | G.filename = NULL; |
| 455 | } |
| 456 | |
| 457 | if (G.rule_vec) { |
| 458 | /* mdev -s */ |
| 459 | /* Do we have rule parsed already? */ |
| 460 | if (G.rule_vec[G.rule_idx]) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 461 | dbg3("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 462 | return G.rule_vec[G.rule_idx++]; |
| 463 | } |
| 464 | make_default_cur_rule(); |
| 465 | } else { |
| 466 | /* not mdev -s */ |
| 467 | clean_up_cur_rule(); |
| 468 | } |
| 469 | |
| 470 | /* Parse one more rule if file isn't fully read */ |
| 471 | rule = &G.cur_rule; |
| 472 | if (G.parser) { |
| 473 | parse_next_rule(); |
| 474 | if (G.rule_vec) { /* mdev -s */ |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 475 | rule = xmemdup(&G.cur_rule, sizeof(G.cur_rule)); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 476 | G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx); |
| 477 | G.rule_vec[G.rule_idx++] = rule; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 478 | dbg3("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | return rule; |
| 483 | } |
| 484 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 485 | static int env_matches(struct envmatch *e) |
| 486 | { |
| 487 | while (e) { |
| 488 | int r; |
| 489 | char *val = getenv(e->envname); |
| 490 | if (!val) |
| 491 | return 0; |
| 492 | r = regexec(&e->match, val, /*size*/ 0, /*range[]*/ NULL, /*eflags*/ 0); |
| 493 | if (r != 0) /* no match */ |
| 494 | return 0; |
| 495 | e = e->next; |
| 496 | } |
| 497 | return 1; |
| 498 | } |
| 499 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 500 | #else |
| 501 | |
| 502 | # define next_rule() (&G.cur_rule) |
| 503 | |
| 504 | #endif |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 505 | |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 506 | static void mkdir_recursive(char *name) |
| 507 | { |
| 508 | /* if name has many levels ("dir1/dir2"), |
| 509 | * bb_make_directory() will create dir1 according to umask, |
| 510 | * not according to its "mode" parameter. |
| 511 | * Since we run with umask=0, need to temporarily switch it. |
| 512 | */ |
| 513 | umask(022); /* "dir1" (if any) will be 0755 too */ |
| 514 | bb_make_directory(name, 0755, FILEUTILS_RECUR); |
| 515 | umask(0); |
| 516 | } |
| 517 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 518 | /* Builds an alias path. |
| 519 | * This function potentionally reallocates the alias parameter. |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 520 | * Only used for ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 521 | */ |
| 522 | static char *build_alias(char *alias, const char *device_name) |
| 523 | { |
| 524 | char *dest; |
| 525 | |
| 526 | /* ">bar/": rename to bar/device_name */ |
| 527 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 528 | dest = strrchr(alias, '/'); |
| 529 | if (dest) { /* ">bar/[baz]" ? */ |
| 530 | *dest = '\0'; /* mkdir bar */ |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 531 | mkdir_recursive(alias); |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 532 | *dest = '/'; |
| 533 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 534 | dest = alias; |
| 535 | alias = concat_path_file(alias, device_name); |
| 536 | free(dest); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | return alias; |
| 541 | } |
| 542 | |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 543 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" |
| 544 | * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 545 | * after NUL, but we promise to not mangle it (IOW: to restore NUL if needed). |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 546 | * 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] | 547 | * |
| 548 | * device_name = $DEVNAME (may be NULL) |
| 549 | * path = /sys/$DEVPATH |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 550 | */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 551 | static void make_device(char *device_name, char *path, int operation) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 552 | { |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 553 | int major, minor, type, len; |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 554 | char *path_end = path + strlen(path); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 555 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 556 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 557 | * 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] | 558 | * because sscanf() will stop at the first nondigit, which \n is. |
| 559 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 560 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 561 | major = -1; |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 562 | if (operation == OP_add) { |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 563 | strcpy(path_end, "/dev"); |
| 564 | len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1); |
| 565 | *path_end = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 566 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 567 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 568 | return; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 569 | /* no "dev" file, but we can still run scripts |
| 570 | * based on device name */ |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 571 | } else if (sscanf(path_end + 1, "%u:%u", &major, &minor) == 2) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 572 | dbg1("dev %u,%u", major, minor); |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 573 | } else { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 574 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 575 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 576 | } |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 577 | /* else: for delete, -1 still deletes the node, but < -1 suppresses that */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 578 | |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 579 | /* Determine device name */ |
| 580 | if (!device_name) { |
| 581 | /* |
| 582 | * There was no $DEVNAME envvar (for example, mdev -s never has). |
| 583 | * But it is very useful: it contains the *path*, not only basename, |
| 584 | * Thankfully, uevent file has it. |
| 585 | * Example of .../sound/card0/controlC0/uevent file on Linux-3.7.7: |
| 586 | * MAJOR=116 |
| 587 | * MINOR=7 |
| 588 | * DEVNAME=snd/controlC0 |
| 589 | */ |
| 590 | strcpy(path_end, "/uevent"); |
| 591 | len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1); |
| 592 | if (len < 0) |
| 593 | len = 0; |
| 594 | *path_end = '\0'; |
| 595 | path_end[1 + len] = '\0'; |
| 596 | device_name = strstr(path_end + 1, "\nDEVNAME="); |
| 597 | if (device_name) { |
| 598 | device_name += sizeof("\nDEVNAME=")-1; |
| 599 | strchrnul(device_name, '\n')[0] = '\0'; |
| 600 | } else { |
| 601 | /* Fall back to just basename */ |
| 602 | device_name = (char*) bb_basename(path); |
| 603 | } |
| 604 | } |
| 605 | /* Determine device type */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 606 | /* |
| 607 | * http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 608 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 609 | * 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] | 610 | * We use strstr("/block/") to forestall future surprises. |
| 611 | */ |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 612 | type = S_IFCHR; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 613 | if (strstr(path, "/block/") || (G.subsystem && is_prefixed_with(G.subsystem, "block"))) |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 614 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 615 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 616 | #if ENABLE_FEATURE_MDEV_CONF |
| 617 | G.rule_idx = 0; /* restart from the beginning (think mdev -s) */ |
| 618 | #endif |
| 619 | for (;;) { |
| 620 | const char *str_to_match; |
| 621 | regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 622 | char *command; |
| 623 | char *alias; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 624 | char aliaslink = aliaslink; /* for compiler */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 625 | char *node_name; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 626 | const struct rule *rule; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 627 | |
Denys Vlasenko | b6beada | 2012-05-18 15:11:16 +0200 | [diff] [blame] | 628 | str_to_match = device_name; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 629 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 630 | rule = next_rule(); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 631 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 632 | #if ENABLE_FEATURE_MDEV_CONF |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 633 | if (!env_matches(rule->envmatch)) |
| 634 | continue; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 635 | if (rule->maj >= 0) { /* @maj,min rule */ |
| 636 | if (major != rule->maj) |
| 637 | continue; |
| 638 | if (minor < rule->min0 || minor > rule->min1) |
| 639 | continue; |
| 640 | memset(off, 0, sizeof(off)); |
| 641 | goto rule_matches; |
| 642 | } |
| 643 | if (rule->envvar) { /* $envvar=regex rule */ |
| 644 | str_to_match = getenv(rule->envvar); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 645 | dbg3("getenv('%s'):'%s'", rule->envvar, str_to_match); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 646 | if (!str_to_match) |
| 647 | continue; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 648 | } |
Denys Vlasenko | b6beada | 2012-05-18 15:11:16 +0200 | [diff] [blame] | 649 | /* else: str_to_match = device_name */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 650 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 651 | if (rule->regex_compiled) { |
| 652 | int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 653 | dbg3("regex_match for '%s':%d", str_to_match, regex_match); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 654 | //bb_error_msg("matches:"); |
| 655 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 656 | // if (off[i].rm_so < 0) continue; |
| 657 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 658 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 659 | // device_name + off[i].rm_so); |
| 660 | //} |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 661 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 662 | if (regex_match != 0 |
| 663 | /* regexec returns whole pattern as "range" 0 */ |
| 664 | || off[0].rm_so != 0 |
| 665 | || (int)off[0].rm_eo != (int)strlen(str_to_match) |
| 666 | ) { |
| 667 | continue; /* this rule doesn't match */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 668 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 669 | } |
| 670 | /* else: it's final implicit "match-all" rule */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 671 | rule_matches: |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 672 | dbg2("rule matched, line %d", G.parser ? G.parser->lineno : -1); |
Denys Vlasenko | 4424dfd | 2013-03-17 23:01:17 +0100 | [diff] [blame] | 673 | #endif |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 674 | /* Build alias name */ |
| 675 | alias = NULL; |
| 676 | if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) { |
| 677 | aliaslink = rule->ren_mov[0]; |
| 678 | if (aliaslink == '!') { |
| 679 | /* "!": suppress node creation/deletion */ |
| 680 | major = -2; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 681 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 682 | else if (aliaslink == '>' || aliaslink == '=') { |
| 683 | if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) { |
| 684 | char *s; |
| 685 | char *p; |
| 686 | unsigned n; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 687 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 688 | /* substitute %1..9 with off[1..9], if any */ |
| 689 | n = 0; |
| 690 | s = rule->ren_mov; |
| 691 | while (*s) |
| 692 | if (*s++ == '%') |
| 693 | n++; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 694 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 695 | p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match)); |
| 696 | s = rule->ren_mov + 1; |
| 697 | while (*s) { |
| 698 | *p = *s; |
| 699 | if ('%' == *s) { |
| 700 | unsigned i = (s[1] - '0'); |
| 701 | if (i <= 9 && off[i].rm_so >= 0) { |
| 702 | n = off[i].rm_eo - off[i].rm_so; |
| 703 | strncpy(p, str_to_match + off[i].rm_so, n); |
| 704 | p += n - 1; |
| 705 | s++; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 706 | } |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 707 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 708 | p++; |
| 709 | s++; |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 710 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 711 | } else { |
| 712 | alias = xstrdup(rule->ren_mov + 1); |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 713 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 714 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 715 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 716 | dbg3("alias:'%s'", alias); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 717 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 718 | command = NULL; |
| 719 | IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;) |
| 720 | if (command) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 721 | /* Are we running this command now? |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 722 | * Run @cmd on create, $cmd on delete, *cmd on any |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 723 | */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 724 | if ((command[0] == '@' && operation == OP_add) |
| 725 | || (command[0] == '$' && operation == OP_remove) |
| 726 | || (command[0] == '*') |
| 727 | ) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 728 | command++; |
| 729 | } else { |
| 730 | command = NULL; |
| 731 | } |
| 732 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 733 | dbg3("command:'%s'", command); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 734 | |
| 735 | /* "Execute" the line we found */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 736 | node_name = device_name; |
| 737 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 738 | node_name = alias = build_alias(alias, device_name); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 739 | dbg3("alias2:'%s'", alias); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 742 | if (operation == OP_add && major >= 0) { |
| 743 | char *slash = strrchr(node_name, '/'); |
| 744 | if (slash) { |
| 745 | *slash = '\0'; |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 746 | mkdir_recursive(node_name); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 747 | *slash = '/'; |
| 748 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 749 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 750 | dbg1("mknod %s (%d,%d) %o" |
| 751 | " %u:%u", |
| 752 | node_name, major, minor, rule->mode | type, |
| 753 | rule->ugid.uid, rule->ugid.gid |
| 754 | ); |
| 755 | } else { |
| 756 | dbg1("mknod %s (%d,%d) %o", |
| 757 | node_name, major, minor, rule->mode | type |
| 758 | ); |
| 759 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 760 | if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST) |
| 761 | bb_perror_msg("can't create '%s'", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 762 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 763 | chmod(node_name, rule->mode); |
| 764 | chown(node_name, rule->ugid.uid, rule->ugid.gid); |
| 765 | } |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 766 | if (major == G.root_major && minor == G.root_minor) |
| 767 | symlink(node_name, "root"); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 768 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
Denys Vlasenko | 638dbc3 | 2012-02-06 01:00:00 +0100 | [diff] [blame] | 769 | if (aliaslink == '>') { |
| 770 | //TODO: on devtmpfs, device_name already exists and symlink() fails. |
| 771 | //End result is that instead of symlink, we have two nodes. |
| 772 | //What should be done? |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 773 | dbg1("symlink: %s", device_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 774 | symlink(node_name, device_name); |
Denys Vlasenko | 638dbc3 | 2012-02-06 01:00:00 +0100 | [diff] [blame] | 775 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | if (ENABLE_FEATURE_MDEV_EXEC && command) { |
| 780 | /* setenv will leak memory, use putenv/unsetenv/free */ |
| 781 | char *s = xasprintf("%s=%s", "MDEV", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 782 | putenv(s); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 783 | dbg1("running: %s", command); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 784 | if (system(command) == -1) |
| 785 | bb_perror_msg("can't run '%s'", command); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 786 | bb_unsetenv_and_free(s); |
| 787 | } |
| 788 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 789 | if (operation == OP_remove && major >= -1) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 790 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 791 | if (aliaslink == '>') { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 792 | dbg1("unlink: %s", device_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 793 | unlink(device_name); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 794 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 795 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 796 | dbg1("unlink: %s", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 797 | unlink(node_name); |
| 798 | } |
| 799 | |
| 800 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 801 | free(alias); |
| 802 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 803 | /* We found matching line. |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 804 | * Stop unless it was prefixed with '-' |
| 805 | */ |
| 806 | if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching) |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 807 | break; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 808 | } /* for (;;) */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 811 | /* File callback for /sys/ traversal. |
| 812 | * We act only on "/sys/.../dev" (pseudo)file |
| 813 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 814 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 815 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 816 | void *userData, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 817 | int depth UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 818 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 819 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 820 | char *path = userData; /* char array[PATH_MAX + SCRATCH_SIZE] */ |
| 821 | char subsys[PATH_MAX]; |
| 822 | int res; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 823 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 824 | /* Is it a ".../dev" file? (len check is for paranoid reasons) */ |
| 825 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX - 32) |
| 826 | return FALSE; /* not .../dev */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 827 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 828 | strcpy(path, fileName); |
| 829 | path[len] = '\0'; |
| 830 | |
| 831 | /* Read ".../subsystem" symlink in the same directory where ".../dev" is */ |
| 832 | strcpy(subsys, path); |
| 833 | strcpy(subsys + len, "/subsystem"); |
| 834 | res = readlink(subsys, subsys, sizeof(subsys)-1); |
| 835 | if (res > 0) { |
| 836 | subsys[res] = '\0'; |
| 837 | free(G.subsystem); |
| 838 | if (G.subsys_env) { |
| 839 | bb_unsetenv_and_free(G.subsys_env); |
| 840 | G.subsys_env = NULL; |
| 841 | } |
| 842 | /* Set G.subsystem and $SUBSYSTEM from symlink's last component */ |
| 843 | G.subsystem = strrchr(subsys, '/'); |
| 844 | if (G.subsystem) { |
| 845 | G.subsystem = xstrdup(G.subsystem + 1); |
| 846 | G.subsys_env = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem); |
| 847 | putenv(G.subsys_env); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | make_device(/*DEVNAME:*/ NULL, path, OP_add); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 852 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 853 | return TRUE; |
| 854 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 855 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 856 | /* Directory callback for /sys/ traversal */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 857 | static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM, |
| 858 | struct stat *statbuf UNUSED_PARAM, |
| 859 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 860 | int depth) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 861 | { |
| 862 | return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 865 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 866 | * |
| 867 | * Firmware loading works like this: |
| 868 | * - kernel sets FIRMWARE env var |
| 869 | * - userspace checks /lib/firmware/$FIRMWARE |
| 870 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 871 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 872 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 873 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 874 | * - kernel loads firmware into device |
| 875 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 876 | static void load_firmware(const char *firmware, const char *sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 877 | { |
| 878 | int cnt; |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 879 | int firmware_fd, loading_fd; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 880 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 881 | /* check for /lib/firmware/$FIRMWARE */ |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 882 | firmware_fd = -1; |
| 883 | if (chdir("/lib/firmware") == 0) |
| 884 | firmware_fd = open(firmware, O_RDONLY); /* can fail */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 885 | |
| 886 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 887 | xchdir(sysfs_path); |
| 888 | for (cnt = 0; cnt < 30; ++cnt) { |
| 889 | loading_fd = open("loading", O_WRONLY); |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 890 | if (loading_fd >= 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 891 | goto loading; |
| 892 | sleep(1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 893 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 894 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 895 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 896 | loading: |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 897 | cnt = 0; |
| 898 | if (firmware_fd >= 0) { |
| 899 | int data_fd; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 900 | |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 901 | /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */ |
| 902 | if (full_write(loading_fd, "1", 1) != 1) |
| 903 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 904 | |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 905 | /* load firmware into /sys/$DEVPATH/data */ |
| 906 | data_fd = open("data", O_WRONLY); |
| 907 | if (data_fd < 0) |
| 908 | goto out; |
| 909 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 910 | if (ENABLE_FEATURE_CLEAN_UP) |
| 911 | close(data_fd); |
| 912 | } |
| 913 | |
| 914 | /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" |
| 915 | * Note: we emit -1 also if firmware file wasn't found. |
| 916 | * There are cases when otherwise kernel would wait for minutes |
| 917 | * before timing out. |
| 918 | */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 919 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 920 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 921 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 922 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 923 | |
| 924 | out: |
Denys Vlasenko | ba76b7a | 2013-02-04 23:35:27 +0100 | [diff] [blame] | 925 | xchdir("/dev"); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 926 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 927 | close(firmware_fd); |
| 928 | close(loading_fd); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 932 | static char *curtime(void) |
| 933 | { |
| 934 | struct timeval tv; |
| 935 | gettimeofday(&tv, NULL); |
Denys Vlasenko | bd77e9d | 2015-04-19 18:54:49 +0200 | [diff] [blame] | 936 | sprintf( |
| 937 | strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec), |
| 938 | ".%06u", |
| 939 | (unsigned)tv.tv_usec |
| 940 | ); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 941 | return G.timestr; |
| 942 | } |
| 943 | |
| 944 | static void open_mdev_log(const char *seq, unsigned my_pid) |
| 945 | { |
| 946 | int logfd = open("mdev.log", O_WRONLY | O_APPEND); |
| 947 | if (logfd >= 0) { |
| 948 | xmove_fd(logfd, STDERR_FILENO); |
| 949 | G.verbose = 2; |
| 950 | applet_name = xasprintf("%s[%s]", applet_name, seq ? seq : utoa(my_pid)); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* If it exists, does /dev/mdev.seq match $SEQNUM? |
| 955 | * If it does not match, earlier mdev is running |
| 956 | * in parallel, and we need to wait. |
| 957 | * Active mdev pokes us with SIGCHLD to check the new file. |
| 958 | */ |
| 959 | static int |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 960 | wait_for_seqfile(unsigned expected_seq) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 961 | { |
| 962 | /* We time out after 2 sec */ |
| 963 | static const struct timespec ts = { 0, 32*1000*1000 }; |
| 964 | int timeout = 2000 / 32; |
| 965 | int seq_fd = -1; |
| 966 | int do_once = 1; |
| 967 | sigset_t set_CHLD; |
| 968 | |
| 969 | sigemptyset(&set_CHLD); |
| 970 | sigaddset(&set_CHLD, SIGCHLD); |
| 971 | sigprocmask(SIG_BLOCK, &set_CHLD, NULL); |
| 972 | |
| 973 | for (;;) { |
| 974 | int seqlen; |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 975 | char seqbuf[sizeof(long)*3 + 2]; |
| 976 | unsigned seqbufnum; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 977 | |
| 978 | if (seq_fd < 0) { |
| 979 | seq_fd = open("mdev.seq", O_RDWR); |
| 980 | if (seq_fd < 0) |
| 981 | break; |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 982 | close_on_exec_on(seq_fd); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 983 | } |
| 984 | seqlen = pread(seq_fd, seqbuf, sizeof(seqbuf) - 1, 0); |
| 985 | if (seqlen < 0) { |
| 986 | close(seq_fd); |
| 987 | seq_fd = -1; |
| 988 | break; |
| 989 | } |
| 990 | seqbuf[seqlen] = '\0'; |
Denys Vlasenko | 26a8b9f | 2014-09-08 18:19:15 +0200 | [diff] [blame] | 991 | if (seqbuf[0] == '\n' || seqbuf[0] == '\0') { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 992 | /* seed file: write out seq ASAP */ |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 993 | xwrite_str(seq_fd, utoa(expected_seq)); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 994 | xlseek(seq_fd, 0, SEEK_SET); |
| 995 | dbg2("first seq written"); |
| 996 | break; |
| 997 | } |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 998 | seqbufnum = atoll(seqbuf); |
| 999 | if (seqbufnum == expected_seq) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1000 | /* correct idx */ |
| 1001 | break; |
| 1002 | } |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1003 | if (seqbufnum > expected_seq) { |
| 1004 | /* a later mdev runs already (this was seen by users to happen) */ |
| 1005 | /* do not overwrite seqfile on exit */ |
| 1006 | close(seq_fd); |
| 1007 | seq_fd = -1; |
| 1008 | break; |
| 1009 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1010 | if (do_once) { |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1011 | dbg2("%s mdev.seq='%s', need '%u'", curtime(), seqbuf, expected_seq); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1012 | do_once = 0; |
| 1013 | } |
| 1014 | if (sigtimedwait(&set_CHLD, NULL, &ts) >= 0) { |
| 1015 | dbg3("woken up"); |
| 1016 | continue; /* don't decrement timeout! */ |
| 1017 | } |
| 1018 | if (--timeout == 0) { |
Denys Vlasenko | bd77e9d | 2015-04-19 18:54:49 +0200 | [diff] [blame] | 1019 | dbg1("%s mdev.seq='%s'", "timed out", seqbuf); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | sigprocmask(SIG_UNBLOCK, &set_CHLD, NULL); |
| 1024 | return seq_fd; |
| 1025 | } |
| 1026 | |
| 1027 | static void signal_mdevs(unsigned my_pid) |
| 1028 | { |
| 1029 | procps_status_t* p = NULL; |
| 1030 | while ((p = procps_scan(p, PSSCAN_ARGV0)) != NULL) { |
| 1031 | if (p->pid != my_pid |
| 1032 | && p->argv0 |
| 1033 | && strcmp(bb_basename(p->argv0), "mdev") == 0 |
| 1034 | ) { |
| 1035 | kill(p->pid, SIGCHLD); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1040 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 1041 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 1042 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 1043 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 1044 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 1045 | INIT_G(); |
| 1046 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 1047 | #if ENABLE_FEATURE_MDEV_CONF |
| 1048 | G.filename = "/etc/mdev.conf"; |
| 1049 | #endif |
| 1050 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 1051 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 1052 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 1053 | bb_sanitize_stdio(); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 1054 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 1055 | /* Force the configuration file settings exactly */ |
| 1056 | umask(0); |
| 1057 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 1058 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 1059 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 1060 | if (argv[1] && strcmp(argv[1], "-s") == 0) { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 1061 | /* |
| 1062 | * Scan: mdev -s |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 1063 | */ |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 1064 | struct stat st; |
| 1065 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 1066 | #if ENABLE_FEATURE_MDEV_CONF |
| 1067 | /* Same as xrealloc_vector(NULL, 4, 0): */ |
| 1068 | G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec)); |
| 1069 | #endif |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 1070 | xstat("/", &st); |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 1071 | G.root_major = major(st.st_dev); |
| 1072 | G.root_minor = minor(st.st_dev); |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 1073 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 1074 | putenv((char*)"ACTION=add"); |
| 1075 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 1076 | /* Create all devices from /sys/dev hierarchy */ |
| 1077 | recursive_action("/sys/dev", |
| 1078 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
| 1079 | fileAction, dirAction, temp, 0); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 1080 | } else { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 1081 | char *fw; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1082 | char *seq; |
| 1083 | char *action; |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 1084 | char *env_devname; |
| 1085 | char *env_devpath; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1086 | unsigned my_pid; |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1087 | unsigned seqnum = seqnum; /* for compiler */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1088 | int seq_fd; |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 1089 | smalluint op; |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1090 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 1091 | /* Hotplug: |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 1092 | * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1093 | * ACTION can be "add", "remove", "change" |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 1094 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 1095 | */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 1096 | env_devname = getenv("DEVNAME"); /* can be NULL */ |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 1097 | G.subsystem = getenv("SUBSYSTEM"); |
Denys Vlasenko | d35cbad | 2013-04-02 14:37:06 +0200 | [diff] [blame] | 1098 | action = getenv("ACTION"); |
| 1099 | env_devpath = getenv("DEVPATH"); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 1100 | if (!action || !env_devpath /*|| !G.subsystem*/) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 1101 | bb_show_usage(); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 1102 | fw = getenv("FIRMWARE"); |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1103 | seq = getenv("SEQNUM"); |
Denys Vlasenko | d35cbad | 2013-04-02 14:37:06 +0200 | [diff] [blame] | 1104 | op = index_in_strings(keywords, action); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 1105 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1106 | my_pid = getpid(); |
| 1107 | open_mdev_log(seq, my_pid); |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1108 | |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1109 | seq_fd = -1; |
| 1110 | if (seq) { |
| 1111 | seqnum = atoll(seq); |
| 1112 | seq_fd = wait_for_seqfile(seqnum); |
| 1113 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1114 | |
| 1115 | dbg1("%s " |
| 1116 | "ACTION:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s" |
| 1117 | "%s%s", |
| 1118 | curtime(), |
| 1119 | action, G.subsystem, env_devname, env_devpath, |
| 1120 | fw ? " FW:" : "", fw ? fw : "" |
| 1121 | ); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 1122 | |
| 1123 | snprintf(temp, PATH_MAX, "/sys%s", env_devpath); |
Bernhard Reutner-Fischer | 6531f09 | 2009-10-26 23:27:04 +0100 | [diff] [blame] | 1124 | if (op == OP_remove) { |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 1125 | /* Ignoring "remove firmware". It was reported |
| 1126 | * to happen and to cause erroneous deletion |
| 1127 | * of device nodes. */ |
| 1128 | if (!fw) |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 1129 | make_device(env_devname, temp, op); |
Denis Vlasenko | 1fb26da | 2009-04-13 02:15:57 +0000 | [diff] [blame] | 1130 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1131 | else { |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 1132 | make_device(env_devname, temp, op); |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 1133 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1134 | if (op == OP_add && fw) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 1135 | load_firmware(fw, temp); |
| 1136 | } |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 1137 | } |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1138 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1139 | dbg1("%s exiting", curtime()); |
| 1140 | if (seq_fd >= 0) { |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1141 | xwrite_str(seq_fd, utoa(seqnum + 1)); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1142 | signal_mdevs(my_pid); |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1143 | } |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 1146 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1147 | RELEASE_CONFIG_BUFFER(temp); |
| 1148 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 1149 | return EXIT_SUCCESS; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 1150 | } |