chiark / gitweb /
release 121
[elogind.git] / udev_rules_parse.c
1 /*
2  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28
29 #include "udev.h"
30 #include "udev_rules.h"
31 #include "udev_selinux.h"
32
33
34 void udev_rules_iter_init(struct udev_rules *rules)
35 {
36         dbg("bufsize=%zi\n", rules->bufsize);
37         rules->current = 0;
38 }
39
40 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
41 {
42         static struct udev_rule *rule;
43
44         if (!rules)
45                 return NULL;
46
47         dbg("current=%zi\n", rules->current);
48         if (rules->current >= rules->bufsize) {
49                 dbg("no more rules\n");
50                 return NULL;
51         }
52
53         /* get next rule */
54         rule = (struct udev_rule *) (rules->buf + rules->current);
55         rules->current += sizeof(struct udev_rule) + rule->bufsize;
56
57         return rule;
58 }
59
60 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
61 {
62         static struct udev_rule *rule;
63
64 next:
65         dbg("current=%zi\n", rules->current);
66         if (rules->current >= rules->bufsize) {
67                 dbg("no more rules\n");
68                 return NULL;
69         }
70         rule = (struct udev_rule *) (rules->buf + rules->current);
71
72         if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
73                 dbg("moving forward, looking for label '%s'\n", label);
74                 rules->current += sizeof(struct udev_rule) + rule->bufsize;
75                 goto next;
76         }
77
78         dbg("found label '%s'\n", label);
79         return rule;
80 }
81
82 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
83 {
84         char *linepos;
85         char *temp;
86
87         linepos = *line;
88         if (linepos == NULL && linepos[0] == '\0')
89                 return -1;
90
91         /* skip whitespace */
92         while (isspace(linepos[0]) || linepos[0] == ',')
93                 linepos++;
94
95         /* get the key */
96         if (linepos[0] == '\0')
97                 return -1;
98         *key = linepos;
99
100         while (1) {
101                 linepos++;
102                 if (linepos[0] == '\0')
103                         return -1;
104                 if (isspace(linepos[0]))
105                         break;
106                 if (linepos[0] == '=')
107                         break;
108                 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
109                         if (linepos[1] == '=')
110                                 break;
111         }
112
113         /* remember end of key */
114         temp = linepos;
115
116         /* skip whitespace after key */
117         while (isspace(linepos[0]))
118                 linepos++;
119         if (linepos[0] == '\0')
120                 return -1;
121
122         /* get operation type */
123         if (linepos[0] == '=' && linepos[1] == '=') {
124                 *operation = KEY_OP_MATCH;
125                 linepos += 2;
126                 dbg("operator=match\n");
127         } else if (linepos[0] == '!' && linepos[1] == '=') {
128                 *operation = KEY_OP_NOMATCH;
129                 linepos += 2;
130                 dbg("operator=nomatch\n");
131         } else if (linepos[0] == '+' && linepos[1] == '=') {
132                 *operation = KEY_OP_ADD;
133                 linepos += 2;
134                 dbg("operator=add\n");
135         } else if (linepos[0] == '=') {
136                 *operation = KEY_OP_ASSIGN;
137                 linepos++;
138                 dbg("operator=assign\n");
139         } else if (linepos[0] == ':' && linepos[1] == '=') {
140                 *operation = KEY_OP_ASSIGN_FINAL;
141                 linepos += 2;
142                 dbg("operator=assign_final\n");
143         } else
144                 return -1;
145
146         /* terminate key */
147         temp[0] = '\0';
148         dbg("key='%s'\n", *key);
149
150         /* skip whitespace after operator */
151         while (isspace(linepos[0]))
152                 linepos++;
153         if (linepos[0] == '\0')
154                 return -1;
155
156         /* get the value*/
157         if (linepos[0] == '"')
158                 linepos++;
159         else
160                 return -1;
161         *value = linepos;
162
163         temp = strchr(linepos, '"');
164         if (!temp)
165                 return -1;
166         temp[0] = '\0';
167         temp++;
168         dbg("value='%s'\n", *value);
169
170         /* move line to next key */
171         *line = temp;
172
173         return 0;
174 }
175
176 /* extract possible KEY{attr} */
177 static char *get_key_attribute(char *str)
178 {
179         char *pos;
180         char *attr;
181
182         attr = strchr(str, '{');
183         if (attr != NULL) {
184                 attr++;
185                 pos = strchr(attr, '}');
186                 if (pos == NULL) {
187                         err("missing closing brace for format\n");
188                         return NULL;
189                 }
190                 pos[0] = '\0';
191                 dbg("attribute='%s'\n", attr);
192                 return attr;
193         }
194
195         return NULL;
196 }
197
198 static int add_rule_key(struct udev_rule *rule, struct key *key,
199                         enum key_operation operation, const char *value)
200 {
201         size_t val_len = strnlen(value, PATH_SIZE);
202
203         key->operation = operation;
204
205         key->val_off = rule->bufsize;
206         strlcpy(rule->buf + rule->bufsize, value, val_len+1);
207         rule->bufsize += val_len+1;
208
209         return 0;
210 }
211
212 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
213                              enum key_operation operation, const char *key, const char *value)
214 {
215         size_t key_len = strnlen(key, PATH_SIZE);
216
217         if (pairs->count >= PAIRS_MAX) {
218                 err("skip, too many keys of the same type in a single rule\n");
219                 return -1;
220         }
221
222         add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
223
224         /* add the key-name of the pair */
225         pairs->keys[pairs->count].key_name_off = rule->bufsize;
226         strlcpy(rule->buf + rule->bufsize, key, key_len+1);
227         rule->bufsize += key_len+1;
228
229         pairs->count++;
230
231         return 0;
232 }
233
234 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
235 {
236         char buf[sizeof(struct udev_rule) + LINE_SIZE];
237         struct udev_rule *rule;
238         size_t rule_size;
239         int valid;
240         char *linepos;
241         char *attr;
242         size_t padding;
243         int physdev = 0;
244         int retval;
245
246         memset(buf, 0x00, sizeof(buf));
247         rule = (struct udev_rule *) buf;
248         rule->event_timeout = -1;
249         linepos = line;
250         valid = 0;
251
252         /* get all the keys */
253         while (1) {
254                 char *key;
255                 char *value;
256                 enum key_operation operation = KEY_OP_UNSET;
257
258                 retval = get_key(&linepos, &key, &operation, &value);
259                 if (retval)
260                         break;
261
262                 if (strcasecmp(key, "ACTION") == 0) {
263                         if (operation != KEY_OP_MATCH &&
264                             operation != KEY_OP_NOMATCH) {
265                                 err("invalid ACTION operation\n");
266                                 goto invalid;
267                         }
268                         add_rule_key(rule, &rule->action, operation, value);
269                         valid = 1;
270                         continue;
271                 }
272
273                 if (strcasecmp(key, "DEVPATH") == 0) {
274                         if (operation != KEY_OP_MATCH &&
275                             operation != KEY_OP_NOMATCH) {
276                                 err("invalid DEVPATH operation\n");
277                                 goto invalid;
278                         }
279                         add_rule_key(rule, &rule->devpath, operation, value);
280                         valid = 1;
281                         continue;
282                 }
283
284                 if (strcasecmp(key, "KERNEL") == 0) {
285                         if (operation != KEY_OP_MATCH &&
286                             operation != KEY_OP_NOMATCH) {
287                                 err("invalid KERNEL operation\n");
288                                 goto invalid;
289                         }
290                         add_rule_key(rule, &rule->kernel, operation, value);
291                         valid = 1;
292                         continue;
293                 }
294
295                 if (strcasecmp(key, "SUBSYSTEM") == 0) {
296                         if (operation != KEY_OP_MATCH &&
297                             operation != KEY_OP_NOMATCH) {
298                                 err("invalid SUBSYSTEM operation\n");
299                                 goto invalid;
300                         }
301                         /* bus, class, subsystem events should all be the same */
302                         if (strcmp(value, "subsystem") == 0 ||
303                             strcmp(value, "bus") == 0 ||
304                             strcmp(value, "class") == 0) {
305                                 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
306                                         err("'%s' must be specified as 'subsystem' \n"
307                                             "please fix it in %s:%u", value, filename, lineno);
308                                 add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus");
309                         } else
310                                 add_rule_key(rule, &rule->subsystem, operation, value);
311                         valid = 1;
312                         continue;
313                 }
314
315                 if (strcasecmp(key, "DRIVER") == 0) {
316                         if (operation != KEY_OP_MATCH &&
317                             operation != KEY_OP_NOMATCH) {
318                                 err("invalid DRIVER operation\n");
319                                 goto invalid;
320                         }
321                         add_rule_key(rule, &rule->driver, operation, value);
322                         valid = 1;
323                         continue;
324                 }
325
326                 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
327                         attr = get_key_attribute(key + sizeof("ATTR")-1);
328                         if (attr == NULL) {
329                                 err("error parsing ATTR attribute\n");
330                                 goto invalid;
331                         }
332                         if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
333                                 goto invalid;
334                         valid = 1;
335                         continue;
336                 }
337
338                 if (strcasecmp(key, "KERNELS") == 0 ||
339                     strcasecmp(key, "ID") == 0) {
340                         if (operation != KEY_OP_MATCH &&
341                             operation != KEY_OP_NOMATCH) {
342                                 err("invalid KERNELS operation\n");
343                                 goto invalid;
344                         }
345                         add_rule_key(rule, &rule->kernels, operation, value);
346                         valid = 1;
347                         continue;
348                 }
349
350                 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
351                     strcasecmp(key, "BUS") == 0) {
352                         if (operation != KEY_OP_MATCH &&
353                             operation != KEY_OP_NOMATCH) {
354                                 err("invalid SUBSYSTEMS operation\n");
355                                 goto invalid;
356                         }
357                         add_rule_key(rule, &rule->subsystems, operation, value);
358                         valid = 1;
359                         continue;
360                 }
361
362                 if (strcasecmp(key, "DRIVERS") == 0) {
363                         if (operation != KEY_OP_MATCH &&
364                             operation != KEY_OP_NOMATCH) {
365                                 err("invalid DRIVERS operation\n");
366                                 goto invalid;
367                         }
368                         add_rule_key(rule, &rule->drivers, operation, value);
369                         valid = 1;
370                         continue;
371                 }
372
373                 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
374                     strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
375                         if (operation != KEY_OP_MATCH &&
376                             operation != KEY_OP_NOMATCH) {
377                                 err("invalid ATTRS operation\n");
378                                 goto invalid;
379                         }
380                         attr = get_key_attribute(key + sizeof("ATTRS")-1);
381                         if (attr == NULL) {
382                                 err("error parsing ATTRS attribute\n");
383                                 goto invalid;
384                         }
385                         if (strncmp(attr, "device/", 7) == 0)
386                                 err("the 'device' link is deprecated and will be removed from a future kernel, \n"
387                                     "please fix it in %s:%u", filename, lineno);
388                         else if (strstr(attr, "../") != NULL)
389                                 err("do not reference parent sysfs directories directly, that may break with a future kernel, \n"
390                                     "please fix it in %s:%u", filename, lineno);
391                         if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
392                                 goto invalid;
393                         valid = 1;
394                         continue;
395                 }
396
397                 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
398                         attr = get_key_attribute(key + sizeof("ENV")-1);
399                         if (attr == NULL) {
400                                 err("error parsing ENV attribute\n");
401                                 goto invalid;
402                         }
403                         if (strncmp(attr, "PHYSDEV", 7) == 0)
404                                 physdev = 1;
405                         if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
406                                 goto invalid;
407                         valid = 1;
408                         continue;
409                 }
410
411                 if (strcasecmp(key, "PROGRAM") == 0) {
412                         add_rule_key(rule, &rule->program, operation, value);
413                         valid = 1;
414                         continue;
415                 }
416
417                 if (strcasecmp(key, "RESULT") == 0) {
418                         if (operation != KEY_OP_MATCH &&
419                             operation != KEY_OP_NOMATCH) {
420                                 err("invalid RESULT operation\n");
421                                 goto invalid;
422                         }
423                         add_rule_key(rule, &rule->result, operation, value);
424                         valid = 1;
425                         continue;
426                 }
427
428                 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
429                         attr = get_key_attribute(key + sizeof("IMPORT")-1);
430                         if (attr != NULL && strstr(attr, "program")) {
431                                 dbg("IMPORT will be executed\n");
432                                 rule->import_type  = IMPORT_PROGRAM;
433                         } else if (attr != NULL && strstr(attr, "file")) {
434                                 dbg("IMPORT will be included as file\n");
435                                 rule->import_type  = IMPORT_FILE;
436                         } else if (attr != NULL && strstr(attr, "parent")) {
437                                 dbg("IMPORT will include the parent values\n");
438                                 rule->import_type = IMPORT_PARENT;
439                         } else {
440                                 /* figure it out if it is executable */
441                                 char file[PATH_SIZE];
442                                 char *pos;
443                                 struct stat statbuf;
444
445                                 strlcpy(file, value, sizeof(file));
446                                 pos = strchr(file, ' ');
447                                 if (pos)
448                                         pos[0] = '\0';
449
450                                 /* allow programs in /lib/udev called without the path */
451                                 if (strchr(file, '/') == NULL) {
452                                         strlcpy(file, "/lib/udev/", sizeof(file));
453                                         strlcat(file, value, sizeof(file));
454                                         pos = strchr(file, ' ');
455                                         if (pos)
456                                                 pos[0] = '\0';
457                                 }
458
459                                 dbg("IMPORT auto mode for '%s'\n", file);
460                                 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
461                                         dbg("IMPORT is executable, will be executed (autotype)\n");
462                                         rule->import_type  = IMPORT_PROGRAM;
463                                 } else {
464                                         dbg("IMPORT is not executable, will be included as file (autotype)\n");
465                                         rule->import_type  = IMPORT_FILE;
466                                 }
467                         }
468                         add_rule_key(rule, &rule->import, operation, value);
469                         valid = 1;
470                         continue;
471                 }
472
473                 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
474                         if (operation != KEY_OP_MATCH &&
475                             operation != KEY_OP_NOMATCH) {
476                                 err("invalid TEST operation\n");
477                                 goto invalid;
478                         }
479                         attr = get_key_attribute(key + sizeof("TEST")-1);
480                         if (attr != NULL)
481                                 rule->test_mode_mask = strtol(attr, NULL, 8);
482                         add_rule_key(rule, &rule->test, operation, value);
483                         valid = 1;
484                         continue;
485                 }
486
487                 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
488                         attr = get_key_attribute(key + sizeof("RUN")-1);
489                         if (attr != NULL) {
490                                 if (strstr(attr, "ignore_error"))
491                                         rule->run_ignore_error = 1;
492                         }
493                         add_rule_key(rule, &rule->run, operation, value);
494                         valid = 1;
495                         continue;
496                 }
497
498                 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
499                         add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
500                         valid = 1;
501                         continue;
502                 }
503
504                 if (strcasecmp(key, "LABEL") == 0) {
505                         add_rule_key(rule, &rule->label, operation, value);
506                         valid = 1;
507                         continue;
508                 }
509
510                 if (strcasecmp(key, "GOTO") == 0) {
511                         add_rule_key(rule, &rule->goto_label, operation, value);
512                         valid = 1;
513                         continue;
514                 }
515
516                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
517                         attr = get_key_attribute(key + sizeof("NAME")-1);
518                         if (attr != NULL) {
519                                 if (strstr(attr, "all_partitions") != NULL) {
520                                         dbg("creation of partition nodes requested\n");
521                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
522                                 }
523                                 if (strstr(attr, "ignore_remove") != NULL) {
524                                         dbg("remove event should be ignored\n");
525                                         rule->ignore_remove = 1;
526                                 }
527                         }
528                         if (value[0] == '\0')
529                                 dbg("name empty, node creation supressed\n");
530                         add_rule_key(rule, &rule->name, operation, value);
531                         continue;
532                 }
533
534                 if (strcasecmp(key, "SYMLINK") == 0) {
535                         if (operation == KEY_OP_MATCH ||
536                             operation == KEY_OP_NOMATCH)
537                                 add_rule_key(rule, &rule->symlink_match, operation, value);
538                         else
539                                 add_rule_key(rule, &rule->symlink, operation, value);
540                         valid = 1;
541                         continue;
542                 }
543
544                 if (strcasecmp(key, "OWNER") == 0) {
545                         valid = 1;
546                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
547                                 char *endptr;
548                                 strtoul(value, &endptr, 10);
549                                 if (endptr[0] != '\0') {
550                                         char owner[32];
551                                         uid_t uid = lookup_user(value);
552                                         dbg("replacing username='%s' by id=%i\n", value, uid);
553                                         sprintf(owner, "%u", (unsigned int) uid);
554                                         add_rule_key(rule, &rule->owner, operation, owner);
555                                         continue;
556                                 }
557                         }
558
559                         add_rule_key(rule, &rule->owner, operation, value);
560                         continue;
561                 }
562
563                 if (strcasecmp(key, "GROUP") == 0) {
564                         valid = 1;
565                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
566                                 char *endptr;
567                                 strtoul(value, &endptr, 10);
568                                 if (endptr[0] != '\0') {
569                                         char group[32];
570                                         gid_t gid = lookup_group(value);
571                                         dbg("replacing groupname='%s' by id=%i\n", value, gid);
572                                         sprintf(group, "%u", (unsigned int) gid);
573                                         add_rule_key(rule, &rule->group, operation, group);
574                                         continue;
575                                 }
576                         }
577
578                         add_rule_key(rule, &rule->group, operation, value);
579                         continue;
580                 }
581
582                 if (strcasecmp(key, "MODE") == 0) {
583                         add_rule_key(rule, &rule->mode, operation, value);
584                         valid = 1;
585                         continue;
586                 }
587
588                 if (strcasecmp(key, "OPTIONS") == 0) {
589                         const char *pos;
590
591                         if (strstr(value, "last_rule") != NULL) {
592                                 dbg("last rule to be applied\n");
593                                 rule->last_rule = 1;
594                         }
595                         if (strstr(value, "ignore_device") != NULL) {
596                                 dbg("device should be ignored\n");
597                                 rule->ignore_device = 1;
598                         }
599                         if (strstr(value, "ignore_remove") != NULL) {
600                                 dbg("remove event should be ignored\n");
601                                 rule->ignore_remove = 1;
602                         }
603                         pos = strstr(value, "link_priority=");
604                         if (pos != NULL) {
605                                 rule->link_priority = atoi(&pos[strlen("link_priority=")]);
606                                 dbg("link priority=%i\n", rule->link_priority);
607                         }
608                         pos = strstr(value, "event_timeout=");
609                         if (pos != NULL) {
610                                 rule->event_timeout = atoi(&pos[strlen("event_timeout=")]);
611                                 dbg("event timout=%i\n", rule->event_timeout);
612                         }
613                         pos = strstr(value, "string_escape=");
614                         if (pos != NULL) {
615                                 pos = &pos[strlen("string_escape=")];
616                                 if (strncmp(pos, "none", strlen("none")) == 0)
617                                         rule->string_escape = ESCAPE_NONE;
618                                 else if (strncmp(pos, "replace", strlen("replace")) == 0)
619                                         rule->string_escape = ESCAPE_REPLACE;
620                         }
621                         if (strstr(value, "all_partitions") != NULL) {
622                                 dbg("creation of partition nodes requested\n");
623                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
624                         }
625                         valid = 1;
626                         continue;
627                 }
628
629                 err("unknown key '%s' in %s:%u\n", key, filename, lineno);
630         }
631
632         if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
633                 err("PHYSDEV* values are deprecated and will be removed from a future kernel, \n"
634                     "please fix it in %s:%u", filename, lineno);
635
636         /* skip line if not any valid key was found */
637         if (!valid)
638                 goto invalid;
639
640         /* grow buffer and add rule */
641         rule_size = sizeof(struct udev_rule) + rule->bufsize;
642         padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
643         dbg("add %zi padding bytes\n", padding);
644         rule_size += padding;
645         rule->bufsize += padding;
646
647         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
648         if (!rules->buf) {
649                 err("realloc failed\n");
650                 goto exit;
651         }
652         dbg("adding rule to offset %zi\n", rules->bufsize);
653         memcpy(rules->buf + rules->bufsize, rule, rule_size);
654         rules->bufsize += rule_size;
655 exit:
656         return 0;
657
658 invalid:
659         err("invalid rule '%s:%u'\n", filename, lineno);
660         return -1;
661 }
662
663 static int parse_file(struct udev_rules *rules, const char *filename)
664 {
665         char line[LINE_SIZE];
666         char *bufline;
667         unsigned int lineno;
668         char *buf;
669         size_t bufsize;
670         size_t cur;
671         size_t count;
672         int retval = 0;
673
674         if (file_map(filename, &buf, &bufsize) != 0) {
675                 err("can't open '%s' as rules file: %s\n", filename, strerror(errno));
676                 return -1;
677         }
678         info("reading '%s' as rules file\n", filename);
679
680         /* loop through the whole file */
681         cur = 0;
682         lineno = 0;
683         while (cur < bufsize) {
684                 unsigned int i, j;
685
686                 count = buf_get_line(buf, bufsize, cur);
687                 bufline = &buf[cur];
688                 cur += count+1;
689                 lineno++;
690
691                 /* eat the whitespace */
692                 while ((count > 0) && isspace(bufline[0])) {
693                         bufline++;
694                         count--;
695                 }
696                 if (count == 0)
697                         continue;
698
699                 /* see if this is a comment */
700                 if (bufline[0] == COMMENT_CHARACTER)
701                         continue;
702
703                 if (count >= sizeof(line)) {
704                         err("line too long, rule skipped '%s:%u'\n", filename, lineno);
705                         continue;
706                 }
707
708                 /* skip backslash and newline from multiline rules */
709                 for (i = j = 0; i < count; i++) {
710                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
711                                 continue;
712
713                         line[j++] = bufline[i];
714                 }
715                 line[j] = '\0';
716
717                 dbg("read '%s'\n", line);
718                 add_to_rules(rules, line, filename, lineno);
719         }
720
721         file_unmap(buf, bufsize);
722         return retval;
723 }
724
725 int udev_rules_init(struct udev_rules *rules, int resolve_names)
726 {
727         struct stat statbuf;
728         char filename[PATH_MAX];
729         LIST_HEAD(name_list);
730         LIST_HEAD(dyn_list);
731         struct name_entry *name_loop, *name_tmp;
732         struct name_entry *dyn_loop, *dyn_tmp;
733         int retval = 0;
734
735         memset(rules, 0x00, sizeof(struct udev_rules));
736         rules->resolve_names = resolve_names;
737
738         /* read main config from single file or all files in a directory */
739         if (stat(udev_rules_dir, &statbuf) != 0)
740                 return -1;
741         if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
742                 dbg("parse single rules file '%s'\n", udev_rules_dir);
743                 name_list_add(&name_list, udev_rules_dir, 1);
744         } else {
745                 dbg("parse rules directory '%s'\n", udev_rules_dir);
746                 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
747         }
748
749         /* read dynamic rules directory */
750         strlcpy(filename, udev_root, sizeof(filename));
751         strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename));
752         if (stat(filename, &statbuf) != 0) {
753                 create_path(filename);
754                 selinux_setfscreatecon(filename, NULL, S_IFDIR|0755);
755                 mkdir(filename, 0755);
756                 selinux_resetfscreatecon();
757         }
758         add_matching_files(&dyn_list, filename, RULESFILE_SUFFIX);
759
760         /* sort dynamic rules files by basename into list of files */
761         list_for_each_entry_safe(dyn_loop, dyn_tmp, &dyn_list, node) {
762                 const char *dyn_base = strrchr(dyn_loop->name, '/');
763
764                 if (dyn_base == NULL)
765                         continue;
766
767                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
768                         const char *name_base = strrchr(name_loop->name, '/');
769
770                         if (name_base == NULL)
771                                 continue;
772
773                         if (strcmp(name_base, dyn_base) > 0)
774                                 break;
775                 }
776                 list_move_tail(&dyn_loop->node, &name_loop->node);
777         }
778
779         /* parse list of files */
780         list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
781                 if (stat(name_loop->name, &statbuf) == 0) {
782                         if (statbuf.st_size)
783                                 parse_file(rules, name_loop->name);
784                         else
785                                 dbg("empty rules file '%s'\n", name_loop->name);
786                 } else
787                         err("could not read '%s': %s\n", name_loop->name, strerror(errno));
788                 list_del(&name_loop->node);
789                 free(name_loop);
790         }
791
792         return retval;
793 }
794
795 void udev_rules_cleanup(struct udev_rules *rules)
796 {
797         if (rules->buf) {
798                 free(rules->buf);
799                 rules->buf = NULL;
800         }
801 }
802