chiark / gitweb /
[PATCH] 009_bk makefile changes.
[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 #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         /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */
193         dig = class_dev->name + strlen(class_dev->name);
194         while (isdigit(*(dig-1)))
195                 dig--;
196         strfieldcpy(udev->kernel_number, dig);
197         dbg("kernel_number='%s'", udev->kernel_number);
198 }
199
200 static void apply_format(struct udevice *udev, unsigned char *string)
201 {
202         char name[NAME_SIZE];
203         char temp[NAME_SIZE];
204         char *tail;
205         char *pos;
206         char *pos2;
207         char *pos3;
208         int num;
209
210         while (1) {
211                 num = 0;
212                 pos = strchr(string, '%');
213
214                 if (pos) {
215                         *pos = '\0';
216                         tail = pos+1;
217                         if (isdigit(tail[0])) {
218                                 num = (int) strtoul(&pos[1], &tail, 10);
219                                 if (tail == NULL) {
220                                         dbg("format parsing error '%s'", pos+1);
221                                         break;
222                                 }
223                         }
224                         strfieldcpy(name, tail+1);
225
226                         switch (tail[0]) {
227                         case 'b':
228                                 if (strlen(udev->bus_id) == 0)
229                                         break;
230                                 strcat(pos, udev->bus_id);
231                                 dbg("substitute bus_id '%s'", udev->bus_id);
232                                 break;
233                         case 'n':
234                                 if (strlen(udev->kernel_number) == 0)
235                                         break;
236                                 strcat(pos, udev->kernel_number);
237                                 dbg("substitute kernel number '%s'", udev->kernel_number);
238                                 break;
239                         case 'D':
240                                 if (strlen(udev->kernel_number) == 0) {
241                                         strcat(pos, "disc");
242                                         break;
243                                 }
244                                 strcat(pos, "part");
245                                 strcat(pos, udev->kernel_number);
246                                 dbg("substitute kernel number '%s'", udev->kernel_number);
247                                 break;
248                         case 'm':
249                                 sprintf(pos, "%u", udev->minor);
250                                 dbg("substitute minor number '%u'", udev->minor);
251                                 break;
252                         case 'M':
253                                 sprintf(pos, "%u", udev->major);
254                                 dbg("substitute major number '%u'", udev->major);
255                                 break;
256                         case 'c':
257                                 if (strlen(udev->callout_value) == 0)
258                                         break;
259                                 if (num) {
260                                         /* get part of return string */
261                                         strncpy(temp, udev->callout_value, sizeof(temp));
262                                         pos2 = temp;
263                                         while (num) {
264                                                 num--;
265                                                 pos3 = strsep(&pos2, " ");
266                                                 if (pos3 == NULL) {
267                                                         dbg("requested part of callout string not found");
268                                                         break;
269                                                 }
270                                         }
271                                         strcat(pos, pos3);
272                                         dbg("substitute partial callout output '%s'", pos3);
273                                 } else {
274                                         strcat(pos, udev->callout_value);
275                                         dbg("substitute callout output '%s'", udev->callout_value);
276                                 }
277                                 break;
278                         default:
279                                 dbg("unknown substitution type '%%%c'", pos[1]);
280                                 break;
281                         }
282                         strcat(string, name);
283                 } else
284                         break;
285         }
286 }
287
288
289 static int exec_callout(struct config_device *dev, char *value, int len)
290 {
291         int retval;
292         int res;
293         int status;
294         int fds[2];
295         pid_t pid;
296         int value_set = 0;
297         char buffer[256];
298         char *pos;
299         char *args[CALLOUT_MAXARG];
300         int i;
301
302         dbg("callout to '%s'", dev->exec_program);
303         retval = pipe(fds);
304         if (retval != 0) {
305                 dbg("pipe failed");
306                 return -1;
307         }
308         pid = fork();
309         if (pid == -1) {
310                 dbg("fork failed");
311                 return -1;
312         }
313
314         if (pid == 0) {
315                 /* child */
316                 close(STDOUT_FILENO);
317                 dup(fds[1]);    /* dup write side of pipe to STDOUT */
318                 if (strchr(dev->exec_program, ' ')) {
319                         /* callout with arguments */
320                         pos = dev->exec_program;
321                         for (i=0; i < CALLOUT_MAXARG-1; i++) {
322                                 args[i] = strsep(&pos, " ");
323                                 if (args[i] == NULL)
324                                         break;
325                         }
326                         if (args[i]) {
327                                 dbg("too many args - %d", i);
328                                 args[i] = NULL;
329                         }
330                         retval = execve(args[0], args, main_envp);
331                 } else {
332                         retval = execve(dev->exec_program, main_argv, main_envp);
333                 }
334                 if (retval != 0) {
335                         dbg("child execve failed");
336                         exit(1);
337                 }
338                 return -1; /* avoid compiler warning */
339         } else {
340                 /* parent reads from fds[0] */
341                 close(fds[1]);
342                 retval = 0;
343                 while (1) {
344                         res = read(fds[0], buffer, sizeof(buffer) - 1);
345                         if (res <= 0)
346                                 break;
347                         buffer[res] = '\0';
348                         if (res > len) {
349                                 dbg("callout len %d too short", len);
350                                 retval = -1;
351                         }
352                         if (value_set) {
353                                 dbg("callout value already set");
354                                 retval = -1;
355                         } else {
356                                 value_set = 1;
357                                 strncpy(value, buffer, len);
358                                 pos = value + strlen(value)-1;
359                                 if (pos[0] == '\n')
360                                 pos[0] = '\0';
361                                 dbg("callout returned '%s'", value);
362                         }
363                 }
364                 close(fds[0]);
365                 res = wait(&status);
366                 if (res < 0) {
367                         dbg("wait failed result %d", res);
368                         retval = -1;
369                 }
370
371 #ifndef __KLIBC__
372                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
373                         dbg("callout program status 0x%x", status);
374                         retval = -1;
375                 }
376 #endif
377         }
378         return retval;
379 }
380
381 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
382 {
383         struct config_device *dev;
384         struct list_head *tmp;
385
386         list_for_each(tmp, &config_device_list) {
387                 dev = list_entry(tmp, struct config_device, node);
388                 if (dev->type != CALLOUT)
389                         continue;
390
391                 if (sysfs_device) {
392                         dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
393                         if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
394                                 continue;
395                 }
396
397                 /* substitute anything that needs to be in the program name */
398                 apply_format(udev, dev->exec_program);
399                 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
400                         continue;
401                 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
402                         continue;
403                 strfieldcpy(udev->name, dev->name);
404                 strfieldcpy(udev->symlink, dev->symlink);
405                 dbg("callout returned matching value '%s', '%s' becomes '%s'",
406                     dev->id, class_dev->name, udev->name);
407                 return 0;
408         }
409         return -ENODEV;
410 }
411
412 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
413 {
414         struct sysfs_attribute *tmpattr = NULL;
415         struct config_device *dev;
416         struct list_head *tmp;
417         char *c;
418
419         list_for_each(tmp, &config_device_list) {
420                 dev = list_entry(tmp, struct config_device, node);
421                 if (dev->type != LABEL)
422                         continue;
423
424                 if (sysfs_device) {
425                         dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
426                         if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
427                                 continue;
428                 }
429
430                 dbg("look for device attribute '%s'", dev->sysfs_file);
431                 /* try to find the attribute in the class device directory */
432                 tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file);
433                 if (tmpattr)
434                         goto label_found;
435
436                 /* look in the class device directory if present */
437                 if (sysfs_device) {
438                         tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file);
439                         if (tmpattr)
440                                 goto label_found;
441                 }
442
443                 continue;
444
445 label_found:
446                 c = tmpattr->value + strlen(tmpattr->value)-1;
447                 if (*c == '\n')
448                         *c = 0x00;
449                 dbg("compare attribute '%s' value '%s' with '%s'",
450                           dev->sysfs_file, tmpattr->value, dev->sysfs_value);
451                 if (strcmp_pattern(dev->sysfs_value, tmpattr->value) != 0)
452                         continue;
453
454                 strfieldcpy(udev->name, dev->name);
455                 strfieldcpy(udev->symlink, dev->symlink);
456                 dbg("found matching attribute '%s', '%s' becomes '%s' ",
457                     dev->sysfs_file, class_dev->name, udev->name);
458
459                 return 0;
460         }
461         return -ENODEV;
462 }
463
464 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
465 {
466         struct config_device *dev;
467         struct list_head *tmp;
468         char path[SYSFS_PATH_MAX];
469         int found;
470         char *temp = NULL;
471
472         /* we have to have a sysfs device for NUMBER to work */
473         if (!sysfs_device)
474                 return -ENODEV;
475
476         list_for_each(tmp, &config_device_list) {
477                 dev = list_entry(tmp, struct config_device, node);
478                 if (dev->type != NUMBER)
479                         continue;
480
481                 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
482                 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
483                         continue;
484
485                 found = 0;
486                 strfieldcpy(path, sysfs_device->path);
487                 temp = strrchr(path, '/');
488                 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
489                 if (strstr(temp, dev->id) != NULL) {
490                         found = 1;
491                 } else {
492                         *temp = 0x00;
493                         temp = strrchr(path, '/');
494                         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
495                         if (strstr(temp, dev->id) != NULL)
496                                 found = 1;
497                 }
498                 if (!found)
499                         continue;
500                 strfieldcpy(udev->name, dev->name);
501                 strfieldcpy(udev->symlink, dev->symlink);
502                 dbg("found matching id '%s', '%s' becomes '%s'",
503                     dev->id, class_dev->name, udev->name);
504                 return 0;
505         }
506         return -ENODEV;
507 }
508
509 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
510 {
511         struct config_device *dev;
512         struct list_head *tmp;
513         char path[SYSFS_PATH_MAX];
514         int found;
515         char *temp = NULL;
516
517         /* we have to have a sysfs device for TOPOLOGY to work */
518         if (!sysfs_device)
519                 return -ENODEV;
520
521         list_for_each(tmp, &config_device_list) {
522                 dev = list_entry(tmp, struct config_device, node);
523                 if (dev->type != TOPOLOGY)
524                         continue;
525
526                 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
527                 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
528                         continue;
529
530                 found = 0;
531                 strfieldcpy(path, sysfs_device->path);
532                 temp = strrchr(path, '/');
533                 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
534                 if (strstr(temp, dev->place) != NULL) {
535                         found = 1;
536                 } else {
537                         *temp = 0x00;
538                         temp = strrchr(path, '/');
539                         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
540                         if (strstr(temp, dev->place) != NULL)
541                                 found = 1;
542                 }
543                 if (!found)
544                         continue;
545
546                 strfieldcpy(udev->name, dev->name);
547                 strfieldcpy(udev->symlink, dev->symlink);
548                 dbg("found matching place '%s', '%s' becomes '%s'",
549                     dev->place, class_dev->name, udev->name);
550                 return 0;
551         }
552         return -ENODEV;
553 }
554
555 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
556 {
557         struct config_device *dev;
558         struct list_head *tmp;
559
560         list_for_each(tmp, &config_device_list) {
561                 dev = list_entry(tmp, struct config_device, node);
562                 if (dev->type != REPLACE)
563                         continue;
564
565                 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
566                 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
567                         continue;
568
569                 strfieldcpy(udev->name, dev->name);
570                 strfieldcpy(udev->symlink, dev->symlink);
571                 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
572                 
573                 return 0;
574         }
575         return -ENODEV;
576 }
577
578 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
579 {
580         /* heh, this is pretty simple... */
581         strfieldcpy(udev->name, class_dev->name);
582 }
583
584 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
585 {
586         struct sysfs_device *sysfs_device = NULL;
587         struct sysfs_class_device *class_dev_parent = NULL;
588         int retval = 0;
589         struct perm_device *perm;
590
591         udev->mode = 0;
592
593         /* find the sysfs_device for this class device */
594         /* Wouldn't it really be nice if libsysfs could do this for us? */
595         sysfs_device = sysfs_get_classdev_device(class_dev);
596         if (sysfs_device == NULL) {
597                 /* bah, let's go backwards up a level to see if the device is there,
598                  * as block partitions don't point to the physical device.  Need to fix that
599                  * up in the kernel...
600                  */
601                 if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
602                         dbg("looking at block device");
603                         if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
604                                 dbg("really is a partition");
605                                 class_dev_parent = sysfs_get_classdev_parent
606                                                                    (class_dev);
607                                 if (class_dev_parent == NULL) {
608                                         dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
609                                 } else {
610                                         dbg("class_dev_parent->name='%s'", class_dev_parent->name);
611                                         sysfs_device = sysfs_get_classdev_device(class_dev_parent);
612                                 }
613                         }
614                 }
615         }
616
617         if (sysfs_device) {
618                 dbg("sysfs_device->path='%s'", sysfs_device->path);
619                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
620                 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
621                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
622         } else {
623                 dbg("class_dev->name = '%s'", class_dev->name);
624         }
625
626         build_kernel_number(class_dev, udev);
627
628         /* rules are looked at in priority order */
629         retval = do_callout(class_dev, udev, sysfs_device);
630         if (retval == 0)
631                 goto found;
632
633         retval = do_label(class_dev, udev, sysfs_device);
634         if (retval == 0)
635                 goto found;
636
637         retval = do_number(class_dev, udev, sysfs_device);
638         if (retval == 0)
639                 goto found;
640
641         retval = do_topology(class_dev, udev, sysfs_device);
642         if (retval == 0)
643                 goto found;
644
645         retval = do_replace(class_dev, udev, sysfs_device);
646         if (retval == 0)
647                 goto found;
648
649         do_kernelname(class_dev, udev);
650         goto done;
651
652 found:
653         /* substitute placeholder */
654         apply_format(udev, udev->name);
655         apply_format(udev, udev->symlink);
656
657 done:
658         perm = find_perm(udev->name);
659         if (perm) {
660                 udev->mode = perm->mode;
661                 strfieldcpy(udev->owner, perm->owner);
662                 strfieldcpy(udev->group, perm->group);
663         } else {
664                 /* no matching perms found :( */
665                 udev->mode = get_default_mode(class_dev);
666                 udev->owner[0] = 0x00;
667                 udev->group[0] = 0x00;
668         }
669         dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
670             udev->name, udev->owner, udev->group, udev->mode);
671
672         return 0;
673 }
674
675 int namedev_init(void)
676 {
677         int retval;
678
679         retval = namedev_init_rules();
680         if (retval)
681                 return retval;
682
683         retval = namedev_init_permissions();
684         if (retval)
685                 return retval;
686
687         dump_config_dev_list();
688         dump_perm_dev_list();
689         return retval;
690 }