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