From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kay Sievers Date: Sun, 07 Mar 2004 01:12:23 +0000 Subject: [PATCH] allow to specify node permissions in the rule Message-Id: <20040307011223.GA22054@vrfy.org> MIME-Version: 1 Content-Type: multipart/mixed; boundary="0OAP2g/MAC+5xKAE" List-Id: To: linux-hotplug@vger.kernel.org --0OAP2g/MAC+5xKAE Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This allows to set the permissions along with the rule. This is not a general replacement for the permissions config, but it may be easier sometimes for the user to specify the permissions along with the rule, cause the permissions config file wants the final node name to match, which seems sometimes a bit difficult to guess, if format % chars are used in the NAME field. Any value not given in the rule is still be read from the permissions file or set to the default. This one will also work: BUS="usb", KERNEL="video*", NAME="my-%k", OWNER="$local" A few words to man page are also added and add_perm_dev() is moved into namedev_parse.c where it belongs to. thanks, Kay --0OAP2g/MAC+5xKAE Content-Type: text/plain; charset=us-ascii Content-Disposition: inline; filename="04-permissions-in-rule.patch" ===== namedev.c 1.126 vs edited ===== --- 1.126/namedev.c Sat Mar 6 04:02:01 2004 +++ edited/namedev.c Sun Mar 7 01:16:24 2004 @@ -46,6 +46,7 @@ LIST_HEAD(config_device_list); LIST_HEAD(perm_device_list); + /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ static int strcmp_pattern(const char *p, const char *s) { @@ -99,39 +100,6 @@ return 1; } -#define copy_var(a, b, var) \ - if (b->var) \ - a->var = b->var; - -#define copy_string(a, b, var) \ - if (strlen(b->var)) \ - strcpy(a->var, b->var); - -int add_perm_dev(struct perm_device *new_dev) -{ - struct perm_device *dev; - struct perm_device *tmp_dev; - - /* update the values if we already have the device */ - list_for_each_entry(dev, &perm_device_list, node) { - if (strcmp(new_dev->name, dev->name)) - continue; - copy_var(dev, new_dev, mode); - copy_string(dev, new_dev, owner); - copy_string(dev, new_dev, group); - return 0; - } - - /* not found, add new structure to the perm list */ - tmp_dev = malloc(sizeof(*tmp_dev)); - if (!tmp_dev) - return -ENOMEM; - memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); - list_add_tail(&tmp_dev->node, &perm_device_list); - //dump_perm_dev(tmp_dev); - return 0; -} - static struct perm_device *find_perm(char *name) { struct perm_device *perm; @@ -865,22 +833,28 @@ goto done; found: - /* substitute placeholder */ apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device); udev->partitions = dev->partitions; + done: + /* get permissions given in rule */ + set_empty_perms(udev, dev->mode, + dev->owner, + dev->group); + + /* get permissions given in config file or set defaults */ perm = find_perm(udev->name); - if (perm) { - udev->mode = perm->mode; - strfieldcpy(udev->owner, perm->owner); - strfieldcpy(udev->group, perm->group); + if (perm != NULL) { + set_empty_perms(udev, perm->mode, + perm->owner, + perm->group); } else { - /* no matching perms found :( */ - udev->mode = get_default_mode(); - strfieldcpy(udev->owner, get_default_owner()); - strfieldcpy(udev->group, get_default_group()); + set_empty_perms(udev, get_default_mode(), + get_default_owner(), + get_default_group()); } + dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", udev->name, udev->owner, udev->group, udev->mode); ===== namedev.h 1.27 vs edited ===== --- 1.27/namedev.h Sat Feb 28 22:51:04 2004 +++ edited/namedev.h Sun Mar 7 01:26:30 2004 @@ -44,6 +44,9 @@ #define FIELD_KERNEL "KERNEL" #define FIELD_NAME "NAME" #define FIELD_SYMLINK "SYMLINK" +#define FIELD_OWNER "OWNER" +#define FIELD_GROUP "GROUP" +#define FIELD_MODE "MODE" #define ATTR_PARTITIONS "all_partitions" #define PARTITIONS_COUNT 15 @@ -54,6 +57,14 @@ #define RULEFILE_EXT ".rules" #define PERMFILE_EXT ".permissions" +#define set_empty_perms(dev, m, o, g) \ + if (dev->mode == 0) \ + dev->mode = m; \ + if (dev->owner[0] == '\0') \ + strfieldcpy(dev->owner, o); \ + if (dev->group[0] == '\0') \ + strfieldcpy(dev->group, g); + struct sysfs_pair { char file[FILE_SIZE]; char value[VALUE_SIZE]; @@ -71,6 +82,9 @@ char name[NAME_SIZE]; char symlink[NAME_SIZE]; struct sysfs_pair sysfs_pair[MAX_SYSFS_PAIRS]; + char owner[OWNER_SIZE]; + char group[GROUP_SIZE]; + unsigned int mode; int partitions; char config_file[NAME_SIZE]; int config_line; @@ -93,7 +107,6 @@ extern int namedev_init_permissions(void); extern int namedev_init_rules(void); -extern int add_perm_dev(struct perm_device *new_dev); extern void dump_config_dev(struct config_device *dev); extern void dump_config_dev_list(void); extern void dump_perm_dev(struct perm_device *dev); ===== namedev_parse.c 1.30 vs edited ===== --- 1.30/namedev_parse.c Wed Mar 3 22:43:42 2004 +++ edited/namedev_parse.c Sun Mar 7 01:33:40 2004 @@ -43,6 +43,7 @@ LIST_HEAD(file_list); + static int add_config_dev(struct config_device *new_dev) { struct config_device *tmp_dev; @@ -61,9 +62,11 @@ dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', " "sysfs_file[0]='%s', sysfs_value[0]='%s', " "kernel='%s', program='%s', result='%s'", + "owner='%s', group='%s', mode=%#o", dev->name, dev->symlink, dev->bus, dev->place, dev->id, dev->sysfs_pair[0].file, dev->sysfs_pair[0].value, - dev->kernel, dev->program, dev->result); + dev->kernel, dev->program, dev->result,; + dev->owner, dev->group, dev->mode); } void dump_config_dev_list(void) @@ -74,6 +77,31 @@ dump_config_dev(dev); } +static int add_perm_dev(struct perm_device *new_dev) +{ + struct perm_device *dev; + struct perm_device *tmp_dev; + + /* update the values if we already have the device */ + list_for_each_entry(dev, &perm_device_list, node) { + if (strcmp(new_dev->name, dev->name) != 0) + continue; + + set_empty_perms(dev, new_dev->mode, new_dev->owner, new_dev->group); + return 0; + } + + /* not found, add new structure to the perm list */ + tmp_dev = malloc(sizeof(*tmp_dev)); + if (!tmp_dev) + return -ENOMEM; + + memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); + list_add_tail(&tmp_dev->node, &perm_device_list); + //dump_perm_dev(tmp_dev); + return 0; +} + void dump_perm_dev(struct perm_device *dev) { dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", @@ -240,12 +268,26 @@ continue; } + if (strcasecmp(temp2, FIELD_OWNER) == 0) { + strfieldcpy(dev.owner, temp3); + continue; + } + + if (strcasecmp(temp2, FIELD_GROUP) == 0) { + strfieldcpy(dev.group, temp3); + continue; + } + + if (strcasecmp(temp2, FIELD_MODE) == 0) { + dev.mode = strtol(temp3, NULL, 8); + continue; + } + dbg("unknown type of field '%s'", temp2); - dbg("You might be using a rules file in the old format, please fix."); goto error; } - /* simple plausibility check for given keys */ + /* simple plausibility checks for given keys */ if ((dev.sysfs_pair[0].file[0] == '\0') ^ (dev.sysfs_pair[0].value[0] == '\0')) { dbg("inconsistency in " FIELD_SYSFS " key"); @@ -336,14 +378,14 @@ strfieldcpy(dev.group, temp2); if (!temp) { - dbg("cannot parse line: %s", line); + dbg("cannot parse line '%s'", line); continue; } dev.mode = strtol(temp, NULL, 8); dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", - dev.name, dev.owner, dev.group, - dev.mode); + dev.name, dev.owner, dev.group, dev.mode); + retval = add_perm_dev(&dev); if (retval) { dbg("add_perm_dev returned with error %d", retval); ===== udev.8 1.51 vs edited ===== --- 1.51/udev.8 Sat Mar 6 23:24:18 2004 +++ edited/udev.8 Sun Mar 7 01:37:32 2004 @@ -220,6 +220,10 @@ This makes it possible to specify additional symlinks in a possibly separate rules file, while the device nodes are maintained by the distribution provided rules file. +.TP +.B OWNER, GROUP, MODE +The permissions for this device. Every specified value overwrites the value +given in the permissions file. .P .RB "The " NAME " ," SYMLINK " and " PROGRAM fields support simple printf-like string substitution: --0OAP2g/MAC+5xKAE-- ------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click _______________________________________________ Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net Linux-hotplug-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel