]> git.ipfire.org Git - thirdparty/systemd.git/blob - namedev.c
[PATCH] don't overwrite old config on install
[thirdparty/systemd.git] / namedev.c
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>
32 #include <sys/wait.h>
33
34 #include "list.h"
35 #include "udev.h"
36 #include "udev_version.h"
37 #include "namedev.h"
38 #include "libsysfs/libsysfs.h"
39 #include "klibc_fixups.h"
40
41 LIST_HEAD(config_device_list);
42 LIST_HEAD(perm_device_list);
43
44 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
45 static int strcmp_pattern(const char *p, const char *s)
46 {
47 if (*s == '\0') {
48 while (*p == '*')
49 p++;
50 return (*p != '\0');
51 }
52 switch (*p) {
53 case '[':
54 {
55 int not = 0;
56 p++;
57 if (*p == '!') {
58 not = 1;
59 p++;
60 }
61 while (*p && (*p != ']')) {
62 int match = 0;
63 if (p[1] == '-') {
64 if ((*s >= *p) && (*s <= p[2]))
65 match = 1;
66 p += 3;
67 } else {
68 match = (*p == *s);
69 p++;
70 }
71 if (match ^ not) {
72 while (*p && (*p != ']'))
73 p++;
74 return strcmp_pattern(p+1, s+1);
75 }
76 }
77 }
78 break;
79 case '*':
80 if (strcmp_pattern(p, s+1))
81 return strcmp_pattern(p+1, s);
82 return 0;
83 case '\0':
84 if (*s == '\0') {
85 return 0;
86 }
87 break;
88 default:
89 if ((*p == *s) || (*p == '?'))
90 return strcmp_pattern(p+1, s+1);
91 break;
92 }
93 return 1;
94 }
95
96 #define copy_var(a, b, var) \
97 if (b->var) \
98 a->var = b->var;
99
100 #define copy_string(a, b, var) \
101 if (strlen(b->var)) \
102 strcpy(a->var, b->var);
103
104 int add_config_dev(struct config_device *new_dev)
105 {
106 struct list_head *tmp;
107 struct config_device *tmp_dev;
108
109 /* update the values if we already have the device */
110 list_for_each(tmp, &config_device_list) {
111 struct config_device *dev = list_entry(tmp, struct config_device, node);
112 if (strcmp_pattern(new_dev->name, dev->name))
113 continue;
114 if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name)))
115 continue;
116 copy_var(dev, new_dev, type);
117 copy_string(dev, new_dev, bus);
118 copy_string(dev, new_dev, sysfs_file);
119 copy_string(dev, new_dev, sysfs_value);
120 copy_string(dev, new_dev, id);
121 copy_string(dev, new_dev, place);
122 copy_string(dev, new_dev, kernel_name);
123 copy_string(dev, new_dev, exec_program);
124 copy_string(dev, new_dev, symlink);
125 return 0;
126 }
127
128 /* not found, add new structure to the device list */
129 tmp_dev = malloc(sizeof(*tmp_dev));
130 if (!tmp_dev)
131 return -ENOMEM;
132 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
133 list_add_tail(&tmp_dev->node, &config_device_list);
134 //dump_config_dev(tmp_dev);
135 return 0;
136 }
137
138 int add_perm_dev(struct perm_device *new_dev)
139 {
140 struct list_head *tmp;
141 struct perm_device *tmp_dev;
142
143 /* update the values if we already have the device */
144 list_for_each(tmp, &perm_device_list) {
145 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
146 if (strcmp_pattern(new_dev->name, dev->name))
147 continue;
148 copy_var(dev, new_dev, mode);
149 copy_string(dev, new_dev, owner);
150 copy_string(dev, new_dev, group);
151 return 0;
152 }
153
154 /* not found, add new structure to the perm list */
155 tmp_dev = malloc(sizeof(*tmp_dev));
156 if (!tmp_dev)
157 return -ENOMEM;
158 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
159 list_add_tail(&tmp_dev->node, &perm_device_list);
160 //dump_perm_dev(tmp_dev);
161 return 0;
162 }
163
164 static struct perm_device *find_perm(char *name)
165 {
166 struct list_head *tmp;
167 struct perm_device *perm = NULL;
168
169 list_for_each(tmp, &perm_device_list) {
170 perm = list_entry(tmp, struct perm_device, node);
171 if (strcmp_pattern(perm->name, name))
172 continue;
173 return perm;
174 }
175 return NULL;
176 }
177
178 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
179 {
180 mode_t mode = 0600; /* default to owner rw only */
181
182 if (strlen(default_mode_str) != 0) {
183 mode = strtol(default_mode_str, NULL, 8);
184 }
185 return mode;
186 }
187
188 static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev)
189 {
190 char *dig;
191
192 dig = class_dev->name + strlen(class_dev->name);
193 while (isdigit(*(dig-1)))
194 dig--;
195 strfieldcpy(udev->kernel_number, dig);
196 dbg("kernel_number='%s'", udev->kernel_number);
197 }
198
199 static void apply_format(struct udevice *udev, unsigned char *string)
200 {
201 char name[NAME_SIZE];
202 char temp[NAME_SIZE];
203 char *tail;
204 char *pos;
205 char *pos2;
206 char *pos3;
207 int num;
208
209 while (1) {
210 num = 0;
211 pos = strchr(string, '%');
212
213 if (pos) {
214 *pos = '\0';
215 tail = pos+1;
216 if (isdigit(tail[0])) {
217 num = (int) strtoul(&pos[1], &tail, 10);
218 if (tail == NULL) {
219 dbg("format parsing error '%s'", pos+1);
220 break;
221 }
222 }
223 strfieldcpy(name, tail+1);
224
225 switch (tail[0]) {
226 case 'b':
227 if (strlen(udev->bus_id) == 0)
228 break;
229 strcat(pos, udev->bus_id);
230 dbg("substitute bus_id '%s'", udev->bus_id);
231 break;
232 case 'n':
233 if (strlen(udev->kernel_number) == 0)
234 break;
235 strcat(pos, udev->kernel_number);
236 dbg("substitute kernel number '%s'", udev->kernel_number);
237 break;
238 case 'D':
239 if (strlen(udev->kernel_number) == 0) {
240 strcat(pos, "disc");
241 break;
242 }
243 strcat(pos, "part");
244 strcat(pos, udev->kernel_number);
245 dbg("substitute kernel number '%s'", udev->kernel_number);
246 break;
247 case 'm':
248 sprintf(pos, "%u", udev->minor);
249 dbg("substitute minor number '%u'", udev->minor);
250 break;
251 case 'M':
252 sprintf(pos, "%u", udev->major);
253 dbg("substitute major number '%u'", udev->major);
254 break;
255 case 'c':
256 if (strlen(udev->callout_value) == 0)
257 break;
258 if (num) {
259 /* get part of return string */
260 strncpy(temp, udev->callout_value, sizeof(temp));
261 pos2 = temp;
262 while (num) {
263 num--;
264 pos3 = strsep(&pos2, " ");
265 if (pos3 == NULL) {
266 dbg("requested part of callout string not found");
267 break;
268 }
269 }
270 strcat(pos, pos3);
271 dbg("substitute partial callout output '%s'", pos3);
272 } else {
273 strcat(pos, udev->callout_value);
274 dbg("substitute callout output '%s'", udev->callout_value);
275 }
276 break;
277 default:
278 dbg("unknown substitution type '%%%c'", pos[1]);
279 break;
280 }
281 strcat(string, name);
282 } else
283 break;
284 }
285 }
286
287
288 static int exec_callout(struct config_device *dev, char *value, int len)
289 {
290 int retval;
291 int res;
292 int status;
293 int fds[2];
294 pid_t pid;
295 int value_set = 0;
296 char buffer[256];
297 char *pos;
298 char *args[CALLOUT_MAXARG];
299 int i;
300
301 dbg("callout to '%s'", dev->exec_program);
302 retval = pipe(fds);
303 if (retval != 0) {
304 dbg("pipe failed");
305 return -1;
306 }
307 pid = fork();
308 if (pid == -1) {
309 dbg("fork failed");
310 return -1;
311 }
312
313 if (pid == 0) {
314 /* child */
315 close(STDOUT_FILENO);
316 dup(fds[1]); /* dup write side of pipe to STDOUT */
317 if (strchr(dev->exec_program, ' ')) {
318 /* callout with arguments */
319 pos = dev->exec_program;
320 for (i=0; i < CALLOUT_MAXARG-1; i++) {
321 args[i] = strsep(&pos, " ");
322 if (args[i] == NULL)
323 break;
324 }
325 if (args[i]) {
326 dbg("too many args - %d", i);
327 args[i] = NULL;
328 }
329 retval = execve(args[0], args, main_envp);
330 } else {
331 retval = execve(dev->exec_program, main_argv, main_envp);
332 }
333 if (retval != 0) {
334 dbg("child execve failed");
335 exit(1);
336 }
337 return -1; /* avoid compiler warning */
338 } else {
339 /* parent reads from fds[0] */
340 close(fds[1]);
341 retval = 0;
342 while (1) {
343 res = read(fds[0], buffer, sizeof(buffer) - 1);
344 if (res <= 0)
345 break;
346 buffer[res] = '\0';
347 if (res > len) {
348 dbg("callout len %d too short", len);
349 retval = -1;
350 }
351 if (value_set) {
352 dbg("callout value already set");
353 retval = -1;
354 } else {
355 value_set = 1;
356 strncpy(value, buffer, len);
357 pos = value + strlen(value)-1;
358 if (pos[0] == '\n')
359 pos[0] = '\0';
360 dbg("callout returned '%s'", value);
361 }
362 }
363 close(fds[0]);
364 res = wait(&status);
365 if (res < 0) {
366 dbg("wait failed result %d", res);
367 retval = -1;
368 }
369
370 #ifndef __KLIBC__
371 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
372 dbg("callout program status 0x%x", status);
373 retval = -1;
374 }
375 #endif
376 }
377 return retval;
378 }
379
380 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
381 {
382 struct config_device *dev;
383 struct list_head *tmp;
384
385 list_for_each(tmp, &config_device_list) {
386 dev = list_entry(tmp, struct config_device, node);
387 if (dev->type != CALLOUT)
388 continue;
389
390 if (sysfs_device) {
391 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
392 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
393 continue;
394 }
395
396 /* substitute anything that needs to be in the program name */
397 apply_format(udev, dev->exec_program);
398 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
399 continue;
400 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
401 continue;
402 strfieldcpy(udev->name, dev->name);
403 strfieldcpy(udev->symlink, dev->symlink);
404 dbg("callout returned matching value '%s', '%s' becomes '%s'",
405 dev->id, class_dev->name, udev->name);
406 return 0;
407 }
408 return -ENODEV;
409 }
410
411 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
412 {
413 struct sysfs_attribute *tmpattr = NULL;
414 struct config_device *dev;
415 struct list_head *tmp;
416 char *c;
417
418 list_for_each(tmp, &config_device_list) {
419 dev = list_entry(tmp, struct config_device, node);
420 if (dev->type != LABEL)
421 continue;
422
423 if (sysfs_device) {
424 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
425 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
426 continue;
427 }
428
429 dbg("look for device attribute '%s'", dev->sysfs_file);
430 /* try to find the attribute in the class device directory */
431 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
432 if (tmpattr)
433 goto label_found;
434
435 /* look in the class device directory if present */
436 if (sysfs_device) {
437 tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
438 if (tmpattr)
439 goto label_found;
440 }
441
442 continue;
443
444 label_found:
445 c = tmpattr->value + strlen(tmpattr->value)-1;
446 if (*c == '\n')
447 *c = 0x00;
448 dbg("compare attribute '%s' value '%s' with '%s'",
449 dev->sysfs_file, tmpattr->value, dev->sysfs_value);
450 if (strcmp_pattern(dev->sysfs_value, tmpattr->value) != 0)
451 continue;
452
453 strfieldcpy(udev->name, dev->name);
454 strfieldcpy(udev->symlink, dev->symlink);
455 dbg("found matching attribute '%s', '%s' becomes '%s' ",
456 dev->sysfs_file, class_dev->name, udev->name);
457
458 return 0;
459 }
460 return -ENODEV;
461 }
462
463 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
464 {
465 struct config_device *dev;
466 struct list_head *tmp;
467 char path[SYSFS_PATH_MAX];
468 int found;
469 char *temp = NULL;
470
471 /* we have to have a sysfs device for NUMBER to work */
472 if (!sysfs_device)
473 return -ENODEV;
474
475 list_for_each(tmp, &config_device_list) {
476 dev = list_entry(tmp, struct config_device, node);
477 if (dev->type != NUMBER)
478 continue;
479
480 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
481 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
482 continue;
483
484 found = 0;
485 strfieldcpy(path, sysfs_device->path);
486 temp = strrchr(path, '/');
487 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
488 if (strstr(temp, dev->id) != NULL) {
489 found = 1;
490 } else {
491 *temp = 0x00;
492 temp = strrchr(path, '/');
493 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
494 if (strstr(temp, dev->id) != NULL)
495 found = 1;
496 }
497 if (!found)
498 continue;
499 strfieldcpy(udev->name, dev->name);
500 strfieldcpy(udev->symlink, dev->symlink);
501 dbg("found matching id '%s', '%s' becomes '%s'",
502 dev->id, class_dev->name, udev->name);
503 return 0;
504 }
505 return -ENODEV;
506 }
507
508 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
509 {
510 struct config_device *dev;
511 struct list_head *tmp;
512 char path[SYSFS_PATH_MAX];
513 int found;
514 char *temp = NULL;
515
516 /* we have to have a sysfs device for TOPOLOGY to work */
517 if (!sysfs_device)
518 return -ENODEV;
519
520 list_for_each(tmp, &config_device_list) {
521 dev = list_entry(tmp, struct config_device, node);
522 if (dev->type != TOPOLOGY)
523 continue;
524
525 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
526 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
527 continue;
528
529 found = 0;
530 strfieldcpy(path, sysfs_device->path);
531 temp = strrchr(path, '/');
532 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
533 if (strstr(temp, dev->place) != NULL) {
534 found = 1;
535 } else {
536 *temp = 0x00;
537 temp = strrchr(path, '/');
538 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
539 if (strstr(temp, dev->place) != NULL)
540 found = 1;
541 }
542 if (!found)
543 continue;
544
545 strfieldcpy(udev->name, dev->name);
546 strfieldcpy(udev->symlink, dev->symlink);
547 dbg("found matching place '%s', '%s' becomes '%s'",
548 dev->place, class_dev->name, udev->name);
549 return 0;
550 }
551 return -ENODEV;
552 }
553
554 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
555 {
556 struct config_device *dev;
557 struct list_head *tmp;
558
559 list_for_each(tmp, &config_device_list) {
560 dev = list_entry(tmp, struct config_device, node);
561 if (dev->type != REPLACE)
562 continue;
563
564 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
565 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
566 continue;
567
568 strfieldcpy(udev->name, dev->name);
569 strfieldcpy(udev->symlink, dev->symlink);
570 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
571
572 return 0;
573 }
574 return -ENODEV;
575 }
576
577 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
578 {
579 /* heh, this is pretty simple... */
580 strfieldcpy(udev->name, class_dev->name);
581 }
582
583 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
584 {
585 struct sysfs_device *sysfs_device = NULL;
586 struct sysfs_class_device *class_dev_parent = NULL;
587 int retval = 0;
588 struct perm_device *perm;
589
590 udev->mode = 0;
591
592 /* find the sysfs_device for this class device */
593 /* Wouldn't it really be nice if libsysfs could do this for us? */
594 sysfs_device = sysfs_get_classdev_device(class_dev);
595 if (sysfs_device == NULL) {
596 /* bah, let's go backwards up a level to see if the device is there,
597 * as block partitions don't point to the physical device. Need to fix that
598 * up in the kernel...
599 */
600 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
601 dbg("looking at block device");
602 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
603 dbg("really is a partition");
604 class_dev_parent = sysfs_get_classdev_parent
605 (class_dev);
606 if (class_dev_parent == NULL) {
607 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
608 } else {
609 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
610 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
611 }
612 }
613 }
614 }
615
616 if (sysfs_device) {
617 dbg("sysfs_device->path='%s'", sysfs_device->path);
618 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
619 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
620 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
621 } else {
622 dbg("class_dev->name = '%s'", class_dev->name);
623 }
624
625 build_kernel_number(class_dev, udev);
626
627 /* rules are looked at in priority order */
628 retval = do_callout(class_dev, udev, sysfs_device);
629 if (retval == 0)
630 goto found;
631
632 retval = do_label(class_dev, udev, sysfs_device);
633 if (retval == 0)
634 goto found;
635
636 retval = do_number(class_dev, udev, sysfs_device);
637 if (retval == 0)
638 goto found;
639
640 retval = do_topology(class_dev, udev, sysfs_device);
641 if (retval == 0)
642 goto found;
643
644 retval = do_replace(class_dev, udev, sysfs_device);
645 if (retval == 0)
646 goto found;
647
648 do_kernelname(class_dev, udev);
649 goto done;
650
651 found:
652 /* substitute placeholder */
653 apply_format(udev, udev->name);
654 apply_format(udev, udev->symlink);
655
656 done:
657 perm = find_perm(udev->name);
658 if (perm) {
659 udev->mode = perm->mode;
660 strfieldcpy(udev->owner, perm->owner);
661 strfieldcpy(udev->group, perm->group);
662 } else {
663 /* no matching perms found :( */
664 udev->mode = get_default_mode(class_dev);
665 udev->owner[0] = 0x00;
666 udev->group[0] = 0x00;
667 }
668 dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
669 udev->name, udev->owner, udev->group, udev->mode);
670
671 return 0;
672 }
673
674 int namedev_init(void)
675 {
676 int retval;
677
678 retval = namedev_init_rules();
679 if (retval)
680 return retval;
681
682 retval = namedev_init_permissions();
683 if (retval)
684 return retval;
685
686 dump_config_dev_list();
687 dump_perm_dev_list();
688 return retval;
689 }