"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 | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 11 | //config: bool "mdev (17 kb)" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 12 | //config: default y |
Samuel Thibault | 77216c3 | 2022-10-16 02:04:59 +0200 | [diff] [blame^] | 13 | //config: select PLATFORM_LINUX |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 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. |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 67 | //config: |
| 68 | //config:config FEATURE_MDEV_DAEMON |
| 69 | //config: bool "Support daemon mode" |
| 70 | //config: default y |
| 71 | //config: depends on MDEV |
| 72 | //config: help |
| 73 | //config: Adds the -d option to run mdev in daemon mode handling hotplug |
| 74 | //config: events from the kernel like udev. If the system generates many |
| 75 | //config: hotplug events this mode of operation will consume less |
| 76 | //config: resources than registering mdev as hotplug helper or using the |
| 77 | //config: uevent applet. |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 78 | |
| 79 | //applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP)) |
| 80 | |
| 81 | //kbuild:lib-$(CONFIG_MDEV) += mdev.o |
| 82 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 83 | //usage:#define mdev_trivial_usage |
Denys Vlasenko | 1c9cd9b | 2020-11-22 14:43:35 +0100 | [diff] [blame] | 84 | //usage: "[-vS] " IF_FEATURE_MDEV_DAEMON("{ ") "[-s]" IF_FEATURE_MDEV_DAEMON(" | [-df] }") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 85 | //usage:#define mdev_full_usage "\n\n" |
Denys Vlasenko | d2e0d3f | 2021-10-08 14:30:50 +0200 | [diff] [blame] | 86 | //usage: " -v Verbose\n" |
| 87 | //usage: " -S Log to syslog too\n" |
| 88 | //usage: " -s Scan /sys and populate /dev\n" |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 89 | //usage: IF_FEATURE_MDEV_DAEMON( |
Denys Vlasenko | d2e0d3f | 2021-10-08 14:30:50 +0200 | [diff] [blame] | 90 | //usage: " -d Daemon, listen on netlink\n" |
| 91 | //usage: " -f Run in foreground\n" |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 92 | //usage: ) |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 93 | //usage: "\n" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 94 | //usage: "Bare mdev is a kernel hotplug helper. To activate it:\n" |
| 95 | //usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 96 | //usage: IF_FEATURE_MDEV_CONF( |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 97 | //usage: "\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 98 | //usage: "It uses /etc/mdev.conf with lines\n" |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 99 | //usage: " [-][ENV=regex;]...DEVNAME UID:GID PERM" |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 100 | //usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 101 | //usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]") |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 102 | //usage: "\n" |
| 103 | //usage: "where DEVNAME is device name regex, @major,minor[-minor2], or\n" |
| 104 | //usage: "environment variable regex. A common use of the latter is\n" |
| 105 | //usage: "to load modules for hotplugged devices:\n" |
| 106 | //usage: " $MODALIAS=.* 0:0 660 @modprobe \"$MODALIAS\"\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 107 | //usage: ) |
Denys Vlasenko | 5c6ba6c | 2011-09-19 12:18:11 +0200 | [diff] [blame] | 108 | //usage: "\n" |
| 109 | //usage: "If /dev/mdev.seq file exists, mdev will wait for its value\n" |
| 110 | //usage: "to match $SEQNUM variable. This prevents plug/unplug races.\n" |
Denys Vlasenko | 8d11854 | 2012-05-18 04:47:18 +0200 | [diff] [blame] | 111 | //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] | 112 | //usage: "\n" |
Denys Vlasenko | 8d11854 | 2012-05-18 04:47:18 +0200 | [diff] [blame] | 113 | //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] | 114 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 115 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 116 | #include "common_bufsiz.h" |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 117 | #include "xregex.h" |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 118 | #include <linux/netlink.h> |
Jan Klötzke | e9003ca | 2019-12-16 22:56:53 +0100 | [diff] [blame] | 119 | #include <syslog.h> |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 121 | /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev |
| 122 | * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev |
| 123 | * contains "4:0\n". Directory name is taken as device name, path component |
| 124 | * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty". |
| 125 | * Then mdev creates the /dev/device_name node. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 126 | * If /sys/class/.../dev file does not exist, mdev still may act |
| 127 | * on this device: see "@|$|*command args..." parameter in config file. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 128 | * |
| 129 | * mdev w/o parameters is called as hotplug helper. It takes device |
| 130 | * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts |
| 131 | * maj,min from "/sys/$DEVPATH/dev" and also examines |
| 132 | * $ACTION ("add"/"delete") and $FIRMWARE. |
| 133 | * |
| 134 | * If action is "add", mdev creates /dev/device_name similarly to mdev -s. |
| 135 | * (todo: explain "delete" and $FIRMWARE) |
| 136 | * |
| 137 | * 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] | 138 | * |
| 139 | * Leading minus in 1st field means "don't stop on this line", otherwise |
| 140 | * search is stopped after the matching line is encountered. |
| 141 | * |
Denys Vlasenko | 45f0a11 | 2011-09-22 04:55:47 +0200 | [diff] [blame] | 142 | * $envvar=regex format is useful for loading modules for hot-plugged devices |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 143 | * which do not have driver loaded yet. In this case /sys/class/.../dev |
| 144 | * does not exist, but $MODALIAS is set to needed module's name |
| 145 | * (actually, an alias to it) by kernel. This rule instructs mdev |
| 146 | * to load the module and exit: |
| 147 | * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS" |
| 148 | * The kernel will generate another hotplug event when /sys/class/.../dev |
| 149 | * file appears. |
| 150 | * |
| 151 | * When line matches, the device node is created, chmod'ed and chown'ed, |
| 152 | * moved to path, and if >path, a symlink to moved node is created, |
| 153 | * all this if /sys/class/.../dev exists. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 154 | * Examples: |
| 155 | * =loop/ - moves to /dev/loop |
| 156 | * >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] | 157 | * |
| 158 | * Then "command args..." is executed (via sh -c 'command args...'). |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 159 | * @:execute on creation, $:on deletion, *:on both. |
Denis Vlasenko | 11c17f7 | 2009-04-19 23:38:08 +0000 | [diff] [blame] | 160 | * This happens regardless of /sys/class/.../dev existence. |
Denis Vlasenko | 5ff9629 | 2009-04-17 00:01:04 +0000 | [diff] [blame] | 161 | */ |
| 162 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 163 | /* Kernel's hotplug environment constantly changes. |
| 164 | * Here are new cases I observed on 3.1.0: |
| 165 | * |
| 166 | * Case with $DEVNAME and $DEVICE, not just $DEVPATH: |
| 167 | * ACTION=add |
| 168 | * BUSNUM=001 |
| 169 | * DEVICE=/proc/bus/usb/001/003 |
| 170 | * DEVNAME=bus/usb/001/003 |
| 171 | * DEVNUM=003 |
| 172 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5 |
| 173 | * DEVTYPE=usb_device |
| 174 | * MAJOR=189 |
| 175 | * MINOR=2 |
| 176 | * PRODUCT=18d1/4e12/227 |
| 177 | * SUBSYSTEM=usb |
| 178 | * TYPE=0/0/0 |
| 179 | * |
| 180 | * Case with $DEVICE, but no $DEVNAME - apparenty, usb iface notification? |
| 181 | * "Please load me a module" thing? |
| 182 | * ACTION=add |
| 183 | * DEVICE=/proc/bus/usb/001/003 |
| 184 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0 |
| 185 | * DEVTYPE=usb_interface |
| 186 | * INTERFACE=8/6/80 |
| 187 | * MODALIAS=usb:v18D1p4E12d0227dc00dsc00dp00ic08isc06ip50 |
| 188 | * PRODUCT=18d1/4e12/227 |
| 189 | * SUBSYSTEM=usb |
| 190 | * TYPE=0/0/0 |
| 191 | * |
| 192 | * ACTION=add |
| 193 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5 |
| 194 | * DEVTYPE=scsi_host |
| 195 | * SUBSYSTEM=scsi |
| 196 | * |
| 197 | * ACTION=add |
| 198 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/scsi_host/host5 |
| 199 | * SUBSYSTEM=scsi_host |
| 200 | * |
| 201 | * ACTION=add |
| 202 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0 |
| 203 | * DEVTYPE=scsi_target |
| 204 | * SUBSYSTEM=scsi |
| 205 | * |
| 206 | * Case with strange $MODALIAS: |
| 207 | * ACTION=add |
| 208 | * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0 |
| 209 | * DEVTYPE=scsi_device |
| 210 | * MODALIAS=scsi:t-0x00 |
| 211 | * SUBSYSTEM=scsi |
| 212 | * |
| 213 | * ACTION=add |
| 214 | * 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 |
| 215 | * SUBSYSTEM=scsi_disk |
| 216 | * |
| 217 | * ACTION=add |
| 218 | * 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 |
| 219 | * SUBSYSTEM=scsi_device |
| 220 | * |
| 221 | * Case with explicit $MAJOR/$MINOR (no need to read /sys/$DEVPATH/dev?): |
| 222 | * ACTION=add |
| 223 | * DEVNAME=bsg/5:0:0:0 |
| 224 | * 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 |
| 225 | * MAJOR=253 |
| 226 | * MINOR=1 |
| 227 | * SUBSYSTEM=bsg |
| 228 | * |
| 229 | * ACTION=add |
| 230 | * DEVPATH=/devices/virtual/bdi/8:16 |
| 231 | * SUBSYSTEM=bdi |
| 232 | * |
| 233 | * ACTION=add |
| 234 | * DEVNAME=sdb |
| 235 | * DEVPATH=/block/sdb |
| 236 | * DEVTYPE=disk |
| 237 | * MAJOR=8 |
| 238 | * MINOR=16 |
| 239 | * SUBSYSTEM=block |
| 240 | * |
| 241 | * Case with ACTION=change: |
| 242 | * ACTION=change |
| 243 | * DEVNAME=sdb |
| 244 | * DEVPATH=/block/sdb |
| 245 | * DEVTYPE=disk |
| 246 | * DISK_MEDIA_CHANGE=1 |
| 247 | * MAJOR=8 |
| 248 | * MINOR=16 |
| 249 | * SUBSYSTEM=block |
| 250 | */ |
| 251 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 252 | #define DEBUG_LVL 2 |
| 253 | |
| 254 | #if DEBUG_LVL >= 1 |
| 255 | # define dbg1(...) do { if (G.verbose) bb_error_msg(__VA_ARGS__); } while(0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 256 | # define dbg1s(msg) do { if (G.verbose) bb_simple_error_msg(msg); } while(0) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 257 | #else |
| 258 | # define dbg1(...) ((void)0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 259 | # define dbg1s(msg) ((void)0) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 260 | #endif |
| 261 | #if DEBUG_LVL >= 2 |
| 262 | # define dbg2(...) do { if (G.verbose >= 2) bb_error_msg(__VA_ARGS__); } while(0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 263 | # define dbg2s(msg) do { if (G.verbose >= 2) bb_simple_error_msg(msg); } while(0) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 264 | #else |
| 265 | # define dbg2(...) ((void)0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 266 | # define dbg2s(msg) ((void)0) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 267 | #endif |
| 268 | #if DEBUG_LVL >= 3 |
| 269 | # define dbg3(...) do { if (G.verbose >= 3) bb_error_msg(__VA_ARGS__); } while(0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 270 | # define dbg3s(msg) do { if (G.verbose >= 3) bb_simple_error_msg(msg); } while(0) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 271 | #else |
| 272 | # define dbg3(...) ((void)0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 273 | # define dbg3s(msg) ((void)0) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 274 | #endif |
| 275 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 276 | static const char keywords[] ALIGN1 = "add\0remove\0"; // "change\0" |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 277 | enum { OP_add, OP_remove }; |
| 278 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 279 | struct envmatch { |
| 280 | struct envmatch *next; |
| 281 | char *envname; |
| 282 | regex_t match; |
| 283 | }; |
| 284 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 285 | struct rule { |
| 286 | bool keep_matching; |
| 287 | bool regex_compiled; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 288 | mode_t mode; |
| 289 | int maj, min0, min1; |
| 290 | struct bb_uidgid_t ugid; |
| 291 | char *envvar; |
| 292 | char *ren_mov; |
| 293 | IF_FEATURE_MDEV_EXEC(char *r_cmd;) |
| 294 | regex_t match; |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 295 | struct envmatch *envmatch; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 296 | }; |
| 297 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 298 | struct globals { |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 299 | int root_major, root_minor; |
Jan Klötzke | 768153b | 2019-12-16 22:56:54 +0100 | [diff] [blame] | 300 | int verbose; |
Denis Vlasenko | 065c714 | 2009-04-13 22:23:02 +0000 | [diff] [blame] | 301 | char *subsystem; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 302 | char *subsys_env; /* for putenv("SUBSYSTEM=subsystem") */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 303 | #if ENABLE_FEATURE_MDEV_CONF |
| 304 | const char *filename; |
| 305 | parser_t *parser; |
| 306 | struct rule **rule_vec; |
| 307 | unsigned rule_idx; |
| 308 | #endif |
| 309 | struct rule cur_rule; |
Denys Vlasenko | bd77e9d | 2015-04-19 18:54:49 +0200 | [diff] [blame] | 310 | char timestr[sizeof("HH:MM:SS.123456")]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 311 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 312 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | c966563 | 2012-04-21 14:35:32 +0200 | [diff] [blame] | 313 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 314 | setup_common_bufsiz(); \ |
Denys Vlasenko | c966563 | 2012-04-21 14:35:32 +0200 | [diff] [blame] | 315 | IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \ |
| 316 | IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \ |
| 317 | } while (0) |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 318 | |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 319 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 320 | /* Prevent infinite loops in /sys symlinks */ |
| 321 | #define MAX_SYSFS_DEPTH 3 |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 322 | |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 323 | /* We use additional bytes in make_device() */ |
| 324 | #define SCRATCH_SIZE 128 |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 325 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 326 | #if ENABLE_FEATURE_MDEV_CONF |
| 327 | |
| 328 | static void make_default_cur_rule(void) |
| 329 | { |
| 330 | memset(&G.cur_rule, 0, sizeof(G.cur_rule)); |
| 331 | G.cur_rule.maj = -1; /* "not a @major,minor rule" */ |
| 332 | G.cur_rule.mode = 0660; |
| 333 | } |
| 334 | |
| 335 | static void clean_up_cur_rule(void) |
| 336 | { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 337 | struct envmatch *e; |
| 338 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 339 | free(G.cur_rule.envvar); |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 340 | free(G.cur_rule.ren_mov); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 341 | if (G.cur_rule.regex_compiled) |
| 342 | regfree(&G.cur_rule.match); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 343 | IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);) |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 344 | e = G.cur_rule.envmatch; |
| 345 | while (e) { |
| 346 | free(e->envname); |
| 347 | regfree(&e->match); |
| 348 | e = e->next; |
| 349 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 350 | make_default_cur_rule(); |
| 351 | } |
| 352 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 353 | static char *parse_envmatch_pfx(char *val) |
| 354 | { |
| 355 | struct envmatch **nextp = &G.cur_rule.envmatch; |
| 356 | |
| 357 | for (;;) { |
| 358 | struct envmatch *e; |
| 359 | char *semicolon; |
| 360 | char *eq = strchr(val, '='); |
| 361 | if (!eq /* || eq == val? */) |
| 362 | return val; |
| 363 | if (endofname(val) != eq) |
| 364 | return val; |
| 365 | semicolon = strchr(eq, ';'); |
| 366 | if (!semicolon) |
| 367 | return val; |
| 368 | /* ENVVAR=regex;... */ |
| 369 | *nextp = e = xzalloc(sizeof(*e)); |
| 370 | nextp = &e->next; |
| 371 | e->envname = xstrndup(val, eq - val); |
| 372 | *semicolon = '\0'; |
| 373 | xregcomp(&e->match, eq + 1, REG_EXTENDED); |
| 374 | *semicolon = ';'; |
| 375 | val = semicolon + 1; |
| 376 | } |
| 377 | } |
| 378 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 379 | static void parse_next_rule(void) |
| 380 | { |
| 381 | /* Note: on entry, G.cur_rule is set to default */ |
| 382 | while (1) { |
| 383 | char *tokens[4]; |
| 384 | char *val; |
| 385 | |
Denys Vlasenko | 499597d | 2012-01-11 00:37:17 +0100 | [diff] [blame] | 386 | /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */ |
| 387 | 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] | 388 | break; |
| 389 | |
| 390 | /* Fields: [-]regex uid:gid mode [alias] [cmd] */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 391 | dbg3("token1:'%s'", tokens[1]); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 392 | |
| 393 | /* 1st field */ |
| 394 | val = tokens[0]; |
| 395 | G.cur_rule.keep_matching = ('-' == val[0]); |
| 396 | val += G.cur_rule.keep_matching; /* swallow leading dash */ |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 397 | val = parse_envmatch_pfx(val); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 398 | if (val[0] == '@') { |
| 399 | /* @major,minor[-minor2] */ |
| 400 | /* (useful when name is ambiguous: |
| 401 | * "/sys/class/usb/lp0" and |
| 402 | * "/sys/class/printer/lp0") |
| 403 | */ |
| 404 | int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1); |
| 405 | if (sc < 2 || G.cur_rule.maj < 0) { |
| 406 | bb_error_msg("bad @maj,min on line %d", G.parser->lineno); |
| 407 | goto next_rule; |
| 408 | } |
| 409 | if (sc == 2) |
| 410 | G.cur_rule.min1 = G.cur_rule.min0; |
| 411 | } else { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 412 | char *eq = strchr(val, '='); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 413 | if (val[0] == '$') { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 414 | /* $ENVVAR=regex ... */ |
| 415 | val++; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 416 | if (!eq) { |
| 417 | bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno); |
| 418 | goto next_rule; |
| 419 | } |
| 420 | G.cur_rule.envvar = xstrndup(val, eq - val); |
| 421 | val = eq + 1; |
| 422 | } |
| 423 | xregcomp(&G.cur_rule.match, val, REG_EXTENDED); |
| 424 | G.cur_rule.regex_compiled = 1; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* 2nd field: uid:gid - device ownership */ |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 428 | if (get_uidgid(&G.cur_rule.ugid, tokens[1]) == 0) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 429 | bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno); |
| 430 | goto next_rule; |
| 431 | } |
| 432 | |
| 433 | /* 3rd field: mode - device permissions */ |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 434 | 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] | 435 | |
| 436 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ |
| 437 | val = tokens[3]; |
| 438 | if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) { |
| 439 | char *s = skip_non_whitespace(val); |
| 440 | G.cur_rule.ren_mov = xstrndup(val, s - val); |
| 441 | val = skip_whitespace(s); |
| 442 | } |
| 443 | |
| 444 | if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) { |
| 445 | const char *s = "$@*"; |
| 446 | const char *s2 = strchr(s, val[0]); |
| 447 | if (!s2) { |
| 448 | bb_error_msg("bad line %u", G.parser->lineno); |
| 449 | goto next_rule; |
| 450 | } |
| 451 | IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);) |
| 452 | } |
| 453 | |
| 454 | return; |
| 455 | next_rule: |
| 456 | clean_up_cur_rule(); |
| 457 | } /* while (config_read) */ |
| 458 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 459 | dbg3("config_close(G.parser)"); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 460 | config_close(G.parser); |
| 461 | G.parser = NULL; |
| 462 | |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | /* If mdev -s, we remember rules in G.rule_vec[]. |
| 467 | * Otherwise, there is no point in doing it, and we just |
| 468 | * save only one parsed rule in G.cur_rule. |
| 469 | */ |
| 470 | static const struct rule *next_rule(void) |
| 471 | { |
| 472 | struct rule *rule; |
| 473 | |
| 474 | /* Open conf file if we didn't do it yet */ |
| 475 | if (!G.parser && G.filename) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 476 | dbg3("config_open('%s')", G.filename); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 477 | G.parser = config_open2(G.filename, fopen_for_read); |
| 478 | G.filename = NULL; |
| 479 | } |
| 480 | |
| 481 | if (G.rule_vec) { |
| 482 | /* mdev -s */ |
| 483 | /* Do we have rule parsed already? */ |
| 484 | if (G.rule_vec[G.rule_idx]) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 485 | 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] | 486 | return G.rule_vec[G.rule_idx++]; |
| 487 | } |
| 488 | make_default_cur_rule(); |
| 489 | } else { |
| 490 | /* not mdev -s */ |
| 491 | clean_up_cur_rule(); |
| 492 | } |
| 493 | |
| 494 | /* Parse one more rule if file isn't fully read */ |
| 495 | rule = &G.cur_rule; |
| 496 | if (G.parser) { |
| 497 | parse_next_rule(); |
| 498 | if (G.rule_vec) { /* mdev -s */ |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 499 | rule = xmemdup(&G.cur_rule, sizeof(G.cur_rule)); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 500 | G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx); |
| 501 | G.rule_vec[G.rule_idx++] = rule; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 502 | 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] | 503 | } |
| 504 | } |
| 505 | |
| 506 | return rule; |
| 507 | } |
| 508 | |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 509 | static int env_matches(struct envmatch *e) |
| 510 | { |
| 511 | while (e) { |
| 512 | int r; |
| 513 | char *val = getenv(e->envname); |
| 514 | if (!val) |
| 515 | return 0; |
| 516 | r = regexec(&e->match, val, /*size*/ 0, /*range[]*/ NULL, /*eflags*/ 0); |
| 517 | if (r != 0) /* no match */ |
| 518 | return 0; |
| 519 | e = e->next; |
| 520 | } |
| 521 | return 1; |
| 522 | } |
| 523 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 524 | #else |
| 525 | |
| 526 | # define next_rule() (&G.cur_rule) |
| 527 | |
| 528 | #endif |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 529 | |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 530 | static void mkdir_recursive(char *name) |
| 531 | { |
| 532 | /* if name has many levels ("dir1/dir2"), |
| 533 | * bb_make_directory() will create dir1 according to umask, |
| 534 | * not according to its "mode" parameter. |
| 535 | * Since we run with umask=0, need to temporarily switch it. |
| 536 | */ |
| 537 | umask(022); /* "dir1" (if any) will be 0755 too */ |
| 538 | bb_make_directory(name, 0755, FILEUTILS_RECUR); |
| 539 | umask(0); |
| 540 | } |
| 541 | |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 542 | /* Builds an alias path. |
| 543 | * This function potentionally reallocates the alias parameter. |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 544 | * Only used for ENABLE_FEATURE_MDEV_RENAME |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 545 | */ |
| 546 | static char *build_alias(char *alias, const char *device_name) |
| 547 | { |
| 548 | char *dest; |
| 549 | |
| 550 | /* ">bar/": rename to bar/device_name */ |
| 551 | /* ">bar[/]baz": rename to bar[/]baz */ |
| 552 | dest = strrchr(alias, '/'); |
| 553 | if (dest) { /* ">bar/[baz]" ? */ |
| 554 | *dest = '\0'; /* mkdir bar */ |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 555 | mkdir_recursive(alias); |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 556 | *dest = '/'; |
| 557 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ |
| 558 | dest = alias; |
| 559 | alias = concat_path_file(alias, device_name); |
| 560 | free(dest); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return alias; |
| 565 | } |
| 566 | |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 567 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" |
| 568 | * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 569 | * 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] | 570 | * 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] | 571 | * |
| 572 | * device_name = $DEVNAME (may be NULL) |
| 573 | * path = /sys/$DEVPATH |
Denys Vlasenko | 76f5e38 | 2009-05-04 21:59:05 +0200 | [diff] [blame] | 574 | */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 575 | static void make_device(char *device_name, char *path, int operation) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 576 | { |
Denis Vlasenko | f46be09 | 2006-10-16 19:39:37 +0000 | [diff] [blame] | 577 | int major, minor, type, len; |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 578 | char *path_end = path + strlen(path); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 579 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 580 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 581 | * 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] | 582 | * because sscanf() will stop at the first nondigit, which \n is. |
| 583 | * We also depend on path having writeable space after it. |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 584 | */ |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 585 | major = -1; |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 586 | if (operation == OP_add) { |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 587 | strcpy(path_end, "/dev"); |
| 588 | len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1); |
| 589 | *path_end = '\0'; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 590 | if (len < 1) { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 591 | if (!ENABLE_FEATURE_MDEV_EXEC) |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 592 | return; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 593 | /* no "dev" file, but we can still run scripts |
| 594 | * based on device name */ |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 595 | } else if (sscanf(path_end + 1, "%u:%u", &major, &minor) == 2) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 596 | dbg1("dev %u,%u", major, minor); |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 597 | } else { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 598 | major = -1; |
Mike Frysinger | ae86a33 | 2008-02-20 18:31:36 +0000 | [diff] [blame] | 599 | } |
Rob Landley | 10b36f9 | 2006-08-10 01:09:37 +0000 | [diff] [blame] | 600 | } |
Denys Vlasenko | f2860bf | 2010-08-16 14:26:15 +0200 | [diff] [blame] | 601 | /* else: for delete, -1 still deletes the node, but < -1 suppresses that */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 602 | |
Nikolaus Voss | c3cf1e3 | 2013-05-13 03:55:11 +0200 | [diff] [blame] | 603 | /* Determine device name */ |
| 604 | if (!device_name) { |
| 605 | /* |
| 606 | * There was no $DEVNAME envvar (for example, mdev -s never has). |
| 607 | * But it is very useful: it contains the *path*, not only basename, |
| 608 | * Thankfully, uevent file has it. |
| 609 | * Example of .../sound/card0/controlC0/uevent file on Linux-3.7.7: |
| 610 | * MAJOR=116 |
| 611 | * MINOR=7 |
| 612 | * DEVNAME=snd/controlC0 |
| 613 | */ |
| 614 | strcpy(path_end, "/uevent"); |
| 615 | len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1); |
| 616 | if (len < 0) |
| 617 | len = 0; |
| 618 | *path_end = '\0'; |
| 619 | path_end[1 + len] = '\0'; |
| 620 | device_name = strstr(path_end + 1, "\nDEVNAME="); |
| 621 | if (device_name) { |
| 622 | device_name += sizeof("\nDEVNAME=")-1; |
| 623 | strchrnul(device_name, '\n')[0] = '\0'; |
| 624 | } else { |
| 625 | /* Fall back to just basename */ |
| 626 | device_name = (char*) bb_basename(path); |
| 627 | } |
| 628 | } |
| 629 | /* Determine device type */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 630 | /* |
| 631 | * http://kernel.org/doc/pending/hotplug.txt says that only |
Denis Vlasenko | a87045c | 2008-07-12 10:28:41 +0000 | [diff] [blame] | 632 | * "/sys/block/..." is for block devices. "/sys/bus" etc is not. |
Denys Vlasenko | 64bb95a | 2010-02-24 10:39:47 +0100 | [diff] [blame] | 633 | * 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] | 634 | * We use strstr("/block/") to forestall future surprises. |
| 635 | */ |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 636 | type = S_IFCHR; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 637 | if (strstr(path, "/block/") || (G.subsystem && is_prefixed_with(G.subsystem, "block"))) |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 638 | type = S_IFBLK; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 639 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 640 | #if ENABLE_FEATURE_MDEV_CONF |
| 641 | G.rule_idx = 0; /* restart from the beginning (think mdev -s) */ |
| 642 | #endif |
| 643 | for (;;) { |
| 644 | const char *str_to_match; |
| 645 | regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
| 646 | char *command; |
| 647 | char *alias; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 648 | char aliaslink = aliaslink; /* for compiler */ |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 649 | char *node_name; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 650 | const struct rule *rule; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 651 | |
Denys Vlasenko | b6beada | 2012-05-18 15:11:16 +0200 | [diff] [blame] | 652 | str_to_match = device_name; |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 653 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 654 | rule = next_rule(); |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 655 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 656 | #if ENABLE_FEATURE_MDEV_CONF |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 657 | if (!env_matches(rule->envmatch)) |
| 658 | continue; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 659 | if (rule->maj >= 0) { /* @maj,min rule */ |
| 660 | if (major != rule->maj) |
| 661 | continue; |
| 662 | if (minor < rule->min0 || minor > rule->min1) |
| 663 | continue; |
| 664 | memset(off, 0, sizeof(off)); |
| 665 | goto rule_matches; |
| 666 | } |
| 667 | if (rule->envvar) { /* $envvar=regex rule */ |
| 668 | str_to_match = getenv(rule->envvar); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 669 | dbg3("getenv('%s'):'%s'", rule->envvar, str_to_match); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 670 | if (!str_to_match) |
| 671 | continue; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 672 | } |
Denys Vlasenko | b6beada | 2012-05-18 15:11:16 +0200 | [diff] [blame] | 673 | /* else: str_to_match = device_name */ |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 674 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 675 | if (rule->regex_compiled) { |
| 676 | 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] | 677 | dbg3("regex_match for '%s':%d", str_to_match, regex_match); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 678 | //bb_error_msg("matches:"); |
| 679 | //for (int i = 0; i < ARRAY_SIZE(off); i++) { |
| 680 | // if (off[i].rm_so < 0) continue; |
| 681 | // bb_error_msg("match %d: '%.*s'\n", i, |
| 682 | // (int)(off[i].rm_eo - off[i].rm_so), |
| 683 | // device_name + off[i].rm_so); |
| 684 | //} |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 685 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 686 | if (regex_match != 0 |
| 687 | /* regexec returns whole pattern as "range" 0 */ |
| 688 | || off[0].rm_so != 0 |
| 689 | || (int)off[0].rm_eo != (int)strlen(str_to_match) |
| 690 | ) { |
| 691 | continue; /* this rule doesn't match */ |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 692 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 693 | } |
| 694 | /* else: it's final implicit "match-all" rule */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 695 | rule_matches: |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 696 | dbg2("rule matched, line %d", G.parser ? G.parser->lineno : -1); |
Denys Vlasenko | 4424dfd | 2013-03-17 23:01:17 +0100 | [diff] [blame] | 697 | #endif |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 698 | /* Build alias name */ |
| 699 | alias = NULL; |
| 700 | if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) { |
| 701 | aliaslink = rule->ren_mov[0]; |
| 702 | if (aliaslink == '!') { |
| 703 | /* "!": suppress node creation/deletion */ |
| 704 | major = -2; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 705 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 706 | else if (aliaslink == '>' || aliaslink == '=') { |
| 707 | if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) { |
| 708 | char *s; |
| 709 | char *p; |
| 710 | unsigned n; |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 711 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 712 | /* substitute %1..9 with off[1..9], if any */ |
| 713 | n = 0; |
| 714 | s = rule->ren_mov; |
| 715 | while (*s) |
| 716 | if (*s++ == '%') |
| 717 | n++; |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 718 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 719 | p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match)); |
| 720 | s = rule->ren_mov + 1; |
| 721 | while (*s) { |
| 722 | *p = *s; |
| 723 | if ('%' == *s) { |
| 724 | unsigned i = (s[1] - '0'); |
| 725 | if (i <= 9 && off[i].rm_so >= 0) { |
| 726 | n = off[i].rm_eo - off[i].rm_so; |
| 727 | strncpy(p, str_to_match + off[i].rm_so, n); |
| 728 | p += n - 1; |
| 729 | s++; |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 730 | } |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 731 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 732 | p++; |
| 733 | s++; |
Denis Vlasenko | f2f3868 | 2008-03-29 17:26:10 +0000 | [diff] [blame] | 734 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 735 | } else { |
| 736 | alias = xstrdup(rule->ren_mov + 1); |
Denys Vlasenko | b38af7b | 2010-04-02 06:47:14 +0200 | [diff] [blame] | 737 | } |
Denis Vlasenko | 4461564 | 2008-03-29 13:10:57 +0000 | [diff] [blame] | 738 | } |
Denis Vlasenko | df96df9 | 2008-07-26 18:35:10 +0000 | [diff] [blame] | 739 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 740 | dbg3("alias:'%s'", alias); |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 741 | |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 742 | command = NULL; |
| 743 | IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;) |
| 744 | if (command) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 745 | /* Are we running this command now? |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 746 | * Run @cmd on create, $cmd on delete, *cmd on any |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 747 | */ |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 748 | if ((command[0] == '@' && operation == OP_add) |
| 749 | || (command[0] == '$' && operation == OP_remove) |
| 750 | || (command[0] == '*') |
| 751 | ) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 752 | command++; |
| 753 | } else { |
| 754 | command = NULL; |
| 755 | } |
| 756 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 757 | dbg3("command:'%s'", command); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 758 | |
| 759 | /* "Execute" the line we found */ |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 760 | node_name = device_name; |
| 761 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
| 762 | node_name = alias = build_alias(alias, device_name); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 763 | dbg3("alias2:'%s'", alias); |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 766 | if (operation == OP_add && major >= 0) { |
| 767 | char *slash = strrchr(node_name, '/'); |
| 768 | if (slash) { |
| 769 | *slash = '\0'; |
Denys Vlasenko | 4609f47 | 2013-01-21 01:22:12 +0100 | [diff] [blame] | 770 | mkdir_recursive(node_name); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 771 | *slash = '/'; |
| 772 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 773 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 774 | dbg1("mknod %s (%d,%d) %o" |
| 775 | " %u:%u", |
| 776 | node_name, major, minor, rule->mode | type, |
| 777 | rule->ugid.uid, rule->ugid.gid |
| 778 | ); |
| 779 | } else { |
| 780 | dbg1("mknod %s (%d,%d) %o", |
| 781 | node_name, major, minor, rule->mode | type |
| 782 | ); |
| 783 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 784 | if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST) |
| 785 | bb_perror_msg("can't create '%s'", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 786 | if (ENABLE_FEATURE_MDEV_CONF) { |
| 787 | chmod(node_name, rule->mode); |
| 788 | chown(node_name, rule->ugid.uid, rule->ugid.gid); |
| 789 | } |
Denys Vlasenko | ea57f51 | 2012-05-18 11:39:57 +0200 | [diff] [blame] | 790 | if (major == G.root_major && minor == G.root_minor) |
| 791 | symlink(node_name, "root"); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 792 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
Denys Vlasenko | 638dbc3 | 2012-02-06 01:00:00 +0100 | [diff] [blame] | 793 | if (aliaslink == '>') { |
| 794 | //TODO: on devtmpfs, device_name already exists and symlink() fails. |
| 795 | //End result is that instead of symlink, we have two nodes. |
| 796 | //What should be done? |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 797 | dbg1("symlink: %s", device_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 798 | symlink(node_name, device_name); |
Denys Vlasenko | 638dbc3 | 2012-02-06 01:00:00 +0100 | [diff] [blame] | 799 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | if (ENABLE_FEATURE_MDEV_EXEC && command) { |
| 804 | /* setenv will leak memory, use putenv/unsetenv/free */ |
| 805 | char *s = xasprintf("%s=%s", "MDEV", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 806 | putenv(s); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 807 | dbg1("running: %s", command); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 808 | if (system(command) == -1) |
| 809 | bb_perror_msg("can't run '%s'", command); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 810 | bb_unsetenv_and_free(s); |
| 811 | } |
| 812 | |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 813 | if (operation == OP_remove && major >= -1) { |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 814 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 815 | if (aliaslink == '>') { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 816 | dbg1("unlink: %s", device_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 817 | unlink(device_name); |
Denys Vlasenko | 516530c | 2012-05-18 04:45:35 +0200 | [diff] [blame] | 818 | } |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 819 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 820 | dbg1("unlink: %s", node_name); |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 821 | unlink(node_name); |
| 822 | } |
| 823 | |
| 824 | if (ENABLE_FEATURE_MDEV_RENAME) |
| 825 | free(alias); |
| 826 | |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 827 | /* We found matching line. |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 828 | * Stop unless it was prefixed with '-' |
| 829 | */ |
| 830 | if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching) |
Denis Vlasenko | f2b39e0 | 2009-04-13 23:18:52 +0000 | [diff] [blame] | 831 | break; |
Denys Vlasenko | 3eccd7e | 2011-09-22 03:46:30 +0200 | [diff] [blame] | 832 | } /* for (;;) */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Denys Vlasenko | 51283b8 | 2018-12-08 15:41:17 +0100 | [diff] [blame] | 835 | static ssize_t readlink2(char *buf, size_t bufsize) |
| 836 | { |
| 837 | // Grr... gcc 8.1.1: |
| 838 | // "passing argument 2 to restrict-qualified parameter aliases with argument 1" |
| 839 | // dance around that... |
| 840 | char *obuf FIX_ALIASING; |
| 841 | obuf = buf; |
| 842 | return readlink(buf, obuf, bufsize); |
| 843 | } |
| 844 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 845 | /* File callback for /sys/ traversal. |
| 846 | * We act only on "/sys/.../dev" (pseudo)file |
| 847 | */ |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 848 | static int FAST_FUNC fileAction(struct recursive_state *state, |
| 849 | const char *fileName, |
| 850 | struct stat *statbuf UNUSED_PARAM) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 851 | { |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 852 | size_t len = strlen(fileName) - 4; /* can't underflow */ |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 853 | char *path = state->userData; /* char array[PATH_MAX + SCRATCH_SIZE] */ |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 854 | char subsys[PATH_MAX]; |
| 855 | int res; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 856 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 857 | /* Is it a ".../dev" file? (len check is for paranoid reasons) */ |
| 858 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX - 32) |
| 859 | return FALSE; /* not .../dev */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 860 | |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 861 | strcpy(path, fileName); |
| 862 | path[len] = '\0'; |
| 863 | |
| 864 | /* Read ".../subsystem" symlink in the same directory where ".../dev" is */ |
| 865 | strcpy(subsys, path); |
| 866 | strcpy(subsys + len, "/subsystem"); |
Denys Vlasenko | 51283b8 | 2018-12-08 15:41:17 +0100 | [diff] [blame] | 867 | res = readlink2(subsys, sizeof(subsys)-1); |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 868 | if (res > 0) { |
| 869 | subsys[res] = '\0'; |
| 870 | free(G.subsystem); |
| 871 | if (G.subsys_env) { |
| 872 | bb_unsetenv_and_free(G.subsys_env); |
| 873 | G.subsys_env = NULL; |
| 874 | } |
| 875 | /* Set G.subsystem and $SUBSYSTEM from symlink's last component */ |
| 876 | G.subsystem = strrchr(subsys, '/'); |
| 877 | if (G.subsystem) { |
| 878 | G.subsystem = xstrdup(G.subsystem + 1); |
| 879 | G.subsys_env = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem); |
| 880 | putenv(G.subsys_env); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | make_device(/*DEVNAME:*/ NULL, path, OP_add); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 885 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 886 | return TRUE; |
| 887 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 888 | |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 889 | /* Directory callback for /sys/ traversal */ |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 890 | static int FAST_FUNC dirAction(struct recursive_state *state, |
| 891 | const char *fileName UNUSED_PARAM, |
| 892 | struct stat *statbuf UNUSED_PARAM) |
Mike Frysinger | b51fd35 | 2007-06-13 09:24:50 +0000 | [diff] [blame] | 893 | { |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 894 | return (state->depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 897 | /* For the full gory details, see linux/Documentation/firmware_class/README |
| 898 | * |
| 899 | * Firmware loading works like this: |
| 900 | * - kernel sets FIRMWARE env var |
| 901 | * - userspace checks /lib/firmware/$FIRMWARE |
| 902 | * - userspace waits for /sys/$DEVPATH/loading to appear |
| 903 | * - userspace writes "1" to /sys/$DEVPATH/loading |
| 904 | * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data |
| 905 | * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading |
| 906 | * - kernel loads firmware into device |
| 907 | */ |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 908 | static void load_firmware(const char *firmware, const char *sysfs_path) |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 909 | { |
| 910 | int cnt; |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 911 | int firmware_fd, loading_fd; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 912 | |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 913 | /* check for /lib/firmware/$FIRMWARE */ |
Denys Vlasenko | 20a3262 | 2016-09-07 14:09:01 +0200 | [diff] [blame] | 914 | firmware_fd = -1; |
| 915 | if (chdir("/lib/firmware") == 0) |
| 916 | firmware_fd = open(firmware, O_RDONLY); /* can fail */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 917 | |
| 918 | /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */ |
| 919 | xchdir(sysfs_path); |
| 920 | for (cnt = 0; cnt < 30; ++cnt) { |
| 921 | loading_fd = open("loading", O_WRONLY); |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 922 | if (loading_fd >= 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 923 | goto loading; |
Denys Vlasenko | ec16c03 | 2020-11-29 11:37:34 +0100 | [diff] [blame] | 924 | sleep1(); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 925 | } |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 926 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 927 | |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 928 | loading: |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 929 | cnt = 0; |
| 930 | if (firmware_fd >= 0) { |
| 931 | int data_fd; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 932 | |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 933 | /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */ |
| 934 | if (full_write(loading_fd, "1", 1) != 1) |
| 935 | goto out; |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 936 | |
Christoph Lohmann | 25cb5aa | 2012-05-19 23:39:45 +0200 | [diff] [blame] | 937 | /* load firmware into /sys/$DEVPATH/data */ |
| 938 | data_fd = open("data", O_WRONLY); |
| 939 | if (data_fd < 0) |
| 940 | goto out; |
| 941 | cnt = bb_copyfd_eof(firmware_fd, data_fd); |
| 942 | if (ENABLE_FEATURE_CLEAN_UP) |
| 943 | close(data_fd); |
| 944 | } |
| 945 | |
| 946 | /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" |
| 947 | * Note: we emit -1 also if firmware file wasn't found. |
| 948 | * There are cases when otherwise kernel would wait for minutes |
| 949 | * before timing out. |
| 950 | */ |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 951 | if (cnt > 0) |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 952 | full_write(loading_fd, "0", 1); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 953 | else |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 954 | full_write(loading_fd, "-1", 2); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 955 | |
| 956 | out: |
Denys Vlasenko | ba76b7a | 2013-02-04 23:35:27 +0100 | [diff] [blame] | 957 | xchdir("/dev"); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 958 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 959 | close(firmware_fd); |
| 960 | close(loading_fd); |
Mike Frysinger | a78ef2c | 2007-06-13 07:34:15 +0000 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 964 | static char *curtime(void) |
| 965 | { |
| 966 | struct timeval tv; |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 967 | xgettimeofday(&tv); |
Denys Vlasenko | bd77e9d | 2015-04-19 18:54:49 +0200 | [diff] [blame] | 968 | sprintf( |
| 969 | strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec), |
| 970 | ".%06u", |
| 971 | (unsigned)tv.tv_usec |
| 972 | ); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 973 | return G.timestr; |
| 974 | } |
| 975 | |
| 976 | static void open_mdev_log(const char *seq, unsigned my_pid) |
| 977 | { |
| 978 | int logfd = open("mdev.log", O_WRONLY | O_APPEND); |
| 979 | if (logfd >= 0) { |
| 980 | xmove_fd(logfd, STDERR_FILENO); |
| 981 | G.verbose = 2; |
| 982 | applet_name = xasprintf("%s[%s]", applet_name, seq ? seq : utoa(my_pid)); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /* If it exists, does /dev/mdev.seq match $SEQNUM? |
| 987 | * If it does not match, earlier mdev is running |
| 988 | * in parallel, and we need to wait. |
| 989 | * Active mdev pokes us with SIGCHLD to check the new file. |
| 990 | */ |
| 991 | static int |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 992 | wait_for_seqfile(unsigned expected_seq) |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 993 | { |
| 994 | /* We time out after 2 sec */ |
| 995 | static const struct timespec ts = { 0, 32*1000*1000 }; |
| 996 | int timeout = 2000 / 32; |
| 997 | int seq_fd = -1; |
| 998 | int do_once = 1; |
| 999 | sigset_t set_CHLD; |
| 1000 | |
| 1001 | sigemptyset(&set_CHLD); |
| 1002 | sigaddset(&set_CHLD, SIGCHLD); |
| 1003 | sigprocmask(SIG_BLOCK, &set_CHLD, NULL); |
| 1004 | |
| 1005 | for (;;) { |
| 1006 | int seqlen; |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1007 | char seqbuf[sizeof(long)*3 + 2]; |
| 1008 | unsigned seqbufnum; |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1009 | |
| 1010 | if (seq_fd < 0) { |
| 1011 | seq_fd = open("mdev.seq", O_RDWR); |
| 1012 | if (seq_fd < 0) |
| 1013 | break; |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1014 | close_on_exec_on(seq_fd); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1015 | } |
| 1016 | seqlen = pread(seq_fd, seqbuf, sizeof(seqbuf) - 1, 0); |
| 1017 | if (seqlen < 0) { |
| 1018 | close(seq_fd); |
| 1019 | seq_fd = -1; |
| 1020 | break; |
| 1021 | } |
| 1022 | seqbuf[seqlen] = '\0'; |
Denys Vlasenko | 26a8b9f | 2014-09-08 18:19:15 +0200 | [diff] [blame] | 1023 | if (seqbuf[0] == '\n' || seqbuf[0] == '\0') { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1024 | /* seed file: write out seq ASAP */ |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1025 | xwrite_str(seq_fd, utoa(expected_seq)); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1026 | xlseek(seq_fd, 0, SEEK_SET); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1027 | dbg2s("first seq written"); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1028 | break; |
| 1029 | } |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1030 | seqbufnum = atoll(seqbuf); |
| 1031 | if (seqbufnum == expected_seq) { |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1032 | /* correct idx */ |
| 1033 | break; |
| 1034 | } |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1035 | if (seqbufnum > expected_seq) { |
| 1036 | /* a later mdev runs already (this was seen by users to happen) */ |
| 1037 | /* do not overwrite seqfile on exit */ |
| 1038 | close(seq_fd); |
| 1039 | seq_fd = -1; |
| 1040 | break; |
| 1041 | } |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1042 | if (do_once) { |
Denys Vlasenko | ad79551 | 2015-04-19 18:55:12 +0200 | [diff] [blame] | 1043 | dbg2("%s mdev.seq='%s', need '%u'", curtime(), seqbuf, expected_seq); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1044 | do_once = 0; |
| 1045 | } |
| 1046 | if (sigtimedwait(&set_CHLD, NULL, &ts) >= 0) { |
| 1047 | dbg3("woken up"); |
| 1048 | continue; /* don't decrement timeout! */ |
| 1049 | } |
| 1050 | if (--timeout == 0) { |
Denys Vlasenko | bd77e9d | 2015-04-19 18:54:49 +0200 | [diff] [blame] | 1051 | dbg1("%s mdev.seq='%s'", "timed out", seqbuf); |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | sigprocmask(SIG_UNBLOCK, &set_CHLD, NULL); |
| 1056 | return seq_fd; |
| 1057 | } |
| 1058 | |
| 1059 | static void signal_mdevs(unsigned my_pid) |
| 1060 | { |
| 1061 | procps_status_t* p = NULL; |
| 1062 | while ((p = procps_scan(p, PSSCAN_ARGV0)) != NULL) { |
| 1063 | if (p->pid != my_pid |
| 1064 | && p->argv0 |
| 1065 | && strcmp(bb_basename(p->argv0), "mdev") == 0 |
| 1066 | ) { |
| 1067 | kill(p->pid, SIGCHLD); |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1072 | static void process_action(char *temp, unsigned my_pid) |
Jan Klötzke | d04b726 | 2019-05-03 22:35:50 +0200 | [diff] [blame] | 1073 | { |
| 1074 | char *fw; |
| 1075 | char *seq; |
| 1076 | char *action; |
| 1077 | char *env_devname; |
| 1078 | char *env_devpath; |
| 1079 | unsigned seqnum = seqnum; /* for compiler */ |
| 1080 | int seq_fd; |
| 1081 | smalluint op; |
| 1082 | |
| 1083 | /* Hotplug: |
| 1084 | * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev |
| 1085 | * ACTION can be "add", "remove", "change" |
| 1086 | * DEVPATH is like "/block/sda" or "/class/input/mice" |
| 1087 | */ |
| 1088 | env_devname = getenv("DEVNAME"); /* can be NULL */ |
| 1089 | G.subsystem = getenv("SUBSYSTEM"); |
| 1090 | action = getenv("ACTION"); |
| 1091 | env_devpath = getenv("DEVPATH"); |
| 1092 | if (!action || !env_devpath /*|| !G.subsystem*/) |
| 1093 | bb_show_usage(); |
| 1094 | fw = getenv("FIRMWARE"); |
| 1095 | seq = getenv("SEQNUM"); |
| 1096 | op = index_in_strings(keywords, action); |
| 1097 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1098 | if (my_pid) |
| 1099 | open_mdev_log(seq, my_pid); |
Jan Klötzke | d04b726 | 2019-05-03 22:35:50 +0200 | [diff] [blame] | 1100 | |
| 1101 | seq_fd = -1; |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1102 | if (my_pid && seq) { |
Jan Klötzke | d04b726 | 2019-05-03 22:35:50 +0200 | [diff] [blame] | 1103 | seqnum = atoll(seq); |
| 1104 | seq_fd = wait_for_seqfile(seqnum); |
| 1105 | } |
| 1106 | |
| 1107 | dbg1("%s " |
| 1108 | "ACTION:%s SEQNUM:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s" |
| 1109 | "%s%s", |
| 1110 | curtime(), |
| 1111 | action, seq, G.subsystem, env_devname, env_devpath, |
| 1112 | fw ? " FW:" : "", fw ? fw : "" |
| 1113 | ); |
| 1114 | |
| 1115 | snprintf(temp, PATH_MAX, "/sys%s", env_devpath); |
| 1116 | if (op == OP_remove) { |
| 1117 | /* Ignoring "remove firmware". It was reported |
| 1118 | * to happen and to cause erroneous deletion |
| 1119 | * of device nodes. */ |
| 1120 | if (!fw) |
| 1121 | make_device(env_devname, temp, op); |
| 1122 | } |
| 1123 | else { |
| 1124 | make_device(env_devname, temp, op); |
| 1125 | if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { |
| 1126 | if (op == OP_add && fw) |
| 1127 | load_firmware(fw, temp); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if (seq_fd >= 0) { |
| 1132 | xwrite_str(seq_fd, utoa(seqnum + 1)); |
| 1133 | signal_mdevs(my_pid); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | static void initial_scan(char *temp) |
| 1138 | { |
| 1139 | struct stat st; |
| 1140 | |
| 1141 | xstat("/", &st); |
| 1142 | G.root_major = major(st.st_dev); |
| 1143 | G.root_minor = minor(st.st_dev); |
| 1144 | |
| 1145 | putenv((char*)"ACTION=add"); |
| 1146 | |
| 1147 | /* Create all devices from /sys/dev hierarchy */ |
| 1148 | recursive_action("/sys/dev", |
| 1149 | ACTION_RECURSE | ACTION_FOLLOWLINKS, |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 1150 | fileAction, dirAction, temp); |
Jan Klötzke | d04b726 | 2019-05-03 22:35:50 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1153 | #if ENABLE_FEATURE_MDEV_DAEMON |
| 1154 | |
Jan Klötzke | 3cd55d4 | 2019-12-16 22:56:50 +0100 | [diff] [blame] | 1155 | /* |
| 1156 | * The kernel (as of v5.4) will pass up to 32 environment variables with a |
| 1157 | * total of 2kiB on each event. On top of that the action string and device |
| 1158 | * path are added. Using a 3kiB buffer for the event should suffice in any |
| 1159 | * case. |
| 1160 | * |
| 1161 | * As far as the socket receive buffer size is concerned 2MiB proved to be too |
| 1162 | * small (see [1]). Udevd seems to use a whooping 128MiB. The socket receive |
| 1163 | * buffer size is just a resource limit. The buffers are allocated lazily so |
| 1164 | * the memory is not wasted. |
| 1165 | * |
| 1166 | * [1] http://lists.busybox.net/pipermail/busybox/2019-December/087665.html |
| 1167 | */ |
| 1168 | # define USER_RCVBUF (3 * 1024) |
| 1169 | # define KERN_RCVBUF (128 * 1024 * 1024) |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1170 | # define MAX_ENV 32 |
| 1171 | |
Jan Klötzke | e3f4759 | 2020-11-22 14:17:56 +0100 | [diff] [blame] | 1172 | static int daemon_init(char *temp) |
| 1173 | { |
| 1174 | int fd; |
| 1175 | |
| 1176 | /* Subscribe for UEVENT kernel messages */ |
| 1177 | /* Without a sufficiently big RCVBUF, a ton of simultaneous events |
| 1178 | * can trigger ENOBUFS on read, which is unrecoverable. |
| 1179 | * Reproducer: |
| 1180 | * mdev -d |
| 1181 | * find /sys -name uevent -exec sh -c 'echo add >"{}"' ';' |
| 1182 | */ |
| 1183 | fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, 1 << 0, KERN_RCVBUF); |
| 1184 | |
| 1185 | /* |
| 1186 | * Make inital scan after the uevent socket is alive and |
| 1187 | * _before_ we fork away. Already open mdev.log because we work |
| 1188 | * in daemon mode. |
| 1189 | */ |
| 1190 | initial_scan(temp); |
| 1191 | |
| 1192 | return fd; |
| 1193 | } |
| 1194 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1195 | static void daemon_loop(char *temp, int fd) |
| 1196 | { |
| 1197 | for (;;) { |
Jan Klötzke | 3cd55d4 | 2019-12-16 22:56:50 +0100 | [diff] [blame] | 1198 | char netbuf[USER_RCVBUF]; |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1199 | char *env[MAX_ENV]; |
| 1200 | char *s, *end; |
| 1201 | ssize_t len; |
| 1202 | int idx; |
| 1203 | |
| 1204 | len = safe_read(fd, netbuf, sizeof(netbuf) - 1); |
| 1205 | if (len < 0) { |
Jan Klötzke | d560030 | 2019-12-16 22:56:52 +0100 | [diff] [blame] | 1206 | if (errno == ENOBUFS) { |
| 1207 | /* |
| 1208 | * We ran out of socket receive buffer space. |
| 1209 | * Start from scratch. |
| 1210 | */ |
| 1211 | dbg1s("uevent overrun, rescanning"); |
| 1212 | close(fd); |
| 1213 | fd = daemon_init(temp); |
| 1214 | continue; |
| 1215 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1216 | bb_simple_perror_msg_and_die("read"); |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1217 | } |
| 1218 | end = netbuf + len; |
| 1219 | *end = '\0'; |
| 1220 | |
| 1221 | idx = 0; |
| 1222 | s = netbuf; |
| 1223 | while (s < end && idx < MAX_ENV) { |
| 1224 | if (endofname(s)[0] == '=') { |
| 1225 | env[idx++] = s; |
| 1226 | putenv(s); |
| 1227 | } |
| 1228 | s += strlen(s) + 1; |
| 1229 | } |
| 1230 | |
| 1231 | process_action(temp, 0); |
| 1232 | |
| 1233 | while (idx) |
| 1234 | bb_unsetenv(env[--idx]); |
| 1235 | } |
| 1236 | } |
| 1237 | #endif |
| 1238 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1239 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d986048 | 2008-07-12 10:23:16 +0000 | [diff] [blame] | 1240 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 1241 | { |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1242 | enum { |
| 1243 | MDEV_OPT_SCAN = 1 << 0, |
Jan Klötzke | e9003ca | 2019-12-16 22:56:53 +0100 | [diff] [blame] | 1244 | MDEV_OPT_SYSLOG = 1 << 1, |
| 1245 | MDEV_OPT_DAEMON = 1 << 2, |
| 1246 | MDEV_OPT_FOREGROUND = 1 << 3, |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1247 | }; |
Jan Klötzke | 728e53d | 2019-05-03 22:35:51 +0200 | [diff] [blame] | 1248 | int opt; |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 1249 | RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 1250 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 1251 | INIT_G(); |
| 1252 | |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 1253 | /* We can be called as hotplug helper */ |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 1254 | /* Kernel cannot provide suitable stdio fds for us, do it ourself */ |
Denis Vlasenko | d48e81f | 2008-07-06 07:00:11 +0000 | [diff] [blame] | 1255 | bb_sanitize_stdio(); |
Denis Vlasenko | f4e6bd0 | 2008-05-31 18:27:58 +0000 | [diff] [blame] | 1256 | |
Denis Vlasenko | 3798db5 | 2009-04-19 21:37:07 +0000 | [diff] [blame] | 1257 | /* Force the configuration file settings exactly */ |
| 1258 | umask(0); |
| 1259 | |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 1260 | xchdir("/dev"); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 1261 | |
Jan Klötzke | 768153b | 2019-12-16 22:56:54 +0100 | [diff] [blame] | 1262 | opt = getopt32(argv, "^" |
| 1263 | "sS" IF_FEATURE_MDEV_DAEMON("df") "v" |
| 1264 | "\0" |
| 1265 | "vv", |
| 1266 | &G.verbose); |
Jan Klötzke | 728e53d | 2019-05-03 22:35:51 +0200 | [diff] [blame] | 1267 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1268 | #if ENABLE_FEATURE_MDEV_CONF |
| 1269 | G.filename = "/etc/mdev.conf"; |
| 1270 | if (opt & (MDEV_OPT_SCAN|MDEV_OPT_DAEMON)) { |
| 1271 | /* Same as xrealloc_vector(NULL, 4, 0): */ |
| 1272 | G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec)); |
| 1273 | } |
| 1274 | #endif |
| 1275 | |
Jan Klötzke | e9003ca | 2019-12-16 22:56:53 +0100 | [diff] [blame] | 1276 | if (opt & MDEV_OPT_SYSLOG) { |
| 1277 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
| 1278 | logmode |= LOGMODE_SYSLOG; |
| 1279 | } |
| 1280 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1281 | #if ENABLE_FEATURE_MDEV_DAEMON |
| 1282 | if (opt & MDEV_OPT_DAEMON) { |
Denys Vlasenko | 03eb6eb | 2020-11-22 16:35:56 +0100 | [diff] [blame] | 1283 | /* Daemon mode listening on uevent netlink socket. Fork away |
Jan Klötzke | e3f4759 | 2020-11-22 14:17:56 +0100 | [diff] [blame] | 1284 | * after initial scan so that caller can be sure everything |
| 1285 | * is up-to-date when mdev process returns. |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1286 | */ |
Denys Vlasenko | 03eb6eb | 2020-11-22 16:35:56 +0100 | [diff] [blame] | 1287 | int fd = daemon_init(temp); |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1288 | |
Denys Vlasenko | 03eb6eb | 2020-11-22 16:35:56 +0100 | [diff] [blame] | 1289 | if (!(opt & MDEV_OPT_FOREGROUND)) { |
| 1290 | /* there is no point in logging to /dev/null */ |
| 1291 | logmode &= ~LOGMODE_STDIO; |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1292 | bb_daemonize_or_rexec(0, argv); |
Denys Vlasenko | 03eb6eb | 2020-11-22 16:35:56 +0100 | [diff] [blame] | 1293 | } |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1294 | |
Jan Klötzke | 498cec2 | 2019-05-07 20:59:43 +0200 | [diff] [blame] | 1295 | daemon_loop(temp, fd); |
| 1296 | } |
| 1297 | #endif |
Jan Klötzke | 728e53d | 2019-05-03 22:35:51 +0200 | [diff] [blame] | 1298 | if (opt & MDEV_OPT_SCAN) { |
Denys Vlasenko | 40b97fb | 2013-02-26 00:40:46 +0100 | [diff] [blame] | 1299 | /* |
| 1300 | * Scan: mdev -s |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 1301 | */ |
Jan Klötzke | d04b726 | 2019-05-03 22:35:50 +0200 | [diff] [blame] | 1302 | initial_scan(temp); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 1303 | } else { |
Jan Klötzke | d04b726 | 2019-05-03 22:35:50 +0200 | [diff] [blame] | 1304 | process_action(temp, getpid()); |
Denis Vlasenko | 018bee6 | 2008-07-15 22:33:13 +0000 | [diff] [blame] | 1305 | |
Denys Vlasenko | 5bce135 | 2013-02-27 10:51:41 +0100 | [diff] [blame] | 1306 | dbg1("%s exiting", curtime()); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Mike Frysinger | c881c73 | 2007-11-19 09:04:22 +0000 | [diff] [blame] | 1309 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1310 | RELEASE_CONFIG_BUFFER(temp); |
| 1311 | |
Denis Vlasenko | aafbae6 | 2009-04-13 13:33:02 +0000 | [diff] [blame] | 1312 | return EXIT_SUCCESS; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 1313 | } |