Mercurial > hg > toybox
comparison toys/mdev.c @ 114:ce6956dfc0cf
Add sync and an incomplete version of mdev.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 23 Apr 2007 15:45:55 -0400 |
parents | |
children | 1e8f4b05cb65 |
comparison
equal
deleted
inserted
replaced
113:1f7dcdef245c | 114:ce6956dfc0cf |
---|---|
1 /* vi:set ts=4: | |
2 * | |
3 * mdev - Mini udev for busybox | |
4 * | |
5 * Copyright 2005 Rob Landley <rob@landley.net> | |
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> | |
7 */ | |
8 | |
9 #include "toys.h" | |
10 #include "lib/xregcomp.h" | |
11 | |
12 // mknod in /dev based on a path like "/sys/block/hda/hda1" | |
13 static void make_device(char *path) | |
14 { | |
15 char *device_name, *s, *temp; | |
16 int major, minor, type, len, fd; | |
17 int mode = 0660; | |
18 uid_t uid = 0; | |
19 gid_t gid = 0; | |
20 | |
21 // Try to read major/minor string | |
22 | |
23 temp = path+strlen(path); | |
24 strcpy(temp, "/dev"); | |
25 fd = open(path, O_RDONLY); | |
26 *temp=0; | |
27 temp++; | |
28 len = read(fd, temp, 64); | |
29 close(fd); | |
30 if (len<1) return; | |
31 temp[len] = 0; | |
32 | |
33 // Determine device name, type, major and minor | |
34 | |
35 device_name = strrchr(path, '/') + 1; | |
36 type = path[5]=='c' ? S_IFCHR : S_IFBLK; | |
37 major = minor = 0; | |
38 sscanf(temp, "%u:%u", &major, &minor); | |
39 | |
40 // If we have a config file, look up permissions for this device | |
41 | |
42 if (CFG_MDEV_CONF) { | |
43 char *conf, *pos, *end; | |
44 | |
45 // mmap the config file | |
46 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) { | |
47 len = lseek(fd, 0, SEEK_END); | |
48 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); | |
49 if (conf) { | |
50 int line = 0; | |
51 | |
52 // Loop through lines in mmaped file | |
53 for (pos = conf; pos-conf<len;) { | |
54 int field; | |
55 char *end2; | |
56 | |
57 line++; | |
58 // find end of this line | |
59 for(end = pos; end-conf<len && *end!='\n'; end++); | |
60 | |
61 // Three fields: regex, uid:gid, mode | |
62 for (field = 3; field; field--) { | |
63 // Skip whitespace | |
64 while (pos<end && isspace(*pos)) pos++; | |
65 if (pos==end || *pos=='#') break; | |
66 for (end2 = pos; | |
67 end2<end && !isspace(*end2) && *end2!='#'; end2++); | |
68 switch(field) { | |
69 // Regex to match this device | |
70 case 3: | |
71 { | |
72 char *regex = strndupa(pos, end2-pos); | |
73 regex_t match; | |
74 regmatch_t off; | |
75 int result; | |
76 | |
77 // Is this it? | |
78 xregcomp(&match, regex, REG_EXTENDED); | |
79 result=regexec(&match, device_name, 1, &off, 0); | |
80 regfree(&match); | |
81 | |
82 // If not this device, skip rest of line | |
83 if (result || off.rm_so | |
84 || off.rm_eo!=strlen(device_name)) | |
85 goto end_line; | |
86 | |
87 break; | |
88 } | |
89 // uid:gid | |
90 case 2: | |
91 { | |
92 char *s2; | |
93 | |
94 // Find : | |
95 for(s = pos; s<end2 && *s!=':'; s++); | |
96 if (s==end2) goto end_line; | |
97 | |
98 // Parse UID | |
99 uid = strtoul(pos,&s2,10); | |
100 if (s!=s2) { | |
101 struct passwd *pass; | |
102 pass = getpwnam(strndupa(pos, s-pos)); | |
103 if (!pass) goto end_line; | |
104 uid = pass->pw_uid; | |
105 } | |
106 s++; | |
107 // parse GID | |
108 gid = strtoul(s,&s2,10); | |
109 if (end2!=s2) { | |
110 struct group *grp; | |
111 grp = getgrnam(strndupa(s, end2-s)); | |
112 if (!grp) goto end_line; | |
113 gid = grp->gr_gid; | |
114 } | |
115 break; | |
116 } | |
117 // mode | |
118 case 1: | |
119 { | |
120 mode = strtoul(pos, &pos, 8); | |
121 if (pos!=end2) goto end_line; | |
122 goto found_device; | |
123 } | |
124 } | |
125 pos=end2; | |
126 } | |
127 end_line: | |
128 // Did everything parse happily? | |
129 if (field && field!=3) error_exit("Bad line %d", line); | |
130 | |
131 // Next line | |
132 pos = ++end; | |
133 } | |
134 found_device: | |
135 munmap(conf, len); | |
136 } | |
137 close(fd); | |
138 } | |
139 } | |
140 | |
141 sprintf(temp, "/dev/%s", device_name); | |
142 umask(0); | |
143 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST) | |
144 perror_exit("mknod %s failed", temp); | |
145 | |
146 if (CFG_MDEV_CONF) chown(temp, uid, gid); | |
147 } | |
148 | |
149 // Recursive search of /sys/block or /sys/class. path must be a writeable | |
150 // buffer of size PATH_MAX containing the directory string to start at. | |
151 | |
152 static void find_dev(char *path) | |
153 { | |
154 DIR *dir; | |
155 int len=strlen(path); | |
156 | |
157 if (!(dir = opendir(path))) | |
158 perror_exit("No %s",path); | |
159 | |
160 for (;;) { | |
161 struct dirent *entry = readdir(dir); | |
162 | |
163 if (!entry) break; | |
164 | |
165 // Skip "." and ".." (also skips hidden files, which is ok) | |
166 | |
167 if (entry->d_name[0]=='.') continue; | |
168 | |
169 if (entry->d_type == DT_DIR) { | |
170 snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name); | |
171 find_dev(path); | |
172 path[len] = 0; | |
173 } | |
174 | |
175 // If there's a dev entry, mknod it | |
176 | |
177 if (strcmp(entry->d_name, "dev")) make_device(path); | |
178 } | |
179 | |
180 closedir(dir); | |
181 } | |
182 | |
183 int mdev_main(int argc, char *argv[]) | |
184 { | |
185 if (toys.optflags) { | |
186 strcpy(toybuf, "/sys/block"); | |
187 find_dev(toybuf); | |
188 strcpy(toybuf, "/sys/class"); | |
189 find_dev(toybuf); | |
190 return 0; | |
191 } | |
192 | |
193 // hotplug support goes here | |
194 | |
195 return 0; | |
196 } |