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> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
| 23 | #include "xregex.h" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 24 | |
Rob Landley | 9bdd742 | 2005-12-17 10:54:17 +0000 | [diff] [blame] | 25 | #define DEV_PATH "/dev" |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 26 | |
| 27 | #include <busybox.h> |
| 28 | |
| 29 | /* mknod in /dev based on a path like "/sys/block/hda/hda1" */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 30 | static void make_device(char *path) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 31 | { |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 32 | char *device_name,*s; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 33 | int major,minor,type,len,fd; |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 34 | int mode=0660; |
| 35 | uid_t uid=0; |
| 36 | gid_t gid=0; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 37 | |
| 38 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 39 | |
| 40 | /* Try to read major/minor string */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 41 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 42 | snprintf(temp, PATH_MAX, "%s/dev", path); |
| 43 | fd = open(temp, O_RDONLY); |
| 44 | len = read(fd, temp, PATH_MAX-1); |
| 45 | if (len<1) goto end; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 46 | close(fd); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 47 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 48 | /* Determine device name, type, major and minor */ |
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 | device_name = strrchr(path, '/') + 1; |
| 51 | type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK; |
Rob Landley | e927743 | 2006-01-22 23:14:16 +0000 | [diff] [blame] | 52 | if(sscanf(temp, "%d:%d", &major, &minor) != 2) goto end; |
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) { |
| 57 | char *conf,*pos,*end; |
| 58 | |
| 59 | /* mmap the config file */ |
| 60 | if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) { |
| 61 | len=lseek(fd,0,SEEK_END); |
Mike Frysinger | 5990efb | 2006-01-04 07:31:19 +0000 | [diff] [blame] | 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) { |
| 64 | int line=0; |
| 65 | |
| 66 | /* Loop through lines in mmaped file*/ |
| 67 | for (pos=conf;pos-conf<len;) { |
| 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 */ |
| 73 | for(end=pos;end-conf<len && *end!='\n';end++); |
| 74 | |
| 75 | /* Three fields: regex, uid:gid, mode */ |
| 76 | for (field=3;field;field--) { |
| 77 | /* Skip whitespace */ |
| 78 | while (pos<end && isspace(*pos)) pos++; |
| 79 | if (pos==end || *pos=='#') break; |
| 80 | for (end2=pos; |
| 81 | end2<end && !isspace(*end2) && *end2!='#'; end2++); |
| 82 | switch(field) { |
| 83 | /* Regex to match this device */ |
| 84 | case 3: |
| 85 | { |
| 86 | char *regex=strndupa(pos,end2-pos); |
| 87 | regex_t match; |
| 88 | regmatch_t off; |
| 89 | int result; |
| 90 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 91 | /* Is this it? */ |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 92 | xregcomp(&match,regex,REG_EXTENDED); |
| 93 | result=regexec(&match,device_name,1,&off,0); |
| 94 | regfree(&match); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 95 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 96 | /* If not this device, skip rest of line */ |
| 97 | if(result || off.rm_so |
| 98 | || off.rm_eo!=strlen(device_name)) |
| 99 | goto end_line; |
| 100 | |
| 101 | break; |
| 102 | } |
| 103 | /* uid:gid */ |
| 104 | case 2: |
| 105 | { |
| 106 | char *s2; |
| 107 | |
| 108 | /* Find : */ |
| 109 | for(s=pos;s<end2 && *s!=':';s++); |
| 110 | if(s==end2) goto end_line; |
| 111 | |
| 112 | /* Parse UID */ |
| 113 | uid=strtoul(pos,&s2,10); |
| 114 | if(s!=s2) { |
| 115 | struct passwd *pass; |
| 116 | pass=getpwnam(strndupa(pos,s-pos)); |
| 117 | if(!pass) goto end_line; |
| 118 | uid=pass->pw_uid; |
| 119 | } |
| 120 | s++; |
| 121 | /* parse GID */ |
| 122 | gid=strtoul(s,&s2,10); |
| 123 | if(end2!=s2) { |
| 124 | struct group *grp; |
| 125 | grp=getgrnam(strndupa(s,end2-s)); |
| 126 | if(!grp) goto end_line; |
| 127 | gid=grp->gr_gid; |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | /* mode */ |
| 132 | case 1: |
| 133 | { |
| 134 | mode=strtoul(pos,&pos,8); |
| 135 | if(pos!=end2) goto end_line; |
| 136 | goto found_device; |
| 137 | } |
| 138 | } |
| 139 | pos=end2; |
| 140 | } |
| 141 | end_line: |
| 142 | /* Did everything parse happily? */ |
| 143 | if (field && field!=3) |
| 144 | bb_error_msg_and_die("Bad line %d",line); |
| 145 | |
| 146 | /* Next line */ |
| 147 | pos=++end; |
| 148 | } |
| 149 | found_device: |
| 150 | munmap(conf,len); |
| 151 | } |
| 152 | close(fd); |
| 153 | } |
| 154 | } |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 155 | |
| 156 | sprintf(temp, "%s/%s", DEV_PATH, device_name); |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 157 | umask(0); |
| 158 | if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 159 | bb_perror_msg_and_die("mknod %s failed", temp); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 160 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 161 | if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 162 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 163 | end: |
| 164 | RELEASE_CONFIG_BUFFER(temp); |
| 165 | } |
| 166 | |
| 167 | /* Recursive search of /sys/block or /sys/class. path must be a writeable |
| 168 | * buffer of size PATH_MAX containing the directory string to start at. */ |
| 169 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 170 | static void find_dev(char *path) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 171 | { |
| 172 | DIR *dir; |
| 173 | int len=strlen(path); |
| 174 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 175 | if (!(dir = opendir(path))) |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 176 | bb_perror_msg_and_die("No %s",path); |
| 177 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 178 | for (;;) { |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 179 | struct dirent *entry = readdir(dir); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 180 | |
Rob Landley | b56c285 | 2005-12-17 10:52:30 +0000 | [diff] [blame] | 181 | if (!entry) break; |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 182 | |
| 183 | /* Skip "." and ".." (also skips hidden files, which is ok) */ |
| 184 | |
| 185 | if (entry->d_name[0]=='.') continue; |
| 186 | |
| 187 | if (entry->d_type == DT_DIR) { |
| 188 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); |
| 189 | find_dev(path); |
| 190 | path[len] = 0; |
| 191 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 192 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 193 | /* If there's a dev entry, mknod it */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 194 | |
Rob Landley | e927743 | 2006-01-22 23:14:16 +0000 | [diff] [blame] | 195 | if (!strcmp(entry->d_name, "dev")) make_device(path); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 196 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 197 | |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 198 | closedir(dir); |
| 199 | } |
| 200 | |
| 201 | int mdev_main(int argc, char *argv[]) |
| 202 | { |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 203 | char *action; |
| 204 | char *env_path; |
| 205 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
| 206 | |
| 207 | /* Scan */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 208 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 209 | if (argc == 2 && !strcmp(argv[1],"-s")) { |
| 210 | strcpy(temp,"/sys/block"); |
| 211 | find_dev(temp); |
| 212 | strcpy(temp,"/sys/class"); |
| 213 | find_dev(temp); |
| 214 | |
| 215 | /* Hotplug */ |
| 216 | |
| 217 | } else { |
| 218 | action = getenv("ACTION"); |
| 219 | env_path = getenv("DEVPATH"); |
| 220 | if (!action || !env_path) bb_show_usage(); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 221 | |
Rob Landley | 29e08ff | 2006-01-12 06:13:50 +0000 | [diff] [blame] | 222 | if (!strcmp(action, "add")) { |
| 223 | sprintf(temp, "/sys%s", env_path); |
| 224 | make_device(temp); |
| 225 | } else if (!strcmp(action, "remove")) { |
| 226 | sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1); |
| 227 | unlink(temp); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if(ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); |
Rob Landley | 70f7ef7 | 2005-12-13 08:21:33 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |