chiark / gitweb /
[PATCH] add a question/answer about automounting usb devices to the FAQ.
[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 <ctype.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <errno.h>
38
39 #include "udev.h"
40 #include "udev_lib.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         char *buf;
157         size_t bufsize;
158         size_t cur;
159         size_t count;
160         int program_given = 0;
161         int retval = 0;
162         struct config_device dev;
163
164         if (file_map(filename, &buf, &bufsize) == 0) {
165                 dbg("reading '%s' as rules file", filename);
166         } else {
167                 dbg("can't open '%s' as rules file", filename);
168                 return -1;
169         }
170
171         /* loop through the whole file */
172         cur = 0;
173         lineno = 0;
174         while (1) {
175                 count = buf_get_line(buf, bufsize, cur);
176
177                 strncpy(line, buf + cur, count);
178                 line[count] = '\0';
179                 temp = line;
180                 lineno++;
181
182                 cur += count+1;
183                 if (cur > bufsize)
184                         break;
185
186                 dbg_parse("read '%s'", temp);
187
188                 /* eat the whitespace */
189                 while (isspace(*temp))
190                         ++temp;
191
192                 /* empty line? */
193                 if ((*temp == '\0') || (*temp == '\n'))
194                         continue;
195
196                 /* see if this is a comment */
197                 if (*temp == COMMENT_CHARACTER)
198                         continue;
199
200                 memset(&dev, 0x00, sizeof(struct config_device));
201
202                 /* get all known keys */
203                 while (1) {
204                         retval = parse_get_pair(&temp, &temp2, &temp3);
205                         if (retval)
206                                 break;
207
208                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
209                                 strfieldcpy(dev.bus, temp3);
210                                 continue;
211                         }
212
213                         if (strcasecmp(temp2, FIELD_ID) == 0) {
214                                 strfieldcpy(dev.id, temp3);
215                                 continue;
216                         }
217
218                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
219                                 strfieldcpy(dev.place, temp3);
220                                 continue;
221                         }
222
223                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
224                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
225                                 int sysfs_pair_num = 0;
226
227                                 /* find first unused pair */
228                                 while (pair->file[0] != '\0') {
229                                         ++sysfs_pair_num;
230                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
231                                                 pair = NULL;
232                                                 break;
233                                         }
234                                         ++pair;
235                                 }
236                                 if (pair) {
237                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
238                                         if (attr == NULL) {
239                                                 dbg("error parsing " FIELD_SYSFS " attribute");
240                                                 continue;
241                                         }
242                                         strfieldcpy(pair->file, attr);
243                                         strfieldcpy(pair->value, temp3);
244                                 }
245                                 continue;
246                         }
247
248                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
249                                 strfieldcpy(dev.kernel, temp3);
250                                 continue;
251                         }
252
253                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
254                                 program_given = 1;
255                                 strfieldcpy(dev.program, temp3);
256                                 continue;
257                         }
258
259                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
260                                 strfieldcpy(dev.result, temp3);
261                                 continue;
262                         }
263
264                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
265                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
266                                 if (attr != NULL)
267                                         if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
268                                                 dbg_parse("creation of partition nodes requested");
269                                                 dev.partitions = PARTITIONS_COUNT;
270                                         }
271                                 strfieldcpy(dev.name, temp3);
272                                 continue;
273                         }
274
275                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
276                                 strfieldcpy(dev.symlink, temp3);
277                                 continue;
278                         }
279
280                         if (strcasecmp(temp2, FIELD_OWNER) == 0) {
281                                 strfieldcpy(dev.owner, temp3);
282                                 continue;
283                         }
284
285                         if (strcasecmp(temp2, FIELD_GROUP) == 0) {
286                                 strfieldcpy(dev.group, temp3);
287                                 continue;
288                         }
289
290                         if (strcasecmp(temp2, FIELD_MODE) == 0) {
291                                 dev.mode = strtol(temp3, NULL, 8);
292                                 continue;
293                         }
294
295                         dbg("unknown type of field '%s'", temp2);
296                         goto error;
297                 }
298
299                 /* simple plausibility checks for given keys */
300                 if ((dev.sysfs_pair[0].file[0] == '\0') ^
301                     (dev.sysfs_pair[0].value[0] == '\0')) {
302                         dbg("inconsistency in " FIELD_SYSFS " key");
303                         goto error;
304                 }
305
306                 if ((dev.result[0] != '\0') && (program_given == 0)) {
307                         dbg(FIELD_RESULT " is only useful when "
308                             FIELD_PROGRAM " is called in any rule before");
309                         goto error;
310                 }
311
312                 dev.config_line = lineno;
313                 strfieldcpy(dev.config_file, filename);
314                 retval = add_config_dev(&dev);
315                 if (retval) {
316                         dbg("add_config_dev returned with error %d", retval);
317                         continue;
318 error:
319                         dbg("%s:%d:%d: parse error, rule skipped",
320                             filename, lineno, temp - line);
321                 }
322         }
323
324         file_unmap(buf, bufsize);
325         return retval;
326 }
327
328 static int namedev_parse_permissions(char *filename)
329 {
330         char line[255];
331         char *temp;
332         char *temp2;
333         char *buf;
334         size_t bufsize;
335         size_t cur;
336         size_t count;
337         int retval = 0;
338         struct perm_device dev;
339
340         if (file_map(filename, &buf, &bufsize) == 0) {
341                 dbg("reading '%s' as permissions file", filename);
342         } else {
343                 dbg("can't open '%s' as permissions file", filename);
344                 return -1;
345         }
346
347         /* loop through the whole file */
348         cur = 0;
349         while (1) {
350                 count = buf_get_line(buf, bufsize, cur);
351
352                 strncpy(line, buf + cur, count);
353                 line[count] = '\0';
354                 temp = line;
355
356                 cur += count+1;
357                 if (cur > bufsize)
358                         break;
359
360                 dbg_parse("read '%s'", temp);
361
362                 /* eat the whitespace at the beginning of the line */
363                 while (isspace(*temp))
364                         ++temp;
365
366                 /* empty line? */
367                 if ((*temp == '\0') || (*temp == '\n'))
368                         continue;
369
370                 /* see if this is a comment */
371                 if (*temp == COMMENT_CHARACTER)
372                         continue;
373
374                 memset(&dev, 0x00, sizeof(dev));
375
376                 /* parse the line */
377                 temp2 = strsep(&temp, ":");
378                 if (!temp2) {
379                         dbg("cannot parse line '%s'", line);
380                         continue;
381                 }
382                 strfieldcpy(dev.name, temp2);
383
384                 temp2 = strsep(&temp, ":");
385                 if (!temp2) {
386                         dbg("cannot parse line '%s'", line);
387                         continue;
388                 }
389                 strfieldcpy(dev.owner, temp2);
390
391                 temp2 = strsep(&temp, ":");
392                 if (!temp2) {
393                         dbg("cannot parse line '%s'", line);
394                         continue;
395                 }
396                 strfieldcpy(dev.group, temp2);
397
398                 if (!temp) {
399                         dbg("cannot parse line '%s'", line);
400                         continue;
401                 }
402                 dev.mode = strtol(temp, NULL, 8);
403
404                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
405                           dev.name, dev.owner, dev.group, dev.mode);
406
407                 retval = add_perm_dev(&dev);
408                 if (retval) {
409                         dbg("add_perm_dev returned with error %d", retval);
410                         goto exit;
411                 }
412         }
413
414 exit:
415         file_unmap(buf, bufsize);
416         return retval;
417 }
418
419 struct files {
420         struct list_head list;
421         char name[NAME_SIZE];
422 };
423
424 /* sort files in lexical order */
425 static int file_list_insert(char *filename)
426 {
427         struct files *loop_file;
428         struct files *new_file;
429
430         list_for_each_entry(loop_file, &file_list, list) {
431                 if (strcmp(loop_file->name, filename) > 0) {
432                         break;
433                 }
434         }
435
436         new_file = malloc(sizeof(struct files));
437         if (new_file == NULL) {
438                 dbg("error malloc");
439                 return -ENOMEM;
440         }
441
442         strfieldcpy(new_file->name, filename);
443         list_add_tail(&new_file->list, &loop_file->list);
444         return 0;
445 }
446
447 /* calls function for file or every file found in directory */
448 static int call_foreach_file(int parser (char *f) , char *filename, char *extension)
449 {
450         struct dirent *ent;
451         DIR *dir;
452         char *ext;
453         char file[NAME_SIZE];
454         struct stat stats;
455         struct files *loop_file;
456         struct files *tmp_file;
457
458         /* look if we have a plain file or a directory to scan */
459         stat(filename, &stats);
460         if ((stats.st_mode & S_IFMT) != S_IFDIR)
461                 return parser(filename);
462
463         /* sort matching filename into list */
464         dbg("open config as directory '%s'", filename);
465         dir = opendir(filename);
466         while (1) {
467                 ent = readdir(dir);
468                 if (ent == NULL || ent->d_name[0] == '\0')
469                         break;
470
471                 dbg("found file '%s'", ent->d_name);
472                 ext = strrchr(ent->d_name, '.');
473                 if (ext == NULL)
474                         continue;
475
476                 if (strcmp(ext, extension) == 0) {
477                         dbg("put file in list '%s'", ent->d_name);
478                         file_list_insert(ent->d_name);
479                 }
480         }
481
482         /* parse every file in the list */
483         list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
484                 strfieldcpy(file, filename);
485                 strfieldcat(file, loop_file->name);
486                 parser(file);
487                 list_del(&loop_file->list);
488                 free(loop_file);
489         }
490
491         closedir(dir);
492         return 0;
493 }
494
495 int namedev_init_rules()
496 {
497         return call_foreach_file(namedev_parse_rules, udev_rules_filename, RULEFILE_EXT);
498 }
499
500 int namedev_init_permissions()
501 {
502         return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, PERMFILE_EXT);
503 }