mdev: do not complain if mdev.conf does not exist;

use #if to prevent gcc from generating dead code
 (sadly, it's more ugly this way)
This commit is contained in:
Denis Vlasenko 2008-07-26 18:35:10 +00:00
parent 48a9971bd4
commit df96df9c7c
3 changed files with 155 additions and 134 deletions

View File

@ -60,18 +60,23 @@ matched, then the default of 0:0 660 is used. To set your own default, simply
create your own total match like so:
.* 1:1 777
You can rename/relocate device nodes by using the next optional field.
<device regex> <uid>:<gid> <octal permissions> [>path]
You can rename/move device nodes by using the next optional field.
<device regex> <uid>:<gid> <octal permissions> [=path]
So if you want to place the device node into a subdirectory, make sure the path
has a trailing /. If you want to rename the device node, just place the name.
hda 0:3 660 >drives/
This will relocate "hda" into the drives/ subdirectory.
hdb 0:3 660 >cdrom
hda 0:3 660 =drives/
This will move "hda" into the drives/ subdirectory.
hdb 0:3 660 =cdrom
This will rename "hdb" to "cdrom".
Similarly, ">path" renames/moves the device but it also creates
a direct symlink /dev/DEVNAME to the renamed/moved device.
If you also enable support for executing your own commands, then the file has
the format:
<device regex> <uid>:<gid> <octal permissions> [<@|$|*> <command>]
<device regex> <uid>:<gid> <octal permissions> [=path] [@|$|*<command>]
or
<device regex> <uid>:<gid> <octal permissions> [>path] [@|$|*<command>]
The special characters have the meaning:
@ Run after creating the device.
$ Run before removing the device.

View File

@ -29,7 +29,6 @@ testing "mdev add /block/sda" \
"env - ACTION=add DEVPATH=/block/sda chroot mdev.testdir /mdev 2>&1;
ls -ln mdev.testdir/dev | $FILTER_LS" \
"\
mdev: /etc/mdev.conf: No such file or directory
brw-rw---- 1 0 0 8,0 sda
" \
"" ""

View File

@ -25,6 +25,7 @@ struct globals {
/* We use additional 64+ bytes in make_device() */
#define SCRATCH_SIZE 80
#if ENABLE_FEATURE_MDEV_RENAME
/* Builds an alias path.
* This function potentionally reallocates the alias parameter.
*/
@ -48,6 +49,7 @@ static char *build_alias(char *alias, const char *device_name)
return alias;
}
#endif
/* 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! */
@ -56,12 +58,20 @@ static void make_device(char *path, int delete)
const char *device_name;
int major, minor, type, len;
int mode = 0660;
#if ENABLE_FEATURE_MDEV_CONF
uid_t uid = 0;
gid_t gid = 0;
char *dev_maj_min = path + strlen(path);
parser_t *parser;
char *tokens[5];
#endif
#if ENABLE_FEATURE_MDEV_EXEC
char *command = NULL;
#endif
#if ENABLE_FEATURE_MDEV_RENAME
char *alias = NULL;
char aliaslink = aliaslink; /* for compiler */
#endif
char *dev_maj_min = path + strlen(path);
/* Force the configuration file settings exactly. */
umask(0);
@ -93,16 +103,15 @@ static void make_device(char *path, int delete)
if (strstr(path, "/block/"))
type = S_IFBLK;
if (ENABLE_FEATURE_MDEV_CONF) {
parser_t *parser = config_open("/etc/mdev.conf");
char *tokens[5];
#if ENABLE_FEATURE_MDEV_CONF
parser = config_open2("/etc/mdev.conf", fopen_for_read);
/* If we have config file, look up user settings */
if (!parser)
goto end_parse;
while (config_read(parser, tokens, 4, 3, "# \t", PARSE_LAST_IS_GREEDY)) {
regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
char *val;
/* Fields: regex uid:gid mode [alias] [cmd] */
@ -208,10 +217,11 @@ static void make_device(char *path, int delete)
}
#endif /* ENABLE_FEATURE_MDEV_RENAME */
#if ENABLE_FEATURE_MDEV_EXEC
/* The rest (opt): command to run */
if (!val)
break;
if (ENABLE_FEATURE_MDEV_EXEC) {
{
const char *s = "@$*";
const char *s2 = strchr(s, *val);
@ -228,13 +238,14 @@ static void make_device(char *path, int delete)
command = xstrdup(val + 1);
}
}
#endif
/* end of field parsing */
break; /* we found matching line, stop */
} /* end of "while line is read from /etc/mdev.conf" */
config_close(parser);
}
end_parse:
#endif /* ENABLE_FEATURE_MDEV_CONF */
if (!delete && sscanf(dev_maj_min, "%u:%u", &major, &minor) == 2) {
@ -247,10 +258,11 @@ static void make_device(char *path, int delete)
if (major == root_major && minor == root_minor)
symlink(device_name, "root");
if (ENABLE_FEATURE_MDEV_CONF) {
#if ENABLE_FEATURE_MDEV_CONF
chown(device_name, uid, gid);
if (ENABLE_FEATURE_MDEV_RENAME && alias) {
#if ENABLE_FEATURE_MDEV_RENAME
if (alias) {
alias = build_alias(alias, device_name);
/* move the device, and optionally
@ -260,10 +272,12 @@ static void make_device(char *path, int delete)
free(alias);
}
}
#endif
#endif
}
if (ENABLE_FEATURE_MDEV_EXEC && command) {
#if ENABLE_FEATURE_MDEV_EXEC
if (command) {
/* setenv will leak memory, use putenv/unsetenv/free */
char *s = xasprintf("MDEV=%s", device_name);
putenv(s);
@ -274,16 +288,19 @@ static void make_device(char *path, int delete)
free(s);
free(command);
}
#endif
if (delete) {
unlink(device_name);
/* At creation time, device might have been moved
* and a symlink might have been created. Undo that. */
if (ENABLE_FEATURE_MDEV_RENAME && alias) {
#if ENABLE_FEATURE_MDEV_RENAME
if (alias) {
alias = build_alias(alias, device_name);
unlink(alias);
free(alias);
}
#endif
}
}