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