chiark / gitweb /
738d02fa2525d50b040d3b8e6e5178e81b44f893
[elogind.git] / src / udev / udev-node.c
1 /*
2  * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
8  *
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/>.
16  */
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdbool.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <grp.h>
27 #include <dirent.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31
32 #include "udev.h"
33
34 #define TMP_FILE_EXT                ".udev-tmp"
35
36 static int node_symlink(struct udev *udev, const char *node, const char *slink)
37 {
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                         log_error("conflicting device node '%s' found, link to '%s' will not be created\n", slink, node);
71                         goto exit;
72                 } else if (S_ISLNK(stats.st_mode)) {
73                         char buf[UTIL_PATH_SIZE];
74                         int len;
75
76                         len = readlink(slink, buf, sizeof(buf));
77                         if (len > 0 && len < (int)sizeof(buf)) {
78                                 buf[len] = '\0';
79                                 if (strcmp(target, buf) == 0) {
80                                         log_debug("preserve already existing symlink '%s' to '%s'\n", slink, target);
81                                         label_fix(slink, true, false);
82                                         utimensat(AT_FDCWD, slink, NULL, AT_SYMLINK_NOFOLLOW);
83                                         goto exit;
84                                 }
85                         }
86                 }
87         } else {
88                 log_debug("creating symlink '%s' to '%s'\n", slink, target);
89                 do {
90                         err = mkdir_parents_label(slink, 0755);
91                         if (err != 0 && err != -ENOENT)
92                                 break;
93                         label_context_set(slink, S_IFLNK);
94                         err = symlink(target, slink);
95                         if (err != 0)
96                                 err = -errno;
97                         label_context_clear();
98                 } while (err == -ENOENT);
99                 if (err == 0)
100                         goto exit;
101         }
102
103         log_debug("atomically replace '%s'\n", slink);
104         util_strscpyl(slink_tmp, sizeof(slink_tmp), slink, TMP_FILE_EXT, NULL);
105         unlink(slink_tmp);
106         do {
107                 err = mkdir_parents_label(slink_tmp, 0755);
108                 if (err != 0 && err != -ENOENT)
109                         break;
110                 label_context_set(slink_tmp, S_IFLNK);
111                 err = symlink(target, slink_tmp);
112                 if (err != 0)
113                         err = -errno;
114                 label_context_clear();
115         } while (err == -ENOENT);
116         if (err != 0) {
117                 log_error("symlink '%s' '%s' failed: %m\n", target, slink_tmp);
118                 goto exit;
119         }
120         err = rename(slink_tmp, slink);
121         if (err != 0) {
122                 log_error("rename '%s' '%s' failed: %m\n", slink_tmp, slink);
123                 unlink(slink_tmp);
124         }
125 exit:
126         return err;
127 }
128
129 /* find device node of device with highest priority */
130 static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize)
131 {
132         struct udev *udev = udev_device_get_udev(dev);
133         DIR *dir;
134         int priority = 0;
135         const char *target = NULL;
136
137         if (add) {
138                 priority = udev_device_get_devlink_priority(dev);
139                 util_strscpy(buf, bufsize, udev_device_get_devnode(dev));
140                 target = buf;
141         }
142
143         dir = opendir(stackdir);
144         if (dir == NULL)
145                 return target;
146         for (;;) {
147                 struct udev_device *dev_db;
148                 struct dirent *dent;
149
150                 dent = readdir(dir);
151                 if (dent == NULL || dent->d_name[0] == '\0')
152                         break;
153                 if (dent->d_name[0] == '.')
154                         continue;
155
156                 log_debug("found '%s' claiming '%s'\n", dent->d_name, stackdir);
157
158                 /* did we find ourself? */
159                 if (strcmp(dent->d_name, udev_device_get_id_filename(dev)) == 0)
160                         continue;
161
162                 dev_db = udev_device_new_from_id_filename(udev, dent->d_name);
163                 if (dev_db != NULL) {
164                         const char *devnode;
165
166                         devnode = udev_device_get_devnode(dev_db);
167                         if (devnode != NULL) {
168                                 if (target == NULL || udev_device_get_devlink_priority(dev_db) > priority) {
169                                         log_debug("'%s' claims priority %i for '%s'\n",
170                                                   udev_device_get_syspath(dev_db), udev_device_get_devlink_priority(dev_db), stackdir);
171                                         priority = udev_device_get_devlink_priority(dev_db);
172                                         util_strscpy(buf, bufsize, devnode);
173                                         target = buf;
174                                 }
175                         }
176                         udev_device_unref(dev_db);
177                 }
178         }
179         closedir(dir);
180         return target;
181 }
182
183 /* manage "stack of names" with possibly specified device priorities */
184 static void link_update(struct udev_device *dev, const char *slink, bool add)
185 {
186         struct udev *udev = udev_device_get_udev(dev);
187         char name_enc[UTIL_PATH_SIZE];
188         char filename[UTIL_PATH_SIZE * 2];
189         char dirname[UTIL_PATH_SIZE];
190         const char *target;
191         char buf[UTIL_PATH_SIZE];
192
193         util_path_encode(slink + strlen("/dev"), name_enc, sizeof(name_enc));
194         util_strscpyl(dirname, sizeof(dirname), "/run/udev/links/", name_enc, NULL);
195         util_strscpyl(filename, sizeof(filename), dirname, "/", udev_device_get_id_filename(dev), NULL);
196
197         if (!add && unlink(filename) == 0)
198                 rmdir(dirname);
199
200         target = link_find_prioritized(dev, add, dirname, buf, sizeof(buf));
201         if (target == NULL) {
202                 log_debug("no reference left, remove '%s'\n", slink);
203                 if (unlink(slink) == 0)
204                         util_delete_path(udev, slink);
205         } else {
206                 log_debug("creating link '%s' to '%s'\n", slink, target);
207                 node_symlink(udev, target, slink);
208         }
209
210         if (add) {
211                 int err;
212
213                 do {
214                         int fd;
215
216                         err = mkdir_parents(filename, 0755);
217                         if (err != 0 && err != -ENOENT)
218                                 break;
219                         fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
220                         if (fd >= 0)
221                                 close(fd);
222                         else
223                                 err = -errno;
224                 } while (err == -ENOENT);
225         }
226 }
227
228 void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
229 {
230         struct udev_list_entry *list_entry;
231
232         /* update possible left-over symlinks */
233         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
234                 const char *name = udev_list_entry_get_name(list_entry);
235                 struct udev_list_entry *list_entry_current;
236                 int found;
237
238                 /* check if old link name still belongs to this device */
239                 found = 0;
240                 udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
241                         const char *name_current = udev_list_entry_get_name(list_entry_current);
242
243                         if (strcmp(name, name_current) == 0) {
244                                 found = 1;
245                                 break;
246                         }
247                 }
248                 if (found)
249                         continue;
250
251                 log_debug("update old name, '%s' no longer belonging to '%s'\n",
252                      name, udev_device_get_devpath(dev));
253                 link_update(dev, name, 0);
254         }
255 }
256
257 static int node_fixup(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
258 {
259         const char *devnode = udev_device_get_devnode(dev);
260         dev_t devnum = udev_device_get_devnum(dev);
261         struct stat stats;
262         int err = 0;
263
264         if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
265                 mode |= S_IFBLK;
266         else
267                 mode |= S_IFCHR;
268
269         if (lstat(devnode, &stats) != 0) {
270                 err = -errno;
271                 log_debug("can not stat() node '%s' (%m)\n", devnode);
272                 goto out;
273         }
274
275         if (((stats.st_mode & S_IFMT) != (mode & S_IFMT)) || (stats.st_rdev != devnum)) {
276                 err = -EEXIST;
277                 log_debug("found node '%s' with non-matching devnum %s, skip handling\n",
278                           udev_device_get_devnode(dev), udev_device_get_id_filename(dev));
279                 goto out;
280         }
281
282         if ((stats.st_mode & 0777) != (mode & 0777) || stats.st_uid != uid || stats.st_gid != gid) {
283                 log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
284                 chmod(devnode, mode);
285                 chown(devnode, uid, gid);
286         } else {
287                 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode, mode, uid, gid);
288         }
289
290         /*
291          * Set initial selinux file context only on add events.
292          * We set the proper context on bootup (triger) or for newly
293          * added devices, but we don't change it later, in case
294          * something else has set a custom context in the meantime.
295          */
296         if (strcmp(udev_device_get_action(dev), "add") == 0)
297             label_fix(devnode, true, false);
298
299         /* always update timestamp when we re-use the node, like on media change events */
300         utimensat(AT_FDCWD, devnode, NULL, 0);
301 out:
302         return err;
303 }
304
305 void udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
306 {
307         struct udev *udev = udev_device_get_udev(dev);
308         char filename[UTIL_PATH_SIZE];
309         struct udev_list_entry *list_entry;
310
311         log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
312                   udev_device_get_devnode(dev), udev_device_get_id_filename(dev), mode, uid, gid);
313
314         if (node_fixup(dev, mode, uid, gid) < 0)
315                 return;
316
317         /* always add /dev/{block,char}/$major:$minor */
318         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
319                  strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char",
320                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
321         node_symlink(udev, udev_device_get_devnode(dev), filename);
322
323         /* create/update symlinks, add symlinks to name index */
324         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
325                 if (udev_list_entry_get_num(list_entry))
326                         /* simple unmanaged link name */
327                         node_symlink(udev, udev_device_get_devnode(dev), udev_list_entry_get_name(list_entry));
328                 else
329                         link_update(dev, udev_list_entry_get_name(list_entry), 1);
330         }
331 }
332
333 void udev_node_remove(struct udev_device *dev)
334 {
335         struct udev_list_entry *list_entry;
336         char filename[UTIL_PATH_SIZE];
337
338         /* remove/update symlinks, remove symlinks from name index */
339         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev))
340                 link_update(dev, udev_list_entry_get_name(list_entry), 0);
341
342         /* remove /dev/{block,char}/$major:$minor */
343         snprintf(filename, sizeof(filename), "/dev/%s/%u:%u",
344                  strcmp(udev_device_get_subsystem(dev), "block") == 0 ? "block" : "char",
345                  major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)));
346         unlink(filename);
347 }