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