65
65
int root_major, root_minor;
68
68
#define G (*(struct globals*)&bb_common_bufsiz1)
69
#define root_major (G.root_major)
70
#define root_minor (G.root_minor)
71
#define subsystem (G.subsystem )
73
70
/* Prevent infinite loops in /sys symlinks */
74
71
#define MAX_SYSFS_DEPTH 3
110
107
static void make_device(char *path, int delete)
109
char *device_name, *subsystem_slash_devname;
113
110
int major, minor, type, len;
115
112
parser_t *parser;
117
114
/* Try to read major/minor string. Note that the kernel puts \n after
140
137
device_name = (char*) bb_basename(path);
141
138
/* http://kernel.org/doc/pending/hotplug.txt says that only
142
139
* "/sys/block/..." is for block devices. "/sys/bus" etc is not.
143
* But since 2.6.25 block devices are also in /sys/class/block,
144
* we use strstr("/block/") to forestall future surprises. */
140
* But since 2.6.25 block devices are also in /sys/class/block.
141
* We use strstr("/block/") to forestall future surprises. */
146
if (strstr(path, "/block/"))
143
if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
149
146
/* Make path point to "subsystem/device_name" */
150
if (path[5] == 'b') /* legacy /sys/block? */
147
subsystem_slash_devname = NULL;
148
/* Check for coldplug invocations first */
149
if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */
151
150
path += sizeof("/sys/") - 1;
151
else if (strncmp(path, "/sys/class/", 11) == 0)
153
152
path += sizeof("/sys/class/") - 1;
154
/* Example of a hotplug invocation:
156
* DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3"
157
* ("/sys" is added by mdev_main)
158
* - path does not contain subsystem
160
subsystem_slash_devname = concat_path_file(G.subsystem, device_name);
161
path = subsystem_slash_devname;
155
164
/* If we have config file, look up user settings */
156
165
if (ENABLE_FEATURE_MDEV_CONF)
196
205
continue; /* no dev, no match */
197
206
sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
198
if (sc < 1 || major != cmaj
199
209
|| (sc == 2 && minor != cmin0)
200
210
|| (sc == 3 && (minor < cmin0 || minor > cmin1))
235
245
/* If no match, skip rest of line */
236
246
/* (regexec returns whole pattern as "range" 0) */
237
if (result || off[0].rm_so
238
249
|| ((int)off[0].rm_eo != (int)strlen(str_to_match))
240
251
continue; /* this line doesn't match */
247
258
/* 2nd field: uid:gid - device ownership */
248
259
if (get_uidgid(&ugid, tokens[1], 1) == 0)
249
bb_error_msg("unknown user/group %s", tokens[1]);
260
bb_error_msg("unknown user/group %s on line %d", tokens[1], parser->lineno);
251
262
/* 3rd field: mode - device permissions */
252
mode = strtoul(tokens[2], NULL, 8);
263
bb_parse_mode(tokens[2], &mode);
255
/* 4th field (opt): >|=alias */
266
/* 4th field (opt): ">|=alias" or "!" to not create the node */
257
268
if (ENABLE_FEATURE_MDEV_RENAME && val) {
259
if (aliaslink == '>' || aliaslink == '=') {
265
s = strchrnul(val, ' ');
266
st = strchrnul(val, '\t');
269
val = (s[0] && s[1]) ? s+1 : NULL;
272
s = strchrnul(val, ' ');
273
st = strchrnul(val, '\t');
276
st = (s[0] && s[1]) ? s+1 : NULL;
279
if (aliaslink == '!' && s == a+1) {
281
/* "!": suppress node creation/deletion */
284
else if (aliaslink == '>' || aliaslink == '=') {
272
287
if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
273
291
/* substitute %1..9 with off[1..9], if any */
313
331
/* Are we running this command now?
314
332
* Run $cmd on delete, @cmd on create, *cmd on both
334
if (s2 - s != delete) {
335
/* We are here if: '*',
336
* or: '@' and delete = 0,
337
* or: '$' and delete = 1
317
339
command = xstrdup(val + 1);
331
354
if (!delete && major >= 0) {
332
355
if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST)
333
bb_perror_msg_and_die("mknod %s", node_name);
334
if (major == root_major && minor == root_minor)
356
bb_perror_msg("can't create '%s'", node_name);
357
if (major == G.root_major && minor == G.root_minor)
335
358
symlink(node_name, "root");
336
359
if (ENABLE_FEATURE_MDEV_CONF) {
337
360
chmod(node_name, mode);
346
369
if (ENABLE_FEATURE_MDEV_EXEC && command) {
347
370
/* setenv will leak memory, use putenv/unsetenv/free */
348
371
char *s = xasprintf("%s=%s", "MDEV", node_name);
349
char *s1 = xasprintf("%s=%s", "SUBSYSTEM", subsystem);
372
char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
352
375
if (system(command) == -1)
353
376
bb_perror_msg("can't run '%s'", command);
354
unsetenv("SUBSYSTEM");
377
bb_unsetenv_and_free(s1);
378
bb_unsetenv_and_free(s);
382
if (delete && major >= 0) {
362
383
if (ENABLE_FEATURE_MDEV_RENAME && alias) {
363
384
if (aliaslink == '>')
364
385
unlink(device_name);
411
433
/* Extract device subsystem -- the name of the directory
412
434
* under /sys/class/ */
413
435
if (1 == depth) {
415
subsystem = strrchr(fileName, '/');
417
subsystem = xstrdup(subsystem + 1);
437
G.subsystem = strrchr(fileName, '/');
439
G.subsystem = xstrdup(G.subsystem + 1);
420
442
return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
502
root_major = major(st.st_dev);
503
root_minor = minor(st.st_dev);
524
G.root_major = major(st.st_dev);
525
G.root_minor = minor(st.st_dev);
505
527
/* ACTION_FOLLOWLINKS is needed since in newer kernels
506
528
* /sys/block/loop* (for example) are symlinks to dirs,
535
560
action = getenv("ACTION");
536
561
env_path = getenv("DEVPATH");
537
subsystem = getenv("SUBSYSTEM");
538
if (!action || !env_path /*|| !subsystem*/)
562
G.subsystem = getenv("SUBSYSTEM");
563
if (!action || !env_path /*|| !G.subsystem*/)
540
565
fw = getenv("FIRMWARE");
566
op = index_in_strings(keywords, action);
542
567
/* If it exists, does /dev/mdev.seq match $SEQNUM?
543
568
* If it does not match, earlier mdev is running
544
569
* in parallel, and we need to wait */
567
592
snprintf(temp, PATH_MAX, "/sys%s", env_path);
568
if (strcmp(action, "remove") == 0) {
593
if (op == OP_remove) {
569
594
/* Ignoring "remove firmware". It was reported
570
595
* to happen and to cause erroneous deletion
571
596
* of device nodes. */
573
make_device(temp, 1);
598
make_device(temp, /*delete:*/ 1);
575
else if (strcmp(action, "add") == 0) {
576
make_device(temp, 0);
600
else if (op == OP_add) {
601
make_device(temp, /*delete:*/ 0);
577
602
if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
579
604
load_firmware(fw, temp);