diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-12 10:23:16 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-12 10:23:16 +0000 |
commit | d9860487e0d88962c1f1ae25ee43bfd0c0b11ad5 (patch) | |
tree | 4d33d61227b72af978ffa9cf03ae3833469ccf09 /util-linux/mdev.c | |
parent | 6b7971805c53e49892d175e59b827873090fff7a (diff) | |
download | busybox-w32-d9860487e0d88962c1f1ae25ee43bfd0c0b11ad5.tar.gz busybox-w32-d9860487e0d88962c1f1ae25ee43bfd0c0b11ad5.tar.bz2 busybox-w32-d9860487e0d88962c1f1ae25ee43bfd0c0b11ad5.zip |
mdev: if device was moved at creation, at removal coreectly remove
it from moved location and also remove symlinks to it
function old new delta
build_alias - 78 +78
mdev_main 505 503 -2
make_device 1300 1294 -6
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 78/-8) Total: 70 bytes
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r-- | util-linux/mdev.c | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index a4c0520ac..4ac5c430a 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
@@ -35,6 +35,30 @@ static char *next_field(char *s) | |||
35 | return s; | 35 | return s; |
36 | } | 36 | } |
37 | 37 | ||
38 | /* Builds an alias path. | ||
39 | * This function potentionally reallocates the alias parameter. | ||
40 | */ | ||
41 | static char *build_alias(char *alias, const char *device_name) | ||
42 | { | ||
43 | char *dest; | ||
44 | |||
45 | /* ">bar/": rename to bar/device_name */ | ||
46 | /* ">bar[/]baz": rename to bar[/]baz */ | ||
47 | dest = strrchr(alias, '/'); | ||
48 | if (dest) { /* ">bar/[baz]" ? */ | ||
49 | *dest = '\0'; /* mkdir bar */ | ||
50 | bb_make_directory(alias, 0755, FILEUTILS_RECUR); | ||
51 | *dest = '/'; | ||
52 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ | ||
53 | dest = alias; | ||
54 | alias = concat_path_file(alias, device_name); | ||
55 | free(dest); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | return alias; | ||
60 | } | ||
61 | |||
38 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ | 62 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
39 | /* NB: "mdev -s" may call us many times, do not leak memory/fds! */ | 63 | /* NB: "mdev -s" may call us many times, do not leak memory/fds! */ |
40 | static void make_device(char *path, int delete) | 64 | static void make_device(char *path, int delete) |
@@ -257,21 +281,7 @@ static void make_device(char *path, int delete) | |||
257 | chown(device_name, uid, gid); | 281 | chown(device_name, uid, gid); |
258 | 282 | ||
259 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { | 283 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { |
260 | char *dest; | 284 | alias = build_alias(alias, device_name); |
261 | |||
262 | /* ">bar/": rename to bar/device_name */ | ||
263 | /* ">bar[/]baz": rename to bar[/]baz */ | ||
264 | dest = strrchr(alias, '/'); | ||
265 | if (dest) { /* ">bar/[baz]" ? */ | ||
266 | *dest = '\0'; /* mkdir bar */ | ||
267 | bb_make_directory(alias, 0755, FILEUTILS_RECUR); | ||
268 | *dest = '/'; | ||
269 | if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */ | ||
270 | dest = alias; | ||
271 | alias = concat_path_file(alias, device_name); | ||
272 | free(dest); | ||
273 | } | ||
274 | } | ||
275 | 285 | ||
276 | /* move the device, and optionally | 286 | /* move the device, and optionally |
277 | * make a symlink to moved device node */ | 287 | * make a symlink to moved device node */ |
@@ -295,8 +305,16 @@ static void make_device(char *path, int delete) | |||
295 | free(command); | 305 | free(command); |
296 | } | 306 | } |
297 | 307 | ||
298 | if (delete) | 308 | if (delete) { |
299 | unlink(device_name); | 309 | unlink(device_name); |
310 | /* At creation time, device might have been moved | ||
311 | * and a symlink might have been created. Undo that. */ | ||
312 | if (ENABLE_FEATURE_MDEV_RENAME && alias) { | ||
313 | alias = build_alias(alias, device_name); | ||
314 | unlink(alias); | ||
315 | free(alias); | ||
316 | } | ||
317 | } | ||
300 | } | 318 | } |
301 | 319 | ||
302 | /* File callback for /sys/ traversal */ | 320 | /* File callback for /sys/ traversal */ |
@@ -309,7 +327,7 @@ static int FAST_FUNC fileAction(const char *fileName, | |||
309 | char *scratch = userData; | 327 | char *scratch = userData; |
310 | 328 | ||
311 | /* len check is for paranoid reasons */ | 329 | /* len check is for paranoid reasons */ |
312 | if (strcmp(fileName + len, "/dev") || len >= PATH_MAX) | 330 | if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX) |
313 | return FALSE; | 331 | return FALSE; |
314 | 332 | ||
315 | strcpy(scratch, fileName); | 333 | strcpy(scratch, fileName); |
@@ -387,7 +405,7 @@ static void load_firmware(const char *const firmware, const char *const sysfs_pa | |||
387 | } | 405 | } |
388 | 406 | ||
389 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 407 | int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
390 | int mdev_main(int argc, char **argv) | 408 | int mdev_main(int argc UNUSED_PARAM, char **argv) |
391 | { | 409 | { |
392 | char *action; | 410 | char *action; |
393 | char *env_path; | 411 | char *env_path; |
@@ -410,7 +428,7 @@ int mdev_main(int argc, char **argv) | |||
410 | 428 | ||
411 | xchdir("/dev"); | 429 | xchdir("/dev"); |
412 | 430 | ||
413 | if (argc == 2 && !strcmp(argv[1], "-s")) { | 431 | if (argv[1] && !strcmp(argv[1], "-s")) { |
414 | /* Scan: | 432 | /* Scan: |
415 | * mdev -s | 433 | * mdev -s |
416 | */ | 434 | */ |