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