Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 1 | /* vi:set ts=4: |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 2 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 3 | * mdev - Mini udev for busybox |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 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 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 11 | #include <ctype.h> |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 12 | #include <dirent.h> |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 17 | #include <sys/mman.h> |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 18 | #include <sys/stat.h> |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 19 | #include <sys/sysmacros.h> |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 23 | #include "busybox.h" |
| 24 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 25 | |
Rob Landley | 9bdd742 | 2005-12-17 10:54:17 +0000 | [diff] [blame] | 26 | #define DEV_PATH "/dev" |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 27 | #define MDEV_CONF "/etc/mdev.conf" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 28 | |
| 29 | #include <busybox.h> |
| 30 | |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 31 | struct mdev_globals |
| 32 | { |
| 33 | int root_major, root_minor; |
| 34 | } mdev_globals; |
| 35 | |
| 36 | #define bbg mdev_globals |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 37 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 38 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 39 | static void make_device(char *path) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 40 | { |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 41 | char *device_name, *s; |
| 42 | int major, minor, type, len, fd; |
| 43 | int mode = 0660; |
| 44 | uid_t uid = 0; |
| 45 | gid_t gid = 0; |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 46 | char *temp = path + strlen(path); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 47 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 48 | /* Try to read major/minor string. Note that the kernel puts \n after |
| 49 | * the data, so we don't need to worry about null terminating the string |
| 50 | * because sscanf() will stop at the first nondigit, which \n is. We |
| 51 | * also depend on path having writeable space after it. */ |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 52 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 53 | strcat(path, "/dev"); |
| 54 | fd = open(path, O_RDONLY); |
| 55 | len = read(fd, temp + 1, 64); |
| 56 | *temp++ = 0; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 57 | close(fd); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 58 | if (len < 1) return; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 59 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 60 | /* Determine device name, type, major and minor */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 61 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 62 | device_name = strrchr(path, '/') + 1; |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 63 | type = path[5]=='c' ? S_IFCHR : S_IFBLK; |
| 64 | if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 65 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 66 | /* If we have a config file, look up permissions for this device */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 67 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 68 | if (ENABLE_FEATURE_MDEV_CONF) { |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 69 | char *conf, *pos, *end; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 70 | |
| 71 | /* mmap the config file */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 72 | if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) { |
| 73 | len = lseek(fd, 0, SEEK_END); |
| 74 | conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 75 | if (conf) { |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 76 | int line = 0; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 77 | |
| 78 | /* Loop through lines in mmaped file*/ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 79 | for (pos=conf; pos-conf<len;) { |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 80 | int field; |
| 81 | char *end2; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 82 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 83 | line++; |
| 84 | /* find end of this line */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 85 | for(end=pos; end-conf<len && *end!='\n'; end++) |
| 86 | ; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 87 | |
| 88 | /* Three fields: regex, uid:gid, mode */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 89 | for (field=3; field; field--) { |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 90 | /* Skip whitespace */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 91 | while (pos<end && isspace(*pos)) |
| 92 | pos++; |
| 93 | if (pos==end || *pos=='#') |
| 94 | break; |
| 95 | for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++) |
| 96 | ; |
| 97 | |
| 98 | switch (field) { |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 99 | /* Regex to match this device */ |
| 100 | case 3: |
| 101 | { |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 102 | char *regex = strndupa(pos,end2-pos); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 103 | regex_t match; |
| 104 | regmatch_t off; |
| 105 | int result; |
| 106 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 107 | /* Is this it? */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 108 | xregcomp(&match,regex,REG_EXTENDED); |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 109 | result = regexec(&match,device_name,1,&off,0); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 110 | regfree(&match); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 111 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 112 | /* If not this device, skip rest of line */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 113 | if (result || off.rm_so || off.rm_eo!=strlen(device_name)) |
| 114 | goto end_line; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 115 | |
| 116 | break; |
| 117 | } |
| 118 | /* uid:gid */ |
| 119 | case 2: |
| 120 | { |
| 121 | char *s2; |
| 122 | |
| 123 | /* Find : */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 124 | for(s=pos; s<end2 && *s!=':'; s++) |
| 125 | ; |
| 126 | if (s == end2) |
| 127 | goto end_line; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 128 | |
| 129 | /* Parse UID */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 130 | uid = strtoul(pos,&s2,10); |
| 131 | if (s != s2) { |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 132 | struct passwd *pass; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 133 | pass = getpwnam(strndupa(pos,s-pos)); |
| 134 | if (!pass) |
| 135 | goto end_line; |
| 136 | uid = pass->pw_uid; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 137 | } |
| 138 | s++; |
| 139 | /* parse GID */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 140 | gid = strtoul(s,&s2,10); |
| 141 | if (end2 != s2) { |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 142 | struct group *grp; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 143 | grp = getgrnam(strndupa(s,end2-s)); |
| 144 | if (!grp) |
| 145 | goto end_line; |
| 146 | gid = grp->gr_gid; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 147 | } |
| 148 | break; |
| 149 | } |
| 150 | /* mode */ |
| 151 | case 1: |
| 152 | { |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 153 | mode = strtoul(pos,&pos,8); |
| 154 | if (pos != end2) |
| 155 | goto end_line; |
| 156 | else |
| 157 | goto found_device; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 160 | pos = end2; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 161 | } |
| 162 | end_line: |
| 163 | /* Did everything parse happily? */ |
| 164 | if (field && field!=3) |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 165 | bb_error_msg_and_die("Bad line %d",line); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 166 | |
| 167 | /* Next line */ |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 168 | pos = ++end; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 169 | } |
| 170 | found_device: |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 171 | munmap(conf, len); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 172 | } |
| 173 | close(fd); |
| 174 | } |
| 175 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 176 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 177 | umask(0); |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 178 | if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
| 179 | bb_perror_msg_and_die("mknod %s failed", device_name); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 180 | |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 181 | if (major==bbg.root_major && minor==bbg.root_minor) |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 182 | symlink(device_name, "root"); |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 183 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 184 | if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* Recursive search of /sys/block or /sys/class. path must be a writeable |
| 188 | * buffer of size PATH_MAX containing the directory string to start at. */ |
| 189 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 190 | static void find_dev(char *path) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 191 | { |
| 192 | DIR *dir; |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 193 | size_t len = strlen(path); |
Mike Frysinger | 248d222 | 2006-02-03 00:19:42 +0000 | [diff] [blame] | 194 | struct dirent *entry; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 195 | |
Mike Frysinger | 248d222 | 2006-02-03 00:19:42 +0000 | [diff] [blame] | 196 | if ((dir = opendir(path)) == NULL) |
| 197 | return; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 198 | |
Mike Frysinger | 248d222 | 2006-02-03 00:19:42 +0000 | [diff] [blame] | 199 | while ((entry = readdir(dir)) != NULL) { |
Rob Landley | 0960ca7 | 2006-06-13 18:27:16 +0000 | [diff] [blame^] | 200 | struct stat st; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 201 | |
| 202 | /* Skip "." and ".." (also skips hidden files, which is ok) */ |
| 203 | |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 204 | if (entry->d_name[0] == '.') |
| 205 | continue; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 206 | |
Rob Landley | 0960ca7 | 2006-06-13 18:27:16 +0000 | [diff] [blame^] | 207 | // uClibc doesn't fill out entry->d_type reliably. so we use lstat(). |
| 208 | |
| 209 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); |
| 210 | if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path); |
| 211 | path[len] = 0; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 212 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 213 | /* If there's a dev entry, mknod it */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 214 | |
Rob Landley | e927743 | 2006-01-22 23:14:16 +0000 | [diff] [blame] | 215 | if (!strcmp(entry->d_name, "dev")) make_device(path); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 216 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 217 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 218 | closedir(dir); |
| 219 | } |
| 220 | |
| 221 | int mdev_main(int argc, char *argv[]) |
| 222 | { |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 223 | char *action; |
| 224 | char *env_path; |
| 225 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 226 | |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 227 | bb_xchdir(DEV_PATH); |
| 228 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 229 | /* Scan */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 230 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 231 | if (argc == 2 && !strcmp(argv[1],"-s")) { |
Rob Landley | a7e3d05 | 2006-02-21 06:11:13 +0000 | [diff] [blame] | 232 | struct stat st; |
| 233 | |
| 234 | stat("/", &st); // If this fails, we have bigger problems. |
Rob Landley | 5cd1ccd | 2006-05-21 18:33:27 +0000 | [diff] [blame] | 235 | bbg.root_major=major(st.st_dev); |
| 236 | bbg.root_minor=minor(st.st_dev); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 237 | strcpy(temp,"/sys/block"); |
| 238 | find_dev(temp); |
| 239 | strcpy(temp,"/sys/class"); |
| 240 | find_dev(temp); |
| 241 | |
| 242 | /* Hotplug */ |
| 243 | |
| 244 | } else { |
| 245 | action = getenv("ACTION"); |
| 246 | env_path = getenv("DEVPATH"); |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 247 | if (!action || !env_path) |
| 248 | bb_show_usage(); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 249 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 250 | if (!strcmp(action, "add")) { |
| 251 | sprintf(temp, "/sys%s", env_path); |
| 252 | make_device(temp); |
| 253 | } else if (!strcmp(action, "remove")) { |
Rob Landley | 15fe2e1 | 2006-05-08 02:53:23 +0000 | [diff] [blame] | 254 | unlink(strrchr(env_path, '/') + 1); |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Mike Frysinger | a421ba8 | 2006-02-03 00:25:37 +0000 | [diff] [blame] | 258 | if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 259 | return 0; |
| 260 | } |