aboutsummaryrefslogtreecommitdiff
path: root/util-linux/mdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r--util-linux/mdev.c366
1 files changed, 201 insertions, 165 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index 0edaf1047..f0a885482 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -12,6 +12,8 @@
12#include "libbb.h" 12#include "libbb.h"
13#include "xregex.h" 13#include "xregex.h"
14 14
15#define ENABLE_FEATURE_MDEV_RENAME_REGEXP 1
16
15struct globals { 17struct globals {
16 int root_major, root_minor; 18 int root_major, root_minor;
17}; 19};
@@ -21,7 +23,21 @@ struct globals {
21 23
22#define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */ 24#define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */
23 25
26/* We use additional 64+ bytes in make_device() */
27#define SCRATCH_SIZE 80
28
29static char *next_field(char *s)
30{
31 char *end = skip_non_whitespace(s);
32 s = skip_whitespace(end);
33 *end = '\0';
34 if (*s == '\0')
35 s = NULL;
36 return s;
37}
38
24/* mknod in /dev based on a path like "/sys/block/hda/hda1" */ 39/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
40/* NB: "mdev -s" may call us many times, do not leak memory/fds! */
25static void make_device(char *path, int delete) 41static void make_device(char *path, int delete)
26{ 42{
27 const char *device_name; 43 const char *device_name;
@@ -29,7 +45,7 @@ static void make_device(char *path, int delete)
29 int mode = 0660; 45 int mode = 0660;
30 uid_t uid = 0; 46 uid_t uid = 0;
31 gid_t gid = 0; 47 gid_t gid = 0;
32 char *temp = path + strlen(path); 48 char *dev_maj_min = path + strlen(path);
33 char *command = NULL; 49 char *command = NULL;
34 char *alias = NULL; 50 char *alias = NULL;
35 51
@@ -42,156 +58,178 @@ static void make_device(char *path, int delete)
42 * also depend on path having writeable space after it. 58 * also depend on path having writeable space after it.
43 */ 59 */
44 if (!delete) { 60 if (!delete) {
45 strcat(path, "/dev"); 61 strcpy(dev_maj_min, "/dev");
46 len = open_read_close(path, temp + 1, 64); 62 len = open_read_close(path, dev_maj_min + 1, 64);
47 *temp++ = 0; 63 *dev_maj_min++ = '\0';
48 if (len < 1) { 64 if (len < 1) {
49 if (ENABLE_FEATURE_MDEV_EXEC) 65 if (!ENABLE_FEATURE_MDEV_EXEC)
50 /* no "dev" file, so just try to run script */
51 *temp = 0;
52 else
53 return; 66 return;
67 /* no "dev" file, so just try to run script */
68 *dev_maj_min = '\0';
54 } 69 }
55 } 70 }
56 71
57 /* Determine device name, type, major and minor */ 72 /* Determine device name, type, major and minor */
58 device_name = bb_basename(path); 73 device_name = bb_basename(path);
59 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK); 74 /* http://kernel.org/doc/pending/hotplug.txt says that only
75 * "/sys/block/..." is for block devices. "sys/bus" etc is not! */
76 type = (strncmp(&path[5], "block/", 6) == 0 ? S_IFBLK : S_IFCHR);
60 77
61 if (ENABLE_FEATURE_MDEV_CONF) { 78 if (ENABLE_FEATURE_MDEV_CONF) {
62 FILE *fp; 79 FILE *fp;
63 char *line, *vline; 80 char *line, *val, *next;
64 unsigned lineno = 0; 81 unsigned lineno = 0;
65 82
66 /* If we have a config file, look up the user settings */ 83 /* If we have config file, look up user settings */
67 fp = fopen_or_warn("/etc/mdev.conf", "r"); 84 fp = fopen_or_warn("/etc/mdev.conf", "r");
68 if (!fp) 85 if (!fp)
69 goto end_parse; 86 goto end_parse;
70 87
71 while ((vline = line = xmalloc_getline(fp)) != NULL) { 88 while ((line = xmalloc_getline(fp)) != NULL) {
72 int field; 89 regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
73
74 /* A pristine copy for command execution. */
75 char *orig_line;
76 if (ENABLE_FEATURE_MDEV_EXEC)
77 orig_line = xstrdup(line);
78 90
79 ++lineno; 91 ++lineno;
92 trim(line);
93 if (!line[0])
94 goto next_line;
95
96 /* Fields: regex uid:gid mode [alias] [cmd] */
97
98 /* 1st field: regex to match this device */
99 next = next_field(line);
100 {
101 regex_t match;
102 int result;
103
104 /* Is this it? */
105 xregcomp(&match, line, REG_EXTENDED);
106 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
107 regfree(&match);
108
109 //bb_error_msg("matches:");
110 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
111 // if (off[i].rm_so < 0) continue;
112 // bb_error_msg("match %d: '%.*s'\n", i,
113 // (int)(off[i].rm_eo - off[i].rm_so),
114 // device_name + off[i].rm_so);
115 //}
116
117 /* If not this device, skip rest of line */
118 /* (regexec returns whole pattern as "range" 0) */
119 if (result || off[0].rm_so || off[0].rm_eo != strlen(device_name))
120 goto next_line;
121 }
80 122
81 /* Three fields: regex, uid:gid, mode */ 123 /* This line matches: stop parsing the file
82 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_RENAME + ENABLE_FEATURE_MDEV_EXEC); ++field) { 124 * after parsing the rest of fields */
83
84 /* Find a non-empty field */
85 char *val;
86 do {
87 val = strtok(vline, " \t");
88 vline = NULL;
89 } while (val && !*val);
90 if (!val) {
91 if (field)
92 break;
93 else
94 goto next_line;
95 }
96
97 if (field == 0) {
98
99 /* Regex to match this device */
100 regex_t match;
101 regmatch_t off;
102 int result;
103
104 /* Is this it? */
105 xregcomp(&match, val, REG_EXTENDED);
106 result = regexec(&match, device_name, 1, &off, 0);
107 regfree(&match);
108
109 /* If not this device, skip rest of line */
110 if (result || off.rm_so || off.rm_eo != strlen(device_name))
111 goto next_line;
112
113 } else if (field == 1) {
114
115 /* uid:gid device ownership */
116 struct passwd *pass;
117 struct group *grp;
118
119 char *str_uid = val;
120 char *str_gid = strchr(val, ':');
121 if (str_gid)
122 *str_gid = '\0', ++str_gid;
123
124 /* Parse UID */
125 pass = getpwnam(str_uid);
126 if (pass)
127 uid = pass->pw_uid;
128 else
129 uid = strtoul(str_uid, NULL, 10);
130
131 /* parse GID */
132 grp = getgrnam(str_gid);
133 if (grp)
134 gid = grp->gr_gid;
135 else
136 gid = strtoul(str_gid, NULL, 10);
137
138 } else if (field == 2) {
139
140 /* Mode device permissions */
141 mode = strtoul(val, NULL, 8);
142
143 } else if (ENABLE_FEATURE_MDEV_RENAME && field == 3) {
144
145 if (*val != '>')
146 ++field;
147 else
148 alias = xstrdup(val + 1);
149
150 }
151
152 if (ENABLE_FEATURE_MDEV_EXEC && field == 3 + ENABLE_FEATURE_MDEV_RENAME) {
153 125
154 /* Optional command to run */ 126 /* 2nd field: uid:gid - device ownership */
155 const char *s = "@$*"; 127 if (!next) /* field must exist */
156 const char *s2 = strchr(s, *val); 128 bb_error_msg_and_die("bad line %u", lineno);
129 val = next;
130 next = next_field(val);
131 {
132 struct passwd *pass;
133 struct group *grp;
134 char *str_uid = val;
135 char *str_gid = strchrnul(val, ':');
136
137 if (*str_gid)
138 *str_gid++ = '\0';
139 /* Parse UID */
140 pass = getpwnam(str_uid);
141 if (pass)
142 uid = pass->pw_uid;
143 else
144 uid = strtoul(str_uid, NULL, 10);
145 /* Parse GID */
146 grp = getgrnam(str_gid);
147 if (grp)
148 gid = grp->gr_gid;
149 else
150 gid = strtoul(str_gid, NULL, 10);
151 }
157 152
158 if (!s2) { 153 /* 3rd field: mode - device permissions */
159 /* Force error */ 154 if (!next) /* field must exist */
160 field = 1; 155 bb_error_msg_and_die("bad line %u", lineno);
161 break; 156 val = next;
157 next = next_field(val);
158 mode = strtoul(val, NULL, 8);
159
160 /* 4th field (opt): >alias */
161 if (ENABLE_FEATURE_MDEV_RENAME) {
162 if (!next)
163 break;
164 if (*next == '>') {
165#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
166 char *s, *p;
167 unsigned i, n;
168#endif
169 val = next;
170 next = next_field(val);
171#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
172 /* substitute %1..9 with off[1..9], if any */
173 n = 0;
174 s = val;
175 while (*s && *s++ == '%')
176 n++;
177
178 p = alias = xzalloc(strlen(val) + n * strlen(device_name));
179 s = val + 1;
180 while (*s) {
181 *p = *s;
182 if ('%' == *s) {
183 i = (s[1] - '0');
184 if (i <= 9 && off[i].rm_so >= 0) {
185 n = off[i].rm_eo - off[i].rm_so;
186 strncpy(p, device_name + off[i].rm_so, n);
187 p += n - 1;
188 s++;
189 }
190 }
191 p++;
192 s++;
162 } 193 }
163 194#else
164 /* Correlate the position in the "@$*" with the delete 195 alias = xstrdup(val + 1);
165 * step so that we get the proper behavior. 196#endif
166 */
167 if ((s2 - s + 1) & (1 << delete))
168 command = xstrdup(orig_line + (val + 1 - line));
169 } 197 }
170 } 198 }
171 199
172 /* Did everything parse happily? */ 200 /* The rest (opt): command to run */
173 if (field <= 2) 201 if (!next)
174 bb_error_msg_and_die("bad line %u", lineno); 202 break;
175 203 val = next;
204 if (ENABLE_FEATURE_MDEV_EXEC) {
205 const char *s = "@$*";
206 const char *s2 = strchr(s, *val);
207
208 if (!s2)
209 bb_error_msg_and_die("bad line %u", lineno);
210
211 /* Correlate the position in the "@$*" with the delete
212 * step so that we get the proper behavior:
213 * @cmd: run on create
214 * $cmd: run on delete
215 * *cmd: run on both
216 */
217 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
218 command = xstrdup(val + 1);
219 }
220 }
221 /* end of field parsing */
222 break; /* we found matching line, stop */
176 next_line: 223 next_line:
177 free(line); 224 free(line);
178 if (ENABLE_FEATURE_MDEV_EXEC) 225 } /* end of "while line is read from /etc/mdev.conf" */
179 free(orig_line);
180 }
181
182 if (ENABLE_FEATURE_CLEAN_UP)
183 fclose(fp);
184 226
185 end_parse: /* nothing */ ; 227 free(line); /* in case we used "break" to get here */
228 fclose(fp);
186 } 229 }
230 end_parse:
187 231
188 if (!delete) { 232 if (!delete && sscanf(dev_maj_min, "%u:%u", &major, &minor) == 2) {
189 if (sscanf(temp, "%d:%d", &major, &minor) != 2) {
190 if (ENABLE_FEATURE_MDEV_EXEC)
191 goto skip_creation;
192 else
193 return;
194 }
195 233
196 if (ENABLE_FEATURE_MDEV_RENAME) 234 if (ENABLE_FEATURE_MDEV_RENAME)
197 unlink(device_name); 235 unlink(device_name);
@@ -208,39 +246,44 @@ static void make_device(char *path, int delete)
208 if (ENABLE_FEATURE_MDEV_RENAME && alias) { 246 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
209 char *dest; 247 char *dest;
210 248
211 temp = strrchr(alias, '/'); 249 /* ">bar/": rename to bar/device_name */
212 if (temp) { 250 /* ">bar[/]baz": rename to bar[/]baz */
213 if (temp[1] != '\0') 251 dest = strrchr(alias, '/');
214 /* given a file name, so rename it */ 252 if (dest) { /* ">bar/[baz]" ? */
215 *temp = '\0'; 253 *dest = '\0'; /* mkdir bar */
216 bb_make_directory(alias, 0755, FILEUTILS_RECUR); 254 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
217 dest = concat_path_file(alias, device_name); 255 *dest = '/';
218 } else 256 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
219 dest = alias; 257 dest = alias;
258 alias = concat_path_file(alias, device_name);
259 free(dest);
260 }
261 }
220 262
221 rename(device_name, dest); // TODO: xrename? 263 /* recreate device_name as a symlink to moved device node */
222 symlink(dest, device_name); 264 if (rename(device_name, alias) == 0) {
265 symlink(alias, device_name);
266 }
223 267
224 if (alias != dest) 268 free(alias);
225 free(alias);
226 free(dest);
227 } 269 }
228 } 270 }
229 skip_creation: /* nothing */ ;
230 } 271 }
272
231 if (ENABLE_FEATURE_MDEV_EXEC && command) { 273 if (ENABLE_FEATURE_MDEV_EXEC && command) {
232 /* setenv will leak memory, so use putenv */ 274 /* setenv will leak memory, use putenv/unsetenv/free */
233 char *s = xasprintf("MDEV=%s", device_name); 275 char *s = xasprintf("MDEV=%s", device_name);
234 putenv(s); 276 putenv(s);
235 if (system(command) == -1) 277 if (system(command) == -1)
236 bb_perror_msg_and_die("cannot run %s", command); 278 bb_perror_msg_and_die("can't run '%s'", command);
237 s[4] = '\0'; 279 s[4] = '\0';
238 unsetenv(s); 280 unsetenv(s);
239 free(s); 281 free(s);
240 free(command); 282 free(command);
241 } 283 }
284
242 if (delete) 285 if (delete)
243 remove_file(device_name, FILEUTILS_FORCE); 286 unlink(device_name);
244} 287}
245 288
246/* File callback for /sys/ traversal */ 289/* File callback for /sys/ traversal */
@@ -249,14 +292,15 @@ static int fileAction(const char *fileName,
249 void *userData, 292 void *userData,
250 int depth ATTRIBUTE_UNUSED) 293 int depth ATTRIBUTE_UNUSED)
251{ 294{
252 size_t len = strlen(fileName) - 4; 295 size_t len = strlen(fileName) - 4; /* can't underflow */
253 char *scratch = userData; 296 char *scratch = userData;
254 297
255 if (strcmp(fileName + len, "/dev")) 298 /* len check is for paranoid reasons */
299 if (strcmp(fileName + len, "/dev") || len >= PATH_MAX)
256 return FALSE; 300 return FALSE;
257 301
258 strcpy(scratch, fileName); 302 strcpy(scratch, fileName);
259 scratch[len] = 0; 303 scratch[len] = '\0';
260 make_device(scratch, 0); 304 make_device(scratch, 0);
261 305
262 return TRUE; 306 return TRUE;
@@ -287,12 +331,6 @@ static void load_firmware(const char *const firmware, const char *const sysfs_pa
287 int cnt; 331 int cnt;
288 int firmware_fd, loading_fd, data_fd; 332 int firmware_fd, loading_fd, data_fd;
289 333
290 /* check for $FIRMWARE from kernel */
291 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
292 * if (!firmware)
293 * return;
294 */
295
296 /* check for /lib/firmware/$FIRMWARE */ 334 /* check for /lib/firmware/$FIRMWARE */
297 xchdir("/lib/firmware"); 335 xchdir("/lib/firmware");
298 firmware_fd = xopen(firmware, O_RDONLY); 336 firmware_fd = xopen(firmware, O_RDONLY);
@@ -304,16 +342,15 @@ static void load_firmware(const char *const firmware, const char *const sysfs_pa
304 xchdir(sysfs_path); 342 xchdir(sysfs_path);
305 for (cnt = 0; cnt < 30; ++cnt) { 343 for (cnt = 0; cnt < 30; ++cnt) {
306 loading_fd = open("loading", O_WRONLY); 344 loading_fd = open("loading", O_WRONLY);
307 if (loading_fd == -1) 345 if (loading_fd != -1)
308 sleep(1); 346 goto loading;
309 else 347 sleep(1);
310 break;
311 } 348 }
312 if (loading_fd == -1) 349 goto out;
313 goto out;
314 350
351 loading:
315 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */ 352 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
316 if (write(loading_fd, "1", 1) != 1) 353 if (full_write(loading_fd, "1", 1) != 1)
317 goto out; 354 goto out;
318 355
319 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */ 356 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
@@ -324,9 +361,9 @@ static void load_firmware(const char *const firmware, const char *const sysfs_pa
324 361
325 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */ 362 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
326 if (cnt > 0) 363 if (cnt > 0)
327 write(loading_fd, "0", 1); 364 full_write(loading_fd, "0", 1);
328 else 365 else
329 write(loading_fd, "-1", 2); 366 full_write(loading_fd, "-1", 2);
330 367
331 out: 368 out:
332 if (ENABLE_FEATURE_CLEAN_UP) { 369 if (ENABLE_FEATURE_CLEAN_UP) {
@@ -341,16 +378,14 @@ int mdev_main(int argc, char **argv)
341{ 378{
342 char *action; 379 char *action;
343 char *env_path; 380 char *env_path;
344 RESERVE_CONFIG_BUFFER(temp,PATH_MAX); 381 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
345 382
346 xchdir("/dev"); 383 xchdir("/dev");
347 384
348 if (argc == 2 && !strcmp(argv[1],"-s")) { 385 if (argc == 2 && !strcmp(argv[1], "-s")) {
349
350 /* Scan: 386 /* Scan:
351 * mdev -s 387 * mdev -s
352 */ 388 */
353
354 struct stat st; 389 struct stat st;
355 390
356 xstat("/", &st); 391 xstat("/", &st);
@@ -366,26 +401,27 @@ int mdev_main(int argc, char **argv)
366 fileAction, dirAction, temp, 0); 401 fileAction, dirAction, temp, 0);
367 402
368 } else { 403 } else {
369
370 /* Hotplug: 404 /* Hotplug:
371 * env ACTION=... DEVPATH=... mdev 405 * env ACTION=... DEVPATH=... mdev
372 * ACTION can be "add" or "remove" 406 * ACTION can be "add" or "remove"
373 * DEVPATH is like "/block/sda" or "/class/input/mice" 407 * DEVPATH is like "/block/sda" or "/class/input/mice"
374 */ 408 */
375
376 action = getenv("ACTION"); 409 action = getenv("ACTION");
377 env_path = getenv("DEVPATH"); 410 env_path = getenv("DEVPATH");
378 if (!action || !env_path) 411 if (!action || !env_path)
379 bb_show_usage(); 412 bb_show_usage();
380 413
381 sprintf(temp, "/sys%s", env_path); 414 snprintf(temp, PATH_MAX, "/sys%s", env_path);
382 if (!strcmp(action, "remove")) 415 if (!strcmp(action, "remove"))
383 make_device(temp, 1); 416 make_device(temp, 1);
384 else if (!strcmp(action, "add")) { 417 else if (!strcmp(action, "add")) {
385 make_device(temp, 0); 418 make_device(temp, 0);
386 419
387 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) 420 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
388 load_firmware(getenv("FIRMWARE"), temp); 421 char *fw = getenv("FIRMWARE");
422 if (fw)
423 load_firmware(fw, temp);
424 }
389 } 425 }
390 } 426 }
391 427