chiark / gitweb /
[PATCH] fix ia64 compile
[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', place='%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->place, 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 (strcasecmp(temp2, FIELD_PLACE) == 0) {
200                                 strlcpy(dev.place, temp3, sizeof(dev.place));
201                                 valid = 1;
202                                 continue;
203                         }
204
205                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
206                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
207                                 int sysfs_pair_num = 0;
208
209                                 /* find first unused pair */
210                                 while (pair->file[0] != '\0') {
211                                         ++sysfs_pair_num;
212                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
213                                                 pair = NULL;
214                                                 break;
215                                         }
216                                         ++pair;
217                                 }
218                                 if (pair) {
219                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
220                                         if (attr == NULL) {
221                                                 dbg("error parsing " FIELD_SYSFS " attribute");
222                                                 continue;
223                                         }
224                                         strlcpy(pair->file, attr, sizeof(pair->file));
225                                         strlcpy(pair->value, temp3, sizeof(pair->value));
226                                         valid = 1;
227                                 }
228                                 continue;
229                         }
230
231                         if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
232                                 strlcpy(dev.driver, temp3, sizeof(dev.driver));
233                                 valid = 1;
234                                 continue;
235                         }
236
237                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
238                                 program_given = 1;
239                                 strlcpy(dev.program, temp3, sizeof(dev.program));
240                                 valid = 1;
241                                 continue;
242                         }
243
244                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
245                                 strlcpy(dev.result, temp3, sizeof(dev.result));
246                                 valid = 1;
247                                 continue;
248                         }
249
250                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
251                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
252                                 /* FIXME: remove old style options and make OPTIONS= mandatory */
253                                 if (attr != NULL) {
254                                         if (strstr(attr, OPTION_PARTITIONS) != NULL) {
255                                                 dbg_parse("creation of partition nodes requested");
256                                                 dev.partitions = DEFAULT_PARTITIONS_COUNT;
257                                         }
258                                         if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
259                                                 dbg_parse("remove event should be ignored");
260                                                 dev.ignore_remove = 1;
261                                         }
262                                 }
263                                 if (temp3[0] != '\0')
264                                         strlcpy(dev.name, temp3, sizeof(dev.name));
265                                 else
266                                         dev.ignore_device = 1;
267                                 valid = 1;
268                                 continue;
269                         }
270
271                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
272                                 strlcpy(dev.symlink, temp3, sizeof(dev.symlink));
273                                 valid = 1;
274                                 continue;
275                         }
276
277                         if (strcasecmp(temp2, FIELD_OWNER) == 0) {
278                                 strlcpy(dev.owner, temp3, sizeof(dev.owner));
279                                 valid = 1;
280                                 continue;
281                         }
282
283                         if (strcasecmp(temp2, FIELD_GROUP) == 0) {
284                                 strlcpy(dev.group, temp3, sizeof(dev.group));
285                                 valid = 1;
286                                 continue;
287                         }
288
289                         if (strcasecmp(temp2, FIELD_MODE) == 0) {
290                                 dev.mode = strtol(temp3, NULL, 8);
291                                 valid = 1;
292                                 continue;
293                         }
294
295                         if (strcasecmp(temp2, FIELD_OPTIONS) == 0) {
296                                 if (strstr(temp3, OPTION_IGNORE_DEVICE) != NULL) {
297                                         dbg_parse("device should be ignored");
298                                         dev.ignore_device = 1;
299                                 }
300                                 if (strstr(temp3, OPTION_IGNORE_REMOVE) != NULL) {
301                                         dbg_parse("remove event should be ignored");
302                                         dev.ignore_remove = 1;
303                                 }
304                                 if (strstr(temp3, OPTION_PARTITIONS) != NULL) {
305                                         dbg_parse("creation of partition nodes requested");
306                                         dev.partitions = DEFAULT_PARTITIONS_COUNT;
307                                 }
308                                 valid = 1;
309                                 continue;
310                         }
311
312                         dbg("unknown type of field '%s'", temp2);
313                         goto error;
314                 }
315
316                 /* skip line if not any valid key was found */
317                 if (!valid)
318                         goto error;
319
320                 /* simple plausibility checks for given keys */
321                 if ((dev.sysfs_pair[0].file[0] == '\0') ^
322                     (dev.sysfs_pair[0].value[0] == '\0')) {
323                         info("inconsistency in " FIELD_SYSFS " key");
324                         goto error;
325                 }
326
327                 if ((dev.result[0] != '\0') && (program_given == 0)) {
328                         info(FIELD_RESULT " is only useful when "
329                              FIELD_PROGRAM " is called in any rule before");
330                         goto error;
331                 }
332
333                 dev.config_line = lineno;
334                 strlcpy(dev.config_file, filename, sizeof(dev.config_file));
335                 retval = add_config_dev(&dev);
336                 if (retval) {
337                         dbg("add_config_dev returned with error %d", retval);
338                         continue;
339 error:
340                         info("parse error %s, line %d:%d, rule skipped",
341                              filename, lineno, (int) (temp - line));
342                 }
343         }
344
345         file_unmap(buf, bufsize);
346         return retval;
347 }
348
349 int namedev_init(void)
350 {
351         struct stat stats;
352         int retval;
353
354         if (stat(udev_rules_filename, &stats) != 0)
355                 return -1;
356
357         if ((stats.st_mode & S_IFMT) != S_IFDIR)
358                 retval = namedev_parse(NULL, udev_rules_filename);
359         else
360                 retval = call_foreach_file(namedev_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
361
362         return retval;
363 }
364
365 void namedev_close(void)
366 {
367         struct config_device *dev;
368         struct config_device *temp_dev;
369
370         list_for_each_entry_safe(dev, temp_dev, &config_device_list, node) {
371                 list_del(&dev->node);
372                 free(dev);
373         }
374 }
375