blob: f6de7ad2aca3261c2bafdc06a1623beda1ca6fc4 [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
Samuel Thibault77216c32022-10-16 02:04:59 +020013//config: select PLATFORM_LINUX
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020014//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: mdev is a mini-udev implementation for dynamically creating device
16//config: nodes in the /dev directory.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020017//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020018//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020019//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 Vlasenko72089cf2017-07-21 09:50:55 +020025//config: Add support for the mdev config file to control ownership and
26//config: permissions of the device nodes.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020027//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020029//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 Vlasenko72089cf2017-07-21 09:50:55 +020035//config: Add support for renaming devices and creating symlinks.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020036//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020037//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020038//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 Vlasenko72089cf2017-07-21 09:50:55 +020044//config: Add support for regular expressions substitutions when renaming
45//config: device.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020046//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 Vlasenko72089cf2017-07-21 09:50:55 +020052//config: This adds support for an optional field to /etc/mdev.conf for
53//config: executing commands when devices are created/removed.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020054//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020055//config: For more information, please see docs/mdev.txt
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020056//config:
57//config:config FEATURE_MDEV_LOAD_FIRMWARE
Denys Vlasenko68b653b2017-07-27 10:53:09 +020058//config: bool "Support loading of firmware"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020059//config: default y
60//config: depends on MDEV
61//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020062//config: Some devices need to load firmware before they can be usable.
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020063//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020064//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ötzke498cec22019-05-07 20:59:43 +020067//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 Vlasenko5c6ba6c2011-09-19 12:18:11 +020078
79//applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP))
80
81//kbuild:lib-$(CONFIG_MDEV) += mdev.o
82
Pere Orga5bc8c002011-04-11 03:29:49 +020083//usage:#define mdev_trivial_usage
Denys Vlasenko1c9cd9b2020-11-22 14:43:35 +010084//usage: "[-vS] " IF_FEATURE_MDEV_DAEMON("{ ") "[-s]" IF_FEATURE_MDEV_DAEMON(" | [-df] }")
Pere Orga5bc8c002011-04-11 03:29:49 +020085//usage:#define mdev_full_usage "\n\n"
Denys Vlasenkod2e0d3f2021-10-08 14:30:50 +020086//usage: " -v Verbose\n"
87//usage: " -S Log to syslog too\n"
88//usage: " -s Scan /sys and populate /dev\n"
Jan Klötzke498cec22019-05-07 20:59:43 +020089//usage: IF_FEATURE_MDEV_DAEMON(
Denys Vlasenkod2e0d3f2021-10-08 14:30:50 +020090//usage: " -d Daemon, listen on netlink\n"
91//usage: " -f Run in foreground\n"
Jan Klötzke498cec22019-05-07 20:59:43 +020092//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020093//usage: "\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020094//usage: "Bare mdev is a kernel hotplug helper. To activate it:\n"
95//usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020096//usage: IF_FEATURE_MDEV_CONF(
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020097//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020098//usage: "It uses /etc/mdev.conf with lines\n"
Denys Vlasenko40b97fb2013-02-26 00:40:46 +010099//usage: " [-][ENV=regex;]...DEVNAME UID:GID PERM"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +0200100//usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]")
Pere Orga5bc8c002011-04-11 03:29:49 +0200101//usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]")
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +0200102//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 Orga5bc8c002011-04-11 03:29:49 +0200107//usage: )
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +0200108//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 Vlasenko8d118542012-05-18 04:47:18 +0200111//usage: "To activate this feature, create empty /dev/mdev.seq at boot.\n"
Denys Vlasenko516530c2012-05-18 04:45:35 +0200112//usage: "\n"
Denys Vlasenko8d118542012-05-18 04:47:18 +0200113//usage: "If /dev/mdev.log file exists, debug log will be appended to it."
Pere Orga5bc8c002011-04-11 03:29:49 +0200114
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000115#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200116#include "common_bufsiz.h"
Rob Landleyb56c2852005-12-17 10:52:30 +0000117#include "xregex.h"
Jan Klötzke498cec22019-05-07 20:59:43 +0200118#include <linux/netlink.h>
Jan Klötzkee9003ca2019-12-16 22:56:53 +0100119#include <syslog.h>
Rob Landley70f7ef72005-12-13 08:21:33 +0000120
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000121/* "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 Vlasenko11c17f72009-04-19 23:38:08 +0000126 * If /sys/class/.../dev file does not exist, mdev still may act
127 * on this device: see "@|$|*command args..." parameter in config file.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000128 *
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 Vlasenko5ff96292009-04-17 00:01:04 +0000138 *
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 Vlasenko45f0a112011-09-22 04:55:47 +0200142 * $envvar=regex format is useful for loading modules for hot-plugged devices
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000143 * 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 Vlasenko5ff96292009-04-17 00:01:04 +0000154 * Examples:
155 * =loop/ - moves to /dev/loop
156 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000157 *
158 * Then "command args..." is executed (via sh -c 'command args...').
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000159 * @:execute on creation, $:on deletion, *:on both.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000160 * This happens regardless of /sys/class/.../dev existence.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000161 */
162
Denys Vlasenko516530c2012-05-18 04:45:35 +0200163/* 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 Vlasenko5bce1352013-02-27 10:51:41 +0100252#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 Byrne69374872019-07-02 11:35:03 +0200256# define dbg1s(msg) do { if (G.verbose) bb_simple_error_msg(msg); } while(0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100257#else
258# define dbg1(...) ((void)0)
James Byrne69374872019-07-02 11:35:03 +0200259# define dbg1s(msg) ((void)0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100260#endif
261#if DEBUG_LVL >= 2
262# define dbg2(...) do { if (G.verbose >= 2) bb_error_msg(__VA_ARGS__); } while(0)
James Byrne69374872019-07-02 11:35:03 +0200263# define dbg2s(msg) do { if (G.verbose >= 2) bb_simple_error_msg(msg); } while(0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100264#else
265# define dbg2(...) ((void)0)
James Byrne69374872019-07-02 11:35:03 +0200266# define dbg2s(msg) ((void)0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100267#endif
268#if DEBUG_LVL >= 3
269# define dbg3(...) do { if (G.verbose >= 3) bb_error_msg(__VA_ARGS__); } while(0)
James Byrne69374872019-07-02 11:35:03 +0200270# define dbg3s(msg) do { if (G.verbose >= 3) bb_simple_error_msg(msg); } while(0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100271#else
272# define dbg3(...) ((void)0)
James Byrne69374872019-07-02 11:35:03 +0200273# define dbg3s(msg) ((void)0)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100274#endif
275
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100276static const char keywords[] ALIGN1 = "add\0remove\0"; // "change\0"
Denys Vlasenko516530c2012-05-18 04:45:35 +0200277enum { OP_add, OP_remove };
278
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100279struct envmatch {
280 struct envmatch *next;
281 char *envname;
282 regex_t match;
283};
284
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200285struct rule {
286 bool keep_matching;
287 bool regex_compiled;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200288 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 Vlasenko40b97fb2013-02-26 00:40:46 +0100295 struct envmatch *envmatch;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200296};
297
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000298struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000299 int root_major, root_minor;
Jan Klötzke768153b2019-12-16 22:56:54 +0100300 int verbose;
Denis Vlasenko065c7142009-04-13 22:23:02 +0000301 char *subsystem;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100302 char *subsys_env; /* for putenv("SUBSYSTEM=subsystem") */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200303#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 Vlasenkobd77e9d2015-04-19 18:54:49 +0200310 char timestr[sizeof("HH:MM:SS.123456")];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100311} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200312#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200313#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200314 setup_common_bufsiz(); \
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200315 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 Vlasenko16714242011-09-21 01:59:15 +0200318
Rob Landleya7e3d052006-02-21 06:11:13 +0000319
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000320/* Prevent infinite loops in /sys symlinks */
321#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +0000322
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200323/* We use additional bytes in make_device() */
324#define SCRATCH_SIZE 128
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000325
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200326#if ENABLE_FEATURE_MDEV_CONF
327
328static 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
335static void clean_up_cur_rule(void)
336{
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100337 struct envmatch *e;
338
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200339 free(G.cur_rule.envvar);
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100340 free(G.cur_rule.ren_mov);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200341 if (G.cur_rule.regex_compiled)
342 regfree(&G.cur_rule.match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200343 IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);)
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100344 e = G.cur_rule.envmatch;
345 while (e) {
346 free(e->envname);
347 regfree(&e->match);
348 e = e->next;
349 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200350 make_default_cur_rule();
351}
352
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100353static 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 Vlasenko3eccd7e2011-09-22 03:46:30 +0200379static 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 Vlasenko499597d2012-01-11 00:37:17 +0100386 /* 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 Vlasenko3eccd7e2011-09-22 03:46:30 +0200388 break;
389
390 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100391 dbg3("token1:'%s'", tokens[1]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200392
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 Vlasenko40b97fb2013-02-26 00:40:46 +0100397 val = parse_envmatch_pfx(val);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200398 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 Vlasenko40b97fb2013-02-26 00:40:46 +0100412 char *eq = strchr(val, '=');
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200413 if (val[0] == '$') {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100414 /* $ENVVAR=regex ... */
415 val++;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200416 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 Vlasenko3eccd7e2011-09-22 03:46:30 +0200425 }
426
427 /* 2nd field: uid:gid - device ownership */
Denys Vlasenko526d8582015-10-19 04:27:17 +0200428 if (get_uidgid(&G.cur_rule.ugid, tokens[1]) == 0) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200429 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 Vlasenko5711a2a2015-10-07 17:55:33 +0200434 G.cur_rule.mode = bb_parse_mode(tokens[2], G.cur_rule.mode);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200435
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 Vlasenko5bce1352013-02-27 10:51:41 +0100459 dbg3("config_close(G.parser)");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200460 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 */
470static 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 Vlasenko5bce1352013-02-27 10:51:41 +0100476 dbg3("config_open('%s')", G.filename);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200477 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 Vlasenko5bce1352013-02-27 10:51:41 +0100485 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 +0200486 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 Yorstond840c5d2015-07-19 23:05:20 +0200499 rule = xmemdup(&G.cur_rule, sizeof(G.cur_rule));
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200500 G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
501 G.rule_vec[G.rule_idx++] = rule;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100502 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 +0200503 }
504 }
505
506 return rule;
507}
508
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100509static 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 Vlasenko3eccd7e2011-09-22 03:46:30 +0200524#else
525
526# define next_rule() (&G.cur_rule)
527
528#endif
Denys Vlasenko16714242011-09-21 01:59:15 +0200529
Denys Vlasenko4609f472013-01-21 01:22:12 +0100530static 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 Vlasenkod9860482008-07-12 10:23:16 +0000542/* Builds an alias path.
543 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000544 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +0000545 */
546static 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 Vlasenko4609f472013-01-21 01:22:12 +0100555 mkdir_recursive(alias);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000556 *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 Vlasenko76f5e382009-05-04 21:59:05 +0200567/* mknod in /dev based on a path like "/sys/block/hda/hda1"
568 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
Denys Vlasenko20a32622016-09-07 14:09:01 +0200569 * after NUL, but we promise to not mangle it (IOW: to restore NUL if needed).
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200570 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
Denys Vlasenko516530c2012-05-18 04:45:35 +0200571 *
572 * device_name = $DEVNAME (may be NULL)
573 * path = /sys/$DEVPATH
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200574 */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200575static void make_device(char *device_name, char *path, int operation)
Rob Landley70f7ef72005-12-13 08:21:33 +0000576{
Denis Vlasenkof46be092006-10-16 19:39:37 +0000577 int major, minor, type, len;
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200578 char *path_end = path + strlen(path);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200579
Rob Landley15fe2e12006-05-08 02:53:23 +0000580 /* 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 Vlasenko8d89bed2008-09-07 23:22:08 +0000582 * because sscanf() will stop at the first nondigit, which \n is.
583 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000584 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000585 major = -1;
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200586 if (operation == OP_add) {
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200587 strcpy(path_end, "/dev");
588 len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1);
589 *path_end = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000590 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000591 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000592 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000593 /* no "dev" file, but we can still run scripts
594 * based on device name */
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200595 } else if (sscanf(path_end + 1, "%u:%u", &major, &minor) == 2) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100596 dbg1("dev %u,%u", major, minor);
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200597 } else {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000598 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000599 }
Rob Landley10b36f92006-08-10 01:09:37 +0000600 }
Denys Vlasenkof2860bf2010-08-16 14:26:15 +0200601 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000602
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200603 /* 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 Vlasenko5bce1352013-02-27 10:51:41 +0100630 /*
631 * http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000632 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100633 * But since 2.6.25 block devices are also in /sys/class/block.
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200634 * We use strstr("/block/") to forestall future surprises.
635 */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000636 type = S_IFCHR;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100637 if (strstr(path, "/block/") || (G.subsystem && is_prefixed_with(G.subsystem, "block")))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000638 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000639
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200640#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 Vlasenkof2b39e02009-04-13 23:18:52 +0000648 char aliaslink = aliaslink; /* for compiler */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200649 char *node_name;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200650 const struct rule *rule;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000651
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200652 str_to_match = device_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000653
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200654 rule = next_rule();
Denis Vlasenko3798db52009-04-19 21:37:07 +0000655
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200656#if ENABLE_FEATURE_MDEV_CONF
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100657 if (!env_matches(rule->envmatch))
658 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200659 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 Vlasenko5bce1352013-02-27 10:51:41 +0100669 dbg3("getenv('%s'):'%s'", rule->envvar, str_to_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200670 if (!str_to_match)
671 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200672 }
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200673 /* else: str_to_match = device_name */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000674
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200675 if (rule->regex_compiled) {
676 int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100677 dbg3("regex_match for '%s':%d", str_to_match, regex_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200678 //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 Vlasenkocf26ab72008-03-27 22:45:44 +0000685
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200686 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 Vlasenko3798db52009-04-19 21:37:07 +0000692 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200693 }
694 /* else: it's final implicit "match-all" rule */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200695 rule_matches:
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100696 dbg2("rule matched, line %d", G.parser ? G.parser->lineno : -1);
Denys Vlasenko4424dfd2013-03-17 23:01:17 +0100697#endif
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200698 /* 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 Vlasenko3798db52009-04-19 21:37:07 +0000705 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200706 else if (aliaslink == '>' || aliaslink == '=') {
707 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
708 char *s;
709 char *p;
710 unsigned n;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000711
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200712 /* 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 Frysingerc881c732007-11-19 09:04:22 +0000718
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200719 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 Vlasenkoc7cc5a92009-04-19 01:27:20 +0000730 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000731 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200732 p++;
733 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000734 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200735 } else {
736 alias = xstrdup(rule->ren_mov + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200737 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000738 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000739 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100740 dbg3("alias:'%s'", alias);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000741
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200742 command = NULL;
743 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
744 if (command) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200745 /* Are we running this command now?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100746 * Run @cmd on create, $cmd on delete, *cmd on any
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200747 */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100748 if ((command[0] == '@' && operation == OP_add)
749 || (command[0] == '$' && operation == OP_remove)
750 || (command[0] == '*')
751 ) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200752 command++;
753 } else {
754 command = NULL;
755 }
756 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100757 dbg3("command:'%s'", command);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000758
759 /* "Execute" the line we found */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200760 node_name = device_name;
761 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
762 node_name = alias = build_alias(alias, device_name);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100763 dbg3("alias2:'%s'", alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000764 }
765
Denys Vlasenko516530c2012-05-18 04:45:35 +0200766 if (operation == OP_add && major >= 0) {
767 char *slash = strrchr(node_name, '/');
768 if (slash) {
769 *slash = '\0';
Denys Vlasenko4609f472013-01-21 01:22:12 +0100770 mkdir_recursive(node_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200771 *slash = '/';
772 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100773 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 Vlasenko3eccd7e2011-09-22 03:46:30 +0200784 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
785 bb_perror_msg("can't create '%s'", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200786 if (ENABLE_FEATURE_MDEV_CONF) {
787 chmod(node_name, rule->mode);
788 chown(node_name, rule->ugid.uid, rule->ugid.gid);
789 }
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200790 if (major == G.root_major && minor == G.root_minor)
791 symlink(node_name, "root");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200792 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100793 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 Vlasenko5bce1352013-02-27 10:51:41 +0100797 dbg1("symlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200798 symlink(node_name, device_name);
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100799 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200800 }
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 Vlasenko3eccd7e2011-09-22 03:46:30 +0200806 putenv(s);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100807 dbg1("running: %s", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200808 if (system(command) == -1)
809 bb_perror_msg("can't run '%s'", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200810 bb_unsetenv_and_free(s);
811 }
812
Denys Vlasenko516530c2012-05-18 04:45:35 +0200813 if (operation == OP_remove && major >= -1) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200814 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko516530c2012-05-18 04:45:35 +0200815 if (aliaslink == '>') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100816 dbg1("unlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200817 unlink(device_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200818 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200819 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100820 dbg1("unlink: %s", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200821 unlink(node_name);
822 }
823
824 if (ENABLE_FEATURE_MDEV_RENAME)
825 free(alias);
826
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000827 /* We found matching line.
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200828 * Stop unless it was prefixed with '-'
829 */
830 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000831 break;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200832 } /* for (;;) */
Rob Landley70f7ef72005-12-13 08:21:33 +0000833}
834
Denys Vlasenko51283b82018-12-08 15:41:17 +0100835static 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 Vlasenko20a32622016-09-07 14:09:01 +0200845/* File callback for /sys/ traversal.
846 * We act only on "/sys/.../dev" (pseudo)file
847 */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200848static int FAST_FUNC fileAction(struct recursive_state *state,
849 const char *fileName,
850 struct stat *statbuf UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000851{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000852 size_t len = strlen(fileName) - 4; /* can't underflow */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200853 char *path = state->userData; /* char array[PATH_MAX + SCRATCH_SIZE] */
Denys Vlasenko20a32622016-09-07 14:09:01 +0200854 char subsys[PATH_MAX];
855 int res;
Rob Landley70f7ef72005-12-13 08:21:33 +0000856
Denys Vlasenko20a32622016-09-07 14:09:01 +0200857 /* 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 Landley70f7ef72005-12-13 08:21:33 +0000860
Denys Vlasenko20a32622016-09-07 14:09:01 +0200861 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 Vlasenko51283b82018-12-08 15:41:17 +0100867 res = readlink2(subsys, sizeof(subsys)-1);
Denys Vlasenko20a32622016-09-07 14:09:01 +0200868 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 Landley70f7ef72005-12-13 08:21:33 +0000885
Mike Frysingerb51fd352007-06-13 09:24:50 +0000886 return TRUE;
887}
Rob Landley70f7ef72005-12-13 08:21:33 +0000888
Mike Frysingerb51fd352007-06-13 09:24:50 +0000889/* Directory callback for /sys/ traversal */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200890static int FAST_FUNC dirAction(struct recursive_state *state,
891 const char *fileName UNUSED_PARAM,
892 struct stat *statbuf UNUSED_PARAM)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000893{
Denys Vlasenko689d0652020-10-01 21:52:16 +0200894 return (state->depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000895}
896
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000897/* 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 Vlasenkoc7cc5a92009-04-19 01:27:20 +0000908static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000909{
910 int cnt;
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200911 int firmware_fd, loading_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000912
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000913 /* check for /lib/firmware/$FIRMWARE */
Denys Vlasenko20a32622016-09-07 14:09:01 +0200914 firmware_fd = -1;
915 if (chdir("/lib/firmware") == 0)
916 firmware_fd = open(firmware, O_RDONLY); /* can fail */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000917
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 Lohmann25cb5aa2012-05-19 23:39:45 +0200922 if (loading_fd >= 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000923 goto loading;
Denys Vlasenkoec16c032020-11-29 11:37:34 +0100924 sleep1();
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000925 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000926 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000927
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000928 loading:
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200929 cnt = 0;
930 if (firmware_fd >= 0) {
931 int data_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000932
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200933 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
934 if (full_write(loading_fd, "1", 1) != 1)
935 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000936
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200937 /* 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 Frysingera78ef2c2007-06-13 07:34:15 +0000951 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000952 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000953 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000954 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000955
956 out:
Denys Vlasenkoba76b7a2013-02-04 23:35:27 +0100957 xchdir("/dev");
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000958 if (ENABLE_FEATURE_CLEAN_UP) {
959 close(firmware_fd);
960 close(loading_fd);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000961 }
962}
963
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100964static char *curtime(void)
965{
966 struct timeval tv;
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100967 xgettimeofday(&tv);
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +0200968 sprintf(
969 strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec),
970 ".%06u",
971 (unsigned)tv.tv_usec
972 );
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100973 return G.timestr;
974}
975
976static 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 */
991static int
Denys Vlasenkoad795512015-04-19 18:55:12 +0200992wait_for_seqfile(unsigned expected_seq)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100993{
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 Vlasenkoad795512015-04-19 18:55:12 +02001007 char seqbuf[sizeof(long)*3 + 2];
1008 unsigned seqbufnum;
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001009
1010 if (seq_fd < 0) {
1011 seq_fd = open("mdev.seq", O_RDWR);
1012 if (seq_fd < 0)
1013 break;
Denys Vlasenkoad795512015-04-19 18:55:12 +02001014 close_on_exec_on(seq_fd);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001015 }
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 Vlasenko26a8b9f2014-09-08 18:19:15 +02001023 if (seqbuf[0] == '\n' || seqbuf[0] == '\0') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001024 /* seed file: write out seq ASAP */
Denys Vlasenkoad795512015-04-19 18:55:12 +02001025 xwrite_str(seq_fd, utoa(expected_seq));
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001026 xlseek(seq_fd, 0, SEEK_SET);
James Byrne69374872019-07-02 11:35:03 +02001027 dbg2s("first seq written");
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001028 break;
1029 }
Denys Vlasenkoad795512015-04-19 18:55:12 +02001030 seqbufnum = atoll(seqbuf);
1031 if (seqbufnum == expected_seq) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001032 /* correct idx */
1033 break;
1034 }
Denys Vlasenkoad795512015-04-19 18:55:12 +02001035 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 Vlasenko5bce1352013-02-27 10:51:41 +01001042 if (do_once) {
Denys Vlasenkoad795512015-04-19 18:55:12 +02001043 dbg2("%s mdev.seq='%s', need '%u'", curtime(), seqbuf, expected_seq);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001044 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 Vlasenkobd77e9d2015-04-19 18:54:49 +02001051 dbg1("%s mdev.seq='%s'", "timed out", seqbuf);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001052 break;
1053 }
1054 }
1055 sigprocmask(SIG_UNBLOCK, &set_CHLD, NULL);
1056 return seq_fd;
1057}
1058
1059static 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ötzke498cec22019-05-07 20:59:43 +02001072static void process_action(char *temp, unsigned my_pid)
Jan Klötzked04b7262019-05-03 22:35:50 +02001073{
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ötzke498cec22019-05-07 20:59:43 +02001098 if (my_pid)
1099 open_mdev_log(seq, my_pid);
Jan Klötzked04b7262019-05-03 22:35:50 +02001100
1101 seq_fd = -1;
Jan Klötzke498cec22019-05-07 20:59:43 +02001102 if (my_pid && seq) {
Jan Klötzked04b7262019-05-03 22:35:50 +02001103 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
1137static 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 Vlasenko689d0652020-10-01 21:52:16 +02001150 fileAction, dirAction, temp);
Jan Klötzked04b7262019-05-03 22:35:50 +02001151}
1152
Jan Klötzke498cec22019-05-07 20:59:43 +02001153#if ENABLE_FEATURE_MDEV_DAEMON
1154
Jan Klötzke3cd55d42019-12-16 22:56:50 +01001155/*
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ötzke498cec22019-05-07 20:59:43 +02001170# define MAX_ENV 32
1171
Jan Klötzkee3f47592020-11-22 14:17:56 +01001172static 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ötzke498cec22019-05-07 20:59:43 +02001195static void daemon_loop(char *temp, int fd)
1196{
1197 for (;;) {
Jan Klötzke3cd55d42019-12-16 22:56:50 +01001198 char netbuf[USER_RCVBUF];
Jan Klötzke498cec22019-05-07 20:59:43 +02001199 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ötzked5600302019-12-16 22:56:52 +01001206 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 Byrne69374872019-07-02 11:35:03 +02001216 bb_simple_perror_msg_and_die("read");
Jan Klötzke498cec22019-05-07 20:59:43 +02001217 }
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 Vlasenko9b49a5e2007-10-11 10:05:36 +00001239int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +00001240int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +00001241{
Jan Klötzke498cec22019-05-07 20:59:43 +02001242 enum {
1243 MDEV_OPT_SCAN = 1 << 0,
Jan Klötzkee9003ca2019-12-16 22:56:53 +01001244 MDEV_OPT_SYSLOG = 1 << 1,
1245 MDEV_OPT_DAEMON = 1 << 2,
1246 MDEV_OPT_FOREGROUND = 1 << 3,
Jan Klötzke498cec22019-05-07 20:59:43 +02001247 };
Jan Klötzke728e53d2019-05-03 22:35:51 +02001248 int opt;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001249 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +00001250
Denys Vlasenko16714242011-09-21 01:59:15 +02001251 INIT_G();
1252
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001253 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001254 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001255 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001256
Denis Vlasenko3798db52009-04-19 21:37:07 +00001257 /* Force the configuration file settings exactly */
1258 umask(0);
1259
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +00001260 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +00001261
Jan Klötzke768153b2019-12-16 22:56:54 +01001262 opt = getopt32(argv, "^"
1263 "sS" IF_FEATURE_MDEV_DAEMON("df") "v"
1264 "\0"
1265 "vv",
1266 &G.verbose);
Jan Klötzke728e53d2019-05-03 22:35:51 +02001267
Jan Klötzke498cec22019-05-07 20:59:43 +02001268#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ötzkee9003ca2019-12-16 22:56:53 +01001276 if (opt & MDEV_OPT_SYSLOG) {
1277 openlog(applet_name, LOG_PID, LOG_DAEMON);
1278 logmode |= LOGMODE_SYSLOG;
1279 }
1280
Jan Klötzke498cec22019-05-07 20:59:43 +02001281#if ENABLE_FEATURE_MDEV_DAEMON
1282 if (opt & MDEV_OPT_DAEMON) {
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001283 /* Daemon mode listening on uevent netlink socket. Fork away
Jan Klötzkee3f47592020-11-22 14:17:56 +01001284 * after initial scan so that caller can be sure everything
1285 * is up-to-date when mdev process returns.
Jan Klötzke498cec22019-05-07 20:59:43 +02001286 */
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001287 int fd = daemon_init(temp);
Jan Klötzke498cec22019-05-07 20:59:43 +02001288
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001289 if (!(opt & MDEV_OPT_FOREGROUND)) {
1290 /* there is no point in logging to /dev/null */
1291 logmode &= ~LOGMODE_STDIO;
Jan Klötzke498cec22019-05-07 20:59:43 +02001292 bb_daemonize_or_rexec(0, argv);
Denys Vlasenko03eb6eb2020-11-22 16:35:56 +01001293 }
Jan Klötzke498cec22019-05-07 20:59:43 +02001294
Jan Klötzke498cec22019-05-07 20:59:43 +02001295 daemon_loop(temp, fd);
1296 }
1297#endif
Jan Klötzke728e53d2019-05-03 22:35:51 +02001298 if (opt & MDEV_OPT_SCAN) {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +01001299 /*
1300 * Scan: mdev -s
Mike Frysingerc881c732007-11-19 09:04:22 +00001301 */
Jan Klötzked04b7262019-05-03 22:35:50 +02001302 initial_scan(temp);
Rob Landley29e08ff2006-01-12 06:13:50 +00001303 } else {
Jan Klötzked04b7262019-05-03 22:35:50 +02001304 process_action(temp, getpid());
Denis Vlasenko018bee62008-07-15 22:33:13 +00001305
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001306 dbg1("%s exiting", curtime());
Rob Landley29e08ff2006-01-12 06:13:50 +00001307 }
1308
Mike Frysingerc881c732007-11-19 09:04:22 +00001309 if (ENABLE_FEATURE_CLEAN_UP)
1310 RELEASE_CONFIG_BUFFER(temp);
1311
Denis Vlasenkoaafbae62009-04-13 13:33:02 +00001312 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +00001313}