]>
Commit | Line | Data |
---|---|---|
ea733a2f | 1 | /* |
0ec5b5e1 | 2 | * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@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" | |
9825617b | 33 | |
912541b0 | 34 | #define TMP_FILE_EXT ".udev-tmp" |
ea733a2f | 35 | |
aa8734ff | 36 | static int node_symlink(struct udev *udev, const char *node, const char *slink) |
fa33d857 | 37 | { |
912541b0 KS |
38 | struct stat stats; |
39 | char target[UTIL_PATH_SIZE]; | |
40 | char *s; | |
41 | size_t l; | |
42 | char slink_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)]; | |
43 | int i = 0; | |
44 | int tail = 0; | |
45 | int err = 0; | |
46 | ||
47 | /* use relative link */ | |
48 | target[0] = '\0'; | |
49 | while (node[i] && (node[i] == slink[i])) { | |
50 | if (node[i] == '/') | |
51 | tail = i+1; | |
52 | i++; | |
53 | } | |
54 | s = target; | |
55 | l = sizeof(target); | |
56 | while (slink[i] != '\0') { | |
57 | if (slink[i] == '/') | |
58 | l = util_strpcpy(&s, l, "../"); | |
59 | i++; | |
60 | } | |
61 | l = util_strscpy(s, l, &node[tail]); | |
62 | if (l == 0) { | |
63 | err = -EINVAL; | |
64 | goto exit; | |
65 | } | |
66 | ||
67 | /* preserve link with correct target, do not replace node of other device */ | |
68 | if (lstat(slink, &stats) == 0) { | |
69 | if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) { | |
70 | struct stat stats2; | |
71 | ||
72 | info(udev, "found existing node instead of symlink '%s'\n", slink); | |
73 | if (lstat(node, &stats2) == 0) { | |
74 | if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) && | |
75 | stats.st_rdev == stats2.st_rdev && stats.st_ino != stats2.st_ino) { | |
76 | info(udev, "replace device node '%s' with symlink to our node '%s'\n", | |
77 | slink, node); | |
78 | } else { | |
79 | err(udev, "device node '%s' already exists, " | |
80 | "link to '%s' will not overwrite it\n", | |
81 | slink, node); | |
82 | goto exit; | |
83 | } | |
84 | } | |
85 | } else if (S_ISLNK(stats.st_mode)) { | |
86 | char buf[UTIL_PATH_SIZE]; | |
87 | int len; | |
88 | ||
89 | dbg(udev, "found existing symlink '%s'\n", slink); | |
90 | len = readlink(slink, buf, sizeof(buf)); | |
91 | if (len > 0 && len < (int)sizeof(buf)) { | |
92 | buf[len] = '\0'; | |
93 | if (strcmp(target, buf) == 0) { | |
94 | info(udev, "preserve already existing symlink '%s' to '%s'\n", | |
95 | slink, target); | |
96 | udev_selinux_lsetfilecon(udev, slink, S_IFLNK); | |
97 | utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW); | |
98 | goto exit; | |
99 | } | |
100 | } | |
101 | } | |
102 | } else { | |
103 | info(udev, "creating symlink '%s' to '%s'\n", slink, target); | |
104 | do { | |
105 | err = util_create_path_selinux(udev, slink); | |
106 | if (err != 0 && err != -ENOENT) | |
107 | break; | |
108 | udev_selinux_setfscreatecon(udev, slink, S_IFLNK); | |
109 | err = symlink(target, slink); | |
110 | if (err != 0) | |
111 | err = -errno; | |
112 | udev_selinux_resetfscreatecon(udev); | |
113 | } while (err == -ENOENT); | |
114 | if (err == 0) | |
115 | goto exit; | |
116 | } | |
117 | ||
118 | info(udev, "atomically replace '%s'\n", slink); | |
119 | util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL); | |
120 | unlink(slink_tmp); | |
121 | do { | |
122 | err = util_create_path_selinux(udev, slink_tmp); | |
123 | if (err != 0 && err != -ENOENT) | |
124 | break; | |
125 | udev_selinux_setfscreatecon(udev, slink_tmp, S_IFLNK); | |
126 | err = symlink(target, slink_tmp); | |
127 | if (err != 0) | |
128 | err = -errno; | |
129 | udev_selinux_resetfscreatecon(udev); | |
130 | } while (err == -ENOENT); | |
131 | if (err != 0) { | |
132 | err(udev, "symlink '%s' '%s' failed: %m\n", target, slink_tmp); | |
133 | goto exit; | |
134 | } | |
135 | err = rename(slink_tmp, slink); | |
136 | if (err != 0) { | |
137 | err(udev, "rename '%s' '%s' failed: %m\n", slink_tmp, slink); | |
138 | unlink(slink_tmp); | |
139 | } | |
fa33d857 | 140 | exit: |
912541b0 | 141 | return err; |
fa33d857 KS |
142 | } |
143 | ||
6c29f2b9 KS |
144 | /* find device node of device with highest priority */ |
145 | static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) | |
24f0605c | 146 | { |
912541b0 KS |
147 | struct udev *udev = udev_device_get_udev(dev); |
148 | DIR *dir; | |
149 | int priority = 0; | |
150 | const char *target = NULL; | |
151 | ||
152 | if (add) { | |
153 | priority = udev_device_get_devlink_priority(dev); | |
154 | util_strscpy(buf, bufsize, udev_device_get_devnode(dev)); | |
155 | target = buf; | |
156 | } | |
157 | ||
158 | dir = opendir(stackdir); | |
159 | if (dir == NULL) | |
160 | return target; | |
161 | for (;;) { | |
162 | struct udev_device *dev_db; | |
163 | struct dirent *dent; | |
164 | ||
165 | dent = readdir(dir); | |
166 | if (dent == NULL || dent->d_name[0] == '\0') | |
167 | break; | |
168 | if (dent->d_name[0] == '.') | |
169 | continue; | |
170 | ||
171 | info(udev, "found '%s' claiming '%s'\n", dent->d_name, stackdir); | |
172 | ||
173 | /* did we find ourself? */ | |
174 | if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0) | |
175 | continue; | |
176 | ||
177 | dev_db = udev_device_new_from_id_filename(udev, dent->d_name); | |
178 | if (dev_db != NULL) { | |
179 | const char *devnode; | |
180 | ||
181 | devnode = udev_device_get_devnode(dev_db); | |
182 | if (devnode != NULL) { | |
183 | dbg(udev, "compare priority of '%s'(%i) > '%s'(%i)\n", target, priority, | |
184 | udev_device_get_devnode(dev_db), udev_device_get_devlink_priority(dev_db)); | |
185 | if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) { | |
186 | info(udev, "'%s' claims priority %i for '%s'\n", | |
187 | udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir); | |
188 | priority = udev_device_get_devlink_priority(dev_db); | |
189 | util_strscpy(buf, bufsize, devnode); | |
190 | target = buf; | |
191 | } | |
192 | } | |
193 | udev_device_unref(dev_db); | |
194 | } | |
195 | } | |
196 | closedir(dir); | |
197 | return target; | |
aa8734ff KS |
198 | } |
199 | ||
6c29f2b9 KS |
200 | /* manage "stack of names" with possibly specified device priorities */ |
201 | static void link_update(struct udev_device *dev, const char *slink, bool add) | |
aa8734ff | 202 | { |
912541b0 KS |
203 | struct udev *udev = udev_device_get_udev(dev); |
204 | char name_enc[UTIL_PATH_SIZE]; | |
205 | char filename[UTIL_PATH_SIZE * 2]; | |
206 | char dirname[UTIL_PATH_SIZE]; | |
207 | const char *target; | |
208 | char buf[UTIL_PATH_SIZE]; | |
209 | ||
210 | dbg(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev)); | |
211 | ||
212 | util_path_encode(&slink[strlen(udev_get_dev_path(udev))+1], name_enc, sizeof(name_enc)); | |
213 | util_strscpyl(dirname, sizeof(dirname), udev_get_run_path(udev), "/links/", name_enc, NULL); | |
214 | util_strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL); | |
215 | ||
216 | if (!add) { | |
217 | dbg(udev, "removing index: '%s'\n", filename); | |
218 | if (unlink(filename) == 0) | |
219 | rmdir(dirname); | |
220 | } | |
221 | ||
222 | target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf)); | |
223 | if (target == NULL) { | |
224 | info(udev, "no reference left, remove '%s'\n", slink); | |
225 | if (unlink(slink) == 0) | |
226 | util_delete_path(udev, slink); | |
227 | } else { | |
228 | info(udev, "creating link '%s' to '%s'\n", slink, target); | |
229 | node_symlink(udev, target, slink); | |
230 | } | |
231 | ||
232 | if (add) { | |
233 | int err; | |
234 | ||
235 | dbg(udev, "creating index: '%s'\n", filename); | |
236 | do { | |
237 | int fd; | |
238 | ||
239 | err = util_create_path(udev, filename); | |
240 | if (err != 0 && err != -ENOENT) | |
241 | break; | |
242 | fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); | |
243 | if (fd >= 0) | |
244 | close(fd); | |
245 | else | |
246 | err = -errno; | |
247 | } while (err == -ENOENT); | |
248 | } | |
24f0605c KS |
249 | } |
250 | ||
ec2dd02e | 251 | void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old) |
24f0605c | 252 | { |
912541b0 KS |
253 | struct udev *udev = udev_device_get_udev(dev); |
254 | struct udev_list_entry *list_entry; | |
255 | ||
256 | /* update possible left-over symlinks */ | |
257 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) { | |
258 | const char *name = udev_list_entry_get_name(list_entry); | |
259 | struct udev_list_entry *list_entry_current; | |
260 | int found; | |
261 | ||
262 | /* check if old link name still belongs to this device */ | |
263 | found = 0; | |
264 | udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) { | |
265 | const char *name_current = udev_list_entry_get_name(list_entry_current); | |
266 | ||
267 | if (strcmp(name, name_current) == 0) { | |
268 | found = 1; | |
269 | break; | |
270 | } | |
271 | } | |
272 | if (found) | |
273 | continue; | |
274 | ||
275 | info(udev, "update old name, '%s' no longer belonging to '%s'\n", | |
276 | name, udev_device_get_devpath(dev)); | |
277 | link_update(dev, name, 0); | |
278 | } | |
24f0605c KS |
279 | } |
280 | ||
220893b3 KS |
281 | static int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid) |
282 | { | |
912541b0 KS |
283 | struct udev *udev = udev_device_get_udev(dev); |
284 | const char *devnode = udev_device_get_devnode(dev); | |
285 | dev_t devnum = udev_device_get_devnum(dev); | |
286 | struct stat stats; | |
287 | int err = 0; | |
288 | ||
289 | if (strcmp(udev_device_get_subsystem(dev), "block") == 0) | |
290 | mode |= S_IFBLK; | |
291 | else | |
292 | mode |= S_IFCHR; | |
293 | ||
294 | if (lstat(devnode, &stats) != 0) { | |
295 | err = -errno; | |
296 | info(udev, "can not stat() node '%s' (%m)\n", devnode); | |
297 | goto out; | |
298 | } | |
299 | ||
300 | if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) { | |
301 | err = -EEXIST; | |
302 | info(udev, "found node '%s' with non-matching devnum %s, skip handling\n", | |
303 | udev_device_get_devnode(dev), udev_device_get_id_filename(dev)); | |
304 | goto out; | |
305 | } | |
306 | ||
307 | if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) { | |
308 | info(udev, "set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid); | |
309 | chmod(devnode, mode); | |
310 | chown(devnode, uid, gid); | |
311 | } else { | |
312 | info(udev, "preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid); | |
313 | } | |
314 | ||
315 | /* | |
316 | * Set initial selinux file context only on add events. | |
317 | * We set the proper context on bootup (triger) or for newly | |
318 | * added devices, but we don't change it later, in case | |
319 | * something else has set a custom context in the meantime. | |
320 | */ | |
321 | if (strcmp(udev_device_get_action(dev), "add") == 0) | |
322 | udev_selinux_lsetfilecon(udev, devnode, mode); | |
323 | ||
324 | /* always update timestamp when we re-use the node, like on media change events */ | |
325 | utimensat(AT_FDCWD, devnode, NULL, 0); | |
220893b3 | 326 | out: |
912541b0 | 327 | return err; |
220893b3 KS |
328 | } |
329 | ||
e7f32890 | 330 | void udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid) |
97853b4f | 331 | { |
912541b0 KS |
332 | struct udev *udev = udev_device_get_udev(dev); |
333 | char filename[UTIL_PATH_SIZE]; | |
334 | struct udev_list_entry *list_entry; | |
335 | int err = 0; | |
336 | ||
337 | info(udev, "handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n", | |
338 | udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid); | |
339 | ||
e7f32890 KS |
340 | if (node_fixup(dev, mode, uid, gid) < 0) |
341 | return; | |
912541b0 KS |
342 | |
343 | /* always add /dev/{block,char}/$major:$minor */ | |
344 | snprintf(filename, sizeof(filename), "%s/%s/%u:%u", | |
345 | udev_get_dev_path(udev), | |
346 | strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char", | |
347 | major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); | |
348 | node_symlink(udev, udev_device_get_devnode(dev), filename); | |
349 | ||
350 | /* create/update symlinks, add symlinks to name index */ | |
351 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) { | |
352 | if (udev_list_entry_get_num(list_entry)) | |
353 | /* simple unmanaged link name */ | |
354 | node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry)); | |
355 | else | |
356 | link_update(dev, udev_list_entry_get_name(list_entry), 1); | |
357 | } | |
ea733a2f GKH |
358 | } |
359 | ||
e7f32890 | 360 | void udev_node_remove(struct udev_device *dev) |
ff9a488d | 361 | { |
912541b0 KS |
362 | struct udev *udev = udev_device_get_udev(dev); |
363 | struct udev_list_entry *list_entry; | |
364 | const char *devnode; | |
365 | struct stat stats; | |
366 | struct udev_device *dev_check; | |
367 | char filename[UTIL_PATH_SIZE]; | |
912541b0 KS |
368 | |
369 | /* remove/update symlinks, remove symlinks from name index */ | |
370 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) | |
371 | link_update(dev, udev_list_entry_get_name(list_entry), 0); | |
372 | ||
373 | /* remove /dev/{block,char}/$major:$minor */ | |
374 | snprintf(filename, sizeof(filename), "%s/%s/%u:%u", | |
375 | udev_get_dev_path(udev), | |
376 | strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char", | |
377 | major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); | |
378 | unlink(filename); | |
ea733a2f | 379 | } |