]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-node.c
2 * Copyright (C) 2003-2010 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
30 #include <sys/types.h>
34 #define TMP_FILE_EXT ".udev-tmp"
36 static int node_symlink(struct udev
*udev
, const char *node
, const char *slink
)
39 char target
[UTIL_PATH_SIZE
];
42 char slink_tmp
[UTIL_PATH_SIZE
+ sizeof(TMP_FILE_EXT
)];
47 /* use relative link */
49 while (node
[i
] && (node
[i
] == slink
[i
])) {
56 while (slink
[i
] != '\0') {
58 l
= util_strpcpy(&s
, l
, "../");
61 l
= util_strscpy(s
, l
, &node
[tail
]);
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
);
72 } else if (S_ISLNK(stats
.st_mode
)) {
73 char buf
[UTIL_PATH_SIZE
];
76 len
= readlink(slink
, buf
, sizeof(buf
));
77 if (len
> 0 && len
< (int)sizeof(buf
)) {
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
);
88 log_debug("creating symlink '%s' to '%s'\n", slink
, target
);
90 err
= mkdir_parents_label(slink
, 0755);
91 if (err
!= 0 && err
!= -ENOENT
)
93 label_context_set(slink
, S_IFLNK
);
94 err
= symlink(target
, slink
);
97 label_context_clear();
98 } while (err
== -ENOENT
);
103 log_debug("atomically replace '%s'\n", slink
);
104 util_strscpyl(slink_tmp
, sizeof(slink_tmp
), slink
, TMP_FILE_EXT
, NULL
);
107 err
= mkdir_parents_label(slink_tmp
, 0755);
108 if (err
!= 0 && err
!= -ENOENT
)
110 label_context_set(slink_tmp
, S_IFLNK
);
111 err
= symlink(target
, slink_tmp
);
114 label_context_clear();
115 } while (err
== -ENOENT
);
117 log_error("symlink '%s' '%s' failed: %m\n", target
, slink_tmp
);
120 err
= rename(slink_tmp
, slink
);
122 log_error("rename '%s' '%s' failed: %m\n", slink_tmp
, slink
);
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
)
132 struct udev
*udev
= udev_device_get_udev(dev
);
135 const char *target
= NULL
;
138 priority
= udev_device_get_devlink_priority(dev
);
139 util_strscpy(buf
, bufsize
, udev_device_get_devnode(dev
));
143 dir
= opendir(stackdir
);
147 struct udev_device
*dev_db
;
151 if (dent
== NULL
|| dent
->d_name
[0] == '\0')
153 if (dent
->d_name
[0] == '.')
156 log_debug("found '%s' claiming '%s'\n", dent
->d_name
, stackdir
);
158 /* did we find ourself? */
159 if (strcmp(dent
->d_name
, udev_device_get_id_filename(dev
)) == 0)
162 dev_db
= udev_device_new_from_device_id(udev
, dent
->d_name
);
163 if (dev_db
!= NULL
) {
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
);
176 udev_device_unref(dev_db
);
183 /* manage "stack of names" with possibly specified device priorities */
184 static void link_update(struct udev_device
*dev
, const char *slink
, bool add
)
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
];
191 char buf
[UTIL_PATH_SIZE
];
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
);
197 if (!add
&& unlink(filename
) == 0)
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
);
206 log_debug("creating link '%s' to '%s'\n", slink
, target
);
207 node_symlink(udev
, target
, slink
);
216 err
= mkdir_parents(filename
, 0755);
217 if (err
!= 0 && err
!= -ENOENT
)
219 fd
= open(filename
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_TRUNC
|O_NOFOLLOW
, 0444);
224 } while (err
== -ENOENT
);
228 void udev_node_update_old_links(struct udev_device
*dev
, struct udev_device
*dev_old
)
230 struct udev_list_entry
*list_entry
;
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
;
238 /* check if old link name still belongs to this device */
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
);
243 if (strcmp(name
, name_current
) == 0) {
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
, false);
257 static int node_fixup(struct udev_device
*dev
, mode_t mode
, uid_t uid
, gid_t gid
)
259 const char *devnode
= udev_device_get_devnode(dev
);
260 dev_t devnum
= udev_device_get_devnum(dev
);
264 if (strcmp(udev_device_get_subsystem(dev
), "block") == 0)
269 if (lstat(devnode
, &stats
) != 0) {
271 log_debug("can not stat() node '%s' (%m)\n", devnode
);
275 if (((stats
.st_mode
& S_IFMT
) != (mode
& S_IFMT
)) || (stats
.st_rdev
!= devnum
)) {
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
));
283 * Set permissions and selinux file context only on add events. We always
284 * set it on bootup (coldplug) with "trigger --action=add" for all devices
285 * and for any newly added devices (hotplug). We don't want to change it
286 * later, in case something else has applied custom settings in the meantime.
288 if (strcmp(udev_device_get_action(dev
), "add") == 0) {
289 if ((stats
.st_mode
& 0777) != (mode
& 0777) || stats
.st_uid
!= uid
|| stats
.st_gid
!= gid
) {
290 log_debug("set permissions %s, %#o, uid=%u, gid=%u\n", devnode
, mode
, uid
, gid
);
291 chmod(devnode
, mode
);
292 chown(devnode
, uid
, gid
);
294 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode
, mode
, uid
, gid
);
297 label_fix(devnode
, true, false);
300 /* always update timestamp when we re-use the node, like on media change events */
301 utimensat(AT_FDCWD
, devnode
, NULL
, 0);
306 void udev_node_add(struct udev_device
*dev
, mode_t mode
, uid_t uid
, gid_t gid
)
308 struct udev
*udev
= udev_device_get_udev(dev
);
309 char filename
[UTIL_PATH_SIZE
];
310 struct udev_list_entry
*list_entry
;
312 log_debug("handling device node '%s', devnum=%s, mode=%#o, uid=%d, gid=%d\n",
313 udev_device_get_devnode(dev
), udev_device_get_id_filename(dev
), mode
, uid
, gid
);
315 if (node_fixup(dev
, mode
, uid
, gid
) < 0)
318 /* always add /dev/{block,char}/$major:$minor */
319 snprintf(filename
, sizeof(filename
), "/dev/%s/%u:%u",
320 strcmp(udev_device_get_subsystem(dev
), "block") == 0 ? "block" : "char",
321 major(udev_device_get_devnum(dev
)), minor(udev_device_get_devnum(dev
)));
322 node_symlink(udev
, udev_device_get_devnode(dev
), filename
);
324 /* create/update symlinks, add symlinks to name index */
325 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(dev
))
326 link_update(dev
, udev_list_entry_get_name(list_entry
), true);
329 void udev_node_remove(struct udev_device
*dev
)
331 struct udev_list_entry
*list_entry
;
332 char filename
[UTIL_PATH_SIZE
];
334 /* remove/update symlinks, remove symlinks from name index */
335 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(dev
))
336 link_update(dev
, udev_list_entry_get_name(list_entry
), false);
338 /* remove /dev/{block,char}/$major:$minor */
339 snprintf(filename
, sizeof(filename
), "/dev/%s/%u:%u",
340 strcmp(udev_device_get_subsystem(dev
), "block") == 0 ? "block" : "char",
341 major(udev_device_get_devnum(dev
)), minor(udev_device_get_devnum(dev
)));