]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-node.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / udev / udev-node.c
CommitLineData
ea733a2f 1/*
22582bb2 2 * Copyright (C) 2003-2013 Kay Sievers <kay@vrfy.org>
ea733a2f 3 *
55e9959b
KS
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
ea733a2f 8 *
55e9959b
KS
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ea733a2f
GKH
16 */
17
24f0605c 18#include <dirent.h>
07630cea
LP
19#include <errno.h>
20#include <fcntl.h>
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdio.h>
24#include <string.h>
32ff5bca 25#include <sys/stat.h>
07630cea 26#include <unistd.h>
ea733a2f 27
6482f626 28#include "formats-util.h"
07630cea
LP
29#include "selinux-util.h"
30#include "smack-util.h"
31#include "string-util.h"
32#include "udev.h"
9825617b 33
9ec6e95b 34static int node_symlink(struct udev_device *dev, const char *node, const char *slink) {
912541b0
KS
35 struct stat stats;
36 char target[UTIL_PATH_SIZE];
37 char *s;
38 size_t l;
38d70045 39 char slink_tmp[UTIL_PATH_SIZE + 32];
912541b0
KS
40 int i = 0;
41 int tail = 0;
42 int err = 0;
43
44 /* use relative link */
45 target[0] = '\0';
46 while (node[i] && (node[i] == slink[i])) {
47 if (node[i] == '/')
48 tail = i+1;
49 i++;
50 }
51 s = target;
52 l = sizeof(target);
53 while (slink[i] != '\0') {
54 if (slink[i] == '/')
d5a89d7d 55 l = strpcpy(&s, l, "../");
912541b0
KS
56 i++;
57 }
d5a89d7d 58 l = strscpy(s, l, &node[tail]);
912541b0
KS
59 if (l == 0) {
60 err = -EINVAL;
61 goto exit;
62 }
63
64 /* preserve link with correct target, do not replace node of other device */
65 if (lstat(slink, &stats) == 0) {
66 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
9f6445e3 67 log_error("conflicting device node '%s' found, link to '%s' will not be created", slink, node);
2be7287b 68 goto exit;
912541b0
KS
69 } else if (S_ISLNK(stats.st_mode)) {
70 char buf[UTIL_PATH_SIZE];
71 int len;
72
912541b0
KS
73 len = readlink(slink, buf, sizeof(buf));
74 if (len > 0 && len < (int)sizeof(buf)) {
75 buf[len] = '\0';
090be865 76 if (streq(target, buf)) {
9f6445e3 77 log_debug("preserve already existing symlink '%s' to '%s'", slink, target);
c9bc0764 78 label_fix(slink, true, false);
912541b0
KS
79 utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
80 goto exit;
81 }
82 }
83 }
84 } else {
9f6445e3 85 log_debug("creating symlink '%s' to '%s'", slink, target);
912541b0 86 do {
d2e54fae 87 err = mkdir_parents_label(slink, 0755);
912541b0
KS
88 if (err != 0 && err != -ENOENT)
89 break;
ecabcf8b 90 mac_selinux_create_file_prepare(slink, S_IFLNK);
912541b0
KS
91 err = symlink(target, slink);
92 if (err != 0)
93 err = -errno;
ecabcf8b 94 mac_selinux_create_file_clear();
912541b0
KS
95 } while (err == -ENOENT);
96 if (err == 0)
97 goto exit;
98 }
99
9f6445e3 100 log_debug("atomically replace '%s'", slink);
38d70045 101 strscpyl(slink_tmp, sizeof(slink_tmp), slink, ".tmp-", udev_device_get_id_filename(dev), NULL);
912541b0
KS
102 unlink(slink_tmp);
103 do {
d2e54fae 104 err = mkdir_parents_label(slink_tmp, 0755);
912541b0
KS
105 if (err != 0 && err != -ENOENT)
106 break;
ecabcf8b 107 mac_selinux_create_file_prepare(slink_tmp, S_IFLNK);
912541b0
KS
108 err = symlink(target, slink_tmp);
109 if (err != 0)
110 err = -errno;
ecabcf8b 111 mac_selinux_create_file_clear();
912541b0
KS
112 } while (err == -ENOENT);
113 if (err != 0) {
56f64d95 114 log_error_errno(errno, "symlink '%s' '%s' failed: %m", target, slink_tmp);
912541b0
KS
115 goto exit;
116 }
117 err = rename(slink_tmp, slink);
118 if (err != 0) {
56f64d95 119 log_error_errno(errno, "rename '%s' '%s' failed: %m", slink_tmp, slink);
912541b0
KS
120 unlink(slink_tmp);
121 }
fa33d857 122exit:
912541b0 123 return err;
fa33d857
KS
124}
125
6c29f2b9 126/* find device node of device with highest priority */
9ec6e95b 127static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
912541b0
KS
128 struct udev *udev = udev_device_get_udev(dev);
129 DIR *dir;
130 int priority = 0;
131 const char *target = NULL;
132
133 if (add) {
134 priority = udev_device_get_devlink_priority(dev);
d5a89d7d 135 strscpy(buf, bufsize, udev_device_get_devnode(dev));
912541b0
KS
136 target = buf;
137 }
138
139 dir = opendir(stackdir);
140 if (dir == NULL)
141 return target;
142 for (;;) {
143 struct udev_device *dev_db;
144 struct dirent *dent;
145
146 dent = readdir(dir);
147 if (dent == NULL || dent->d_name[0] == '\0')
148 break;
149 if (dent->d_name[0] == '.')
150 continue;
151
9f6445e3 152 log_debug("found '%s' claiming '%s'", dent->d_name, stackdir);
912541b0
KS
153
154 /* did we find ourself? */
090be865 155 if (streq(dent->d_name, udev_device_get_id_filename(dev)))
912541b0
KS
156 continue;
157
dbf61afb 158 dev_db = udev_device_new_from_device_id(udev, dent->d_name);
912541b0
KS
159 if (dev_db != NULL) {
160 const char *devnode;
161
162 devnode = udev_device_get_devnode(dev_db);
163 if (devnode != NULL) {
912541b0 164 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
9f6445e3 165 log_debug("'%s' claims priority %i for '%s'",
baa30fbc 166 udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
912541b0 167 priority = udev_device_get_devlink_priority(dev_db);
d5a89d7d 168 strscpy(buf, bufsize, devnode);
912541b0
KS
169 target = buf;
170 }
171 }
172 udev_device_unref(dev_db);
173 }
174 }
175 closedir(dir);
176 return target;
aa8734ff
KS
177}
178
6c29f2b9 179/* manage "stack of names" with possibly specified device priorities */
9ec6e95b 180static void link_update(struct udev_device *dev, const char *slink, bool add) {
912541b0
KS
181 char name_enc[UTIL_PATH_SIZE];
182 char filename[UTIL_PATH_SIZE * 2];
183 char dirname[UTIL_PATH_SIZE];
184 const char *target;
185 char buf[UTIL_PATH_SIZE];
186
4cb72937 187 util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
d5a89d7d
KS
188 strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
189 strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
912541b0 190
baa30fbc
KS
191 if (!add && unlink(filename) == 0)
192 rmdir(dirname);
912541b0
KS
193
194 target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
195 if (target == NULL) {
9f6445e3 196 log_debug("no reference left, remove '%s'", slink);
912541b0 197 if (unlink(slink) == 0)
37d52274 198 rmdir_parents(slink, "/");
912541b0 199 } else {
9f6445e3 200 log_debug("creating link '%s' to '%s'", slink, target);
38d70045 201 node_symlink(dev, target, slink);
912541b0
KS
202 }
203
204 if (add) {
205 int err;
206
912541b0
KS
207 do {
208 int fd;
209
3cbd5f6b 210 err = mkdir_parents(filename, 0755);
912541b0
KS
211 if (err != 0 && err != -ENOENT)
212 break;
213 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
214 if (fd >= 0)
215 close(fd);
216 else
217 err = -errno;
218 } while (err == -ENOENT);
219 }
24f0605c
KS
220}
221
9ec6e95b 222void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) {
912541b0
KS
223 struct udev_list_entry *list_entry;
224
225 /* update possible left-over symlinks */
226 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
227 const char *name = udev_list_entry_get_name(list_entry);
228 struct udev_list_entry *list_entry_current;
229 int found;
230
231 /* check if old link name still belongs to this device */
232 found = 0;
233 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
234 const char *name_current = udev_list_entry_get_name(list_entry_current);
235
090be865 236 if (streq(name, name_current)) {
912541b0
KS
237 found = 1;
238 break;
239 }
240 }
241 if (found)
242 continue;
243
9f6445e3 244 log_debug("update old name, '%s' no longer belonging to '%s'",
912541b0 245 name, udev_device_get_devpath(dev));
8a173387 246 link_update(dev, name, false);
912541b0 247 }
24f0605c
KS
248}
249
c26547d6
KS
250static int node_permissions_apply(struct udev_device *dev, bool apply,
251 mode_t mode, uid_t uid, gid_t gid,
252 struct udev_list *seclabel_list) {
912541b0
KS
253 const char *devnode = udev_device_get_devnode(dev);
254 dev_t devnum = udev_device_get_devnum(dev);
255 struct stat stats;
c26547d6 256 struct udev_list_entry *entry;
912541b0
KS
257 int err = 0;
258
090be865 259 if (streq(udev_device_get_subsystem(dev), "block"))
912541b0
KS
260 mode |= S_IFBLK;
261 else
262 mode |= S_IFCHR;
263
264 if (lstat(devnode, &stats) != 0) {
265 err = -errno;
56f64d95 266 log_debug_errno(errno, "can not stat() node '%s' (%m)", devnode);
912541b0
KS
267 goto out;
268 }
269
270 if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
271 err = -EEXIST;
9f6445e3 272 log_debug("found node '%s' with non-matching devnum %s, skip handling",
baa30fbc 273 udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
912541b0
KS
274 goto out;
275 }
276
22582bb2 277 if (apply) {
b7e2b764 278 bool selinux = false;
b7e2b764 279 bool smack = false;
b7e2b764 280
48a849ee 281 if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
9f6445e3 282 log_debug("set permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
543afdc6
TG
283 err = chmod(devnode, mode);
284 if (err < 0)
56f64d95 285 log_warning_errno(errno, "setting mode of %s to %#o failed: %m", devnode, mode);
543afdc6
TG
286 err = chown(devnode, uid, gid);
287 if (err < 0)
56f64d95 288 log_warning_errno(errno, "setting owner of %s to uid=%u, gid=%u failed: %m", devnode, uid, gid);
48a849ee 289 } else {
9f6445e3 290 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u", devnode, mode, uid, gid);
48a849ee 291 }
c26547d6 292
c26547d6
KS
293 /* apply SECLABEL{$module}=$label */
294 udev_list_entry_foreach(entry, udev_list_get_entry(seclabel_list)) {
295 const char *name, *label;
d53e386d 296 int r;
c26547d6
KS
297
298 name = udev_list_entry_get_name(entry);
299 label = udev_list_entry_get_value(entry);
300
301 if (streq(name, "selinux")) {
b7e2b764 302 selinux = true;
d53e386d 303
ecabcf8b
LP
304 r = mac_selinux_apply(devnode, label);
305 if (r < 0)
da927ba9 306 log_error_errno(r, "SECLABEL: failed to set SELinux label '%s': %m", label);
463b5dbb
KS
307 else
308 log_debug("SECLABEL: set SELinux label '%s'", label);
c26547d6 309
9a4e038c 310 } else if (streq(name, "smack")) {
b7e2b764 311 smack = true;
d53e386d 312
5ab58c20 313 r = mac_smack_apply(devnode, SMACK_ATTR_ACCESS, label);
d53e386d 314 if (r < 0)
da927ba9 315 log_error_errno(r, "SECLABEL: failed to set SMACK label '%s': %m", label);
c26547d6
KS
316 else
317 log_debug("SECLABEL: set SMACK label '%s'", label);
c26547d6
KS
318
319 } else
320 log_error("SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
321 }
b7e2b764
KS
322
323 /* set the defaults */
324 if (!selinux)
2ec62d8e 325 mac_selinux_fix(devnode, true, false);
9a4e038c 326 if (!smack)
5ab58c20 327 mac_smack_apply(devnode, SMACK_ATTR_ACCESS, NULL);
48a849ee 328 }
912541b0
KS
329
330 /* always update timestamp when we re-use the node, like on media change events */
331 utimensat(AT_FDCWD, devnode, NULL, 0);
220893b3 332out:
912541b0 333 return err;
220893b3
KS
334}
335
c26547d6
KS
336void udev_node_add(struct udev_device *dev, bool apply,
337 mode_t mode, uid_t uid, gid_t gid,
338 struct udev_list *seclabel_list) {
912541b0
KS
339 char filename[UTIL_PATH_SIZE];
340 struct udev_list_entry *list_entry;
912541b0 341
1fa2f38f 342 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid="UID_FMT", gid="GID_FMT,
baa30fbc 343 udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
912541b0 344
c26547d6 345 if (node_permissions_apply(dev, apply, mode, uid, gid, seclabel_list) < 0)
e7f32890 346 return;
912541b0
KS
347
348 /* always add /dev/{block,char}/$major:$minor */
4cb72937 349 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
090be865 350 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
912541b0 351 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
38d70045 352 node_symlink(dev, udev_device_get_devnode(dev), filename);
912541b0
KS
353
354 /* create/update symlinks, add symlinks to name index */
8a173387
KS
355 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
356 link_update(dev, udev_list_entry_get_name(list_entry), true);
ea733a2f
GKH
357}
358
9ec6e95b 359void udev_node_remove(struct udev_device *dev) {
912541b0 360 struct udev_list_entry *list_entry;
912541b0 361 char filename[UTIL_PATH_SIZE];
912541b0
KS
362
363 /* remove/update symlinks, remove symlinks from name index */
364 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
8a173387 365 link_update(dev, udev_list_entry_get_name(list_entry), false);
912541b0
KS
366
367 /* remove /dev/{block,char}/$major:$minor */
4cb72937 368 snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
090be865 369 streq(udev_device_get_subsystem(dev), "block") ? "block" : "char",
912541b0
KS
370 major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
371 unlink(filename);
ea733a2f 372}