summaryrefslogtreecommitdiff
path: root/udev
diff options
context:
space:
mode:
Diffstat (limited to 'udev')
-rw-r--r--udev/test-udev.c36
-rw-r--r--udev/udev-event.c102
-rw-r--r--udev/udev-node.c176
-rw-r--r--udev/udev-rules.c5
-rw-r--r--udev/udev.h3
-rw-r--r--udev/udev.xml25
-rw-r--r--udev/udevd.c5
7 files changed, 95 insertions, 257 deletions
diff --git a/udev/test-udev.c b/udev/test-udev.c
index 8039459906..8d5baf7f54 100644
--- a/udev/test-udev.c
+++ b/udev/test-udev.c
@@ -43,7 +43,6 @@ int main(int argc, char *argv[])
char syspath[UTIL_PATH_SIZE];
const char *devpath;
const char *action;
- const char *subsystem;
sigset_t mask, sigmask_orig;
int err = -EINVAL;
@@ -55,12 +54,15 @@ int main(int argc, char *argv[])
sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
- action = getenv("ACTION");
- devpath = getenv("DEVPATH");
- subsystem = getenv("SUBSYSTEM");
+ action = argv[1];
+ if (action == NULL) {
+ err(udev, "action missing\n");
+ goto out;
+ }
- if (action == NULL || subsystem == NULL || devpath == NULL) {
- err(udev, "action, subsystem or devpath missing\n");
+ devpath = argv[2];
+ if (devpath == NULL) {
+ err(udev, "devpath missing\n");
goto out;
}
@@ -73,10 +75,6 @@ int main(int argc, char *argv[])
goto out;
}
- /* skip reading of db, but read kernel parameters */
- udev_device_set_info_loaded(dev);
- udev_device_read_uevent_file(dev);
-
udev_device_set_action(dev, action);
event = udev_event_new(dev);
@@ -88,6 +86,24 @@ int main(int argc, char *argv[])
goto out;
}
+ /* do what devtmpfs usually provides us */
+ if (udev_device_get_devnode(dev) != NULL) {
+ mode_t mode;
+
+ if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
+ mode |= S_IFBLK;
+ else
+ mode |= S_IFCHR;
+
+ if (strcmp(action, "remove") != 0) {
+ util_create_path(udev, udev_device_get_devnode(dev));
+ 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);
diff --git a/udev/udev-event.c b/udev/udev-event.c
index e10d48e13b..3801f23a6b 100644
--- a/udev/udev-event.c
+++ b/udev/udev-event.c
@@ -59,7 +59,6 @@ void udev_event_unref(struct udev_event *event)
if (event == NULL)
return;
udev_list_cleanup(&event->run_list);
- free(event->tmp_node);
free(event->program_result);
free(event->name);
dbg(event->udev, "free event %p\n", event);
@@ -71,7 +70,7 @@ size_t udev_event_apply_format(struct udev_event *event, const char *src, char *
struct udev_device *dev = event->dev;
enum subst_type {
SUBST_UNKNOWN,
- SUBST_TEMP_NODE,
+ SUBST_DEVNODE,
SUBST_ATTR,
SUBST_ENV,
SUBST_KERNEL,
@@ -93,7 +92,8 @@ size_t udev_event_apply_format(struct udev_event *event, const char *src, char *
char fmt;
enum subst_type type;
} map[] = {
- { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
+ { .name = "devnode", .fmt = 'N', .type = SUBST_DEVNODE },
+ { .name = "tempnode", .fmt = 'N', .type = SUBST_DEVNODE },
{ .name = "attr", .fmt = 's', .type = SUBST_ATTR },
{ .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
{ .name = "env", .fmt = 'E', .type = SUBST_ENV },
@@ -332,7 +332,7 @@ subst:
dev_parent = udev_device_get_parent(event->dev);
if (dev_parent == NULL)
break;
- devnode = udev_device_get_devnode(dev_parent);
+ devnode = udev_device_get_devnode(dev_parent);
if (devnode != NULL) {
size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
@@ -342,56 +342,10 @@ subst:
}
break;
}
- case SUBST_TEMP_NODE: {
- dev_t devnum;
- struct stat statbuf;
- char filename[UTIL_PATH_SIZE];
- const char *devtype;
-
- if (event->tmp_node != NULL) {
- l = util_strpcpy(&s, l, event->tmp_node);
- dbg(event->udev, "tempnode: return earlier created one\n");
- break;
- }
- devnum = udev_device_get_devnum(dev);
- if (major(devnum) == 0)
- break;
- /* lookup kernel provided node */
- if (udev_device_get_knodename(dev) != NULL) {
- util_strscpyl(filename, sizeof(filename),
- udev_get_dev_path(event->udev), "/", udev_device_get_knodename(dev), NULL);
- if (stat(filename, &statbuf) == 0 && statbuf.st_rdev == devnum) {
- l = util_strpcpy(&s, l, filename);
- dbg(event->udev, "tempnode: return kernel node\n");
- break;
- }
- }
- /* lookup /dev/{char,block}/<maj>:<min> */
- if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
- devtype = "block";
- else
- devtype = "char";
- snprintf(filename, sizeof(filename), "%s/%s/%u:%u",
- udev_get_dev_path(event->udev), devtype,
- major(udev_device_get_devnum(dev)),
- minor(udev_device_get_devnum(dev)));
- if (stat(filename, &statbuf) == 0 && statbuf.st_rdev == devnum) {
- l = util_strpcpy(&s, l, filename);
- dbg(event->udev, "tempnode: return maj:min node\n");
- break;
- }
- /* create temporary node */
- dbg(event->udev, "tempnode: create temp node\n");
- asprintf(&event->tmp_node, "%s/.tmp-%s-%u:%u",
- udev_get_dev_path(event->udev), devtype,
- major(udev_device_get_devnum(dev)),
- minor(udev_device_get_devnum(dev)));
- if (event->tmp_node == NULL)
- break;
- udev_node_mknod(dev, event->tmp_node, 0600, 0, 0);
- l = util_strpcpy(&s, l, event->tmp_node);
+ case SUBST_DEVNODE:
+ if (udev_device_get_devnode(dev) != NULL)
+ l = util_strpcpy(&s, l, udev_device_get_devnode(dev));
break;
- }
case SUBST_NAME:
if (event->name != NULL) {
l = util_strpcpy(&s, l, event->name);
@@ -975,44 +929,6 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules,
}
if (major(udev_device_get_devnum(dev)) != 0) {
- char filename[UTIL_PATH_SIZE];
-
- if (event->tmp_node != NULL) {
- info(event->udev, "cleanup temporary device node\n");
- util_unlink_secure(event->udev, event->tmp_node);
- free(event->tmp_node);
- event->tmp_node = NULL;
- }
-
- /* no rule, use kernel provided name */
- if (event->name == NULL) {
- if (udev_device_get_knodename(dev) != NULL) {
- event->name = strdup(udev_device_get_knodename(dev));
- info(event->udev, "no node name set, will use kernel supplied name '%s'\n", event->name);
- } else {
- event->name = strdup(udev_device_get_sysname(event->dev));
- info(event->udev, "no node name set, will use device name '%s'\n", event->name);
- }
- }
-
- if (event->name == NULL || event->name[0] == '\0') {
- udev_device_delete_db(dev);
- udev_device_tag_index(dev, NULL, false);
- udev_device_unref(event->dev_db);
- err = -ENOMEM;
- err(event->udev, "no node name, something went wrong, ignoring\n");
- goto out;
- }
-
- if (udev_device_get_knodename(dev) != NULL && strcmp(udev_device_get_knodename(dev), event->name) != 0)
- err(event->udev, "kernel-provided name '%s' and NAME= '%s' disagree, "
- "please use SYMLINK+= or change the kernel to provide the proper name\n",
- udev_device_get_knodename(dev), event->name);
-
- /* set device node name */
- util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", event->name, NULL);
- udev_device_set_devnode(dev, filename);
-
/* remove/update possible left-over symlinks from old database entry */
if (event->dev_db != NULL)
udev_node_update_old_links(dev, event->dev_db);
@@ -1030,10 +946,6 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules,
}
}
- /* set sticky bit, so we do not remove the node on module unload */
- if (event->static_node)
- event->mode |= 01000;
-
err = udev_node_add(dev, event->mode, event->uid, event->gid);
}
diff --git a/udev/udev-node.c b/udev/udev-node.c
index 44d3686db6..31046bd713 100644
--- a/udev/udev-node.c
+++ b/udev/udev-node.c
@@ -33,90 +33,6 @@
#define TMP_FILE_EXT ".udev-tmp"
-int udev_node_mknod(struct udev_device *dev, const char *file, mode_t mode, uid_t uid, gid_t gid)
-{
- struct udev *udev = udev_device_get_udev(dev);
- dev_t devnum = udev_device_get_devnum(dev);
- struct stat stats;
- int err = 0;
-
-
- if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
- mode |= S_IFBLK;
- else
- mode |= S_IFCHR;
-
- if (file == NULL)
- file = udev_device_get_devnode(dev);
-
- if (lstat(file, &stats) == 0) {
- if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
- info(udev, "preserve file '%s', because it has correct dev_t\n", file);
- if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
- /* preserve 'sticky' bit, if already set */
- mode |= stats.st_mode & 01000;
- info(udev, "set permissions %s, %#o, uid=%u, gid=%u\n", file, mode, uid, gid);
- chmod(file, mode);
- chown(file, uid, gid);
- } else {
- info(udev, "preserve permissions %s, %#o, uid=%u, gid=%u\n", file, mode, uid, gid);
- }
- /*
- * Set initial selinux file context only on add events.
- * We set the proper context on bootup (triger) or for newly
- * added devices, but we don't change it later, in case
- * something else has set a custom context in the meantime.
- */
- if (strcmp(udev_device_get_action(dev), "add") == 0)
- udev_selinux_lsetfilecon(udev, file, mode);
- /* always update timestamp when we re-use the node, like on media change events */
- utimensat(AT_FDCWD, file, NULL, 0);
- } else {
- char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
-
- info(udev, "atomically replace existing file '%s'\n", file);
- util_strscpyl(file_tmp, sizeof(file_tmp), file, TMP_FILE_EXT, NULL);
- unlink(file_tmp);
- udev_selinux_setfscreatecon(udev, file_tmp, mode);
- err = mknod(file_tmp, mode, devnum);
- udev_selinux_resetfscreatecon(udev);
- if (err != 0) {
- err(udev, "mknod '%s' %u:%u %#o failed: %m\n",
- file_tmp, major(devnum), minor(devnum), mode);
- goto exit;
- }
- err = rename(file_tmp, file);
- if (err != 0) {
- err(udev, "rename '%s' '%s' failed: %m\n", file_tmp, file);
- unlink(file_tmp);
- goto exit;
- }
- info(udev, "set permissions '%s' %#o uid=%u gid=%u\n", file, mode, uid, gid);
- chmod(file, mode);
- chown(file, uid, gid);
- }
- } else {
- info(udev, "mknod '%s' %u:%u %#o\n", file, major(devnum), minor(devnum), mode);
- do {
- err = util_create_path_selinux(udev, file);
- if (err != 0 && err != -ENOENT)
- break;
- udev_selinux_setfscreatecon(udev, file, mode);
- err = mknod(file, mode, devnum);
- if (err != 0)
- err = -errno;
- udev_selinux_resetfscreatecon(udev);
- } while (err == -ENOENT);
- if (err != 0 && err != -EEXIST)
- err(udev, "mknod '%s' %u:%u %#o' failed: %m\n", file, major(devnum), minor(devnum), mode);
- info(udev, "set permissions '%s' %#o uid=%u gid=%u\n", file, mode, uid, gid);
- chmod(file, mode);
- chown(file, uid, gid);
- }
-exit:
- return err;
-}
-
static int node_symlink(struct udev *udev, const char *node, const char *slink)
{
struct stat stats;
@@ -362,6 +278,55 @@ void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev
}
}
+static int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
+{
+ struct udev *udev = udev_device_get_udev(dev);
+ const char *devnode = udev_device_get_devnode(dev);
+ dev_t devnum = udev_device_get_devnum(dev);
+ struct stat stats;
+ int err = 0;
+
+ if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
+ mode |= S_IFBLK;
+ else
+ mode |= S_IFCHR;
+
+ if (lstat(devnode, &stats) != 0) {
+ err = -errno;
+ info(udev, "can not stat() node '%s' (%m)\n", devnode);
+ goto out;
+ }
+
+ if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
+ err = -EEXIST;
+ info(udev, "found node '%s' with non-matching devnum %s, skip handling\n",
+ udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
+ goto out;
+ }
+
+ if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
+ info(udev, "set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
+ chmod(devnode, mode);
+ chown(devnode, uid, gid);
+ } else {
+ info(udev, "preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
+ }
+
+ /*
+ * Set initial selinux file context only on add events.
+ * We set the proper context on bootup (triger) or for newly
+ * added devices, but we don't change it later, in case
+ * something else has set a custom context in the meantime.
+ */
+ if (strcmp(udev_device_get_action(dev), "add") == 0)
+ udev_selinux_lsetfilecon(udev, devnode, mode);
+
+ /* always update timestamp when we re-use the node, like on media change events */
+ utimensat(AT_FDCWD, devnode, NULL, 0);
+out:
+ return err;
+}
+
int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
{
struct udev *udev = udev_device_get_udev(dev);
@@ -369,15 +334,12 @@ int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
struct udev_list_entry *list_entry;
int err = 0;
- info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
- udev_device_get_devnode(dev),
- major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
- mode, uid, gid);
+ info(udev, "handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
+ udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
- if (udev_node_mknod(dev, NULL, mode, uid, gid) != 0) {
- err = -1;
+ err = node_fixup(dev, mode, uid, gid);
+ if (err < 0)
goto exit;
- }
/* always add /dev/{block,char}/$major:$minor */
snprintf(filename, sizeof(filename), "%s/%s/%u:%u",
@@ -412,38 +374,6 @@ int udev_node_remove(struct udev_device *dev)
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
link_update(dev, udev_list_entry_get_name(list_entry), 0);
- devnode = udev_device_get_devnode(dev);
- if (devnode == NULL)
- goto out;
-
- if (stat(devnode, &stats) == 0) {
- if (stats.st_rdev != udev_device_get_devnum(dev)) {
- info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
- err = -1;
- goto out;
- }
-
- if (stats.st_mode & 01000) {
- info(udev, "device node '%s' has sticky bit set, skip removal\n", devnode);
- goto out;
- }
- } else {
- info(udev, "device node '%s' not found\n", devnode);
- }
-
- dev_check = udev_device_new_from_syspath(udev, udev_device_get_syspath(dev));
- if (dev_check != NULL) {
- /* do not remove device node if the same sys-device is re-created in the meantime */
- info(udev, "keeping device node of existing device'%s'\n", devnode);
- udev_device_unref(dev_check);
- goto out;
- }
-
- info(udev, "removing device node '%s'\n", devnode);
- err = util_unlink_secure(udev, devnode);
- if (err == 0)
- util_delete_path(udev, devnode);
-
/* remove /dev/{block,char}/$major:$minor */
snprintf(filename, sizeof(filename), "%s/%s/%u:%u",
udev_get_dev_path(udev),
diff --git a/udev/udev-rules.c b/udev/udev-rules.c
index 411b9d88eb..7e79545124 100644
--- a/udev/udev-rules.c
+++ b/udev/udev-rules.c
@@ -2504,9 +2504,6 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
&rules->buf[rule->rule.filename_off],
rule->rule.filename_line);
break;
- case TK_A_STATIC_NODE:
- event->static_node = true;
- break;
case TK_A_ENV: {
const char *name = &rules->buf[cur->key.attr_off];
char *value = &rules->buf[cur->key.value_off];
@@ -2729,8 +2726,6 @@ void udev_rules_apply_static_dev_perms(struct udev_rules *rules)
else
mode = 0600;
}
- /* set sticky bit, so we do not remove the node on module unload */
- mode |= 01000;
if (mode != (stats.st_mode & 01777)) {
chmod(filename, mode);
info(rules->udev, "chmod '%s' %#o\n", filename, mode);
diff --git a/udev/udev.h b/udev/udev.h
index 4f2886c69c..56b1652c74 100644
--- a/udev/udev.h
+++ b/udev/udev.h
@@ -32,7 +32,6 @@ struct udev_event {
struct udev_device *dev_parent;
struct udev_device *dev_db;
char *name;
- char *tmp_node;
char *program_result;
mode_t mode;
uid_t uid;
@@ -51,7 +50,6 @@ struct udev_event {
bool owner_final;
bool mode_set;
bool mode_final;
- bool static_node;
bool name_final;
bool devlink_final;
bool run_final;
@@ -91,7 +89,6 @@ void udev_watch_end(struct udev *udev, struct udev_device *dev);
struct udev_device *udev_watch_lookup(struct udev *udev, int wd);
/* udev-node.c */
-int udev_node_mknod(struct udev_device *dev, const char *file, mode_t mode, uid_t uid, gid_t gid);
int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid);
int udev_node_remove(struct udev_device *dev);
void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old);
diff --git a/udev/udev.xml b/udev/udev.xml
index 7b31173b21..fca0dad7bf 100644
--- a/udev/udev.xml
+++ b/udev/udev.xml
@@ -167,9 +167,8 @@
<varlistentry>
<term><option>NAME</option></term>
<listitem>
- <para>Match the name of the node or network interface. It can
- be used once the NAME key has been set in one of the preceding
- rules.</para>
+ <para>Match the name of a network interface. It can be used once the
+ NAME key has been set in one of the preceding rules.</para>
</listitem>
</varlistentry>
@@ -319,17 +318,8 @@
<varlistentry>
<term><option>NAME</option></term>
<listitem>
- <para>What a network interface should be named.</para>
- <para>Also, as a temporary workaround, this is what a device node
- should be named; usually the kernel provides the defined node name or creates
- and removes the node before udev even receives any event. Changing
- the node name from the kernel's default creates inconsistencies
- and is not supported. If the kernel and NAME specify different names,
- an error is logged. udev is only expected to handle device node
- permissions and to create additional symlinks, not to change
- kernel-provided device node names. Instead of renaming a device node,
- SYMLINK should be used. However, symlink names must never conflict with
- device node names, as that would result in unpredictable behavior.</para>
+ <para>The name to use for a network interface. The name of a device node
+ can not be changed by udev, only additional symlinks can be created.</para>
</listitem>
</varlistentry>
@@ -637,7 +627,7 @@
<varlistentry>
<term><option>$name</option></term>
<listitem>
- <para>The current name of the device node. If not changed by a rule, it is the
+ <para>The current name of the device. If not changed by a rule, it is the
name of the kernel device.</para>
</listitem>
</varlistentry>
@@ -665,10 +655,9 @@
</varlistentry>
<varlistentry>
- <term><option>$tempnode</option>, <option>%N</option></term>
+ <term><option>$devnode</option>, <option>%N</option></term>
<listitem>
- <para>The name of a temporary device node created to provide access to
- the device from a external program before the real node is created.</para>
+ <para>The name of the device node.</para>
</listitem>
</varlistentry>
diff --git a/udev/udevd.c b/udev/udevd.c
index 63b4fd7ad2..6b561977dc 100644
--- a/udev/udevd.c
+++ b/udev/udevd.c
@@ -863,11 +863,10 @@ static void static_dev_create_from_modules(struct udev *udev)
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;