summaryrefslogtreecommitdiff
path: root/udev_device.c
diff options
context:
space:
mode:
Diffstat (limited to 'udev_device.c')
-rw-r--r--udev_device.c138
1 files changed, 77 insertions, 61 deletions
diff --git a/udev_device.c b/udev_device.c
index 7821c91cb4..0325001428 100644
--- a/udev_device.c
+++ b/udev_device.c
@@ -26,83 +26,99 @@
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
-#include <dirent.h>
-#include <syslog.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/utsname.h>
-
-#include "udev_libc_wrapper.h"
+
#include "udev.h"
-#include "logging.h"
-#include "udev_utils.h"
-#include "list.h"
+#include "udev_rules.h"
-int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
+struct udevice *udev_device_init(void)
{
- char *pos;
+ struct udevice *udev;
+ udev = malloc(sizeof(struct udevice));
+ if (udev == NULL)
+ return NULL;
memset(udev, 0x00, sizeof(struct udevice));
+
INIT_LIST_HEAD(&udev->symlink_list);
INIT_LIST_HEAD(&udev->run_list);
INIT_LIST_HEAD(&udev->env_list);
- if (subsystem)
- strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
-
- if (action)
- strlcpy(udev->action, action, sizeof(udev->action));
-
- if (devpath) {
- strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
- remove_trailing_chars(udev->devpath, '/');
-
- if (strncmp(udev->devpath, "/block/", 7) == 0)
- udev->type = DEV_BLOCK;
- else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
- udev->type = DEV_NET;
- else if (strncmp(udev->devpath, "/class/", 7) == 0)
- udev->type = DEV_CLASS;
- else if (strncmp(udev->devpath, "/devices/", 9) == 0)
- udev->type = DEV_DEVICE;
-
- /* get kernel name */
- pos = strrchr(udev->devpath, '/');
- if (pos) {
- strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
- dbg("kernel_name='%s'", udev->kernel_name);
-
- /* Some block devices have '!' in their name, change that to '/' */
- pos = udev->kernel_name;
- while (pos[0] != '\0') {
- if (pos[0] == '!')
- pos[0] = '/';
- pos++;
- }
-
- /* get kernel number */
- pos = &udev->kernel_name[strlen(udev->kernel_name)];
- while (isdigit(pos[-1]))
- pos--;
- strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
- dbg("kernel_number='%s'", udev->kernel_number);
- }
- }
+ /* set sysfs device to local storage, can be overridden if needed */
+ udev->dev = &udev->dev_local;
- if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
- udev->mode = 0660;
- strcpy(udev->owner, "root");
- strcpy(udev->group, "root");
- }
+ /* default node permissions */
+ udev->mode = 0660;
+ strcpy(udev->owner, "root");
+ strcpy(udev->group, "root");
- return 0;
+ return udev;
}
-void udev_cleanup_device(struct udevice *udev)
+void udev_device_cleanup(struct udevice *udev)
{
name_list_cleanup(&udev->symlink_list);
name_list_cleanup(&udev->run_list);
name_list_cleanup(&udev->env_list);
+ free(udev);
+}
+
+dev_t udev_device_get_devt(struct udevice *udev)
+{
+ const char *attr;
+ unsigned int major, minor;
+
+ /* read it from sysfs */
+ attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
+ if (attr != NULL) {
+ if (sscanf(attr, "%u:%u", &major, &minor) == 2)
+ return makedev(major, minor);
+ }
+ return makedev(0, 0);
+}
+
+int udev_device_event(struct udev_rules *rules, struct udevice *udev)
+{
+ int retval = 0;
+
+ /* device node or netif */
+ if ((major(udev->devt) != 0 || strcmp(udev->dev->subsystem, "net") == 0) &&
+ strcmp(udev->action, "add") == 0) {
+ dbg("device node or netif add '%s'", udev->dev->devpath);
+ udev_rules_get_name(rules, udev);
+ if (udev->ignore_device) {
+ info("device event will be ignored");
+ return 0;
+ }
+ /* create node, store in db */
+ if (udev->name[0] != '\0')
+ retval = udev_add_device(udev);
+ else
+ info("device node creation supressed");
+ return 0;
+ }
+
+ if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
+ struct name_entry *name_loop;
+
+ udev_rules_get_run(rules, udev);
+ if (udev->ignore_device) {
+ info("device event will be ignored");
+ return 0;
+ }
+ /* get data from db, remove db-entry, delete node */
+ retval = udev_remove_device(udev);
+
+ /* restore stored persistent data */
+ list_for_each_entry(name_loop, &udev->env_list, node)
+ putenv(name_loop->name);
+ return 0;
+ }
+
+ /* default devices */
+ udev_rules_get_run(rules, udev);
+ if (udev->ignore_device)
+ info("device event will be ignored");
+
+ return retval;
}