chiark / gitweb /
[PATCH] fix udev-test.pl
[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         default:
114                 dbg_parse("unknown type of method");
115         }
116 }
117
118 void dump_config_dev_list(void)
119 {
120         struct list_head *tmp;
121
122         list_for_each(tmp, &config_device_list) {
123                 struct config_device *dev = list_entry(tmp, struct config_device, node);
124                 dump_config_dev(dev);
125         }
126 }
127
128 void dump_perm_dev(struct perm_device *dev)
129 {
130         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
131                   dev->name, dev->owner, dev->group, dev->mode);
132 }
133
134 void dump_perm_dev_list(void)
135 {
136         struct list_head *tmp;
137
138         list_for_each(tmp, &perm_device_list) {
139                 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
140                 dump_perm_dev(dev);
141         }
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 == 0x00)
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                 dbg_parse("unknown type of method '%s'", temp2);
217                 goto error;
218 keys:
219                 /* get all known keys */
220                 while (1) {
221                         retval = get_pair(&temp, &temp2, &temp3);
222                         if (retval)
223                                 break;
224
225                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
226                                 strfieldcpy(dev.bus, temp3);
227                                 continue;
228                         }
229
230                         if (strcasecmp(temp2, FIELD_ID) == 0) {
231                                 strfieldcpy(dev.id, temp3);
232                                 continue;
233                         }
234
235                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
236                                 strfieldcpy(dev.place, temp3);
237                                 continue;
238                         }
239
240                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
241                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
242                                 int sysfs_pair_num = 0;
243
244                                 /* find first unused pair */
245                                 while (pair->file[0] != '\0') {
246                                         ++sysfs_pair_num;
247                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
248                                                 pair = NULL;
249                                                 break;
250                                         }
251                                         ++pair;
252                                 }
253                                 if (pair) {
254                                         /* remove prepended 'SYSFS_' */
255                                         strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
256                                         strfieldcpy(pair->value, temp3);
257                                 }
258                                 continue;
259                         }
260
261                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
262                                 strfieldcpy(dev.kernel_name, temp3);
263                                 continue;
264                         }
265
266                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
267                                 strfieldcpy(dev.exec_program, temp3);
268                                 continue;
269                         }
270
271                         if (strcasecmp(temp2, FIELD_NAME) == 0) {
272                                 strfieldcpy(dev.name, temp3);
273                                 continue;
274                         }
275
276                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
277                                 strfieldcpy(dev.symlink, temp3);
278                                 continue;
279                         }
280
281                         dbg_parse("unknown type of field '%s'", temp2);
282                 }
283
284                 /* check presence of keys according to method type */
285                 switch (dev.type) {
286                 case LABEL:
287                         dbg_parse(TYPE_LABEL " name='%s', bus='%s', "
288                                   "sysfs_file[0]='%s', sysfs_value[0]='%s', symlink='%s'",
289                                   dev.name, dev.bus, dev.sysfs_pair[0].file,
290                                   dev.sysfs_pair[0].value, dev.symlink);
291                         if ((*dev.name == '\0') ||
292                             (*dev.bus == '\0') ||
293                             (*dev.sysfs_pair[0].file == '\0') ||
294                             (*dev.sysfs_pair[0].value == '\0'))
295                                 goto error;
296                         break;
297                 case NUMBER:
298                         dbg_parse(TYPE_NUMBER "name='%s', bus='%s', id='%s', symlink='%s'",
299                                   dev.name, dev.bus, dev.id, dev.symlink);
300                         if ((*dev.name == '\0') ||
301                             (*dev.bus == '\0') ||
302                             (*dev.id == '\0'))
303                                 goto error;
304                         break;
305                 case TOPOLOGY:
306                         dbg_parse(TYPE_TOPOLOGY "name='%s', bus='%s', "
307                                   "place='%s', symlink='%s'",
308                                   dev.name, dev.bus, dev.place, dev.symlink);
309                         if ((*dev.name == '\0') ||
310                             (*dev.bus == '\0') ||
311                             (*dev.place == '\0'))
312                                 goto error;
313                         break;
314                 case REPLACE:
315                         dbg_parse(TYPE_REPLACE "name='%s', kernel_name='%s', symlink='%s'",
316                                   dev.name, dev.kernel_name, dev.symlink);
317                         if ((*dev.name == '\0') ||
318                             (*dev.kernel_name == '\0'))
319                                 goto error;
320                         break;
321                 case CALLOUT:
322                         dbg_parse(TYPE_CALLOUT "name='%s', bus='%s', program='%s', "
323                                   "id='%s', symlink='%s'",
324                                   dev.name, dev.bus, dev.exec_program,
325                                   dev.id, dev.symlink);
326                         if ((*dev.name == '\0') ||
327                             (*dev.bus == '\0') ||
328                             (*dev.id == '\0') ||
329                             (*dev.exec_program == '\0'))
330                                 goto error;
331                         break;
332                 default:
333                         dbg_parse("unknown type of method");
334                         goto error;
335                 }
336
337                 retval = add_config_dev(&dev);
338                 if (retval) {
339                         dbg("add_config_dev returned with error %d", retval);
340                         continue;
341                 }
342         }
343 error:
344         dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
345                   lineno, temp - line);
346 exit:
347         fclose(fd);
348         return retval;
349 }
350
351 int namedev_init_permissions(void)
352 {
353         char line[255];
354         char *temp;
355         char *temp2;
356         FILE *fd;
357         int retval = 0;
358         struct perm_device dev;
359
360         fd = fopen(udev_permissions_filename, "r");
361         if (fd != NULL) {
362                 dbg("reading '%s' as permissions file", udev_permissions_filename);
363         } else {
364                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
365                 return -ENODEV;
366         }
367
368         /* loop through the whole file */
369         while (1) {
370                 temp = fgets(line, sizeof(line), fd);
371                 if (temp == NULL)
372                         break;
373
374                 dbg_parse("read '%s'", temp);
375
376                 /* eat the whitespace at the beginning of the line */
377                 while (isspace(*temp))
378                         ++temp;
379
380                 /* empty line? */
381                 if (*temp == 0x00)
382                         continue;
383
384                 /* see if this is a comment */
385                 if (*temp == COMMENT_CHARACTER)
386                         continue;
387
388                 memset(&dev, 0x00, sizeof(dev));
389
390                 /* parse the line */
391                 temp2 = strsep(&temp, ":");
392                 if (!temp2) {
393                         dbg("cannot parse line '%s'", line);
394                         continue;
395                 }
396                 strncpy(dev.name, temp2, sizeof(dev.name));
397
398                 temp2 = strsep(&temp, ":");
399                 if (!temp2) {
400                         dbg("cannot parse line '%s'", line);
401                         continue;
402                 }
403                 strncpy(dev.owner, temp2, sizeof(dev.owner));
404
405                 temp2 = strsep(&temp, ":");
406                 if (!temp2) {
407                         dbg("cannot parse line '%s'", line);
408                         continue;
409                 }
410                 strncpy(dev.group, temp2, sizeof(dev.owner));
411
412                 if (!temp) {
413                         dbg("cannot parse line: %s", line);
414                         continue;
415                 }
416                 dev.mode = strtol(temp, NULL, 8);
417
418                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
419                           dev.name, dev.owner, dev.group,
420                           dev.mode);
421                 retval = add_perm_dev(&dev);
422                 if (retval) {
423                         dbg("add_perm_dev returned with error %d", retval);
424                         goto exit;
425                 }
426         }
427
428 exit:
429         fclose(fd);
430         return retval;
431 }       
432
433