blob: d83e10bf31f402718a2dcf6a1eca6d5ea66b8d85 [file] [log] [blame]
Rob Landleyc92fde02007-04-23 15:45:55 -04001/* vi:set ts=4:
Rob Landley2c226852007-11-15 18:30:30 -06002 *
Rob Landleyc92fde02007-04-23 15:45:55 -04003 * mdev - Mini udev for busybox
Rob Landley2c226852007-11-15 18:30:30 -06004 *
Rob Landley988abb32008-05-12 00:52:27 -05005 * Copyright 2005, 2008 Rob Landley <rob@landley.net>
Rob Landleyc92fde02007-04-23 15:45:55 -04006 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
Rob Landleyfece5cb2007-12-03 20:05:57 -06007 *
8 * Not in SUSv3.
Rob Landley28964802008-01-19 17:08:39 -06009
Rob Landley0f8c4c52008-02-12 19:05:44 -060010USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
Rob Landley0a521a22008-02-12 17:35:10 -060011
Rob Landley28964802008-01-19 17:08:39 -060012config MDEV
13 bool "mdev"
Rob Landleyb7529b62012-03-08 20:14:55 -060014 default n
Rob Landley28964802008-01-19 17:08:39 -060015 help
16 usage: mdev [-s]
17
18 Create devices in /dev using information from /sys.
19
20 -s Scan all entries in /sys to populate /dev.
21
22config MDEV_CONF
23 bool "Configuration file for mdev"
Rob Landley988abb32008-05-12 00:52:27 -050024 default y
Rob Landley28964802008-01-19 17:08:39 -060025 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
29
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*/
Rob Landleyc92fde02007-04-23 15:45:55 -040035
36#include "toys.h"
37#include "lib/xregcomp.h"
38
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;
47
48 // Try to read major/minor string
49
Rob Landley988abb32008-05-12 00:52:27 -050050 temp = strrchr(path, '/');
Rob Landleyc92fde02007-04-23 15:45:55 -040051 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;
Rob Landley2c226852007-11-15 18:30:30 -060058
Rob Landleyc92fde02007-04-23 15:45:55 -040059 // Determine device name, type, major and minor
Rob Landley2c226852007-11-15 18:30:30 -060060
Rob Landleyc92fde02007-04-23 15:45:55 -040061 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);
65
66 // If we have a config file, look up permissions for this device
Rob Landley2c226852007-11-15 18:30:30 -060067
Rob Landleyc92fde02007-04-23 15:45:55 -040068 if (CFG_MDEV_CONF) {
69 char *conf, *pos, *end;
70
71 // mmap the config file
72 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) {
Rob Landley2de1d1a2010-01-05 10:45:16 -060073 len = fdlength(fd);
Rob Landleyc92fde02007-04-23 15:45:55 -040074 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
75 if (conf) {
76 int line = 0;
77
78 // Loop through lines in mmaped file
79 for (pos = conf; pos-conf<len;) {
80 int field;
81 char *end2;
Rob Landley2c226852007-11-15 18:30:30 -060082
Rob Landleyc92fde02007-04-23 15:45:55 -040083 line++;
84 // find end of this line
85 for(end = pos; end-conf<len && *end!='\n'; end++);
86
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;
102
103 // Is this it?
104 xregcomp(&match, regex, REG_EXTENDED);
105 result=regexec(&match, device_name, 1, &off, 0);
106 regfree(&match);
Rob Landley2c226852007-11-15 18:30:30 -0600107
Rob Landleyc92fde02007-04-23 15:45:55 -0400108 // 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;
112
113 break;
114 }
115 // uid:gid
116 case 2:
117 {
118 char *s2;
119
120 // Find :
121 for(s = pos; s<end2 && *s!=':'; s++);
122 if (s==end2) goto end_line;
123
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 }
153end_line:
154 // Did everything parse happily?
155 if (field && field!=3) error_exit("Bad line %d", line);
156
157 // Next line
158 pos = ++end;
159 }
160found_device:
161 munmap(conf, len);
162 }
163 close(fd);
164 }
165 }
166
167 sprintf(temp, "/dev/%s", device_name);
Rob Landleyc92fde02007-04-23 15:45:55 -0400168 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
169 perror_exit("mknod %s failed", temp);
170
Rob Landley7471b562008-12-14 01:08:37 -0600171 // Dear gcc: shut up about ignoring the return value here. If it doesn't
172 // work, what exactly are we supposed to do about it?
173 if (CFG_MDEV_CONF) mode=chown(temp, uid, gid);
Rob Landleyc92fde02007-04-23 15:45:55 -0400174}
175
Rob Landley988abb32008-05-12 00:52:27 -0500176static int callback(char *path, struct dirtree *node)
Rob Landleyc92fde02007-04-23 15:45:55 -0400177{
Rob Landley988abb32008-05-12 00:52:27 -0500178 // Entries in /sys/class/block aren't char devices, so skip 'em. (We'll
179 // get block devices out of /sys/block.)
180 if(!strcmp(node->name, "block")) return 1;
Rob Landleyc92fde02007-04-23 15:45:55 -0400181
Rob Landley988abb32008-05-12 00:52:27 -0500182 // Does this directory have a "dev" entry in it?
183 if (S_ISDIR(node->st.st_mode) || S_ISLNK(node->st.st_mode)) {
184 char *dest = path+strlen(path);
185 strcpy(dest, "/dev");
186 if (!access(path, R_OK)) make_device(path);
187 *dest = 0;
Rob Landleyc92fde02007-04-23 15:45:55 -0400188 }
Rob Landley2c226852007-11-15 18:30:30 -0600189
Rob Landley988abb32008-05-12 00:52:27 -0500190 // Circa 2.6.25 the entries more than 2 deep are all either redundant
191 // (mouse#, event#) or unnamed (every usb_* entry is called "device").
192 return node->depth>1;
Rob Landleyc92fde02007-04-23 15:45:55 -0400193}
194
Rob Landley0a521a22008-02-12 17:35:10 -0600195void mdev_main(void)
Rob Landleyc92fde02007-04-23 15:45:55 -0400196{
Rob Landley988abb32008-05-12 00:52:27 -0500197 // Handle -s
198
Rob Landleyc92fde02007-04-23 15:45:55 -0400199 if (toys.optflags) {
Rob Landley988abb32008-05-12 00:52:27 -0500200 xchdir("/sys/class");
Rob Landleyc92fde02007-04-23 15:45:55 -0400201 strcpy(toybuf, "/sys/class");
Rob Landley988abb32008-05-12 00:52:27 -0500202 dirtree_read(toybuf, NULL, callback);
203 strcpy(toybuf+5, "block");
204 dirtree_read(toybuf, NULL, callback);
Rob Landley2c226852007-11-15 18:30:30 -0600205 }
Rob Landley988abb32008-05-12 00:52:27 -0500206// if (toys.optflags) {
207// strcpy(toybuf, "/sys/block");
208// find_dev(toybuf);
209// strcpy(toybuf, "/sys/class");
210// find_dev(toybuf);
211// return;
212// }
Rob Landley2c226852007-11-15 18:30:30 -0600213
Rob Landleyc92fde02007-04-23 15:45:55 -0400214 // hotplug support goes here
Rob Landleyc92fde02007-04-23 15:45:55 -0400215}