PATCH : new key MODEFILE
Hi, I was looking into using the key name RUN and PROGRAM in udev,
and I realised that udev attempt to run those programs even if they don't exist. This is a bit wasteful timewise...
In shell programming, you typically do something like this :
if [ -x /sbin/myprogram ]; then
/sbin/myprogram $MYARGS
fi
My first thought was to hack the RUN and PROGRAM keys to check
the existence of the program to be launched (cut to the first whitespace...), but this was a bit hackish.
I also thought that a generic facility to test file
existence/permisions would be useful. Maybe we want to replicate this kind of construct :
if [ -x /sbin/myprogram ] && [ -r /etc/itsconfigfile ]; then /sbin/myprogram $MYARGS fi And you can think of many other interesting ideas along those
lines, where you can use dummy files to impact the rules as an alternative to env variables...
So, I decided to create a real MODEMASK key in udev. This idea
is that this key take a filename and a permission key and verify that the file has such permission.
The example above would be :
MODEMASK{/sbin/myprogram}="0100", MODEMASK{/etc/itsconfigfile}="0400",
The good news is that it was not so bad, and the changes very
localised. I tested it with udev-103, and verified that the patch applies to udev-105.
Patch included...
Regards,
Jean
---------------------------------------------------------------------
udev_rules.c +60 -0
@@ -609,6 +609,53 @@ static int match_key(const char *key_namreturn -1 ; } +static int match_modemask (const char *key_name,struct udev_rule *rule,struct key *key,const char *key_file) +{ +int match; +char value[PATH_SIZE ]; +char filename[PATH_SIZE ]; +struct stat stats; +mode_t modemask; +char *eptr; + +if (key->operation !=KEY_OP_MATCH && + key->operation !=KEY_OP_NOMATCH ) +return 0 ; + +strlcpy (value, rule->buf + key->val_off,sizeof (value)); + +/* Value should be a mode mask - octal integer */ + modemask =strtol (value, &eptr,8 ); +/* Parsing error = no match */ +if (*eptr !='\0' ) +return -1 ; + +/* Check if we have an absolute path, otherwise default to /dev */ +if (key_file[0 ] ='/' ) +snprintf (filename,sizeof (filename),"%s" , key_file); +else +snprintf (filename,sizeof (filename),"%s/%s" , udev_root, key_file); + filename[sizeof (filename)-1 ] ='\0' ; + +/* If file does not exist -> no match */ +if (stat (filename, &stats) !=0 ) +return -1 ; +dbg ("match %s '%o' <-> '%o'" , key_name, modemask, stats.st_mode); + +/* Check if match -> we make a binary AND of the two numbers */ + match = stats.st_mode & modemask; +if (match && (key->operation !=KEY_OP_NOMATCH )) { +dbg ("%s is true (matching value)" , key_name); +return 0 ; + } +if (!match && (key->operation =KEY_OP_NOMATCH )) { +dbg ("%s is true (non-matching value)" , key_name); +return 0 ; + } +dbg ("%s is false" , key_name); +return -1 ; +} +/* match a single rule against a given device and possibly its parent devices */ static int match_rule (struct udevice *udev,struct udev_rule *rule) {
@@ -690,6 +737,19 @@ static int match_rule(struct udevice *ud } } +/* Check for matching MODEMASK attribute pairs */ +for (i =0 ; i < rule->modemask.count; i++) { +struct key_pair *pair = &rule->modemask.keys[i]; + +if (pair->key.operation =KEY_OP_MATCH || + pair->key.operation =KEY_OP_NOMATCH ) { +const char *key_name =key_pair_name (rule, pair); + +if (match_modemask ("MODEMASK" , rule, &pair->key, key_name)) + goto nomatch; + } + } +/* walk up the chain of parent devices and find a match */ udev->dev_parent = udev->dev;while (1 ) {
udev_rules.h +1 -0
@@ -71,6 +71,7 @@ struct udev_rule {struct key_pairs attrs;struct key_pairs env; +struct key_pairs modemask;struct key program;struct key result;struct key import;
udev_rules_parse.c +12 -0
@@ -400,6 +400,18 @@ static int add_to_rules(struct udev_rulecontinue ; } +if (strncasecmp (key,"MODEMASK{" ,sizeof ("MODEMASK{" )-1 ) =0 ) { + attr =get_key_attribute (key +sizeof ("MODEMASK" )-1 ); +if (attr =NULL ) { +err ("error parsing MODEMASK attribute" ); + goto invalid; + } +if (add_rule_key_pair (rule, &rule->modemask, operation, attr, value) !=0 ) + goto invalid; + valid =1 ; +continue ; + } +if (strcasecmp (key,"PROGRAM" ) =0 ) {add_rule_key (rule, &rule->program, operation, value); valid =1 ;
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 5 lines · Jean Tourrilhes
Hi, I was looking into using the key name RUN and PROGRAM in udev,and I realised that udev attempt to run those programs even if they don't exist.
Why would you want to set those keys to none-existent program?
- -andrey
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
iD8DBQFF38OaR6LMutpd94wRAgeNAJ4ov2WytfqoaRdfpJxtiV8nm/O3RQCdHdOK g/BYOwZ32nT36VTUpY1HQE4=IWel
-----END PGP SIGNATURE-----
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 3 lines · Jean Tourrilhes
I was looking into using the key name RUN and PROGRAM in udev,
and I realised that udev attempt to run those programs even if they don't exist. This is a bit wasteful timewise...
Well, having rules for non-existent programs seems like the "real waste", and I call it an configuration error. Care to explain the benefit of such rules and extension?
Thank,
Kay
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 3 lines · Kay Sievers
Well, having rules for non-existent programs seems like the "real waste", and I call it an configuration error. Care to explain the benefit of such rules and extension?
It's useful for distributions because it allows maintaining some rules in a different package.
OTOH I highly doubt that adding this code is an useful optimization.
Signature · 12 lines
ciao, Marco ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV _______________________________________________ Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net [email protected] https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 11 lines · Kay Sievers
Quoted 3 lines · Jean Tourrilhes
I was looking into using the key name RUN and PROGRAM in udev,
and I realised that udev attempt to run those programs even if they don't exist. This is a bit wasteful timewise...Well, having rules for non-existent programs seems like the "real waste", and I call it an configuration error. Care to explain the benefit of such rules and extension?
Thank,
Kay
Hi again,
Sorry for the delay, my wife was keeping me busy over the week
end ;-)
Thanks for your comments. I think you are only looking at it
from a strict udev perspective, while I'm looking at it from the other end. I'll try to explain it a bit better...
Obviously, you never want to put rules for non-existent
programs. However, life is usually messy, and it's difficult to guarantee that the rules and the program stays in sync over the full lifetime of a system. I usually clone my HDD and config files between the various systems I manage. By accident, the user may have the rules still exist and the program gone or moved.
Then, I do shell programming, and in shell this type of
conditional construct is quite common. For example, check the net.agent script in the udev package for the function check_program(). If it's good practice over there, it's probably good practice over here.
But, if it was only for those two reasons, I would agree with
you that it's not a big deal. That's why I designed it so that it's as generic as possible and can be used for many other things.
For example, you can do :
------------------------------------------
KERNEL="sdc1", MODEMASK{/mnt/media}="0100", RUN="mount /dev/%k /mnt/media"
------------------------------------------
You could also do :
------------------------------------------
KERNEL="sdb", MODEMASK{sda}="0001", MODEMASK{sda}="0002", MODE="0666"
------------------------------------------
If you let your imagination wild, you'll probably find many
other use.
Have fun... Jean
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 6 lines · Jean Tourrilhes
Quoted 4 lines · Kay Sievers
Quoted 3 lines · Jean Tourrilhes
I was looking into using the key name RUN and PROGRAM in udev,
and I realised that udev attempt to run those programs even if they don't exist. This is a bit wasteful timewise...
Quoted 6 lines
I designed it so that it's as
generic as possible and can be used for many other things.
For example, you can do :
------------------------------------------
KERNEL="sdc1", MODEMASK{/mnt/media}="0100", RUN="mount /dev/%k/mnt/media"
------------------------------------------
Hmm, this should also work, and give you all the usual shell magic:
RUN+="/bin/sh -c 'test -r /mnt/media && mount $root/%k /mnt/media'"
I doubt that optimizing not to spawn sh is really noticeable.
Quoted 4 lines
You could also do :
------------------------------------------
KERNEL="sdb", MODEMASK{sda}="0001", MODEMASK{sda}="0002", MODE="0666"
------------------------------------------
The node usually doesn't exist at this time. :)
Thanks,
Kay
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 9 lines · Kay Sievers
Quoted 6 lines · Jean Tourrilhes
I designed it so that it's as
generic as possible and can be used for many other things.
For example, you can do :
------------------------------------------
KERNEL="sdc1", MODEMASK{/mnt/media}="0100", RUN="mount /dev/%k/mnt/media"
------------------------------------------
Quoted 3 lines
Hmm, this should also work, and give you all the usual shell magic:
RUN+="/bin/sh -c 'test -r/mnt/media&& mount $root/%k/mnt/media'"
I doubt that optimizing not to spawn sh is really noticeable.
During boot time, it's death by the thousands cuts. Spawning a
shell on my box is 4ms.
-------------------------------------------
# time 'test -r /mnt/media' -su: test -r /mnt/media: No such file or directory
real 0m0.004s user 0m0.004s sys 0m0.000s
-------------------------------------------
Quoted 6 lines
Quoted 4 lines
You could also do :
------------------------------------------
KERNEL="sdb", MODEMASK{sda}="0001", MODEMASK{sda}="0002", MODE="0666"
------------------------------------------The node usually doesn't exist at this time. :)
Check again the rule, it's more subtle than that.
Quoted 2 lines
Thanks,
Kay
Jean
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 10 lines · Jean Tourrilhes
Quoted 8 lines · Kay Sievers
Quoted 6 lines · Jean Tourrilhes
I designed it so that it's as
generic as possible and can be used for many other things.
For example, you can do :
------------------------------------------
KERNEL="sdc1", MODEMASK{/mnt/media}="0100", RUN="mount /dev/%k/mnt/media"
------------------------------------------
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
You could also do :
------------------------------------------
KERNEL="sdb", MODEMASK{sda}="0001", MODEMASK{sda}="0002", MODE="0666"
------------------------------------------The node usually doesn't exist at this time. :)
Check again the rule, it's more subtle than that.
We don't know, if sda is created before sdb. I don't think we should match on other device properties, that just asks for timing trouble, especially during coldplug where everything (that hasn't a parent-child relation) runs in parallel.
How about a TEST{<mask>}=<file> key that accepts the usual:
e, f, d, r, w, x, or an octal bitmask?
It would look like:
TEST{r}="/etc/foo"
TEST{w}="/tmp/foo"
TEST{0002}="/tmp/foo"
I don't think, we should default to /dev, reading from there is nothing we want to encourage anybody to do. And I think a single key per rule would be fine for a start.
Thanks,
Kay
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 8 lines · Kay Sievers
Quoted 2 lines · Jean Tourrilhes
Check again the rule, it's more subtle than that.
We don't know, if sda is created before sdb. I don't think we should match on other device properties, that just asks for timing trouble, especially during coldplug where everything (that hasn't a parent-child relation) runs in parallel.
Ok, I admit that my rule was contrieved. And most likely useless.
Quoted 7 lines
How about a TEST{<mask>}=<file> key that accepts the usual:
e, f, d, r, w, x, or an octal bitmask?
It would look like:
TEST{r}="/etc/foo"
TEST{w}="/tmp/foo"
TEST{0002}="/tmp/foo"
It seems to me that accepting the letters would be a lot of
work, because of the variety of combinations, unless we can grab that from a library. But we don't want to depend on libraries.
As the setting of mode with MODE="" seems to accept only octal
bitmask, I was trying to match both with the same functionality. In any case, if you implement letters, MODE="" would be the first place where I would use it ;-)
Quoted 6 lines
I don't think, we should default to /dev, reading from there is nothing we want to encourage anybody to do. And I think a single key per rule would be fine for a start.
Thanks,
Kay
Thinking over it, we may want to think a bit more about
it. Just drop it in your todo list and in a few months check if it still make sense. I use that method to great effect ;-)
Thanks, and have fun...
Jean
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CIDÞVDEV
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel