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