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