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