namedev.c revision 50e5de03d16be3e35e65cdf65f552807ad24e996
/*
*
* 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 <time.h>
#include "list.h"
#include "udev.h"
#include "udev_version.h"
#include "logging.h"
#include "namedev.h"
#include "libsysfs/libsysfs.h"
#include "klibc_fixups.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;
}
if (b->var) \
#define copy_string(a, b, var) \
{
struct perm_device *dev;
struct perm_device *tmp_dev;
/* update the values if we already have the device */
continue;
return 0;
}
/* not found, add new structure to the perm list */
if (!tmp_dev)
return -ENOMEM;
//dump_perm_dev(tmp_dev);
return 0;
}
{
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;
return num;
} else {
}
}
return -1;
}
static void apply_format(struct udevice *udev, unsigned char *string, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
{
char *tail;
char *pos;
char *pos2;
char *pos3;
char *attr;
int num;
char c;
struct sysfs_attribute *tmpattr;
while (1) {
pos[0] = '\0';
c = tail[0];
} else {
break;
}
switch (c) {
case 'b':
break;
break;
case 'k':
break;
break;
case 'n':
break;
break;
case 'm':
break;
case 'M':
break;
case 'c':
break;
if (num > 0) {
while (num) {
num--;
dbg("requested part of result string not found");
break;
}
}
if (pos3) {
}
} else {
}
break;
case 's':
break;
}
} else {
dbg("missing attribute");
}
break;
case '%':
break;
default:
dbg("unknown substitution type '%%%c'", c);
break;
}
}
}
/*
* Note, we can have multiple files for different busses in here due
* to the mess that USB has for its device tree...
*/
static struct bus_file {
char *bus;
char *file;
} bus_files[] = {
{}
};
#define SECONDS_TO_WAIT_FOR_FILE 10
{
/* sleep until we see the file for this specific bus type show up this
* is needed because we can easily out-run the kernel in looking for
* these files before the paticular subsystem has created them in the
* sysfs tree properly.
*
* be slow, poo on you for arguing that before even testing it...
*/
struct sysfs_attribute *tmpattr;
int found = 0;
int loop = SECONDS_TO_WAIT_FOR_FILE;
while (1) {
if (!found)
break;
/* sleep to give the kernel a chance to create the file */
sleep(1);
--loop;
if (loop == 0)
break;
b = &bus_files[0];
}
found = 1;
if (tmpattr) {
/* found it! */
goto exit;
}
}
++b;
}
if (!found)
dbg("did not find bus type '%s' on list of bus_id_files, "
exit:
return; /* here to prevent compiler warning... */
}
{
while (*temp != 0x00) {
/* Some block devices have a ! in their name,
* we need to change that to / */
if (*temp == '!')
*temp = '/';
++temp;
}
}
{
int retval;
int res;
int status;
int fds[2];
int value_set = 0;
char buffer[256];
char *pos;
char *args[PROGRAM_MAXARG];
int i;
if (retval != 0) {
dbg("pipe failed");
return -1;
}
switch(pid) {
case 0:
/* child */
/* exec with arguments */
for (i=0; i < PROGRAM_MAXARG-1; i++) {
break;
}
if (args[i]) {
dbg("too many args - %d", i);
}
} else {
}
dbg("child execve failed");
exit(1);
case -1:
dbg("fork failed");
return -1;
default:
/* parent reads from fds[0] */
retval = 0;
while (1) {
if (res <= 0)
break;
retval = -1;
}
if (value_set) {
dbg("result value already set");
retval = -1;
} else {
value_set = 1;
if (pos[0] == '\n')
pos[0] = '\0';
}
}
if (res < 0) {
retval = -1;
}
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;
return -ENODEV;
return -ENODEV;
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;
}
{
struct sysfs_device *sysfs_device;
struct sysfs_class_device *class_dev_parent;
int loop;
/* Figure out where the device symlink is at. For char devices this will
* always be in the class_dev->path. But for block devices, it's different.
* The main block device will have the device symlink in it's path, but
* all partitions have the symlink in its parent directory.
* But we need to watch out for block devices that do not have parents, yet
* look like a partition (fd0, loop0, etc.) They all do not have a device
* symlink yet. We do sit and spin on waiting for them right now, we should
* possibly have a whitelist for these devices here...
*/
if (class_dev_parent)
dbg("Really a partition");
loop = 10;
while (loop--) {
if (class_dev_parent)
else
if (sysfs_device != NULL)
goto device_found;
}
dbg("timed out waiting for device symlink, continuing on anyway...");
/* We have another issue with just the wait above - the sysfs part of
* the kernel may not be quick enough to have created the link to the
* device under the "bus" subsystem. Due to this, the sysfs_device->bus
* will not contain the actual bus name :(
*
* Libsysfs now provides a new API sysfs_get_device_bus(), so use it
* if needed
*/
if (sysfs_device) {
goto bus_found;
loop = 10;
while (loop--) {
goto bus_found;
}
dbg("timed out waiting to find the device bus, continuing on anyway");
goto exit;
dbg("device %s is registered with bus '%s'",
}
exit:
return sysfs_device;
}
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 */
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 config_device *dev;
struct perm_device *perm;
char *pos;
/* find the sysfs_device associated with this class device */
if (sysfs_device) {
}
/* 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, '%s' becomes '%s'",
goto found;
}
}
/* no rule was found so we use the kernel name */
goto done;
/* substitute placeholder */
done:
if (perm) {
} else {
/* no matching perms found :( */
}
dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
return 0;
}
int namedev_init(void)
{
int retval;
retval = namedev_init_rules();
if (retval)
return retval;
if (retval)
return retval;
return retval;
}