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