]> git.ipfire.org Git - thirdparty/systemd.git/blame - namedev.c
[PATCH] add a "first" list to udevstart and make it contain the class/mem/ devices
[thirdparty/systemd.git] / namedev.c
CommitLineData
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>
616a7078 32#include <time.h>
c27e6911 33#include <sys/wait.h>
dac056aa 34#include <sys/stat.h>
bbbe503e 35#include <sys/sysinfo.h>
2232cac8 36
c80da508 37#include "libsysfs/sysfs/libsysfs.h"
2232cac8
GKH
38#include "list.h"
39#include "udev.h"
c81b35c0 40#include "udev_lib.h"
2232cac8 41#include "udev_version.h"
54988802 42#include "logging.h"
2232cac8 43#include "namedev.h"
2023350e 44#include "klibc_fixups.h"
0a8dd7f3 45#include "udevdb.h"
2232cac8 46
a27cd06c
KS
47static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
48
19feb351 49LIST_HEAD(config_device_list);
61219c75 50LIST_HEAD(perm_device_list);
2232cac8 51
e41016d3 52
9f1da361
KS
53/* compare string with pattern (supports * ? [0-9] [!A-Z]) */
54static int strcmp_pattern(const char *p, const char *s)
c124eafa 55{
8a08e4b1
KS
56 if (s[0] == '\0') {
57 while (p[0] == '*')
9f1da361 58 p++;
8a08e4b1 59 return (p[0] != '\0');
9f1da361 60 }
8a08e4b1 61 switch (p[0]) {
9f1da361
KS
62 case '[':
63 {
64 int not = 0;
65 p++;
8a08e4b1 66 if (p[0] == '!') {
9f1da361
KS
67 not = 1;
68 p++;
69 }
8a08e4b1 70 while ((p[0] != '\0') && (p[0] != ']')) {
9f1da361
KS
71 int match = 0;
72 if (p[1] == '-') {
8a08e4b1 73 if ((s[0] >= p[0]) && (s[0] <= p[2]))
9f1da361
KS
74 match = 1;
75 p += 3;
76 } else {
8a08e4b1 77 match = (p[0] == s[0]);
9f1da361
KS
78 p++;
79 }
80 if (match ^ not) {
8a08e4b1 81 while ((p[0] != '\0') && (p[0] != ']'))
9f1da361 82 p++;
8a08e4b1
KS
83 if (p[0] == ']')
84 return strcmp_pattern(p+1, s+1);
9f1da361
KS
85 }
86 }
87 }
88 break;
89 case '*':
90 if (strcmp_pattern(p, s+1))
91 return strcmp_pattern(p+1, s);
92 return 0;
93 case '\0':
8a08e4b1 94 if (s[0] == '\0') {
9f1da361
KS
95 return 0;
96 }
97 break;
98 default:
8a08e4b1 99 if ((p[0] == s[0]) || (p[0] == '?'))
9f1da361
KS
100 return strcmp_pattern(p+1, s+1);
101 break;
102 }
103 return 1;
c124eafa
KS
104}
105
61219c75
GKH
106static struct perm_device *find_perm(char *name)
107{
843d1a84 108 struct perm_device *perm;
61219c75 109
843d1a84 110 list_for_each_entry(perm, &perm_device_list, node) {
61219c75
GKH
111 if (strcmp_pattern(perm->name, name))
112 continue;
113 return perm;
114 }
115 return NULL;
116}
117
765cbd97 118static mode_t get_default_mode(void)
185a35a4 119{
89571022
GKH
120 mode_t mode = 0600; /* default to owner rw only */
121
267f534d 122 if (strlen(default_mode_str) != 0)
89571022 123 mode = strtol(default_mode_str, NULL, 8);
267f534d 124
89571022 125 return mode;
185a35a4
GKH
126}
127
267f534d 128static char *get_default_owner(void)
74c73ef9 129{
267f534d 130 if (strlen(default_owner_str) == 0)
c472e3c8 131 strfieldcpy(default_owner_str, "root");
267f534d 132
74c73ef9 133 return default_owner_str;
134}
135
267f534d 136static char *get_default_group(void)
74c73ef9 137{
267f534d 138 if (strlen(default_group_str) == 0)
c472e3c8 139 strfieldcpy(default_group_str, "root");
267f534d 140
74c73ef9 141 return default_group_str;
142}
143
a27cd06c
KS
144/* extract possible {attr} and move str behind it */
145static char *get_format_attribute(char **str)
146{
147 char *pos;
148 char *attr = NULL;
149
150 if (*str[0] == '{') {
151 pos = strchr(*str, '}');
152 if (pos == NULL) {
153 dbg("missing closing brace for format");
154 return NULL;
155 }
156 pos[0] = '\0';
157 attr = *str+1;
158 *str = pos+1;
159 dbg("attribute='%s', str='%s'", attr, *str);
160 }
161 return attr;
162}
163
164/* extract possible format length and move str behind it*/
165static int get_format_len(char **str)
166{
167 int num;
168 char *tail;
169
170 if (isdigit(*str[0])) {
171 num = (int) strtoul(*str, &tail, 10);
aebef544 172 if (num > 0) {
a27cd06c
KS
173 *str = tail;
174 dbg("format length=%i", num);
175 return num;
176 } else {
177 dbg("format parsing error '%s'", *str);
178 }
179 }
180 return -1;
181}
182
0a8dd7f3
DZ
183/** Finds the lowest positive N such that <name>N isn't present in
184 * $(udevroot) either as a file or a symlink.
185 *
186 * @param name Name to check for
187 * @return 0 if <name> didn't exist and N otherwise.
188 */
189static unsigned int find_free_number (struct udevice *udev, char *name)
190{
191 char temp[NAME_SIZE];
192 char path[NAME_SIZE];
193 struct udevice dev;
194 int result;
195
196 /* have to sweep the database for each lookup */
197 result = 0;
198 strncpy(temp, name, sizeof (temp));
199 while (1) {
200 if (udevdb_get_dev_byname(temp, path, &dev) != 0)
201 goto found;
202 /* symlink might be stale if $(udevroot) isn't cleaned; check
203 * on major/minor to see if it's the same device
204 */
205 if (dev.major == udev->major && dev.minor == udev->minor)
206 goto found;
207 snprintf (temp, sizeof(temp), "%s%d", name, ++result);
208 }
209
210found:
211 return result;
212}
213
831f800d
KS
214static void apply_format(struct udevice *udev, char *string, size_t maxsize,
215 struct sysfs_class_device *class_dev,
216 struct sysfs_device *sysfs_device)
f3b04a2e 217{
b1c5e333 218 char temp[NAME_SIZE];
27c3403d 219 char temp2[NAME_SIZE];
b1c5e333 220 char *tail;
f3b04a2e 221 char *pos;
a27cd06c 222 char *attr;
63ead27c 223 int len;
88f09368 224 int i;
a27cd06c 225 char c;
ef672b3d 226 char *spos;
558f80ba 227 char *rest;
ef672b3d 228 int slen;
a27cd06c 229 struct sysfs_attribute *tmpattr;
0a8dd7f3 230 unsigned int next_free_number;
f3b04a2e 231
8ffb636f 232 pos = string;
f3b04a2e 233 while (1) {
a36a3c3a
KS
234 pos = strchr(pos, '%');
235 if (pos == NULL)
a27cd06c 236 break;
b1c5e333 237
a36a3c3a
KS
238 pos[0] = '\0';
239 tail = pos+1;
240 len = get_format_len(&tail);
241 c = tail[0];
242 strfieldcpy(temp, tail+1);
243 tail = temp;
244 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
a27cd06c
KS
245 attr = get_format_attribute(&tail);
246
a36a3c3a 247
a27cd06c
KS
248 switch (c) {
249 case 'b':
250 if (strlen(udev->bus_id) == 0)
f3b04a2e 251 break;
17794d77 252 strfieldcatmax(string, udev->bus_id, maxsize);
a27cd06c
KS
253 dbg("substitute bus_id '%s'", udev->bus_id);
254 break;
255 case 'k':
256 if (strlen(udev->kernel_name) == 0)
3e540368 257 break;
17794d77 258 strfieldcatmax(string, udev->kernel_name, maxsize);
a27cd06c
KS
259 dbg("substitute kernel name '%s'", udev->kernel_name);
260 break;
261 case 'n':
262 if (strlen(udev->kernel_number) == 0)
f3b04a2e 263 break;
17794d77 264 strfieldcatmax(string, udev->kernel_number, maxsize);
a27cd06c 265 dbg("substitute kernel number '%s'", udev->kernel_number);
f3b04a2e 266 break;
a27cd06c 267 case 'm':
17794d77 268 strintcatmax(string, udev->minor, maxsize);
a27cd06c
KS
269 dbg("substitute minor number '%u'", udev->minor);
270 break;
c58e36c0 271 case 'M':
17794d77 272 strintcatmax(string, udev->major, maxsize);
a27cd06c
KS
273 dbg("substitute major number '%u'", udev->major);
274 break;
275 case 'c':
276 if (strlen(udev->program_result) == 0)
f3b04a2e 277 break;
88f09368 278 /* get part part of the result string */
63ead27c 279 i = 0;
88f09368 280 if (attr != NULL)
558f80ba 281 i = strtoul(attr, &rest, 10);
88f09368 282 if (i > 0) {
9fe3f9a9 283 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
88f09368 284 i--;
9fe3f9a9 285 if (i == 0)
a27cd06c 286 break;
b1c5e333 287 }
9fe3f9a9
KS
288 if (i > 0) {
289 dbg("requested part of result string not found");
290 break;
a27cd06c 291 }
558f80ba
KS
292 if (rest[0] == '+')
293 strfieldcpy(temp2, spos);
294 else
295 strfieldcpymax(temp2, spos, slen+1);
17794d77 296 strfieldcatmax(string, temp2, maxsize);
27c3403d 297 dbg("substitute part of result string '%s'", temp2);
a27cd06c 298 } else {
17794d77 299 strfieldcatmax(string, udev->program_result, maxsize);
a27cd06c 300 dbg("substitute result string '%s'", udev->program_result);
f3b04a2e 301 }
f3b04a2e 302 break;
a27cd06c
KS
303 case 's':
304 if (attr != NULL) {
305 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
306 if (tmpattr == NULL) {
307 dbg("sysfa attribute '%s' not found", attr);
308 break;
309 }
17794d77 310 strfieldcatmax(string, tmpattr->value, maxsize);
a27cd06c
KS
311 dbg("substitute sysfs value '%s'", tmpattr->value);
312 } else {
313 dbg("missing attribute");
314 }
315 break;
316 case '%':
17794d77 317 strfieldcatmax(string, "%", maxsize);
a36a3c3a 318 pos++;
a27cd06c 319 break;
0a8dd7f3
DZ
320 case 'e':
321 next_free_number = find_free_number(udev, string);
322 if (next_free_number > 0) {
323 snprintf(temp2, sizeof(temp2), "%d", next_free_number);
324 strfieldcatmax(string, temp2, maxsize);
325 }
326 break;
a27cd06c
KS
327 default:
328 dbg("unknown substitution type '%%%c'", c);
329 break;
330 }
63ead27c
KS
331 /* truncate to specified length */
332 if (len > 0)
333 pos[len] = '\0';
334
17794d77 335 strfieldcatmax(string, tail, maxsize);
f3b04a2e
GKH
336 }
337}
338
bb051f66
GKH
339/*
340 * Note, we can have multiple files for different busses in here due
341 * to the mess that USB has for its device tree...
342 */
dac056aa
GKH
343static struct bus_file {
344 char *bus;
345 char *file;
346} bus_files[] = {
347 { .bus = "scsi", .file = "vendor" },
348 { .bus = "usb", .file = "idVendor" },
bb051f66 349 { .bus = "usb", .file = "iInterface" },
b7824727 350 { .bus = "usb-serial", .file = "detach_state" },
2441c207 351 { .bus = "ide", .file = "detach_state" },
48076332 352 { .bus = "pci", .file = "vendor" },
dac056aa
GKH
353 {}
354};
355
356#define SECONDS_TO_WAIT_FOR_FILE 10
357static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
358{
359 /* sleep until we see the file for this specific bus type show up this
360 * is needed because we can easily out-run the kernel in looking for
361 * these files before the paticular subsystem has created them in the
362 * sysfs tree properly.
363 *
364 * And people thought that the /sbin/hotplug event system was going to
365 * be slow, poo on you for arguing that before even testing it...
366 */
367 struct bus_file *b = &bus_files[0];
368 struct sysfs_attribute *tmpattr;
bb051f66
GKH
369 int found = 0;
370 int loop = SECONDS_TO_WAIT_FOR_FILE;
dac056aa
GKH
371
372 while (1) {
bb051f66
GKH
373 if (b->bus == NULL) {
374 if (!found)
375 break;
376 /* sleep to give the kernel a chance to create the file */
377 sleep(1);
378 --loop;
379 if (loop == 0)
380 break;
381 b = &bus_files[0];
382 }
dac056aa 383 if (strcmp(sysfs_device->bus, b->bus) == 0) {
bb051f66
GKH
384 found = 1;
385 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
386 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
387 if (tmpattr) {
388 /* found it! */
389 goto exit;
dac056aa 390 }
bb051f66 391 dbg("can't find '%s' file", b->file);
dac056aa 392 }
bb051f66 393 ++b;
dac056aa 394 }
bb051f66
GKH
395 if (!found)
396 dbg("did not find bus type '%s' on list of bus_id_files, "
397 "contact greg@kroah.com", sysfs_device->bus);
dac056aa
GKH
398exit:
399 return; /* here to prevent compiler warning... */
400}
19dc5d4c 401
1b941027
GKH
402static void fix_kernel_name(struct udevice *udev)
403{
404 char *temp = udev->kernel_name;
405
406 while (*temp != 0x00) {
407 /* Some block devices have a ! in their name,
408 * we need to change that to / */
409 if (*temp == '!')
410 *temp = '/';
411 ++temp;
412 }
413}
414
ac28b86d 415static int execute_program(char *path, char *value, int len)
c27e6911
PM
416{
417 int retval;
35b38379 418 int count;
c27e6911
PM
419 int status;
420 int fds[2];
421 pid_t pid;
20524642 422 char *pos;
35b38379
KS
423 char arg[PROGRAM_SIZE];
424 char *argv[sizeof(arg) / 2];
bc434511 425 int i;
c27e6911 426
35b38379
KS
427 i = 0;
428 if (strchr(path, ' ')) {
429 strfieldcpy(arg, path);
430 pos = arg;
431 while (pos != NULL) {
432 if (pos[0] == '\'') {
433 /* don't separate if in apostrophes */
434 pos++;
435 argv[i] = strsep(&pos, "\'");
436 while (pos[0] == ' ')
437 pos++;
438 } else {
439 argv[i] = strsep(&pos, " ");
440 }
441 dbg("arg[%i] '%s'", i, argv[i]);
442 i++;
443 }
444 }
445 argv[i] = NULL;
446
c27e6911
PM
447 retval = pipe(fds);
448 if (retval != 0) {
449 dbg("pipe failed");
450 return -1;
451 }
35b38379 452
c27e6911 453 pid = fork();
2a25816f
KS
454 switch(pid) {
455 case 0:
8f43a65e 456 /* child */
c27e6911 457 close(STDOUT_FILENO);
dde05ccb
GKH
458
459 /* dup write side of pipe to STDOUT */
460 dup(fds[1]);
35b38379
KS
461 if (argv[0] != NULL) {
462 dbg("execute '%s' with given arguments", argv[0]);
463 retval = execv(argv[0], argv);
bc434511 464 } else {
35b38379 465 dbg("execute '%s' with main argument", path);
2a25816f 466 retval = execv(path, main_argv);
bc434511 467 }
35b38379 468
dde05ccb 469 info(FIELD_PROGRAM " execution of '%s' failed", path);
2a25816f
KS
470 exit(1);
471 case -1:
472 dbg("fork failed");
473 return -1;
474 default:
8f43a65e 475 /* parent reads from fds[0] */
c27e6911
PM
476 close(fds[1]);
477 retval = 0;
35b38379 478 i = 0;
c27e6911 479 while (1) {
35b38379
KS
480 count = read(fds[0], value + i, len - i-1);
481 if (count <= 0)
c27e6911 482 break;
35b38379
KS
483
484 i += count;
485 if (i >= len-1) {
ac28b86d 486 dbg("result len %d too short", len);
c27e6911 487 retval = -1;
35b38379 488 break;
c27e6911
PM
489 }
490 }
35b38379
KS
491
492 if (count < 0) {
493 dbg("read failed with '%s'", strerror(errno));
c27e6911
PM
494 retval = -1;
495 }
496
bbbe503e 497 if (i > 0 && value[i-1] == '\n')
35b38379
KS
498 i--;
499 value[i] = '\0';
500 dbg("result is '%s'", value);
501
502 close(fds[0]);
503 wait(&status);
504
c27e6911 505 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
ac28b86d 506 dbg("exec program status 0x%x", status);
c27e6911
PM
507 retval = -1;
508 }
509 }
510 return retval;
511}
512
a27cd06c 513static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
ca1cc0fe
GKH
514{
515 struct sysfs_attribute *tmpattr = NULL;
a8b01705
GKH
516 char *c;
517
a27cd06c 518 dbg("look for device attribute '%s'", attr);
a8b01705 519 /* try to find the attribute in the class device directory */
a27cd06c 520 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
a8b01705 521 if (tmpattr)
a27cd06c 522 goto attr_found;
a8b01705
GKH
523
524 /* look in the class device directory if present */
525 if (sysfs_device) {
a27cd06c 526 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
a8b01705 527 if (tmpattr)
a27cd06c 528 goto attr_found;
a8b01705 529 }
a8b01705 530
a27cd06c
KS
531 return NULL;
532
533attr_found:
534 c = strchr(tmpattr->value, '\n');
535 if (c != NULL)
536 c[0] = '\0';
537
538 dbg("found attribute '%s'", tmpattr->path);
539 return tmpattr;
540}
541
542static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
543{
544 struct sysfs_attribute *tmpattr;
d5f91372
KS
545 int i;
546 int len;
a27cd06c
KS
547
548 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
549 return -ENODEV;
550
551 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
552 if (tmpattr == NULL)
553 return -ENODEV;
554
d5f91372
KS
555 /* strip trailing whitespace of value, if not asked to match for it */
556 if (! isspace(pair->value[strlen(pair->value)-1])) {
557 i = len = strlen(tmpattr->value);
558 while (i > 0 && isspace(tmpattr->value[i-1]))
559 i--;
560 if (i < len) {
561 tmpattr->value[i] = '\0';
562 dbg("remove %i trailing whitespace chars from '%s'",
563 len - i, tmpattr->value);
564 }
565 }
566
a8b01705
GKH
567 dbg("compare attribute '%s' value '%s' with '%s'",
568 pair->file, tmpattr->value, pair->value);
569 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
570 return -ENODEV;
571
572 dbg("found matching attribute '%s' with value '%s'",
573 pair->file, pair->value);
574 return 0;
575}
576
ac28b86d 577static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
a8b01705
GKH
578{
579 struct sysfs_pair *pair;
a8b01705 580 int i;
137af0cc 581
ac28b86d
KS
582 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
583 pair = &dev->sysfs_pair[i];
584 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
585 break;
586 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
587 dbg("sysfs attribute doesn't match");
588 return -ENODEV;
ca1cc0fe 589 }
ca1cc0fe 590 }
ac28b86d
KS
591
592 return 0;
ca1cc0fe
GKH
593}
594
ac28b86d 595static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
ca1cc0fe 596{
ca1cc0fe 597 char path[SYSFS_PATH_MAX];
ca1cc0fe
GKH
598 char *temp = NULL;
599
1f7148c7 600 /* we have to have a sysfs device for ID to work */
ca1cc0fe
GKH
601 if (!sysfs_device)
602 return -ENODEV;
603
ac28b86d
KS
604 strfieldcpy(path, sysfs_device->path);
605 temp = strrchr(path, '/');
3a8c51a7 606 temp++;
ac28b86d 607 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
3a8c51a7 608 if (strcmp_pattern(dev->id, temp) != 0)
ac28b86d 609 return -ENODEV;
5a42932b
PM
610 else
611 return 0;
ca1cc0fe
GKH
612}
613
ac28b86d 614static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
8c51bbfe 615{
8c51bbfe
GKH
616 char path[SYSFS_PATH_MAX];
617 int found;
618 char *temp = NULL;
619
1f7148c7 620 /* we have to have a sysfs device for PLACE to work */
8c51bbfe
GKH
621 if (!sysfs_device)
622 return -ENODEV;
623
ac28b86d
KS
624 found = 0;
625 strfieldcpy(path, sysfs_device->path);
626 temp = strrchr(path, '/');
627 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
628 if (strstr(temp, dev->place) != NULL) {
629 found = 1;
630 } else {
631 *temp = 0x00;
8c51bbfe 632 temp = strrchr(path, '/');
19feb351 633 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
ac28b86d 634 if (strstr(temp, dev->place) != NULL)
8c51bbfe 635 found = 1;
8c51bbfe 636 }
ac28b86d
KS
637 if (!found) {
638 dbg("place doesn't match");
639 return -ENODEV;
120d45d0 640 }
120d45d0 641
ac28b86d 642 return 0;
09e52d51
KS
643}
644
9a52390c 645static int whitelist_search(struct sysfs_class_device *class_dev)
646{
fcf3ce34 647 char *sysblock = "/sys/block";
9a52390c 648 int i;
9a52390c 649
650 static char *list[] = {
651 "nb",
652 "ram",
653 "loop",
654 "fd",
655 "md",
656 "dos_cd",
657 "double",
658 "flash",
659 "msd",
660 "rflash",
661 "rom",
662 "rrom",
663 "sbpcd",
664 "pcd",
665 "pf",
666 "scd",
667 "ubd",
fcf3ce34 668 NULL,
9a52390c 669 };
670
fcf3ce34
GKH
671 if (strncmp(class_dev->path, sysblock, strlen(sysblock)))
672 return 0;
673
674 for (i=0; list[i] != NULL; i++)
675 if (!strncmp(class_dev->name, list[i], strlen(list[i])))
676 return 1;
9a52390c 677
678 return 0;
679}
680
dac056aa 681static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
185a35a4 682{
dac056aa
GKH
683 struct sysfs_device *sysfs_device;
684 struct sysfs_class_device *class_dev_parent;
616a7078 685 struct timespec tspec;
dac056aa 686 int loop;
07562d6e
GKH
687
688 /* Figure out where the device symlink is at. For char devices this will
689 * always be in the class_dev->path. But for block devices, it's different.
690 * The main block device will have the device symlink in it's path, but
691 * all partitions have the symlink in its parent directory.
692 * But we need to watch out for block devices that do not have parents, yet
693 * look like a partition (fd0, loop0, etc.) They all do not have a device
9a52390c 694 * symlink yet. We do sit and spin on waiting for them right now unless
695 * they happen to be in the whitelist in which case we exit.
dac056aa 696 */
616a7078 697 class_dev_parent = sysfs_get_classdev_parent(class_dev);
949e32f2
KS
698 if (class_dev_parent != NULL)
699 dbg("given class device has a parent, use this instead");
1edbb8d3 700
616a7078
AM
701 tspec.tv_sec = 0;
702 tspec.tv_nsec = 10000000; /* sleep 10 millisec */
703 loop = 10;
dac056aa 704 while (loop--) {
9a52390c 705 if (udev_sleep) {
706 if (whitelist_search(class_dev)) {
707 sysfs_device = NULL;
708 goto exit;
709 }
961e4784 710 nanosleep(&tspec, NULL);
9a52390c 711 }
949e32f2 712
616a7078
AM
713 if (class_dev_parent)
714 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
715 else
716 sysfs_device = sysfs_get_classdev_device(class_dev);
dac056aa 717 if (sysfs_device != NULL)
616a7078 718 goto device_found;
7bd22a78 719 }
54988802 720 dbg("timed out waiting for device symlink, continuing on anyway...");
949e32f2 721
616a7078
AM
722device_found:
723 /* We have another issue with just the wait above - the sysfs part of
724 * the kernel may not be quick enough to have created the link to the
725 * device under the "bus" subsystem. Due to this, the sysfs_device->bus
726 * will not contain the actual bus name :(
616a7078
AM
727 */
728 if (sysfs_device) {
616a7078
AM
729 if (sysfs_device->bus[0] != '\0')
730 goto bus_found;
267f534d 731
616a7078
AM
732 loop = 10;
733 tspec.tv_nsec = 10000000;
734 while (loop--) {
961e4784
GKH
735 if (udev_sleep)
736 nanosleep(&tspec, NULL);
616a7078
AM
737 sysfs_get_device_bus(sysfs_device);
738
739 if (sysfs_device->bus[0] != '\0')
740 goto bus_found;
741 }
54988802 742 dbg("timed out waiting to find the device bus, continuing on anyway");
616a7078
AM
743 goto exit;
744bus_found:
54988802 745 dbg("device %s is registered with bus '%s'",
616a7078
AM
746 sysfs_device->name, sysfs_device->bus);
747 }
dac056aa
GKH
748exit:
749 return sysfs_device;
750}
751
724257d9 752static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
dac056aa 753{
724257d9 754 while (1) {
ac28b86d
KS
755 /* check for matching bus value */
756 if (dev->bus[0] != '\0') {
757 if (sysfs_device == NULL) {
758 dbg("device has no bus");
c5118442 759 goto try_parent;
ac28b86d
KS
760 }
761 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
762 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
763 dbg(FIELD_BUS " is not matching");
c5118442 764 goto try_parent;
ac28b86d
KS
765 } else {
766 dbg(FIELD_BUS " matches");
767 }
768 }
120d45d0 769
ac28b86d
KS
770 /* check for matching kernel name*/
771 if (dev->kernel[0] != '\0') {
772 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name);
773 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
774 dbg(FIELD_KERNEL " is not matching");
c5118442 775 goto try_parent;
ac28b86d
KS
776 } else {
777 dbg(FIELD_KERNEL " matches");
778 }
779 }
ca1cc0fe 780
ac28b86d
KS
781 /* check for matching bus id */
782 if (dev->id[0] != '\0') {
783 dbg("check " FIELD_ID);
784 if (match_id(dev, class_dev, sysfs_device) != 0) {
785 dbg(FIELD_ID " is not matching");
c5118442 786 goto try_parent;
ac28b86d
KS
787 } else {
788 dbg(FIELD_ID " matches");
789 }
790 }
ca1cc0fe 791
ac28b86d
KS
792 /* check for matching place of device */
793 if (dev->place[0] != '\0') {
794 dbg("check " FIELD_PLACE);
795 if (match_place(dev, class_dev, sysfs_device) != 0) {
796 dbg(FIELD_PLACE " is not matching");
c5118442 797 goto try_parent;
ac28b86d
KS
798 } else {
799 dbg(FIELD_PLACE " matches");
800 }
801 }
8c51bbfe 802
ac28b86d
KS
803 /* check for matching sysfs pairs */
804 if (dev->sysfs_pair[0].file[0] != '\0') {
805 dbg("check " FIELD_SYSFS " pairs");
806 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
807 dbg(FIELD_SYSFS " is not matching");
c5118442 808 goto try_parent;
ac28b86d
KS
809 } else {
810 dbg(FIELD_SYSFS " matches");
811 }
812 }
813
814 /* execute external program */
815 if (dev->program[0] != '\0') {
816 dbg("check " FIELD_PROGRAM);
831f800d
KS
817 apply_format(udev, dev->program, sizeof(dev->program),
818 class_dev, sysfs_device);
ac28b86d 819 if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
35b38379 820 dbg(FIELD_PROGRAM " returned nonzero");
c5118442 821 goto try_parent;
ac28b86d
KS
822 } else {
823 dbg(FIELD_PROGRAM " returned successful");
824 }
825 }
826
827 /* check for matching result of external program */
828 if (dev->result[0] != '\0') {
829 dbg("check for " FIELD_RESULT
830 " dev->result='%s', udev->program_result='%s'",
831 dev->result, udev->program_result);
832 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
833 dbg(FIELD_RESULT " is not matching");
c5118442 834 goto try_parent;
ac28b86d
KS
835 } else {
836 dbg(FIELD_RESULT " matches");
837 }
838 }
839
724257d9
GKH
840 /* Yeah, we matched! */
841 return 0;
842
c5118442
KS
843try_parent:
844 dbg("try parent sysfs device");
724257d9
GKH
845 sysfs_device = sysfs_get_device_parent(sysfs_device);
846 if (sysfs_device == NULL)
847 return -ENODEV;
848 dbg("sysfs_device->path='%s'", sysfs_device->path);
849 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
850 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
851 }
852
853}
854
855int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
856{
857 struct sysfs_device *sysfs_device = NULL;
858 struct config_device *dev;
859 struct perm_device *perm;
bbbe503e 860 struct sysinfo info;
724257d9
GKH
861 char *pos;
862
863 udev->mode = 0;
864
865 /* find the sysfs_device associated with this class device */
866 sysfs_device = get_sysfs_device(class_dev);
867 if (sysfs_device) {
868 dbg("sysfs_device->path='%s'", sysfs_device->path);
869 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
870 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
871 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
872 wait_for_device_to_initialize(sysfs_device);
724257d9 873 }
1b941027 874 dbg("class_dev->name = '%s'", class_dev->name);
724257d9
GKH
875
876 strfieldcpy(udev->kernel_name, class_dev->name);
1b941027
GKH
877 fix_kernel_name(udev);
878 dbg("udev->kernel_name = '%s'", udev->kernel_name);
724257d9
GKH
879
880 /* get kernel number */
881 pos = class_dev->name + strlen(class_dev->name);
882 while (isdigit(*(pos-1)))
883 pos--;
884 strfieldcpy(udev->kernel_number, pos);
885 dbg("kernel_number='%s'", udev->kernel_number);
886
887 /* look for a matching rule to apply */
888 list_for_each_entry(dev, &config_device_list, node) {
889 dbg("process rule");
724257d9 890 if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
97ed02ee 891 if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
c5118442 892 info("configured rule in '%s' at line %i applied, '%s' is ignored",
bd5f8e7c 893 dev->config_file, dev->config_line, udev->kernel_name);
c5118442
KS
894 return -1;
895 }
896
97ed02ee 897 if (dev->symlink[0] != '\0') {
615ba3e8 898 char temp[NAME_SIZE];
ddd5b5dc 899
97ed02ee 900 info("configured rule in '%s' at line %i applied, added symlink '%s'",
bd5f8e7c 901 dev->config_file, dev->config_line, dev->symlink);
ddd5b5dc
KS
902 strfieldcpy(temp, dev->symlink);
903 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
0a5417a0
KS
904 if (udev->symlink[0] != '\0')
905 strfieldcat(udev->symlink, " ");
ddd5b5dc 906 strfieldcat(udev->symlink, temp);
97ed02ee 907 }
908
909 if (dev->name[0] != '\0') {
949e32f2
KS
910 /* apply all_partitions flag only at a main block device */
911 if (dev->partitions > 0 &&
912 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
913 continue;
914
97ed02ee 915 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
bd5f8e7c 916 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
97ed02ee 917 strfieldcpy(udev->name, dev->name);
918 goto found;
919 }
724257d9 920 }
ac28b86d 921 }
ac28b86d 922 /* no rule was found so we use the kernel name */
7a4877bf 923 strfieldcpy(udev->name, udev->kernel_name);
bbbe503e
KS
924 if (udev->type == 'n')
925 goto done;
926 else
927 goto perms;
c2405f50 928
09e52d51 929found:
0a5417a0 930 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
1e799bab
KS
931 strfieldcpy(udev->config_file, dev->config_file);
932 udev->config_line = dev->config_line;
f61d732a
KS
933
934 if (udev->type == 'n')
bbbe503e 935 goto done;
f61d732a 936
50e5de03 937 udev->partitions = dev->partitions;
e41016d3 938
e41016d3
KS
939 /* get permissions given in rule */
940 set_empty_perms(udev, dev->mode,
941 dev->owner,
942 dev->group);
943
bbbe503e 944perms:
e41016d3 945 /* get permissions given in config file or set defaults */
61219c75 946 perm = find_perm(udev->name);
e41016d3
KS
947 if (perm != NULL) {
948 set_empty_perms(udev, perm->mode,
949 perm->owner,
950 perm->group);
61219c75 951 } else {
e41016d3
KS
952 set_empty_perms(udev, get_default_mode(),
953 get_default_owner(),
954 get_default_group());
615e05f8 955 }
e41016d3 956
61219c75
GKH
957 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
958 udev->name, udev->owner, udev->group, udev->mode);
7bd22a78 959
bbbe503e 960done:
b608ade8 961 /* store time of action */
bbbe503e
KS
962 sysinfo(&info);
963 udev->config_uptime = info.uptime;
b608ade8 964
120d45d0 965 return 0;
185a35a4
GKH
966}
967
2232cac8
GKH
968int namedev_init(void)
969{
970 int retval;
28d6536a 971
e8baccca 972 retval = namedev_init_rules();
2232cac8
GKH
973 if (retval)
974 return retval;
975
976 retval = namedev_init_permissions();
977 if (retval)
978 return retval;
979
19feb351 980 dump_config_dev_list();
61219c75 981 dump_perm_dev_list();
2232cac8
GKH
982 return retval;
983}