changelog shortlog tags changeset files revisions annotate raw

toys/mdev.c

changeset 257: 951110c37fee
parent:bf3f98a58ee2
child:b4077be6c746
author: Rob Landley <rob@landley.net>
date: Tue Feb 12 19:05:44 2008 -0600 (4 years ago)
permissions: -rw-r--r--
description: Add TOYFLAG_UMASK.
1/* vi:set ts=4:
2 *
3 * mdev - Mini udev for busybox
4 *
5 * Copyright 2005, 2007 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7 *
8 * Not in SUSv3.
9
10USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
11
12config MDEV
13 bool "mdev"
14 default n
15 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"
24 default n
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
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*/
35
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
50 temp = path+strlen(path);
51 strcpy(temp, "/dev");
52 fd = open(path, O_RDONLY);
53 *temp=0;
54 temp++;
55 len = read(fd, temp, 64);
56 close(fd);
57 if (len<1) return;
58 temp[len] = 0;
59
60 // Determine device name, type, major and minor
61
62 device_name = strrchr(path, '/') + 1;
63 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
64 major = minor = 0;
65 sscanf(temp, "%u:%u", &major, &minor);
66
67 // If we have a config file, look up permissions for this device
68
69 if (CFG_MDEV_CONF) {
70 char *conf, *pos, *end;
71
72 // mmap the config file
73 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) {
74 len = lseek(fd, 0, SEEK_END);
75 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
76 if (conf) {
77 int line = 0;
78
79 // Loop through lines in mmaped file
80 for (pos = conf; pos-conf<len;) {
81 int field;
82 char *end2;
83
84 line++;
85 // find end of this line
86 for(end = pos; end-conf<len && *end!='\n'; end++);
87
88 // Three fields: regex, uid:gid, mode
89 for (field = 3; field; field--) {
90 // Skip whitespace
91 while (pos<end && isspace(*pos)) pos++;
92 if (pos==end || *pos=='#') break;
93 for (end2 = pos;
94 end2<end && !isspace(*end2) && *end2!='#'; end2++);
95 switch(field) {
96 // Regex to match this device
97 case 3:
98 {
99 char *regex = strndupa(pos, end2-pos);
100 regex_t match;
101 regmatch_t off;
102 int result;
103
104 // Is this it?
105 xregcomp(&match, regex, REG_EXTENDED);
106 result=regexec(&match, device_name, 1, &off, 0);
107 regfree(&match);
108
109 // If not this device, skip rest of line
110 if (result || off.rm_so
111 || off.rm_eo!=strlen(device_name))
112 goto end_line;
113
114 break;
115 }
116 // uid:gid
117 case 2:
118 {
119 char *s2;
120
121 // Find :
122 for(s = pos; s<end2 && *s!=':'; s++);
123 if (s==end2) goto end_line;
124
125 // Parse UID
126 uid = strtoul(pos,&s2,10);
127 if (s!=s2) {
128 struct passwd *pass;
129 pass = getpwnam(strndupa(pos, s-pos));
130 if (!pass) goto end_line;
131 uid = pass->pw_uid;
132 }
133 s++;
134 // parse GID
135 gid = strtoul(s,&s2,10);
136 if (end2!=s2) {
137 struct group *grp;
138 grp = getgrnam(strndupa(s, end2-s));
139 if (!grp) goto end_line;
140 gid = grp->gr_gid;
141 }
142 break;
143 }
144 // mode
145 case 1:
146 {
147 mode = strtoul(pos, &pos, 8);
148 if (pos!=end2) goto end_line;
149 goto found_device;
150 }
151 }
152 pos=end2;
153 }
154end_line:
155 // Did everything parse happily?
156 if (field && field!=3) error_exit("Bad line %d", line);
157
158 // Next line
159 pos = ++end;
160 }
161found_device:
162 munmap(conf, len);
163 }
164 close(fd);
165 }
166 }
167
168 sprintf(temp, "/dev/%s", device_name);
169 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
170 perror_exit("mknod %s failed", temp);
171
172 if (CFG_MDEV_CONF) chown(temp, uid, gid);
173}
174
175// Recursive search of /sys/block or /sys/class. path must be a writeable
176// buffer of size PATH_MAX containing the directory string to start at.
177
178static void find_dev(char *path)
179{
180 DIR *dir;
181 int len=strlen(path);
182
183 if (!(dir = opendir(path)))
184 perror_exit("No %s",path);
185
186 for (;;) {
187 struct dirent *entry = readdir(dir);
188
189 if (!entry) break;
190
191 // Skip "." and ".." (also skips hidden files, which is ok)
192
193 if (entry->d_name[0]=='.') continue;
194
195 if (entry->d_type == DT_DIR) {
196 snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name);
197 find_dev(path);
198 path[len] = 0;
199 }
200
201 // If there's a dev entry, mknod it
202
203 if (strcmp(entry->d_name, "dev")) make_device(path);
204 }
205
206 closedir(dir);
207}
208
209void mdev_main(void)
210{
211 if (toys.optflags) {
212 strcpy(toybuf, "/sys/block");
213 find_dev(toybuf);
214 strcpy(toybuf, "/sys/class");
215 find_dev(toybuf);
216 return;
217 }
218
219 // hotplug support goes here
220}