chiark / gitweb /
f77f3db915758e24200e2d6201c14413572641cc
[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 LIST_HEAD(udev_rule_list);
41
42 static int add_config_dev(struct udev_rule *new_rule)
43 {
44         struct udev_rule *tmp_rule;
45
46         tmp_rule = malloc(sizeof(*tmp_rule));
47         if (tmp_rule == NULL)
48                 return -ENOMEM;
49         memcpy(tmp_rule, new_rule, sizeof(*tmp_rule));
50         list_add_tail(&tmp_rule->node, &udev_rule_list);
51         udev_rule_dump(tmp_rule);
52
53         return 0;
54 }
55
56 void udev_rule_dump(struct udev_rule *rule)
57 {
58         dbg("name='%s', symlink='%s', bus='%s', id='%s', "
59             "sysfs_file[0]='%s', sysfs_value[0]='%s', "
60             "kernel='%s', program='%s', result='%s'"
61             "owner='%s', group='%s', mode=%#o",
62             rule->name, rule->symlink, rule->bus, rule->id,
63             rule->sysfs_pair[0].file, rule->sysfs_pair[0].value,
64             rule->kernel, rule->program, rule->result,
65             rule->owner, rule->group, rule->mode);
66 }
67
68 void udev_rule_list_dump(void)
69 {
70         struct udev_rule *rule;
71
72         list_for_each_entry(rule, &udev_rule_list, node)
73                 udev_rule_dump(rule);
74 }
75
76 /* extract possible KEY{attr} */
77 static char *get_key_attribute(char *str)
78 {
79         char *pos;
80         char *attr;
81
82         attr = strchr(str, '{');
83         if (attr != NULL) {
84                 attr++;
85                 pos = strchr(attr, '}');
86                 if (pos == NULL) {
87                         dbg("missing closing brace for format");
88                         return NULL;
89                 }
90                 pos[0] = '\0';
91                 dbg("attribute='%s'", attr);
92                 return attr;
93         }
94
95         return NULL;
96 }
97
98 static int rules_parse(struct udevice *udev, const char *filename)
99 {
100         char line[LINE_SIZE];
101         char *bufline;
102         int lineno;
103         char *temp;
104         char *temp2;
105         char *temp3;
106         char *attr;
107         char *buf;
108         size_t bufsize;
109         size_t cur;
110         size_t count;
111         int program_given = 0;
112         int valid;
113         int retval = 0;
114         struct udev_rule rule;
115
116         if (file_map(filename, &buf, &bufsize) == 0) {
117                 dbg("reading '%s' as rules file", filename);
118         } else {
119                 dbg("can't open '%s' as rules file", filename);
120                 return -1;
121         }
122
123         /* loop through the whole file */
124         cur = 0;
125         lineno = 0;
126         while (cur < bufsize) {
127                 unsigned int i, j;
128
129                 count = buf_get_line(buf, bufsize, cur);
130                 bufline = &buf[cur];
131                 cur += count+1;
132                 lineno++;
133
134                 if (count >= sizeof(line)) {
135                         info("line too long, rule skipped %s, line %d", filename, lineno);
136                         continue;
137                 }
138
139                 /* eat the whitespace */
140                 while ((count > 0) && isspace(bufline[0])) {
141                         bufline++;
142                         count--;
143                 }
144                 if (count == 0)
145                         continue;
146
147                 /* see if this is a comment */
148                 if (bufline[0] == COMMENT_CHARACTER)
149                         continue;
150
151                 /* skip backslash and newline from multi line rules */
152                 for (i = j = 0; i < count; i++) {
153                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
154                                 continue;
155
156                         line[j++] = bufline[i];
157                 }
158                 line[j] = '\0';
159                 dbg("read '%s'", line);
160
161                 /* get all known keys */
162                 memset(&rule, 0x00, sizeof(struct udev_rule));
163                 temp = line;
164                 valid = 0;
165
166                 while (1) {
167                         retval = parse_get_pair(&temp, &temp2, &temp3);
168                         if (retval)
169                                 break;
170
171                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
172                                 strlcpy(rule.kernel, temp3, sizeof(rule.kernel));
173                                 valid = 1;
174                                 continue;
175                         }
176
177                         if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
178                                 strlcpy(rule.subsystem, temp3, sizeof(rule.subsystem));
179                                 valid = 1;
180                                 continue;
181                         }
182
183                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
184                                 strlcpy(rule.bus, temp3, sizeof(rule.bus));
185                                 valid = 1;
186                                 continue;
187                         }
188
189                         if (strcasecmp(temp2, FIELD_ID) == 0) {
190                                 strlcpy(rule.id, temp3, sizeof(rule.id));
191                                 valid = 1;
192                                 continue;
193                         }
194
195                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
196                                 struct sysfs_pair *pair = &rule.sysfs_pair[0];
197                                 int sysfs_pair_num = 0;
198
199                                 /* find first unused pair */
200                                 while (pair->file[0] != '\0') {
201                                         ++sysfs_pair_num;
202                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
203                                                 pair = NULL;
204                                                 break;
205                                         }
206                                         ++pair;
207                                 }
208                                 if (pair) {
209                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
210                                         if (attr == NULL) {
211                                                 dbg("error parsing " FIELD_SYSFS " attribute");
212                                                 continue;
213                                         }
214                                         strlcpy(pair->file, attr, sizeof(pair->file));
215                                         strlcpy(pair->value, temp3, sizeof(pair->value));
216                                         valid = 1;
217                                 }
218                                 continue;
219                         }
220
221                         if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
222                                 strlcpy(rule.driver, temp3, sizeof(rule.driver));
223                                 valid = 1;
224                                 continue;
225                         }
226
227                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
228                                 program_given = 1;
229                                 strlcpy(rule.program, temp3, sizeof(rule.program));
230                                 valid = 1;
231                                 continue;
232                         }
233
234                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
235                                 strlcpy(rule.result, temp3, sizeof(rule.result));
236                                 valid = 1;
237                                 continue;
238                         }
239
240                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
241                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
242                                 /* FIXME: remove old style options and make OPTIONS= mandatory */
243                                 if (attr != NULL) {
244                                         if (strstr(attr, OPTION_PARTITIONS) != NULL) {
245                                                 dbg("creation of partition nodes requested");
246                                                 rule.partitions = DEFAULT_PARTITIONS_COUNT;
247                                         }
248                                         if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
249                                                 dbg("remove event should be ignored");
250                                                 rule.ignore_remove = 1;
251                                         }
252                                 }
253                                 if (temp3[0] != '\0')
254                                         strlcpy(rule.name, temp3, sizeof(rule.name));
255                                 else
256                                         rule.ignore_device = 1;
257                                 valid = 1;
258                                 continue;
259                         }
260
261                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
262                                 strlcpy(rule.symlink, temp3, sizeof(rule.symlink));
263                                 valid = 1;
264                                 continue;
265                         }
266
267                         if (strcasecmp(temp2, FIELD_OWNER) == 0) {
268                                 strlcpy(rule.owner, temp3, sizeof(rule.owner));
269                                 valid = 1;
270                                 continue;
271                         }
272
273                         if (strcasecmp(temp2, FIELD_GROUP) == 0) {
274                                 strlcpy(rule.group, temp3, sizeof(rule.group));
275                                 valid = 1;
276                                 continue;
277                         }
278
279                         if (strcasecmp(temp2, FIELD_MODE) == 0) {
280                                 rule.mode = strtol(temp3, NULL, 8);
281                                 valid = 1;
282                                 continue;
283                         }
284
285                         if (strcasecmp(temp2, FIELD_OPTIONS) == 0) {
286                                 if (strstr(temp3, OPTION_LAST_RULE) != NULL) {
287                                         dbg("last rule to be applied");
288                                         rule.last_rule = 1;
289                                 }
290                                 if (strstr(temp3, OPTION_IGNORE_DEVICE) != NULL) {
291                                         dbg("device should be ignored");
292                                         rule.ignore_device = 1;
293                                 }
294                                 if (strstr(temp3, OPTION_IGNORE_REMOVE) != NULL) {
295                                         dbg("remove event should be ignored");
296                                         rule.ignore_remove = 1;
297                                 }
298                                 if (strstr(temp3, OPTION_PARTITIONS) != NULL) {
299                                         dbg("creation of partition nodes requested");
300                                         rule.partitions = DEFAULT_PARTITIONS_COUNT;
301                                 }
302                                 valid = 1;
303                                 continue;
304                         }
305
306                         dbg("unknown type of field '%s'", temp2);
307                         goto error;
308                 }
309
310                 /* skip line if not any valid key was found */
311                 if (!valid)
312                         goto error;
313
314                 /* simple plausibility checks for given keys */
315                 if ((rule.sysfs_pair[0].file[0] == '\0') ^
316                     (rule.sysfs_pair[0].value[0] == '\0')) {
317                         info("inconsistency in " FIELD_SYSFS " key");
318                         goto error;
319                 }
320
321                 if ((rule.result[0] != '\0') && (program_given == 0)) {
322                         info(FIELD_RESULT " is only useful when "
323                              FIELD_PROGRAM " is called in any rule before");
324                         goto error;
325                 }
326
327                 rule.config_line = lineno;
328                 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
329                 retval = add_config_dev(&rule);
330                 if (retval) {
331                         dbg("add_config_dev returned with error %d", retval);
332                         continue;
333 error:
334                         info("parse error %s, line %d:%d, rule skipped",
335                              filename, lineno, (int) (temp - line));
336                 }
337         }
338
339         file_unmap(buf, bufsize);
340         return retval;
341 }
342
343 int udev_rules_init(void)
344 {
345         struct stat stats;
346         int retval;
347
348         if (stat(udev_rules_filename, &stats) != 0)
349                 return -1;
350
351         if ((stats.st_mode & S_IFMT) != S_IFDIR)
352                 retval = rules_parse(NULL, udev_rules_filename);
353         else
354                 retval = call_foreach_file(rules_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
355
356         return retval;
357 }
358
359 void udev_rules_close(void)
360 {
361         struct udev_rule *rule;
362         struct udev_rule *temp_rule;
363
364         list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) {
365                 list_del(&rule->node);
366                 free(rule);
367         }
368 }
369