int main(int argc, char *argv[]) { struct udev *udev; static const struct option options[] = { { "debug", no_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'V' }, {} }; const char *command; unsigned int i; int rc = 1; udev = udev_new(); if (udev == NULL) goto out; log_open(); udev_set_log_fn(udev, udev_main_log); label_init("/dev"); for (;;) { int option; option = getopt_long(argc, argv, "+dhV", options, NULL); if (option == -1) break; switch (option) { case 'd': log_set_max_level(LOG_DEBUG); udev_set_log_priority(udev, LOG_DEBUG); break; case 'h': rc = adm_help(udev, argc, argv); goto out; case 'V': rc = adm_version(udev, argc, argv); goto out; default: goto out; } } command = argv[optind]; if (command != NULL) for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++) { if (strcmp(udevadm_cmds[i]->name, command) == 0) { argc -= optind; argv += optind; /* we need '0' here to reset the internal state */ optind = 0; rc = run_command(udev, udevadm_cmds[i], argc, argv); goto out; } } fprintf(stderr, "missing or unknown command\n\n"); adm_help(udev, argc, argv); rc = 2; out: label_finish(); udev_unref(udev); log_close(); return rc; }
int main(int argc, char *argv[]) { _cleanup_udev_unref_ struct udev *udev = NULL; _cleanup_udev_event_unref_ struct udev_event *event = NULL; _cleanup_udev_device_unref_ struct udev_device *dev = NULL; _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL; char syspath[UTIL_PATH_SIZE]; const char *devpath; const char *action; sigset_t mask, sigmask_orig; int err; err = fake_filesystems(); if (err < 0) return EXIT_FAILURE; udev = udev_new(); if (udev == NULL) return EXIT_FAILURE; log_debug("version %s\n", VERSION); label_init("/dev"); sigprocmask(SIG_SETMASK, NULL, &sigmask_orig); action = argv[1]; if (action == NULL) { log_error("action missing\n"); goto out; } devpath = argv[2]; if (devpath == NULL) { log_error("devpath missing\n"); goto out; } rules = udev_rules_new(udev, 1); strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL); dev = udev_device_new_from_syspath(udev, syspath); if (dev == NULL) { log_debug("unknown device '%s'\n", devpath); goto out; } udev_device_set_action(dev, action); event = udev_event_new(dev); sigfillset(&mask); sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); if (event->fd_signal < 0) { fprintf(stderr, "error creating signalfd\n"); goto out; } /* do what devtmpfs usually provides us */ if (udev_device_get_devnode(dev) != NULL) { mode_t mode = 0600; if (streq(udev_device_get_subsystem(dev), "block")) mode |= S_IFBLK; else mode |= S_IFCHR; if (!streq(action, "remove")) { mkdir_parents_label(udev_device_get_devnode(dev), 0755); mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev)); } else { unlink(udev_device_get_devnode(dev)); util_delete_path(udev, udev_device_get_devnode(dev)); } } err = udev_event_execute_rules(event, rules, &sigmask_orig); if (err == 0) udev_event_execute_run(event, NULL); out: if (event != NULL && event->fd_signal >= 0) close(event->fd_signal); label_finish(); return err ? EXIT_FAILURE : EXIT_SUCCESS; }