]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-node.c
2 * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@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
)) {
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",
79 log_error("device node '%s' already exists, "
80 "link to '%s' will not overwrite it\n",
85 } else if (S_ISLNK(stats
.st_mode
)) {
86 char buf
[UTIL_PATH_SIZE
];
89 len
= readlink(slink
, buf
, sizeof(buf
));
90 if (len
> 0 && len
< (int)sizeof(buf
)) {
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
);
101 log_debug("creating symlink '%s' to '%s'\n", slink
, target
);
103 err
= mkdir_parents_label(slink
, 0755);
104 if (err
!= 0 && err
!= -ENOENT
)
106 label_context_set(slink
, S_IFLNK
);
107 err
= symlink(target
, slink
);
110 label_context_clear();
111 } while (err
== -ENOENT
);
116 log_debug("atomically replace '%s'\n", slink
);
117 util_strscpyl(slink_tmp
, sizeof(slink_tmp
), slink
, TMP_FILE_EXT
, NULL
);
120 err
= mkdir_parents_label(slink_tmp
, 0755);
121 if (err
!= 0 && err
!= -ENOENT
)
123 label_context_set(slink_tmp
, S_IFLNK
);
124 err
= symlink(target
, slink_tmp
);
127 label_context_clear();
128 } while (err
== -ENOENT
);
130 log_error("symlink '%s' '%s' failed: %m\n", target
, slink_tmp
);
133 err
= rename(slink_tmp
, slink
);
135 log_error("rename '%s' '%s' failed: %m\n", slink_tmp
, slink
);
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
)
145 struct udev
*udev
= udev_device_get_udev(dev
);
148 const char *target
= NULL
;
151 priority
= udev_device_get_devlink_priority(dev
);
152 util_strscpy(buf
, bufsize
, udev_device_get_devnode(dev
));
156 dir
= opendir(stackdir
);
160 struct udev_device
*dev_db
;
164 if (dent
== NULL
|| dent
->d_name
[0] == '\0')
166 if (dent
->d_name
[0] == '.')
169 log_debug("found '%s' claiming '%s'\n", dent
->d_name
, stackdir
);
171 /* did we find ourself? */
172 if (strcmp(dent
->d_name
, udev_device_get_id_filename(dev
)) == 0)
175 dev_db
= udev_device_new_from_id_filename(udev
, dent
->d_name
);
176 if (dev_db
!= NULL
) {
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
);
189 udev_device_unref(dev_db
);
196 /* manage "stack of names" with possibly specified device priorities */
197 static void link_update(struct udev_device
*dev
, const char *slink
, bool add
)
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
];
204 char buf
[UTIL_PATH_SIZE
];
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
);
210 if (!add
&& unlink(filename
) == 0)
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
);
219 log_debug("creating link '%s' to '%s'\n", slink
, target
);
220 node_symlink(udev
, target
, slink
);
229 err
= mkdir_parents(filename
, 0755);
230 if (err
!= 0 && err
!= -ENOENT
)
232 fd
= open(filename
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_TRUNC
|O_NOFOLLOW
, 0444);
237 } while (err
== -ENOENT
);
241 void udev_node_update_old_links(struct udev_device
*dev
, struct udev_device
*dev_old
)
243 struct udev_list_entry
*list_entry
;
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
;
251 /* check if old link name still belongs to this device */
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
);
256 if (strcmp(name
, name_current
) == 0) {
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);
270 static int node_fixup(struct udev_device
*dev
, mode_t mode
, uid_t uid
, gid_t gid
)
272 const char *devnode
= udev_device_get_devnode(dev
);
273 dev_t devnum
= udev_device_get_devnum(dev
);
277 if (strcmp(udev_device_get_subsystem(dev
), "block") == 0)
282 if (lstat(devnode
, &stats
) != 0) {
284 log_debug("can not stat() node '%s' (%m)\n", devnode
);
288 if (((stats
.st_mode
& S_IFMT
) != (mode
& S_IFMT
)) || (stats
.st_rdev
!= devnum
)) {
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
));
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
);
300 log_debug("preserve permissions %s, %#o, uid=%u, gid=%u\n", devnode
, mode
, uid
, gid
);
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.
309 if (strcmp(udev_device_get_action(dev
), "add") == 0)
310 label_fix(devnode
, true);
312 /* always update timestamp when we re-use the node, like on media change events */
313 utimensat(AT_FDCWD
, devnode
, NULL
, 0);
318 void udev_node_add(struct udev_device
*dev
, mode_t mode
, uid_t uid
, gid_t gid
)
320 struct udev
*udev
= udev_device_get_udev(dev
);
321 char filename
[UTIL_PATH_SIZE
];
322 struct udev_list_entry
*list_entry
;
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
);
327 if (node_fixup(dev
, mode
, uid
, gid
) < 0)
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
);
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
));
342 link_update(dev
, udev_list_entry_get_name(list_entry
), 1);
346 void udev_node_remove(struct udev_device
*dev
)
348 struct udev_list_entry
*list_entry
;
349 char filename
[UTIL_PATH_SIZE
];
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);
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
)));