]>
Commit | Line | Data |
---|---|---|
2232cac8 GKH |
1 | /* |
2 | * namedev.c | |
3 | * | |
4 | * Userspace devfs | |
5 | * | |
6 | * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> | |
7 | * | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation version 2 of the License. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | * | |
22 | */ | |
23 | ||
5ef7b799 | 24 | /* define this to enable parsing debugging */ |
a4318095 | 25 | /* #define DEBUG_PARSER */ |
149f2106 | 26 | |
2232cac8 GKH |
27 | #include <stddef.h> |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <stdio.h> | |
31 | #include <fcntl.h> | |
32 | #include <ctype.h> | |
33 | #include <unistd.h> | |
34 | #include <errno.h> | |
c27e6911 | 35 | #include <sys/wait.h> |
2232cac8 GKH |
36 | |
37 | #include "list.h" | |
38 | #include "udev.h" | |
39 | #include "udev_version.h" | |
40 | #include "namedev.h" | |
185a35a4 | 41 | #include "libsysfs/libsysfs.h" |
2023350e | 42 | #include "klibc_fixups.h" |
2232cac8 GKH |
43 | |
44 | #define TYPE_LABEL "LABEL" | |
45 | #define TYPE_NUMBER "NUMBER" | |
46 | #define TYPE_TOPOLOGY "TOPOLOGY" | |
47 | #define TYPE_REPLACE "REPLACE" | |
c27e6911 | 48 | #define TYPE_CALLOUT "CALLOUT" |
bc434511 | 49 | #define CALLOUT_MAXARG 8 |
2232cac8 | 50 | |
2232cac8 GKH |
51 | static LIST_HEAD(config_device_list); |
52 | ||
c124eafa KS |
53 | /* s2 may end with '*' to match everything */ |
54 | static int strncmp_wildcard(char *s1, char *s2, int max) | |
55 | { | |
56 | int len = strlen(s2); | |
57 | if (len > max) | |
58 | len = max; | |
59 | if (s2[len-1] == '*') | |
60 | len--; | |
61 | else | |
62 | len = max; | |
63 | return strncmp(s1, s2, len); | |
64 | } | |
65 | ||
469c7cff GKH |
66 | static void dump_dev(struct config_device *dev) |
67 | { | |
68 | switch (dev->type) { | |
69 | case KERNEL_NAME: | |
8f43a65e KS |
70 | dbg_parse("KERNEL name='%s' ," |
71 | "owner='%s', group='%s', mode=%#o", | |
72 | dev->name, dev->owner, dev->group, dev->mode); | |
469c7cff GKH |
73 | break; |
74 | case LABEL: | |
8f43a65e KS |
75 | dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', " |
76 | "owner='%s', group='%s', mode=%#o", | |
77 | dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value, | |
78 | dev->owner, dev->group, dev->mode); | |
469c7cff GKH |
79 | break; |
80 | case NUMBER: | |
8f43a65e KS |
81 | dbg_parse("NUMBER name='%s', bus='%s', id='%s', " |
82 | "owner='%s', group='%s', mode=%#o", | |
83 | dev->name, dev->bus, dev->id, | |
84 | dev->owner, dev->group, dev->mode); | |
469c7cff GKH |
85 | break; |
86 | case TOPOLOGY: | |
8f43a65e KS |
87 | dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', " |
88 | "owner='%s', group='%s', mode=%#o", | |
89 | dev->name, dev->bus, dev->place, | |
90 | dev->owner, dev->group, dev->mode); | |
469c7cff GKH |
91 | break; |
92 | case REPLACE: | |
8f43a65e KS |
93 | dbg_parse("REPLACE name=%s, kernel_name=%s, " |
94 | "owner='%s', group='%s', mode=%#o", | |
95 | dev->name, dev->kernel_name, | |
96 | dev->owner, dev->group, dev->mode); | |
469c7cff | 97 | break; |
c27e6911 | 98 | case CALLOUT: |
53dc383e | 99 | dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', " |
8f43a65e | 100 | "owner='%s', group='%s', mode=%#o", |
53dc383e | 101 | dev->name, dev->bus, dev->exec_program, dev->id, |
8f43a65e | 102 | dev->owner, dev->group, dev->mode); |
c27e6911 | 103 | break; |
469c7cff | 104 | default: |
8f43a65e | 105 | dbg_parse("unknown type of method"); |
469c7cff GKH |
106 | } |
107 | } | |
108 | ||
2232cac8 GKH |
109 | #define copy_var(a, b, var) \ |
110 | if (b->var) \ | |
469c7cff | 111 | a->var = b->var; |
2232cac8 GKH |
112 | |
113 | #define copy_string(a, b, var) \ | |
114 | if (strlen(b->var)) \ | |
469c7cff | 115 | strcpy(a->var, b->var); |
2232cac8 GKH |
116 | |
117 | static int add_dev(struct config_device *new_dev) | |
118 | { | |
119 | struct list_head *tmp; | |
120 | struct config_device *tmp_dev; | |
121 | ||
09e52d51 | 122 | /* update the values if we already have the device */ |
2232cac8 GKH |
123 | list_for_each(tmp, &config_device_list) { |
124 | struct config_device *dev = list_entry(tmp, struct config_device, node); | |
c124eafa KS |
125 | if (strncmp_wildcard(dev->name, new_dev->name, sizeof(dev->name))) |
126 | continue; | |
137af0cc GKH |
127 | if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name))) |
128 | continue; | |
09e52d51 KS |
129 | copy_var(dev, new_dev, type); |
130 | copy_var(dev, new_dev, mode); | |
131 | copy_string(dev, new_dev, bus); | |
132 | copy_string(dev, new_dev, sysfs_file); | |
133 | copy_string(dev, new_dev, sysfs_value); | |
134 | copy_string(dev, new_dev, id); | |
135 | copy_string(dev, new_dev, place); | |
136 | copy_string(dev, new_dev, kernel_name); | |
137af0cc | 137 | copy_string(dev, new_dev, exec_program); |
09e52d51 KS |
138 | copy_string(dev, new_dev, owner); |
139 | copy_string(dev, new_dev, group); | |
140 | return 0; | |
2232cac8 GKH |
141 | } |
142 | ||
8f43a65e | 143 | /* not found, add new structure to the device list */ |
2232cac8 GKH |
144 | tmp_dev = malloc(sizeof(*tmp_dev)); |
145 | if (!tmp_dev) | |
146 | return -ENOMEM; | |
147 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
148 | list_add(&tmp_dev->node, &config_device_list); | |
469c7cff | 149 | //dump_dev(tmp_dev); |
2232cac8 GKH |
150 | return 0; |
151 | } | |
152 | ||
469c7cff GKH |
153 | static void dump_dev_list(void) |
154 | { | |
155 | struct list_head *tmp; | |
156 | ||
157 | list_for_each(tmp, &config_device_list) { | |
158 | struct config_device *dev = list_entry(tmp, struct config_device, node); | |
159 | dump_dev(dev); | |
160 | } | |
161 | } | |
2232cac8 | 162 | |
469c7cff GKH |
163 | static int get_pair(char **orig_string, char **left, char **right) |
164 | { | |
165 | char *temp; | |
166 | char *string = *orig_string; | |
167 | ||
70033702 AB |
168 | if (!string) |
169 | return -ENODEV; | |
170 | ||
469c7cff GKH |
171 | /* eat any whitespace */ |
172 | while (isspace(*string)) | |
173 | ++string; | |
174 | ||
175 | /* split based on '=' */ | |
176 | temp = strsep(&string, "="); | |
177 | *left = temp; | |
70033702 AB |
178 | if (!string) |
179 | return -ENODEV; | |
469c7cff GKH |
180 | |
181 | /* take the right side and strip off the '"' */ | |
182 | while (isspace(*string)) | |
183 | ++string; | |
184 | if (*string == '"') | |
185 | ++string; | |
70033702 AB |
186 | else |
187 | return -ENODEV; | |
188 | ||
469c7cff | 189 | temp = strsep(&string, "\""); |
70033702 AB |
190 | if (!string || *temp == '\0') |
191 | return -ENODEV; | |
469c7cff GKH |
192 | *right = temp; |
193 | *orig_string = string; | |
194 | ||
195 | return 0; | |
196 | } | |
2232cac8 | 197 | |
70033702 AB |
198 | static int get_value(const char *left, char **orig_string, char **ret_string) |
199 | { | |
200 | int retval; | |
201 | char *left_string; | |
202 | ||
203 | retval = get_pair(orig_string, &left_string, ret_string); | |
204 | if (retval) | |
205 | return retval; | |
206 | if (strcasecmp(left_string, left) != 0) | |
207 | return -ENODEV; | |
208 | return 0; | |
209 | } | |
210 | ||
2232cac8 GKH |
211 | static int namedev_init_config(void) |
212 | { | |
2232cac8 | 213 | char line[255]; |
70033702 | 214 | int lineno; |
2232cac8 GKH |
215 | char *temp; |
216 | char *temp2; | |
217 | char *temp3; | |
218 | FILE *fd; | |
219 | int retval = 0; | |
220 | struct config_device dev; | |
221 | ||
f7b4eca4 | 222 | dbg("opening '%s' to read as config", udev_config_filename); |
c056c514 | 223 | fd = fopen(udev_config_filename, "r"); |
2232cac8 | 224 | if (fd == NULL) { |
f7b4eca4 | 225 | dbg("can't open '%s'", udev_config_filename); |
2232cac8 GKH |
226 | return -ENODEV; |
227 | } | |
228 | ||
229 | /* loop through the whole file */ | |
70033702 | 230 | lineno = 0; |
2232cac8 GKH |
231 | while (1) { |
232 | /* get a line */ | |
233 | temp = fgets(line, sizeof(line), fd); | |
234 | if (temp == NULL) | |
70033702 AB |
235 | goto exit; |
236 | lineno++; | |
2232cac8 | 237 | |
f7b4eca4 | 238 | dbg_parse("read '%s'", temp); |
2232cac8 GKH |
239 | |
240 | /* eat the whitespace at the beginning of the line */ | |
241 | while (isspace(*temp)) | |
242 | ++temp; | |
243 | ||
8f43a65e | 244 | /* empty line? */ |
2232cac8 GKH |
245 | if (*temp == 0x00) |
246 | continue; | |
247 | ||
248 | /* see if this is a comment */ | |
249 | if (*temp == COMMENT_CHARACTER) | |
250 | continue; | |
251 | ||
469c7cff | 252 | memset(&dev, 0x00, sizeof(struct config_device)); |
2232cac8 GKH |
253 | |
254 | /* parse the line */ | |
255 | temp2 = strsep(&temp, ","); | |
256 | if (strcasecmp(temp2, TYPE_LABEL) == 0) { | |
257 | /* label type */ | |
258 | dev.type = LABEL; | |
259 | ||
260 | /* BUS="bus" */ | |
261 | retval = get_value("BUS", &temp, &temp3); | |
262 | if (retval) | |
70033702 AB |
263 | break; |
264 | strfieldcpy(dev.bus, temp3); | |
469c7cff GKH |
265 | |
266 | /* file="value" */ | |
267 | temp2 = strsep(&temp, ","); | |
268 | retval = get_pair(&temp, &temp2, &temp3); | |
269 | if (retval) | |
70033702 AB |
270 | break; |
271 | strfieldcpy(dev.sysfs_file, temp2); | |
272 | strfieldcpy(dev.sysfs_value, temp3); | |
469c7cff GKH |
273 | |
274 | /* NAME="new_name" */ | |
275 | temp2 = strsep(&temp, ","); | |
276 | retval = get_value("NAME", &temp, &temp3); | |
277 | if (retval) | |
70033702 AB |
278 | break; |
279 | strfieldcpy(dev.name, temp3); | |
469c7cff | 280 | |
8f43a65e | 281 | dbg_parse("LABEL name='%s', bus='%s', " |
53dc383e KS |
282 | "sysfs_file='%s', sysfs_value='%s'", |
283 | dev.name, dev.bus, dev.sysfs_file, | |
8f43a65e | 284 | dev.sysfs_value); |
2232cac8 GKH |
285 | } |
286 | ||
287 | if (strcasecmp(temp2, TYPE_NUMBER) == 0) { | |
288 | /* number type */ | |
289 | dev.type = NUMBER; | |
290 | ||
291 | /* BUS="bus" */ | |
292 | retval = get_value("BUS", &temp, &temp3); | |
293 | if (retval) | |
70033702 AB |
294 | break; |
295 | strfieldcpy(dev.bus, temp3); | |
469c7cff GKH |
296 | |
297 | /* ID="id" */ | |
298 | temp2 = strsep(&temp, ","); | |
cb08e0f2 | 299 | retval = get_value("ID", &temp, &temp3); |
469c7cff | 300 | if (retval) |
70033702 AB |
301 | break; |
302 | strfieldcpy(dev.id, temp3); | |
469c7cff GKH |
303 | |
304 | /* NAME="new_name" */ | |
305 | temp2 = strsep(&temp, ","); | |
306 | retval = get_value("NAME", &temp, &temp3); | |
307 | if (retval) | |
70033702 AB |
308 | break; |
309 | strfieldcpy(dev.name, temp3); | |
469c7cff | 310 | |
8f43a65e KS |
311 | dbg_parse("NUMBER name='%s', bus='%s', id='%s'", |
312 | dev.name, dev.bus, dev.id); | |
2232cac8 GKH |
313 | } |
314 | ||
315 | if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) { | |
316 | /* number type */ | |
317 | dev.type = TOPOLOGY; | |
318 | ||
319 | /* BUS="bus" */ | |
320 | retval = get_value("BUS", &temp, &temp3); | |
321 | if (retval) | |
70033702 AB |
322 | break; |
323 | strfieldcpy(dev.bus, temp3); | |
469c7cff GKH |
324 | |
325 | /* PLACE="place" */ | |
326 | temp2 = strsep(&temp, ","); | |
cb08e0f2 | 327 | retval = get_value("PLACE", &temp, &temp3); |
469c7cff | 328 | if (retval) |
70033702 AB |
329 | break; |
330 | strfieldcpy(dev.place, temp3); | |
469c7cff GKH |
331 | |
332 | /* NAME="new_name" */ | |
333 | temp2 = strsep(&temp, ","); | |
334 | retval = get_value("NAME", &temp, &temp3); | |
335 | if (retval) | |
70033702 AB |
336 | break; |
337 | strfieldcpy(dev.name, temp3); | |
469c7cff | 338 | |
8f43a65e KS |
339 | dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'", |
340 | dev.name, dev.bus, dev.place); | |
2232cac8 GKH |
341 | } |
342 | ||
343 | if (strcasecmp(temp2, TYPE_REPLACE) == 0) { | |
344 | /* number type */ | |
345 | dev.type = REPLACE; | |
346 | ||
347 | /* KERNEL="kernel_name" */ | |
348 | retval = get_value("KERNEL", &temp, &temp3); | |
349 | if (retval) | |
70033702 AB |
350 | break; |
351 | strfieldcpy(dev.kernel_name, temp3); | |
2232cac8 GKH |
352 | |
353 | /* NAME="new_name" */ | |
354 | temp2 = strsep(&temp, ","); | |
355 | retval = get_value("NAME", &temp, &temp3); | |
356 | if (retval) | |
70033702 AB |
357 | break; |
358 | strfieldcpy(dev.name, temp3); | |
8f43a65e KS |
359 | dbg_parse("REPLACE name='%s', kernel_name='%s'", |
360 | dev.name, dev.kernel_name); | |
2232cac8 | 361 | } |
c27e6911 PM |
362 | if (strcasecmp(temp2, TYPE_CALLOUT) == 0) { |
363 | /* number type */ | |
364 | dev.type = CALLOUT; | |
365 | ||
cb08e0f2 KS |
366 | /* BUS="bus" */ |
367 | retval = get_value("BUS", &temp, &temp3); | |
c27e6911 | 368 | if (retval) |
70033702 | 369 | break; |
cb08e0f2 | 370 | strfieldcpy(dev.bus, temp3); |
c27e6911 | 371 | |
cb08e0f2 | 372 | /* PROGRAM="executable" */ |
c27e6911 | 373 | temp2 = strsep(&temp, ","); |
cb08e0f2 | 374 | retval = get_value("PROGRAM", &temp, &temp3); |
c27e6911 | 375 | if (retval) |
70033702 | 376 | break; |
cb08e0f2 | 377 | strfieldcpy(dev.exec_program, temp3); |
c27e6911 PM |
378 | |
379 | /* ID="id" */ | |
380 | temp2 = strsep(&temp, ","); | |
381 | retval = get_value("ID", &temp, &temp3); | |
382 | if (retval) | |
70033702 AB |
383 | break; |
384 | strfieldcpy(dev.id, temp3); | |
c27e6911 PM |
385 | |
386 | /* NAME="new_name" */ | |
387 | temp2 = strsep(&temp, ","); | |
388 | retval = get_value("NAME", &temp, &temp3); | |
389 | if (retval) | |
70033702 AB |
390 | break; |
391 | strfieldcpy(dev.name, temp3); | |
8f43a65e KS |
392 | dbg_parse("CALLOUT name='%s', program='%s'", |
393 | dev.name, dev.exec_program); | |
c27e6911 | 394 | } |
2232cac8 GKH |
395 | |
396 | retval = add_dev(&dev); | |
397 | if (retval) { | |
398 | dbg("add_dev returned with error %d", retval); | |
399 | goto exit; | |
400 | } | |
401 | } | |
8f43a65e | 402 | dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename, |
70033702 | 403 | lineno, temp - line, temp); |
2232cac8 GKH |
404 | exit: |
405 | fclose(fd); | |
406 | return retval; | |
407 | } | |
408 | ||
409 | ||
410 | static int namedev_init_permissions(void) | |
411 | { | |
2232cac8 GKH |
412 | char line[255]; |
413 | char *temp; | |
414 | char *temp2; | |
415 | FILE *fd; | |
416 | int retval = 0; | |
417 | struct config_device dev; | |
418 | ||
f7b4eca4 | 419 | dbg("opening '%s' to read as permissions config", udev_config_permission_filename); |
c056c514 | 420 | fd = fopen(udev_config_permission_filename, "r"); |
2232cac8 | 421 | if (fd == NULL) { |
f7b4eca4 | 422 | dbg("can't open '%s'", udev_config_permission_filename); |
2232cac8 GKH |
423 | return -ENODEV; |
424 | } | |
425 | ||
426 | /* loop through the whole file */ | |
427 | while (1) { | |
2232cac8 GKH |
428 | temp = fgets(line, sizeof(line), fd); |
429 | if (temp == NULL) | |
430 | break; | |
431 | ||
f7b4eca4 | 432 | dbg_parse("read '%s'", temp); |
2232cac8 GKH |
433 | |
434 | /* eat the whitespace at the beginning of the line */ | |
435 | while (isspace(*temp)) | |
436 | ++temp; | |
437 | ||
8f43a65e | 438 | /* empty line? */ |
2232cac8 GKH |
439 | if (*temp == 0x00) |
440 | continue; | |
441 | ||
442 | /* see if this is a comment */ | |
443 | if (*temp == COMMENT_CHARACTER) | |
444 | continue; | |
445 | ||
446 | memset(&dev, 0x00, sizeof(dev)); | |
447 | ||
448 | /* parse the line */ | |
449 | temp2 = strsep(&temp, ":"); | |
04a81cac | 450 | if (!temp2) { |
f7b4eca4 | 451 | dbg("cannot parse line '%s'", line); |
04a81cac MI |
452 | continue; |
453 | } | |
9d496c74 | 454 | strncpy(dev.name, temp2, sizeof(dev.name)); |
2232cac8 GKH |
455 | |
456 | temp2 = strsep(&temp, ":"); | |
04a81cac | 457 | if (!temp2) { |
f7b4eca4 | 458 | dbg("cannot parse line '%s'", line); |
04a81cac MI |
459 | continue; |
460 | } | |
9d496c74 | 461 | strncpy(dev.owner, temp2, sizeof(dev.owner)); |
2232cac8 GKH |
462 | |
463 | temp2 = strsep(&temp, ":"); | |
04a81cac | 464 | if (!temp2) { |
f7b4eca4 | 465 | dbg("cannot parse line '%s'", line); |
04a81cac MI |
466 | continue; |
467 | } | |
9d496c74 | 468 | strncpy(dev.group, temp2, sizeof(dev.owner)); |
2232cac8 | 469 | |
befd83cc | 470 | if (!temp) { |
471 | dbg("cannot parse line: %s", line); | |
472 | continue; | |
473 | } | |
9d496c74 | 474 | dev.mode = strtol(temp, NULL, 8); |
2232cac8 | 475 | |
8f43a65e KS |
476 | dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", |
477 | dev.name, dev.owner, dev.group, | |
478 | dev.mode); | |
2232cac8 GKH |
479 | retval = add_dev(&dev); |
480 | if (retval) { | |
481 | dbg("add_dev returned with error %d", retval); | |
482 | goto exit; | |
483 | } | |
484 | } | |
485 | ||
486 | exit: | |
487 | fclose(fd); | |
488 | return retval; | |
489 | } | |
490 | ||
c2405f50 | 491 | static mode_t get_default_mode(struct sysfs_class_device *class_dev) |
185a35a4 | 492 | { |
19dc5d4c GKH |
493 | /* just default everyone to rw for the world! */ |
494 | return 0666; | |
185a35a4 GKH |
495 | } |
496 | ||
f3b04a2e GKH |
497 | static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev) |
498 | { | |
499 | char *dig; | |
500 | ||
501 | /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */ | |
502 | dig = class_dev->name + strlen(class_dev->name); | |
503 | while (isdigit(*(dig-1))) | |
504 | dig--; | |
505 | strfieldcpy(udev->kernel_number, dig); | |
f7b4eca4 | 506 | dbg("kernel_number='%s'", udev->kernel_number); |
f3b04a2e GKH |
507 | } |
508 | ||
509 | static void apply_format(struct udevice *udev, unsigned char *string) | |
510 | { | |
511 | char name[NAME_SIZE]; | |
512 | char *pos; | |
513 | ||
514 | while (1) { | |
515 | pos = strchr(string, '%'); | |
516 | ||
517 | if (pos) { | |
518 | strfieldcpy(name, pos+2); | |
519 | *pos = 0x00; | |
520 | switch (pos[1]) { | |
521 | case 'b': | |
522 | if (strlen(udev->bus_id) == 0) | |
523 | break; | |
6968d494 | 524 | strcat(pos, udev->bus_id); |
f3b04a2e GKH |
525 | dbg("substitute bus_id '%s'", udev->bus_id); |
526 | break; | |
527 | case 'n': | |
528 | if (strlen(udev->kernel_number) == 0) | |
529 | break; | |
530 | strcat(pos, udev->kernel_number); | |
531 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
532 | break; | |
5c6f0f14 AB |
533 | case 'D': |
534 | if (strlen(udev->kernel_number) == 0) { | |
535 | strcat(pos, "disk"); | |
536 | break; | |
537 | } | |
538 | strcat(pos, "part"); | |
539 | strcat(pos, udev->kernel_number); | |
540 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
541 | break; | |
f3b04a2e GKH |
542 | case 'm': |
543 | sprintf(pos, "%u", udev->minor); | |
544 | dbg("substitute minor number '%u'", udev->minor); | |
545 | break; | |
546 | case 'M': | |
547 | sprintf(pos, "%u", udev->major); | |
548 | dbg("substitute major number '%u'", udev->major); | |
549 | break; | |
550 | case 'c': | |
551 | if (strlen(udev->callout_value) == 0) | |
552 | break; | |
553 | strcat(pos, udev->callout_value); | |
554 | dbg("substitute callout output '%s'", udev->callout_value); | |
555 | break; | |
556 | default: | |
557 | dbg("unknown substitution type '%%%c'", pos[1]); | |
558 | break; | |
559 | } | |
560 | strcat(string, name); | |
561 | } else | |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
19dc5d4c | 566 | |
c27e6911 PM |
567 | static int exec_callout(struct config_device *dev, char *value, int len) |
568 | { | |
569 | int retval; | |
570 | int res; | |
571 | int status; | |
572 | int fds[2]; | |
573 | pid_t pid; | |
574 | int value_set = 0; | |
575 | char buffer[256]; | |
bc434511 KS |
576 | char *arg; |
577 | char *args[CALLOUT_MAXARG]; | |
578 | int i; | |
c27e6911 | 579 | |
8f43a65e | 580 | dbg("callout to '%s'", dev->exec_program); |
c27e6911 PM |
581 | retval = pipe(fds); |
582 | if (retval != 0) { | |
583 | dbg("pipe failed"); | |
584 | return -1; | |
585 | } | |
586 | pid = fork(); | |
587 | if (pid == -1) { | |
588 | dbg("fork failed"); | |
589 | return -1; | |
590 | } | |
591 | ||
592 | if (pid == 0) { | |
8f43a65e | 593 | /* child */ |
c27e6911 PM |
594 | close(STDOUT_FILENO); |
595 | dup(fds[1]); /* dup write side of pipe to STDOUT */ | |
bc434511 KS |
596 | if (strchr(dev->exec_program, ' ')) { |
597 | /* callout with arguments */ | |
598 | arg = dev->exec_program; | |
599 | for (i=0; i < CALLOUT_MAXARG-1; i++) { | |
600 | args[i] = strsep(&arg, " "); | |
601 | if (args[i] == NULL) | |
602 | break; | |
603 | } | |
604 | if (args[i]) { | |
53dc383e | 605 | dbg("too many args - %d", i); |
bc434511 KS |
606 | args[i] = NULL; |
607 | } | |
608 | retval = execve(args[0], args, main_envp); | |
609 | } else { | |
610 | retval = execve(dev->exec_program, main_argv, main_envp); | |
611 | } | |
c27e6911 PM |
612 | if (retval != 0) { |
613 | dbg("child execve failed"); | |
614 | exit(1); | |
615 | } | |
616 | return -1; /* avoid compiler warning */ | |
617 | } else { | |
8f43a65e | 618 | /* parent reads from fds[0] */ |
c27e6911 PM |
619 | close(fds[1]); |
620 | retval = 0; | |
621 | while (1) { | |
622 | res = read(fds[0], buffer, sizeof(buffer) - 1); | |
623 | if (res <= 0) | |
624 | break; | |
625 | buffer[res] = '\0'; | |
626 | if (res > len) { | |
f7b4eca4 | 627 | dbg("callout len %d too short", len); |
c27e6911 PM |
628 | retval = -1; |
629 | } | |
630 | if (value_set) { | |
631 | dbg("callout value already set"); | |
632 | retval = -1; | |
633 | } else { | |
634 | value_set = 1; | |
635 | strncpy(value, buffer, len); | |
636 | } | |
637 | } | |
bc434511 | 638 | dbg("callout returned '%s'", value); |
c27e6911 PM |
639 | close(fds[0]); |
640 | res = wait(&status); | |
641 | if (res < 0) { | |
642 | dbg("wait failed result %d", res); | |
643 | retval = -1; | |
644 | } | |
645 | ||
1e959a4b | 646 | #ifndef __KLIBC__ |
c27e6911 PM |
647 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
648 | dbg("callout program status 0x%x", status); | |
649 | retval = -1; | |
650 | } | |
1e959a4b | 651 | #endif |
c27e6911 PM |
652 | } |
653 | return retval; | |
654 | } | |
655 | ||
137af0cc | 656 | static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
657 | { |
658 | struct config_device *dev; | |
659 | struct list_head *tmp; | |
120d45d0 GKH |
660 | |
661 | list_for_each(tmp, &config_device_list) { | |
662 | dev = list_entry(tmp, struct config_device, node); | |
663 | if (dev->type != CALLOUT) | |
664 | continue; | |
f3b04a2e | 665 | |
137af0cc GKH |
666 | if (sysfs_device) { |
667 | dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); | |
668 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) | |
669 | continue; | |
670 | } | |
671 | ||
f3b04a2e GKH |
672 | /* substitute anything that needs to be in the program name */ |
673 | apply_format(udev, dev->exec_program); | |
674 | if (exec_callout(dev, udev->callout_value, NAME_SIZE)) | |
120d45d0 | 675 | continue; |
f3b04a2e | 676 | if (strncmp_wildcard(udev->callout_value, dev->id, NAME_SIZE) != 0) |
120d45d0 | 677 | continue; |
70033702 | 678 | strfieldcpy(udev->name, dev->name); |
120d45d0 GKH |
679 | if (dev->mode != 0) { |
680 | udev->mode = dev->mode; | |
70033702 AB |
681 | strfieldcpy(udev->owner, dev->owner); |
682 | strfieldcpy(udev->group, dev->group); | |
120d45d0 | 683 | } |
8f43a65e | 684 | dbg_parse("callout returned matching value '%s', '%s' becomes '%s'" |
f7b4eca4 | 685 | " - owner='%s', group='%s', mode=%#o", |
8f43a65e KS |
686 | dev->id, class_dev->name, udev->name, |
687 | dev->owner, dev->group, dev->mode); | |
120d45d0 GKH |
688 | return 0; |
689 | } | |
690 | return -ENODEV; | |
691 | } | |
692 | ||
ca1cc0fe GKH |
693 | static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
694 | { | |
695 | struct sysfs_attribute *tmpattr = NULL; | |
696 | struct config_device *dev; | |
697 | struct list_head *tmp; | |
ca1cc0fe GKH |
698 | |
699 | list_for_each(tmp, &config_device_list) { | |
700 | dev = list_entry(tmp, struct config_device, node); | |
701 | if (dev->type != LABEL) | |
702 | continue; | |
703 | ||
137af0cc GKH |
704 | if (sysfs_device) { |
705 | dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); | |
706 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) | |
707 | continue; | |
708 | } | |
709 | ||
8f43a65e | 710 | dbg_parse("look for device attribute '%s'", dev->sysfs_file); |
ca1cc0fe GKH |
711 | /* try to find the attribute in the class device directory */ |
712 | tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file); | |
713 | if (tmpattr) | |
714 | goto label_found; | |
715 | ||
716 | /* look in the class device directory if present */ | |
717 | if (sysfs_device) { | |
718 | tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file); | |
719 | if (tmpattr) | |
720 | goto label_found; | |
721 | } | |
722 | ||
723 | continue; | |
724 | ||
725 | label_found: | |
726 | tmpattr->value[strlen(tmpattr->value)-1] = 0x00; | |
8f43a65e KS |
727 | dbg_parse("compare attribute '%s' value '%s' with '%s'", |
728 | dev->sysfs_file, tmpattr->value, dev->sysfs_value); | |
ca1cc0fe GKH |
729 | if (strcmp(dev->sysfs_value, tmpattr->value) != 0) |
730 | continue; | |
731 | ||
70033702 | 732 | strfieldcpy(udev->name, dev->name); |
ca1cc0fe GKH |
733 | if (dev->mode != 0) { |
734 | udev->mode = dev->mode; | |
70033702 AB |
735 | strfieldcpy(udev->owner, dev->owner); |
736 | strfieldcpy(udev->group, dev->group); | |
ca1cc0fe | 737 | } |
8f43a65e KS |
738 | dbg_parse("found matching attribute '%s', '%s' becomes '%s' " |
739 | "- owner='%s', group='%s', mode=%#o", | |
740 | dev->sysfs_file, class_dev->name, udev->name, | |
741 | dev->owner, dev->group, dev->mode); | |
ca1cc0fe GKH |
742 | |
743 | return 0; | |
744 | } | |
745 | return -ENODEV; | |
746 | } | |
747 | ||
748 | static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) | |
749 | { | |
750 | struct config_device *dev; | |
751 | struct list_head *tmp; | |
752 | char path[SYSFS_PATH_MAX]; | |
753 | int found; | |
754 | char *temp = NULL; | |
755 | ||
756 | /* we have to have a sysfs device for NUMBER to work */ | |
757 | if (!sysfs_device) | |
758 | return -ENODEV; | |
759 | ||
760 | list_for_each(tmp, &config_device_list) { | |
761 | dev = list_entry(tmp, struct config_device, node); | |
762 | if (dev->type != NUMBER) | |
763 | continue; | |
764 | ||
137af0cc GKH |
765 | dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
766 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) | |
767 | continue; | |
768 | ||
ca1cc0fe | 769 | found = 0; |
70033702 | 770 | strfieldcpy(path, sysfs_device->path); |
ca1cc0fe | 771 | temp = strrchr(path, '/'); |
8f43a65e | 772 | dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
773 | if (strstr(temp, dev->id) != NULL) { |
774 | found = 1; | |
775 | } else { | |
776 | *temp = 0x00; | |
777 | temp = strrchr(path, '/'); | |
8f43a65e | 778 | dbg_parse("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
779 | if (strstr(temp, dev->id) != NULL) |
780 | found = 1; | |
781 | } | |
782 | if (!found) | |
783 | continue; | |
70033702 | 784 | strfieldcpy(udev->name, dev->name); |
ca1cc0fe GKH |
785 | if (dev->mode != 0) { |
786 | udev->mode = dev->mode; | |
70033702 AB |
787 | strfieldcpy(udev->owner, dev->owner); |
788 | strfieldcpy(udev->group, dev->group); | |
ca1cc0fe | 789 | } |
8f43a65e KS |
790 | dbg_parse("found matching id '%s', '%s' becomes '%s'" |
791 | " - owner='%s', group ='%s', mode=%#o", | |
792 | dev->id, class_dev->name, udev->name, | |
793 | dev->owner, dev->group, dev->mode); | |
ca1cc0fe GKH |
794 | return 0; |
795 | } | |
796 | return -ENODEV; | |
797 | } | |
798 | ||
799 | ||
8c51bbfe GKH |
800 | static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
801 | { | |
802 | struct config_device *dev; | |
803 | struct list_head *tmp; | |
804 | char path[SYSFS_PATH_MAX]; | |
805 | int found; | |
806 | char *temp = NULL; | |
807 | ||
808 | /* we have to have a sysfs device for TOPOLOGY to work */ | |
809 | if (!sysfs_device) | |
810 | return -ENODEV; | |
811 | ||
812 | list_for_each(tmp, &config_device_list) { | |
813 | dev = list_entry(tmp, struct config_device, node); | |
814 | if (dev->type != TOPOLOGY) | |
815 | continue; | |
816 | ||
137af0cc GKH |
817 | dbg_parse("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
818 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) | |
819 | continue; | |
820 | ||
20ff86bd | 821 | found = 0; |
70033702 | 822 | strfieldcpy(path, sysfs_device->path); |
8c51bbfe | 823 | temp = strrchr(path, '/'); |
8f43a65e | 824 | dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
825 | if (strstr(temp, dev->place) != NULL) { |
826 | found = 1; | |
827 | } else { | |
828 | *temp = 0x00; | |
829 | temp = strrchr(path, '/'); | |
8f43a65e | 830 | dbg_parse("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
831 | if (strstr(temp, dev->place) != NULL) |
832 | found = 1; | |
833 | } | |
834 | if (!found) | |
835 | continue; | |
836 | ||
70033702 | 837 | strfieldcpy(udev->name, dev->name); |
8c51bbfe GKH |
838 | if (dev->mode != 0) { |
839 | udev->mode = dev->mode; | |
70033702 AB |
840 | strfieldcpy(udev->owner, dev->owner); |
841 | strfieldcpy(udev->group, dev->group); | |
8c51bbfe | 842 | } |
8f43a65e KS |
843 | dbg_parse("found matching place '%s', '%s' becomes '%s'" |
844 | " - owner='%s', group ='%s', mode=%#o", | |
845 | dev->place, class_dev->name, udev->name, | |
846 | dev->owner, dev->group, dev->mode); | |
8c51bbfe GKH |
847 | return 0; |
848 | } | |
849 | return -ENODEV; | |
850 | } | |
851 | ||
137af0cc | 852 | static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
853 | { |
854 | struct config_device *dev; | |
855 | struct list_head *tmp; | |
856 | ||
857 | list_for_each(tmp, &config_device_list) { | |
858 | dev = list_entry(tmp, struct config_device, node); | |
859 | if (dev->type != REPLACE) | |
860 | continue; | |
861 | ||
8f43a65e | 862 | dbg_parse("compare name '%s' with '%s'", |
120d45d0 GKH |
863 | dev->kernel_name, dev->name); |
864 | if (strcmp(dev->kernel_name, class_dev->name) != 0) | |
865 | continue; | |
866 | ||
70033702 | 867 | strfieldcpy(udev->name, dev->name); |
120d45d0 GKH |
868 | if (dev->mode != 0) { |
869 | udev->mode = dev->mode; | |
70033702 AB |
870 | strfieldcpy(udev->owner, dev->owner); |
871 | strfieldcpy(udev->group, dev->group); | |
120d45d0 | 872 | } |
8f43a65e KS |
873 | dbg_parse("found name, '%s' becomes '%s' - owner='%s', group='%s', mode = %#o", |
874 | dev->kernel_name, udev->name, | |
875 | dev->owner, dev->group, dev->mode); | |
120d45d0 GKH |
876 | |
877 | return 0; | |
878 | } | |
879 | return -ENODEV; | |
880 | } | |
881 | ||
09e52d51 KS |
882 | static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev) |
883 | { | |
884 | struct config_device *dev; | |
885 | struct list_head *tmp; | |
a1b8786a | 886 | int len; |
09e52d51 KS |
887 | |
888 | strfieldcpy(udev->name, class_dev->name); | |
c124eafa | 889 | /* look for permissions */ |
09e52d51 KS |
890 | list_for_each(tmp, &config_device_list) { |
891 | dev = list_entry(tmp, struct config_device, node); | |
a1b8786a | 892 | len = strlen(dev->name); |
c124eafa KS |
893 | if (strncmp_wildcard(class_dev->name, dev->name, sizeof(dev->name))) |
894 | continue; | |
09e52d51 | 895 | if (dev->mode != 0) { |
8f43a65e | 896 | dbg_parse("found permissions for '%s'", class_dev->name); |
09e52d51 KS |
897 | udev->mode = dev->mode; |
898 | strfieldcpy(udev->owner, dev->owner); | |
899 | strfieldcpy(udev->group, dev->group); | |
900 | } | |
901 | } | |
902 | } | |
903 | ||
9d496c74 | 904 | static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev) |
185a35a4 | 905 | { |
7bd22a78 GKH |
906 | struct sysfs_device *sysfs_device = NULL; |
907 | struct sysfs_class_device *class_dev_parent = NULL; | |
185a35a4 | 908 | int retval = 0; |
7bd22a78 | 909 | char *temp = NULL; |
185a35a4 | 910 | |
9d496c74 | 911 | udev->mode = 0; |
7bd22a78 GKH |
912 | |
913 | /* find the sysfs_device for this class device */ | |
8a0c11d3 | 914 | /* Wouldn't it really be nice if libsysfs could do this for us? */ |
03e64c8f | 915 | if (class_dev->sysdevice) { |
7bd22a78 GKH |
916 | sysfs_device = class_dev->sysdevice; |
917 | } else { | |
918 | /* bah, let's go backwards up a level to see if the device is there, | |
919 | * as block partitions don't point to the physical device. Need to fix that | |
920 | * up in the kernel... | |
921 | */ | |
922 | if (strstr(class_dev->path, "block")) { | |
f7b4eca4 | 923 | dbg_parse("looking at block device"); |
7bd22a78 GKH |
924 | if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { |
925 | char path[SYSFS_PATH_MAX]; | |
926 | ||
f7b4eca4 | 927 | dbg_parse("really is a partition"); |
70033702 | 928 | strfieldcpy(path, class_dev->path); |
7bd22a78 GKH |
929 | temp = strrchr(path, '/'); |
930 | *temp = 0x00; | |
931 | dbg_parse("looking for a class device at '%s'", path); | |
932 | class_dev_parent = sysfs_open_class_device(path); | |
933 | if (class_dev_parent == NULL) { | |
934 | dbg("sysfs_open_class_device at '%s' failed", path); | |
935 | } else { | |
f7b4eca4 | 936 | dbg_parse("class_dev_parent->name='%s'", class_dev_parent->name); |
7bd22a78 GKH |
937 | if (class_dev_parent->sysdevice) |
938 | sysfs_device = class_dev_parent->sysdevice; | |
939 | } | |
940 | } | |
941 | } | |
942 | } | |
943 | ||
944 | if (sysfs_device) { | |
8f43a65e KS |
945 | dbg_parse("sysfs_device->path='%s'", sysfs_device->path); |
946 | dbg_parse("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
137af0cc | 947 | dbg_parse("sysfs_device->bus='%s'", sysfs_device->bus); |
f3b04a2e | 948 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); |
03e64c8f | 949 | } else { |
5ef7b799 | 950 | dbg_parse("class_dev->name = '%s'", class_dev->name); |
03e64c8f | 951 | } |
120d45d0 | 952 | |
f3b04a2e GKH |
953 | build_kernel_number(class_dev, udev); |
954 | ||
120d45d0 | 955 | /* rules are looked at in priority order */ |
137af0cc | 956 | retval = do_callout(class_dev, udev, sysfs_device); |
120d45d0 | 957 | if (retval == 0) |
09e52d51 | 958 | goto found; |
120d45d0 | 959 | |
ca1cc0fe GKH |
960 | retval = do_label(class_dev, udev, sysfs_device); |
961 | if (retval == 0) | |
09e52d51 | 962 | goto found; |
ca1cc0fe GKH |
963 | |
964 | retval = do_number(class_dev, udev, sysfs_device); | |
965 | if (retval == 0) | |
09e52d51 | 966 | goto found; |
ca1cc0fe | 967 | |
8c51bbfe GKH |
968 | retval = do_topology(class_dev, udev, sysfs_device); |
969 | if (retval == 0) | |
09e52d51 | 970 | goto found; |
8c51bbfe | 971 | |
137af0cc | 972 | retval = do_replace(class_dev, udev, sysfs_device); |
120d45d0 | 973 | if (retval == 0) |
09e52d51 | 974 | goto found; |
120d45d0 | 975 | |
09e52d51 KS |
976 | do_kernelname(class_dev, udev); |
977 | goto done; | |
c2405f50 | 978 | |
09e52d51 | 979 | found: |
98b88dbf | 980 | /* substitute placeholder in NAME */ |
f3b04a2e | 981 | apply_format(udev, udev->name); |
98b88dbf | 982 | |
09e52d51 | 983 | done: |
c2405f50 | 984 | /* mode was never set above */ |
9d496c74 GKH |
985 | if (!udev->mode) { |
986 | udev->mode = get_default_mode(class_dev); | |
987 | udev->owner[0] = 0x00; | |
988 | udev->group[0] = 0x00; | |
615e05f8 | 989 | } |
7bd22a78 GKH |
990 | |
991 | if (class_dev_parent) | |
992 | sysfs_close_class_device(class_dev_parent); | |
993 | ||
120d45d0 | 994 | return 0; |
185a35a4 GKH |
995 | } |
996 | ||
9d496c74 | 997 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev) |
185a35a4 | 998 | { |
185a35a4 GKH |
999 | int retval; |
1000 | ||
9d496c74 | 1001 | retval = get_attr(class_dev, dev); |
19dc5d4c GKH |
1002 | if (retval) |
1003 | dbg("get_attr failed"); | |
1004 | ||
185a35a4 GKH |
1005 | return retval; |
1006 | } | |
2232cac8 GKH |
1007 | |
1008 | int namedev_init(void) | |
1009 | { | |
1010 | int retval; | |
185a35a4 | 1011 | |
2232cac8 GKH |
1012 | retval = namedev_init_config(); |
1013 | if (retval) | |
1014 | return retval; | |
1015 | ||
1016 | retval = namedev_init_permissions(); | |
1017 | if (retval) | |
1018 | return retval; | |
1019 | ||
469c7cff | 1020 | dump_dev_list(); |
2232cac8 GKH |
1021 | return retval; |
1022 | } |