chiark / gitweb /
[PATCH] do not build the tdb binary programs, only the objects.
[elogind.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         attr->mode = -1;
425         if (class_dev->sysdevice) {
426                 dbg("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path);
427                 dbg("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
428         } else {
429                 dbg("class_dev->name = '%s'", class_dev->name);
430         }
431         list_for_each(tmp, &config_device_list) {
432                 struct config_device *dev = list_entry(tmp, struct config_device, node);
433                 switch (dev->type) {
434                 case LABEL:
435                         {
436                         char *temp;
437
438                         dbg("LABEL: match file '%s' with value '%s'", dev->sysfs_file, dev->sysfs_value);
439                         /* try to find the attribute in the class device directory */
440                         temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file);
441                         if (temp)
442                                 goto label_found;
443
444                         /* look in the class device device directory if present */
445                         if (class_dev->sysdevice) {
446                                 temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file);
447                                 if (temp)
448                                         goto label_found;
449                         }
450
451                         /* bah, let's go backwards up a level to see if the device is there,
452                          * as block partitions don't point to the physical device.  Need to fix that
453                          * up in the kernel...
454                          */
455                         if (strstr(class_dev->directory->path, "block")) {
456                                 dbg("looking at block device...");
457                                 if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
458                                         char path[SYSFS_PATH_MAX];
459                                         struct sysfs_class_device *class_dev_parent;
460
461                                         dbg("really is a partition...");
462                                         strcpy(path, class_dev->directory->path);
463                                         temp = strrchr(path, '/');
464                                         *temp = 0x00;
465                                         dbg("looking for a class device at '%s'", path);
466                                         class_dev_parent = sysfs_open_class_device(path);
467                                         if (class_dev_parent == NULL) {
468                                                 dbg ("sysfs_open_class_device failed");
469                                                 continue;
470                                         }
471                                         dbg("class_dev_parent->name = %s", class_dev_parent->name);
472
473                                         /* try to find the attribute in the class device directory */
474                                         temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file);
475                                         if (temp) {
476                                                 //sysfs_close_class_device(class_dev_parent);
477                                                 goto label_found;
478                                         }
479
480                                         /* look in the class device device directory if present */
481                                         if (class_dev_parent->sysdevice) {
482                                                 temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file);
483                                                 if (temp) {
484                                                         // sysfs_close_class_device(class_dev_parent);
485                                                         goto label_found;
486                                                 }
487                                         }
488                                         
489                                 }
490                         }
491                         continue;
492
493 label_found:
494                         temp[strlen(temp)-1] = 0x00;
495                         dbg("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, temp, dev->sysfs_value);
496                         if (strcmp(dev->sysfs_value, temp) != 0)
497                                 continue;
498
499                         strcpy(attr->name, dev->attr.name);
500                         if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
501                                 temp[0] = class_dev->directory->path[strlen(class_dev->directory->path)-1];
502                                 temp[1] = 0x00;
503                                 strcat(attr->name, temp);
504                         }
505                         if (dev->attr.mode != 0) {
506                                 attr->mode = dev->attr.mode;
507                                 strcpy(attr->owner, dev->attr.owner);
508                                 strcpy(attr->group, dev->attr.group);
509                         }
510                         dbg("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
511                                 dev->sysfs_file, dev->sysfs_value, attr->name, 
512                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
513                         goto done;
514                         break;
515                         }
516                 case NUMBER:
517                         {
518                         char path[SYSFS_PATH_MAX];
519                         char *temp;
520
521                         found = 0;
522                         if (!class_dev->sysdevice)
523                                 continue;
524                         strcpy(path, class_dev->sysdevice->directory->path);
525                         temp = strrchr(path, '/');
526                         dbg("NUMBER path = '%s'", path);
527                         dbg("NUMBER temp = '%s' id = '%s'", temp, dev->id);
528                         if (strstr(temp, dev->id) != NULL) {
529                                 found = 1;
530                         } else {
531                                 *temp = 0x00;
532                                 temp = strrchr(path, '/');
533                                 dbg("TOPOLOGY temp = '%s' id = '%s'", temp, dev->id);
534                                 if (strstr(temp, dev->id) != NULL)
535                                         found = 1;
536                         }
537                         if (!found)
538                                 continue;
539
540                         strcpy(attr->name, dev->attr.name);
541                         if (dev->attr.mode != 0) {
542                                 attr->mode = dev->attr.mode;
543                                 strcpy(attr->owner, dev->attr.owner);
544                                 strcpy(attr->group, dev->attr.group);
545                         }
546                         dbg("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
547                                 dev->id, attr->name, 
548                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
549                         goto done;
550                         break;
551                         }
552                 case TOPOLOGY:
553                         {
554                         char path[SYSFS_PATH_MAX];
555                         char *temp;
556
557                         if (!class_dev->sysdevice)
558                                 continue;
559                         found = 0;      
560                         strcpy(path, class_dev->sysdevice->directory->path);
561                         temp = strrchr(path, '/');
562                         dbg("TOPOLOGY path = '%s'", path);
563                         dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
564                         if (strstr(temp, dev->place) != NULL) {
565                                 found = 1;
566                         } else {
567                                 *temp = 0x00;
568                                 temp = strrchr(path, '/');
569                                 dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
570                                 if (strstr(temp, dev->place) != NULL)
571                                         found = 1;
572                         }
573                         if (!found)
574                                 continue;
575
576                         strcpy(attr->name, dev->attr.name);
577                         if (dev->attr.mode != 0) {
578                                 attr->mode = dev->attr.mode;
579                                 strcpy(attr->owner, dev->attr.owner);
580                                 strcpy(attr->group, dev->attr.group);
581                         }
582                         dbg("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
583                                 dev->place, attr->name, 
584                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
585                         goto done;
586                         break;
587                         }
588                 case REPLACE:
589                         if (strcmp(dev->kernel_name, class_dev->name) != 0)
590                                 continue;
591                         strcpy(attr->name, dev->attr.name);
592                         if (dev->attr.mode != 0) {
593                                 attr->mode = dev->attr.mode;
594                                 strcpy(attr->owner, dev->attr.owner);
595                                 strcpy(attr->group, dev->attr.group);
596                         }
597                         dbg("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
598                                 dev->kernel_name, attr->name, 
599                                 dev->attr.owner, dev->attr.group, dev->attr.mode);
600                         goto done;
601                         break;
602                 case KERNEL_NAME:
603                         break;
604                 default:
605                         dbg("Unknown type of device '%d'", dev->type);
606                         break;
607                 }       
608         }
609         strcpy(attr->name, class_dev->name);
610         
611 done:
612         if (attr->mode == -1) { 
613                 attr->mode = get_default_mode(class_dev);
614                 attr->owner[0] = 0x00;
615                 attr->group[0] = 0x00;
616         }
617         return retval;
618 }
619
620 int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
621 {
622         int retval;
623
624         retval = get_attr(class_dev, attr);
625         if (retval)
626                 dbg("get_attr failed");
627
628         return retval;
629 }
630
631 int namedev_init(void)
632 {
633         int retval;
634         
635         retval = namedev_init_config();
636         if (retval)
637                 return retval;
638
639         retval = namedev_init_permissions();
640         if (retval)
641                 return retval;
642
643         dump_dev_list();
644         return retval;
645 }
646
647