chiark / gitweb /
scsi_id: initialize serial strings
[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         linepos = line;
249         valid = 0;
250
251         /* get all the keys */
252         while (1) {
253                 char *key;
254                 char *value;
255                 enum key_operation operation = KEY_OP_UNSET;
256
257                 retval = get_key(&linepos, &key, &operation, &value);
258                 if (retval)
259                         break;
260
261                 if (strcasecmp(key, "ACTION") == 0) {
262                         if (operation != KEY_OP_MATCH &&
263                             operation != KEY_OP_NOMATCH) {
264                                 err("invalid ACTION operation\n");
265                                 goto invalid;
266                         }
267                         add_rule_key(rule, &rule->action, operation, value);
268                         valid = 1;
269                         continue;
270                 }
271
272                 if (strcasecmp(key, "DEVPATH") == 0) {
273                         if (operation != KEY_OP_MATCH &&
274                             operation != KEY_OP_NOMATCH) {
275                                 err("invalid DEVPATH operation\n");
276                                 goto invalid;
277                         }
278                         add_rule_key(rule, &rule->devpath, operation, value);
279                         valid = 1;
280                         continue;
281                 }
282
283                 if (strcasecmp(key, "KERNEL") == 0) {
284                         if (operation != KEY_OP_MATCH &&
285                             operation != KEY_OP_NOMATCH) {
286                                 err("invalid KERNEL operation\n");
287                                 goto invalid;
288                         }
289                         add_rule_key(rule, &rule->kernel, operation, value);
290                         valid = 1;
291                         continue;
292                 }
293
294                 if (strcasecmp(key, "SUBSYSTEM") == 0) {
295                         if (operation != KEY_OP_MATCH &&
296                             operation != KEY_OP_NOMATCH) {
297                                 err("invalid SUBSYSTEM operation\n");
298                                 goto invalid;
299                         }
300                         /* bus, class, subsystem events should all be the same */
301                         if (strcmp(value, "subsystem") == 0 ||
302                             strcmp(value, "bus") == 0 ||
303                             strcmp(value, "class") == 0) {
304                                 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
305                                         err("'%s' must be specified as 'subsystem' \n"
306                                             "please fix it in %s:%u", value, filename, lineno);
307                                 add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus");
308                         } else
309                                 add_rule_key(rule, &rule->subsystem, operation, value);
310                         valid = 1;
311                         continue;
312                 }
313
314                 if (strcasecmp(key, "DRIVER") == 0) {
315                         if (operation != KEY_OP_MATCH &&
316                             operation != KEY_OP_NOMATCH) {
317                                 err("invalid DRIVER operation\n");
318                                 goto invalid;
319                         }
320                         add_rule_key(rule, &rule->driver, operation, value);
321                         valid = 1;
322                         continue;
323                 }
324
325                 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
326                         attr = get_key_attribute(key + sizeof("ATTR")-1);
327                         if (attr == NULL) {
328                                 err("error parsing ATTR attribute\n");
329                                 goto invalid;
330                         }
331                         if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
332                                 goto invalid;
333                         valid = 1;
334                         continue;
335                 }
336
337                 if (strcasecmp(key, "KERNELS") == 0 ||
338                     strcasecmp(key, "ID") == 0) {
339                         if (operation != KEY_OP_MATCH &&
340                             operation != KEY_OP_NOMATCH) {
341                                 err("invalid KERNELS operation\n");
342                                 goto invalid;
343                         }
344                         add_rule_key(rule, &rule->kernels, operation, value);
345                         valid = 1;
346                         continue;
347                 }
348
349                 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
350                     strcasecmp(key, "BUS") == 0) {
351                         if (operation != KEY_OP_MATCH &&
352                             operation != KEY_OP_NOMATCH) {
353                                 err("invalid SUBSYSTEMS operation\n");
354                                 goto invalid;
355                         }
356                         add_rule_key(rule, &rule->subsystems, operation, value);
357                         valid = 1;
358                         continue;
359                 }
360
361                 if (strcasecmp(key, "DRIVERS") == 0) {
362                         if (operation != KEY_OP_MATCH &&
363                             operation != KEY_OP_NOMATCH) {
364                                 err("invalid DRIVERS operation\n");
365                                 goto invalid;
366                         }
367                         add_rule_key(rule, &rule->drivers, operation, value);
368                         valid = 1;
369                         continue;
370                 }
371
372                 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
373                     strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
374                         if (operation != KEY_OP_MATCH &&
375                             operation != KEY_OP_NOMATCH) {
376                                 err("invalid ATTRS operation\n");
377                                 goto invalid;
378                         }
379                         attr = get_key_attribute(key + sizeof("ATTRS")-1);
380                         if (attr == NULL) {
381                                 err("error parsing ATTRS attribute\n");
382                                 goto invalid;
383                         }
384                         if (strncmp(attr, "device/", 7) == 0)
385                                 err("the 'device' link is deprecated and will be removed from a future kernel, \n"
386                                     "please fix it in %s:%u", filename, lineno);
387                         else if (strstr(attr, "../") != NULL)
388                                 err("do not reference parent sysfs directories directly, that may break with a future kernel, \n"
389                                     "please fix it in %s:%u", filename, lineno);
390                         if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
391                                 goto invalid;
392                         valid = 1;
393                         continue;
394                 }
395
396                 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
397                         attr = get_key_attribute(key + sizeof("ENV")-1);
398                         if (attr == NULL) {
399                                 err("error parsing ENV attribute\n");
400                                 goto invalid;
401                         }
402                         if (strncmp(attr, "PHYSDEV", 7) == 0)
403                                 physdev = 1;
404                         if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
405                                 goto invalid;
406                         valid = 1;
407                         continue;
408                 }
409
410                 if (strcasecmp(key, "PROGRAM") == 0) {
411                         add_rule_key(rule, &rule->program, operation, value);
412                         valid = 1;
413                         continue;
414                 }
415
416                 if (strcasecmp(key, "RESULT") == 0) {
417                         if (operation != KEY_OP_MATCH &&
418                             operation != KEY_OP_NOMATCH) {
419                                 err("invalid RESULT operation\n");
420                                 goto invalid;
421                         }
422                         add_rule_key(rule, &rule->result, operation, value);
423                         valid = 1;
424                         continue;
425                 }
426
427                 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
428                         attr = get_key_attribute(key + sizeof("IMPORT")-1);
429                         if (attr != NULL && strstr(attr, "program")) {
430                                 dbg("IMPORT will be executed\n");
431                                 rule->import_type  = IMPORT_PROGRAM;
432                         } else if (attr != NULL && strstr(attr, "file")) {
433                                 dbg("IMPORT will be included as file\n");
434                                 rule->import_type  = IMPORT_FILE;
435                         } else if (attr != NULL && strstr(attr, "parent")) {
436                                 dbg("IMPORT will include the parent values\n");
437                                 rule->import_type = IMPORT_PARENT;
438                         } else {
439                                 /* figure it out if it is executable */
440                                 char file[PATH_SIZE];
441                                 char *pos;
442                                 struct stat statbuf;
443
444                                 strlcpy(file, value, sizeof(file));
445                                 pos = strchr(file, ' ');
446                                 if (pos)
447                                         pos[0] = '\0';
448
449                                 /* allow programs in /lib/udev called without the path */
450                                 if (strchr(file, '/') == NULL) {
451                                         strlcpy(file, "/lib/udev/", sizeof(file));
452                                         strlcat(file, value, sizeof(file));
453                                         pos = strchr(file, ' ');
454                                         if (pos)
455                                                 pos[0] = '\0';
456                                 }
457
458                                 dbg("IMPORT auto mode for '%s'\n", file);
459                                 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
460                                         dbg("IMPORT is executable, will be executed (autotype)\n");
461                                         rule->import_type  = IMPORT_PROGRAM;
462                                 } else {
463                                         dbg("IMPORT is not executable, will be included as file (autotype)\n");
464                                         rule->import_type  = IMPORT_FILE;
465                                 }
466                         }
467                         add_rule_key(rule, &rule->import, operation, value);
468                         valid = 1;
469                         continue;
470                 }
471
472                 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
473                         if (operation != KEY_OP_MATCH &&
474                             operation != KEY_OP_NOMATCH) {
475                                 err("invalid TEST operation\n");
476                                 goto invalid;
477                         }
478                         attr = get_key_attribute(key + sizeof("TEST")-1);
479                         if (attr != NULL)
480                                 rule->test_mode_mask = strtol(attr, NULL, 8);
481                         add_rule_key(rule, &rule->test, operation, value);
482                         valid = 1;
483                         continue;
484                 }
485
486                 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
487                         attr = get_key_attribute(key + sizeof("RUN")-1);
488                         if (attr != NULL) {
489                                 if (strstr(attr, "ignore_error"))
490                                         rule->run_ignore_error = 1;
491                         }
492                         add_rule_key(rule, &rule->run, operation, value);
493                         valid = 1;
494                         continue;
495                 }
496
497                 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
498                         add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
499                         valid = 1;
500                         continue;
501                 }
502
503                 if (strcasecmp(key, "LABEL") == 0) {
504                         add_rule_key(rule, &rule->label, operation, value);
505                         valid = 1;
506                         continue;
507                 }
508
509                 if (strcasecmp(key, "GOTO") == 0) {
510                         add_rule_key(rule, &rule->goto_label, operation, value);
511                         valid = 1;
512                         continue;
513                 }
514
515                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
516                         attr = get_key_attribute(key + sizeof("NAME")-1);
517                         if (attr != NULL) {
518                                 if (strstr(attr, "all_partitions") != NULL) {
519                                         dbg("creation of partition nodes requested\n");
520                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
521                                 }
522                                 if (strstr(attr, "ignore_remove") != NULL) {
523                                         dbg("remove event should be ignored\n");
524                                         rule->ignore_remove = 1;
525                                 }
526                         }
527                         if (value[0] == '\0')
528                                 dbg("name empty, node creation supressed\n");
529                         add_rule_key(rule, &rule->name, operation, value);
530                         continue;
531                 }
532
533                 if (strcasecmp(key, "SYMLINK") == 0) {
534                         if (operation == KEY_OP_MATCH ||
535                             operation == KEY_OP_NOMATCH)
536                                 add_rule_key(rule, &rule->symlink_match, operation, value);
537                         else
538                                 add_rule_key(rule, &rule->symlink, operation, value);
539                         valid = 1;
540                         continue;
541                 }
542
543                 if (strcasecmp(key, "OWNER") == 0) {
544                         valid = 1;
545                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
546                                 char *endptr;
547                                 strtoul(value, &endptr, 10);
548                                 if (endptr[0] != '\0') {
549                                         char owner[32];
550                                         uid_t uid = lookup_user(value);
551                                         dbg("replacing username='%s' by id=%i\n", value, uid);
552                                         sprintf(owner, "%u", (unsigned int) uid);
553                                         add_rule_key(rule, &rule->owner, operation, owner);
554                                         continue;
555                                 }
556                         }
557
558                         add_rule_key(rule, &rule->owner, operation, value);
559                         continue;
560                 }
561
562                 if (strcasecmp(key, "GROUP") == 0) {
563                         valid = 1;
564                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
565                                 char *endptr;
566                                 strtoul(value, &endptr, 10);
567                                 if (endptr[0] != '\0') {
568                                         char group[32];
569                                         gid_t gid = lookup_group(value);
570                                         dbg("replacing groupname='%s' by id=%i\n", value, gid);
571                                         sprintf(group, "%u", (unsigned int) gid);
572                                         add_rule_key(rule, &rule->group, operation, group);
573                                         continue;
574                                 }
575                         }
576
577                         add_rule_key(rule, &rule->group, operation, value);
578                         continue;
579                 }
580
581                 if (strcasecmp(key, "MODE") == 0) {
582                         add_rule_key(rule, &rule->mode, operation, value);
583                         valid = 1;
584                         continue;
585                 }
586
587                 if (strcasecmp(key, "OPTIONS") == 0) {
588                         const char *pos;
589
590                         if (strstr(value, "last_rule") != NULL) {
591                                 dbg("last rule to be applied\n");
592                                 rule->last_rule = 1;
593                         }
594                         if (strstr(value, "ignore_device") != NULL) {
595                                 dbg("device should be ignored\n");
596                                 rule->ignore_device = 1;
597                         }
598                         if (strstr(value, "ignore_remove") != NULL) {
599                                 dbg("remove event should be ignored\n");
600                                 rule->ignore_remove = 1;
601                         }
602                         pos = strstr(value, "link_priority=");
603                         if (pos != NULL) {
604                                 rule->link_priority = atoi(&pos[strlen("link_priority=")]);
605                                 dbg("link priority=%i\n", rule->link_priority);
606                         }
607                         pos = strstr(value, "string_escape=");
608                         if (pos != NULL) {
609                                 pos = &pos[strlen("string_escape=")];
610                                 if (strncmp(pos, "none", strlen("none")) == 0)
611                                         rule->string_escape = ESCAPE_NONE;
612                                 else if (strncmp(pos, "replace", strlen("replace")) == 0)
613                                         rule->string_escape = ESCAPE_REPLACE;
614                         }
615                         if (strstr(value, "all_partitions") != NULL) {
616                                 dbg("creation of partition nodes requested\n");
617                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
618                         }
619                         valid = 1;
620                         continue;
621                 }
622
623                 err("unknown key '%s' in %s:%u\n", key, filename, lineno);
624         }
625
626         if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
627                 err("PHYSDEV* values are deprecated and will be removed from a future kernel, \n"
628                     "please fix it in %s:%u", filename, lineno);
629
630         /* skip line if not any valid key was found */
631         if (!valid)
632                 goto invalid;
633
634         /* grow buffer and add rule */
635         rule_size = sizeof(struct udev_rule) + rule->bufsize;
636         padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
637         dbg("add %zi padding bytes\n", padding);
638         rule_size += padding;
639         rule->bufsize += padding;
640
641         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
642         if (!rules->buf) {
643                 err("realloc failed\n");
644                 goto exit;
645         }
646         dbg("adding rule to offset %zi\n", rules->bufsize);
647         memcpy(rules->buf + rules->bufsize, rule, rule_size);
648         rules->bufsize += rule_size;
649 exit:
650         return 0;
651
652 invalid:
653         err("invalid rule '%s:%u'\n", filename, lineno);
654         return -1;
655 }
656
657 static int parse_file(struct udev_rules *rules, const char *filename)
658 {
659         char line[LINE_SIZE];
660         char *bufline;
661         unsigned int lineno;
662         char *buf;
663         size_t bufsize;
664         size_t cur;
665         size_t count;
666         int retval = 0;
667
668         if (file_map(filename, &buf, &bufsize) != 0) {
669                 err("can't open '%s' as rules file: %s\n", filename, strerror(errno));
670                 return -1;
671         }
672         info("reading '%s' as rules file\n", filename);
673
674         /* loop through the whole file */
675         cur = 0;
676         lineno = 0;
677         while (cur < bufsize) {
678                 unsigned int i, j;
679
680                 count = buf_get_line(buf, bufsize, cur);
681                 bufline = &buf[cur];
682                 cur += count+1;
683                 lineno++;
684
685                 /* eat the whitespace */
686                 while ((count > 0) && isspace(bufline[0])) {
687                         bufline++;
688                         count--;
689                 }
690                 if (count == 0)
691                         continue;
692
693                 /* see if this is a comment */
694                 if (bufline[0] == COMMENT_CHARACTER)
695                         continue;
696
697                 if (count >= sizeof(line)) {
698                         err("line too long, rule skipped '%s:%u'\n", filename, lineno);
699                         continue;
700                 }
701
702                 /* skip backslash and newline from multiline rules */
703                 for (i = j = 0; i < count; i++) {
704                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
705                                 continue;
706
707                         line[j++] = bufline[i];
708                 }
709                 line[j] = '\0';
710
711                 dbg("read '%s'\n", line);
712                 add_to_rules(rules, line, filename, lineno);
713         }
714
715         file_unmap(buf, bufsize);
716         return retval;
717 }
718
719 int udev_rules_init(struct udev_rules *rules, int resolve_names)
720 {
721         struct stat statbuf;
722         char filename[PATH_MAX];
723         LIST_HEAD(name_list);
724         LIST_HEAD(dyn_list);
725         struct name_entry *name_loop, *name_tmp;
726         struct name_entry *dyn_loop, *dyn_tmp;
727         int retval = 0;
728
729         memset(rules, 0x00, sizeof(struct udev_rules));
730         rules->resolve_names = resolve_names;
731
732         /* read main config from single file or all files in a directory */
733         if (stat(udev_rules_dir, &statbuf) != 0)
734                 return -1;
735         if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
736                 dbg("parse single rules file '%s'\n", udev_rules_dir);
737                 name_list_add(&name_list, udev_rules_dir, 1);
738         } else {
739                 dbg("parse rules directory '%s'\n", udev_rules_dir);
740                 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
741         }
742
743         /* read dynamic rules directory */
744         strlcpy(filename, udev_root, sizeof(filename));
745         strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename));
746         if (stat(filename, &statbuf) != 0) {
747                 create_path(filename);
748                 selinux_setfscreatecon(filename, NULL, S_IFDIR|0755);
749                 mkdir(filename, 0755);
750                 selinux_resetfscreatecon();
751         }
752         add_matching_files(&dyn_list, filename, RULESFILE_SUFFIX);
753
754         /* sort dynamic rules files by basename into list of files */
755         list_for_each_entry_safe(dyn_loop, dyn_tmp, &dyn_list, node) {
756                 const char *dyn_base = strrchr(dyn_loop->name, '/');
757
758                 if (dyn_base == NULL)
759                         continue;
760
761                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
762                         const char *name_base = strrchr(name_loop->name, '/');
763
764                         if (name_base == NULL)
765                                 continue;
766
767                         if (strcmp(name_base, dyn_base) > 0)
768                                 break;
769                 }
770                 list_move_tail(&dyn_loop->node, &name_loop->node);
771         }
772
773         /* parse list of files */
774         list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
775                 if (stat(name_loop->name, &statbuf) == 0) {
776                         if (statbuf.st_size)
777                                 parse_file(rules, name_loop->name);
778                         else
779                                 dbg("empty rules file '%s'\n", name_loop->name);
780                 } else
781                         err("could not read '%s': %s\n", name_loop->name, strerror(errno));
782                 list_del(&name_loop->node);
783                 free(name_loop);
784         }
785
786         return retval;
787 }
788
789 void udev_rules_cleanup(struct udev_rules *rules)
790 {
791         if (rules->buf) {
792                 free(rules->buf);
793                 rules->buf = NULL;
794         }
795 }
796