X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=udev%2Fudevd.c;h=df5c1995bc9f06033c140b02b767b02b31986066;hb=b6a49cd681ac3ffd0702a79741fb482db85b5a36;hp=612f04d1b95012a512db8a5a18891ce22dbadfe7;hpb=c830e98d6a8e43b1b5cc8470aa750960989778bc;p=elogind.git diff --git a/udev/udevd.c b/udev/udevd.c index 612f04d1b..df5c1995b 100644 --- a/udev/udevd.c +++ b/udev/udevd.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,7 @@ #include #include "udev.h" +#include "sd-daemon.h" #define UDEVD_PRIORITY -4 #define UDEV_PRIORITY -2 @@ -73,7 +75,7 @@ static struct udev_rules *rules; static struct udev_queue_export *udev_queue_export; static struct udev_ctrl *udev_ctrl; static struct udev_monitor *monitor; -static int worker_watch[2]; +static int worker_watch[2] = { -1, -1 }; static pid_t settle_pid; static bool stop_exec_queue; static bool reload_config; @@ -95,11 +97,11 @@ enum poll_fd { }; static struct pollfd pfd[] = { - [FD_NETLINK] = { .events = POLLIN }, - [FD_WORKER] = { .events = POLLIN }, - [FD_SIGNAL] = { .events = POLLIN }, - [FD_INOTIFY] = { .events = POLLIN }, - [FD_CONTROL] = { .events = POLLIN }, + [FD_NETLINK] = { .events = POLLIN, .fd = -1 }, + [FD_WORKER] = { .events = POLLIN, .fd = -1 }, + [FD_SIGNAL] = { .events = POLLIN, .fd = -1 }, + [FD_INOTIFY] = { .events = POLLIN, .fd = -1 }, + [FD_CONTROL] = { .events = POLLIN, .fd = -1 }, }; enum event_state { @@ -121,6 +123,7 @@ struct event { const char *devpath_old; dev_t devnum; bool is_block; + int ifindex; }; static struct event *node_to_event(struct udev_list_node *node) @@ -226,8 +229,10 @@ static void worker_new(struct event *event) udev_monitor_enable_receiving(worker_monitor); worker = calloc(1, sizeof(struct worker)); - if (worker == NULL) + if (worker == NULL) { + udev_monitor_unref(worker_monitor); return; + } /* worker + event reference */ worker->refcount = 2; worker->udev = event->udev; @@ -399,13 +404,13 @@ static void event_run(struct event *event, bool force) worker_new(event); } -static void event_queue_insert(struct udev_device *dev) +static int event_queue_insert(struct udev_device *dev) { struct event *event; event = calloc(1, sizeof(struct event)); if (event == NULL) - return; + return -1; event->udev = udev_device_get_udev(dev); event->dev = dev; @@ -415,6 +420,7 @@ static void event_queue_insert(struct udev_device *dev) event->devpath_old = udev_device_get_devpath_old(dev); event->devnum = udev_device_get_devnum(dev); event->is_block = (strcmp("block", udev_device_get_subsystem(dev)) == 0); + event->ifindex = udev_device_get_ifindex(dev); udev_queue_export_device_queued(udev_queue_export, dev); info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev), @@ -426,8 +432,10 @@ static void event_queue_insert(struct udev_device *dev) /* run all events with a timeout set immediately */ if (udev_device_get_timeout(dev) > 0) { event_run(event, true); - return; + return 0; } + + return 0; } static void worker_kill(struct udev *udev, int retain) @@ -480,6 +488,10 @@ static bool is_devpath_busy(struct event *event) if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block) return true; + /* check network device ifindex */ + if (event->ifindex != 0 && event->ifindex == loop_event->ifindex) + return true; + /* check our old name */ if (event->devpath_old != NULL && strcmp(loop_event->devpath, event->devpath_old) == 0) { event->delaying_seqnum = loop_event->seqnum; @@ -495,6 +507,11 @@ static bool is_devpath_busy(struct event *event) /* identical device event found */ if (loop_event->devpath_len == event->devpath_len) { + /* devices names might have changed/swapped in the meantime */ + if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block)) + continue; + if (event->ifindex != 0 && event->ifindex != loop_event->ifindex) + continue; event->delaying_seqnum = loop_event->seqnum; return true; } @@ -805,7 +822,7 @@ static void static_dev_create_from_modules(struct udev *udev) continue; util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/", devname, NULL); - util_create_path(udev, filename); + util_create_path_selinux(udev, filename); udev_selinux_setfscreatecon(udev, filename, mode); info(udev, "mknod '%s' %c%u:%u\n", filename, type, maj, min); if (mknod(filename, mode, makedev(maj, min)) < 0 && errno == EEXIST) @@ -816,7 +833,7 @@ static void static_dev_create_from_modules(struct udev *udev) fclose(f); } -static int copy_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth) +static int copy_dev_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth) { struct dirent *dent; @@ -869,7 +886,7 @@ static int copy_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth) continue; } - copy_dir(udev, dir2_from, dir2_to, maxdepth-1); + copy_dev_dir(udev, dir2_from, dir2_to, maxdepth-1); closedir(dir2_to); closedir(dir2_from); @@ -895,10 +912,14 @@ static void static_dev_create_links(struct udev *udev, DIR *dir) unsigned int i; for (i = 0; i < ARRAY_SIZE(stdlinks); i++) { - udev_selinux_setfscreateconat(udev, dirfd(dir), stdlinks[i].link, S_IFLNK); - if (symlinkat(stdlinks[i].target, dirfd(dir), stdlinks[i].link) < 0 && errno == EEXIST) - utimensat(dirfd(dir), stdlinks[i].link, NULL, AT_SYMLINK_NOFOLLOW); - udev_selinux_resetfscreatecon(udev); + struct stat sb; + + if (stat(stdlinks[i].target, &sb) == 0) { + udev_selinux_setfscreateconat(udev, dirfd(dir), stdlinks[i].link, S_IFLNK); + if (symlinkat(stdlinks[i].target, dirfd(dir), stdlinks[i].link) < 0 && errno == EEXIST) + utimensat(dirfd(dir), stdlinks[i].link, NULL, AT_SYMLINK_NOFOLLOW); + udev_selinux_resetfscreatecon(udev); + } } } @@ -909,7 +930,7 @@ static void static_dev_create_from_devices(struct udev *udev, DIR *dir) dir_from = opendir(LIBEXECDIR "/devices"); if (dir_from == NULL) return; - copy_dir(udev, dir_from, dir, 8); + copy_dev_dir(udev, dir_from, dir, 8); closedir(dir_from); } @@ -950,6 +971,98 @@ static int mem_size_mb(void) return memsize; } +static int convert_db(struct udev *udev) +{ + char filename[UTIL_PATH_SIZE]; + FILE *f; + struct udev_enumerate *udev_enumerate; + struct udev_list_entry *list_entry; + + /* current database */ + util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/data", NULL); + if (access(filename, F_OK) >= 0) + return 0; + + /* make sure we do not get here again */ + util_create_path(udev, filename); + mkdir(filename, 0755); + + /* old database */ + util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db", NULL); + if (access(filename, F_OK) < 0) + return 0; + + f = fopen("/dev/kmsg", "w"); + if (f != NULL) { + fprintf(f, "<30>udev[%u]: converting old udev database\n", getpid()); + fclose(f); + } + + udev_enumerate = udev_enumerate_new(udev); + if (udev_enumerate == NULL) + return -1; + udev_enumerate_scan_devices(udev_enumerate); + udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { + struct udev_device *device; + + device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry)); + if (device == NULL) + continue; + + /* try to find the old database for devices without a current one */ + if (udev_device_read_db(device, NULL) < 0) { + bool have_db; + const char *id; + struct stat stats; + char devpath[UTIL_PATH_SIZE]; + char from[UTIL_PATH_SIZE]; + + have_db = false; + + /* find database in old location */ + id = udev_device_get_id_filename(device); + util_strscpyl(from, sizeof(from), udev_get_dev_path(udev), "/.udev/db/", id, NULL); + if (lstat(from, &stats) == 0) { + if (!have_db) { + udev_device_read_db(device, from); + have_db = true; + } + unlink(from); + } + + /* find old database with $subsys:$sysname name */ + util_strscpyl(from, sizeof(from), udev_get_dev_path(udev), + "/.udev/db/", udev_device_get_subsystem(device), ":", + udev_device_get_sysname(device), NULL); + if (lstat(from, &stats) == 0) { + if (!have_db) { + udev_device_read_db(device, from); + have_db = true; + } + unlink(from); + } + + /* find old database with the encoded devpath name */ + util_path_encode(udev_device_get_devpath(device), devpath, sizeof(devpath)); + util_strscpyl(from, sizeof(from), udev_get_dev_path(udev), "/.udev/db/", devpath, NULL); + if (lstat(from, &stats) == 0) { + if (!have_db) { + udev_device_read_db(device, from); + have_db = true; + } + unlink(from); + } + + /* write out new database */ + if (have_db) + udev_device_update_db(device); + } + udev_device_unref(device); + } + udev_enumerate_unref(udev_enumerate); + return 0; +} + int main(int argc, char *argv[]) { struct udev *udev; @@ -979,10 +1092,33 @@ int main(int argc, char *argv[]) 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); + } + } + /* 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; - option = getopt_long(argc, argv, "cdeDthV", options, NULL); + option = getopt_long(argc, argv, "c:deDtN:hV", options, NULL); if (option == -1) break; @@ -1033,12 +1169,6 @@ int main(int argc, char *argv[]) } } - if (getuid() != 0) { - fprintf(stderr, "root privileges required\n"); - err(udev, "root privileges required\n"); - goto exit; - } - /* * read the kernel commandline, in case we need to get into debug mode * udev.log-priority= syslog priority @@ -1073,7 +1203,21 @@ int main(int argc, char *argv[]) fclose(f); } - /* make sure std{in,out,err} fds are in a sane state */ + if (getuid() != 0) { + fprintf(stderr, "root privileges required\n"); + err(udev, "root privileges required\n"); + goto exit; + } + + /* set umask before creating any file/directory */ + chdir("/"); + umask(022); + + /* create standard links, copy static nodes, create nodes from modules */ + static_dev_create(udev); + static_dev_create_from_modules(udev); + + /* before opening new files, make sure std{in,out,err} fds are in a sane state */ fd = open("/dev/null", O_RDWR); if (fd < 0) { fprintf(stderr, "cannot open /dev/null\n"); @@ -1084,16 +1228,20 @@ int main(int argc, char *argv[]) if (write(STDERR_FILENO, 0, 0) < 0) dup2(fd, STDERR_FILENO); - udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH); + /* udevadm control socket */ + if (sd_listen_fds(true) == 1 && sd_is_socket(SD_LISTEN_FDS_START, AF_LOCAL, SOCK_DGRAM, -1)) + udev_ctrl = udev_ctrl_new_from_fd(udev, SD_LISTEN_FDS_START); + else + udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH); if (udev_ctrl == NULL) { - fprintf(stderr, "error initializing control socket"); - err(udev, "error initializing udevd socket"); + fprintf(stderr, "error initializing udev control socket"); + err(udev, "error initializing udev control socket"); rc = 1; goto exit; } if (udev_ctrl_enable_receiving(udev_ctrl) < 0) { - fprintf(stderr, "error binding control socket, seems udevd is already running\n"); - err(udev, "error binding control socket, seems udevd is already running\n"); + fprintf(stderr, "error binding udev control socket\n"); + err(udev, "error binding udev control socket\n"); rc = 1; goto exit; } @@ -1130,12 +1278,10 @@ int main(int argc, char *argv[]) IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); /* watch dynamic rules directory */ - util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL); + util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/rules.d", NULL); if (stat(filename, &statbuf) != 0) { util_create_path(udev, filename); - udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755); mkdir(filename, 0755); - udev_selinux_resetfscreatecon(udev); } inotify_add_watch(pfd[FD_INOTIFY].fd, filename, IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); @@ -1174,6 +1320,17 @@ int main(int argc, char *argv[]) goto exit; } + /* if needed, convert old database from earlier udev version */ + convert_db(udev); + + if (!debug) { + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + } + if (fd > STDERR_FILENO) + close(fd); + if (daemonize) { pid_t pid; @@ -1189,35 +1346,34 @@ int main(int argc, char *argv[]) rc = 0; goto exit; } + } else { + sd_notify(1, "READY=1"); } - f = fopen("/dev/kmsg", "w"); - if (f != NULL) { - fprintf(f, "<6>udev: starting version " VERSION "\n"); - fclose(f); - } - - if (!debug) { - dup2(fd, STDIN_FILENO); - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); - } - if (fd > STDERR_FILENO) - close(fd); - /* set scheduling priority for the main daemon process */ setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY); - chdir("/"); - umask(022); setsid(); - /* OOM_DISABLE == -17 */ - fd = open("/proc/self/oom_adj", O_RDWR); + f = fopen("/dev/kmsg", "w"); + if (f != NULL) { + fprintf(f, "<30>udev[%u]: starting version " VERSION "\n", getpid()); + fclose(f); + } + + fd = open("/proc/self/oom_score_adj", O_RDWR); if (fd < 0) { - err(udev, "error disabling OOM: %m\n"); + /* Fallback to old interface */ + fd = open("/proc/self/oom_adj", O_RDWR); + if (fd < 0) { + err(udev, "error disabling OOM: %m\n"); + } else { + /* OOM_DISABLE == -17 */ + write(fd, "-17", 3); + close(fd); + } } else { - write(fd, "-17", 3); + write(fd, "-1000", 5); close(fd); } @@ -1232,8 +1388,6 @@ int main(int argc, char *argv[]) } info(udev, "set children_max to %u\n", children_max); - static_dev_create(udev); - static_dev_create_from_modules(udev); udev_rules_apply_static_dev_perms(rules); udev_list_init(&event_list); @@ -1267,9 +1421,8 @@ int main(int argc, char *argv[]) dev = udev_monitor_receive_device(monitor); if (dev != NULL) - event_queue_insert(dev); - else - udev_device_unref(dev); + if (event_queue_insert(dev) < 0) + udev_device_unref(dev); } /* start new events */