Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 1 | /* vi:set ts=4: |
| 2 | * |
| 3 | * mdev - Mini udev for busybox |
| 4 | * |
| 5 | * Copyright 2005 Rob Landley <rob@landley.net> |
| 6 | * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> |
| 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | |
| 11 | #include <dirent.h> |
| 12 | #include <errno.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #define DEV_PATH "/dev" |
| 21 | #define DEV_MODE 0660 |
| 22 | |
| 23 | #include <busybox.h> |
| 24 | |
| 25 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
| 26 | void make_device(char *path) |
| 27 | { |
| 28 | char *device_name, *s; |
| 29 | int major,minor,type,len,fd; |
| 30 | |
| 31 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 32 | |
| 33 | /* Try to read major/minor string */ |
| 34 | |
| 35 | snprintf(temp, PATH_MAX, "%s/dev", path); |
| 36 | fd = open(temp, O_RDONLY); |
| 37 | len = read(fd, temp, PATH_MAX-1); |
| 38 | if (len<1) goto end; |
| 39 | temp[len] = 0; |
| 40 | close(fd); |
| 41 | |
| 42 | /* Determine device name, type, major and minor */ |
| 43 | |
| 44 | device_name = strrchr(path, '/') + 1; |
| 45 | type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK; |
| 46 | major = minor = 0; |
| 47 | for(s = temp; *s; s++) { |
| 48 | if(*s == ':') { |
| 49 | major = minor; |
| 50 | minor = 0; |
| 51 | } else { |
| 52 | minor *= 10; |
| 53 | minor += (*s) - '0'; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* Open config file here, look up permissions */ |
| 58 | |
| 59 | sprintf(temp, "%s/%s", DEV_PATH, device_name); |
| 60 | if(mknod(temp, DEV_MODE | type, makedev(major, minor)) && errno != EEXIST) |
| 61 | bb_perror_msg_and_die("mknod %s failed", temp); |
| 62 | |
| 63 | /* Perform shellout here */ |
| 64 | |
| 65 | end: |
| 66 | RELEASE_CONFIG_BUFFER(temp); |
| 67 | } |
| 68 | |
| 69 | /* Recursive search of /sys/block or /sys/class. path must be a writeable |
| 70 | * buffer of size PATH_MAX containing the directory string to start at. */ |
| 71 | |
| 72 | void find_dev(char *path) |
| 73 | { |
| 74 | DIR *dir; |
| 75 | int len=strlen(path); |
| 76 | |
| 77 | if(!(dir = opendir(path))) |
| 78 | bb_perror_msg_and_die("No %s",path); |
| 79 | |
| 80 | for(;;) { |
| 81 | struct dirent *entry = readdir(dir); |
| 82 | |
| 83 | if(!entry) break; |
| 84 | |
| 85 | /* Skip "." and ".." (also skips hidden files, which is ok) */ |
| 86 | |
| 87 | if (entry->d_name[0]=='.') continue; |
| 88 | |
| 89 | if (entry->d_type == DT_DIR) { |
| 90 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); |
| 91 | find_dev(path); |
| 92 | path[len] = 0; |
| 93 | } |
| 94 | |
| 95 | /* If there's a dev entry, mknod it */ |
| 96 | |
| 97 | if (strcmp(entry->d_name, "dev")) make_device(path); |
| 98 | } |
| 99 | |
| 100 | closedir(dir); |
| 101 | } |
| 102 | |
| 103 | int mdev_main(int argc, char *argv[]) |
| 104 | { |
| 105 | if (argc > 1) { |
| 106 | if(argc == 2 && !strcmp(argv[1],"-s")) { |
| 107 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 108 | strcpy(temp,"/sys/block"); |
| 109 | find_dev(temp); |
| 110 | strcpy(temp,"/sys/class"); |
| 111 | find_dev(temp); |
| 112 | if(ENABLE_FEATURE_CLEAN_UP) |
| 113 | RELEASE_CONFIG_BUFFER(temp); |
| 114 | return 0; |
| 115 | } else bb_show_usage(); |
| 116 | } |
| 117 | |
| 118 | /* hotplug support goes here */ |
| 119 | |
| 120 | return 0; |
| 121 | } |