chiark / gitweb /
[PATCH] replace list_for_each with list_for_each_entry, saving a few lines of code.
[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 config_device *dev;
121
122         list_for_each_entry(dev, &config_device_list, node)
123                 dump_config_dev(dev);
124 }
125
126 void dump_perm_dev(struct perm_device *dev)
127 {
128         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
129                   dev->name, dev->owner, dev->group, dev->mode);
130 }
131
132 void dump_perm_dev_list(void)
133 {
134         struct perm_device *dev;
135
136         list_for_each_entry(dev, &perm_device_list, node)
137                 dump_perm_dev(dev);
138 }
139
140
141 int namedev_init_rules(void)
142 {
143         char line[255];
144         int lineno;
145         char *temp;
146         char *temp2;
147         char *temp3;
148         FILE *fd;
149         int retval = 0;
150         struct config_device dev;
151
152         fd = fopen(udev_rules_filename, "r");
153         if (fd != NULL) {
154                 dbg("reading '%s' as rules file", udev_rules_filename);
155         } else {
156                 dbg("can't open '%s' as a rules file", udev_rules_filename);
157                 return -ENODEV;
158         }
159
160         /* loop through the whole file */
161         lineno = 0;
162         while (1) {
163                 /* get a line */
164                 temp = fgets(line, sizeof(line), fd);
165                 if (temp == NULL)
166                         goto exit;
167                 lineno++;
168                 dbg_parse("read '%s'", temp);
169
170                 /* eat the whitespace */
171                 while (isspace(*temp))
172                         ++temp;
173
174                 /* empty line? */
175                 if ((*temp == '\0') || (*temp == '\n'))
176                         continue;
177
178                 /* see if this is a comment */
179                 if (*temp == COMMENT_CHARACTER)
180                         continue;
181
182                 memset(&dev, 0x00, sizeof(struct config_device));
183
184                 /* get the method */
185                 temp2 = strsep(&temp, ",");
186
187                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
188                         dev.type = LABEL;
189                         goto keys;
190                 }
191
192                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
193                         dev.type = NUMBER;
194                         goto keys;
195                 }
196
197                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
198                         dev.type = TOPOLOGY;
199                         goto keys;
200                 }
201
202                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
203                         dev.type = REPLACE;
204                         goto keys;
205                 }
206
207                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
208                         dev.type = CALLOUT;
209                         goto keys;
210                 }
211
212                 dbg_parse("unknown type of method '%s'", temp2);
213                 goto error;
214 keys:
215                 /* get all known keys */
216                 while (1) {
217                         retval = get_pair(&temp, &temp2, &temp3);
218                         if (retval)
219                                 break;
220
221                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
222                                 strfieldcpy(dev.bus, temp3);
223                                 continue;
224                         }
225
226                         if (strcasecmp(temp2, FIELD_ID) == 0) {
227                                 strfieldcpy(dev.id, temp3);
228                                 continue;
229                         }
230
231                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
232                                 strfieldcpy(dev.place, temp3);
233                                 continue;
234                         }
235
236                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
237                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
238                                 int sysfs_pair_num = 0;
239
240                                 /* find first unused pair */
241                                 while (pair->file[0] != '\0') {
242                                         ++sysfs_pair_num;
243                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
244                                                 pair = NULL;
245                                                 break;
246                                         }
247                                         ++pair;
248                                 }
249                                 if (pair) {
250                                         /* remove prepended 'SYSFS_' */
251                                         strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
252                                         strfieldcpy(pair->value, temp3);
253                                 }
254                                 continue;
255                         }
256
257                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
258                                 strfieldcpy(dev.kernel_name, temp3);
259                                 continue;
260                         }
261
262                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
263                                 strfieldcpy(dev.exec_program, temp3);
264                                 continue;
265                         }
266
267                         if (strcasecmp(temp2, FIELD_NAME) == 0) {
268                                 strfieldcpy(dev.name, temp3);
269                                 continue;
270                         }
271
272                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
273                                 strfieldcpy(dev.symlink, temp3);
274                                 continue;
275                         }
276
277                         dbg_parse("unknown type of field '%s'", temp2);
278                 }
279
280                 /* check presence of keys according to method type */
281                 switch (dev.type) {
282                 case LABEL:
283                         dbg_parse(TYPE_LABEL " name='%s', bus='%s', "
284                                   "sysfs_file[0]='%s', sysfs_value[0]='%s', symlink='%s'",
285                                   dev.name, dev.bus, dev.sysfs_pair[0].file,
286                                   dev.sysfs_pair[0].value, dev.symlink);
287                         if ((*dev.name == '\0') ||
288                             (*dev.sysfs_pair[0].file == '\0') ||
289                             (*dev.sysfs_pair[0].value == '\0'))
290                                 goto error;
291                         break;
292                 case NUMBER:
293                         dbg_parse(TYPE_NUMBER " name='%s', bus='%s', id='%s', symlink='%s'",
294                                   dev.name, dev.bus, dev.id, dev.symlink);
295                         if ((*dev.name == '\0') ||
296                             (*dev.bus == '\0') ||
297                             (*dev.id == '\0'))
298                                 goto error;
299                         break;
300                 case TOPOLOGY:
301                         dbg_parse(TYPE_TOPOLOGY " name='%s', bus='%s', "
302                                   "place='%s', symlink='%s'",
303                                   dev.name, dev.bus, dev.place, dev.symlink);
304                         if ((*dev.name == '\0') ||
305                             (*dev.bus == '\0') ||
306                             (*dev.place == '\0'))
307                                 goto error;
308                         break;
309                 case REPLACE:
310                         dbg_parse(TYPE_REPLACE " name='%s', kernel_name='%s', symlink='%s'",
311                                   dev.name, dev.kernel_name, dev.symlink);
312                         if ((*dev.name == '\0') ||
313                             (*dev.kernel_name == '\0'))
314                                 goto error;
315                         break;
316                 case CALLOUT:
317                         dbg_parse(TYPE_CALLOUT " name='%s', bus='%s', program='%s', "
318                                   "id='%s', symlink='%s'",
319                                   dev.name, dev.bus, dev.exec_program,
320                                   dev.id, dev.symlink);
321                         if ((*dev.name == '\0') ||
322                             (*dev.id == '\0') ||
323                             (*dev.exec_program == '\0'))
324                                 goto error;
325                         break;
326                 default:
327                         dbg_parse("unknown type of method");
328                         goto error;
329                 }
330
331                 retval = add_config_dev(&dev);
332                 if (retval) {
333                         dbg("add_config_dev returned with error %d", retval);
334                         continue;
335                 }
336         }
337 error:
338         dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
339                   lineno, temp - line);
340 exit:
341         fclose(fd);
342         return retval;
343 }
344
345 int namedev_init_permissions(void)
346 {
347         char line[255];
348         char *temp;
349         char *temp2;
350         FILE *fd;
351         int retval = 0;
352         struct perm_device dev;
353
354         fd = fopen(udev_permissions_filename, "r");
355         if (fd != NULL) {
356                 dbg("reading '%s' as permissions file", udev_permissions_filename);
357         } else {
358                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
359                 return -ENODEV;
360         }
361
362         /* loop through the whole file */
363         while (1) {
364                 temp = fgets(line, sizeof(line), fd);
365                 if (temp == NULL)
366                         break;
367
368                 dbg_parse("read '%s'", temp);
369
370                 /* eat the whitespace at the beginning of the line */
371                 while (isspace(*temp))
372                         ++temp;
373
374                 /* empty line? */
375                 if ((*temp == '\0') || (*temp == '\n'))
376                         continue;
377
378                 /* see if this is a comment */
379                 if (*temp == COMMENT_CHARACTER)
380                         continue;
381
382                 memset(&dev, 0x00, sizeof(dev));
383
384                 /* parse the line */
385                 temp2 = strsep(&temp, ":");
386                 if (!temp2) {
387                         dbg("cannot parse line '%s'", line);
388                         continue;
389                 }
390                 strncpy(dev.name, temp2, sizeof(dev.name));
391
392                 temp2 = strsep(&temp, ":");
393                 if (!temp2) {
394                         dbg("cannot parse line '%s'", line);
395                         continue;
396                 }
397                 strncpy(dev.owner, temp2, sizeof(dev.owner));
398
399                 temp2 = strsep(&temp, ":");
400                 if (!temp2) {
401                         dbg("cannot parse line '%s'", line);
402                         continue;
403                 }
404                 strncpy(dev.group, temp2, sizeof(dev.owner));
405
406                 if (!temp) {
407                         dbg("cannot parse line: %s", line);
408                         continue;
409                 }
410                 dev.mode = strtol(temp, NULL, 8);
411
412                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
413                           dev.name, dev.owner, dev.group,
414                           dev.mode);
415                 retval = add_perm_dev(&dev);
416                 if (retval) {
417                         dbg("add_perm_dev returned with error %d", retval);
418                         goto exit;
419                 }
420         }
421
422 exit:
423         fclose(fd);
424         return retval;
425 }       
426
427