Mercurial > hg > toybox
annotate toys/mdev.c @ 587:82ffae226c40
Convert another realpath use to xrealpath().
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 01 Jun 2012 17:59:11 -0500 |
parents | 4877cff01b25 |
children |
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 * |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
5 * Copyright 2005, 2008 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 |
257 | 10 USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK)) |
254
bf3f98a58ee2
Fix up (admittedly stubby) mdev so it's actually hooked up to the build.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
11 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
12 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
|
13 bool "mdev" |
535
d51be130fda2
More stabs at getting #includes right, and moving off of deprecated functions.
Rob Landley <rob@landley.net>
parents:
367
diff
changeset
|
14 default n |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
15 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
16 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
|
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 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
|
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 -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
|
21 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
22 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
|
23 bool "Configuration file for mdev" |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
24 default y |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
25 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
|
26 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
27 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
|
28 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
|
29 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 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
|
34 */ |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #include "toys.h" |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 #include "lib/xregcomp.h" |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 |
580
4877cff01b25
dirtree logic cleanup: switch DIRTREE_NORECURSE and DIRTREE_NOSAVE to DIRTREE_RECURSE and DIRTREE_SAVE.
Rob Landley <rob@landley.net>
parents:
577
diff
changeset
|
39 // todo, open() block devices to trigger partition scanning. |
4877cff01b25
dirtree logic cleanup: switch DIRTREE_NORECURSE and DIRTREE_NOSAVE to DIRTREE_RECURSE and DIRTREE_SAVE.
Rob Landley <rob@landley.net>
parents:
577
diff
changeset
|
40 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 // 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
|
42 static void make_device(char *path) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 char *device_name, *s, *temp; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 int major, minor, type, len, fd; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 int mode = 0660; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 uid_t uid = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 gid_t gid = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 // Try to read major/minor string |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
52 temp = strrchr(path, '/'); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 fd = open(path, O_RDONLY); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 *temp=0; |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
55 temp = toybuf; |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 len = read(fd, temp, 64); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 close(fd); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 if (len<1) return; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 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
|
60 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 // 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
|
62 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 device_name = strrchr(path, '/') + 1; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 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
|
65 major = minor = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 sscanf(temp, "%u:%u", &major, &minor); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 // 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
|
69 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 if (CFG_MDEV_CONF) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 char *conf, *pos, *end; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 // mmap the config file |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) { |
367
b8c8b4827e97
Use fdlength() instead of lseek(SEEK_END).
Rob Landley <rob@landley.net>
parents:
329
diff
changeset
|
75 len = fdlength(fd); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 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
|
77 if (conf) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 int line = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 // Loop through lines in mmaped file |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 for (pos = conf; pos-conf<len;) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 int field; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 char *end2; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
84 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 line++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 // find end of this line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 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
|
88 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 // Three fields: regex, uid:gid, mode |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 for (field = 3; field; field--) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 // Skip whitespace |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 while (pos<end && isspace(*pos)) pos++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 if (pos==end || *pos=='#') break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 for (end2 = pos; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 end2<end && !isspace(*end2) && *end2!='#'; end2++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 switch(field) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 // Regex to match this device |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 case 3: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 { |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
100 char *regex = strndup(pos, end2-pos); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 regex_t match; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 regmatch_t off; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 int result; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 // Is this it? |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 xregcomp(&match, regex, REG_EXTENDED); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
107 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
|
108 regfree(&match); |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
109 free(regex); |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
110 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 // 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
|
112 if (result || off.rm_so |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 || off.rm_eo!=strlen(device_name)) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 // uid:gid |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 case 2: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 char *s2; |
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 // Find : |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 for(s = pos; s<end2 && *s!=':'; s++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 if (s==end2) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 // Parse UID |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 uid = strtoul(pos,&s2,10); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 if (s!=s2) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 struct passwd *pass; |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
131 char *str = strndup(pos, s-pos); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
132 pass = getpwnam(str); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
133 free(str); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 if (!pass) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 uid = pass->pw_uid; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 s++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 // parse GID |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 gid = strtoul(s,&s2,10); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 if (end2!=s2) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 struct group *grp; |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
142 char *str = strndup(s, end2-s); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
143 grp = getgrnam(str); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
144 free(str); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 if (!grp) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 gid = grp->gr_gid; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 break; |
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 // mode |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 case 1: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 mode = strtoul(pos, &pos, 8); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 if (pos!=end2) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 goto found_device; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 pos=end2; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 end_line: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 // Did everything parse happily? |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 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
|
163 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 // Next line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 pos = ++end; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 found_device: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 munmap(conf, len); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 close(fd); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 } |
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 sprintf(temp, "/dev/%s", device_name); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 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
|
176 perror_exit("mknod %s failed", temp); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 |
329
6773399539aa
Shut up some new aggressively stupid warnings gcc 4.3 produces.
Rob Landley <rob@landley.net>
parents:
292
diff
changeset
|
178 if (CFG_MDEV_CONF) mode=chown(temp, uid, gid); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
181 static int callback(struct dirtree *node) |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 { |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
183 // Entries in /sys/class/block aren't char devices, so skip 'em. (We'll |
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
184 // get block devices out of /sys/block.) |
580
4877cff01b25
dirtree logic cleanup: switch DIRTREE_NORECURSE and DIRTREE_NOSAVE to DIRTREE_RECURSE and DIRTREE_SAVE.
Rob Landley <rob@landley.net>
parents:
577
diff
changeset
|
185 if(!strcmp(node->name, "block")) return 0; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
186 |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
187 // Does this directory have a "dev" entry in it? |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
188 // This is path based because the hotplug callbacks are |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
189 if (S_ISDIR(node->st.st_mode) || S_ISLNK(node->st.st_mode)) { |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
190 int len=4; |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
191 char *dev = dirtree_path(node, &len); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
192 strcpy(dev+len, "/dev"); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
193 if (!access(dev, R_OK)) make_device(dev); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
194 free(dev); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
195 } |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
196 |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
197 // Circa 2.6.25 the entries more than 2 deep are all either redundant |
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
198 // (mouse#, event#) or unnamed (every usb_* entry is called "device"). |
580
4877cff01b25
dirtree logic cleanup: switch DIRTREE_NORECURSE and DIRTREE_NOSAVE to DIRTREE_RECURSE and DIRTREE_SAVE.
Rob Landley <rob@landley.net>
parents:
577
diff
changeset
|
199 |
4877cff01b25
dirtree logic cleanup: switch DIRTREE_NORECURSE and DIRTREE_NOSAVE to DIRTREE_RECURSE and DIRTREE_SAVE.
Rob Landley <rob@landley.net>
parents:
577
diff
changeset
|
200 return (node->parent && node->parent->parent) ? 0 : DIRTREE_RECURSE; |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 |
254
bf3f98a58ee2
Fix up (admittedly stubby) mdev so it's actually hooked up to the build.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
203 void mdev_main(void) |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
204 { |
292
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
205 // Handle -s |
b4077be6c746
Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents:
257
diff
changeset
|
206 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 if (toys.optflags) { |
577
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
208 dirtree_read("/sys/class", callback); |
2a757e592ff7
Remove strndupa() gnu-ism at Georgi's suggestion, and adjust mdev to compile with new dirtree. (No idea if it works, this command was never finished and needs a lot more work.)
Rob Landley <rob@landley.net>
parents:
535
diff
changeset
|
209 dirtree_read("/sys/block", callback); |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
210 } |
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
211 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 // hotplug support goes here |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 } |