]>
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 | ||
24 | #include <stddef.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <stdio.h> | |
28 | #include <fcntl.h> | |
29 | #include <ctype.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
c27e6911 | 32 | #include <sys/wait.h> |
dac056aa | 33 | #include <sys/stat.h> |
2232cac8 | 34 | |
c80da508 | 35 | #include "libsysfs/sysfs/libsysfs.h" |
2232cac8 GKH |
36 | #include "list.h" |
37 | #include "udev.h" | |
9af5bb2f | 38 | #include "udev_utils.h" |
2232cac8 | 39 | #include "udev_version.h" |
54988802 | 40 | #include "logging.h" |
2232cac8 | 41 | #include "namedev.h" |
02fa9ae5 | 42 | #include "udev_db.h" |
2232cac8 | 43 | |
a27cd06c KS |
44 | static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr); |
45 | ||
9f1da361 KS |
46 | /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ |
47 | static int strcmp_pattern(const char *p, const char *s) | |
c124eafa | 48 | { |
8a08e4b1 KS |
49 | if (s[0] == '\0') { |
50 | while (p[0] == '*') | |
9f1da361 | 51 | p++; |
8a08e4b1 | 52 | return (p[0] != '\0'); |
9f1da361 | 53 | } |
8a08e4b1 | 54 | switch (p[0]) { |
9f1da361 KS |
55 | case '[': |
56 | { | |
57 | int not = 0; | |
58 | p++; | |
8a08e4b1 | 59 | if (p[0] == '!') { |
9f1da361 KS |
60 | not = 1; |
61 | p++; | |
62 | } | |
8a08e4b1 | 63 | while ((p[0] != '\0') && (p[0] != ']')) { |
9f1da361 KS |
64 | int match = 0; |
65 | if (p[1] == '-') { | |
8a08e4b1 | 66 | if ((s[0] >= p[0]) && (s[0] <= p[2])) |
9f1da361 KS |
67 | match = 1; |
68 | p += 3; | |
69 | } else { | |
8a08e4b1 | 70 | match = (p[0] == s[0]); |
9f1da361 KS |
71 | p++; |
72 | } | |
73 | if (match ^ not) { | |
8a08e4b1 | 74 | while ((p[0] != '\0') && (p[0] != ']')) |
9f1da361 | 75 | p++; |
8a08e4b1 KS |
76 | if (p[0] == ']') |
77 | return strcmp_pattern(p+1, s+1); | |
9f1da361 KS |
78 | } |
79 | } | |
80 | } | |
81 | break; | |
82 | case '*': | |
83 | if (strcmp_pattern(p, s+1)) | |
84 | return strcmp_pattern(p+1, s); | |
85 | return 0; | |
86 | case '\0': | |
8a08e4b1 | 87 | if (s[0] == '\0') { |
9f1da361 KS |
88 | return 0; |
89 | } | |
90 | break; | |
91 | default: | |
8a08e4b1 | 92 | if ((p[0] == s[0]) || (p[0] == '?')) |
9f1da361 KS |
93 | return strcmp_pattern(p+1, s+1); |
94 | break; | |
95 | } | |
96 | return 1; | |
c124eafa KS |
97 | } |
98 | ||
a27cd06c KS |
99 | /* extract possible {attr} and move str behind it */ |
100 | static char *get_format_attribute(char **str) | |
101 | { | |
102 | char *pos; | |
103 | char *attr = NULL; | |
104 | ||
105 | if (*str[0] == '{') { | |
106 | pos = strchr(*str, '}'); | |
107 | if (pos == NULL) { | |
108 | dbg("missing closing brace for format"); | |
109 | return NULL; | |
110 | } | |
111 | pos[0] = '\0'; | |
112 | attr = *str+1; | |
113 | *str = pos+1; | |
114 | dbg("attribute='%s', str='%s'", attr, *str); | |
115 | } | |
116 | return attr; | |
117 | } | |
118 | ||
119 | /* extract possible format length and move str behind it*/ | |
120 | static int get_format_len(char **str) | |
121 | { | |
122 | int num; | |
123 | char *tail; | |
124 | ||
125 | if (isdigit(*str[0])) { | |
126 | num = (int) strtoul(*str, &tail, 10); | |
aebef544 | 127 | if (num > 0) { |
a27cd06c KS |
128 | *str = tail; |
129 | dbg("format length=%i", num); | |
130 | return num; | |
131 | } else { | |
132 | dbg("format parsing error '%s'", *str); | |
133 | } | |
134 | } | |
135 | return -1; | |
136 | } | |
137 | ||
0a8dd7f3 DZ |
138 | /** Finds the lowest positive N such that <name>N isn't present in |
139 | * $(udevroot) either as a file or a symlink. | |
140 | * | |
141 | * @param name Name to check for | |
142 | * @return 0 if <name> didn't exist and N otherwise. | |
143 | */ | |
2b41e68a | 144 | static int find_free_number(struct udevice *udev, const char *name) |
0a8dd7f3 | 145 | { |
2b41e68a KS |
146 | char filename[NAME_SIZE]; |
147 | int num = 0; | |
148 | struct udevice db_udev; | |
0a8dd7f3 | 149 | |
2b41e68a | 150 | strfieldcpy(filename, name); |
0a8dd7f3 | 151 | while (1) { |
2b41e68a KS |
152 | dbg("look for existing node '%s'", filename); |
153 | memset(&db_udev, 0x00, sizeof(struct udevice)); | |
02fa9ae5 | 154 | if (udev_db_get_device_byname(&db_udev, filename) != 0) { |
2b41e68a KS |
155 | dbg("free num=%d", num); |
156 | return num; | |
157 | } | |
0a8dd7f3 | 158 | |
2b41e68a KS |
159 | num++; |
160 | if (num > 1000) { | |
161 | info("find_free_number gone crazy (num=%d), aborted", num); | |
162 | return -1; | |
163 | } | |
8673dcb8 | 164 | snprintf(filename, NAME_SIZE, "%s%d", name, num); |
2b41e68a KS |
165 | filename[NAME_SIZE-1] = '\0'; |
166 | } | |
0a8dd7f3 DZ |
167 | } |
168 | ||
831f800d KS |
169 | static void apply_format(struct udevice *udev, char *string, size_t maxsize, |
170 | struct sysfs_class_device *class_dev, | |
171 | struct sysfs_device *sysfs_device) | |
f3b04a2e | 172 | { |
b1c5e333 | 173 | char temp[NAME_SIZE]; |
27c3403d | 174 | char temp2[NAME_SIZE]; |
b1c5e333 | 175 | char *tail; |
f3b04a2e | 176 | char *pos; |
a27cd06c | 177 | char *attr; |
63ead27c | 178 | int len; |
88f09368 | 179 | int i; |
a27cd06c | 180 | char c; |
ef672b3d | 181 | char *spos; |
558f80ba | 182 | char *rest; |
ef672b3d | 183 | int slen; |
a27cd06c | 184 | struct sysfs_attribute *tmpattr; |
0a8dd7f3 | 185 | unsigned int next_free_number; |
69aa6dfb | 186 | struct sysfs_class_device *class_dev_parent; |
f3b04a2e | 187 | |
8ffb636f | 188 | pos = string; |
f3b04a2e | 189 | while (1) { |
a36a3c3a KS |
190 | pos = strchr(pos, '%'); |
191 | if (pos == NULL) | |
a27cd06c | 192 | break; |
b1c5e333 | 193 | |
a36a3c3a KS |
194 | pos[0] = '\0'; |
195 | tail = pos+1; | |
196 | len = get_format_len(&tail); | |
197 | c = tail[0]; | |
198 | strfieldcpy(temp, tail+1); | |
199 | tail = temp; | |
200 | dbg("format=%c, string='%s', tail='%s'",c , string, tail); | |
a27cd06c KS |
201 | attr = get_format_attribute(&tail); |
202 | ||
a36a3c3a | 203 | |
a27cd06c | 204 | switch (c) { |
c1ab0461 KS |
205 | case 'p': |
206 | if (strlen(udev->devpath) == 0) | |
207 | break; | |
208 | strfieldcatmax(string, udev->devpath, maxsize); | |
209 | dbg("substitute kernel name '%s'", udev->kernel_name); | |
210 | break; | |
a27cd06c KS |
211 | case 'b': |
212 | if (strlen(udev->bus_id) == 0) | |
f3b04a2e | 213 | break; |
17794d77 | 214 | strfieldcatmax(string, udev->bus_id, maxsize); |
a27cd06c KS |
215 | dbg("substitute bus_id '%s'", udev->bus_id); |
216 | break; | |
217 | case 'k': | |
218 | if (strlen(udev->kernel_name) == 0) | |
3e540368 | 219 | break; |
17794d77 | 220 | strfieldcatmax(string, udev->kernel_name, maxsize); |
a27cd06c KS |
221 | dbg("substitute kernel name '%s'", udev->kernel_name); |
222 | break; | |
223 | case 'n': | |
224 | if (strlen(udev->kernel_number) == 0) | |
f3b04a2e | 225 | break; |
17794d77 | 226 | strfieldcatmax(string, udev->kernel_number, maxsize); |
a27cd06c | 227 | dbg("substitute kernel number '%s'", udev->kernel_number); |
f3b04a2e | 228 | break; |
a27cd06c | 229 | case 'm': |
17794d77 | 230 | strintcatmax(string, udev->minor, maxsize); |
a27cd06c KS |
231 | dbg("substitute minor number '%u'", udev->minor); |
232 | break; | |
c58e36c0 | 233 | case 'M': |
17794d77 | 234 | strintcatmax(string, udev->major, maxsize); |
a27cd06c KS |
235 | dbg("substitute major number '%u'", udev->major); |
236 | break; | |
237 | case 'c': | |
238 | if (strlen(udev->program_result) == 0) | |
f3b04a2e | 239 | break; |
88f09368 | 240 | /* get part part of the result string */ |
63ead27c | 241 | i = 0; |
88f09368 | 242 | if (attr != NULL) |
558f80ba | 243 | i = strtoul(attr, &rest, 10); |
88f09368 | 244 | if (i > 0) { |
9fe3f9a9 | 245 | foreach_strpart(udev->program_result, " \n\r", spos, slen) { |
88f09368 | 246 | i--; |
9fe3f9a9 | 247 | if (i == 0) |
a27cd06c | 248 | break; |
b1c5e333 | 249 | } |
9fe3f9a9 KS |
250 | if (i > 0) { |
251 | dbg("requested part of result string not found"); | |
252 | break; | |
a27cd06c | 253 | } |
558f80ba KS |
254 | if (rest[0] == '+') |
255 | strfieldcpy(temp2, spos); | |
256 | else | |
257 | strfieldcpymax(temp2, spos, slen+1); | |
17794d77 | 258 | strfieldcatmax(string, temp2, maxsize); |
27c3403d | 259 | dbg("substitute part of result string '%s'", temp2); |
a27cd06c | 260 | } else { |
17794d77 | 261 | strfieldcatmax(string, udev->program_result, maxsize); |
a27cd06c | 262 | dbg("substitute result string '%s'", udev->program_result); |
f3b04a2e | 263 | } |
f3b04a2e | 264 | break; |
a27cd06c KS |
265 | case 's': |
266 | if (attr != NULL) { | |
267 | tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr); | |
268 | if (tmpattr == NULL) { | |
269 | dbg("sysfa attribute '%s' not found", attr); | |
270 | break; | |
271 | } | |
1fa26490 | 272 | /* strip trailing whitespace of matching value */ |
273 | if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) { | |
274 | i = len = strlen(tmpattr->value); | |
275 | while (i > 0 && isspace(tmpattr->value[i-1])) | |
276 | i--; | |
277 | if (i < len) { | |
278 | tmpattr->value[i] = '\0'; | |
279 | dbg("remove %i trailing whitespace chars from '%s'", | |
280 | len - i, tmpattr->value); | |
281 | } | |
282 | } | |
17794d77 | 283 | strfieldcatmax(string, tmpattr->value, maxsize); |
a27cd06c KS |
284 | dbg("substitute sysfs value '%s'", tmpattr->value); |
285 | } else { | |
286 | dbg("missing attribute"); | |
287 | } | |
288 | break; | |
289 | case '%': | |
17794d77 | 290 | strfieldcatmax(string, "%", maxsize); |
a36a3c3a | 291 | pos++; |
a27cd06c | 292 | break; |
0a8dd7f3 DZ |
293 | case 'e': |
294 | next_free_number = find_free_number(udev, string); | |
295 | if (next_free_number > 0) { | |
2b41e68a | 296 | sprintf(temp2, "%d", next_free_number); |
0a8dd7f3 DZ |
297 | strfieldcatmax(string, temp2, maxsize); |
298 | } | |
299 | break; | |
69aa6dfb KS |
300 | case 'P': |
301 | class_dev_parent = sysfs_get_classdev_parent(class_dev); | |
302 | if (class_dev_parent != NULL) { | |
303 | struct udevice udev_parent; | |
304 | ||
305 | dbg("found parent '%s', get the node name", class_dev_parent->path); | |
306 | memset(&udev_parent, 0x00, sizeof(struct udevice)); | |
307 | /* lookup the name in the udev_db with the DEVPATH of the parent */ | |
308 | strfieldcpy(udev_parent.devpath, &class_dev_parent->path[strlen(sysfs_path)]); | |
309 | if (udev_db_get_device(&udev_parent) == 0) { | |
310 | strfieldcatmax(string, udev_parent.name, maxsize); | |
311 | dbg("substitute parent node name'%s'", udev_parent.name); | |
312 | } else | |
313 | dbg("parent not found in database"); | |
314 | } | |
315 | break; | |
c1ab0461 KS |
316 | case 'N': |
317 | if (udev->tmp_node[0] == '\0') { | |
318 | dbg("create temporary device node for callout"); | |
319 | snprintf(udev->tmp_node, NAME_SIZE-1, "%s/.tmp-%u-%u", udev_root, udev->major, udev->minor); | |
320 | udev_make_node(udev, udev->tmp_node, udev->major, udev->minor, 0600, 0, 0); | |
321 | } | |
322 | strfieldcatmax(string, udev->tmp_node, maxsize); | |
323 | dbg("substitute temporary device node name '%s'", udev->tmp_node); | |
324 | break; | |
69aa6dfb KS |
325 | case 'r': |
326 | strfieldcatmax(string, udev_root, maxsize); | |
327 | dbg("substitute udev_root '%s'", udev_root); | |
328 | break; | |
a27cd06c KS |
329 | default: |
330 | dbg("unknown substitution type '%%%c'", c); | |
331 | break; | |
332 | } | |
63ead27c KS |
333 | /* truncate to specified length */ |
334 | if (len > 0) | |
335 | pos[len] = '\0'; | |
336 | ||
17794d77 | 337 | strfieldcatmax(string, tail, maxsize); |
f3b04a2e GKH |
338 | } |
339 | } | |
340 | ||
1b941027 GKH |
341 | static void fix_kernel_name(struct udevice *udev) |
342 | { | |
343 | char *temp = udev->kernel_name; | |
344 | ||
345 | while (*temp != 0x00) { | |
346 | /* Some block devices have a ! in their name, | |
347 | * we need to change that to / */ | |
348 | if (*temp == '!') | |
349 | *temp = '/'; | |
350 | ++temp; | |
351 | } | |
352 | } | |
353 | ||
af4b05d4 | 354 | static int execute_program(struct udevice *udev, const char *path, char *value, int len) |
c27e6911 PM |
355 | { |
356 | int retval; | |
35b38379 | 357 | int count; |
c27e6911 PM |
358 | int status; |
359 | int fds[2]; | |
360 | pid_t pid; | |
20524642 | 361 | char *pos; |
35b38379 | 362 | char arg[PROGRAM_SIZE]; |
f608f8ac | 363 | char *argv[(PROGRAM_SIZE / 2) + 1]; |
bc434511 | 364 | int i; |
c27e6911 | 365 | |
f608f8ac | 366 | strfieldcpy(arg, path); |
35b38379 KS |
367 | i = 0; |
368 | if (strchr(path, ' ')) { | |
35b38379 KS |
369 | pos = arg; |
370 | while (pos != NULL) { | |
371 | if (pos[0] == '\'') { | |
372 | /* don't separate if in apostrophes */ | |
373 | pos++; | |
374 | argv[i] = strsep(&pos, "\'"); | |
a75e2c14 | 375 | while (pos && pos[0] == ' ') |
35b38379 | 376 | pos++; |
a75e2c14 | 377 | } else { |
35b38379 KS |
378 | argv[i] = strsep(&pos, " "); |
379 | } | |
380 | dbg("arg[%i] '%s'", i, argv[i]); | |
381 | i++; | |
382 | } | |
f608f8ac KS |
383 | argv[i] = NULL; |
384 | dbg("execute '%s' with parsed arguments", arg); | |
385 | } else { | |
386 | argv[0] = arg; | |
af4b05d4 | 387 | argv[1] = udev->subsystem; |
f608f8ac KS |
388 | argv[2] = NULL; |
389 | dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]); | |
35b38379 | 390 | } |
f608f8ac | 391 | |
c27e6911 PM |
392 | retval = pipe(fds); |
393 | if (retval != 0) { | |
394 | dbg("pipe failed"); | |
395 | return -1; | |
396 | } | |
35b38379 | 397 | |
c27e6911 | 398 | pid = fork(); |
2a25816f KS |
399 | switch(pid) { |
400 | case 0: | |
8f43a65e | 401 | /* child */ |
6e3e3c34 HH |
402 | /* dup2 write side of pipe to STDOUT */ |
403 | dup2(fds[1], STDOUT_FILENO); | |
f608f8ac | 404 | retval = execv(arg, argv); |
35b38379 | 405 | |
dde05ccb | 406 | info(FIELD_PROGRAM " execution of '%s' failed", path); |
2a25816f KS |
407 | exit(1); |
408 | case -1: | |
409 | dbg("fork failed"); | |
410 | return -1; | |
411 | default: | |
8f43a65e | 412 | /* parent reads from fds[0] */ |
c27e6911 PM |
413 | close(fds[1]); |
414 | retval = 0; | |
35b38379 | 415 | i = 0; |
c27e6911 | 416 | while (1) { |
35b38379 KS |
417 | count = read(fds[0], value + i, len - i-1); |
418 | if (count <= 0) | |
c27e6911 | 419 | break; |
35b38379 KS |
420 | |
421 | i += count; | |
422 | if (i >= len-1) { | |
ac28b86d | 423 | dbg("result len %d too short", len); |
c27e6911 | 424 | retval = -1; |
35b38379 | 425 | break; |
c27e6911 PM |
426 | } |
427 | } | |
35b38379 KS |
428 | |
429 | if (count < 0) { | |
430 | dbg("read failed with '%s'", strerror(errno)); | |
c27e6911 PM |
431 | retval = -1; |
432 | } | |
433 | ||
bbbe503e | 434 | if (i > 0 && value[i-1] == '\n') |
35b38379 KS |
435 | i--; |
436 | value[i] = '\0'; | |
437 | dbg("result is '%s'", value); | |
438 | ||
439 | close(fds[0]); | |
e920fed3 | 440 | waitpid(pid, &status, 0); |
35b38379 | 441 | |
c27e6911 | 442 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
ac28b86d | 443 | dbg("exec program status 0x%x", status); |
c27e6911 PM |
444 | retval = -1; |
445 | } | |
446 | } | |
447 | return retval; | |
448 | } | |
449 | ||
a27cd06c | 450 | static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr) |
ca1cc0fe GKH |
451 | { |
452 | struct sysfs_attribute *tmpattr = NULL; | |
a8b01705 GKH |
453 | char *c; |
454 | ||
a27cd06c | 455 | dbg("look for device attribute '%s'", attr); |
a8b01705 | 456 | /* try to find the attribute in the class device directory */ |
a27cd06c | 457 | tmpattr = sysfs_get_classdev_attr(class_dev, attr); |
a8b01705 | 458 | if (tmpattr) |
a27cd06c | 459 | goto attr_found; |
a8b01705 GKH |
460 | |
461 | /* look in the class device directory if present */ | |
462 | if (sysfs_device) { | |
a27cd06c | 463 | tmpattr = sysfs_get_device_attr(sysfs_device, attr); |
a8b01705 | 464 | if (tmpattr) |
a27cd06c | 465 | goto attr_found; |
a8b01705 | 466 | } |
a8b01705 | 467 | |
a27cd06c KS |
468 | return NULL; |
469 | ||
470 | attr_found: | |
471 | c = strchr(tmpattr->value, '\n'); | |
472 | if (c != NULL) | |
473 | c[0] = '\0'; | |
474 | ||
475 | dbg("found attribute '%s'", tmpattr->path); | |
476 | return tmpattr; | |
477 | } | |
478 | ||
479 | static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair) | |
480 | { | |
481 | struct sysfs_attribute *tmpattr; | |
d5f91372 KS |
482 | int i; |
483 | int len; | |
a27cd06c KS |
484 | |
485 | if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0')) | |
486 | return -ENODEV; | |
487 | ||
488 | tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file); | |
489 | if (tmpattr == NULL) | |
490 | return -ENODEV; | |
491 | ||
d5f91372 KS |
492 | /* strip trailing whitespace of value, if not asked to match for it */ |
493 | if (! isspace(pair->value[strlen(pair->value)-1])) { | |
494 | i = len = strlen(tmpattr->value); | |
495 | while (i > 0 && isspace(tmpattr->value[i-1])) | |
496 | i--; | |
497 | if (i < len) { | |
498 | tmpattr->value[i] = '\0'; | |
499 | dbg("remove %i trailing whitespace chars from '%s'", | |
500 | len - i, tmpattr->value); | |
501 | } | |
502 | } | |
503 | ||
a8b01705 GKH |
504 | dbg("compare attribute '%s' value '%s' with '%s'", |
505 | pair->file, tmpattr->value, pair->value); | |
506 | if (strcmp_pattern(pair->value, tmpattr->value) != 0) | |
507 | return -ENODEV; | |
508 | ||
509 | dbg("found matching attribute '%s' with value '%s'", | |
510 | pair->file, pair->value); | |
511 | return 0; | |
512 | } | |
513 | ||
ac28b86d | 514 | static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
a8b01705 GKH |
515 | { |
516 | struct sysfs_pair *pair; | |
a8b01705 | 517 | int i; |
137af0cc | 518 | |
ac28b86d KS |
519 | for (i = 0; i < MAX_SYSFS_PAIRS; ++i) { |
520 | pair = &dev->sysfs_pair[i]; | |
521 | if ((pair->file[0] == '\0') || (pair->value[0] == '\0')) | |
522 | break; | |
523 | if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) { | |
524 | dbg("sysfs attribute doesn't match"); | |
525 | return -ENODEV; | |
ca1cc0fe | 526 | } |
ca1cc0fe | 527 | } |
ac28b86d KS |
528 | |
529 | return 0; | |
ca1cc0fe GKH |
530 | } |
531 | ||
ac28b86d | 532 | static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
ca1cc0fe | 533 | { |
ca1cc0fe | 534 | char path[SYSFS_PATH_MAX]; |
ca1cc0fe GKH |
535 | char *temp = NULL; |
536 | ||
1f7148c7 | 537 | /* we have to have a sysfs device for ID to work */ |
ca1cc0fe GKH |
538 | if (!sysfs_device) |
539 | return -ENODEV; | |
540 | ||
ac28b86d KS |
541 | strfieldcpy(path, sysfs_device->path); |
542 | temp = strrchr(path, '/'); | |
3a8c51a7 | 543 | temp++; |
ac28b86d | 544 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
3a8c51a7 | 545 | if (strcmp_pattern(dev->id, temp) != 0) |
ac28b86d | 546 | return -ENODEV; |
5a42932b PM |
547 | else |
548 | return 0; | |
ca1cc0fe GKH |
549 | } |
550 | ||
ac28b86d | 551 | static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
8c51bbfe | 552 | { |
8c51bbfe GKH |
553 | char path[SYSFS_PATH_MAX]; |
554 | int found; | |
555 | char *temp = NULL; | |
556 | ||
1f7148c7 | 557 | /* we have to have a sysfs device for PLACE to work */ |
8c51bbfe GKH |
558 | if (!sysfs_device) |
559 | return -ENODEV; | |
560 | ||
ac28b86d KS |
561 | found = 0; |
562 | strfieldcpy(path, sysfs_device->path); | |
563 | temp = strrchr(path, '/'); | |
564 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); | |
565 | if (strstr(temp, dev->place) != NULL) { | |
566 | found = 1; | |
567 | } else { | |
568 | *temp = 0x00; | |
8c51bbfe | 569 | temp = strrchr(path, '/'); |
19feb351 | 570 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
ac28b86d | 571 | if (strstr(temp, dev->place) != NULL) |
8c51bbfe | 572 | found = 1; |
8c51bbfe | 573 | } |
ac28b86d KS |
574 | if (!found) { |
575 | dbg("place doesn't match"); | |
576 | return -ENODEV; | |
120d45d0 | 577 | } |
120d45d0 | 578 | |
ac28b86d | 579 | return 0; |
09e52d51 KS |
580 | } |
581 | ||
5f72c470 KS |
582 | static int match_rule(struct udevice *udev, struct config_device *dev, |
583 | struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) | |
dac056aa | 584 | { |
724257d9 | 585 | while (1) { |
6818c51d | 586 | /* check for matching kernel name */ |
ac28b86d | 587 | if (dev->kernel[0] != '\0') { |
5f72c470 KS |
588 | dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", |
589 | dev->kernel, class_dev->name); | |
ac28b86d KS |
590 | if (strcmp_pattern(dev->kernel, class_dev->name) != 0) { |
591 | dbg(FIELD_KERNEL " is not matching"); | |
82b16983 | 592 | goto exit; |
ac28b86d | 593 | } |
82b16983 | 594 | dbg(FIELD_KERNEL " matches"); |
ac28b86d | 595 | } |
ca1cc0fe | 596 | |
6818c51d KS |
597 | /* check for matching subsystem */ |
598 | if (dev->subsystem[0] != '\0') { | |
5f72c470 KS |
599 | dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'", |
600 | dev->subsystem, class_dev->name); | |
6818c51d KS |
601 | if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) { |
602 | dbg(FIELD_SUBSYSTEM " is not matching"); | |
82b16983 | 603 | goto exit; |
6818c51d | 604 | } |
82b16983 | 605 | dbg(FIELD_SUBSYSTEM " matches"); |
6818c51d KS |
606 | } |
607 | ||
2092fbcd KS |
608 | /* check for matching driver */ |
609 | if (dev->driver[0] != '\0') { | |
5f72c470 KS |
610 | dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'", |
611 | dev->driver, sysfs_device->driver_name); | |
2092fbcd KS |
612 | if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) { |
613 | dbg(FIELD_DRIVER " is not matching"); | |
614 | goto try_parent; | |
615 | } else { | |
616 | dbg(FIELD_DRIVER " matches"); | |
617 | } | |
618 | } | |
619 | ||
82b16983 KS |
620 | /* check for matching bus value */ |
621 | if (dev->bus[0] != '\0') { | |
622 | if (sysfs_device == NULL) { | |
623 | dbg("device has no bus"); | |
624 | goto try_parent; | |
625 | } | |
626 | dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", | |
627 | dev->bus, sysfs_device->bus); | |
628 | if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) { | |
629 | dbg(FIELD_BUS " is not matching"); | |
630 | goto try_parent; | |
631 | } | |
632 | dbg(FIELD_BUS " matches"); | |
633 | } | |
634 | ||
ac28b86d KS |
635 | /* check for matching bus id */ |
636 | if (dev->id[0] != '\0') { | |
637 | dbg("check " FIELD_ID); | |
638 | if (match_id(dev, class_dev, sysfs_device) != 0) { | |
639 | dbg(FIELD_ID " is not matching"); | |
c5118442 | 640 | goto try_parent; |
ac28b86d KS |
641 | } else { |
642 | dbg(FIELD_ID " matches"); | |
643 | } | |
644 | } | |
ca1cc0fe | 645 | |
ac28b86d KS |
646 | /* check for matching place of device */ |
647 | if (dev->place[0] != '\0') { | |
648 | dbg("check " FIELD_PLACE); | |
649 | if (match_place(dev, class_dev, sysfs_device) != 0) { | |
650 | dbg(FIELD_PLACE " is not matching"); | |
c5118442 | 651 | goto try_parent; |
ac28b86d KS |
652 | } else { |
653 | dbg(FIELD_PLACE " matches"); | |
654 | } | |
655 | } | |
8c51bbfe | 656 | |
ac28b86d KS |
657 | /* check for matching sysfs pairs */ |
658 | if (dev->sysfs_pair[0].file[0] != '\0') { | |
659 | dbg("check " FIELD_SYSFS " pairs"); | |
660 | if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) { | |
661 | dbg(FIELD_SYSFS " is not matching"); | |
c5118442 | 662 | goto try_parent; |
ac28b86d KS |
663 | } else { |
664 | dbg(FIELD_SYSFS " matches"); | |
665 | } | |
666 | } | |
667 | ||
668 | /* execute external program */ | |
669 | if (dev->program[0] != '\0') { | |
29a3cead KS |
670 | char program[PROGRAM_SIZE]; |
671 | ||
ac28b86d | 672 | dbg("check " FIELD_PROGRAM); |
29a3cead KS |
673 | strfieldcpy(program, dev->program); |
674 | apply_format(udev, program, sizeof(program), class_dev, sysfs_device); | |
af4b05d4 | 675 | if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) { |
35b38379 | 676 | dbg(FIELD_PROGRAM " returned nonzero"); |
c5118442 | 677 | goto try_parent; |
ac28b86d KS |
678 | } else { |
679 | dbg(FIELD_PROGRAM " returned successful"); | |
680 | } | |
681 | } | |
682 | ||
683 | /* check for matching result of external program */ | |
684 | if (dev->result[0] != '\0') { | |
0ba5bb10 | 685 | dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'", |
ac28b86d KS |
686 | dev->result, udev->program_result); |
687 | if (strcmp_pattern(dev->result, udev->program_result) != 0) { | |
688 | dbg(FIELD_RESULT " is not matching"); | |
c5118442 | 689 | goto try_parent; |
ac28b86d KS |
690 | } else { |
691 | dbg(FIELD_RESULT " matches"); | |
692 | } | |
693 | } | |
694 | ||
82b16983 | 695 | /* we matched */ |
724257d9 GKH |
696 | return 0; |
697 | ||
c5118442 KS |
698 | try_parent: |
699 | dbg("try parent sysfs device"); | |
724257d9 GKH |
700 | sysfs_device = sysfs_get_device_parent(sysfs_device); |
701 | if (sysfs_device == NULL) | |
82b16983 | 702 | goto exit; |
724257d9 GKH |
703 | dbg("sysfs_device->path='%s'", sysfs_device->path); |
704 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
724257d9 | 705 | } |
82b16983 KS |
706 | |
707 | exit: | |
708 | return -1; | |
724257d9 GKH |
709 | } |
710 | ||
7a947ce5 | 711 | int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev) |
724257d9 | 712 | { |
707680b1 | 713 | struct sysfs_class_device *class_dev_parent; |
724257d9 GKH |
714 | struct sysfs_device *sysfs_device = NULL; |
715 | struct config_device *dev; | |
724257d9 GKH |
716 | char *pos; |
717 | ||
707680b1 KS |
718 | dbg("class_dev->name='%s'", class_dev->name); |
719 | ||
720 | /* Figure out where the "device"-symlink is at. For char devices this will | |
721 | * always be in the class_dev->path. On block devices, only the main block | |
722 | * device will have the device symlink in it's path. All partition devices | |
723 | * need to look at the symlink in its parent directory. | |
724 | */ | |
725 | class_dev_parent = sysfs_get_classdev_parent(class_dev); | |
726 | if (class_dev_parent != NULL) { | |
727 | dbg("given class device has a parent, use this instead"); | |
728 | sysfs_device = sysfs_get_classdev_device(class_dev_parent); | |
729 | } else { | |
730 | sysfs_device = sysfs_get_classdev_device(class_dev); | |
731 | } | |
724257d9 | 732 | |
724257d9 | 733 | if (sysfs_device) { |
7a947ce5 | 734 | dbg("found devices device: path='%s', bus_id='%s', bus='%s'", |
707680b1 | 735 | sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus); |
724257d9 | 736 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); |
724257d9 GKH |
737 | } |
738 | ||
739 | strfieldcpy(udev->kernel_name, class_dev->name); | |
1b941027 GKH |
740 | fix_kernel_name(udev); |
741 | dbg("udev->kernel_name = '%s'", udev->kernel_name); | |
724257d9 GKH |
742 | |
743 | /* get kernel number */ | |
744 | pos = class_dev->name + strlen(class_dev->name); | |
745 | while (isdigit(*(pos-1))) | |
746 | pos--; | |
747 | strfieldcpy(udev->kernel_number, pos); | |
748 | dbg("kernel_number='%s'", udev->kernel_number); | |
749 | ||
750 | /* look for a matching rule to apply */ | |
751 | list_for_each_entry(dev, &config_device_list, node) { | |
752 | dbg("process rule"); | |
5f72c470 | 753 | if (match_rule(udev, dev, class_dev, sysfs_device) == 0) { |
e9390146 | 754 | |
38875577 MB |
755 | /* empty name, symlink and perms will not create any node */ |
756 | if (dev->name[0] == '\0' && dev->symlink[0] == '\0' && | |
757 | dev->mode == 0000 && dev->owner[0] == '\0' && dev->group[0] == '\0') { | |
758 | info("configured rule in '%s[%i]' applied, '%s' is ignored", | |
759 | dev->config_file, dev->config_line, udev->kernel_name); | |
760 | return -1; | |
c5118442 KS |
761 | } |
762 | ||
38875577 MB |
763 | /* apply permissions */ |
764 | if (dev->mode != 0000) { | |
765 | udev->mode = dev->mode; | |
766 | dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name); | |
767 | } | |
768 | if (dev->owner[0] != '\0') { | |
769 | strfieldcpy(udev->owner, dev->owner); | |
770 | apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device); | |
771 | dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name); | |
772 | } | |
773 | if (dev->group[0] != '\0') { | |
774 | strfieldcpy(udev->group, dev->group); | |
775 | apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device); | |
776 | dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name); | |
777 | } | |
778 | ||
779 | /* collect symlinks for this or the final matching rule */ | |
97ed02ee | 780 | if (dev->symlink[0] != '\0') { |
615ba3e8 | 781 | char temp[NAME_SIZE]; |
ddd5b5dc | 782 | |
3908058c | 783 | info("configured rule in '%s[%i]' applied, added symlink '%s'", |
bd5f8e7c | 784 | dev->config_file, dev->config_line, dev->symlink); |
ddd5b5dc KS |
785 | strfieldcpy(temp, dev->symlink); |
786 | apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device); | |
0a5417a0 KS |
787 | if (udev->symlink[0] != '\0') |
788 | strfieldcat(udev->symlink, " "); | |
ddd5b5dc | 789 | strfieldcat(udev->symlink, temp); |
97ed02ee | 790 | } |
791 | ||
5f72c470 | 792 | /* rule matches */ |
97ed02ee | 793 | if (dev->name[0] != '\0') { |
949e32f2 KS |
794 | /* apply all_partitions flag only at a main block device */ |
795 | if (dev->partitions > 0 && | |
796 | (udev->type != 'b' || udev->kernel_number[0] != '\0')) | |
797 | continue; | |
798 | ||
e9390146 | 799 | info("configured rule in '%s[%i]' applied, '%s' becomes '%s'", |
bd5f8e7c | 800 | dev->config_file, dev->config_line, udev->kernel_name, dev->name); |
5f72c470 | 801 | |
97ed02ee | 802 | strfieldcpy(udev->name, dev->name); |
5f72c470 KS |
803 | apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device); |
804 | strfieldcpy(udev->config_file, dev->config_file); | |
805 | udev->config_line = dev->config_line; | |
806 | udev->ignore_remove = dev->ignore_remove; | |
807 | ||
808 | if (udev->type == 'n') | |
3fd7a9bf | 809 | goto exit; |
5f72c470 KS |
810 | |
811 | udev->partitions = dev->partitions; | |
65ab1334 | 812 | |
38875577 MB |
813 | dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i", |
814 | udev->name, udev->owner, udev->group, udev->mode, udev->partitions); | |
5f72c470 | 815 | |
65ab1334 | 816 | goto exit; |
97ed02ee | 817 | } |
724257d9 | 818 | } |
ac28b86d | 819 | } |
c2405f50 | 820 | |
5f72c470 KS |
821 | /* no rule matched, so we use the kernel name */ |
822 | strfieldcpy(udev->name, udev->kernel_name); | |
65ab1334 | 823 | dbg("no rule found, use kernel name '%s'", udev->name); |
7bd22a78 | 824 | |
3fd7a9bf | 825 | exit: |
c1ab0461 KS |
826 | if (udev->tmp_node[0] != '\0') { |
827 | dbg("removing temporary device node"); | |
828 | unlink_secure(udev->tmp_node); | |
829 | udev->tmp_node[0] = '\0'; | |
830 | } | |
831 | ||
120d45d0 | 832 | return 0; |
185a35a4 | 833 | } |