chiark / gitweb /
[PATCH] check for strlen()==0 before accessing strlen()-1
[elogind.git] / udev_rules.c
1 /*
2  * udev_rules.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *
10  *      This program is free software; you can redistribute it and/or modify it
11  *      under the terms of the GNU General Public License as published by the
12  *      Free Software Foundation version 2 of the License.
13  * 
14  *      This program is distributed in the hope that it will be useful, but
15  *      WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *      General Public License for more details.
18  * 
19  *      You should have received a copy of the GNU General Public License along
20  *      with this program; if not, write to the Free Software Foundation, Inc.,
21  *      675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35
36 #include "libsysfs/sysfs/libsysfs.h"
37 #include "list.h"
38 #include "udev_libc_wrapper.h"
39 #include "udev.h"
40 #include "udev_utils.h"
41 #include "udev_version.h"
42 #include "logging.h"
43 #include "udev_rules.h"
44 #include "udev_db.h"
45
46
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p, const char *s)
49 {
50         if (s[0] == '\0') {
51                 while (p[0] == '*')
52                         p++;
53                 return (p[0] != '\0');
54         }
55         switch (p[0]) {
56         case '[':
57                 {
58                         int not = 0;
59                         p++;
60                         if (p[0] == '!') {
61                                 not = 1;
62                                 p++;
63                         }
64                         while ((p[0] != '\0') && (p[0] != ']')) {
65                                 int match = 0;
66                                 if (p[1] == '-') {
67                                         if ((s[0] >= p[0]) && (s[0] <= p[2]))
68                                                 match = 1;
69                                         p += 3;
70                                 } else {
71                                         match = (p[0] == s[0]);
72                                         p++;
73                                 }
74                                 if (match ^ not) {
75                                         while ((p[0] != '\0') && (p[0] != ']'))
76                                                 p++;
77                                         if (p[0] == ']')
78                                                 return strcmp_pattern(p+1, s+1);
79                                 }
80                         }
81                 }
82                 break;
83         case '*':
84                 if (strcmp_pattern(p, s+1))
85                         return strcmp_pattern(p+1, s);
86                 return 0;
87         case '\0':
88                 if (s[0] == '\0') {
89                         return 0;
90                 }
91                 break;
92         default:
93                 if ((p[0] == s[0]) || (p[0] == '?'))
94                         return strcmp_pattern(p+1, s+1);
95                 break;
96         }
97         return 1;
98 }
99
100 /* extract possible {attr} and move str behind it */
101 static char *get_format_attribute(char **str)
102 {
103         char *pos;
104         char *attr = NULL;
105
106         if (*str[0] == '{') {
107                 pos = strchr(*str, '}');
108                 if (pos == NULL) {
109                         err("missing closing brace for format");
110                         return NULL;
111                 }
112                 pos[0] = '\0';
113                 attr = *str+1;
114                 *str = pos+1;
115                 dbg("attribute='%s', str='%s'", attr, *str);
116         }
117         return attr;
118 }
119
120 /* extract possible format length and move str behind it*/
121 static int get_format_len(char **str)
122 {
123         int num;
124         char *tail;
125
126         if (isdigit(*str[0])) {
127                 num = (int) strtoul(*str, &tail, 10);
128                 if (num > 0) {
129                         *str = tail;
130                         dbg("format length=%i", num);
131                         return num;
132                 } else {
133                         err("format parsing error '%s'", *str);
134                 }
135         }
136         return -1;
137 }
138
139 /** Finds the lowest positive N such that <name>N isn't present in 
140  *  $(udevroot) either as a file or a symlink.
141  *
142  *  @param  name                Name to check for
143  *  @return                     0 if <name> didn't exist and N otherwise.
144  */
145 static int find_free_number(struct udevice *udev, const char *name)
146 {
147         char devpath[PATH_SIZE];
148         char filename[PATH_SIZE];
149         int num = 0;
150
151         strlcpy(filename, name, sizeof(filename));
152         while (1) {
153                 dbg("look for existing node '%s'", filename);
154                 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
155                         dbg("free num=%d", num);
156                         return num;
157                 }
158
159                 num++;
160                 if (num > 1000) {
161                         info("find_free_number gone crazy (num=%d), aborted", num);
162                         return -1;
163                 }
164                 snprintf(filename, sizeof(filename), "%s%d", name, num);
165                 filename[sizeof(filename)-1] = '\0';
166         }
167 }
168
169 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
170                                 const char *name, char *value, size_t len)
171 {
172         struct sysfs_attribute *tmpattr;
173
174         dbg("look for device attribute '%s'", name);
175         if (class_dev) {
176                 tmpattr = sysfs_get_classdev_attr(class_dev, name);
177                 if (tmpattr)
178                         goto attr_found;
179         }
180         if (sysfs_device) {
181                 tmpattr = sysfs_get_device_attr(sysfs_device, name);
182                 if (tmpattr)
183                         goto attr_found;
184         }
185
186         return -1;
187
188 attr_found:
189         strlcpy(value, tmpattr->value, len);
190         remove_trailing_char(value, '\n');
191
192         dbg("found attribute '%s'", tmpattr->path);
193         return 0;
194 }
195
196 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
197                          struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
198 {
199         char temp[PATH_SIZE];
200         char temp2[PATH_SIZE];
201         char *tail, *pos, *cpos, *attr, *rest;
202         int len;
203         int i;
204         char c;
205         unsigned int next_free_number;
206         struct sysfs_class_device *class_dev_parent;
207
208         pos = string;
209         while (1) {
210                 pos = strchr(pos, '%');
211                 if (pos == NULL)
212                         break;
213
214                 pos[0] = '\0';
215                 tail = pos+1;
216                 len = get_format_len(&tail);
217                 c = tail[0];
218                 strlcpy(temp, tail+1, sizeof(temp));
219                 tail = temp;
220                 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
221                 attr = get_format_attribute(&tail);
222
223                 switch (c) {
224                 case 'p':
225                         strlcat(string, udev->devpath, maxsize);
226                         dbg("substitute kernel name '%s'", udev->kernel_name);
227                         break;
228                 case 'b':
229                         strlcat(string, udev->bus_id, maxsize);
230                         dbg("substitute bus_id '%s'", udev->bus_id);
231                         break;
232                 case 'k':
233                         strlcat(string, udev->kernel_name, maxsize);
234                         dbg("substitute kernel name '%s'", udev->kernel_name);
235                         break;
236                 case 'n':
237                         strlcat(string, udev->kernel_number, maxsize);
238                         dbg("substitute kernel number '%s'", udev->kernel_number);
239                                 break;
240                 case 'm':
241                         sprintf(temp2, "%d", minor(udev->devt));
242                         strlcat(string, temp2, maxsize);
243                         dbg("substitute minor number '%s'", temp2);
244                         break;
245                 case 'M':
246                         sprintf(temp2, "%d", major(udev->devt));
247                         strlcat(string, temp2, maxsize);
248                         dbg("substitute major number '%s'", temp2);
249                         break;
250                 case 'c':
251                         if (udev->program_result[0] == '\0')
252                                 break;
253                         /* get part part of the result string */
254                         i = 0;
255                         if (attr != NULL)
256                                 i = strtoul(attr, &rest, 10);
257                         if (i > 0) {
258                                 dbg("request part #%d of result string", i);
259                                 cpos = udev->program_result;
260                                 while (--i) {
261                                         while (cpos[0] != '\0' && !isspace(cpos[0]))
262                                                 cpos++;
263                                         while (isspace(cpos[0]))
264                                                 cpos++;
265                                 }
266                                 if (i > 0) {
267                                         err("requested part of result string not found");
268                                         break;
269                                 }
270                                 strlcpy(temp2, cpos, sizeof(temp2));
271                                 /* %{2+}c copies the whole string from the second part on */
272                                 if (rest[0] != '+') {
273                                         cpos = strchr(temp2, ' ');
274                                         if (cpos)
275                                                 cpos[0] = '\0';
276                                 }
277                                 strlcat(string, temp2, maxsize);
278                                 dbg("substitute part of result string '%s'", temp2);
279                         } else {
280                                 strlcat(string, udev->program_result, maxsize);
281                                 dbg("substitute result string '%s'", udev->program_result);
282                         }
283                         break;
284                 case 's':
285                         if (attr == NULL) {
286                                 dbg("missing attribute");
287                                 break;
288                         }
289                         if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
290                                 struct sysfs_device *parent_device;
291
292                                 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
293                                 parent_device = sysfs_get_device_parent(sysfs_device);
294                                 while (parent_device) {
295                                         dbg("looking at '%s'", parent_device->path);
296                                         if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
297                                                 break;
298                                         parent_device = sysfs_get_device_parent(parent_device);
299                                 }
300                                 if (!parent_device)
301                                         break;
302                         }
303                         /* strip trailing whitespace of sysfs value */
304                         i = strlen(temp2);
305                         while (i > 0 && isspace(temp2[i-1]))
306                                 temp2[--i] = '\0';
307                         replace_untrusted_chars(temp2);
308                         strlcat(string, temp2, maxsize);
309                         dbg("substitute sysfs value '%s'", temp2);
310                         break;
311                 case '%':
312                         strlcat(string, "%", maxsize);
313                         pos++;
314                         break;
315                 case 'e':
316                         next_free_number = find_free_number(udev, string);
317                         if (next_free_number > 0) {
318                                 sprintf(temp2, "%d", next_free_number);
319                                 strlcat(string, temp2, maxsize);
320                         }
321                         break;
322                 case 'P':
323                         if (!class_dev)
324                                 break;
325                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
326                         if (class_dev_parent != NULL) {
327                                 struct udevice udev_parent;
328
329                                 dbg("found parent '%s', get the node name", class_dev_parent->path);
330                                 udev_init_device(&udev_parent, NULL, NULL);
331                                 /* lookup the name in the udev_db with the DEVPATH of the parent */
332                                 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
333                                         strlcat(string, udev_parent.name, maxsize);
334                                         dbg("substitute parent node name'%s'", udev_parent.name);
335                                 } else
336                                         dbg("parent not found in database");
337                                 udev_cleanup_device(&udev_parent);
338                         }
339                         break;
340                 case 'N':
341                         if (udev->tmp_node[0] == '\0') {
342                                 dbg("create temporary device node for callout");
343                                 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
344                                          udev_root, major(udev->devt), minor(udev->devt));
345                                 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
346                                 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
347                         }
348                         strlcat(string, udev->tmp_node, maxsize);
349                         dbg("substitute temporary device node name '%s'", udev->tmp_node);
350                         break;
351                 case 'r':
352                         strlcat(string, udev_root, maxsize);
353                         dbg("substitute udev_root '%s'", udev_root);
354                         break;
355                 default:
356                         err("unknown substitution type '%%%c'", c);
357                         break;
358                 }
359                 /* truncate to specified length */
360                 if (len > 0)
361                         pos[len] = '\0';
362
363                 strlcat(string, tail, maxsize);
364         }
365 }
366
367 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
368 {
369         int retval;
370         int count;
371         int status;
372         int fds[2];
373         pid_t pid;
374         char *pos;
375         char arg[PATH_SIZE];
376         char *argv[(sizeof(arg) / 2) + 1];
377         int i;
378
379         strlcpy(arg, path, sizeof(arg));
380         i = 0;
381         if (strchr(path, ' ')) {
382                 pos = arg;
383                 while (pos != NULL) {
384                         if (pos[0] == '\'') {
385                                 /* don't separate if in apostrophes */
386                                 pos++;
387                                 argv[i] = strsep(&pos, "\'");
388                                 while (pos && pos[0] == ' ')
389                                         pos++;
390                         } else {
391                                 argv[i] = strsep(&pos, " ");
392                         }
393                         dbg("arg[%i] '%s'", i, argv[i]);
394                         i++;
395                 }
396                 argv[i] =  NULL;
397                 dbg("execute '%s' with parsed arguments", arg);
398         } else {
399                 argv[0] = arg;
400                 argv[1] = udev->subsystem;
401                 argv[2] = NULL;
402                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
403         }
404
405         retval = pipe(fds);
406         if (retval != 0) {
407                 err("pipe failed");
408                 return -1;
409         }
410
411         pid = fork();
412         switch(pid) {
413         case 0:
414                 /* child dup2 write side of pipe to STDOUT */
415                 dup2(fds[1], STDOUT_FILENO);
416                 retval = execv(arg, argv);
417
418                 info(KEY_PROGRAM " execution of '%s' failed", path);
419                 exit(1);
420         case -1:
421                 err("fork of '%s' failed", path);
422                 return -1;
423         default:
424                 /* parent reads from fds[0] */
425                 close(fds[1]);
426                 retval = 0;
427                 i = 0;
428                 while (1) {
429                         count = read(fds[0], value + i, len - i-1);
430                         if (count < 0) {
431                                 err("read failed with '%s'", strerror(errno));
432                                 retval = -1;
433                         }
434
435                         if (count == 0)
436                                 break;
437
438                         i += count;
439                         if (i >= len-1) {
440                                 err("result len %d too short", len);
441                                 retval = -1;
442                                 break;
443                         }
444                 }
445                 value[i] = '\0';
446
447                 close(fds[0]);
448                 waitpid(pid, &status, 0);
449
450                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
451                         dbg("exec program status 0x%x", status);
452                         retval = -1;
453                 }
454         }
455
456         if (!retval) {
457                 remove_trailing_char(value, '\n');
458                 dbg("result is '%s'", value);
459                 replace_untrusted_chars(value);
460         } else
461                 value[0] = '\0';
462
463         return retval;
464 }
465
466 static int match_rule(struct udevice *udev, struct udev_rule *rule,
467                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
468 {
469         struct sysfs_device *parent_device = sysfs_device;
470
471         if (rule->kernel_operation != KEY_OP_UNSET) {
472                 dbg("check for " KEY_KERNEL " rule->kernel='%s' class_dev->name='%s'",
473                     rule->kernel, class_dev->name);
474                 if (strcmp_pattern(rule->kernel, class_dev->name) != 0) {
475                         dbg(KEY_KERNEL " is not matching");
476                         if (rule->kernel_operation != KEY_OP_NOMATCH)
477                                 goto exit;
478                 } else {
479                         dbg(KEY_KERNEL " matches");
480                         if (rule->kernel_operation == KEY_OP_NOMATCH)
481                                 goto exit;
482                 }
483                 dbg(KEY_KERNEL " key is true");
484         }
485
486         if (rule->subsystem_operation != KEY_OP_UNSET) {
487                 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' class_dev->name='%s'",
488                     rule->subsystem, class_dev->name);
489                 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
490                         dbg(KEY_SUBSYSTEM " is not matching");
491                         if (rule->subsystem_operation != KEY_OP_NOMATCH)
492                                 goto exit;
493                 } else {
494                         dbg(KEY_SUBSYSTEM " matches");
495                         if (rule->subsystem_operation == KEY_OP_NOMATCH)
496                                 goto exit;
497                 }
498                 dbg(KEY_SUBSYSTEM " key is true");
499         }
500
501         if (rule->env_pair_count) {
502                 int i;
503
504                 dbg("check for " KEY_ENV " pairs");
505                 for (i = 0; i < rule->env_pair_count; i++) {
506                         struct key_pair *pair;
507                         const char *value;
508
509                         pair = &rule->env_pair[i];
510                         value = getenv(pair->name);
511                         if (!value) {
512                                 dbg(KEY_ENV "{'%s'} is not found", pair->name);
513                                 goto exit;
514                         }
515                         if (strcmp_pattern(pair->value, value) != 0) {
516                                 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
517                                 if (pair->operation != KEY_OP_NOMATCH)
518                                         goto exit;
519                         } else {
520                                 dbg(KEY_ENV "{'%s'} matches", pair->name);
521                                 if (pair->operation == KEY_OP_NOMATCH)
522                                         goto exit;
523                         }
524                 }
525                 dbg(KEY_ENV " key is true");
526         }
527
528         /* walk up the chain of physical devices and find a match */
529         while (1) {
530                 /* check for matching driver */
531                 if (rule->driver_operation != KEY_OP_UNSET) {
532                         if (parent_device == NULL) {
533                                 dbg("device has no sysfs_device");
534                                 goto exit;
535                         }
536                         dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
537                             rule->driver, parent_device->driver_name);
538                         if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
539                                 dbg(KEY_DRIVER " is not matching");
540                                 if (rule->driver_operation != KEY_OP_NOMATCH)
541                                         goto try_parent;
542                         } else {
543                                 dbg(KEY_DRIVER " matches");
544                                 if (rule->driver_operation == KEY_OP_NOMATCH)
545                                         goto try_parent;
546                         }
547                         dbg(KEY_DRIVER " key is true");
548                 }
549
550                 /* check for matching bus value */
551                 if (rule->bus_operation != KEY_OP_UNSET) {
552                         if (parent_device == NULL) {
553                                 dbg("device has no sysfs_device");
554                                 goto exit;
555                         }
556                         dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
557                             rule->bus, parent_device->bus);
558                         if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
559                                 dbg(KEY_BUS " is not matching");
560                                 if (rule->bus_operation != KEY_OP_NOMATCH)
561                                         goto try_parent;
562                         } else {
563                                 dbg(KEY_BUS " matches");
564                                 if (rule->bus_operation == KEY_OP_NOMATCH)
565                                         goto try_parent;
566                         }
567                         dbg(KEY_BUS " key is true");
568                 }
569
570                 /* check for matching bus id */
571                 if (rule->id_operation != KEY_OP_UNSET) {
572                         if (parent_device == NULL) {
573                                 dbg("device has no sysfs_device");
574                                 goto exit;
575                         }
576                         dbg("check " KEY_ID);
577                         if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
578                                 dbg(KEY_ID " is not matching");
579                                 if (rule->id_operation != KEY_OP_NOMATCH)
580                                         goto try_parent;
581                         } else {
582                                 dbg(KEY_ID " matches");
583                                 if (rule->id_operation == KEY_OP_NOMATCH)
584                                         goto try_parent;
585                         }
586                         dbg(KEY_ID " key is true");
587                 }
588
589                 /* check for matching sysfs pairs */
590                 if (rule->sysfs_pair_count) {
591                         int i;
592
593                         dbg("check " KEY_SYSFS " pairs");
594                         for (i = 0; i < rule->sysfs_pair_count; i++) {
595                                 struct key_pair *pair;
596                                 char value[VALUE_SIZE];
597                                 size_t len;
598
599                                 pair = &rule->sysfs_pair[i];
600                                 if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
601                                         goto try_parent;
602
603                                 /* strip trailing whitespace of value, if not asked to match for it */
604                                 len = strlen(pair->value);
605                                 if (len && !isspace(pair->value[len-1])) {
606                                         len = strlen(value);
607                                         while (len > 0 && isspace(value[len-1]))
608                                                 value[--len] = '\0';
609                                         dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
610                                 }
611
612                                 dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
613                                 if (strcmp_pattern(pair->value, value) != 0) {
614                                         dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
615                                         if (pair->operation != KEY_OP_NOMATCH)
616                                                 goto try_parent;
617                                 } else {
618                                         dbg(KEY_SYSFS "{'%s'} matches", pair->name);
619                                         if (pair->operation == KEY_OP_NOMATCH)
620                                                 goto try_parent;
621                                 }
622                         }
623                         dbg(KEY_SYSFS " keys are true");
624                 }
625
626                 /* found matching physical device  */
627                 break;
628 try_parent:
629                 dbg("try parent sysfs device");
630                 parent_device = sysfs_get_device_parent(parent_device);
631                 if (parent_device == NULL)
632                         goto exit;
633                 dbg("look at sysfs_device->path='%s'", parent_device->path);
634                 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
635         }
636
637         /* execute external program */
638         if (rule->program_operation != KEY_OP_UNSET) {
639                 char program[PATH_SIZE];
640
641                 dbg("check " KEY_PROGRAM);
642                 strlcpy(program, rule->program, sizeof(program));
643                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
644                 if (execute_program(udev, program, udev->program_result, sizeof(udev->program_result)) != 0) {
645                         dbg(KEY_PROGRAM " returned nonzero");
646                         if (rule->program_operation != KEY_OP_NOMATCH)
647                                 goto exit;
648                 } else {
649                         dbg(KEY_PROGRAM " returned successful");
650                         if (rule->program_operation == KEY_OP_NOMATCH)
651                                 goto exit;
652                 }
653                 dbg(KEY_PROGRAM " key is true");
654         }
655
656         /* check for matching result of external program */
657         if (rule->result_operation != KEY_OP_UNSET) {
658                 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
659                    rule->result, udev->program_result);
660                 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
661                         dbg(KEY_RESULT " is not matching");
662                         if (rule->result_operation != KEY_OP_NOMATCH)
663                                 goto exit;
664                 } else {
665                         dbg(KEY_RESULT " matches");
666                         if (rule->result_operation == KEY_OP_NOMATCH)
667                                 goto exit;
668                 }
669                 dbg(KEY_RESULT " key is true");
670         }
671
672         /* rule matches */
673         return 0;
674
675 exit:
676         return -1;
677 }
678
679 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
680 {
681         struct sysfs_class_device *class_dev_parent;
682         struct sysfs_device *sysfs_device = NULL;
683         struct udev_rule *rule;
684
685         dbg("class_dev->name='%s'", class_dev->name);
686
687         /* Figure out where the "device"-symlink is at.  For char devices this will
688          * always be in the class_dev->path.  On block devices, only the main block
689          * device will have the device symlink in it's path. All partition devices
690          * need to look at the symlink in its parent directory.
691          */
692         class_dev_parent = sysfs_get_classdev_parent(class_dev);
693         if (class_dev_parent != NULL) {
694                 dbg("given class device has a parent, use this instead");
695                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
696         } else {
697                 sysfs_device = sysfs_get_classdev_device(class_dev);
698         }
699
700         if (sysfs_device) {
701                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
702                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
703                 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
704         }
705
706         dbg("udev->kernel_name='%s'", udev->kernel_name);
707
708         /* look for a matching rule to apply */
709         list_for_each_entry(rule, &udev_rule_list, node) {
710                 dbg("process rule");
711                 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
712
713                         /* apply options */
714                         if (rule->ignore_device) {
715                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
716                                      rule->config_file, rule->config_line, udev->kernel_name);
717                                 return -1;
718                         }
719                         if (rule->ignore_remove) {
720                                 udev->ignore_remove = 1;
721                                 dbg("remove event should be ignored");
722                         }
723                         /* apply all_partitions option only at a main block device */
724                         if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
725                                 udev->partitions = rule->partitions;
726                                 dbg("creation of partition nodes requested");
727                         }
728
729                         /* apply permissions */
730                         if (rule->mode != 0000) {
731                                 udev->mode = rule->mode;
732                                 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
733                         }
734                         if (rule->owner[0] != '\0') {
735                                 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
736                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
737                                 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
738                         }
739                         if (rule->group[0] != '\0') {
740                                 strlcpy(udev->group, rule->group, sizeof(udev->group));
741                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
742                                 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
743                         }
744
745                         /* collect symlinks */
746                         if (rule->symlink[0] != '\0') {
747                                 char temp[PATH_SIZE];
748                                 char *pos, *next;
749
750                                 info("configured rule in '%s[%i]' applied, added symlink '%s'",
751                                      rule->config_file, rule->config_line, rule->symlink);
752                                 strlcpy(temp, rule->symlink, sizeof(temp));
753                                 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
754
755                                 /* add multiple symlinks separated by spaces */
756                                 pos = temp;
757                                 next = strchr(temp, ' ');
758                                 while (next) {
759                                         next[0] = '\0';
760                                         info("add symlink '%s'", pos);
761                                         name_list_add(&udev->symlink_list, pos, 0);
762                                         pos = &next[1];
763                                         next = strchr(pos, ' ');
764                                 }
765                                 info("add symlink '%s'", pos);
766                                 name_list_add(&udev->symlink_list, pos, 0);
767                         }
768
769                         /* rule matches */
770                         if (rule->name[0] != '\0') {
771                                 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
772                                      rule->config_file, rule->config_line, udev->kernel_name, rule->name);
773
774                                 strlcpy(udev->name, rule->name, sizeof(udev->name));
775                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
776                                 strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
777                                 udev->config_line = rule->config_line;
778
779                                 if (udev->type != DEV_NET)
780                                         dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
781                                             udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
782
783                                 break;
784                         }
785
786                         if (rule->last_rule) {
787                                 dbg("last rule to be applied");
788                                 break;
789                         }
790
791                 }
792         }
793
794         if (udev->name[0] == '\0') {
795                 /* no rule matched, so we use the kernel name */
796                 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
797                 info("no rule found, use kernel name '%s'", udev->name);
798         }
799
800         if (udev->tmp_node[0] != '\0') {
801                 dbg("removing temporary device node");
802                 unlink_secure(udev->tmp_node);
803                 udev->tmp_node[0] = '\0';
804         }
805
806         return 0;
807 }