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