annotate toys/mdev.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents d51be130fda2
children 2a757e592ff7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
951110c37fee Add TOYFLAG_UMASK.
Rob Landley <rob@landley.net>
parents: 254
diff changeset
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
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;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 temp++;
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 {
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 char *regex = strndupa(pos, end2-pos);
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);
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 114
diff changeset
107
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 // 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
109 if (result || off.rm_so
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 || off.rm_eo!=strlen(device_name))
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 goto end_line;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
112
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 break;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 // uid:gid
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 case 2:
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 char *s2;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
119
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 // Find :
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 for(s = pos; s<end2 && *s!=':'; s++);
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 if (s==end2) goto end_line;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
123
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 // Parse UID
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 uid = strtoul(pos,&s2,10);
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 if (s!=s2) {
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 struct passwd *pass;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 pass = getpwnam(strndupa(pos, s-pos));
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 if (!pass) goto end_line;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 uid = pass->pw_uid;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 s++;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 // parse GID
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 gid = strtoul(s,&s2,10);
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 if (end2!=s2) {
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 struct group *grp;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 grp = getgrnam(strndupa(s, end2-s));
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 if (!grp) goto end_line;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 gid = grp->gr_gid;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 break;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 // mode
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 case 1:
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 mode = strtoul(pos, &pos, 8);
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 if (pos!=end2) goto end_line;
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 goto found_device;
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 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 pos=end2;
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 end_line:
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 // Did everything parse happily?
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 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
156
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 // Next line
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 pos = ++end;
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 found_device:
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 munmap(conf, len);
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 close(fd);
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
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 sprintf(temp, "/dev/%s", device_name);
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
329
6773399539aa Shut up some new aggressively stupid warnings gcc 4.3 produces.
Rob Landley <rob@landley.net>
parents: 292
diff changeset
171 // Dear gcc: shut up about ignoring the return value here. If it doesn't
6773399539aa Shut up some new aggressively stupid warnings gcc 4.3 produces.
Rob Landley <rob@landley.net>
parents: 292
diff changeset
172 // work, what exactly are we supposed to do about it?
6773399539aa Shut up some new aggressively stupid warnings gcc 4.3 produces.
Rob Landley <rob@landley.net>
parents: 292
diff changeset
173 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
174 }
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
175
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
176 static int callback(char *path, struct dirtree *node)
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 {
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
178 // 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
179 // get block devices out of /sys/block.)
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
180 if(!strcmp(node->name, "block")) return 1;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 114
diff changeset
181
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
182 // Does this directory have a "dev" entry in it?
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 if (S_ISDIR(node->st.st_mode) || S_ISLNK(node->st.st_mode)) {
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 char *dest = path+strlen(path);
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 strcpy(dest, "/dev");
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
186 if (!access(path, R_OK)) make_device(path);
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 *dest = 0;
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 114
diff changeset
189
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
190 // 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
191 // (mouse#, event#) or unnamed (every usb_* entry is called "device").
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
192 return node->depth>1;
114
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
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
195 void mdev_main(void)
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
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 // 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
198
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 if (toys.optflags) {
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
200 xchdir("/sys/class");
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 strcpy(toybuf, "/sys/class");
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
202 dirtree_read(toybuf, NULL, callback);
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 strcpy(toybuf+5, "block");
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 dirtree_read(toybuf, NULL, callback);
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 114
diff changeset
205 }
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
206 // if (toys.optflags) {
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
207 // strcpy(toybuf, "/sys/block");
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
208 // find_dev(toybuf);
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
209 // strcpy(toybuf, "/sys/class");
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
210 // find_dev(toybuf);
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
211 // return;
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
212 // }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 114
diff changeset
213
114
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 // hotplug support goes here
ce6956dfc0cf Add sync and an incomplete version of mdev.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 }