blob: ebf83d1a3a7bc0de00d1896898b5bbf7fa4bc6bc [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landley70f7ef72005-12-13 08:21:33 +00003 * mdev - Mini udev for busybox
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Rob Landley70f7ef72005-12-13 08:21:33 +00005 * Copyright 2005 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Rob Landley70f7ef72005-12-13 08:21:33 +00009 */
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020010//config:config MDEV
Denys Vlasenkob097a842018-12-28 03:20:17 +010011//config: bool "mdev (17 kb)"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020012//config: default y
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020013//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: mdev is a mini-udev implementation for dynamically creating device
15//config: nodes in the /dev directory.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020016//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020018//config:
19//config:config FEATURE_MDEV_CONF
20//config: bool "Support /etc/mdev.conf"
21//config: default y
22//config: depends on MDEV
23//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020024//config: Add support for the mdev config file to control ownership and
25//config: permissions of the device nodes.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020026//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020028//config:
29//config:config FEATURE_MDEV_RENAME
30//config: bool "Support subdirs/symlinks"
31//config: default y
32//config: depends on FEATURE_MDEV_CONF
33//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020034//config: Add support for renaming devices and creating symlinks.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020035//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020037//config:
38//config:config FEATURE_MDEV_RENAME_REGEXP
39//config: bool "Support regular expressions substitutions when renaming device"
40//config: default y
41//config: depends on FEATURE_MDEV_RENAME
42//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020043//config: Add support for regular expressions substitutions when renaming
44//config: device.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020045//config:
46//config:config FEATURE_MDEV_EXEC
47//config: bool "Support command execution at device addition/removal"
48//config: default y
49//config: depends on FEATURE_MDEV_CONF
50//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020051//config: This adds support for an optional field to /etc/mdev.conf for
52//config: executing commands when devices are created/removed.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020053//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020054//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020055//config:
56//config:config FEATURE_MDEV_LOAD_FIRMWARE
Denys Vlasenko68b653b2017-07-27 10:53:09 +020057//config: bool "Support loading of firmware"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020058//config: default y
59//config: depends on MDEV
60//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020061//config: Some devices need to load firmware before they can be usable.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020062//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020063//config: These devices will request userspace look up the files in
64//config: /lib/firmware/ and if it exists, send it to the kernel for
65//config: loading into the hardware.
Jan Klötzke498cec22019-05-07 20:59:43 +020066//config:
67//config:config FEATURE_MDEV_DAEMON
68//config: bool "Support daemon mode"
69//config: default y
70//config: depends on MDEV
71//config: help
72//config: Adds the -d option to run mdev in daemon mode handling hotplug
73//config: events from the kernel like udev. If the system generates many
74//config: hotplug events this mode of operation will consume less
75//config: resources than registering mdev as hotplug helper or using the
76//config: uevent applet.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020077
78//applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP))
79
80//kbuild:lib-$(CONFIG_MDEV) += mdev.o
81
Pere Orga5bc8c002011-04-11 03:29:49 +020082//usage:#define mdev_trivial_usage
Denys Vlasenko1c9cd9b2020-11-22 14:43:35 +010083//usage: "[-vS] " IF_FEATURE_MDEV_DAEMON("{ ") "[-s]" IF_FEATURE_MDEV_DAEMON(" | [-df] }")
Pere Orga5bc8c002011-04-11 03:29:49 +020084//usage:#define mdev_full_usage "\n\n"
Denys Vlasenko1c9cd9b2020-11-22 14:43:35 +010085//usage: " -v verbose\n"
86//usage: " -S log to syslog too\n"
87//usage: " -s scan /sys and populate /dev\n"
Jan Klötzke498cec22019-05-07 20:59:43 +020088//usage: IF_FEATURE_MDEV_DAEMON(
Denys Vlasenko1c9cd9b2020-11-22 14:43:35 +010089//usage: " -d daemon, listen on netlink\n"
90//usage: " -f stay in foreground\n"
Jan Klötzke498cec22019-05-07 20:59:43 +020091//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020092//usage: "\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020093//usage: "Bare mdev is a kernel hotplug helper. To activate it:\n"
94//usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020095//usage: IF_FEATURE_MDEV_CONF(
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020096//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020097//usage: "It uses /etc/mdev.conf with lines\n"
Denys Vlasenko40b97fb2013-02-26 00:40:46 +010098//usage: " [-][ENV=regex;]...DEVNAME UID:GID PERM"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020099//usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]")
Pere Orga5bc8c002011-04-11 03:29:49 +0200100//usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]")
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +0200101//usage: "\n"
102//usage: "where DEVNAME is device name regex, @major,minor[-minor2], or\n"
103//usage: "environment variable regex. A common use of the latter is\n"
104//usage: "to load modules for hotplugged devices:\n"
105//usage: " $MODALIAS=.* 0:0 660 @modprobe \"$MODALIAS\"\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200106//usage: )
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +0200107//usage: "\n"
108//usage: "If /dev/mdev.seq file exists, mdev will wait for its value\n"
109//usage: "to match $SEQNUM variable. This prevents plug/unplug races.\n"
Denys Vlasenko8d118542012-05-18 04:47:18 +0200110//usage: "To activate this feature, create empty /dev/mdev.seq at boot.\n"
Denys Vlasenko516530c2012-05-18 04:45:35 +0200111//usage: "\n"
Denys Vlasenko8d118542012-05-18 04:47:18 +0200112//usage: "If /dev/mdev.log file exists, debug log will be appended to it."
Pere Orga5bc8c002011-04-11 03:29:49 +0200113
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000114#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200115#include "common_bufsiz.h"
Rob Landleyb56c2852005-12-17 10:52:30 +0000116#include "xregex.h"
Jan Klötzke498cec22019-05-07 20:59:43 +0200117#include <linux/netlink.h>
Jan Klötzkee9003ca2019-12-16 22:56:53 +0100118#include <syslog.h>
Rob Landley70f7ef72005-12-13 08:21:33 +0000119
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000120/* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
121 * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
122 * contains "4:0\n". Directory name is taken as device name, path component
123 * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
124 * Then mdev creates the /dev/device_name node.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000125 * If /sys/class/.../dev file does not exist, mdev still may act
126 * on this device: see "@|$|*command args..." parameter in config file.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000127 *
128 * mdev w/o parameters is called as hotplug helper. It takes device
129 * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
130 * maj,min from "/sys/$DEVPATH/dev" and also examines
131 * $ACTION ("add"/"delete") and $FIRMWARE.
132 *
133 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
134 * (todo: explain "delete" and $FIRMWARE)
135 *
136 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000137 *
138 * Leading minus in 1st field means "don't stop on this line", otherwise
139 * search is stopped after the matching line is encountered.
140 *
Denys Vlasenko45f0a112011-09-22 04:55:47 +0200141 * $envvar=regex format is useful for loading modules for hot-plugged devices
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000142 * which do not have driver loaded yet. In this case /sys/class/.../dev
143 * does not exist, but $MODALIAS is set to needed module's name
144 * (actually, an alias to it) by kernel. This rule instructs mdev
145 * to load the module and exit:
146 * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
147 * The kernel will generate another hotplug event when /sys/class/.../dev
148 * file appears.
149 *
150 * When line matches, the device node is created, chmod'ed and chown'ed,
151 * moved to path, and if >path, a symlink to moved node is created,
152 * all this if /sys/class/.../dev exists.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000153 * Examples:
154 * =loop/ - moves to /dev/loop
155 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000156 *
157 * Then "command args..." is executed (via sh -c 'command args...').
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000158 * @:execute on creation, $:on deletion, *:on both.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000159 * This happens regardless of /sys/class/.../dev existence.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000160 */
161
Denys Vlasenko516530c2012-05-18 04:45:35 +0200162/* Kernel's hotplug environment constantly changes.
163 * Here are new cases I observed on 3.1.0:
164 *
165 * Case with $DEVNAME and $DEVICE, not just $DEVPATH:
166 * ACTION=add
167 * BUSNUM=001
168 * DEVICE=/proc/bus/usb/001/003
169 * DEVNAME=bus/usb/001/003
170 * DEVNUM=003
171 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5
172 * DEVTYPE=usb_device
173 * MAJOR=189
174 * MINOR=2
175 * PRODUCT=18d1/4e12/227
176 * SUBSYSTEM=usb
177 * TYPE=0/0/0
178 *
179 * Case with $DEVICE, but no $DEVNAME - apparenty, usb iface notification?
180 * "Please load me a module" thing?
181 * ACTION=add
182 * DEVICE=/proc/bus/usb/001/003
183 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0
184 * DEVTYPE=usb_interface
185 * INTERFACE=8/6/80
186 * MODALIAS=usb:v18D1p4E12d0227dc00dsc00dp00ic08isc06ip50
187 * PRODUCT=18d1/4e12/227
188 * SUBSYSTEM=usb
189 * TYPE=0/0/0
190 *
191 * ACTION=add
192 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5
193 * DEVTYPE=scsi_host
194 * SUBSYSTEM=scsi
195 *
196 * ACTION=add
197 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/scsi_host/host5
198 * SUBSYSTEM=scsi_host
199 *
200 * ACTION=add
201 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0
202 * DEVTYPE=scsi_target
203 * SUBSYSTEM=scsi
204 *
205 * Case with strange $MODALIAS:
206 * ACTION=add
207 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0
208 * DEVTYPE=scsi_device
209 * MODALIAS=scsi:t-0x00
210 * SUBSYSTEM=scsi
211 *
212 * ACTION=add
213 * 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
214 * SUBSYSTEM=scsi_disk
215 *
216 * ACTION=add
217 * 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
218 * SUBSYSTEM=scsi_device
219 *
220 * Case with explicit $MAJOR/$MINOR (no need to read /sys/$DEVPATH/dev?):
221 * ACTION=add
222 * DEVNAME=bsg/5:0:0:0
223 * 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
224 * MAJOR=253
225 * MINOR=1
226 * SUBSYSTEM=bsg
227 *
228 * ACTION=add
229 * DEVPATH=/devices/virtual/bdi/8:16
230 * SUBSYSTEM=bdi
231 *
232 * ACTION=add
233 * DEVNAME=sdb
234 * DEVPATH=/block/sdb
235 * DEVTYPE=disk
236 * MAJOR=8
237 * MINOR=16
238 * SUBSYSTEM=block
239 *
240 * Case with ACTION=change:
241 * ACTION=change
242 * DEVNAME=sdb
243 * DEVPATH=/block/sdb
244 * DEVTYPE=disk
245 * DISK_MEDIA_CHANGE=1
246 * MAJOR=8
247 * MINOR=16
248 * SUBSYSTEM=block
249 */
250
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100251#define DEBUG_LVL 2
252
253#if DEBUG_LVL >= 1
254# define dbg1(...) do { if (G.verbose) bb_error_msg(__VA_ARGS__); } while(0)
James Byrne69374872019-07-02 11:35:03 +0200255# define dbg1s(msg) do { if (G.verbose) bb_simple_error_msg(msg); } while(0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100256#else
257# define dbg1(...) ((void)0)
James Byrne69374872019-07-02 11:35:03 +0200258# define dbg1s(msg) ((void)0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100259#endif
260#if DEBUG_LVL >= 2
261# define dbg2(...) do { if (G.verbose >= 2) bb_error_msg(__VA_ARGS__); } while(0)
James Byrne69374872019-07-02 11:35:03 +0200262# define dbg2s(msg) do { if (G.verbose >= 2) bb_simple_error_msg(msg); } while(0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100263#else
264# define dbg2(...) ((void)0)
James Byrne69374872019-07-02 11:35:03 +0200265# define dbg2s(msg) ((void)0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100266#endif
267#if DEBUG_LVL >= 3
268# define dbg3(...) do { if (G.verbose >= 3) bb_error_msg(__VA_ARGS__); } while(0)
James Byrne69374872019-07-02 11:35:03 +0200269# define dbg3s(msg) do { if (G.verbose >= 3) bb_simple_error_msg(msg); } while(0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100270#else
271# define dbg3(...) ((void)0)
James Byrne69374872019-07-02 11:35:03 +0200272# define dbg3s(msg) ((void)0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100273#endif
274
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100275static const char keywords[] ALIGN1 = "add\0remove\0"; // "change\0"
Denys Vlasenko516530c2012-05-18 04:45:35 +0200276enum { OP_add, OP_remove };
277
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100278struct envmatch {
279 struct envmatch *next;
280 char *envname;
281 regex_t match;
282};
283
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200284struct rule {
285 bool keep_matching;
286 bool regex_compiled;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200287 mode_t mode;
288 int maj, min0, min1;
289 struct bb_uidgid_t ugid;
290 char *envvar;
291 char *ren_mov;
292 IF_FEATURE_MDEV_EXEC(char *r_cmd;)
293 regex_t match;
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100294 struct envmatch *envmatch;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200295};
296
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000297struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000298 int root_major, root_minor;
Jan Klötzke768153b2019-12-16 22:56:54 +0100299 int verbose;
Denis Vlasenko065c7142009-04-13 22:23:02 +0000300 char *subsystem;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100301 char *subsys_env; /* for putenv("SUBSYSTEM=subsystem") */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200302#if ENABLE_FEATURE_MDEV_CONF
303 const char *filename;
304 parser_t *parser;
305 struct rule **rule_vec;
306 unsigned rule_idx;
307#endif
308 struct rule cur_rule;
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +0200309 char timestr[sizeof("HH:MM:SS.123456")];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100310} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200311#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200312#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200313 setup_common_bufsiz(); \
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200314 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \
315 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \
316} while (0)
Denys Vlasenko16714242011-09-21 01:59:15 +0200317
Rob Landleya7e3d052006-02-21 06:11:13 +0000318
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000319/* Prevent infinite loops in /sys symlinks */
320#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +0000321
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200322/* We use additional bytes in make_device() */
323#define SCRATCH_SIZE 128
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000324
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200325#if ENABLE_FEATURE_MDEV_CONF
326
327static void make_default_cur_rule(void)
328{
329 memset(&G.cur_rule, 0, sizeof(G.cur_rule));
330 G.cur_rule.maj = -1; /* "not a @major,minor rule" */
331 G.cur_rule.mode = 0660;
332}
333
334static void clean_up_cur_rule(void)
335{
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100336 struct envmatch *e;
337
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200338 free(G.cur_rule.envvar);
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100339 free(G.cur_rule.ren_mov);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200340 if (G.cur_rule.regex_compiled)
341 regfree(&G.cur_rule.match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200342 IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);)
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100343 e = G.cur_rule.envmatch;
344 while (e) {
345 free(e->envname);
346 regfree(&e->match);
347 e = e->next;
348 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200349 make_default_cur_rule();
350}
351
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100352static char *parse_envmatch_pfx(char *val)
353{
354 struct envmatch **nextp = &G.cur_rule.envmatch;
355
356 for (;;) {
357 struct envmatch *e;
358 char *semicolon;
359 char *eq = strchr(val, '=');
360 if (!eq /* || eq == val? */)
361 return val;
362 if (endofname(val) != eq)
363 return val;
364 semicolon = strchr(eq, ';');
365 if (!semicolon)
366 return val;
367 /* ENVVAR=regex;... */
368 *nextp = e = xzalloc(sizeof(*e));
369 nextp = &e->next;
370 e->envname = xstrndup(val, eq - val);
371 *semicolon = '\0';
372 xregcomp(&e->match, eq + 1, REG_EXTENDED);
373 *semicolon = ';';
374 val = semicolon + 1;
375 }
376}
377
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200378static void parse_next_rule(void)
379{
380 /* Note: on entry, G.cur_rule is set to default */
381 while (1) {
382 char *tokens[4];
383 char *val;
384
Denys Vlasenko499597d2012-01-11 00:37:17 +0100385 /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */
386 if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL & ~PARSE_EOL_COMMENTS))
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200387 break;
388
389 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100390 dbg3("token1:'%s'", tokens[1]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200391
392 /* 1st field */
393 val = tokens[0];
394 G.cur_rule.keep_matching = ('-' == val[0]);
395 val += G.cur_rule.keep_matching; /* swallow leading dash */
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100396 val = parse_envmatch_pfx(val);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200397 if (val[0] == '@') {
398 /* @major,minor[-minor2] */
399 /* (useful when name is ambiguous:
400 * "/sys/class/usb/lp0" and
401 * "/sys/class/printer/lp0")
402 */
403 int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1);
404 if (sc < 2 || G.cur_rule.maj < 0) {
405 bb_error_msg("bad @maj,min on line %d", G.parser->lineno);
406 goto next_rule;
407 }
408 if (sc == 2)
409 G.cur_rule.min1 = G.cur_rule.min0;
410 } else {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100411 char *eq = strchr(val, '=');
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200412 if (val[0] == '$') {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100413 /* $ENVVAR=regex ... */
414 val++;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200415 if (!eq) {
416 bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno);
417 goto next_rule;
418 }
419 G.cur_rule.envvar = xstrndup(val, eq - val);
420 val = eq + 1;
421 }
422 xregcomp(&G.cur_rule.match, val, REG_EXTENDED);
423 G.cur_rule.regex_compiled = 1;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200424 }
425
426 /* 2nd field: uid:gid - device ownership */
Denys Vlasenko526d8582015-10-19 04:27:17 +0200427 if (get_uidgid(&G.cur_rule.ugid, tokens[1]) == 0) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200428 bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno);
429 goto next_rule;
430 }
431
432 /* 3rd field: mode - device permissions */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +0200433 G.cur_rule.mode = bb_parse_mode(tokens[2], G.cur_rule.mode);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200434
435 /* 4th field (opt): ">|=alias" or "!" to not create the node */
436 val = tokens[3];
437 if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) {
438 char *s = skip_non_whitespace(val);
439 G.cur_rule.ren_mov = xstrndup(val, s - val);
440 val = skip_whitespace(s);
441 }
442
443 if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) {
444 const char *s = "$@*";
445 const char *s2 = strchr(s, val[0]);
446 if (!s2) {
447 bb_error_msg("bad line %u", G.parser->lineno);
448 goto next_rule;
449 }
450 IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);)
451 }
452
453 return;
454 next_rule:
455 clean_up_cur_rule();
456 } /* while (config_read) */
457
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100458 dbg3("config_close(G.parser)");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200459 config_close(G.parser);
460 G.parser = NULL;
461
462 return;
463}
464
465/* If mdev -s, we remember rules in G.rule_vec[].
466 * Otherwise, there is no point in doing it, and we just
467 * save only one parsed rule in G.cur_rule.
468 */
469static const struct rule *next_rule(void)
470{
471 struct rule *rule;
472
473 /* Open conf file if we didn't do it yet */
474 if (!G.parser && G.filename) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100475 dbg3("config_open('%s')", G.filename);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200476 G.parser = config_open2(G.filename, fopen_for_read);
477 G.filename = NULL;
478 }
479
480 if (G.rule_vec) {
481 /* mdev -s */
482 /* Do we have rule parsed already? */
483 if (G.rule_vec[G.rule_idx]) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100484 dbg3("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200485 return G.rule_vec[G.rule_idx++];
486 }
487 make_default_cur_rule();
488 } else {
489 /* not mdev -s */
490 clean_up_cur_rule();
491 }
492
493 /* Parse one more rule if file isn't fully read */
494 rule = &G.cur_rule;
495 if (G.parser) {
496 parse_next_rule();
497 if (G.rule_vec) { /* mdev -s */
Ron Yorstond840c5d2015-07-19 23:05:20 +0200498 rule = xmemdup(&G.cur_rule, sizeof(G.cur_rule));
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200499 G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
500 G.rule_vec[G.rule_idx++] = rule;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100501 dbg3("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200502 }
503 }
504
505 return rule;
506}
507
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100508static int env_matches(struct envmatch *e)
509{
510 while (e) {
511 int r;
512 char *val = getenv(e->envname);
513 if (!val)
514 return 0;
515 r = regexec(&e->match, val, /*size*/ 0, /*range[]*/ NULL, /*eflags*/ 0);
516 if (r != 0) /* no match */
517 return 0;
518 e = e->next;
519 }
520 return 1;
521}
522
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200523#else
524
525# define next_rule() (&G.cur_rule)
526
527#endif
Denys Vlasenko16714242011-09-21 01:59:15 +0200528
Denys Vlasenko4609f472013-01-21 01:22:12 +0100529static void mkdir_recursive(char *name)
530{
531 /* if name has many levels ("dir1/dir2"),
532 * bb_make_directory() will create dir1 according to umask,
533 * not according to its "mode" parameter.
534 * Since we run with umask=0, need to temporarily switch it.
535 */
536 umask(022); /* "dir1" (if any) will be 0755 too */
537 bb_make_directory(name, 0755, FILEUTILS_RECUR);
538 umask(0);
539}
540
Denis Vlasenkod9860482008-07-12 10:23:16 +0000541/* Builds an alias path.
542 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000543 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +0000544 */
545static char *build_alias(char *alias, const char *device_name)
546{
547 char *dest;
548
549 /* ">bar/": rename to bar/device_name */
550 /* ">bar[/]baz": rename to bar[/]baz */
551 dest = strrchr(alias, '/');
552 if (dest) { /* ">bar/[baz]" ? */
553 *dest = '\0'; /* mkdir bar */
Denys Vlasenko4609f472013-01-21 01:22:12 +0100554 mkdir_recursive(alias);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000555 *dest = '/';
556 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
557 dest = alias;
558 alias = concat_path_file(alias, device_name);
559 free(dest);
560 }
561 }
562
563 return alias;
564}
565
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200566/* mknod in /dev based on a path like "/sys/block/hda/hda1"
567 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
Denys Vlasenko20a32622016-09-07 14:09:01 +0200568 * after NUL, but we promise to not mangle it (IOW: to restore NUL if needed).
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200569 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
Denys Vlasenko516530c2012-05-18 04:45:35 +0200570 *
571 * device_name = $DEVNAME (may be NULL)
572 * path = /sys/$DEVPATH
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200573 */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200574static void make_device(char *device_name, char *path, int operation)
Rob Landley70f7ef72005-12-13 08:21:33 +0000575{
Denis Vlasenkof46be092006-10-16 19:39:37 +0000576 int major, minor, type, len;
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200577 char *path_end = path + strlen(path);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200578
Rob Landley15fe2e12006-05-08 02:53:23 +0000579 /* Try to read major/minor string. Note that the kernel puts \n after
580 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000581 * because sscanf() will stop at the first nondigit, which \n is.
582 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000583 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000584 major = -1;
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200585 if (operation == OP_add) {
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200586 strcpy(path_end, "/dev");
587 len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1);
588 *path_end = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000589 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000590 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000591 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000592 /* no "dev" file, but we can still run scripts
593 * based on device name */
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200594 } else if (sscanf(path_end + 1, "%u:%u", &major, &minor) == 2) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100595 dbg1("dev %u,%u", major, minor);
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200596 } else {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000597 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000598 }
Rob Landley10b36f92006-08-10 01:09:37 +0000599 }
Denys Vlasenkof2860bf2010-08-16 14:26:15 +0200600 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000601
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200602 /* Determine device name */
603 if (!device_name) {
604 /*
605 * There was no $DEVNAME envvar (for example, mdev -s never has).
606 * But it is very useful: it contains the *path*, not only basename,
607 * Thankfully, uevent file has it.
608 * Example of .../sound/card0/controlC0/uevent file on Linux-3.7.7:
609 * MAJOR=116
610 * MINOR=7
611 * DEVNAME=snd/controlC0
612 */
613 strcpy(path_end, "/uevent");
614 len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1);
615 if (len < 0)
616 len = 0;
617 *path_end = '\0';
618 path_end[1 + len] = '\0';
619 device_name = strstr(path_end + 1, "\nDEVNAME=");
620 if (device_name) {
621 device_name += sizeof("\nDEVNAME=")-1;
622 strchrnul(device_name, '\n')[0] = '\0';
623 } else {
624 /* Fall back to just basename */
625 device_name = (char*) bb_basename(path);
626 }
627 }
628 /* Determine device type */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100629 /*
630 * http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000631 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100632 * But since 2.6.25 block devices are also in /sys/class/block.
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200633 * We use strstr("/block/") to forestall future surprises.
634 */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000635 type = S_IFCHR;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100636 if (strstr(path, "/block/") || (G.subsystem && is_prefixed_with(G.subsystem, "block")))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000637 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000638
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200639#if ENABLE_FEATURE_MDEV_CONF
640 G.rule_idx = 0; /* restart from the beginning (think mdev -s) */
641#endif
642 for (;;) {
643 const char *str_to_match;
644 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
645 char *command;
646 char *alias;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000647 char aliaslink = aliaslink; /* for compiler */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200648 char *node_name;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200649 const struct rule *rule;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000650
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200651 str_to_match = device_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000652
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200653 rule = next_rule();
Denis Vlasenko3798db52009-04-19 21:37:07 +0000654
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200655#if ENABLE_FEATURE_MDEV_CONF
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100656 if (!env_matches(rule->envmatch))
657 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200658 if (rule->maj >= 0) { /* @maj,min rule */
659 if (major != rule->maj)
660 continue;
661 if (minor < rule->min0 || minor > rule->min1)
662 continue;
663 memset(off, 0, sizeof(off));
664 goto rule_matches;
665 }
666 if (rule->envvar) { /* $envvar=regex rule */
667 str_to_match = getenv(rule->envvar);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100668 dbg3("getenv('%s'):'%s'", rule->envvar, str_to_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200669 if (!str_to_match)
670 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200671 }
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200672 /* else: str_to_match = device_name */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000673
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200674 if (rule->regex_compiled) {
675 int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100676 dbg3("regex_match for '%s':%d", str_to_match, regex_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200677 //bb_error_msg("matches:");
678 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
679 // if (off[i].rm_so < 0) continue;
680 // bb_error_msg("match %d: '%.*s'\n", i,
681 // (int)(off[i].rm_eo - off[i].rm_so),
682 // device_name + off[i].rm_so);
683 //}
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000684
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200685 if (regex_match != 0
686 /* regexec returns whole pattern as "range" 0 */
687 || off[0].rm_so != 0
688 || (int)off[0].rm_eo != (int)strlen(str_to_match)
689 ) {
690 continue; /* this rule doesn't match */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000691 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200692 }
693 /* else: it's final implicit "match-all" rule */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200694 rule_matches:
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100695 dbg2("rule matched, line %d", G.parser ? G.parser->lineno : -1);
Denys Vlasenko4424dfd2013-03-17 23:01:17 +0100696#endif
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200697 /* Build alias name */
698 alias = NULL;
699 if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) {
700 aliaslink = rule->ren_mov[0];
701 if (aliaslink == '!') {
702 /* "!": suppress node creation/deletion */
703 major = -2;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000704 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200705 else if (aliaslink == '>' || aliaslink == '=') {
706 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
707 char *s;
708 char *p;
709 unsigned n;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000710
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200711 /* substitute %1..9 with off[1..9], if any */
712 n = 0;
713 s = rule->ren_mov;
714 while (*s)
715 if (*s++ == '%')
716 n++;
Mike Frysingerc881c732007-11-19 09:04:22 +0000717
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200718 p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match));
719 s = rule->ren_mov + 1;
720 while (*s) {
721 *p = *s;
722 if ('%' == *s) {
723 unsigned i = (s[1] - '0');
724 if (i <= 9 && off[i].rm_so >= 0) {
725 n = off[i].rm_eo - off[i].rm_so;
726 strncpy(p, str_to_match + off[i].rm_so, n);
727 p += n - 1;
728 s++;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000729 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000730 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200731 p++;
732 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000733 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200734 } else {
735 alias = xstrdup(rule->ren_mov + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200736 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000737 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000738 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100739 dbg3("alias:'%s'", alias);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000740
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200741 command = NULL;
742 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
743 if (command) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200744 /* Are we running this command now?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100745 * Run @cmd on create, $cmd on delete, *cmd on any
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200746 */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100747 if ((command[0] == '@' && operation == OP_add)
748 || (command[0] == '$' && operation == OP_remove)
749 || (command[0] == '*')
750 ) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200751 command++;
752 } else {
753 command = NULL;
754 }
755 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100756 dbg3("command:'%s'", command);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000757
758 /* "Execute" the line we found */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200759 node_name = device_name;
760 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
761 node_name = alias = build_alias(alias, device_name);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100762 dbg3("alias2:'%s'", alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000763 }
764
Denys Vlasenko516530c2012-05-18 04:45:35 +0200765 if (operation == OP_add && major >= 0) {
766 char *slash = strrchr(node_name, '/');
767 if (slash) {
768 *slash = '\0';
Denys Vlasenko4609f472013-01-21 01:22:12 +0100769 mkdir_recursive(node_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200770 *slash = '/';
771 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100772 if (ENABLE_FEATURE_MDEV_CONF) {
773 dbg1("mknod %s (%d,%d) %o"
774 " %u:%u",
775 node_name, major, minor, rule->mode | type,
776 rule->ugid.uid, rule->ugid.gid
777 );
778 } else {
779 dbg1("mknod %s (%d,%d) %o",
780 node_name, major, minor, rule->mode | type
781 );
782 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200783 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
784 bb_perror_msg("can't create '%s'", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200785 if (ENABLE_FEATURE_MDEV_CONF) {
786 chmod(node_name, rule->mode);
787 chown(node_name, rule->ugid.uid, rule->ugid.gid);
788 }
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200789 if (major == G.root_major && minor == G.root_minor)
790 symlink(node_name, "root");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200791 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100792 if (aliaslink == '>') {
793//TODO: on devtmpfs, device_name already exists and symlink() fails.
794//End result is that instead of symlink, we have two nodes.
795//What should be done?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100796 dbg1("symlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200797 symlink(node_name, device_name);
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100798 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200799 }
800 }
801
802 if (ENABLE_FEATURE_MDEV_EXEC && command) {
803 /* setenv will leak memory, use putenv/unsetenv/free */
804 char *s = xasprintf("%s=%s", "MDEV", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200805 putenv(s);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100806 dbg1("running: %s", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200807 if (system(command) == -1)
808 bb_perror_msg("can't run '%s'", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200809 bb_unsetenv_and_free(s);
810 }
811
Denys Vlasenko516530c2012-05-18 04:45:35 +0200812 if (operation == OP_remove && major >= -1) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200813 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko516530c2012-05-18 04:45:35 +0200814 if (aliaslink == '>') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100815 dbg1("unlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200816 unlink(device_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200817 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200818 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100819 dbg1("unlink: %s", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200820 unlink(node_name);
821 }
822
823 if (ENABLE_FEATURE_MDEV_RENAME)
824 free(alias);
825
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000826 /* We found matching line.
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200827 * Stop unless it was prefixed with '-'
828 */
829 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000830 break;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200831 } /* for (;;) */
Rob Landley70f7ef72005-12-13 08:21:33 +0000832}
833
Denys Vlasenko51283b82018-12-08 15:41:17 +0100834static ssize_t readlink2(char *buf, size_t bufsize)
835{
836 // Grr... gcc 8.1.1:
837 // "passing argument 2 to restrict-qualified parameter aliases with argument 1"
838 // dance around that...
839 char *obuf FIX_ALIASING;
840 obuf = buf;
841 return readlink(buf, obuf, bufsize);
842}
843
Denys Vlasenko20a32622016-09-07 14:09:01 +0200844/* File callback for /sys/ traversal.
845 * We act only on "/sys/.../dev" (pseudo)file
846 */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200847static int FAST_FUNC fileAction(struct recursive_state *state,
848 const char *fileName,
849 struct stat *statbuf UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000850{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000851 size_t len = strlen(fileName) - 4; /* can't underflow */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200852 char *path = state->userData; /* char array[PATH_MAX + SCRATCH_SIZE] */
Denys Vlasenko20a32622016-09-07 14:09:01 +0200853 char subsys[PATH_MAX];
854 int res;
Rob Landley70f7ef72005-12-13 08:21:33 +0000855
Denys Vlasenko20a32622016-09-07 14:09:01 +0200856 /* Is it a ".../dev" file? (len check is for paranoid reasons) */
857 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX - 32)
858 return FALSE; /* not .../dev */
Rob Landley70f7ef72005-12-13 08:21:33 +0000859
Denys Vlasenko20a32622016-09-07 14:09:01 +0200860 strcpy(path, fileName);
861 path[len] = '\0';
862
863 /* Read ".../subsystem" symlink in the same directory where ".../dev" is */
864 strcpy(subsys, path);
865 strcpy(subsys + len, "/subsystem");
Denys Vlasenko51283b82018-12-08 15:41:17 +0100866 res = readlink2(subsys, sizeof(subsys)-1);
Denys Vlasenko20a32622016-09-07 14:09:01 +0200867 if (res > 0) {
868 subsys[res] = '\0';
869 free(G.subsystem);
870 if (G.subsys_env) {
871 bb_unsetenv_and_free(G.subsys_env);
872 G.subsys_env = NULL;
873 }
874 /* Set G.subsystem and $SUBSYSTEM from symlink's last component */
875 G.subsystem = strrchr(subsys, '/');
876 if (G.subsystem) {
877 G.subsystem = xstrdup(G.subsystem + 1);
878 G.subsys_env = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
879 putenv(G.subsys_env);
880 }
881 }
882
883 make_device(/*DEVNAME:*/ NULL, path, OP_add);
Rob Landley70f7ef72005-12-13 08:21:33 +0000884
Mike Frysingerb51fd352007-06-13 09:24:50 +0000885 return TRUE;
886}
Rob Landley70f7ef72005-12-13 08:21:33 +0000887
Mike Frysingerb51fd352007-06-13 09:24:50 +0000888/* Directory callback for /sys/ traversal */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200889static int FAST_FUNC dirAction(struct recursive_state *state,
890 const char *fileName UNUSED_PARAM,
891 struct stat *statbuf UNUSED_PARAM)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000892{
Denys Vlasenko689d0652020-10-01 21:52:16 +0200893 return (state->depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000894}
895
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000896/* For the full gory details, see linux/Documentation/firmware_class/README
897 *
898 * Firmware loading works like this:
899 * - kernel sets FIRMWARE env var
900 * - userspace checks /lib/firmware/$FIRMWARE
901 * - userspace waits for /sys/$DEVPATH/loading to appear
902 * - userspace writes "1" to /sys/$DEVPATH/loading
903 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
904 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
905 * - kernel loads firmware into device
906 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000907static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000908{
909 int cnt;
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200910 int firmware_fd, loading_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000911
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000912 /* check for /lib/firmware/$FIRMWARE */
Denys Vlasenko20a32622016-09-07 14:09:01 +0200913 firmware_fd = -1;
914 if (chdir("/lib/firmware") == 0)
915 firmware_fd = open(firmware, O_RDONLY); /* can fail */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000916
917 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
918 xchdir(sysfs_path);
919 for (cnt = 0; cnt < 30; ++cnt) {
920 loading_fd = open("loading", O_WRONLY);
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200921 if (loading_fd >= 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000922 goto loading;
Denys Vlasenkoec16c032020-11-29 11:37:34 +0100923 sleep1();
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000924 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000925 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000926
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000927 loading:
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200928 cnt = 0;
929 if (firmware_fd >= 0) {
930 int data_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000931
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200932 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
933 if (full_write(loading_fd, "1", 1) != 1)
934 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000935
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200936 /* load firmware into /sys/$DEVPATH/data */
937 data_fd = open("data", O_WRONLY);
938 if (data_fd < 0)
939 goto out;
940 cnt = bb_copyfd_eof(firmware_fd, data_fd);
941 if (ENABLE_FEATURE_CLEAN_UP)
942 close(data_fd);
943 }
944
945 /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading"
946 * Note: we emit -1 also if firmware file wasn't found.
947 * There are cases when otherwise kernel would wait for minutes
948 * before timing out.
949 */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000950 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000951 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000952 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000953 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000954
955 out:
Denys Vlasenkoba76b7a2013-02-04 23:35:27 +0100956 xchdir("/dev");
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000957 if (ENABLE_FEATURE_CLEAN_UP) {
958 close(firmware_fd);
959 close(loading_fd);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000960 }
961}
962
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100963static char *curtime(void)
964{
965 struct timeval tv;
966 gettimeofday(&tv, NULL);
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +0200967 sprintf(
968 strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec),
969 ".%06u",
970 (unsigned)tv.tv_usec
971 );
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100972 return G.timestr;
973}
974
975static void open_mdev_log(const char *seq, unsigned my_pid)
976{
977 int logfd = open("mdev.log", O_WRONLY | O_APPEND);
978 if (logfd >= 0) {
979 xmove_fd(logfd, STDERR_FILENO);
980 G.verbose = 2;
981 applet_name = xasprintf("%s[%s]", applet_name, seq ? seq : utoa(my_pid));
982 }
983}
984
985/* If it exists, does /dev/mdev.seq match $SEQNUM?
986 * If it does not match, earlier mdev is running
987 * in parallel, and we need to wait.
988 * Active mdev pokes us with SIGCHLD to check the new file.
989 */
990static int
Denys Vlasenkoad795512015-04-19 18:55:12 +0200991wait_for_seqfile(unsigned expected_seq)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100992{
993 /* We time out after 2 sec */
994 static const struct timespec ts = { 0, 32*1000*1000 };
995 int timeout = 2000 / 32;
996 int seq_fd = -1;
997 int do_once = 1;
998 sigset_t set_CHLD;
999
1000 sigemptyset(&set_CHLD);
1001 sigaddset(&set_CHLD, SIGCHLD);
1002 sigprocmask(SIG_BLOCK, &set_CHLD, NULL);
1003
1004 for (;;) {
1005 int seqlen;
Denys Vlasenkoad795512015-04-19 18:55:12 +02001006 char seqbuf[sizeof(long)*3 + 2];
1007 unsigned seqbufnum;
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001008
1009 if (seq_fd < 0) {
1010 seq_fd = open("mdev.seq", O_RDWR);
1011 if (seq_fd < 0)
1012 break;
Denys Vlasenkoad795512015-04-19 18:55:12 +02001013 close_on_exec_on(seq_fd);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001014 }
1015 seqlen = pread(seq_fd, seqbuf, sizeof(seqbuf) - 1, 0);
1016 if (seqlen < 0) {
1017 close(seq_fd);
1018 seq_fd = -1;
1019 break;
1020 }
1021 seqbuf[seqlen] = '\0';
Denys Vlasenko26a8b9f2014-09-08 18:19:15 +02001022 if (seqbuf[0] == '\n' || seqbuf[0] == '\0') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001023 /* seed file: write out seq ASAP */
Denys Vlasenkoad795512015-04-19 18:55:12 +02001024 xwrite_str(seq_fd, utoa(expected_seq));
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001025 xlseek(seq_fd, 0, SEEK_SET);
James Byrne69374872019-07-02 11:35:03 +02001026 dbg2s("first seq written");
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001027 break;
1028 }
Denys Vlasenkoad795512015-04-19 18:55:12 +02001029 seqbufnum = atoll(seqbuf);
1030 if (seqbufnum == expected_seq) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001031 /* correct idx */
1032 break;
1033 }
Denys Vlasenkoad795512015-04-19 18:55:12 +02001034 if (seqbufnum > expected_seq) {
1035 /* a later mdev runs already (this was seen by users to happen) */
1036 /* do not overwrite seqfile on exit */
1037 close(seq_fd);
1038 seq_fd = -1;
1039 break;
1040 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001041 if (do_once) {
Denys Vlasenkoad795512015-04-19 18:55:12 +02001042 dbg2("%s mdev.seq='%s', need '%u'", curtime(), seqbuf, expected_seq);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001043 do_once = 0;
1044 }
1045 if (sigtimedwait(&set_CHLD, NULL, &ts) >= 0) {
1046 dbg3("woken up");
1047 continue; /* don't decrement timeout! */
1048 }
1049 if (--timeout == 0) {
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +02001050 dbg1("%s mdev.seq='%s'", "timed out", seqbuf);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001051 break;
1052 }
1053 }
1054 sigprocmask(SIG_UNBLOCK, &set_CHLD, NULL);
1055 return seq_fd;
1056}
1057
1058static void signal_mdevs(unsigned my_pid)
1059{
1060 procps_status_t* p = NULL;
1061 while ((p = procps_scan(p, PSSCAN_ARGV0)) != NULL) {
1062 if (p->pid != my_pid
1063 && p->argv0
1064 && strcmp(bb_basename(p->argv0), "mdev") == 0
1065 ) {
1066 kill(p->pid, SIGCHLD);
1067 }
1068 }
1069}
1070
Jan Klötzke498cec22019-05-07 20:59:43 +02001071static void process_action(char *temp, unsigned my_pid)
Jan Klötzked04b7262019-05-03 22:35:50 +02001072{
1073 char *fw;
1074 char *seq;
1075 char *action;
1076 char *env_devname;
1077 char *env_devpath;
1078 unsigned seqnum = seqnum; /* for compiler */
1079 int seq_fd;
1080 smalluint op;
1081
1082 /* Hotplug:
1083 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
1084 * ACTION can be "add", "remove", "change"
1085 * DEVPATH is like "/block/sda" or "/class/input/mice"
1086 */
1087 env_devname = getenv("DEVNAME"); /* can be NULL */
1088 G.subsystem = getenv("SUBSYSTEM");
1089 action = getenv("ACTION");
1090 env_devpath = getenv("DEVPATH");
1091 if (!action || !env_devpath /*|| !G.subsystem*/)
1092 bb_show_usage();
1093 fw = getenv("FIRMWARE");
1094 seq = getenv("SEQNUM");
1095 op = index_in_strings(keywords, action);
1096
Jan Klötzke498cec22019-05-07 20:59:43 +02001097 if (my_pid)
1098 open_mdev_log(seq, my_pid);
Jan Klötzked04b7262019-05-03 22:35:50 +02001099
1100 seq_fd = -1;
Jan Klötzke498cec22019-05-07 20:59:43 +02001101 if (my_pid && seq) {
Jan Klötzked04b7262019-05-03 22:35:50 +02001102 seqnum = atoll(seq);
1103 seq_fd = wait_for_seqfile(seqnum);
1104 }
1105
1106 dbg1("%s "
1107 "ACTION:%s SEQNUM:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
1108 "%s%s",
1109 curtime(),
1110 action, seq, G.subsystem, env_devname, env_devpath,
1111 fw ? " FW:" : "", fw ? fw : ""
1112 );
1113
1114 snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
1115 if (op == OP_remove) {
1116 /* Ignoring "remove firmware". It was reported
1117 * to happen and to cause erroneous deletion
1118 * of device nodes. */
1119 if (!fw)
1120 make_device(env_devname, temp, op);
1121 }
1122 else {
1123 make_device(env_devname, temp, op);
1124 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
1125 if (op == OP_add && fw)
1126 load_firmware(fw, temp);
1127 }
1128 }
1129
1130 if (seq_fd >= 0) {
1131 xwrite_str(seq_fd, utoa(seqnum + 1));
1132 signal_mdevs(my_pid);
1133 }
1134}
1135
1136static void initial_scan(char *temp)
1137{
1138 struct stat st;
1139
1140 xstat("/", &st);
1141 G.root_major = major(st.st_dev);
1142 G.root_minor = minor(st.st_dev);
1143
1144 putenv((char*)"ACTION=add");
1145
1146 /* Create all devices from /sys/dev hierarchy */
1147 recursive_action("/sys/dev",
1148 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Denys Vlasenko689d0652020-10-01 21:52:16 +02001149 fileAction, dirAction, temp);
Jan Klötzked04b7262019-05-03 22:35:50 +02001150}
1151
Jan Klötzke498cec22019-05-07 20:59:43 +02001152#if ENABLE_FEATURE_MDEV_DAEMON
1153
Jan Klötzke3cd55d42019-12-16 22:56:50 +01001154/*
1155 * The kernel (as of v5.4) will pass up to 32 environment variables with a
1156 * total of 2kiB on each event. On top of that the action string and device
1157 * path are added. Using a 3kiB buffer for the event should suffice in any
1158 * case.
1159 *
1160 * As far as the socket receive buffer size is concerned 2MiB proved to be too
1161 * small (see [1]). Udevd seems to use a whooping 128MiB. The socket receive
1162 * buffer size is just a resource limit. The buffers are allocated lazily so
1163 * the memory is not wasted.
1164 *
1165 * [1] http://lists.busybox.net/pipermail/busybox/2019-December/087665.html
1166 */
1167# define USER_RCVBUF (3 * 1024)
1168# define KERN_RCVBUF (128 * 1024 * 1024)
Jan Klötzke498cec22019-05-07 20:59:43 +02001169# define MAX_ENV 32
1170
Jan Klötzkee3f47592020-11-22 14:17:56 +01001171static int daemon_init(char *temp)
1172{
1173 int fd;
1174
1175 /* Subscribe for UEVENT kernel messages */
1176 /* Without a sufficiently big RCVBUF, a ton of simultaneous events
1177 * can trigger ENOBUFS on read, which is unrecoverable.
1178 * Reproducer:
1179 * mdev -d
1180 * find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
1181 */
1182 fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, 1 << 0, KERN_RCVBUF);
1183
1184 /*
1185 * Make inital scan after the uevent socket is alive and
1186 * _before_ we fork away. Already open mdev.log because we work
1187 * in daemon mode.
1188 */
1189 initial_scan(temp);
1190
1191 return fd;
1192}
1193
Jan Klötzke498cec22019-05-07 20:59:43 +02001194static void daemon_loop(char *temp, int fd)
1195{
1196 for (;;) {
Jan Klötzke3cd55d42019-12-16 22:56:50 +01001197 char netbuf[USER_RCVBUF];
Jan Klötzke498cec22019-05-07 20:59:43 +02001198 char *env[MAX_ENV];
1199 char *s, *end;
1200 ssize_t len;
1201 int idx;
1202
1203 len = safe_read(fd, netbuf, sizeof(netbuf) - 1);
1204 if (len < 0) {
Jan Klötzked5600302019-12-16 22:56:52 +01001205 if (errno == ENOBUFS) {
1206 /*
1207 * We ran out of socket receive buffer space.
1208 * Start from scratch.
1209 */
1210 dbg1s("uevent overrun, rescanning");
1211 close(fd);
1212 fd = daemon_init(temp);
1213 continue;
1214 }
James Byrne69374872019-07-02 11:35:03 +02001215 bb_simple_perror_msg_and_die("read");
Jan Klötzke498cec22019-05-07 20:59:43 +02001216 }
1217 end = netbuf + len;
1218 *end = '\0';
1219
1220 idx = 0;
1221 s = netbuf;
1222 while (s < end && idx < MAX_ENV) {
1223 if (endofname(s)[0] == '=') {
1224 env[idx++] = s;
1225 putenv(s);
1226 }
1227 s += strlen(s) + 1;
1228 }
1229
1230 process_action(temp, 0);
1231
1232 while (idx)
1233 bb_unsetenv(env[--idx]);
1234 }
1235}
1236#endif
1237
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001238int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +00001239int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +00001240{
Jan Klötzke498cec22019-05-07 20:59:43 +02001241 enum {
1242 MDEV_OPT_SCAN = 1 << 0,
Jan Klötzkee9003ca2019-12-16 22:56:53 +01001243 MDEV_OPT_SYSLOG = 1 << 1,
1244 MDEV_OPT_DAEMON = 1 << 2,
1245 MDEV_OPT_FOREGROUND = 1 << 3,
Jan Klötzke498cec22019-05-07 20:59:43 +02001246 };
Jan Klötzke728e53d2019-05-03 22:35:51 +02001247 int opt;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001248 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +00001249
Denys Vlasenko16714242011-09-21 01:59:15 +02001250 INIT_G();
1251
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001252 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001253 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001254 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001255
Denis Vlasenko3798db52009-04-19 21:37:07 +00001256 /* Force the configuration file settings exactly */
1257 umask(0);
1258
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +00001259 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +00001260
Jan Klötzke768153b2019-12-16 22:56:54 +01001261 opt = getopt32(argv, "^"
1262 "sS" IF_FEATURE_MDEV_DAEMON("df") "v"
1263 "\0"
1264 "vv",
1265 &G.verbose);
Jan Klötzke728e53d2019-05-03 22:35:51 +02001266
Jan Klötzke498cec22019-05-07 20:59:43 +02001267#if ENABLE_FEATURE_MDEV_CONF
1268 G.filename = "/etc/mdev.conf";
1269 if (opt & (MDEV_OPT_SCAN|MDEV_OPT_DAEMON)) {
1270 /* Same as xrealloc_vector(NULL, 4, 0): */
1271 G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
1272 }
1273#endif
1274
Jan Klötzkee9003ca2019-12-16 22:56:53 +01001275 if (opt & MDEV_OPT_SYSLOG) {
1276 openlog(applet_name, LOG_PID, LOG_DAEMON);
1277 logmode |= LOGMODE_SYSLOG;
1278 }
1279
Jan Klötzke498cec22019-05-07 20:59:43 +02001280#if ENABLE_FEATURE_MDEV_DAEMON
1281 if (opt & MDEV_OPT_DAEMON) {
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001282 /* Daemon mode listening on uevent netlink socket. Fork away
Jan Klötzkee3f47592020-11-22 14:17:56 +01001283 * after initial scan so that caller can be sure everything
1284 * is up-to-date when mdev process returns.
Jan Klötzke498cec22019-05-07 20:59:43 +02001285 */
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001286 int fd = daemon_init(temp);
Jan Klötzke498cec22019-05-07 20:59:43 +02001287
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001288 if (!(opt & MDEV_OPT_FOREGROUND)) {
1289 /* there is no point in logging to /dev/null */
1290 logmode &= ~LOGMODE_STDIO;
Jan Klötzke498cec22019-05-07 20:59:43 +02001291 bb_daemonize_or_rexec(0, argv);
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001292 }
Jan Klötzke498cec22019-05-07 20:59:43 +02001293
Jan Klötzke498cec22019-05-07 20:59:43 +02001294 daemon_loop(temp, fd);
1295 }
1296#endif
Jan Klötzke728e53d2019-05-03 22:35:51 +02001297 if (opt & MDEV_OPT_SCAN) {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +01001298 /*
1299 * Scan: mdev -s
Mike Frysingerc881c732007-11-19 09:04:22 +00001300 */
Jan Klötzked04b7262019-05-03 22:35:50 +02001301 initial_scan(temp);
Rob Landley29e08ff2006-01-12 06:13:50 +00001302 } else {
Jan Klötzked04b7262019-05-03 22:35:50 +02001303 process_action(temp, getpid());
Denis Vlasenko018bee62008-07-15 22:33:13 +00001304
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001305 dbg1("%s exiting", curtime());
Rob Landley29e08ff2006-01-12 06:13:50 +00001306 }
1307
Mike Frysingerc881c732007-11-19 09:04:22 +00001308 if (ENABLE_FEATURE_CLEAN_UP)
1309 RELEASE_CONFIG_BUFFER(temp);
1310
Denis Vlasenkoaafbae62009-04-13 13:33:02 +00001311 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +00001312}