chiark / gitweb /
udevcontrol: add max_childs command
[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 *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, rule, sizeof(struct udev_rule));
50         list_add_tail(&tmp_rule->node, &udev_rule_list);
51
52         dbg("name='%s', symlink='%s', bus='%s', id='%s', "
53             "sysfs_file[0]='%s', sysfs_value[0]='%s', "
54             "kernel='%s', program='%s', result='%s', "
55             "owner='%s', group='%s', mode=%#o, "
56             "all_partions=%u, ignore_remove=%u, ignore_device=%u, last_rule=%u",
57             rule->name, rule->symlink, rule->bus, rule->id,
58             rule->sysfs_pair[0].name, rule->sysfs_pair[0].value,
59             rule->kernel, rule->program, rule->result, rule->owner, rule->group, rule->mode,
60             rule->partitions, rule->ignore_remove, rule->ignore_device, rule->last_rule);
61
62         return 0;
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 rules_parse(const char *filename)
178 {
179         char line[LINE_SIZE];
180         char *bufline;
181         int lineno;
182         char *linepos;
183         char *attr;
184         char *buf;
185         size_t bufsize;
186         size_t cur;
187         size_t count;
188         int program_given = 0;
189         int valid;
190         int retval = 0;
191         struct udev_rule rule;
192
193         if (file_map(filename, &buf, &bufsize) != 0) {
194                 err("can't open '%s' as rules file", filename);
195                 return -1;
196         }
197         dbg("reading '%s' as rules file", filename);
198
199         /* loop through the whole file */
200         cur = 0;
201         lineno = 0;
202         while (cur < bufsize) {
203                 unsigned int i, j;
204
205                 count = buf_get_line(buf, bufsize, cur);
206                 bufline = &buf[cur];
207                 cur += count+1;
208                 lineno++;
209
210                 if (count >= sizeof(line)) {
211                         info("line too long, rule skipped %s, line %d", filename, lineno);
212                         continue;
213                 }
214
215                 /* eat the whitespace */
216                 while ((count > 0) && isspace(bufline[0])) {
217                         bufline++;
218                         count--;
219                 }
220                 if (count == 0)
221                         continue;
222
223                 /* see if this is a comment */
224                 if (bufline[0] == COMMENT_CHARACTER)
225                         continue;
226
227                 /* skip backslash and newline from multi line rules */
228                 for (i = j = 0; i < count; i++) {
229                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
230                                 continue;
231
232                         line[j++] = bufline[i];
233                 }
234                 line[j] = '\0';
235                 dbg("read '%s'", line);
236
237                 /* get all known keys */
238                 memset(&rule, 0x00, sizeof(struct udev_rule));
239                 linepos = line;
240                 valid = 0;
241
242                 while (1) {
243                         char *key;
244                         char *value;
245                         enum key_operation operation = KEY_OP_UNSET;
246
247                         retval = get_key(&linepos, &key, &operation, &value);
248                         if (retval)
249                                 break;
250
251                         if (strcasecmp(key, KEY_KERNEL) == 0) {
252                                 strlcpy(rule.kernel, value, sizeof(rule.kernel));
253                                 rule.kernel_operation = operation;
254                                 valid = 1;
255                                 continue;
256                         }
257
258                         if (strcasecmp(key, KEY_SUBSYSTEM) == 0) {
259                                 strlcpy(rule.subsystem, value, sizeof(rule.subsystem));
260                                 rule.subsystem_operation = operation;
261                                 valid = 1;
262                                 continue;
263                         }
264
265                         if (strcasecmp(key, KEY_ACTION) == 0) {
266                                 strlcpy(rule.action, value, sizeof(rule.action));
267                                 rule.action_operation = operation;
268                                 valid = 1;
269                                 continue;
270                         }
271
272                         if (strcasecmp(key, KEY_BUS) == 0) {
273                                 strlcpy(rule.bus, value, sizeof(rule.bus));
274                                 rule.bus_operation = operation;
275                                 valid = 1;
276                                 continue;
277                         }
278
279                         if (strcasecmp(key, KEY_ID) == 0) {
280                                 strlcpy(rule.id, value, sizeof(rule.id));
281                                 rule.id_operation = operation;
282                                 valid = 1;
283                                 continue;
284                         }
285
286                         if (strncasecmp(key, KEY_SYSFS, sizeof(KEY_SYSFS)-1) == 0) {
287                                 struct key_pair *pair;
288
289                                 if (rule.sysfs_pair_count >= KEY_SYSFS_PAIRS_MAX) {
290                                         err("skip rule, to many " KEY_SYSFS " keys in a single rule");
291                                         goto error;
292                                 }
293                                 pair = &rule.sysfs_pair[rule.sysfs_pair_count];
294                                 attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1);
295                                 if (attr == NULL) {
296                                         err("error parsing " KEY_SYSFS " attribute");
297                                         goto error;
298                                 }
299                                 strlcpy(pair->name, attr, sizeof(pair->name));
300                                 strlcpy(pair->value, value, sizeof(pair->value));
301                                 pair->operation = operation;
302                                 rule.sysfs_pair_count++;
303                                 valid = 1;
304                                 continue;
305                         }
306
307                         if (strncasecmp(key, KEY_ENV, sizeof(KEY_ENV)-1) == 0) {
308                                 struct key_pair *pair;
309
310                                 if (rule.env_pair_count >= KEY_ENV_PAIRS_MAX) {
311                                         err("skip rule, to many " KEY_ENV " keys in a single rule");
312                                         goto error;
313                                 }
314                                 pair = &rule.env_pair[rule.env_pair_count];
315                                 attr = get_key_attribute(key + sizeof(KEY_ENV)-1);
316                                 if (attr == NULL) {
317                                         err("error parsing " KEY_ENV " attribute");
318                                         continue;
319                                 }
320                                 strlcpy(pair->name, attr, sizeof(pair->name));
321                                 strlcpy(pair->value, value, sizeof(pair->value));
322                                 pair->operation = operation;
323                                 rule.env_pair_count++;
324                                 valid = 1;
325                                 continue;
326                         }
327
328                         if (strcasecmp(key, KEY_DRIVER) == 0) {
329                                 strlcpy(rule.driver, value, sizeof(rule.driver));
330                                 rule.driver_operation = operation;
331                                 valid = 1;
332                                 continue;
333                         }
334
335                         if (strcasecmp(key, KEY_RESULT) == 0) {
336                                 strlcpy(rule.result, value, sizeof(rule.result));
337                                 rule.result_operation = operation;
338                                 valid = 1;
339                                 continue;
340                         }
341
342                         if (strcasecmp(key, KEY_PROGRAM) == 0) {
343                                 strlcpy(rule.program, value, sizeof(rule.program));
344                                 rule.program_operation = operation;
345                                 program_given = 1;
346                                 valid = 1;
347                                 continue;
348                         }
349
350                         if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) {
351                                 attr = get_key_attribute(key + sizeof(KEY_NAME)-1);
352                                 if (attr != NULL) {
353                                         if (strstr(attr, OPTION_PARTITIONS) != NULL) {
354                                                 dbg("creation of partition nodes requested");
355                                                 rule.partitions = DEFAULT_PARTITIONS_COUNT;
356                                         }
357                                         /* FIXME: remove old style option and make OPTIONS= mandatory */
358                                         if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
359                                                 dbg("remove event should be ignored");
360                                                 rule.ignore_remove = 1;
361                                         }
362                                 }
363                                 rule.name_operation = operation;
364                                 strlcpy(rule.name, value, sizeof(rule.name));
365                                 valid = 1;
366                                 continue;
367                         }
368
369                         if (strcasecmp(key, KEY_SYMLINK) == 0) {
370                                 strlcpy(rule.symlink, value, sizeof(rule.symlink));
371                                 rule.symlink_operation = operation;
372                                 valid = 1;
373                                 continue;
374                         }
375
376                         if (strcasecmp(key, KEY_OWNER) == 0) {
377                                 strlcpy(rule.owner, value, sizeof(rule.owner));
378                                 rule.owner_operation = operation;
379                                 valid = 1;
380                                 continue;
381                         }
382
383                         if (strcasecmp(key, KEY_GROUP) == 0) {
384                                 strlcpy(rule.group, value, sizeof(rule.group));
385                                 rule.group_operation = operation;
386                                 valid = 1;
387                                 continue;
388                         }
389
390                         if (strcasecmp(key, KEY_MODE) == 0) {
391                                 rule.mode = strtol(value, NULL, 8);
392                                 rule.mode_operation = operation;
393                                 valid = 1;
394                                 continue;
395                         }
396
397                         if (strcasecmp(key, KEY_RUN) == 0) {
398                                 strlcpy(rule.run, value, sizeof(rule.run));
399                                 rule.run_operation = operation;
400                                 valid = 1;
401                                 continue;
402                         }
403
404                         if (strcasecmp(key, KEY_OPTIONS) == 0) {
405                                 if (strstr(value, OPTION_LAST_RULE) != NULL) {
406                                         dbg("last rule to be applied");
407                                         rule.last_rule = 1;
408                                 }
409                                 if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) {
410                                         dbg("device should be ignored");
411                                         rule.ignore_device = 1;
412                                 }
413                                 if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) {
414                                         dbg("remove event should be ignored");
415                                         rule.ignore_remove = 1;
416                                 }
417                                 if (strstr(value, OPTION_PARTITIONS) != NULL) {
418                                         dbg("creation of partition nodes requested");
419                                         rule.partitions = DEFAULT_PARTITIONS_COUNT;
420                                 }
421                                 valid = 1;
422                                 continue;
423                         }
424
425                         err("unknown key '%s'", key);
426                         goto error;
427                 }
428
429                 /* skip line if not any valid key was found */
430                 if (!valid)
431                         goto error;
432
433                 if ((rule.result[0] != '\0') && (program_given == 0)) {
434                         info(KEY_RESULT " is only useful when " KEY_PROGRAM " is called in any rule before");
435                         goto error;
436                 }
437
438                 rule.config_line = lineno;
439                 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
440                 retval = add_config_dev(&rule);
441                 if (retval) {
442                         dbg("add_config_dev returned with error %d", retval);
443                         continue;
444 error:
445                         err("parse error %s, line %d:%d, rule skipped",
446                              filename, lineno, (int) (linepos - line));
447                 }
448         }
449
450         file_unmap(buf, bufsize);
451         return retval;
452 }
453
454 int udev_rules_init(void)
455 {
456         struct stat stats;
457         int retval;
458
459         if (stat(udev_rules_filename, &stats) != 0)
460                 return -1;
461
462         if ((stats.st_mode & S_IFMT) != S_IFDIR)
463                 retval = rules_parse(udev_rules_filename);
464         else {
465                 struct name_entry *name_loop, *name_tmp;
466                 LIST_HEAD(name_list);
467
468                 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
469
470                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
471                         rules_parse(name_loop->name);
472                         list_del(&name_loop->node);
473                 }
474         }
475
476         return retval;
477 }
478
479 void udev_rules_close(void)
480 {
481         struct udev_rule *rule;
482         struct udev_rule *temp_rule;
483
484         list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) {
485                 list_del(&rule->node);
486                 free(rule);
487         }
488 }
489