chiark / gitweb /
[PATCH] remove extra ; in namedev_parse.c
[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
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 static int add_perm_dev(struct perm_device *new_dev)
81 {
82         struct perm_device *dev;
83         struct perm_device *tmp_dev;
84
85         /* update the values if we already have the device */
86         list_for_each_entry(dev, &perm_device_list, node) {
87                 if (strcmp(new_dev->name, dev->name) != 0)
88                         continue;
89
90                 set_empty_perms(dev, new_dev->mode, new_dev->owner, new_dev->group);
91                 return 0;
92         }
93
94         /* not found, add new structure to the perm list */
95         tmp_dev = malloc(sizeof(*tmp_dev));
96         if (!tmp_dev)
97                 return -ENOMEM;
98
99         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
100         list_add_tail(&tmp_dev->node, &perm_device_list);
101         //dump_perm_dev(tmp_dev);
102         return 0;
103 }
104
105 void dump_perm_dev(struct perm_device *dev)
106 {
107         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
108                   dev->name, dev->owner, dev->group, dev->mode);
109 }
110
111 void dump_perm_dev_list(void)
112 {
113         struct perm_device *dev;
114
115         list_for_each_entry(dev, &perm_device_list, node)
116                 dump_perm_dev(dev);
117 }
118
119 /* extract possible KEY{attr} or KEY_attr */
120 static char *get_key_attribute(char *str)
121 {
122         char *pos;
123         char *attr;
124
125         attr = strchr(str, '{');
126         if (attr != NULL) {
127                 attr++;
128                 pos = strchr(attr, '}');
129                 if (pos == NULL) {
130                         dbg("missing closing brace for format");
131                         return NULL;
132                 }
133                 pos[0] = '\0';
134                 dbg("attribute='%s'", attr);
135                 return attr;
136         }
137
138         attr = strchr(str, '_');
139         if (attr != NULL) {
140                 attr++;
141                 dbg("attribute='%s'", attr);
142                 return attr;
143         }
144
145         return NULL;
146 }
147
148 static int namedev_parse_rules(char *filename)
149 {
150         char line[255];
151         int lineno;
152         char *temp;
153         char *temp2;
154         char *temp3;
155         char *attr;
156         FILE *fd;
157         int program_given = 0;
158         int retval = 0;
159         struct config_device dev;
160
161         fd = fopen(filename, "r");
162         if (fd != NULL) {
163                 dbg("reading '%s' as rules file", filename);
164         } else {
165                 dbg("can't open '%s' as a rules file", filename);
166                 return -ENODEV;
167         }
168
169         /* loop through the whole file */
170         lineno = 0;
171         while (1) {
172                 /* get a line */
173                 temp = fgets(line, sizeof(line), fd);
174                 if (temp == NULL)
175                         goto exit;
176                 lineno++;
177                 dbg_parse("read '%s'", temp);
178
179                 /* eat the whitespace */
180                 while (isspace(*temp))
181                         ++temp;
182
183                 /* empty line? */
184                 if ((*temp == '\0') || (*temp == '\n'))
185                         continue;
186
187                 /* see if this is a comment */
188                 if (*temp == COMMENT_CHARACTER)
189                         continue;
190
191                 memset(&dev, 0x00, sizeof(struct config_device));
192
193                 /* get all known keys */
194                 while (1) {
195                         retval = parse_get_pair(&temp, &temp2, &temp3);
196                         if (retval)
197                                 break;
198
199                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
200                                 strfieldcpy(dev.bus, temp3);
201                                 continue;
202                         }
203
204                         if (strcasecmp(temp2, FIELD_ID) == 0) {
205                                 strfieldcpy(dev.id, temp3);
206                                 continue;
207                         }
208
209                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
210                                 strfieldcpy(dev.place, temp3);
211                                 continue;
212                         }
213
214                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
215                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
216                                 int sysfs_pair_num = 0;
217
218                                 /* find first unused pair */
219                                 while (pair->file[0] != '\0') {
220                                         ++sysfs_pair_num;
221                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
222                                                 pair = NULL;
223                                                 break;
224                                         }
225                                         ++pair;
226                                 }
227                                 if (pair) {
228                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
229                                         if (attr == NULL) {
230                                                 dbg("error parsing " FIELD_SYSFS " attribute");
231                                                 continue;
232                                         }
233                                         strfieldcpy(pair->file, attr);
234                                         strfieldcpy(pair->value, temp3);
235                                 }
236                                 continue;
237                         }
238
239                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
240                                 strfieldcpy(dev.kernel, temp3);
241                                 continue;
242                         }
243
244                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
245                                 program_given = 1;
246                                 strfieldcpy(dev.program, temp3);
247                                 continue;
248                         }
249
250                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
251                                 strfieldcpy(dev.result, temp3);
252                                 continue;
253                         }
254
255                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
256                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
257                                 if (attr != NULL)
258                                         if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
259                                                 dbg_parse("creation of partition nodes requested");
260                                                 dev.partitions = PARTITIONS_COUNT;
261                                         }
262                                 strfieldcpy(dev.name, temp3);
263                                 continue;
264                         }
265
266                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
267                                 strfieldcpy(dev.symlink, temp3);
268                                 continue;
269                         }
270
271                         if (strcasecmp(temp2, FIELD_OWNER) == 0) {
272                                 strfieldcpy(dev.owner, temp3);
273                                 continue;
274                         }
275
276                         if (strcasecmp(temp2, FIELD_GROUP) == 0) {
277                                 strfieldcpy(dev.group, temp3);
278                                 continue;
279                         }
280
281                         if (strcasecmp(temp2, FIELD_MODE) == 0) {
282                                 dev.mode = strtol(temp3, NULL, 8);
283                                 continue;
284                         }
285
286                         dbg("unknown type of field '%s'", temp2);
287                         goto error;
288                 }
289
290                 /* simple plausibility checks for given keys */
291                 if ((dev.sysfs_pair[0].file[0] == '\0') ^
292                     (dev.sysfs_pair[0].value[0] == '\0')) {
293                         dbg("inconsistency in " FIELD_SYSFS " key");
294                         goto error;
295                 }
296
297                 if ((dev.result[0] != '\0') && (program_given == 0)) {
298                         dbg(FIELD_RESULT " is only useful when "
299                             FIELD_PROGRAM " is called in any rule before");
300                         goto error;
301                 }
302
303                 dev.config_line = lineno;
304                 strfieldcpy(dev.config_file, filename);
305                 retval = add_config_dev(&dev);
306                 if (retval) {
307                         dbg("add_config_dev returned with error %d", retval);
308                         continue;
309 error:
310                         dbg("%s:%d:%d: parse error, rule skipped",
311                             filename, lineno, temp - line);
312                 }
313         }
314 exit:
315         fclose(fd);
316         return retval;
317 }
318
319 static int namedev_parse_permissions(char *filename)
320 {
321         char line[255];
322         char *temp;
323         char *temp2;
324         FILE *fd;
325         int retval = 0;
326         struct perm_device dev;
327
328         fd = fopen(filename, "r");
329         if (fd != NULL) {
330                 dbg("reading '%s' as permissions file", filename);
331         } else {
332                 dbg("can't open '%s' as permissions file", filename);
333                 return -ENODEV;
334         }
335
336         /* loop through the whole file */
337         while (1) {
338                 temp = fgets(line, sizeof(line), fd);
339                 if (temp == NULL)
340                         break;
341
342                 dbg_parse("read '%s'", temp);
343
344                 /* eat the whitespace at the beginning of the line */
345                 while (isspace(*temp))
346                         ++temp;
347
348                 /* empty line? */
349                 if ((*temp == '\0') || (*temp == '\n'))
350                         continue;
351
352                 /* see if this is a comment */
353                 if (*temp == COMMENT_CHARACTER)
354                         continue;
355
356                 memset(&dev, 0x00, sizeof(dev));
357
358                 /* parse the line */
359                 temp2 = strsep(&temp, ":");
360                 if (!temp2) {
361                         dbg("cannot parse line '%s'", line);
362                         continue;
363                 }
364                 strfieldcpy(dev.name, temp2);
365
366                 temp2 = strsep(&temp, ":");
367                 if (!temp2) {
368                         dbg("cannot parse line '%s'", line);
369                         continue;
370                 }
371                 strfieldcpy(dev.owner, temp2);
372
373                 temp2 = strsep(&temp, ":");
374                 if (!temp2) {
375                         dbg("cannot parse line '%s'", line);
376                         continue;
377                 }
378                 strfieldcpy(dev.group, temp2);
379
380                 if (!temp) {
381                         dbg("cannot parse line '%s'", line);
382                         continue;
383                 }
384                 dev.mode = strtol(temp, NULL, 8);
385
386                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
387                           dev.name, dev.owner, dev.group, dev.mode);
388
389                 retval = add_perm_dev(&dev);
390                 if (retval) {
391                         dbg("add_perm_dev returned with error %d", retval);
392                         goto exit;
393                 }
394         }
395
396 exit:
397         fclose(fd);
398         return retval;
399 }
400
401 struct files {
402         struct list_head list;
403         char name[NAME_SIZE];
404 };
405
406 /* sort files in lexical order */
407 static int file_list_insert(char *filename)
408 {
409         struct files *loop_file;
410         struct files *new_file;
411
412         list_for_each_entry(loop_file, &file_list, list) {
413                 if (strcmp(loop_file->name, filename) > 0) {
414                         break;
415                 }
416         }
417
418         new_file = malloc(sizeof(struct files));
419         if (new_file == NULL) {
420                 dbg("error malloc");
421                 return -ENOMEM;
422         }
423
424         strfieldcpy(new_file->name, filename);
425         list_add_tail(&new_file->list, &loop_file->list);
426         return 0;
427 }
428
429 /* calls function for file or every file found in directory */
430 static int call_foreach_file(int parser (char *f) , char *filename, char *extension)
431 {
432         struct dirent *ent;
433         DIR *dir;
434         char *ext;
435         char file[NAME_SIZE];
436         struct stat stats;
437         struct files *loop_file;
438         struct files *tmp_file;
439
440         /* look if we have a plain file or a directory to scan */
441         stat(filename, &stats);
442         if ((stats.st_mode & S_IFMT) != S_IFDIR)
443                 return parser(filename);
444
445         /* sort matching filename into list */
446         dbg("open config as directory '%s'", filename);
447         dir = opendir(filename);
448         while (1) {
449                 ent = readdir(dir);
450                 if (ent == NULL || ent->d_name[0] == '\0')
451                         break;
452
453                 dbg("found file '%s'", ent->d_name);
454                 ext = strrchr(ent->d_name, '.');
455                 if (ext == NULL)
456                         continue;
457
458                 if (strcmp(ext, extension) == 0) {
459                         dbg("put file in list '%s'", ent->d_name);
460                         file_list_insert(ent->d_name);
461                 }
462         }
463
464         /* parse every file in the list */
465         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
466                 strfieldcpy(file, filename);
467                 strfieldcat(file, loop_file->name);
468                 parser(file);
469                 list_del(&loop_file->list);
470                 free(loop_file);
471         }
472
473         closedir(dir);
474         return 0;
475 }
476
477 int namedev_init_rules()
478 {
479         return call_foreach_file(namedev_parse_rules, udev_rules_filename, RULEFILE_EXT);
480 }
481
482 int namedev_init_permissions()
483 {
484         return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, PERMFILE_EXT);
485 }