mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-10-26 08:55:18 +03:00
[PATCH] Clean up the namedev interface a bit, making the code smaller...
This commit is contained in:
parent
fa41b24051
commit
19dc5d4ce1
72
namedev.c
72
namedev.c
@ -78,8 +78,6 @@ struct config_device {
|
||||
|
||||
static LIST_HEAD(config_device_list);
|
||||
|
||||
static char sysfs_path[SYSFS_PATH_MAX];
|
||||
|
||||
static void dump_dev(struct config_device *dev)
|
||||
{
|
||||
switch (dev->type) {
|
||||
@ -445,47 +443,18 @@ exit:
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
|
||||
static int get_default_mode(struct sysfs_class_device *class_dev)
|
||||
{
|
||||
char temp[3];
|
||||
int retval = 0;
|
||||
|
||||
char *dev;
|
||||
|
||||
dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
|
||||
if (dev == NULL)
|
||||
return -ENODEV;
|
||||
|
||||
dbg("dev = %s", dev);
|
||||
|
||||
temp[0] = dev[0];
|
||||
temp[1] = dev[1];
|
||||
temp[2] = 0x00;
|
||||
*major = (int)strtol(&temp[0], NULL, 16);
|
||||
|
||||
temp[0] = dev[2];
|
||||
temp[1] = dev[3];
|
||||
temp[2] = 0x00;
|
||||
*minor = (int)strtol(&temp[0], NULL, 16);
|
||||
|
||||
dbg("found major = %d, minor = %d", *major, *minor);
|
||||
|
||||
retval = 0;
|
||||
return retval;
|
||||
/* just default everyone to rw for the world! */
|
||||
return 0666;
|
||||
}
|
||||
|
||||
|
||||
static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
|
||||
{
|
||||
struct list_head *tmp;
|
||||
int retval = 0;
|
||||
|
||||
retval = get_major_minor(class_dev, &attr->major, &attr->minor);
|
||||
if (retval) {
|
||||
dbg ("get_major_minor failed");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
list_for_each(tmp, &config_device_list) {
|
||||
struct config_device *dev = list_entry(tmp, struct config_device, node);
|
||||
if (strcmp(dev->name, class_dev->name) == 0) {
|
||||
@ -498,7 +467,7 @@ static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *at
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
attr->mode = 0666;
|
||||
attr->mode = get_default_mode(class_dev);
|
||||
attr->owner[0] = 0x00;
|
||||
attr->group[0] = 0x00;
|
||||
strcpy(attr->name, class_dev->name);
|
||||
@ -506,32 +475,14 @@ exit:
|
||||
return retval;
|
||||
}
|
||||
|
||||
int namedev_name_device(char *device_name, struct device_attr *attr)
|
||||
int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
|
||||
{
|
||||
char dev_path[SYSFS_PATH_MAX];
|
||||
struct sysfs_class_device *class_dev;
|
||||
int retval;
|
||||
|
||||
strcpy(dev_path, sysfs_path);
|
||||
strcat(dev_path, device_name);
|
||||
|
||||
dbg("looking at %s", dev_path);
|
||||
|
||||
/* open up the sysfs class device for this thing... */
|
||||
class_dev = sysfs_open_class_device(dev_path);
|
||||
if (class_dev == NULL) {
|
||||
dbg ("sysfs_open_class_device failed");
|
||||
return -ENODEV;
|
||||
}
|
||||
dbg("class_dev->name = %s", class_dev->name);
|
||||
|
||||
retval = get_attr(class_dev, attr);
|
||||
if (retval) {
|
||||
dbg ("get_attr failed");
|
||||
goto exit;
|
||||
}
|
||||
exit:
|
||||
sysfs_close_class_device(class_dev);
|
||||
if (retval)
|
||||
dbg("get_attr failed");
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
@ -539,11 +490,6 @@ int namedev_init(void)
|
||||
{
|
||||
int retval;
|
||||
|
||||
retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
|
||||
if (retval)
|
||||
return retval;
|
||||
dbg("sysfs_path = %s", sysfs_path);
|
||||
|
||||
retval = namedev_init_config();
|
||||
if (retval)
|
||||
return retval;
|
||||
|
@ -23,6 +23,8 @@
|
||||
#ifndef NAMEDEV_H
|
||||
#define NAMEDEV_H
|
||||
|
||||
struct sysfs_class_device;
|
||||
|
||||
/* namedev config files */
|
||||
#define COMMENT_CHARACTER '#'
|
||||
#define NAMEDEV_CONFIG_ROOT "/home/greg/src/udev/"
|
||||
@ -30,6 +32,6 @@
|
||||
#define NAMEDEV_CONFIG_FILE "namedev.config"
|
||||
|
||||
extern int namedev_init(void);
|
||||
extern int namedev_name_device(char *device_name, struct device_attr *attr);
|
||||
extern int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr);
|
||||
|
||||
#endif
|
||||
|
177
udev.c
177
udev.c
@ -31,8 +31,11 @@
|
||||
#include "udev.h"
|
||||
#include "udev_version.h"
|
||||
#include "namedev.h"
|
||||
#include "libsysfs/libsysfs.h"
|
||||
|
||||
|
||||
static char sysfs_path[SYSFS_PATH_MAX];
|
||||
|
||||
static char *get_action(void)
|
||||
{
|
||||
char *action;
|
||||
@ -44,16 +47,9 @@ static char *get_action(void)
|
||||
|
||||
static char *get_device(void)
|
||||
{
|
||||
static char device[255];
|
||||
char *temp;
|
||||
|
||||
temp = getenv("DEVPATH");
|
||||
if (temp == NULL)
|
||||
return NULL;
|
||||
strcpy(device, "");
|
||||
// strcpy(device, SYSFS_ROOT);
|
||||
strcat(device, temp);
|
||||
char *device;
|
||||
|
||||
device = getenv("DEVPATH");
|
||||
return device;
|
||||
}
|
||||
|
||||
@ -68,45 +64,32 @@ static char *get_device(void)
|
||||
* Yes, this will probably change when we go to a bigger major/minor
|
||||
* range, and will have to be changed at that time.
|
||||
*/
|
||||
static int get_major_minor (char *dev, int *major, int *minor)
|
||||
static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
|
||||
{
|
||||
char filename[255];
|
||||
char line[20];
|
||||
char temp[3];
|
||||
int fd;
|
||||
int retval = 0;
|
||||
|
||||
/* add the dev file to the directory and see if it's present */
|
||||
strncpy(filename, dev, sizeof(filename));
|
||||
strncat(filename, DEV_FILE, sizeof(filename));
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
dbg("Can't open %s", filename);
|
||||
char *dev;
|
||||
|
||||
dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
|
||||
if (dev == NULL)
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* get the major/minor */
|
||||
retval = read(fd, line, sizeof(line));
|
||||
if (retval < 0) {
|
||||
dbg("read error on %s", dev);
|
||||
goto exit;
|
||||
}
|
||||
dbg("dev = %s", dev);
|
||||
|
||||
temp[0] = line[0];
|
||||
temp[1] = line[1];
|
||||
temp[0] = dev[0];
|
||||
temp[1] = dev[1];
|
||||
temp[2] = 0x00;
|
||||
*major = (int)strtol(&temp[0], NULL, 16);
|
||||
|
||||
temp[0] = line[2];
|
||||
temp[1] = line[3];
|
||||
temp[0] = dev[2];
|
||||
temp[1] = dev[3];
|
||||
temp[2] = 0x00;
|
||||
*minor = (int)strtol(&temp[0], NULL, 16);
|
||||
|
||||
dbg("found major = %d, minor = %d", *major, *minor);
|
||||
|
||||
retval = 0;
|
||||
exit:
|
||||
close(fd);
|
||||
return retval;
|
||||
}
|
||||
|
||||
@ -130,15 +113,6 @@ static char *get_name(char *dev, int major, int minor)
|
||||
return &name[0];
|
||||
}
|
||||
|
||||
/*
|
||||
* Again, this will live in the naming deamon
|
||||
*/
|
||||
static int get_mode(char *name, char *dev, int major, int minor)
|
||||
{
|
||||
/* just default everyone to rw for the world! */
|
||||
return 0666;
|
||||
}
|
||||
|
||||
/*
|
||||
* We also want to add some permissions here, and possibly some symlinks
|
||||
*/
|
||||
@ -199,35 +173,61 @@ static int delete_node(char *name)
|
||||
return unlink(filename);
|
||||
}
|
||||
|
||||
static int add_device(char *device, char type, struct device_attr *attr)
|
||||
struct sysfs_class_device *get_class_dev(char *device_name)
|
||||
{
|
||||
char *name;
|
||||
char dev_path[SYSFS_PATH_MAX];
|
||||
struct sysfs_class_device *class_dev;
|
||||
|
||||
strcpy(dev_path, sysfs_path);
|
||||
strcat(dev_path, device_name);
|
||||
|
||||
dbg("looking at %s", dev_path);
|
||||
|
||||
/* open up the sysfs class device for this thing... */
|
||||
class_dev = sysfs_open_class_device(dev_path);
|
||||
if (class_dev == NULL) {
|
||||
dbg ("sysfs_open_class_device failed");
|
||||
return NULL;
|
||||
}
|
||||
dbg("class_dev->name = %s", class_dev->name);
|
||||
|
||||
return class_dev;
|
||||
}
|
||||
|
||||
static int add_device(char *device, char *subsystem)
|
||||
{
|
||||
struct sysfs_class_device *class_dev;
|
||||
struct device_attr attr;
|
||||
//char *name;
|
||||
int major;
|
||||
int minor;
|
||||
int mode;
|
||||
char type;
|
||||
//int mode;
|
||||
int retval = -EINVAL;
|
||||
#if 0
|
||||
retval = get_major_minor(device, &major, &minor);
|
||||
|
||||
/* for now, the block layer is the only place where block devices are */
|
||||
if (strcmp(subsystem, "block") == 0)
|
||||
type = 'b';
|
||||
else
|
||||
type = 'c';
|
||||
|
||||
class_dev = get_class_dev(device);
|
||||
if (class_dev == NULL)
|
||||
goto exit;
|
||||
|
||||
retval = namedev_name_device(class_dev, &attr);
|
||||
if (retval)
|
||||
return retval;
|
||||
|
||||
retval = get_major_minor(class_dev, &major, &minor);
|
||||
if (retval) {
|
||||
dbg ("get_major_minor failed");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
name = get_name(device, major, minor);
|
||||
if (name == NULL) {
|
||||
dbg ("get_name failed");
|
||||
retval = -ENODEV;
|
||||
goto exit;
|
||||
}
|
||||
sysfs_close_class_device(class_dev);
|
||||
|
||||
mode = get_mode(name, device, major, minor);
|
||||
if (mode < 0) {
|
||||
dbg ("get_mode failed");
|
||||
retval = -EINVAL;
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
return create_node(attr->name, type, attr->major, attr->minor, attr->mode);
|
||||
return create_node(attr.name, type, major, minor, attr.mode);
|
||||
|
||||
exit:
|
||||
return retval;
|
||||
@ -250,14 +250,20 @@ static int remove_device(char *device)
|
||||
exit:
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int udev_init(void)
|
||||
{
|
||||
int retval;
|
||||
|
||||
retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
|
||||
dbg("sysfs_path = %s", sysfs_path);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct device_attr attr;
|
||||
char *subsystem;
|
||||
char *action;
|
||||
char *device;
|
||||
char type;
|
||||
int retval = -EINVAL;
|
||||
|
||||
if (argc != 2) {
|
||||
@ -265,26 +271,6 @@ int main(int argc, char *argv[])
|
||||
goto exit;
|
||||
}
|
||||
|
||||
namedev_init();
|
||||
|
||||
/* sleep for a second or two to give the kernel a chance to
|
||||
* create the dev file
|
||||
*/
|
||||
sleep(2);
|
||||
|
||||
/* for now, the block layer is the only place where block devices are */
|
||||
subsystem = argv[1];
|
||||
if (strcmp(subsystem, "block") == 0)
|
||||
type = 'b';
|
||||
else
|
||||
type = 'c';
|
||||
|
||||
action = get_action();
|
||||
if (!action) {
|
||||
dbg ("no action?");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
device = get_device();
|
||||
if (!device) {
|
||||
dbg ("no device?");
|
||||
@ -292,12 +278,29 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
dbg("looking at %s", device);
|
||||
|
||||
retval = namedev_name_device(device, &attr);
|
||||
if (retval)
|
||||
return retval;
|
||||
/* we only care about class devices and block stuff */
|
||||
if (!strstr(device, "class") &&
|
||||
!strstr(device, "block")) {
|
||||
dbg("not block or class");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* sleep for a second or two to give the kernel a chance to
|
||||
* create the dev file
|
||||
*/
|
||||
sleep(1);
|
||||
|
||||
udev_init();
|
||||
namedev_init();
|
||||
|
||||
action = get_action();
|
||||
if (!action) {
|
||||
dbg ("no action?");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (strcmp(action, "add") == 0)
|
||||
return add_device(device, type, &attr);
|
||||
return add_device(device, argv[1]);
|
||||
|
||||
if (strcmp(action, "remove") == 0)
|
||||
return remove_device(device);
|
||||
|
Loading…
Reference in New Issue
Block a user