static bool debug;
-static void log_fn(struct udev *udev, int priority,
+void udev_main_log(struct udev *udev, int priority,
const char *file, int line, const char *fn,
const char *format, va_list args)
{
}
}
-static void event_run(struct event *event, bool force)
+static void event_run(struct event *event)
{
struct udev_list_node *loop;
return;
}
- if (!force && children >= children_max) {
+ if (children >= children_max) {
if (children_max > 1)
info(event->udev, "maximum number (%i) of children reached\n", children);
return;
event->state = EVENT_QUEUED;
udev_list_node_append(&event->node, &event_list);
-
- /* run all events with a timeout set immediately */
- if (udev_device_get_timeout(dev) > 0) {
- event_run(event, true);
- return 0;
- }
-
return 0;
}
continue;
}
- event_run(event, false);
+ event_run(event);
}
}
struct udev_device *dev;
ev = (struct inotify_event *)(buf + pos);
- if (ev->len) {
- const char *s;
-
- info(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
- s = strstr(ev->name, ".rules");
- if (s == NULL)
- continue;
- if (strlen(s) != strlen(".rules"))
- continue;
- reload = true;
- continue;
- }
-
dev = udev_watch_lookup(udev, ev->wd);
if (dev != NULL) {
info(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
if (sscanf(devno, "%c%u:%u", &type, &maj, &min) != 3)
continue;
- /* set sticky bit, so we do not remove the node on module unload */
if (type == 'c')
- mode = 01600|S_IFCHR;
+ mode = S_IFCHR;
else if (type == 'b')
- mode = 01600|S_IFBLK;
+ mode = S_IFBLK;
else
continue;
{
DIR *dir_from;
- dir_from = opendir(LIBEXECDIR "/devices");
+ dir_from = opendir(PKGLIBEXECDIR "/devices");
if (dir_from == NULL)
return;
copy_dev_dir(udev, dir_from, dir, 8);
return 0;
}
+static bool check_rules_timestamp(struct udev *udev)
+{
+ char **p;
+ unsigned long long *stamp_usec;
+ int i, n;
+ bool changed = false;
+
+ n = udev_get_rules_path(udev, &p, &stamp_usec);
+ for (i = 0; i < n; i++) {
+ struct stat stats;
+
+ if (stat(p[i], &stats) < 0)
+ continue;
+
+ if (stamp_usec[i] == ts_usec(&stats.st_mtim))
+ continue;
+
+ /* first check */
+ if (stamp_usec[i] != 0) {
+ info(udev, "reload - timestamp of '%s' changed\n", p[i]);
+ changed = true;
+ }
+
+ /* update timestamp */
+ stamp_usec[i] = ts_usec(&stats.st_mtim);
+ }
+
+ return changed;
+}
+
int main(int argc, char *argv[])
{
struct udev *udev;
int fd_worker = -1;
struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker;
struct udev_ctrl_connection *ctrl_conn = NULL;
+ char **s;
int rc = 1;
udev = udev_new();
goto exit;
udev_log_init("udevd");
- udev_set_log_fn(udev, log_fn);
+ udev_set_log_fn(udev, udev_main_log);
info(udev, "version %s\n", VERSION);
udev_selinux_init(udev);
- /* make sure, that our runtime dir exists and is writable */
- if (utimensat(AT_FDCWD, udev_get_run_config_path(udev), NULL, 0) < 0) {
- /* try to create our own subdirectory, do not create parent directories */
- mkdir(udev_get_run_config_path(udev), 0755);
-
- if (utimensat(AT_FDCWD, udev_get_run_config_path(udev), NULL, 0) >= 0) {
- /* directory seems writable now */
- udev_set_run_path(udev, udev_get_run_config_path(udev));
- } else {
- /* fall back to /dev/.udev */
- char filename[UTIL_PATH_SIZE];
-
- util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev", NULL);
- if (udev_set_run_path(udev, filename) == NULL)
- goto exit;
- mkdir(udev_get_run_path(udev), 0755);
- err(udev, "error: runtime directory '%s' not writable, for now falling back to '%s'",
- udev_get_run_config_path(udev), udev_get_run_path(udev));
- }
- }
- /* relabel runtime dir only if it resides below /dev */
- if (strncmp(udev_get_run_path(udev), udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) == 0)
- udev_selinux_lsetfilecon(udev, udev_get_run_path(udev), 0755);
- info(udev, "runtime dir '%s'\n", udev_get_run_path(udev));
-
for (;;) {
int option;
chdir("/");
umask(022);
+ /* /run/udev */
+ mkdir(udev_get_run_path(udev), 0755);
+
/* create standard links, copy static nodes, create nodes from modules */
static_dev_create(udev);
static_dev_create_from_modules(udev);
rc = 4;
goto exit;
}
-
- if (udev_get_rules_path(udev) != NULL) {
- inotify_add_watch(fd_inotify, udev_get_rules_path(udev),
- IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
- } else {
- char filename[UTIL_PATH_SIZE];
- struct stat statbuf;
-
- inotify_add_watch(fd_inotify, LIBEXECDIR "/rules.d",
- IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
- inotify_add_watch(fd_inotify, SYSCONFDIR "/udev/rules.d",
- IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
-
- /* watch dynamic rules directory */
- util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/rules.d", NULL);
- if (stat(filename, &statbuf) != 0) {
- util_create_path(udev, filename);
- mkdir(filename, 0755);
- }
- inotify_add_watch(fd_inotify, filename,
- IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
- }
udev_watch_restore(udev);
/* block and listen to all signals on signalfd */
udev_list_node_init(&worker_list);
for (;;) {
+ static unsigned long long last_usec;
struct epoll_event ev[8];
int fdcount;
int timeout;
is_ctrl = true;
}
+ /* check for changed config, every 3 seconds at most */
+ if ((now_usec() - last_usec) > 3 * 1000 * 1000) {
+ if (check_rules_timestamp(udev))
+ reload = true;
+ if (udev_builtin_validate(udev))
+ reload = true;
+
+ last_usec = now_usec();
+ }
+
+ /* reload requested, HUP signal received, rules changed, builtin changed */
+ if (reload) {
+ worker_kill(udev, 0);
+ rules = udev_rules_unref(rules);
+ udev_builtin_exit(udev);
+ reload = 0;
+ }
+
/* event has finished */
if (is_worker)
worker_returned(fd_worker);
if (udev_exit)
continue;
- /* device node and rules directory inotify watch */
+ /* device node watch */
if (is_inotify)
handle_inotify(udev);
*/
if (is_ctrl)
ctrl_conn = handle_ctrl_msg(udev_ctrl);
-
- /* rules changed, set by inotify or a HUP signal */
- if (reload) {
- worker_kill(udev, 0);
- rules = udev_rules_unref(rules);
- udev_builtin_exit(udev);
- reload = 0;
- }
}
rc = EXIT_SUCCESS;