chiark / gitweb /
5b8c5ab2d6989a32d8648b6ce048128116d8f144
[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 <errno.h>
37
38 #include "udev.h"
39 #include "udev_utils.h"
40 #include "logging.h"
41 #include "namedev.h"
42
43
44 static int add_config_dev(struct config_device *new_dev)
45 {
46         struct config_device *tmp_dev;
47
48         tmp_dev = malloc(sizeof(*tmp_dev));
49         if (tmp_dev == NULL)
50                 return -ENOMEM;
51         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
52         list_add_tail(&tmp_dev->node, &config_device_list);
53         //dump_config_dev(tmp_dev);
54         return 0;
55 }
56
57 void dump_config_dev(struct config_device *dev)
58 {
59         dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
60                   "sysfs_file[0]='%s', sysfs_value[0]='%s', "
61                   "kernel='%s', program='%s', result='%s'"
62                   "owner='%s', group='%s', mode=%#o",
63                   dev->name, dev->symlink, dev->bus, dev->place, dev->id,
64                   dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
65                   dev->kernel, dev->program, dev->result,
66                   dev->owner, dev->group, dev->mode);
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 static int add_perm_dev(struct perm_device *new_dev)
78 {
79         struct perm_device *dev;
80         struct perm_device *tmp_dev;
81
82         /* if we already have that entry, just update the values */
83         list_for_each_entry(dev, &perm_device_list, node) {
84                 if (strcmp(new_dev->name, dev->name) != 0)
85                         continue;
86
87                 /* don't overwrite values from earlier entries */
88                 if (dev->mode == 0000)
89                         dev->mode = new_dev->mode;
90                 if (dev->owner[0] == '\0')
91                         strfieldcpy(dev->owner, new_dev->owner);
92                 if (dev->owner[0] == '\0')
93                         strfieldcpy(dev->group, new_dev->group);
94
95                 return 0;
96         }
97
98         /* not found, add new structure to the perm list */
99         tmp_dev = malloc(sizeof(*tmp_dev));
100         if (!tmp_dev)
101                 return -ENOMEM;
102
103         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
104         list_add_tail(&tmp_dev->node, &perm_device_list);
105         /* dump_perm_dev(tmp_dev); */
106
107         return 0;
108 }
109
110 void dump_perm_dev(struct perm_device *dev)
111 {
112         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
113                   dev->name, dev->owner, dev->group, dev->mode);
114 }
115
116 void dump_perm_dev_list(void)
117 {
118         struct perm_device *dev;
119
120         list_for_each_entry(dev, &perm_device_list, node)
121                 dump_perm_dev(dev);
122 }
123
124 /* extract possible KEY{attr} */
125 static char *get_key_attribute(char *str)
126 {
127         char *pos;
128         char *attr;
129
130         attr = strchr(str, '{');
131         if (attr != NULL) {
132                 attr++;
133                 pos = strchr(attr, '}');
134                 if (pos == NULL) {
135                         dbg("missing closing brace for format");
136                         return NULL;
137                 }
138                 pos[0] = '\0';
139                 dbg("attribute='%s'", attr);
140                 return attr;
141         }
142
143         return NULL;
144 }
145
146 static int namedev_parse_rules(const char *filename, void *data)
147 {
148         char line[LINE_SIZE];
149         char *bufline;
150         int lineno;
151         char *temp;
152         char *temp2;
153         char *temp3;
154         char *attr;
155         char *buf;
156         size_t bufsize;
157         size_t cur;
158         size_t count;
159         int program_given = 0;
160         int valid;
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 (cur < bufsize) {
175                 count = buf_get_line(buf, bufsize, cur);
176                 bufline = &buf[cur];
177                 cur += count+1;
178                 lineno++;
179
180                 if (count >= LINE_SIZE) {
181                         info("line too long, rule skipped %s, line %d",
182                              filename, lineno);
183                         continue;
184                 }
185
186                 /* eat the whitespace */
187                 while ((count > 0) && isspace(bufline[0])) {
188                         bufline++;
189                         count--;
190                 }
191                 if (count == 0)
192                         continue;
193
194                 /* see if this is a comment */
195                 if (bufline[0] == COMMENT_CHARACTER)
196                         continue;
197
198                 strncpy(line, bufline, count);
199                 line[count] = '\0';
200                 dbg_parse("read '%s'", line);
201
202                 /* get all known keys */
203                 memset(&dev, 0x00, sizeof(struct config_device));
204                 temp = line;
205                 valid = 0;
206
207                 while (1) {
208                         retval = parse_get_pair(&temp, &temp2, &temp3);
209                         if (retval)
210                                 break;
211
212                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
213                                 strfieldcpy(dev.bus, temp3);
214                                 valid = 1;
215                                 continue;
216                         }
217
218                         if (strcasecmp(temp2, FIELD_ID) == 0) {
219                                 strfieldcpy(dev.id, temp3);
220                                 valid = 1;
221                                 continue;
222                         }
223
224                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
225                                 strfieldcpy(dev.place, temp3);
226                                 valid = 1;
227                                 continue;
228                         }
229
230                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
231                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
232                                 int sysfs_pair_num = 0;
233
234                                 /* find first unused pair */
235                                 while (pair->file[0] != '\0') {
236                                         ++sysfs_pair_num;
237                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
238                                                 pair = NULL;
239                                                 break;
240                                         }
241                                         ++pair;
242                                 }
243                                 if (pair) {
244                                         attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
245                                         if (attr == NULL) {
246                                                 dbg("error parsing " FIELD_SYSFS " attribute");
247                                                 continue;
248                                         }
249                                         strfieldcpy(pair->file, attr);
250                                         strfieldcpy(pair->value, temp3);
251                                         valid = 1;
252                                 }
253                                 continue;
254                         }
255
256                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
257                                 strfieldcpy(dev.kernel, temp3);
258                                 valid = 1;
259                                 continue;
260                         }
261
262                         if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
263                                 strfieldcpy(dev.subsystem, temp3);
264                                 valid = 1;
265                                 continue;
266                         }
267
268                         if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
269                                 strfieldcpy(dev.driver, temp3);
270                                 valid = 1;
271                                 continue;
272                         }
273
274                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
275                                 program_given = 1;
276                                 strfieldcpy(dev.program, temp3);
277                                 valid = 1;
278                                 continue;
279                         }
280
281                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
282                                 strfieldcpy(dev.result, temp3);
283                                 valid = 1;
284                                 continue;
285                         }
286
287                         if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
288                                 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
289                                 if (attr != NULL) {
290                                         if (strstr(attr, ATTR_PARTITIONS) != NULL) {
291                                                 dbg_parse("creation of partition nodes requested");
292                                                 dev.partitions = PARTITIONS_COUNT;
293                                         }
294                                         if (strstr(attr, ATTR_IGNORE_REMOVE) != NULL) {
295                                                 dbg_parse("remove event should be ignored");
296                                                 dev.ignore_remove = 1;
297                                         }
298                                 }
299                                 strfieldcpy(dev.name, temp3);
300                                 valid = 1;
301                                 continue;
302                         }
303
304                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
305                                 strfieldcpy(dev.symlink, temp3);
306                                 valid = 1;
307                                 continue;
308                         }
309
310                         if (strcasecmp(temp2, FIELD_OWNER) == 0) {
311                                 strfieldcpy(dev.owner, temp3);
312                                 valid = 1;
313                                 continue;
314                         }
315
316                         if (strcasecmp(temp2, FIELD_GROUP) == 0) {
317                                 strfieldcpy(dev.group, temp3);
318                                 valid = 1;
319                                 continue;
320                         }
321
322                         if (strcasecmp(temp2, FIELD_MODE) == 0) {
323                                 dev.mode = strtol(temp3, NULL, 8);
324                                 valid = 1;
325                                 continue;
326                         }
327
328                         dbg("unknown type of field '%s'", temp2);
329                         goto error;
330                 }
331
332                 /* skip line if not any valid key was found */
333                 if (!valid)
334                         goto error;
335
336                 /* simple plausibility checks for given keys */
337                 if ((dev.sysfs_pair[0].file[0] == '\0') ^
338                     (dev.sysfs_pair[0].value[0] == '\0')) {
339                         info("inconsistency in " FIELD_SYSFS " key");
340                         goto error;
341                 }
342
343                 if ((dev.result[0] != '\0') && (program_given == 0)) {
344                         info(FIELD_RESULT " is only useful when "
345                              FIELD_PROGRAM " is called in any rule before");
346                         goto error;
347                 }
348
349                 dev.config_line = lineno;
350                 strfieldcpy(dev.config_file, filename);
351                 retval = add_config_dev(&dev);
352                 if (retval) {
353                         dbg("add_config_dev returned with error %d", retval);
354                         continue;
355 error:
356                         info("parse error %s, line %d:%d, rule skipped",
357                              filename, lineno, temp - line);
358                 }
359         }
360
361         file_unmap(buf, bufsize);
362         return retval;
363 }
364
365 static int namedev_parse_permissions(const char *filename, void *data)
366 {
367         char line[LINE_SIZE];
368         char *bufline;
369         char *temp;
370         char *temp2;
371         char *buf;
372         size_t bufsize;
373         size_t cur;
374         size_t count;
375         int retval = 0;
376         struct perm_device dev;
377         int lineno;
378
379         if (file_map(filename, &buf, &bufsize) == 0) {
380                 dbg("reading '%s' as permissions file", filename);
381         } else {
382                 dbg("can't open '%s' as permissions file", filename);
383                 return -1;
384         }
385
386         /* loop through the whole file */
387         cur = 0;
388         lineno = 0;
389         while (cur < bufsize) {
390                 count = buf_get_line(buf, bufsize, cur);
391                 bufline = &buf[cur];
392                 cur += count+1;
393                 lineno++;
394
395                 if (count >= LINE_SIZE) {
396                         info("line too long, rule skipped %s, line %d",
397                              filename, lineno);
398                         continue;
399                 }
400
401                 /* eat the whitespace */
402                 while ((count > 0) && isspace(bufline[0])) {
403                         bufline++;
404                         count--;
405                 }
406                 if (count == 0)
407                         continue;
408
409                 /* see if this is a comment */
410                 if (bufline[0] == COMMENT_CHARACTER)
411                         continue;
412
413                 strncpy(line, bufline, count);
414                 line[count] = '\0';
415                 dbg_parse("read '%s'", line);
416
417                 /* parse the line */
418                 memset(&dev, 0x00, sizeof(struct perm_device));
419                 temp = line;
420
421                 temp2 = strsep(&temp, ":");
422                 if (!temp2) {
423                         dbg("cannot parse line '%s'", line);
424                         continue;
425                 }
426                 strfieldcpy(dev.name, temp2);
427
428                 temp2 = strsep(&temp, ":");
429                 if (!temp2) {
430                         dbg("cannot parse line '%s'", line);
431                         continue;
432                 }
433                 strfieldcpy(dev.owner, temp2);
434
435                 temp2 = strsep(&temp, ":");
436                 if (!temp2) {
437                         dbg("cannot parse line '%s'", line);
438                         continue;
439                 }
440                 strfieldcpy(dev.group, temp2);
441
442                 if (!temp) {
443                         dbg("cannot parse line '%s'", line);
444                         continue;
445                 }
446                 dev.mode = strtol(temp, NULL, 8);
447
448                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
449                           dev.name, dev.owner, dev.group, dev.mode);
450
451                 retval = add_perm_dev(&dev);
452                 if (retval) {
453                         dbg("add_perm_dev returned with error %d", retval);
454                         goto exit;
455                 }
456         }
457
458 exit:
459         file_unmap(buf, bufsize);
460         return retval;
461 }
462
463 int namedev_init_rules(void)
464 {
465         struct stat stats;
466
467         stat(udev_rules_filename, &stats);
468         if ((stats.st_mode & S_IFMT) != S_IFDIR)
469                 return namedev_parse_rules(udev_rules_filename, NULL);
470         else
471                 return call_foreach_file(namedev_parse_rules, udev_rules_filename,
472                                          RULEFILE_SUFFIX, NULL);
473 }
474
475 int namedev_init_permissions(void)
476 {
477         struct stat stats;
478
479         stat(udev_permissions_filename, &stats);
480         if ((stats.st_mode & S_IFMT) != S_IFDIR)
481                 return namedev_parse_permissions(udev_permissions_filename, NULL);
482         else
483                 return call_foreach_file(namedev_parse_permissions, udev_permissions_filename,
484                                          PERMFILE_SUFFIX, NULL);
485 }