chiark / gitweb /
[PATCH] add a script that tests the IGNORE rule
[elogind.git] / namedev_parse.c
1 /*
2  * namedev_parse.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 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 /* define this to enable parsing debugging */
25 /* #define DEBUG_PARSER */
26
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <errno.h>
35
36 #include "udev.h"
37 #include "namedev.h"
38
39 static int add_config_dev(struct config_device *new_dev)
40 {
41         struct config_device *tmp_dev;
42
43         tmp_dev = malloc(sizeof(*tmp_dev));
44         if (tmp_dev == NULL)
45                 return -ENOMEM;
46         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
47         list_add_tail(&tmp_dev->node, &config_device_list);
48         //dump_config_dev(tmp_dev);
49         return 0;
50 }
51
52 int get_pair(char **orig_string, char **left, char **right)
53 {
54         char *temp;
55         char *string = *orig_string;
56
57         if (!string)
58                 return -ENODEV;
59
60         /* eat any whitespace */
61         while (isspace(*string) || *string == ',')
62                 ++string;
63
64         /* split based on '=' */
65         temp = strsep(&string, "=");
66         *left = temp;
67         if (!string)
68                 return -ENODEV;
69
70         /* take the right side and strip off the '"' */
71         while (isspace(*string))
72                 ++string;
73         if (*string == '"')
74                 ++string;
75         else
76                 return -ENODEV;
77
78         temp = strsep(&string, "\"");
79         if (!string || *temp == '\0')
80                 return -ENODEV;
81         *right = temp;
82         *orig_string = string;
83         
84         return 0;
85 }
86
87 void dump_config_dev(struct config_device *dev)
88 {
89         switch (dev->type) {
90         case KERNEL_NAME:
91                 dbg_parse("KERNEL name='%s'", dev->name);
92                 break;
93         case LABEL:
94                 dbg_parse("LABEL name='%s', bus='%s', sysfs_file[0]='%s', sysfs_value[0]='%s'",
95                           dev->name, dev->bus, dev->sysfs_pair[0].file, dev->sysfs_pair[0].value);
96                 break;
97         case NUMBER:
98                 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
99                           dev->name, dev->bus, dev->id);
100                 break;
101         case TOPOLOGY:
102                 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
103                           dev->name, dev->bus, dev->place);
104                 break;
105         case REPLACE:
106                 dbg_parse("REPLACE name='%s', kernel_name='%s'",
107                           dev->name, dev->kernel_name);
108                 break;
109         case CALLOUT:
110                 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
111                           dev->name, dev->bus, dev->exec_program, dev->id);
112                 break;
113         case IGNORE:
114                 dbg_parse("IGNORE name='%s', kernel_name='%s'",
115                           dev->name, dev->kernel_name);
116                 break;
117         default:
118                 dbg_parse("unknown type of method");
119         }
120 }
121
122 void dump_config_dev_list(void)
123 {
124         struct config_device *dev;
125
126         list_for_each_entry(dev, &config_device_list, node)
127                 dump_config_dev(dev);
128 }
129
130 void dump_perm_dev(struct perm_device *dev)
131 {
132         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
133                   dev->name, dev->owner, dev->group, dev->mode);
134 }
135
136 void dump_perm_dev_list(void)
137 {
138         struct perm_device *dev;
139
140         list_for_each_entry(dev, &perm_device_list, node)
141                 dump_perm_dev(dev);
142 }
143
144
145 int namedev_init_rules(void)
146 {
147         char line[255];
148         int lineno;
149         char *temp;
150         char *temp2;
151         char *temp3;
152         FILE *fd;
153         int retval = 0;
154         struct config_device dev;
155
156         fd = fopen(udev_rules_filename, "r");
157         if (fd != NULL) {
158                 dbg("reading '%s' as rules file", udev_rules_filename);
159         } else {
160                 dbg("can't open '%s' as a rules file", udev_rules_filename);
161                 return -ENODEV;
162         }
163
164         /* loop through the whole file */
165         lineno = 0;
166         while (1) {
167                 /* get a line */
168                 temp = fgets(line, sizeof(line), fd);
169                 if (temp == NULL)
170                         goto exit;
171                 lineno++;
172                 dbg_parse("read '%s'", temp);
173
174                 /* eat the whitespace */
175                 while (isspace(*temp))
176                         ++temp;
177
178                 /* empty line? */
179                 if ((*temp == '\0') || (*temp == '\n'))
180                         continue;
181
182                 /* see if this is a comment */
183                 if (*temp == COMMENT_CHARACTER)
184                         continue;
185
186                 memset(&dev, 0x00, sizeof(struct config_device));
187
188                 /* get the method */
189                 temp2 = strsep(&temp, ",");
190
191                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
192                         dev.type = LABEL;
193                         goto keys;
194                 }
195
196                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
197                         dev.type = NUMBER;
198                         goto keys;
199                 }
200
201                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
202                         dev.type = TOPOLOGY;
203                         goto keys;
204                 }
205
206                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
207                         dev.type = REPLACE;
208                         goto keys;
209                 }
210
211                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
212                         dev.type = CALLOUT;
213                         goto keys;
214                 }
215
216                 if (strcasecmp(temp2, TYPE_IGNORE) == 0) {
217                         dev.type = IGNORE;
218                         goto keys;
219                 }
220
221                 dbg_parse("unknown type of method '%s'", temp2);
222                 goto error;
223 keys:
224                 /* get all known keys */
225                 while (1) {
226                         retval = get_pair(&temp, &temp2, &temp3);
227                         if (retval)
228                                 break;
229
230                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
231                                 strfieldcpy(dev.bus, temp3);
232                                 continue;
233                         }
234
235                         if (strcasecmp(temp2, FIELD_ID) == 0) {
236                                 strfieldcpy(dev.id, temp3);
237                                 continue;
238                         }
239
240                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
241                                 strfieldcpy(dev.place, temp3);
242                                 continue;
243                         }
244
245                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
246                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
247                                 int sysfs_pair_num = 0;
248
249                                 /* find first unused pair */
250                                 while (pair->file[0] != '\0') {
251                                         ++sysfs_pair_num;
252                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
253                                                 pair = NULL;
254                                                 break;
255                                         }
256                                         ++pair;
257                                 }
258                                 if (pair) {
259                                         /* remove prepended 'SYSFS_' */
260                                         strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
261                                         strfieldcpy(pair->value, temp3);
262                                 }
263                                 continue;
264                         }
265
266                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
267                                 strfieldcpy(dev.kernel_name, temp3);
268                                 continue;
269                         }
270
271                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
272                                 strfieldcpy(dev.exec_program, temp3);
273                                 continue;
274                         }
275
276                         if (strcasecmp(temp2, FIELD_NAME) == 0) {
277                                 strfieldcpy(dev.name, temp3);
278                                 continue;
279                         }
280
281                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
282                                 strfieldcpy(dev.symlink, temp3);
283                                 continue;
284                         }
285
286                         dbg_parse("unknown type of field '%s'", temp2);
287                 }
288
289                 /* check presence of keys according to method type */
290                 switch (dev.type) {
291                 case LABEL:
292                         dbg_parse(TYPE_LABEL " name='%s', bus='%s', "
293                                   "sysfs_file[0]='%s', sysfs_value[0]='%s', symlink='%s'",
294                                   dev.name, dev.bus, dev.sysfs_pair[0].file,
295                                   dev.sysfs_pair[0].value, dev.symlink);
296                         if ((*dev.name == '\0') ||
297                             (*dev.sysfs_pair[0].file == '\0') ||
298                             (*dev.sysfs_pair[0].value == '\0'))
299                                 goto error;
300                         break;
301                 case NUMBER:
302                         dbg_parse(TYPE_NUMBER " name='%s', bus='%s', id='%s', symlink='%s'",
303                                   dev.name, dev.bus, dev.id, dev.symlink);
304                         if ((*dev.name == '\0') ||
305                             (*dev.bus == '\0') ||
306                             (*dev.id == '\0'))
307                                 goto error;
308                         break;
309                 case TOPOLOGY:
310                         dbg_parse(TYPE_TOPOLOGY " name='%s', bus='%s', "
311                                   "place='%s', symlink='%s'",
312                                   dev.name, dev.bus, dev.place, dev.symlink);
313                         if ((*dev.name == '\0') ||
314                             (*dev.bus == '\0') ||
315                             (*dev.place == '\0'))
316                                 goto error;
317                         break;
318                 case REPLACE:
319                         dbg_parse(TYPE_REPLACE " name='%s', kernel_name='%s', symlink='%s'",
320                                   dev.name, dev.kernel_name, dev.symlink);
321                         if ((*dev.name == '\0') ||
322                             (*dev.kernel_name == '\0'))
323                                 goto error;
324                         break;
325                 case CALLOUT:
326                         dbg_parse(TYPE_CALLOUT " name='%s', bus='%s', program='%s', "
327                                   "id='%s', symlink='%s'",
328                                   dev.name, dev.bus, dev.exec_program,
329                                   dev.id, dev.symlink);
330                         if ((*dev.name == '\0') ||
331                             (*dev.id == '\0') ||
332                             (*dev.exec_program == '\0'))
333                                 goto error;
334                         break;
335                 case IGNORE:
336                         dbg_parse(TYPE_IGNORE "name='%s', kernel_name='%s'",
337                                   dev.name, dev.kernel_name);
338                         if ((*dev.kernel_name == '\0'))
339                                 goto error;
340                         break;
341                 default:
342                         dbg_parse("unknown type of method");
343                         goto error;
344                 }
345
346                 retval = add_config_dev(&dev);
347                 if (retval) {
348                         dbg("add_config_dev returned with error %d", retval);
349                         continue;
350                 }
351         }
352 error:
353         dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
354                   lineno, temp - line);
355 exit:
356         fclose(fd);
357         return retval;
358 }
359
360 int namedev_init_permissions(void)
361 {
362         char line[255];
363         char *temp;
364         char *temp2;
365         FILE *fd;
366         int retval = 0;
367         struct perm_device dev;
368
369         fd = fopen(udev_permissions_filename, "r");
370         if (fd != NULL) {
371                 dbg("reading '%s' as permissions file", udev_permissions_filename);
372         } else {
373                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
374                 return -ENODEV;
375         }
376
377         /* loop through the whole file */
378         while (1) {
379                 temp = fgets(line, sizeof(line), fd);
380                 if (temp == NULL)
381                         break;
382
383                 dbg_parse("read '%s'", temp);
384
385                 /* eat the whitespace at the beginning of the line */
386                 while (isspace(*temp))
387                         ++temp;
388
389                 /* empty line? */
390                 if ((*temp == '\0') || (*temp == '\n'))
391                         continue;
392
393                 /* see if this is a comment */
394                 if (*temp == COMMENT_CHARACTER)
395                         continue;
396
397                 memset(&dev, 0x00, sizeof(dev));
398
399                 /* parse the line */
400                 temp2 = strsep(&temp, ":");
401                 if (!temp2) {
402                         dbg("cannot parse line '%s'", line);
403                         continue;
404                 }
405                 strncpy(dev.name, temp2, sizeof(dev.name));
406
407                 temp2 = strsep(&temp, ":");
408                 if (!temp2) {
409                         dbg("cannot parse line '%s'", line);
410                         continue;
411                 }
412                 strncpy(dev.owner, temp2, sizeof(dev.owner));
413
414                 temp2 = strsep(&temp, ":");
415                 if (!temp2) {
416                         dbg("cannot parse line '%s'", line);
417                         continue;
418                 }
419                 strncpy(dev.group, temp2, sizeof(dev.owner));
420
421                 if (!temp) {
422                         dbg("cannot parse line: %s", line);
423                         continue;
424                 }
425                 dev.mode = strtol(temp, NULL, 8);
426
427                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
428                           dev.name, dev.owner, dev.group,
429                           dev.mode);
430                 retval = add_perm_dev(&dev);
431                 if (retval) {
432                         dbg("add_perm_dev returned with error %d", retval);
433                         goto exit;
434                 }
435         }
436
437 exit:
438         fclose(fd);
439         return retval;
440 }       
441
442