Mercurial > hg > toybox
annotate toys/mdev.c @ 233:d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
toys/*.c. Move relevant info into comment at the top of each toys/*.c. Also
convert more of Makefile into a thin wrapper around shell scripts that actually
do the work. (Makefile is only still there for the user interface.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 19 Jan 2008 17:08:39 -0600 |
parents | 30a6db5a95c2 |
children | bf3f98a58ee2 |
rev | line source |
---|---|
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi:set ts=4: |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
2 * |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * mdev - Mini udev for busybox |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
4 * |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
5 * Copyright 2005, 2007 Rob Landley <rob@landley.net> |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com> |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
7 * |
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
8 * Not in SUSv3. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
9 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
10 config MDEV |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
11 bool "mdev" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
12 default n |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
13 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
14 usage: mdev [-s] |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
15 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
16 Create devices in /dev using information from /sys. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
17 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
18 -s Scan all entries in /sys to populate /dev. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
19 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
20 config MDEV_CONF |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
21 bool "Configuration file for mdev" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
22 default n |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
23 depends on MDEV |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
24 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
25 The mdev config file (/etc/mdev.conf) contains lines that look like: |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
26 hd[a-z][0-9]* 0:3 660 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
27 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
28 Each line must contain three whitespace separated fields. The first |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
29 field is a regular expression matching one or more device names, and |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
30 the second and third fields are uid:gid and file permissions for |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
31 matching devies. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
32 */ |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 #include "toys.h" |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 #include "lib/xregcomp.h" |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 // mknod in /dev based on a path like "/sys/block/hda/hda1" |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 static void make_device(char *path) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 char *device_name, *s, *temp; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 int major, minor, type, len, fd; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 int mode = 0660; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 uid_t uid = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 gid_t gid = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 // Try to read major/minor string |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 temp = path+strlen(path); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 strcpy(temp, "/dev"); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 fd = open(path, O_RDONLY); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 *temp=0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 temp++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 len = read(fd, temp, 64); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 close(fd); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 if (len<1) return; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 temp[len] = 0; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
57 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 // Determine device name, type, major and minor |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
59 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 device_name = strrchr(path, '/') + 1; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 type = path[5]=='c' ? S_IFCHR : S_IFBLK; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 major = minor = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 sscanf(temp, "%u:%u", &major, &minor); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 // If we have a config file, look up permissions for this device |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
66 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 if (CFG_MDEV_CONF) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 char *conf, *pos, *end; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 // mmap the config file |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 len = lseek(fd, 0, SEEK_END); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 if (conf) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 int line = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 // Loop through lines in mmaped file |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 for (pos = conf; pos-conf<len;) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 int field; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 char *end2; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
81 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 line++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 // find end of this line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 for(end = pos; end-conf<len && *end!='\n'; end++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 // Three fields: regex, uid:gid, mode |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 for (field = 3; field; field--) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 // Skip whitespace |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 while (pos<end && isspace(*pos)) pos++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 if (pos==end || *pos=='#') break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 for (end2 = pos; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 end2<end && !isspace(*end2) && *end2!='#'; end2++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 switch(field) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 // Regex to match this device |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 case 3: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 char *regex = strndupa(pos, end2-pos); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 regex_t match; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 regmatch_t off; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 int result; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 // Is this it? |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 xregcomp(&match, regex, REG_EXTENDED); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 result=regexec(&match, device_name, 1, &off, 0); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 regfree(&match); |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
106 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 // If not this device, skip rest of line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 if (result || off.rm_so |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 || off.rm_eo!=strlen(device_name)) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 // uid:gid |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 case 2: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 char *s2; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 // Find : |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 for(s = pos; s<end2 && *s!=':'; s++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 if (s==end2) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 // Parse UID |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 uid = strtoul(pos,&s2,10); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 if (s!=s2) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 struct passwd *pass; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 pass = getpwnam(strndupa(pos, s-pos)); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 if (!pass) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 uid = pass->pw_uid; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 s++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 // parse GID |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 gid = strtoul(s,&s2,10); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 if (end2!=s2) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 struct group *grp; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 grp = getgrnam(strndupa(s, end2-s)); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 if (!grp) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 gid = grp->gr_gid; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
142 // mode |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 case 1: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 mode = strtoul(pos, &pos, 8); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 if (pos!=end2) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 goto found_device; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 pos=end2; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 end_line: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 // Did everything parse happily? |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 if (field && field!=3) error_exit("Bad line %d", line); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 // Next line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 pos = ++end; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 found_device: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 munmap(conf, len); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 close(fd); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 sprintf(temp, "/dev/%s", device_name); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 umask(0); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 perror_exit("mknod %s failed", temp); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 if (CFG_MDEV_CONF) chown(temp, uid, gid); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
172 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
174 // Recursive search of /sys/block or /sys/class. path must be a writeable |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 // buffer of size PATH_MAX containing the directory string to start at. |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 static void find_dev(char *path) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
178 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 DIR *dir; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 int len=strlen(path); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
181 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 if (!(dir = opendir(path))) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
183 perror_exit("No %s",path); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
184 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
185 for (;;) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 struct dirent *entry = readdir(dir); |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
187 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
188 if (!entry) break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
189 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
190 // Skip "." and ".." (also skips hidden files, which is ok) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
191 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 if (entry->d_name[0]=='.') continue; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 if (entry->d_type == DT_DIR) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
195 snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 find_dev(path); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
197 path[len] = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 } |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
199 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 // If there's a dev entry, mknod it |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
201 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 if (strcmp(entry->d_name, "dev")) make_device(path); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
203 } |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
204 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 closedir(dir); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
208 void mdev_main(int argc, char *argv[]) |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 if (toys.optflags) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 strcpy(toybuf, "/sys/block"); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 find_dev(toybuf); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 strcpy(toybuf, "/sys/class"); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
214 find_dev(toybuf); |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
215 return; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
216 } |
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
217 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 // hotplug support goes here |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 } |