- type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
-
- if (ENABLE_FEATURE_MDEV_CONF) {
- FILE *fp;
- char *line, *vline;
- size_t lineno = 0;
-
- /* If we have a config file, look up the user settings */
- fp = fopen_or_warn("/etc/mdev.conf", "r");
- if (!fp)
- goto end_parse;
-
- while ((vline = line = xmalloc_getline(fp)) != NULL) {
- int field;
-
- /* A pristine copy for command execution. */
- char *orig_line;
- if (ENABLE_FEATURE_MDEV_EXEC)
- orig_line = xstrdup(line);
-
- ++lineno;
-
- /* Three fields: regex, uid:gid, mode */
- for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); ++field) {
-
- /* Find a non-empty field */
- char *val;
- do {
- val = strtok(vline, " \t");
- vline = NULL;
- } while (val && !*val);
- if (!val)
- break;
-
- if (field == 0) {
-
- /* Regex to match this device */
- regex_t match;
- regmatch_t off;
- int result;
-
- /* Is this it? */
- xregcomp(&match, val, REG_EXTENDED);
- result = regexec(&match, device_name, 1, &off, 0);
- regfree(&match);
-
- /* If not this device, skip rest of line */
- if (result || off.rm_so || off.rm_eo != strlen(device_name))
- goto next_line;
-
- } else if (field == 1) {
-
- /* uid:gid device ownership */
- struct passwd *pass;
- struct group *grp;
-
- char *str_uid = val;
- char *str_gid = strchr(val, ':');
- if (str_gid)
- *str_gid = '\0', ++str_gid;
-
- /* Parse UID */
- pass = getpwnam(str_uid);
- if (pass)
- uid = pass->pw_uid;
- else
- uid = strtoul(str_uid, NULL, 10);
-
- /* parse GID */
- grp = getgrnam(str_gid);
- if (grp)
- gid = grp->gr_gid;
- else
- gid = strtoul(str_gid, NULL, 10);
-
- } else if (field == 2) {
+ /* http://kernel.org/doc/pending/hotplug.txt says that only
+ * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
+ * But since 2.6.25 block devices are also in /sys/class/block.
+ * We use strstr("/block/") to forestall future surprises. */
+ type = S_IFCHR;
+ if (strstr(path, "/block/"))
+ type = S_IFBLK;
+
+#if ENABLE_FEATURE_MDEV_CONF
+ parser = config_open2("/etc/mdev.conf", fopen_for_read);
+
+ /* If we have config file, look up user settings */
+ while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) {
+ regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
+ char *val;
+
+ /* Fields: regex uid:gid mode [alias] [cmd] */
+
+ /* 1st field: @<numeric maj,min>... */
+ if (tokens[0][0] == '@') {
+ /* @major,minor[-last] */
+ /* (useful when name is ambiguous:
+ * "/sys/class/usb/lp0" and
+ * "/sys/class/printer/lp0") */
+ int cmaj, cmin0, cmin1, sc;
+ if (major < 0)
+ continue; /* no dev, no match */
+ sc = sscanf(tokens[0], "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
+ if (sc < 1 || major != cmaj
+ || (sc == 2 && minor != cmin0)
+ || (sc == 3 && (minor < cmin0 || minor > cmin1))
+ ) {
+ continue; /* no match */
+ }
+ } else { /* ... or regex to match device name */
+ regex_t match;
+ int result;
+
+ /* Is this it? */
+ xregcomp(&match, tokens[0], REG_EXTENDED);
+ result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
+ regfree(&match);
+
+ //bb_error_msg("matches:");
+ //for (int i = 0; i < ARRAY_SIZE(off); i++) {
+ // if (off[i].rm_so < 0) continue;
+ // bb_error_msg("match %d: '%.*s'\n", i,
+ // (int)(off[i].rm_eo - off[i].rm_so),
+ // device_name + off[i].rm_so);
+ //}
+
+ /* If not this device, skip rest of line */
+ /* (regexec returns whole pattern as "range" 0) */
+ if (result || off[0].rm_so
+ || ((int)off[0].rm_eo != (int)strlen(device_name))
+ ) {
+ continue;
+ }
+ }