Mercurial > hg > toybox
annotate toys/other/mdev.c @ 656:6df4ccc0acbe
Regularize command headers, update links to standards documents.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 25 Aug 2012 18:08:51 -0500 |
parents | 2986aa63a021 |
children | 786841fdb1e0 |
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 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
3 * mdev.c - Populate /dev directory and handle hotplug events |
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> |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
7 |
257 | 8 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
|
9 |
233
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" |
535
d51be130fda2
More stabs at getting #includes right, and moving off of deprecated functions.
Rob Landley <rob@landley.net>
parents:
367
diff
changeset
|
12 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
|
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" |
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
|
22 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
|
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 |
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
|
37 // 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
|
38 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 // 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
|
40 static void make_device(char *path) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 char *device_name, *s, *temp; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 int major, minor, type, len, fd; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 int mode = 0660; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 uid_t uid = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 gid_t gid = 0; |
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 // Try to read major/minor string |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 |
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
|
50 temp = strrchr(path, '/'); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 fd = open(path, O_RDONLY); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 *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
|
53 temp = toybuf; |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 len = read(fd, temp, 64); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 close(fd); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 if (len<1) return; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 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
|
58 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 // 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
|
60 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 device_name = strrchr(path, '/') + 1; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 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
|
63 major = minor = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 sscanf(temp, "%u:%u", &major, &minor); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 // 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
|
67 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 if (CFG_MDEV_CONF) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 char *conf, *pos, *end; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 // mmap the config file |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 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
|
73 len = fdlength(fd); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 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
|
75 if (conf) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 int line = 0; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 // Loop through lines in mmaped file |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 for (pos = conf; pos-conf<len;) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 int field; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 char *end2; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
82 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 line++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 // find end of this line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 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
|
86 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 // Three fields: regex, uid:gid, mode |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 for (field = 3; field; field--) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 // Skip whitespace |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 while (pos<end && isspace(*pos)) pos++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 if (pos==end || *pos=='#') break; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 for (end2 = pos; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 end2<end && !isspace(*end2) && *end2!='#'; end2++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 switch(field) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 // Regex to match this device |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 case 3: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 { |
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
|
98 char *regex = strndup(pos, end2-pos); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 regex_t match; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 regmatch_t off; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 int result; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 // Is this it? |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 xregcomp(&match, regex, REG_EXTENDED); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 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
|
106 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
|
107 free(regex); |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
108 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 // 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
|
110 if (result || off.rm_so |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 || off.rm_eo!=strlen(device_name)) |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 goto end_line; |
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 break; |
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 // uid:gid |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 case 2: |
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 char *s2; |
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 // Find : |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 for(s = pos; s<end2 && *s!=':'; s++); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 if (s==end2) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 // Parse UID |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 uid = strtoul(pos,&s2,10); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 if (s!=s2) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 free(str); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 if (!pass) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 uid = pass->pw_uid; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 s++; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 // parse GID |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 gid = strtoul(s,&s2,10); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 if (end2!=s2) { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 free(str); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 if (!grp) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 gid = grp->gr_gid; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 break; |
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 // mode |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 case 1: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 { |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 mode = strtoul(pos, &pos, 8); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 if (pos!=end2) goto end_line; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 goto found_device; |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 } |
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 pos=end2; |
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 end_line: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 // Did everything parse happily? |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 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
|
161 |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 // Next line |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 pos = ++end; |
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 found_device: |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 munmap(conf, len); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 close(fd); |
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 } |
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 sprintf(temp, "/dev/%s", device_name); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 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
|
174 perror_exit("mknod %s failed", temp); |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 |
329
6773399539aa
Shut up some new aggressively stupid warnings gcc 4.3 produces.
Rob Landley <rob@landley.net>
parents:
292
diff
changeset
|
176 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
|
177 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
178 |
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
|
179 static int callback(struct dirtree *node) |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 { |
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
|
181 // 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
|
182 // 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
|
183 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
|
184 |
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
|
185 // 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
|
186 // 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 free(dev); |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 } |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
194 |
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
|
195 // 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
|
196 // (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
|
197 |
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
|
198 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
|
199 } |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 |
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
|
201 void mdev_main(void) |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 { |
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
|
203 // 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
|
204 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 } |
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
114
diff
changeset
|
209 |
114
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 // hotplug support goes here |
ce6956dfc0cf
Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 } |