chiark / gitweb /
[PATCH] This patch causes the remove handler to check that each symlink
[elogind.git] / namedev_parse.c
1 /*
2  * namedev_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 #ifdef DEBUG
26 /* define this to enable parsing debugging also */
27 /* #define DEBUG_PARSER */
28 #endif
29
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38
39 #include "udev_libc_wrapper.h"
40 #include "udev.h"
41 #include "udev_utils.h"
42 #include "logging.h"
43 #include "namedev.h"
44
45 LIST_HEAD(config_device_list);
46
47 static int add_config_dev(struct config_device *new_dev)
48 {
49         struct config_device *tmp_dev;
50
51         tmp_dev = malloc(sizeof(*tmp_dev));
52         if (tmp_dev == NULL)
53                 return -ENOMEM;
54         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
55         list_add_tail(&tmp_dev->node, &config_device_list);
56         //dump_config_dev(tmp_dev);
57         return 0;
58 }
59
60 void dump_config_dev(struct config_device *dev)
61 {
62         dbg_parse("name='%s', symlink='%s', bus='%s', id='%s', "
63                   "sysfs_file[0]='%s', sysfs_value[0]='%s', "
64                   "kernel='%s', program='%s', result='%s'"
65                   "owner='%s', group='%s', mode=%#o",
66                   dev->name, dev->symlink, dev->bus, dev->id,
67                   dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
68                   dev->kernel, dev->program, dev->result,
69                   dev->owner, dev->group, dev->mode);
70 }
71
72 void dump_config_dev_list(void)
73 {
74         struct config_device *dev;
75
76         list_for_each_entry(dev, &config_device_list, node)
77                 dump_config_dev(dev);
78 }
79
80 /* extract possible KEY{attr} */
81 static char *get_key_attribute(char *str)
82 {
83         char *pos;
84         char *attr;
85
86         attr = strchr(str, '{');
87         if (attr != NULL) {
88                 attr++;
89                 pos = strchr(attr, '}');
90                 if (pos == NULL) {
91                         dbg("missing closing brace for format");
92                         return NULL;
93                 }
94                 pos[0] = '\0';
95                 dbg("attribute='%s'", attr);
96                 return attr;
97         }
98
99         return NULL;
100 }
101
102 static int namedev_parse(struct udevice *udev, const char *filename)
103 {
104         char line[LINE_SIZE];
105         char *bufline;
106         int lineno;
107         char *temp;
108         char *temp2;
109         char *temp3;
110         char *attr;
111         char *buf;
112         size_t bufsize;
113         size_t cur;
114         size_t count;
115         int program_given = 0;
116         int valid;
117         int retval = 0;
118         struct config_device dev;
119
120         if (file_map(filename, &buf, &bufsize) == 0) {
121                 dbg("reading '%s' as rules file", filename);
122         } else {
123                 dbg("can't open '%s' as rules file", filename);
124                 return -1;
125         }
126
127         /* loop through the whole file */
128         cur = 0;
129         lineno = 0;
130         while (cur < bufsize) {
131                 unsigned int i, j;
132
133                 count = buf_get_line(buf, bufsize, cur);
134                 bufline = &buf[cur];
135                 cur += count+1;
136                 lineno++;
137
138                 if (count >= sizeof(line)) {
139                         info("line too long, rule skipped %s, line %d", filename, lineno);
140                         continue;
141                 }
142
143                 /* eat the whitespace */
144                 while ((count > 0) && isspace(bufline[0])) {
145                         bufline++;
146                         count--;
147                 }
148                 if (count == 0)
149                         continue;
150
151                 /* see if this is a comment */
152                 if (bufline[0] == COMMENT_CHARACTER)
153                         continue;
154
155                 /* skip backslash and newline from multi line rules */
156                 for (i = j = 0; i < count; i++) {
157                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
158                                 continue;
159
160                         line[j++] = bufline[i];
161                 }
162                 line[j] = '\0';
163                 dbg_parse("read '%s'", line);
164
165                 /* get all known keys */
166                 memset(&dev, 0x00, sizeof(struct config_device));
167                 temp = line;
168                 valid = 0;
169
170                 while (1) {
171                         retval = parse_get_pair(&temp, &temp2, &temp3);
172                         if (retval)
173                                 break;
174
175                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
176                                 strlcpy(dev.kernel, temp3, sizeof(dev.kernel));
177                                 valid = 1;
178                                 continue;
179                         }
180
181                         if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
182                                 strlcpy(dev.subsystem, temp3, sizeof(dev.subsystem));
183                                 valid = 1;
184                                 continue;
185                         }
186
187                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
188                                 strlcpy(dev.bus, temp3, sizeof(dev.bus));
189                                 valid = 1;
190                                 continue;
191                         }
192
193                         if (strcasecmp(temp2, FIELD_ID) == 0) {
194                                 strlcpy(dev.id, temp3, sizeof(dev.id));
195                                 valid = 1;
196                                 continue;
197                         }
198
199                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
200                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
201                                 int sysfs_pair_num = 0;
202
203                                 /* find first unused pair */
204                                 while (pair->file[0] != '\0') {
205                                         ++sysfs_pair_num;
206                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
207                                                 pair = NULL;
208                                                 break;
209                                         }
210                                         ++pair;
211                                 }
212                                 if (pair) {
213                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
214                                         if (attr == NULL) {
215                                                 dbg("error parsing " FIELD_SYSFS " attribute");
216                                                 continue;
217                                         }
218                                         strlcpy(pair->file, attr, sizeof(pair->file));
219                                         strlcpy(pair->value, temp3, sizeof(pair->value));
220                                         valid = 1;
221                                 }
222                                 continue;
223                         }
224
225                         if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
226                                 strlcpy(dev.driver, temp3, sizeof(dev.driver));
227                                 valid = 1;
228                                 continue;
229                         }
230
231                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
232                                 program_given = 1;
233                                 strlcpy(dev.program, temp3, sizeof(dev.program));
234                                 valid = 1;
235                                 continue;
236                         }
237
238                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
239                                 strlcpy(dev.result, temp3, sizeof(dev.result));
240                                 valid = 1;
241                                 continue;
242                         }
243
244                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
245                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
246                                 /* FIXME: remove old style options and make OPTIONS= mandatory */
247                                 if (attr != NULL) {
248                                         if (strstr(attr, OPTION_PARTITIONS) != NULL) {
249                                                 dbg_parse("creation of partition nodes requested");
250                                                 dev.partitions = DEFAULT_PARTITIONS_COUNT;
251                                         }
252                                         if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
253                                                 dbg_parse("remove event should be ignored");
254                                                 dev.ignore_remove = 1;
255                                         }
256                                 }
257                                 if (temp3[0] != '\0')
258                                         strlcpy(dev.name, temp3, sizeof(dev.name));
259                                 else
260                                         dev.ignore_device = 1;
261                                 valid = 1;
262                                 continue;
263                         }
264
265                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
266                                 strlcpy(dev.symlink, temp3, sizeof(dev.symlink));
267                                 valid = 1;
268                                 continue;
269                         }
270
271                         if (strcasecmp(temp2, FIELD_OWNER) == 0) {
272                                 strlcpy(dev.owner, temp3, sizeof(dev.owner));
273                                 valid = 1;
274                                 continue;
275                         }
276
277                         if (strcasecmp(temp2, FIELD_GROUP) == 0) {
278                                 strlcpy(dev.group, temp3, sizeof(dev.group));
279                                 valid = 1;
280                                 continue;
281                         }
282
283                         if (strcasecmp(temp2, FIELD_MODE) == 0) {
284                                 dev.mode = strtol(temp3, NULL, 8);
285                                 valid = 1;
286                                 continue;
287                         }
288
289                         if (strcasecmp(temp2, FIELD_OPTIONS) == 0) {
290                                 if (strstr(temp3, OPTION_IGNORE_DEVICE) != NULL) {
291                                         dbg_parse("device should be ignored");
292                                         dev.ignore_device = 1;
293                                 }
294                                 if (strstr(temp3, OPTION_IGNORE_REMOVE) != NULL) {
295                                         dbg_parse("remove event should be ignored");
296                                         dev.ignore_remove = 1;
297                                 }
298                                 if (strstr(temp3, OPTION_PARTITIONS) != NULL) {
299                                         dbg_parse("creation of partition nodes requested");
300                                         dev.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 ((dev.sysfs_pair[0].file[0] == '\0') ^
316                     (dev.sysfs_pair[0].value[0] == '\0')) {
317                         info("inconsistency in " FIELD_SYSFS " key");
318                         goto error;
319                 }
320
321                 if ((dev.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                 dev.config_line = lineno;
328                 strlcpy(dev.config_file, filename, sizeof(dev.config_file));
329                 retval = add_config_dev(&dev);
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 namedev_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 = namedev_parse(NULL, udev_rules_filename);
353         else
354                 retval = call_foreach_file(namedev_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
355
356         return retval;
357 }
358
359 void namedev_close(void)
360 {
361         struct config_device *dev;
362         struct config_device *temp_dev;
363
364         list_for_each_entry_safe(dev, temp_dev, &config_device_list, node) {
365                 list_del(&dev->node);
366                 free(dev);
367         }
368 }
369