Esempio n. 1
0
int udev_device_event(struct udev_rules *rules, struct udevice *udev)
{
	int retval = 0;

	/* add device node */
	if (major(udev->devt) != 0 &&
	    (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) {
		struct udevice *udev_old;

		dbg("device node add '%s'", udev->dev->devpath);

		udev_rules_get_name(rules, udev);
		if (udev->ignore_device) {
			info("device event will be ignored");
			goto exit;
		}
		if (udev->name[0] == '\0') {
			info("device node creation supressed");
			goto exit;
		}

		/* read current database entry, we may want to cleanup symlinks */
		udev_old = udev_device_init();
		if (udev_old != NULL) {
			if (udev_db_get_device(udev_old, udev->dev->devpath) != 0) {
				udev_device_cleanup(udev_old);
				udev_old = NULL;
			} else
				info("device '%s' already in database, validate currently present symlinks",
				     udev->dev->devpath);
		}

		/* create node and symlinks */
		retval = udev_node_add(udev, udev_old);
		if (retval == 0) {
			/* store record in database */
			udev_db_add_device(udev);

			/* remove possibly left-over symlinks */
			if (udev_old != NULL) {
				struct name_entry *link_loop;
				struct name_entry *link_old_loop;
				struct name_entry *link_old_tmp_loop;

				/* remove still valid symlinks from old list */
				list_for_each_entry_safe(link_old_loop, link_old_tmp_loop, &udev_old->symlink_list, node)
					list_for_each_entry(link_loop, &udev->symlink_list, node)
						if (strcmp(link_old_loop->name, link_loop->name) == 0) {
							dbg("symlink '%s' still valid, keep it", link_old_loop->name);
							list_del(&link_old_loop->node);
							free(link_old_loop);
						}
				udev_node_remove_symlinks(udev_old);
				udev_device_cleanup(udev_old);
			}
		}
		goto exit;
	}
Esempio n. 2
0
int udev_device_event(struct udev_rules *rules, struct udevice *udev)
{
	int retval = 0;

	/* add device node */
	if (major(udev->devt) != 0 &&
	    (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) {
		struct udevice *udev_old;

		dbg("device node add '%s'", udev->dev->devpath);

		udev_rules_get_name(rules, udev);
		if (udev->ignore_device) {
			info("device event will be ignored");
			goto exit;
		}
		if (udev->name[0] == '\0') {
			info("device node creation supressed");
			goto exit;
		}

		/* read current database entry; cleanup, if it is known device */
		udev_old = udev_device_init(NULL);
		if (udev_old != NULL) {
			udev_old->test_run = udev->test_run;
			if (udev_db_get_device(udev_old, udev->dev->devpath) == 0) {
				info("device '%s' already in database, cleanup", udev->dev->devpath);
				udev_db_delete_device(udev_old);
			} else {
				udev_device_cleanup(udev_old);
				udev_old = NULL;
			}
		}

		/* create node */
		retval = udev_node_add(udev);
		if (retval != 0)
			goto exit;

		/* store in database */
		udev_db_add_device(udev);

		/* create, replace, delete symlinks according to priority */
		udev_node_update_symlinks(udev, udev_old);

		if (udev_old != NULL)
			udev_device_cleanup(udev_old);
		goto exit;
	}

	/* add netif */
	if (strcmp(udev->dev->subsystem, "net") == 0 && strcmp(udev->action, "add") == 0) {
		dbg("netif add '%s'", udev->dev->devpath);
		udev_rules_get_name(rules, udev);
		if (udev->ignore_device) {
			info("device event will be ignored");
			goto exit;
		}
		if (udev->name[0] == '\0') {
			info("device renaming supressed");
			goto exit;
		}

		/* look if we want to change the name of the netif */
		if (strcmp(udev->name, udev->dev->kernel) != 0) {
			char devpath[PATH_MAX];
			char *pos;

			retval = rename_netif(udev);
			if (retval != 0)
				goto exit;
			info("renamed netif to '%s'", udev->name);

			/* export old name */
			setenv("INTERFACE_OLD", udev->dev->kernel, 1);

			/* now change the devpath, because the kernel device name has changed */
			strlcpy(devpath, udev->dev->devpath, sizeof(devpath));
			pos = strrchr(devpath, '/');
			if (pos != NULL) {
				pos[1] = '\0';
				strlcat(devpath, udev->name, sizeof(devpath));
				sysfs_device_set_values(udev->dev, devpath, NULL, NULL);
				setenv("DEVPATH", udev->dev->devpath, 1);
				setenv("INTERFACE", udev->name, 1);
				info("changed devpath to '%s'", udev->dev->devpath);
			}
		}
		goto exit;
	}

	/* remove device node */
	if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
		struct name_entry *name_loop;

		/* import database entry, and delete it */
		if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
			udev_db_delete_device(udev);
			if (udev->ignore_remove) {
				info("ignore_remove for '%s'", udev->name);
				goto exit;
			}
			/* restore stored persistent data */
			list_for_each_entry(name_loop, &udev->env_list, node)
				putenv(name_loop->name);
		} else {
			dbg("'%s' not found in database, using kernel name '%s'",
			    udev->dev->devpath, udev->dev->kernel);
			strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
		}

		udev_rules_get_run(rules, udev);
		if (udev->ignore_device) {
			info("device event will be ignored");
			goto exit;
		}

		/* remove the node */
		retval = udev_node_remove(udev);

		/* delete or restore symlinks according to priority */
		udev_node_update_symlinks(udev, NULL);
		goto exit;
	}

	/* default devices */
	udev_rules_get_run(rules, udev);
	if (udev->ignore_device)
		info("device event will be ignored");

exit:
	return retval;
}