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;
};
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 {
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)
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;
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);
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);
}
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;
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, "c:deDthV", options, NULL);
+ option = getopt_long(argc, argv, "c:deDtN:hV", options, NULL);
if (option == -1)
break;
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;
}
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);
goto exit;
}
+ /* if needed, convert old database from earlier udev version */
+ convert_db(udev);
+
if (!debug) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
f = fopen("/dev/kmsg", "w");
if (f != NULL) {
- fprintf(f, "<6>udev[%u]: starting version " VERSION "\n", getpid());
+ fprintf(f, "<30>udev[%u]: starting version " VERSION "\n", getpid());
fclose(f);
}