X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev%2Fudevd.c;h=df5c1995bc9f06033c140b02b767b02b31986066;hp=873634fe4519cf3149201b19727aee22e1b880a1;hb=0e4fa2abfefd6fd955e8cada7caf2733dabc529e;hpb=35df38c36a65bd0a9fd108f7e10ea6a593d5cdd7 diff --git a/udev/udevd.c b/udev/udevd.c index 873634fe4..df5c1995b 100644 --- a/udev/udevd.c +++ b/udev/udevd.c @@ -45,6 +45,7 @@ #include #include "udev.h" +#include "sd-daemon.h" #define UDEVD_PRIORITY -4 #define UDEV_PRIORITY -2 @@ -74,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; @@ -96,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 { @@ -122,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) @@ -227,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; @@ -400,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; @@ -416,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), @@ -427,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) @@ -481,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; @@ -496,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; } @@ -806,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) @@ -817,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; @@ -870,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); @@ -896,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); + } } } @@ -910,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); } @@ -951,81 +971,96 @@ static int mem_size_mb(void) return memsize; } -static int init_notify(const char *state) +static int convert_db(struct udev *udev) { - int fd = -1, r; - struct msghdr msghdr; - struct iovec iovec; - struct ucred *ucred; - union { - struct sockaddr sa; - struct sockaddr_un un; - } sockaddr; - union { - struct cmsghdr cmsghdr; - uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; - } control; - const char *e; - - if (!(e = getenv("NOTIFY_SOCKET"))) { - r = 0; - goto finish; - } + char filename[UTIL_PATH_SIZE]; + FILE *f; + struct udev_enumerate *udev_enumerate; + struct udev_list_entry *list_entry; - /* Must be an abstract socket, or an absolute path */ - if ((e[0] != '@' && e[0] != '/') || e[1] == 0) { - r = -EINVAL; - goto finish; - } + /* current database */ + util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/data", NULL); + if (access(filename, F_OK) >= 0) + return 0; - if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) { - r = -errno; - goto finish; - } + /* make sure we do not get here again */ + util_create_path(udev, filename); + mkdir(filename, 0755); - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sa.sa_family = AF_UNIX; - strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path)); - - if (sockaddr.un.sun_path[0] == '@') - sockaddr.un.sun_path[0] = 0; - - memset(&iovec, 0, sizeof(iovec)); - iovec.iov_base = (char*) state; - iovec.iov_len = strlen(state); - - memset(&control, 0, sizeof(control)); - control.cmsghdr.cmsg_level = SOL_SOCKET; - control.cmsghdr.cmsg_type = SCM_CREDENTIALS; - control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred)); - - ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr); - ucred->pid = getpid(); - ucred->uid = getuid(); - ucred->gid = getgid(); - - memset(&msghdr, 0, sizeof(msghdr)); - msghdr.msg_name = &sockaddr; - msghdr.msg_namelen = sizeof(sa_family_t) + strlen(e); - if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) - msghdr.msg_namelen = sizeof(struct sockaddr_un); - msghdr.msg_iov = &iovec; - msghdr.msg_iovlen = 1; - msghdr.msg_control = &control; - msghdr.msg_controllen = control.cmsghdr.cmsg_len; - - if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) { - r = -errno; - goto finish; + /* 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); } - r = 0; + 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; -finish: - if (fd >= 0) - close(fd); + 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); + } - return r; + /* 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[]) @@ -1057,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; @@ -1155,6 +1213,10 @@ int main(int argc, char *argv[]) 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) { @@ -1166,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; } @@ -1212,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); @@ -1256,6 +1320,9 @@ 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); @@ -1280,7 +1347,7 @@ int main(int argc, char *argv[]) goto exit; } } else { - init_notify("READY=1"); + sd_notify(1, "READY=1"); } /* set scheduling priority for the main daemon process */ @@ -1290,16 +1357,23 @@ int main(int argc, char *argv[]) f = fopen("/dev/kmsg", "w"); if (f != NULL) { - fprintf(f, "<6>udev: starting version " VERSION "\n"); + fprintf(f, "<30>udev[%u]: starting version " VERSION "\n", getpid()); fclose(f); } - /* OOM_DISABLE == -17 */ - fd = open("/proc/self/oom_adj", O_RDWR); + 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); } @@ -1314,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); @@ -1349,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 */