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