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