#include <unistd.h>
#include <syslog.h>
-#include "libsysfs/sysfs/libsysfs.h"
-#include "udev_libc_wrapper.h"
#include "udev.h"
-#include "udev_utils.h"
-#include "udev_sysfs.h"
-#include "udev_version.h"
#include "udev_rules.h"
-#include "logging.h"
+#include "udev_selinux.h"
#ifdef USE_LOG
void log_message(int priority, const char *format, ...)
int main(int argc, char *argv[], char *envp[])
{
- struct udevice udev;
+ struct sysfs_device *dev;
+ struct udevice *udev;
+ const char *maj, *min;
struct udev_rules rules;
const char *action;
const char *devpath;
const char *subsystem;
struct sigaction act;
+ int devnull;
int retval = -EINVAL;
if (argc == 2 && strcmp(argv[1], "-V") == 0) {
exit(0);
}
+ /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
+ devnull = open("/dev/null", O_RDWR);
+ if (devnull >= 0) {
+ if (devnull != STDIN_FILENO)
+ dup2(devnull, STDIN_FILENO);
+ if (devnull != STDOUT_FILENO)
+ dup2(devnull, STDOUT_FILENO);
+ if (devnull != STDERR_FILENO)
+ dup2(devnull, STDERR_FILENO);
+ if (devnull > STDERR_FILENO)
+ close(devnull);
+ }
+
logging_init("udev");
- udev_init_config();
+ if (devnull < 0)
+ err("fatal, could not open /dev/null: %s", strerror(errno));
+ udev_config_init();
+ selinux_init();
dbg("version %s", UDEV_VERSION);
/* set signal handlers */
devpath = getenv("DEVPATH");
subsystem = getenv("SUBSYSTEM");
/* older kernels passed the SUBSYSTEM only as argument */
- if (!subsystem && argc == 2)
+ if (subsystem == NULL && argc == 2)
subsystem = argv[1];
- if (!action || !subsystem || !devpath) {
+ if (action == NULL || subsystem == NULL || devpath == NULL) {
err("action, subsystem or devpath missing");
goto exit;
}
setenv("UDEV_LOG", priority, 1);
}
- udev_init_device(&udev, devpath, subsystem, action);
- udev_rules_init(&rules, 1, 0);
+ sysfs_init();
+ udev_rules_init(&rules, 0);
+
+ dev = sysfs_device_get(devpath);
+ if (dev == NULL) {
+ info("unable to open '%s'", devpath);
+ goto fail;
+ }
+
+ udev = udev_device_init();
+ if (udev == NULL)
+ goto fail;
+
+ /* override built-in sysfs device */
+ udev->dev = dev;
+ strlcpy(udev->action, action, sizeof(udev->action));
- retval = udev_process_event(&rules, &udev);
+ /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
+ maj = getenv("MAJOR");
+ min = getenv("MINOR");
+ if (maj != NULL && min != NULL)
+ udev->devt = makedev(atoi(maj), atoi(min));
+ else
+ udev->devt = udev_device_get_devt(udev);
- if (!retval && udev_run && !list_empty(&udev.run_list)) {
+ retval = udev_device_event(&rules, udev);
+
+ if (retval == 0 && !udev->ignore_device && udev_run) {
struct name_entry *name_loop;
dbg("executing run list");
- list_for_each_entry(name_loop, &udev.run_list, node) {
+ list_for_each_entry(name_loop, &udev->run_list, node) {
if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
- else
- run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
+ else {
+ char program[PATH_SIZE];
+
+ strlcpy(program, name_loop->name, sizeof(program));
+ udev_rules_apply_format(udev, program, sizeof(program));
+ run_program(program, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
+ }
}
}
- udev_rules_close(&rules);
- udev_cleanup_device(&udev);
+ udev_device_cleanup(udev);
+fail:
+ udev_rules_cleanup(&rules);
+ sysfs_cleanup();
exit:
logging_close();
- return retval;
+ if (retval != 0)
+ return 1;
+ return 0;
}