mdev: fix a bug where it was not stopping on first matching rule

(testsuite entry added). Revamped line parsing while at it.

function                                             old     new   delta
next_field                                             -      36     +36
make_device                                         1104    1022     -82
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 36/-82)            Total: -46 bytes
This commit is contained in:
Denis Vlasenko 2008-03-29 13:10:57 +00:00
parent 7cb808e1c5
commit 4461564c77
2 changed files with 115 additions and 112 deletions

View File

@ -6,8 +6,8 @@
# ls -ln is showing date. Need to remove that, it's variable # ls -ln is showing date. Need to remove that, it's variable
# sed: (1) "maj, min" -> "maj,min" (2) coalesce spaces # sed: (1) "maj, min" -> "maj,min" (2) coalesce spaces
# cut: remove user, group, and date # cut: remove date
FILTER_LS="sed -e 's/, */,/g' -e 's/ */ /g' | cut -d' ' -f 1,2,5,9-" FILTER_LS="sed -e 's/, */,/g' -e 's/ */ /g' | cut -d' ' -f 1-5,9-"
# testing "test name" "options" "expected result" "file input" "stdin" # testing "test name" "options" "expected result" "file input" "stdin"
@ -16,6 +16,7 @@ mkdir mdev.testdir
# We need mdev executable to be in chroot jail! # We need mdev executable to be in chroot jail!
# (will still fail with dynamically linked one, though...) # (will still fail with dynamically linked one, though...)
cp ../busybox mdev.testdir/mdev cp ../busybox mdev.testdir/mdev
mkdir mdev.testdir/etc
mkdir mdev.testdir/dev mkdir mdev.testdir/dev
mkdir -p mdev.testdir/sys/block/sda mkdir -p mdev.testdir/sys/block/sda
echo "8:0" >mdev.testdir/sys/block/sda/dev echo "8:0" >mdev.testdir/sys/block/sda/dev
@ -25,7 +26,19 @@ testing "mdev add /block/sda" \
ls -ln mdev.testdir/dev | $FILTER_LS" \ ls -ln mdev.testdir/dev | $FILTER_LS" \
"\ "\
mdev: /etc/mdev.conf: No such file or directory mdev: /etc/mdev.conf: No such file or directory
brw-rw---- 1 8,0 sda brw-rw---- 1 0 0 8,0 sda
" \
"" ""
# continuing to use directory structure from prev test
rm mdev.testdir/dev/sda
echo ".* 1:1 666" >mdev.testdir/etc/mdev.conf
echo "sda 2:2 444" >>mdev.testdir/etc/mdev.conf
testing "mdev stops on first rule" \
"env - ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1;
ls -ln mdev.testdir/dev | $FILTER_LS" \
"\
brw-rw-rw- 1 1 1 8,0 sda
" \ " \
"" "" "" ""

View File

@ -24,6 +24,16 @@ struct globals {
/* We use additional 64+ bytes in make_device() */ /* We use additional 64+ bytes in make_device() */
#define SCRATCH_SIZE 80 #define SCRATCH_SIZE 80
static char *next_field(char *s)
{
char *end = skip_non_whitespace(s);
s = skip_whitespace(end);
*end = '\0';
if (*s == '\0')
s = NULL;
return s;
}
/* mknod in /dev based on a path like "/sys/block/hda/hda1" */ /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
/* NB: "mdev -s" may call us many times, do not leak memory/fds! */ /* NB: "mdev -s" may call us many times, do not leak memory/fds! */
static void make_device(char *path, int delete) static void make_device(char *path, int delete)
@ -63,111 +73,97 @@ static void make_device(char *path, int delete)
if (ENABLE_FEATURE_MDEV_CONF) { if (ENABLE_FEATURE_MDEV_CONF) {
FILE *fp; FILE *fp;
char *line, *vline; char *line, *val, *next;
unsigned lineno = 0; unsigned lineno = 0;
/* If we have a config file, look up the user settings */ /* If we have config file, look up user settings */
fp = fopen_or_warn("/etc/mdev.conf", "r"); fp = fopen_or_warn("/etc/mdev.conf", "r");
if (!fp) if (!fp)
goto end_parse; goto end_parse;
while ((vline = line = xmalloc_fgetline(fp)) != NULL) { while ((line = xmalloc_fgetline(fp)) != NULL) {
int field;
char *orig_line;
if (ENABLE_FEATURE_MDEV_EXEC)
orig_line = xstrdup(line); /* pristine copy for command execution. */
++lineno; ++lineno;
trim(line);
// TODO: get rid of loop, if (!line[0])
// just linear skip_whitespace() style code will do! goto next_line;
// (will also get rid of orig_line).
/* Fields: regex uid:gid mode [alias] [cmd] */ /* Fields: regex uid:gid mode [alias] [cmd] */
for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_RENAME + ENABLE_FEATURE_MDEV_EXEC); ++field) {
/* Find a non-empty field */ /* 1st field: regex to match this device */
char *val; next = next_field(line);
do { {
val = strtok(vline, " \t");
vline = NULL;
} while (val && !*val);
if (!val) {
if (field)
break;
else
goto next_line;
}
if (field == 0) {
/* Regex to match this device */
regex_t match; regex_t match;
regmatch_t off; regmatch_t off;
int result; int result;
/* Is this it? */ /* Is this it? */
xregcomp(&match, val, REG_EXTENDED); xregcomp(&match, line, REG_EXTENDED);
result = regexec(&match, device_name, 1, &off, 0); result = regexec(&match, device_name, 1, &off, 0);
regfree(&match); regfree(&match);
/* If not this device, skip rest of line */ /* If not this device, skip rest of line */
if (result || off.rm_so || off.rm_eo != strlen(device_name)) if (result || off.rm_so || off.rm_eo != strlen(device_name))
goto next_line; goto next_line;
}
} else if (field == 1) { /* This line matches: stop parsing the file
* after parsing the rest of fields */
/* uid:gid device ownership */ /* 2nd field: uid:gid - device ownership */
if (!next) /* field must exist */
bb_error_msg_and_die("bad line %u", lineno);
val = next;
next = next_field(val);
{
struct passwd *pass; struct passwd *pass;
struct group *grp; struct group *grp;
char *str_uid = val; char *str_uid = val;
char *str_gid = strchrnul(val, ':'); char *str_gid = strchrnul(val, ':');
if (*str_gid) if (*str_gid)
*str_gid++ = '\0'; *str_gid++ = '\0';
/* Parse UID */ /* Parse UID */
pass = getpwnam(str_uid); pass = getpwnam(str_uid);
if (pass) if (pass)
uid = pass->pw_uid; uid = pass->pw_uid;
else else
uid = strtoul(str_uid, NULL, 10); uid = strtoul(str_uid, NULL, 10);
/* Parse GID */ /* Parse GID */
grp = getgrnam(str_gid); grp = getgrnam(str_gid);
if (grp) if (grp)
gid = grp->gr_gid; gid = grp->gr_gid;
else else
gid = strtoul(str_gid, NULL, 10); gid = strtoul(str_gid, NULL, 10);
}
} else if (field == 2) { /* 3rd field: mode - device permissions */
if (!next) /* field must exist */
/* Mode device permissions */ bb_error_msg_and_die("bad line %u", lineno);
val = next;
next = next_field(val);
mode = strtoul(val, NULL, 8); mode = strtoul(val, NULL, 8);
} else if (ENABLE_FEATURE_MDEV_RENAME && field == 3) { /* 4th field (opt): >alias */
if (ENABLE_FEATURE_MDEV_RENAME) {
if (*val != '>') if (!next)
++field; break;
else { val = next;
free(alias); /* don't leak in case we matched it on prev line */ next = next_field(val);
if (*val == '>') {
alias = xstrdup(val + 1); alias = xstrdup(val + 1);
} }
} }
if (ENABLE_FEATURE_MDEV_EXEC && field == 3 + ENABLE_FEATURE_MDEV_RENAME) { /* The rest (opt): command to run */
if (!next)
/* Optional command to run */ break;
val = next;
if (ENABLE_FEATURE_MDEV_EXEC) {
const char *s = "@$*"; const char *s = "@$*";
const char *s2 = strchr(s, *val); const char *s2 = strchr(s, *val);
if (!s2) { if (!s2)
/* Force error */ bb_error_msg_and_die("bad line %u", lineno);
field = 1;
break;
}
/* Correlate the position in the "@$*" with the delete /* Correlate the position in the "@$*" with the delete
* step so that we get the proper behavior: * step so that we get the proper behavior:
@ -176,22 +172,16 @@ static void make_device(char *path, int delete)
* *cmd: run on both * *cmd: run on both
*/ */
if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) { if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
free(command); /* don't leak in case we matched it on prev line */ command = xstrdup(val + 1);
command = xstrdup(orig_line + (val + 1 - line));
} }
} }
} /* end of "for every field" */ /* end of field parsing */
break; /* we found matching line, stop */
/* Did everything parse happily? */
if (field <= 2)
bb_error_msg_and_die("bad line %u", lineno);
next_line: next_line:
free(line); free(line);
if (ENABLE_FEATURE_MDEV_EXEC)
free(orig_line);
} /* end of "while line is read from /etc/mdev.conf" */ } /* end of "while line is read from /etc/mdev.conf" */
free(line); /* in case we used "break" to get here */
fclose(fp); fclose(fp);
} }
end_parse: end_parse: