]> git.ipfire.org Git - thirdparty/systemd.git/blob - namedev.c
[PATCH] start working on label support, and fix some segfaults for block devices.
[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
33 #include "list.h"
34 #include "udev.h"
35 #include "udev_version.h"
36 #include "namedev.h"
37 #include "libsysfs/libsysfs.h"
38
39 #define TYPE_LABEL "LABEL"
40 #define TYPE_NUMBER "NUMBER"
41 #define TYPE_TOPOLOGY "TOPOLOGY"
42 #define TYPE_REPLACE "REPLACE"
43
44 static LIST_HEAD(config_device_list);
45
46 static void dump_dev(struct config_device *dev)
47 {
48 switch (dev->type) {
49 case KERNEL_NAME:
50 dbg("KERNEL name ='%s'"
51 " owner = '%s', group = '%s', mode = '%#o'",
52 dev->attr.name,
53 dev->attr.owner, dev->attr.group, dev->attr.mode);
54 break;
55 case LABEL:
56 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
57 " owner = '%s', group = '%s', mode = '%#o'",
58 dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
59 dev->attr.owner, dev->attr.group, dev->attr.mode);
60 break;
61 case NUMBER:
62 dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
63 " owner = '%s', group = '%s', mode = '%#o'",
64 dev->attr.name, dev->bus, dev->id,
65 dev->attr.owner, dev->attr.group, dev->attr.mode);
66 break;
67 case TOPOLOGY:
68 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
69 " owner = '%s', group = '%s', mode = '%#o'",
70 dev->attr.name, dev->bus, dev->place,
71 dev->attr.owner, dev->attr.group, dev->attr.mode);
72 break;
73 case REPLACE:
74 dbg("REPLACE name = %s, kernel_name = %s"
75 " owner = '%s', group = '%s', mode = '%#o'",
76 dev->attr.name, dev->kernel_name,
77 dev->attr.owner, dev->attr.group, dev->attr.mode);
78 break;
79 default:
80 dbg("Unknown type of device!");
81 }
82 }
83
84 #define copy_var(a, b, var) \
85 if (b->var) \
86 a->var = b->var;
87
88 #define copy_string(a, b, var) \
89 if (strlen(b->var)) \
90 strcpy(a->var, b->var);
91
92 static int add_dev(struct config_device *new_dev)
93 {
94 struct list_head *tmp;
95 struct config_device *tmp_dev;
96
97 /* loop through the whole list of devices to see if we already have
98 * this one... */
99 list_for_each(tmp, &config_device_list) {
100 struct config_device *dev = list_entry(tmp, struct config_device, node);
101 if (strcmp(dev->attr.name, new_dev->attr.name) == 0) {
102 /* the same, copy the new info into this structure */
103 copy_var(dev, new_dev, type);
104 copy_var(dev, new_dev, attr.mode);
105 copy_string(dev, new_dev, bus);
106 copy_string(dev, new_dev, sysfs_file);
107 copy_string(dev, new_dev, sysfs_value);
108 copy_string(dev, new_dev, id);
109 copy_string(dev, new_dev, place);
110 copy_string(dev, new_dev, kernel_name);
111 copy_string(dev, new_dev, attr.owner);
112 copy_string(dev, new_dev, attr.group);
113 return 0;
114 }
115 }
116
117 /* not found, lets create a new structure, and add it to the list */
118 tmp_dev = malloc(sizeof(*tmp_dev));
119 if (!tmp_dev)
120 return -ENOMEM;
121 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
122 list_add(&tmp_dev->node, &config_device_list);
123 //dump_dev(tmp_dev);
124 return 0;
125 }
126
127 static void dump_dev_list(void)
128 {
129 struct list_head *tmp;
130
131 list_for_each(tmp, &config_device_list) {
132 struct config_device *dev = list_entry(tmp, struct config_device, node);
133 dump_dev(dev);
134 }
135 }
136
137 static int get_value(const char *left, char **orig_string, char **ret_string)
138 {
139 char *temp;
140 char *string = *orig_string;
141
142 /* eat any whitespace */
143 while (isspace(*string))
144 ++string;
145
146 /* split based on '=' */
147 temp = strsep(&string, "=");
148 if (strcasecmp(temp, left) == 0) {
149 /* got it, now strip off the '"' */
150 while (isspace(*string))
151 ++string;
152 if (*string == '"')
153 ++string;
154 temp = strsep(&string, "\"");
155 *ret_string = temp;
156 *orig_string = string;
157 return 0;
158 }
159 return -ENODEV;
160 }
161
162 static int get_pair(char **orig_string, char **left, char **right)
163 {
164 char *temp;
165 char *string = *orig_string;
166
167 /* eat any whitespace */
168 while (isspace(*string))
169 ++string;
170
171 /* split based on '=' */
172 temp = strsep(&string, "=");
173 *left = temp;
174
175 /* take the right side and strip off the '"' */
176 while (isspace(*string))
177 ++string;
178 if (*string == '"')
179 ++string;
180 temp = strsep(&string, "\"");
181 *right = temp;
182 *orig_string = string;
183
184 return 0;
185 }
186
187 static int namedev_init_config(void)
188 {
189 char filename[255];
190 char line[255];
191 char *temp;
192 char *temp2;
193 char *temp3;
194 FILE *fd;
195 int retval = 0;
196 struct config_device dev;
197
198 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_FILE);
199 dbg("opening %s to read as permissions config", filename);
200 fd = fopen(filename, "r");
201 if (fd == NULL) {
202 dbg("Can't open %s", filename);
203 return -ENODEV;
204 }
205
206 /* loop through the whole file */
207 while (1) {
208 /* get a line */
209 temp = fgets(line, sizeof(line), fd);
210 if (temp == NULL)
211 break;
212
213 dbg("read %s", temp);
214
215 /* eat the whitespace at the beginning of the line */
216 while (isspace(*temp))
217 ++temp;
218
219 /* no more line? */
220 if (*temp == 0x00)
221 continue;
222
223 /* see if this is a comment */
224 if (*temp == COMMENT_CHARACTER)
225 continue;
226
227 memset(&dev, 0x00, sizeof(struct config_device));
228
229 /* parse the line */
230 temp2 = strsep(&temp, ",");
231 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
232 /* label type */
233 dev.type = LABEL;
234
235 /* BUS="bus" */
236 retval = get_value("BUS", &temp, &temp3);
237 if (retval)
238 continue;
239 strcpy(dev.bus, temp3);
240
241 /* file="value" */
242 temp2 = strsep(&temp, ",");
243 retval = get_pair(&temp, &temp2, &temp3);
244 if (retval)
245 continue;
246 strcpy(dev.sysfs_file, temp2);
247 strcpy(dev.sysfs_value, temp3);
248
249 /* NAME="new_name" */
250 temp2 = strsep(&temp, ",");
251 retval = get_value("NAME", &temp, &temp3);
252 if (retval)
253 continue;
254 strcpy(dev.attr.name, temp3);
255
256 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.attr.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
257 }
258
259 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
260 /* number type */
261 dev.type = NUMBER;
262
263 /* BUS="bus" */
264 retval = get_value("BUS", &temp, &temp3);
265 if (retval)
266 continue;
267 strcpy(dev.bus, temp3);
268
269 /* ID="id" */
270 temp2 = strsep(&temp, ",");
271 retval = get_value("id", &temp, &temp3);
272 if (retval)
273 continue;
274 strcpy(dev.id, temp3);
275
276 /* NAME="new_name" */
277 temp2 = strsep(&temp, ",");
278 retval = get_value("NAME", &temp, &temp3);
279 if (retval)
280 continue;
281 strcpy(dev.attr.name, temp3);
282
283 dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.attr.name, dev.bus, dev.id);
284 }
285
286 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
287 /* number type */
288 dev.type = TOPOLOGY;
289
290 /* BUS="bus" */
291 retval = get_value("BUS", &temp, &temp3);
292 if (retval)
293 continue;
294 strcpy(dev.bus, temp3);
295
296 /* PLACE="place" */
297 temp2 = strsep(&temp, ",");
298 retval = get_value("place", &temp, &temp3);
299 if (retval)
300 continue;
301 strcpy(dev.place, temp3);
302
303 /* NAME="new_name" */
304 temp2 = strsep(&temp, ",");
305 retval = get_value("NAME", &temp, &temp3);
306 if (retval)
307 continue;
308 strcpy(dev.attr.name, temp3);
309
310 dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.attr.name, dev.bus, dev.place);
311 }
312
313 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
314 /* number type */
315 dev.type = REPLACE;
316
317 /* KERNEL="kernel_name" */
318 retval = get_value("KERNEL", &temp, &temp3);
319 if (retval)
320 continue;
321 strcpy(dev.kernel_name, temp3);
322
323 /* NAME="new_name" */
324 temp2 = strsep(&temp, ",");
325 retval = get_value("NAME", &temp, &temp3);
326 if (retval)
327 continue;
328 strcpy(dev.attr.name, temp3);
329 dbg("REPLACE name = %s, kernel_name = %s", dev.attr.name, dev.kernel_name);
330 }
331
332 retval = add_dev(&dev);
333 if (retval) {
334 dbg("add_dev returned with error %d", retval);
335 goto exit;
336 }
337 }
338
339 exit:
340 fclose(fd);
341 return retval;
342 }
343
344
345 static int namedev_init_permissions(void)
346 {
347 char filename[255];
348 char line[255];
349 char *temp;
350 char *temp2;
351 FILE *fd;
352 int retval = 0;
353 struct config_device dev;
354
355 strcpy(filename, NAMEDEV_CONFIG_ROOT NAMEDEV_CONFIG_PERMISSION_FILE);
356 dbg("opening %s to read as permissions config", filename);
357 fd = fopen(filename, "r");
358 if (fd == NULL) {
359 dbg("Can't open %s", filename);
360 return -ENODEV;
361 }
362
363 /* loop through the whole file */
364 while (1) {
365 /* get a line */
366 temp = fgets(line, sizeof(line), fd);
367 if (temp == NULL)
368 break;
369
370 dbg("read %s", temp);
371
372 /* eat the whitespace at the beginning of the line */
373 while (isspace(*temp))
374 ++temp;
375
376 /* no more line? */
377 if (*temp == 0x00)
378 continue;
379
380 /* see if this is a comment */
381 if (*temp == COMMENT_CHARACTER)
382 continue;
383
384 memset(&dev, 0x00, sizeof(dev));
385
386 /* parse the line */
387 temp2 = strsep(&temp, ":");
388 strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
389
390 temp2 = strsep(&temp, ":");
391 strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
392
393 temp2 = strsep(&temp, ":");
394 strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
395
396 dev.attr.mode = strtol(temp, NULL, 8);
397
398 dbg("name = %s, owner = %s, group = %s, mode = %#o", dev.attr.name, dev.attr.owner, dev.attr.group, dev.attr.mode);
399 retval = add_dev(&dev);
400 if (retval) {
401 dbg("add_dev returned with error %d", retval);
402 goto exit;
403 }
404 }
405
406 exit:
407 fclose(fd);
408 return retval;
409 }
410
411 static int get_default_mode(struct sysfs_class_device *class_dev)
412 {
413 /* just default everyone to rw for the world! */
414 return 0666;
415 }
416
417
418 static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
419 {
420 struct list_head *tmp;
421 int retval = 0;
422 int found;
423
424 if (class_dev->sysdevice) {
425 dbg("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path);
426 dbg("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
427 } else {
428 dbg("class_dev->name = '%s'", class_dev->name);
429 }
430 list_for_each(tmp, &config_device_list) {
431 struct config_device *dev = list_entry(tmp, struct config_device, node);
432 switch (dev->type) {
433 case LABEL:
434 {
435 char *temp;
436
437 dbg("LABEL: match file '%s' with value '%s'", dev->sysfs_file, dev->sysfs_value);
438 /* try to find the attribute in the class device directory */
439 temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file);
440 if (temp)
441 goto label_found;
442
443 /* look in the class device device directory if present */
444 if (class_dev->sysdevice) {
445 temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file);
446 if (temp)
447 goto label_found;
448 }
449
450 /* bah, let's go backwards up a level to see if the device is there,
451 * as block partitions don't point to the physical device. Need to fix that
452 * up in the kernel...
453 */
454 if (strstr(class_dev->directory->path, "block")) {
455 dbg("looking at block device...");
456 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
457 char path[SYSFS_PATH_MAX];
458 struct sysfs_class_device *class_dev_parent;
459
460 dbg("really is a partition...");
461 strcpy(path, class_dev->directory->path);
462 temp = strrchr(path, '/');
463 *temp = 0x00;
464 dbg("looking for a class device at '%s'", path);
465 class_dev_parent = sysfs_open_class_device(path);
466 if (class_dev_parent == NULL) {
467 dbg ("sysfs_open_class_device failed");
468 continue;
469 }
470 dbg("class_dev_parent->name = %s", class_dev_parent->name);
471
472 /* try to find the attribute in the class device directory */
473 temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file);
474 if (temp) {
475 //sysfs_close_class_device(class_dev_parent);
476 goto label_found;
477 }
478
479 /* look in the class device device directory if present */
480 if (class_dev_parent->sysdevice) {
481 temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file);
482 if (temp) {
483 // sysfs_close_class_device(class_dev_parent);
484 goto label_found;
485 }
486 }
487
488 }
489 }
490 continue;
491
492 label_found:
493 dbg("file '%s' found with value '%s'", dev->sysfs_file, temp);
494 if (strcmp(dev->sysfs_value, temp) != 0)
495 continue;
496
497 strcpy(attr->name, dev->attr.name);
498 attr->mode = dev->attr.mode;
499 strcpy(attr->owner, dev->attr.owner);
500 strcpy(attr->group, dev->attr.group);
501 dbg("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
502 dev->sysfs_file, dev->sysfs_value, attr->name,
503 dev->attr.owner, dev->attr.group, dev->attr.mode);
504 return retval;
505 dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
506 " owner = '%s', group = '%s', mode = '%#o'",
507 dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
508 dev->attr.owner, dev->attr.group, dev->attr.mode);
509 break;
510 }
511 case NUMBER:
512 {
513 char path[SYSFS_PATH_MAX];
514 char *temp;
515
516 found = 0;
517 if (!class_dev->sysdevice)
518 continue;
519 strcpy(path, class_dev->sysdevice->directory->path);
520 temp = strrchr(path, '/');
521 dbg("NUMBER path = '%s'", path);
522 dbg("NUMBER temp = '%s' id = '%s'", temp, dev->id);
523 if (strstr(temp, dev->id) != NULL) {
524 found = 1;
525 } else {
526 *temp = 0x00;
527 temp = strrchr(path, '/');
528 dbg("TOPOLOGY temp = '%s' id = '%s'", temp, dev->id);
529 if (strstr(temp, dev->id) != NULL)
530 found = 1;
531 }
532 if (!found)
533 continue;
534
535 strcpy(attr->name, dev->attr.name);
536 attr->mode = dev->attr.mode;
537 strcpy(attr->owner, dev->attr.owner);
538 strcpy(attr->group, dev->attr.group);
539 dbg("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
540 dev->id, attr->name,
541 dev->attr.owner, dev->attr.group, dev->attr.mode);
542 return retval;
543 break;
544 }
545 case TOPOLOGY:
546 {
547 char path[SYSFS_PATH_MAX];
548 char *temp;
549
550 if (!class_dev->sysdevice)
551 continue;
552 found = 0;
553 strcpy(path, class_dev->sysdevice->directory->path);
554 temp = strrchr(path, '/');
555 dbg("TOPOLOGY path = '%s'", path);
556 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
557 if (strstr(temp, dev->place) != NULL) {
558 found = 1;
559 } else {
560 *temp = 0x00;
561 temp = strrchr(path, '/');
562 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
563 if (strstr(temp, dev->place) != NULL)
564 found = 1;
565 }
566 if (!found)
567 continue;
568
569 strcpy(attr->name, dev->attr.name);
570 attr->mode = dev->attr.mode;
571 strcpy(attr->owner, dev->attr.owner);
572 strcpy(attr->group, dev->attr.group);
573 dbg("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
574 dev->place, attr->name,
575 dev->attr.owner, dev->attr.group, dev->attr.mode);
576 return retval;
577 break;
578 }
579 case REPLACE:
580 if (strcmp(dev->kernel_name, class_dev->name) != 0)
581 continue;
582 strcpy(attr->name, dev->attr.name);
583 attr->mode = dev->attr.mode;
584 strcpy(attr->owner, dev->attr.owner);
585 strcpy(attr->group, dev->attr.group);
586 dbg("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
587 dev->kernel_name, attr->name,
588 dev->attr.owner, dev->attr.group, dev->attr.mode);
589 return retval;
590 break;
591 case KERNEL_NAME:
592 break;
593 default:
594 dbg("Unknown type of device '%d'", dev->type);
595 break;
596 }
597 }
598 attr->mode = get_default_mode(class_dev);
599 attr->owner[0] = 0x00;
600 attr->group[0] = 0x00;
601 strcpy(attr->name, class_dev->name);
602 return retval;
603 }
604
605 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
606 {
607 int retval;
608
609 retval = get_attr(class_dev, attr);
610 if (retval)
611 dbg("get_attr failed");
612
613 return retval;
614 }
615
616 int namedev_init(void)
617 {
618 int retval;
619
620 retval = namedev_init_config();
621 if (retval)
622 return retval;
623
624 retval = namedev_init_permissions();
625 if (retval)
626 return retval;
627
628 dump_dev_list();
629 return retval;
630 }
631
632