toybox

view toys/mdev.c @ 292:b4077be6c746

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...)
author Rob Landley <rob@landley.net>
date Mon May 12 00:52:27 2008 -0500 (2008-05-12)
parents 951110c37fee
children 6773399539aa
line source
1 /* vi:set ts=4:
2 *
3 * mdev - Mini udev for busybox
4 *
5 * Copyright 2005, 2008 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7 *
8 * Not in SUSv3.
10 USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
12 config MDEV
13 bool "mdev"
14 default y
15 help
16 usage: mdev [-s]
18 Create devices in /dev using information from /sys.
20 -s Scan all entries in /sys to populate /dev.
22 config MDEV_CONF
23 bool "Configuration file for mdev"
24 default y
25 depends on MDEV
26 help
27 The mdev config file (/etc/mdev.conf) contains lines that look like:
28 hd[a-z][0-9]* 0:3 660
30 Each line must contain three whitespace separated fields. The first
31 field is a regular expression matching one or more device names, and
32 the second and third fields are uid:gid and file permissions for
33 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"
40 static 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 string
50 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 minor
61 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 device
68 if (CFG_MDEV_CONF) {
69 char *conf, *pos, *end;
71 // mmap the config file
72 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 file
79 for (pos = conf; pos-conf<len;) {
80 int field;
81 char *end2;
83 line++;
84 // find end of this line
85 for(end = pos; end-conf<len && *end!='\n'; end++);
87 // Three fields: regex, uid:gid, mode
88 for (field = 3; field; field--) {
89 // Skip whitespace
90 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 device
96 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 line
109 if (result || off.rm_so
110 || off.rm_eo!=strlen(device_name))
111 goto end_line;
113 break;
114 }
115 // uid:gid
116 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 UID
125 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 GID
134 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 // mode
144 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 }
153 end_line:
154 // Did everything parse happily?
155 if (field && field!=3) error_exit("Bad line %d", line);
157 // Next line
158 pos = ++end;
159 }
160 found_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 }
174 static int callback(char *path, struct dirtree *node)
175 {
176 // Entries in /sys/class/block aren't char devices, so skip 'em. (We'll
177 // 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 redundant
189 // (mouse#, event#) or unnamed (every usb_* entry is called "device").
190 return node->depth>1;
191 }
193 void mdev_main(void)
194 {
195 // Handle -s
197 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 here
213 }