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