annotate toys/mdev.c @ 114:ce6956dfc0cf

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