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