chiark / gitweb /
add WAIT_FOR_SYSFS key to loop until a file in sysfs arrives
[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                 return NULL;
57
58         /* get next rule */
59         rule = (struct udev_rule *) (rules->buf + rules->current);
60         rules->current += sizeof(struct udev_rule) + rule->bufsize;
61
62         return rule;
63 }
64
65 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
66 {
67         char *linepos;
68         char *temp;
69
70         linepos = *line;
71         if (!linepos)
72                 return -1;
73
74         /* skip whitespace */
75         while (isspace(linepos[0]) || linepos[0] == ',')
76                 linepos++;
77
78         /* get the key */
79         *key = linepos;
80         while (1) {
81                 linepos++;
82                 if (linepos[0] == '\0')
83                         return -1;
84                 if (isspace(linepos[0]))
85                         break;
86                 if (linepos[0] == '=')
87                         break;
88                 if (linepos[0] == '+')
89                         break;
90                 if (linepos[0] == '!')
91                         break;
92                 if (linepos[0] == ':')
93                         break;
94         }
95
96         /* remember end of key */
97         temp = linepos;
98
99         /* skip whitespace after key */
100         while (isspace(linepos[0]))
101                 linepos++;
102
103         /* get operation type */
104         if (linepos[0] == '=' && linepos[1] == '=') {
105                 *operation = KEY_OP_MATCH;
106                 linepos += 2;
107                 dbg("operator=match");
108         } else if (linepos[0] == '!' && linepos[1] == '=') {
109                 *operation = KEY_OP_NOMATCH;
110                 linepos += 2;
111                 dbg("operator=nomatch");
112         } else if (linepos[0] == '+' && linepos[1] == '=') {
113                 *operation = KEY_OP_ADD;
114                 linepos += 2;
115                 dbg("operator=add");
116         } else if (linepos[0] == '=') {
117                 *operation = KEY_OP_ASSIGN;
118                 linepos++;
119                 dbg("operator=assign");
120         } else if (linepos[0] == ':' && linepos[1] == '=') {
121                 *operation = KEY_OP_ASSIGN_FINAL;
122                 linepos += 2;
123                 dbg("operator=assign_final");
124         } else
125                 return -1;
126
127         /* terminate key */
128         temp[0] = '\0';
129         dbg("key='%s'", *key);
130
131         /* skip whitespace after operator */
132         while (isspace(linepos[0]))
133                 linepos++;
134
135         /* get the value*/
136         if (linepos[0] == '"')
137                 linepos++;
138         else
139                 return -1;
140         *value = linepos;
141
142         temp = strchr(linepos, '"');
143         if (!temp)
144                 return -1;
145         temp[0] = '\0';
146         temp++;
147         dbg("value='%s'", *value);
148
149         /* move line to next key */
150         *line = temp;
151
152         return 0;
153 }
154
155 /* extract possible KEY{attr} */
156 static char *get_key_attribute(char *str)
157 {
158         char *pos;
159         char *attr;
160
161         attr = strchr(str, '{');
162         if (attr != NULL) {
163                 attr++;
164                 pos = strchr(attr, '}');
165                 if (pos == NULL) {
166                         err("missing closing brace for format");
167                         return NULL;
168                 }
169                 pos[0] = '\0';
170                 dbg("attribute='%s'", attr);
171                 return attr;
172         }
173
174         return NULL;
175 }
176
177 static int add_rule_key(struct udev_rule *rule, struct key *key,
178                         enum key_operation operation, const char *value)
179 {
180         size_t val_len = strnlen(value, PATH_SIZE);
181
182         key->operation = operation;
183
184         key->val_off = rule->bufsize;
185         strlcpy(rule->buf + rule->bufsize, value, val_len+1);
186         rule->bufsize += val_len+1;
187
188         return 0;
189 }
190
191 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
192                              enum key_operation operation, const char *key, const char *value)
193 {
194         size_t key_len = strnlen(key, PATH_SIZE);
195
196         if (pairs->count >= PAIRS_MAX) {
197                 err("skip, too many keys in a single rule");
198                 return -1;
199         }
200
201         add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
202
203         /* add the key-name of the pair */
204         pairs->keys[pairs->count].key_name_off = rule->bufsize;
205         strlcpy(rule->buf + rule->bufsize, key, key_len+1);
206         rule->bufsize += key_len+1;
207
208         pairs->count++;
209
210         return 0;
211 }
212
213 static int add_to_rules(struct udev_rules *rules, char *line)
214 {
215         struct udev_rule *rule;
216         size_t rule_size;
217         int valid;
218         char *linepos;
219         char *attr;
220         int retval;
221
222         /* get all the keys */
223         rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
224         if (!rule) {
225                 err("malloc failed");
226                 return -1;
227         }
228         linepos = line;
229         valid = 0;
230
231         while (1) {
232                 char *key;
233                 char *value;
234                 enum key_operation operation = KEY_OP_UNSET;
235
236                 retval = get_key(&linepos, &key, &operation, &value);
237                 if (retval)
238                         break;
239
240                 if (strcasecmp(key, "KERNEL") == 0) {
241                         add_rule_key(rule, &rule->kernel_name, operation, value);
242                         valid = 1;
243                         continue;
244                 }
245
246                 if (strcasecmp(key, "SUBSYSTEM") == 0) {
247                         add_rule_key(rule, &rule->subsystem, operation, value);
248                         valid = 1;
249                         continue;
250                 }
251
252                 if (strcasecmp(key, "ACTION") == 0) {
253                         add_rule_key(rule, &rule->action, operation, value);
254                         valid = 1;
255                         continue;
256                 }
257
258                 if (strcasecmp(key, "DEVPATH") == 0) {
259                         add_rule_key(rule, &rule->devpath, operation, value);
260                         valid = 1;
261                         continue;
262                 }
263
264                 if (strcasecmp(key, "BUS") == 0) {
265                         add_rule_key(rule, &rule->bus, operation, value);
266                         valid = 1;
267                         continue;
268                 }
269
270                 if (strcasecmp(key, "ID") == 0) {
271                         add_rule_key(rule, &rule->id, operation, value);
272                         valid = 1;
273                         continue;
274                 }
275
276                 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
277                         attr = get_key_attribute(key + sizeof("SYSFS")-1);
278                         if (attr == NULL) {
279                                 err("error parsing SYSFS attribute in '%s'", line);
280                                 continue;
281                         }
282                         add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
283                         valid = 1;
284                         continue;
285                 }
286
287                 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
288                         add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
289                         valid = 1;
290                         continue;
291                 }
292
293                 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
294                         attr = get_key_attribute(key + sizeof("ENV")-1);
295                         if (attr == NULL) {
296                                 err("error parsing ENV attribute");
297                                 continue;
298                         }
299                         add_rule_key_pair(rule, &rule->env, operation, attr, value);
300                         valid = 1;
301                         continue;
302                 }
303
304                 if (strcasecmp(key, "MODALIAS") == 0) {
305                         add_rule_key(rule, &rule->modalias, operation, value);
306                         valid = 1;
307                         continue;
308                 }
309
310                 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
311                         attr = get_key_attribute(key + sizeof("IMPORT")-1);
312                         if (attr && strstr(attr, "program")) {
313                                 dbg("IMPORT will be executed");
314                                 rule->import_exec = 1;
315                         } else if (attr && strstr(attr, "file")) {
316                                 dbg("IMPORT will be included as file");
317                         } else {
318                                 /* figure it out if it is executable */
319                                 char file[PATH_SIZE];
320                                 char *pos;
321                                 struct stat stats;
322
323                                 strlcpy(file, value, sizeof(file));
324                                 pos = strchr(file, ' ');
325                                 if (pos)
326                                         pos[0] = '\0';
327                                 dbg("IMPORT auto mode for '%s'", file);
328                                 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
329                                                 dbg("IMPORT is executable, will be executed");
330                                                 rule->import_exec = 1;
331                                 }
332                         }
333                         add_rule_key(rule, &rule->import, operation, value);
334                         valid = 1;
335                         continue;
336                 }
337
338                 if (strcasecmp(key, "DRIVER") == 0) {
339                         add_rule_key(rule, &rule->driver, operation, value);
340                         valid = 1;
341                         continue;
342                 }
343
344                 if (strcasecmp(key, "RESULT") == 0) {
345                         add_rule_key(rule, &rule->result, operation, value);
346                         valid = 1;
347                         continue;
348                 }
349
350                 if (strcasecmp(key, "PROGRAM") == 0) {
351                         add_rule_key(rule, &rule->program, operation, value);
352                         valid = 1;
353                         continue;
354                 }
355
356                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
357                         attr = get_key_attribute(key + sizeof("NAME")-1);
358                         if (attr != NULL) {
359                                 if (strstr(attr, "all_partitions") != NULL) {
360                                         dbg("creation of partition nodes requested");
361                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
362                                 }
363                                 if (strstr(attr, "ignore_remove") != NULL) {
364                                         dbg("remove event should be ignored");
365                                         rule->ignore_remove = 1;
366                                 }
367                         }
368                         add_rule_key(rule, &rule->name, operation, value);
369                         continue;
370                 }
371
372                 if (strcasecmp(key, "SYMLINK") == 0) {
373                         add_rule_key(rule, &rule->symlink, operation, value);
374                         valid = 1;
375                         continue;
376                 }
377
378                 if (strcasecmp(key, "OWNER") == 0) {
379                         valid = 1;
380                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
381                                 char *endptr;
382                                 strtoul(value, &endptr, 10);
383                                 if (endptr[0] != '\0') {
384                                         char owner[32];
385                                         uid_t uid = lookup_user(value);
386                                         dbg("replacing username='%s' by id=%i", value, uid);
387                                         sprintf(owner, "%li", uid);
388                                         add_rule_key(rule, &rule->owner, operation, owner);
389                                         continue;
390                                 }
391                         }
392
393                         add_rule_key(rule, &rule->owner, operation, value);
394                         continue;
395                 }
396
397                 if (strcasecmp(key, "GROUP") == 0) {
398                         valid = 1;
399                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
400                                 char *endptr;
401                                 strtoul(value, &endptr, 10);
402                                 if (endptr[0] != '\0') {
403                                         char group[32];
404                                         gid_t gid = lookup_group(value);
405                                         dbg("replacing groupname='%s' by id=%i", value, gid);
406                                         sprintf(group, "%li", gid);
407                                         add_rule_key(rule, &rule->owner, operation, group);
408                                         continue;
409                                 }
410                         }
411
412                         add_rule_key(rule, &rule->group, operation, value);
413                         continue;
414                 }
415
416                 if (strcasecmp(key, "MODE") == 0) {
417                         rule->mode = strtol(value, NULL, 8);
418                         rule->mode_operation = operation;
419                         valid = 1;
420                         continue;
421                 }
422
423                 if (strcasecmp(key, "RUN") == 0) {
424                         add_rule_key(rule, &rule->run, operation, value);
425                         valid = 1;
426                         continue;
427                 }
428
429                 if (strcasecmp(key, "OPTIONS") == 0) {
430                         if (strstr(value, "last_rule") != NULL) {
431                                 dbg("last rule to be applied");
432                                 rule->last_rule = 1;
433                         }
434                         if (strstr(value, "ignore_device") != NULL) {
435                                 dbg("device should be ignored");
436                                 rule->ignore_device = 1;
437                         }
438                         if (strstr(value, "ignore_remove") != NULL) {
439                                 dbg("remove event should be ignored");
440                                 rule->ignore_remove = 1;
441                         }
442                         if (strstr(value, "all_partitions") != NULL) {
443                                 dbg("creation of partition nodes requested");
444                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
445                         }
446                         valid = 1;
447                         continue;
448                 }
449
450                 err("unknown key '%s', in '%s'", key, line);
451         }
452
453         /* skip line if not any valid key was found */
454         if (!valid) {
455                 err("invalid rule '%s'", line);
456                 goto exit;
457         }
458
459         /* grow buffer and add rule */
460         rule_size = sizeof(struct udev_rule) + rule->bufsize;
461         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
462         if (!rules->buf) {
463                 err("realloc failed");
464                 goto exit;
465         }
466         memcpy(rules->buf + rules->bufsize, rule, rule_size);
467         rules->bufsize += rule_size;
468 exit:
469         free(rule);
470         return 0;
471 }
472
473 static int parse_file(struct udev_rules *rules, const char *filename)
474 {
475         char line[LINE_SIZE];
476         char *bufline;
477         int lineno;
478         char *buf;
479         size_t bufsize;
480         size_t cur;
481         size_t count;
482         int retval = 0;
483
484         if (file_map(filename, &buf, &bufsize) != 0) {
485                 err("can't open '%s' as rules file", filename);
486                 return -1;
487         }
488         dbg("reading '%s' as rules file", filename);
489
490         /* loop through the whole file */
491         cur = 0;
492         lineno = 0;
493         while (cur < bufsize) {
494                 unsigned int i, j;
495
496                 count = buf_get_line(buf, bufsize, cur);
497                 bufline = &buf[cur];
498                 cur += count+1;
499                 lineno++;
500
501                 if (count >= sizeof(line)) {
502                         info("line too long, rule skipped %s, line %d", filename, lineno);
503                         continue;
504                 }
505
506                 /* eat the whitespace */
507                 while ((count > 0) && isspace(bufline[0])) {
508                         bufline++;
509                         count--;
510                 }
511                 if (count == 0)
512                         continue;
513
514                 /* see if this is a comment */
515                 if (bufline[0] == COMMENT_CHARACTER)
516                         continue;
517
518                 /* skip backslash and newline from multi line rules */
519                 for (i = j = 0; i < count; i++) {
520                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
521                                 continue;
522
523                         line[j++] = bufline[i];
524                 }
525                 line[j] = '\0';
526
527                 dbg("read '%s'", line);
528                 add_to_rules(rules, line);
529         }
530
531         file_unmap(buf, bufsize);
532         return retval;
533 }
534
535 static int rules_map(struct udev_rules *rules, const char *filename)
536 {
537         if (file_map(filename, &rules->buf, &rules->bufsize)) {
538                 rules->buf = NULL;
539                 return -1;
540         }
541         if (rules->bufsize == 0) {
542                 file_unmap(rules->buf, rules->bufsize);
543                 rules->buf = NULL;
544                 return -1;
545         }
546
547         return 0;
548 }
549
550 int udev_rules_init(struct udev_rules *rules, int resolve_names)
551 {
552         char comp[PATH_SIZE];
553         struct stat stats;
554         int retval;
555
556         memset(rules, 0x00, sizeof(struct udev_rules));
557         rules->resolve_names = resolve_names;
558
559         /* check for precompiled rules */
560         strlcpy(comp, udev_rules_filename, sizeof(comp));
561         strlcat(comp, ".compiled", sizeof(comp));
562         if (stat(comp, &stats) == 0) {
563                 dbg("map compiled rules '%s'", comp);
564                 if (rules_map(rules, comp) == 0)
565                         return 0;
566         }
567
568         if (stat(udev_rules_filename, &stats) != 0)
569                 return -1;
570
571         if ((stats.st_mode & S_IFMT) != S_IFDIR) {
572                 dbg("parse single rules file '%s'", udev_rules_filename);
573                 retval = parse_file(rules, udev_rules_filename);
574         } else {
575                 struct name_entry *name_loop, *name_tmp;
576                 LIST_HEAD(name_list);
577
578                 dbg("parse rules directory '%s'", udev_rules_filename);
579                 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
580
581                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
582                         parse_file(rules, name_loop->name);
583                         list_del(&name_loop->node);
584                 }
585         }
586
587         return retval;
588 }
589
590 void udev_rules_close(struct udev_rules *rules)
591 {
592         if (rules->mapped)
593                 file_unmap(rules->buf, rules->bufsize);
594         else
595                 free(rules->buf);
596
597         rules->buf = NULL;
598 }