namedev.c revision 7a947ce51586fd4212447643df90580542777ab9
/*
*
* Userspace devfs
*
* Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
*
*
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include "libsysfs/sysfs/libsysfs.h"
#include "list.h"
#include "udev.h"
#include "udev_lib.h"
#include "udev_version.h"
#include "logging.h"
#include "namedev.h"
#include "klibc_fixups.h"
#include "udevdb.h"
static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
/* compare string with pattern (supports * ? [0-9] [!A-Z]) */
static int strcmp_pattern(const char *p, const char *s)
{
if (s[0] == '\0') {
while (p[0] == '*')
p++;
return (p[0] != '\0');
}
switch (p[0]) {
case '[':
{
int not = 0;
p++;
if (p[0] == '!') {
not = 1;
p++;
}
while ((p[0] != '\0') && (p[0] != ']')) {
int match = 0;
if (p[1] == '-') {
if ((s[0] >= p[0]) && (s[0] <= p[2]))
match = 1;
p += 3;
} else {
match = (p[0] == s[0]);
p++;
}
while ((p[0] != '\0') && (p[0] != ']'))
p++;
if (p[0] == ']')
}
}
}
break;
case '*':
if (strcmp_pattern(p, s+1))
return strcmp_pattern(p+1, s);
return 0;
case '\0':
if (s[0] == '\0') {
return 0;
}
break;
default:
if ((p[0] == s[0]) || (p[0] == '?'))
break;
}
return 1;
}
{
struct perm_device *perm;
continue;
return perm;
}
return NULL;
}
static mode_t get_default_mode(void)
{
if (strlen(default_mode_str) != 0)
return mode;
}
static char *get_default_owner(void)
{
if (strlen(default_owner_str) == 0)
return default_owner_str;
}
static char *get_default_group(void)
{
if (strlen(default_group_str) == 0)
return default_group_str;
}
/* extract possible {attr} and move str behind it */
static char *get_format_attribute(char **str)
{
char *pos;
if (*str[0] == '{') {
dbg("missing closing brace for format");
return NULL;
}
pos[0] = '\0';
}
return attr;
}
/* extract possible format length and move str behind it*/
static int get_format_len(char **str)
{
int num;
char *tail;
if (num > 0) {
return num;
} else {
}
}
return -1;
}
/** Finds the lowest positive N such that <name>N isn't present in
* $(udevroot) either as a file or a symlink.
*
* @param name Name to check for
* @return 0 if <name> didn't exist and N otherwise.
*/
{
int result;
/* have to sweep the database for each lookup */
result = 0;
while (1) {
goto found;
/* symlink might be stale if $(udevroot) isn't cleaned; check
*/
goto found;
}
return result;
}
struct sysfs_class_device *class_dev,
struct sysfs_device *sysfs_device)
{
char *tail;
char *pos;
char *attr;
int len;
int i;
char c;
char *spos;
char *rest;
int slen;
struct sysfs_attribute *tmpattr;
unsigned int next_free_number;
while (1) {
break;
pos[0] = '\0';
c = tail[0];
switch (c) {
case 'b':
break;
break;
case 'k':
break;
break;
case 'n':
break;
break;
case 'm':
break;
case 'M':
break;
case 'c':
break;
/* get part part of the result string */
i = 0;
if (i > 0) {
i--;
if (i == 0)
break;
}
if (i > 0) {
dbg("requested part of result string not found");
break;
}
if (rest[0] == '+')
else
} else {
}
break;
case 's':
break;
}
/* strip trailing whitespace of matching value */
i--;
if (i < len) {
dbg("remove %i trailing whitespace chars from '%s'",
}
}
} else {
dbg("missing attribute");
}
break;
case '%':
pos++;
break;
case 'e':
if (next_free_number > 0) {
}
break;
default:
dbg("unknown substitution type '%%%c'", c);
break;
}
/* truncate to specified length */
if (len > 0)
}
}
{
while (*temp != 0x00) {
/* Some block devices have a ! in their name,
* we need to change that to / */
if (*temp == '!')
*temp = '/';
++temp;
}
}
{
int retval;
int count;
int status;
int fds[2];
char *pos;
char arg[PROGRAM_SIZE];
int i;
i = 0;
if (pos[0] == '\'') {
/* don't separate if in apostrophes */
pos++;
pos++;
} else {
}
i++;
}
} else {
}
if (retval != 0) {
dbg("pipe failed");
return -1;
}
switch(pid) {
case 0:
/* child */
/* dup2 write side of pipe to STDOUT */
exit(1);
case -1:
dbg("fork failed");
return -1;
default:
/* parent reads from fds[0] */
retval = 0;
i = 0;
while (1) {
if (count <= 0)
break;
i += count;
if (i >= len-1) {
retval = -1;
break;
}
}
if (count < 0) {
retval = -1;
}
i--;
value[i] = '\0';
retval = -1;
}
}
return retval;
}
static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
{
char *c;
/* try to find the attribute in the class device directory */
if (tmpattr)
goto attr_found;
/* look in the class device directory if present */
if (sysfs_device) {
if (tmpattr)
goto attr_found;
}
return NULL;
if (c != NULL)
c[0] = '\0';
return tmpattr;
}
static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
{
struct sysfs_attribute *tmpattr;
int i;
int len;
return -ENODEV;
return -ENODEV;
/* strip trailing whitespace of value, if not asked to match for it */
i--;
if (i < len) {
dbg("remove %i trailing whitespace chars from '%s'",
}
}
dbg("compare attribute '%s' value '%s' with '%s'",
return -ENODEV;
dbg("found matching attribute '%s' with value '%s'",
return 0;
}
static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
{
struct sysfs_pair *pair;
int i;
for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
break;
dbg("sysfs attribute doesn't match");
return -ENODEV;
}
}
return 0;
}
static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
{
char path[SYSFS_PATH_MAX];
/* we have to have a sysfs device for ID to work */
if (!sysfs_device)
return -ENODEV;
temp++;
return -ENODEV;
else
return 0;
}
static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
{
char path[SYSFS_PATH_MAX];
int found;
/* we have to have a sysfs device for PLACE to work */
if (!sysfs_device)
return -ENODEV;
found = 0;
found = 1;
} else {
*temp = 0x00;
found = 1;
}
if (!found) {
dbg("place doesn't match");
return -ENODEV;
}
return 0;
}
static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
{
while (1) {
/* check for matching bus value */
if (sysfs_device == NULL) {
dbg("device has no bus");
goto try_parent;
}
goto try_parent;
} else {
}
}
/* check for matching kernel name*/
dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
goto try_parent;
} else {
}
}
/* check for matching bus id */
goto try_parent;
} else {
}
}
/* check for matching place of device */
goto try_parent;
} else {
}
}
/* check for matching sysfs pairs */
goto try_parent;
} else {
}
}
/* execute external program */
char program[PROGRAM_SIZE];
goto try_parent;
} else {
}
}
/* check for matching result of external program */
" dev->result='%s', udev->program_result='%s'",
goto try_parent;
} else {
}
}
/* Yeah, we matched! */
return 0;
dbg("try parent sysfs device");
if (sysfs_device == NULL)
return -ENODEV;
}
}
{
struct sysfs_class_device *class_dev_parent;
struct config_device *dev;
struct perm_device *perm;
char *pos;
/* Figure out where the "device"-symlink is at. For char devices this will
* always be in the class_dev->path. On block devices, only the main block
* device will have the device symlink in it's path. All partition devices
* need to look at the symlink in its parent directory.
*/
if (class_dev_parent != NULL) {
dbg("given class device has a parent, use this instead");
} else {
}
if (sysfs_device) {
dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
}
/* get kernel number */
pos--;
/* look for a matching rule to apply */
dbg("process rule");
info("configured rule in '%s' at line %i applied, '%s' is ignored",
return -1;
}
info("configured rule in '%s' at line %i applied, added symlink '%s'",
}
/* apply all_partitions flag only at a main block device */
if (dev->partitions > 0 &&
continue;
info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
goto found;
}
}
}
/* no rule was found so we use the kernel name */
goto done;
else
goto perms;
goto done;
/* get permissions given in rule */
/* get permissions given in config file or set defaults */
} else {
}
dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
done:
/* store time of action */
return 0;
}
int namedev_init(void)
{
int retval;
retval = namedev_init_rules();
if (retval)
return retval;
if (retval)
return retval;
return retval;
}