| changeset 292: | b4077be6c746 |
| parent: | 951110c37fee |
| child: | 6773399539aa |
| author: | Rob Landley <rob@landley.net> |
| date: | Mon May 12 00:52:27 2008 -0500 (3 years ago) |
| permissions: | -rw-r--r-- |
| description: | Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel. (Yeah, I know sysfs hasn't actually got an API, but I like to pretend...) |
1/* vi:set ts=4:2 *3 * mdev - Mini udev for busybox4 *5 * Copyright 2005, 2008 Rob Landley <rob@landley.net>6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>7 *8 * Not in SUSv3.10USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))12config MDEV13 bool "mdev"14 default y15 help16 usage: mdev [-s]18 Create devices in /dev using information from /sys.20 -s Scan all entries in /sys to populate /dev.22config MDEV_CONF23 bool "Configuration file for mdev"24 default y25 depends on MDEV26 help27 The mdev config file (/etc/mdev.conf) contains lines that look like:28 hd[a-z][0-9]* 0:3 66030 Each line must contain three whitespace separated fields. The first31 field is a regular expression matching one or more device names, and32 the second and third fields are uid:gid and file permissions for33 matching devies.34*/36#include "toys.h"37#include "lib/xregcomp.h"39// mknod in /dev based on a path like "/sys/block/hda/hda1"40static void make_device(char *path)41{42 char *device_name, *s, *temp;43 int major, minor, type, len, fd;44 int mode = 0660;45 uid_t uid = 0;46 gid_t gid = 0;48 // Try to read major/minor string50 temp = strrchr(path, '/');51 fd = open(path, O_RDONLY);52 *temp=0;53 temp++;54 len = read(fd, temp, 64);55 close(fd);56 if (len<1) return;57 temp[len] = 0;59 // Determine device name, type, major and minor61 device_name = strrchr(path, '/') + 1;62 type = path[5]=='c' ? S_IFCHR : S_IFBLK;63 major = minor = 0;64 sscanf(temp, "%u:%u", &major, &minor);66 // If we have a config file, look up permissions for this device68 if (CFG_MDEV_CONF) {69 char *conf, *pos, *end;71 // mmap the config file72 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) {73 len = lseek(fd, 0, SEEK_END);74 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);75 if (conf) {76 int line = 0;78 // Loop through lines in mmaped file79 for (pos = conf; pos-conf<len;) {80 int field;81 char *end2;83 line++;84 // find end of this line85 for(end = pos; end-conf<len && *end!='\n'; end++);87 // Three fields: regex, uid:gid, mode88 for (field = 3; field; field--) {89 // Skip whitespace90 while (pos<end && isspace(*pos)) pos++;91 if (pos==end || *pos=='#') break;92 for (end2 = pos;93 end2<end && !isspace(*end2) && *end2!='#'; end2++);94 switch(field) {95 // Regex to match this device96 case 3:97 {98 char *regex = strndupa(pos, end2-pos);99 regex_t match;100 regmatch_t off;101 int result;103 // Is this it?104 xregcomp(&match, regex, REG_EXTENDED);105 result=regexec(&match, device_name, 1, &off, 0);106 regfree(&match);108 // If not this device, skip rest of line109 if (result || off.rm_so110 || off.rm_eo!=strlen(device_name))111 goto end_line;113 break;114 }115 // uid:gid116 case 2:117 {118 char *s2;120 // Find :121 for(s = pos; s<end2 && *s!=':'; s++);122 if (s==end2) goto end_line;124 // Parse UID125 uid = strtoul(pos,&s2,10);126 if (s!=s2) {127 struct passwd *pass;128 pass = getpwnam(strndupa(pos, s-pos));129 if (!pass) goto end_line;130 uid = pass->pw_uid;131 }132 s++;133 // parse GID134 gid = strtoul(s,&s2,10);135 if (end2!=s2) {136 struct group *grp;137 grp = getgrnam(strndupa(s, end2-s));138 if (!grp) goto end_line;139 gid = grp->gr_gid;140 }141 break;142 }143 // mode144 case 1:145 {146 mode = strtoul(pos, &pos, 8);147 if (pos!=end2) goto end_line;148 goto found_device;149 }150 }151 pos=end2;152 }153end_line:154 // Did everything parse happily?155 if (field && field!=3) error_exit("Bad line %d", line);157 // Next line158 pos = ++end;159 }160found_device:161 munmap(conf, len);162 }163 close(fd);164 }165 }167 sprintf(temp, "/dev/%s", device_name);168 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)169 perror_exit("mknod %s failed", temp);171 if (CFG_MDEV_CONF) chown(temp, uid, gid);172}174static int callback(char *path, struct dirtree *node)175{176 // Entries in /sys/class/block aren't char devices, so skip 'em. (We'll177 // get block devices out of /sys/block.)178 if(!strcmp(node->name, "block")) return 1;180 // Does this directory have a "dev" entry in it?181 if (S_ISDIR(node->st.st_mode) || S_ISLNK(node->st.st_mode)) {182 char *dest = path+strlen(path);183 strcpy(dest, "/dev");184 if (!access(path, R_OK)) make_device(path);185 *dest = 0;186 }188 // Circa 2.6.25 the entries more than 2 deep are all either redundant189 // (mouse#, event#) or unnamed (every usb_* entry is called "device").190 return node->depth>1;191}193void mdev_main(void)194{195 // Handle -s197 if (toys.optflags) {198 xchdir("/sys/class");199 strcpy(toybuf, "/sys/class");200 dirtree_read(toybuf, NULL, callback);201 strcpy(toybuf+5, "block");202 dirtree_read(toybuf, NULL, callback);203 }204// if (toys.optflags) {205// strcpy(toybuf, "/sys/block");206// find_dev(toybuf);207// strcpy(toybuf, "/sys/class");208// find_dev(toybuf);209// return;210// }212 // hotplug support goes here213}