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