chiark / gitweb /
make SYSFS{} usable for all devices
[elogind.git] / udev_rules_parse.c
1 /*
2  * udev_rules_parse.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *
10  *      This program is free software; you can redistribute it and/or modify it
11  *      under the terms of the GNU General Public License as published by the
12  *      Free Software Foundation version 2 of the License.
13  * 
14  *      This program is distributed in the hope that it will be useful, but
15  *      WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *      General Public License for more details.
18  * 
19  *      You should have received a copy of the GNU General Public License along
20  *      with this program; if not, write to the Free Software Foundation, Inc.,
21  *      675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33
34 #include "udev_libc_wrapper.h"
35 #include "udev.h"
36 #include "udev_utils.h"
37 #include "logging.h"
38 #include "udev_rules.h"
39
40
41 void udev_rules_iter_init(struct udev_rules *rules)
42 {
43         dbg("bufsize=%zi", rules->bufsize);
44         rules->current = 0;
45 }
46
47 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
48 {
49         static struct udev_rule *rule;
50
51         if (!rules)
52                 return NULL;
53
54         dbg("current=%zi", rules->current);
55         if (rules->current >= rules->bufsize) {
56                 dbg("no more rules");
57                 return NULL;
58         }
59
60         /* get next rule */
61         rule = (struct udev_rule *) (rules->buf + rules->current);
62         rules->current += sizeof(struct udev_rule) + rule->bufsize;
63
64         return rule;
65 }
66
67 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
68 {
69         static struct udev_rule *rule;
70
71 next:
72         dbg("current=%zi", rules->current);
73         if (rules->current >= rules->bufsize) {
74                 dbg("no more rules");
75                 return NULL;
76         }
77         rule = (struct udev_rule *) (rules->buf + rules->current);
78
79         if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
80                 dbg("moving forward, looking for label '%s'", label);
81                 rules->current += sizeof(struct udev_rule) + rule->bufsize;
82                 goto next;
83         }
84
85         dbg("found label '%s'", label);
86         return rule;
87 }
88
89 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
90 {
91         char *linepos;
92         char *temp;
93
94         linepos = *line;
95         if (!linepos)
96                 return -1;
97
98         /* skip whitespace */
99         while (isspace(linepos[0]) || linepos[0] == ',')
100                 linepos++;
101
102         /* get the key */
103         *key = linepos;
104         while (1) {
105                 linepos++;
106                 if (linepos[0] == '\0')
107                         return -1;
108                 if (isspace(linepos[0]))
109                         break;
110                 if (linepos[0] == '=')
111                         break;
112                 if (linepos[0] == '+')
113                         break;
114                 if (linepos[0] == '!')
115                         break;
116                 if (linepos[0] == ':')
117                         break;
118         }
119
120         /* remember end of key */
121         temp = linepos;
122
123         /* skip whitespace after key */
124         while (isspace(linepos[0]))
125                 linepos++;
126
127         /* get operation type */
128         if (linepos[0] == '=' && linepos[1] == '=') {
129                 *operation = KEY_OP_MATCH;
130                 linepos += 2;
131                 dbg("operator=match");
132         } else if (linepos[0] == '!' && linepos[1] == '=') {
133                 *operation = KEY_OP_NOMATCH;
134                 linepos += 2;
135                 dbg("operator=nomatch");
136         } else if (linepos[0] == '+' && linepos[1] == '=') {
137                 *operation = KEY_OP_ADD;
138                 linepos += 2;
139                 dbg("operator=add");
140         } else if (linepos[0] == '=') {
141                 *operation = KEY_OP_ASSIGN;
142                 linepos++;
143                 dbg("operator=assign");
144         } else if (linepos[0] == ':' && linepos[1] == '=') {
145                 *operation = KEY_OP_ASSIGN_FINAL;
146                 linepos += 2;
147                 dbg("operator=assign_final");
148         } else
149                 return -1;
150
151         /* terminate key */
152         temp[0] = '\0';
153         dbg("key='%s'", *key);
154
155         /* skip whitespace after operator */
156         while (isspace(linepos[0]))
157                 linepos++;
158
159         /* get the value*/
160         if (linepos[0] == '"')
161                 linepos++;
162         else
163                 return -1;
164         *value = linepos;
165
166         temp = strchr(linepos, '"');
167         if (!temp)
168                 return -1;
169         temp[0] = '\0';
170         temp++;
171         dbg("value='%s'", *value);
172
173         /* move line to next key */
174         *line = temp;
175
176         return 0;
177 }
178
179 /* extract possible KEY{attr} */
180 static char *get_key_attribute(char *str)
181 {
182         char *pos;
183         char *attr;
184
185         attr = strchr(str, '{');
186         if (attr != NULL) {
187                 attr++;
188                 pos = strchr(attr, '}');
189                 if (pos == NULL) {
190                         err("missing closing brace for format");
191                         return NULL;
192                 }
193                 pos[0] = '\0';
194                 dbg("attribute='%s'", attr);
195                 return attr;
196         }
197
198         return NULL;
199 }
200
201 static int add_rule_key(struct udev_rule *rule, struct key *key,
202                         enum key_operation operation, const char *value)
203 {
204         size_t val_len = strnlen(value, PATH_SIZE);
205
206         key->operation = operation;
207
208         key->val_off = rule->bufsize;
209         strlcpy(rule->buf + rule->bufsize, value, val_len+1);
210         rule->bufsize += val_len+1;
211
212         return 0;
213 }
214
215 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
216                              enum key_operation operation, const char *key, const char *value)
217 {
218         size_t key_len = strnlen(key, PATH_SIZE);
219
220         if (pairs->count >= PAIRS_MAX) {
221                 err("skip, too many keys in a single rule");
222                 return -1;
223         }
224
225         add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
226
227         /* add the key-name of the pair */
228         pairs->keys[pairs->count].key_name_off = rule->bufsize;
229         strlcpy(rule->buf + rule->bufsize, key, key_len+1);
230         rule->bufsize += key_len+1;
231
232         pairs->count++;
233
234         return 0;
235 }
236
237 static int add_to_rules(struct udev_rules *rules, char *line)
238 {
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 retval;
246
247         /* get all the keys */
248         rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
249         if (!rule) {
250                 err("malloc failed");
251                 return -1;
252         }
253         linepos = line;
254         valid = 0;
255
256         while (1) {
257                 char *key;
258                 char *value;
259                 enum key_operation operation = KEY_OP_UNSET;
260
261                 retval = get_key(&linepos, &key, &operation, &value);
262                 if (retval)
263                         break;
264
265                 if (strcasecmp(key, "LABEL") == 0) {
266                         add_rule_key(rule, &rule->label, operation, value);
267                         valid = 1;
268                         continue;
269                 }
270
271                 if (strcasecmp(key, "GOTO") == 0) {
272                         add_rule_key(rule, &rule->goto_label, operation, value);
273                         valid = 1;
274                         continue;
275                 }
276
277                 if (strcasecmp(key, "KERNEL") == 0) {
278                         add_rule_key(rule, &rule->kernel_name, operation, value);
279                         valid = 1;
280                         continue;
281                 }
282
283                 if (strcasecmp(key, "SUBSYSTEM") == 0) {
284                         add_rule_key(rule, &rule->subsystem, operation, value);
285                         valid = 1;
286                         continue;
287                 }
288
289                 if (strcasecmp(key, "ACTION") == 0) {
290                         add_rule_key(rule, &rule->action, operation, value);
291                         valid = 1;
292                         continue;
293                 }
294
295                 if (strcasecmp(key, "DEVPATH") == 0) {
296                         add_rule_key(rule, &rule->devpath, operation, value);
297                         valid = 1;
298                         continue;
299                 }
300
301                 if (strcasecmp(key, "BUS") == 0) {
302                         add_rule_key(rule, &rule->bus, operation, value);
303                         valid = 1;
304                         continue;
305                 }
306
307                 if (strcasecmp(key, "ID") == 0) {
308                         add_rule_key(rule, &rule->id, operation, value);
309                         valid = 1;
310                         continue;
311                 }
312
313                 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
314                         attr = get_key_attribute(key + sizeof("SYSFS")-1);
315                         if (attr == NULL) {
316                                 err("error parsing SYSFS attribute in '%s'", line);
317                                 continue;
318                         }
319                         add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
320                         valid = 1;
321                         continue;
322                 }
323
324                 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
325                         add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
326                         valid = 1;
327                         continue;
328                 }
329
330                 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
331                         attr = get_key_attribute(key + sizeof("ENV")-1);
332                         if (attr == NULL) {
333                                 err("error parsing ENV attribute");
334                                 continue;
335                         }
336                         add_rule_key_pair(rule, &rule->env, operation, attr, value);
337                         valid = 1;
338                         continue;
339                 }
340
341                 if (strcasecmp(key, "MODALIAS") == 0) {
342                         add_rule_key(rule, &rule->modalias, operation, value);
343                         valid = 1;
344                         continue;
345                 }
346
347                 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
348                         attr = get_key_attribute(key + sizeof("IMPORT")-1);
349                         if (attr && strstr(attr, "program")) {
350                                 dbg("IMPORT will be executed");
351                                 rule->import_type  = IMPORT_PROGRAM;
352                         } else if (attr && strstr(attr, "file")) {
353                                 dbg("IMPORT will be included as file");
354                                 rule->import_type  = IMPORT_FILE;
355                         } else if (attr && strstr(attr, "parent")) {
356                                 dbg("IMPORT will include the parent values");
357                                 rule->import_type = IMPORT_PARENT;
358                         } else {
359                                 /* figure it out if it is executable */
360                                 char file[PATH_SIZE];
361                                 char *pos;
362                                 struct stat stats;
363
364                                 strlcpy(file, value, sizeof(file));
365                                 pos = strchr(file, ' ');
366                                 if (pos)
367                                         pos[0] = '\0';
368                                 dbg("IMPORT auto mode for '%s'", file);
369                                 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
370                                         dbg("IMPORT is executable, will be executed (autotype)");
371                                         rule->import_type  = IMPORT_PROGRAM;
372                                 } else {
373                                         dbg("IMPORT is not executable, will be included as file (autotype)");
374                                         rule->import_type  = IMPORT_FILE;
375                                 }
376                         }
377                         add_rule_key(rule, &rule->import, operation, value);
378                         valid = 1;
379                         continue;
380                 }
381
382                 if (strcasecmp(key, "DRIVER") == 0) {
383                         add_rule_key(rule, &rule->driver, operation, value);
384                         valid = 1;
385                         continue;
386                 }
387
388                 if (strcasecmp(key, "RESULT") == 0) {
389                         add_rule_key(rule, &rule->result, operation, value);
390                         valid = 1;
391                         continue;
392                 }
393
394                 if (strcasecmp(key, "PROGRAM") == 0) {
395                         add_rule_key(rule, &rule->program, operation, value);
396                         valid = 1;
397                         continue;
398                 }
399
400                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
401                         attr = get_key_attribute(key + sizeof("NAME")-1);
402                         if (attr != NULL) {
403                                 if (strstr(attr, "all_partitions") != NULL) {
404                                         dbg("creation of partition nodes requested");
405                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
406                                 }
407                                 if (strstr(attr, "ignore_remove") != NULL) {
408                                         dbg("remove event should be ignored");
409                                         rule->ignore_remove = 1;
410                                 }
411                         }
412                         if (value[0] == '\0') {
413                                 dbg("name empty device should be ignored");
414                                 rule->name.operation = operation;
415                                 rule->ignore_device = 1;
416                         } else
417                                 add_rule_key(rule, &rule->name, operation, value);
418                         continue;
419                 }
420
421                 if (strcasecmp(key, "SYMLINK") == 0) {
422                         add_rule_key(rule, &rule->symlink, operation, value);
423                         valid = 1;
424                         continue;
425                 }
426
427                 if (strcasecmp(key, "OWNER") == 0) {
428                         valid = 1;
429                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
430                                 char *endptr;
431                                 strtoul(value, &endptr, 10);
432                                 if (endptr[0] != '\0') {
433                                         char owner[32];
434                                         uid_t uid = lookup_user(value);
435                                         dbg("replacing username='%s' by id=%i", value, uid);
436                                         sprintf(owner, "%li", uid);
437                                         add_rule_key(rule, &rule->owner, operation, owner);
438                                         continue;
439                                 }
440                         }
441
442                         add_rule_key(rule, &rule->owner, operation, value);
443                         continue;
444                 }
445
446                 if (strcasecmp(key, "GROUP") == 0) {
447                         valid = 1;
448                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
449                                 char *endptr;
450                                 strtoul(value, &endptr, 10);
451                                 if (endptr[0] != '\0') {
452                                         char group[32];
453                                         gid_t gid = lookup_group(value);
454                                         dbg("replacing groupname='%s' by id=%i", value, gid);
455                                         sprintf(group, "%li", gid);
456                                         add_rule_key(rule, &rule->group, operation, group);
457                                         continue;
458                                 }
459                         }
460
461                         add_rule_key(rule, &rule->group, operation, value);
462                         continue;
463                 }
464
465                 if (strcasecmp(key, "MODE") == 0) {
466                         rule->mode = strtol(value, NULL, 8);
467                         rule->mode_operation = operation;
468                         valid = 1;
469                         continue;
470                 }
471
472                 if (strcasecmp(key, "RUN") == 0) {
473                         add_rule_key(rule, &rule->run, operation, value);
474                         valid = 1;
475                         continue;
476                 }
477
478                 if (strcasecmp(key, "OPTIONS") == 0) {
479                         if (strstr(value, "last_rule") != NULL) {
480                                 dbg("last rule to be applied");
481                                 rule->last_rule = 1;
482                         }
483                         if (strstr(value, "ignore_device") != NULL) {
484                                 dbg("device should be ignored");
485                                 rule->ignore_device = 1;
486                         }
487                         if (strstr(value, "ignore_remove") != NULL) {
488                                 dbg("remove event should be ignored");
489                                 rule->ignore_remove = 1;
490                         }
491                         if (strstr(value, "all_partitions") != NULL) {
492                                 dbg("creation of partition nodes requested");
493                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
494                         }
495                         valid = 1;
496                         continue;
497                 }
498
499                 err("unknown key '%s', in '%s'", key, line);
500         }
501
502         /* skip line if not any valid key was found */
503         if (!valid) {
504                 err("invalid rule '%s'", line);
505                 goto exit;
506         }
507
508         /* grow buffer and add rule */
509         rule_size = sizeof(struct udev_rule) + rule->bufsize;
510         padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
511         dbg("add %zi padding bytes", padding);
512         rule_size += padding;
513         rule->bufsize += padding;
514
515         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
516         if (!rules->buf) {
517                 err("realloc failed");
518                 goto exit;
519         }
520         dbg("adding rule to offset %zi", rules->bufsize);
521         memcpy(rules->buf + rules->bufsize, rule, rule_size);
522         rules->bufsize += rule_size;
523 exit:
524         free(rule);
525         return 0;
526 }
527
528 static int parse_file(struct udev_rules *rules, const char *filename)
529 {
530         char line[LINE_SIZE];
531         char *bufline;
532         int lineno;
533         char *buf;
534         size_t bufsize;
535         size_t cur;
536         size_t count;
537         int retval = 0;
538
539         if (file_map(filename, &buf, &bufsize) != 0) {
540                 err("can't open '%s' as rules file", filename);
541                 return -1;
542         }
543         dbg("reading '%s' as rules file", filename);
544
545         /* loop through the whole file */
546         cur = 0;
547         lineno = 0;
548         while (cur < bufsize) {
549                 unsigned int i, j;
550
551                 count = buf_get_line(buf, bufsize, cur);
552                 bufline = &buf[cur];
553                 cur += count+1;
554                 lineno++;
555
556                 if (count >= sizeof(line)) {
557                         info("line too long, rule skipped %s, line %d", filename, lineno);
558                         continue;
559                 }
560
561                 /* eat the whitespace */
562                 while ((count > 0) && isspace(bufline[0])) {
563                         bufline++;
564                         count--;
565                 }
566                 if (count == 0)
567                         continue;
568
569                 /* see if this is a comment */
570                 if (bufline[0] == COMMENT_CHARACTER)
571                         continue;
572
573                 /* skip backslash and newline from multi line rules */
574                 for (i = j = 0; i < count; i++) {
575                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
576                                 continue;
577
578                         line[j++] = bufline[i];
579                 }
580                 line[j] = '\0';
581
582                 dbg("read '%s'", line);
583                 add_to_rules(rules, line);
584         }
585
586         file_unmap(buf, bufsize);
587         return retval;
588 }
589
590 static int rules_map(struct udev_rules *rules, const char *filename)
591 {
592         if (file_map(filename, &rules->buf, &rules->bufsize)) {
593                 rules->buf = NULL;
594                 return -1;
595         }
596         if (rules->bufsize == 0) {
597                 file_unmap(rules->buf, rules->bufsize);
598                 rules->buf = NULL;
599                 return -1;
600         }
601         rules->mapped = 1;
602
603         return 0;
604 }
605
606 int udev_rules_init(struct udev_rules *rules, int resolve_names)
607 {
608         char comp[PATH_SIZE];
609         struct stat stats;
610         int retval;
611
612         memset(rules, 0x00, sizeof(struct udev_rules));
613         rules->resolve_names = resolve_names;
614
615         /* check for precompiled rules */
616         strlcpy(comp, udev_rules_filename, sizeof(comp));
617         strlcat(comp, ".compiled", sizeof(comp));
618         if (stat(comp, &stats) == 0) {
619                 dbg("map compiled rules '%s'", comp);
620                 if (rules_map(rules, comp) == 0)
621                         return 0;
622         }
623
624         if (stat(udev_rules_filename, &stats) != 0)
625                 return -1;
626
627         if ((stats.st_mode & S_IFMT) != S_IFDIR) {
628                 dbg("parse single rules file '%s'", udev_rules_filename);
629                 retval = parse_file(rules, udev_rules_filename);
630         } else {
631                 struct name_entry *name_loop, *name_tmp;
632                 LIST_HEAD(name_list);
633
634                 dbg("parse rules directory '%s'", udev_rules_filename);
635                 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
636
637                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
638                         parse_file(rules, name_loop->name);
639                         list_del(&name_loop->node);
640                 }
641         }
642
643         return retval;
644 }
645
646 void udev_rules_close(struct udev_rules *rules)
647 {
648         if (rules->mapped)
649                 file_unmap(rules->buf, rules->bufsize);
650         else
651                 free(rules->buf);
652
653         rules->buf = NULL;
654 }