chiark / gitweb /
[PATCH] fix NAME="foo-%c{N}" gets a truncated name
[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  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifdef DEBUG
25 /* define this to enable parsing debugging also */
26 /* #define DEBUG_PARSER */
27 #endif
28
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <errno.h>
39
40 #include "udev.h"
41 #include "logging.h"
42 #include "namedev.h"
43
44 LIST_HEAD(file_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         /*FIXME dump all sysfs's */
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                   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 }
69
70 void dump_config_dev_list(void)
71 {
72         struct config_device *dev;
73
74         list_for_each_entry(dev, &config_device_list, node)
75                 dump_config_dev(dev);
76 }
77
78 void dump_perm_dev(struct perm_device *dev)
79 {
80         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
81                   dev->name, dev->owner, dev->group, dev->mode);
82 }
83
84 void dump_perm_dev_list(void)
85 {
86         struct perm_device *dev;
87
88         list_for_each_entry(dev, &perm_device_list, node)
89                 dump_perm_dev(dev);
90 }
91
92 /* extract possible KEY{attr} or KEY_attr */
93 static char *get_key_attribute(char *str)
94 {
95         char *pos;
96         char *attr;
97
98         attr = strchr(str, '{');
99         if (attr != NULL) {
100                 attr++;
101                 pos = strchr(attr, '}');
102                 if (pos == NULL) {
103                         dbg("missing closing brace for format");
104                         return NULL;
105                 }
106                 pos[0] = '\0';
107                 dbg("attribute='%s'", attr);
108                 return attr;
109         }
110
111         attr = strchr(str, '_');
112         if (attr != NULL) {
113                 attr++;
114                 dbg("attribute='%s'", attr);
115                 return attr;
116         }
117
118         return NULL;
119 }
120
121 static int namedev_parse_rules(char *filename)
122 {
123         char line[255];
124         int lineno;
125         char *temp;
126         char *temp2;
127         char *temp3;
128         char *attr;
129         FILE *fd;
130         int program_given = 0;
131         int retval = 0;
132         struct config_device dev;
133
134         fd = fopen(filename, "r");
135         if (fd != NULL) {
136                 dbg("reading '%s' as rules file", filename);
137         } else {
138                 dbg("can't open '%s' as a rules file", filename);
139                 return -ENODEV;
140         }
141
142         /* loop through the whole file */
143         lineno = 0;
144         while (1) {
145                 /* get a line */
146                 temp = fgets(line, sizeof(line), fd);
147                 if (temp == NULL)
148                         goto exit;
149                 lineno++;
150                 dbg_parse("read '%s'", temp);
151
152                 /* eat the whitespace */
153                 while (isspace(*temp))
154                         ++temp;
155
156                 /* empty line? */
157                 if ((*temp == '\0') || (*temp == '\n'))
158                         continue;
159
160                 /* see if this is a comment */
161                 if (*temp == COMMENT_CHARACTER)
162                         continue;
163
164                 memset(&dev, 0x00, sizeof(struct config_device));
165
166                 /* get all known keys */
167                 while (1) {
168                         retval = parse_get_pair(&temp, &temp2, &temp3);
169                         if (retval)
170                                 break;
171
172                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
173                                 strfieldcpy(dev.bus, temp3);
174                                 continue;
175                         }
176
177                         if (strcasecmp(temp2, FIELD_ID) == 0) {
178                                 strfieldcpy(dev.id, temp3);
179                                 continue;
180                         }
181
182                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
183                                 strfieldcpy(dev.place, temp3);
184                                 continue;
185                         }
186
187                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
188                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
189                                 int sysfs_pair_num = 0;
190
191                                 /* find first unused pair */
192                                 while (pair->file[0] != '\0') {
193                                         ++sysfs_pair_num;
194                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
195                                                 pair = NULL;
196                                                 break;
197                                         }
198                                         ++pair;
199                                 }
200                                 if (pair) {
201                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
202                                         if (attr == NULL) {
203                                                 dbg("error parsing " FIELD_SYSFS " attribute");
204                                                 continue;
205                                         }
206                                         strfieldcpy(pair->file, attr);
207                                         strfieldcpy(pair->value, temp3);
208                                 }
209                                 continue;
210                         }
211
212                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
213                                 strfieldcpy(dev.kernel, temp3);
214                                 continue;
215                         }
216
217                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
218                                 program_given = 1;
219                                 strfieldcpy(dev.program, temp3);
220                                 continue;
221                         }
222
223                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
224                                 strfieldcpy(dev.result, temp3);
225                                 continue;
226                         }
227
228                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
229                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
230                                 if (attr != NULL)
231                                         if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
232                                                 dbg_parse("creation of partition nodes requested");
233                                                 dev.partitions = PARTITIONS_COUNT;
234                                         }
235                                 strfieldcpy(dev.name, temp3);
236                                 continue;
237                         }
238
239                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
240                                 strfieldcpy(dev.symlink, temp3);
241                                 continue;
242                         }
243
244                         dbg("unknown type of field '%s'", temp2);
245                         dbg("You might be using a rules file in the old format, please fix.");
246                         goto error;
247                 }
248
249                 /* simple plausibility check for given keys */
250                 if ((dev.sysfs_pair[0].file[0] == '\0') ^
251                     (dev.sysfs_pair[0].value[0] == '\0')) {
252                         dbg("inconsistency in " FIELD_SYSFS " key");
253                         goto error;
254                 }
255
256                 if ((dev.result[0] != '\0') && (program_given == 0)) {
257                         dbg(FIELD_RESULT " is only useful when "
258                             FIELD_PROGRAM " is called in any rule before");
259                         goto error;
260                 }
261
262                 dev.config_line = lineno;
263                 strfieldcpy(dev.config_file, filename);
264                 retval = add_config_dev(&dev);
265                 if (retval) {
266                         dbg("add_config_dev returned with error %d", retval);
267                         continue;
268 error:
269                         dbg("%s:%d:%d: parse error, rule skipped",
270                             filename, lineno, temp - line);
271                 }
272         }
273 exit:
274         fclose(fd);
275         return retval;
276 }
277
278 static int namedev_parse_permissions(char *filename)
279 {
280         char line[255];
281         char *temp;
282         char *temp2;
283         FILE *fd;
284         int retval = 0;
285         struct perm_device dev;
286
287         fd = fopen(filename, "r");
288         if (fd != NULL) {
289                 dbg("reading '%s' as permissions file", filename);
290         } else {
291                 dbg("can't open '%s' as permissions file", filename);
292                 return -ENODEV;
293         }
294
295         /* loop through the whole file */
296         while (1) {
297                 temp = fgets(line, sizeof(line), fd);
298                 if (temp == NULL)
299                         break;
300
301                 dbg_parse("read '%s'", temp);
302
303                 /* eat the whitespace at the beginning of the line */
304                 while (isspace(*temp))
305                         ++temp;
306
307                 /* empty line? */
308                 if ((*temp == '\0') || (*temp == '\n'))
309                         continue;
310
311                 /* see if this is a comment */
312                 if (*temp == COMMENT_CHARACTER)
313                         continue;
314
315                 memset(&dev, 0x00, sizeof(dev));
316
317                 /* parse the line */
318                 temp2 = strsep(&temp, ":");
319                 if (!temp2) {
320                         dbg("cannot parse line '%s'", line);
321                         continue;
322                 }
323                 strfieldcpy(dev.name, temp2);
324
325                 temp2 = strsep(&temp, ":");
326                 if (!temp2) {
327                         dbg("cannot parse line '%s'", line);
328                         continue;
329                 }
330                 strfieldcpy(dev.owner, temp2);
331
332                 temp2 = strsep(&temp, ":");
333                 if (!temp2) {
334                         dbg("cannot parse line '%s'", line);
335                         continue;
336                 }
337                 strfieldcpy(dev.group, temp2);
338
339                 if (!temp) {
340                         dbg("cannot parse line: %s", line);
341                         continue;
342                 }
343                 dev.mode = strtol(temp, NULL, 8);
344
345                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
346                           dev.name, dev.owner, dev.group,
347                           dev.mode);
348                 retval = add_perm_dev(&dev);
349                 if (retval) {
350                         dbg("add_perm_dev returned with error %d", retval);
351                         goto exit;
352                 }
353         }
354
355 exit:
356         fclose(fd);
357         return retval;
358 }
359
360 struct files {
361         struct list_head list;
362         char name[NAME_SIZE];
363 };
364
365 /* sort files in lexical order */
366 static int file_list_insert(char *filename)
367 {
368         struct files *loop_file;
369         struct files *new_file;
370
371         list_for_each_entry(loop_file, &file_list, list) {
372                 if (strcmp(loop_file->name, filename) > 0) {
373                         break;
374                 }
375         }
376
377         new_file = malloc(sizeof(struct files));
378         if (new_file == NULL) {
379                 dbg("error malloc");
380                 return -ENOMEM;
381         }
382
383         strfieldcpy(new_file->name, filename);
384         list_add_tail(&new_file->list, &loop_file->list);
385         return 0;
386 }
387
388 /* calls function for file or every file found in directory */
389 static int call_foreach_file(int parser (char *f) , char *filename, char *extension)
390 {
391         struct dirent *ent;
392         DIR *dir;
393         char *ext;
394         char file[NAME_SIZE];
395         struct stat stats;
396         struct files *loop_file;
397         struct files *tmp_file;
398
399         /* look if we have a plain file or a directory to scan */
400         stat(filename, &stats);
401         if ((stats.st_mode & S_IFMT) != S_IFDIR)
402                 return parser(filename);
403
404         /* sort matching filename into list */
405         dbg("open config as directory '%s'", filename);
406         dir = opendir(filename);
407         while (1) {
408                 ent = readdir(dir);
409                 if (ent == NULL || ent->d_name[0] == '\0')
410                         break;
411
412                 dbg("found file '%s'", ent->d_name);
413                 ext = strrchr(ent->d_name, '.');
414                 if (ext == NULL)
415                         continue;
416
417                 if (strcmp(ext, extension) == 0) {
418                         dbg("put file in list '%s'", ent->d_name);
419                         file_list_insert(ent->d_name);
420                 }
421         }
422
423         /* parse every file in the list */
424         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
425                 strfieldcpy(file, filename);
426                 strfieldcat(file, loop_file->name);
427                 parser(file);
428                 list_del(&loop_file->list);
429                 free(loop_file);
430         }
431
432         closedir(dir);
433         return 0;
434 }
435
436 int namedev_init_rules()
437 {
438         return call_foreach_file(namedev_parse_rules, udev_rules_filename, RULEFILE_EXT);
439 }
440
441 int namedev_init_permissions()
442 {
443         return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, PERMFILE_EXT);
444 }