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