chiark / gitweb /
[PATCH] v013 release
[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 #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 <fcntl.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <errno.h>
37
38 #include "udev.h"
39 #include "namedev.h"
40
41 static int add_config_dev(struct config_device *new_dev)
42 {
43         struct config_device *tmp_dev;
44
45         tmp_dev = malloc(sizeof(*tmp_dev));
46         if (tmp_dev == NULL)
47                 return -ENOMEM;
48         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
49         list_add_tail(&tmp_dev->node, &config_device_list);
50         //dump_config_dev(tmp_dev);
51         return 0;
52 }
53
54 int get_pair(char **orig_string, char **left, char **right)
55 {
56         char *temp;
57         char *string = *orig_string;
58
59         if (!string)
60                 return -ENODEV;
61
62         /* eat any whitespace */
63         while (isspace(*string) || *string == ',')
64                 ++string;
65
66         /* split based on '=' */
67         temp = strsep(&string, "=");
68         *left = temp;
69         if (!string)
70                 return -ENODEV;
71
72         /* take the right side and strip off the '"' */
73         while (isspace(*string))
74                 ++string;
75         if (*string == '"')
76                 ++string;
77         else
78                 return -ENODEV;
79
80         temp = strsep(&string, "\"");
81         if (!string || *temp == '\0')
82                 return -ENODEV;
83         *right = temp;
84         *orig_string = string;
85         
86         return 0;
87 }
88
89 void dump_config_dev(struct config_device *dev)
90 {
91         /*FIXME dump all sysfs's */
92         dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
93                   "sysfs_file[0]='%s', sysfs_value[0]='%s', "
94                   "kernel='%s', program='%s', result='%s'",
95                   dev->name, dev->symlink, dev->bus, dev->place, dev->id,
96                   dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
97                   dev->kernel, dev->program, dev->result);
98 }
99
100 void dump_config_dev_list(void)
101 {
102         struct config_device *dev;
103
104         list_for_each_entry(dev, &config_device_list, node)
105                 dump_config_dev(dev);
106 }
107
108 void dump_perm_dev(struct perm_device *dev)
109 {
110         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
111                   dev->name, dev->owner, dev->group, dev->mode);
112 }
113
114 void dump_perm_dev_list(void)
115 {
116         struct perm_device *dev;
117
118         list_for_each_entry(dev, &perm_device_list, node)
119                 dump_perm_dev(dev);
120 }
121
122
123 int namedev_init_rules(void)
124 {
125         char line[255];
126         int lineno;
127         char *temp;
128         char *temp2;
129         char *temp3;
130         FILE *fd;
131         int program_given = 0;
132         int retval = 0;
133         struct config_device dev;
134
135         fd = fopen(udev_rules_filename, "r");
136         if (fd != NULL) {
137                 dbg("reading '%s' as rules file", udev_rules_filename);
138         } else {
139                 dbg("can't open '%s' as a rules file", udev_rules_filename);
140                 return -ENODEV;
141         }
142
143         /* loop through the whole file */
144         lineno = 0;
145         while (1) {
146                 /* get a line */
147                 temp = fgets(line, sizeof(line), fd);
148                 if (temp == NULL)
149                         goto exit;
150                 lineno++;
151                 dbg_parse("read '%s'", temp);
152
153                 /* eat the whitespace */
154                 while (isspace(*temp))
155                         ++temp;
156
157                 /* empty line? */
158                 if ((*temp == '\0') || (*temp == '\n'))
159                         continue;
160
161                 /* see if this is a comment */
162                 if (*temp == COMMENT_CHARACTER)
163                         continue;
164
165                 memset(&dev, 0x00, sizeof(struct config_device));
166
167                 /* get all known keys */
168                 while (1) {
169                         retval = get_pair(&temp, &temp2, &temp3);
170                         if (retval)
171                                 break;
172
173                         if (strcasecmp(temp2, FIELD_BUS) == 0) {
174                                 strfieldcpy(dev.bus, temp3);
175                                 continue;
176                         }
177
178                         if (strcasecmp(temp2, FIELD_ID) == 0) {
179                                 strfieldcpy(dev.id, temp3);
180                                 continue;
181                         }
182
183                         if (strcasecmp(temp2, FIELD_PLACE) == 0) {
184                                 strfieldcpy(dev.place, temp3);
185                                 continue;
186                         }
187
188                         if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
189                                 struct sysfs_pair *pair = &dev.sysfs_pair[0];
190                                 int sysfs_pair_num = 0;
191
192                                 /* find first unused pair */
193                                 while (pair->file[0] != '\0') {
194                                         ++sysfs_pair_num;
195                                         if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
196                                                 pair = NULL;
197                                                 break;
198                                         }
199                                         ++pair;
200                                 }
201                                 if (pair) {
202                                         /* remove prepended 'SYSFS_' */
203                                         strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
204                                         strfieldcpy(pair->value, temp3);
205                                 }
206                                 continue;
207                         }
208
209                         if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
210                                 strfieldcpy(dev.kernel, temp3);
211                                 continue;
212                         }
213
214                         if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
215                                 program_given = 1;
216                                 strfieldcpy(dev.program, temp3);
217                                 continue;
218                         }
219
220                         if (strcasecmp(temp2, FIELD_RESULT) == 0) {
221                                 strfieldcpy(dev.result, temp3);
222                                 continue;
223                         }
224
225                         if (strcasecmp(temp2, FIELD_NAME) == 0) {
226                                 strfieldcpy(dev.name, temp3);
227                                 continue;
228                         }
229
230                         if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
231                                 strfieldcpy(dev.symlink, temp3);
232                                 continue;
233                         }
234
235                         dbg("unknown type of field '%s'", temp2);
236                         dbg("You might be using a rules file in the old format, please fix.");
237                         goto error;
238                 }
239
240                 /* simple plausibility check for given keys */
241                 if ((dev.sysfs_pair[0].file[0] == '\0') ^
242                     (dev.sysfs_pair[0].value[0] == '\0')) {
243                         dbg("inconsistency in SYSFS_ key");
244                         goto error;
245                 }
246
247                 if ((dev.result[0] != '\0') && (program_given == 0)) {
248                         dbg("RESULT is only useful when PROGRAM called in any rule before");
249                         goto error;
250                 }
251
252                 retval = add_config_dev(&dev);
253                 if (retval) {
254                         dbg("add_config_dev returned with error %d", retval);
255                         continue;
256 error:
257                         dbg("%s:%d:%d: parse error, rule skipped",
258                                   udev_rules_filename, lineno, temp - line);
259                 }
260         }
261 exit:
262         fclose(fd);
263         return retval;
264 }
265
266 int namedev_init_permissions(void)
267 {
268         char line[255];
269         char *temp;
270         char *temp2;
271         FILE *fd;
272         int retval = 0;
273         struct perm_device dev;
274
275         fd = fopen(udev_permissions_filename, "r");
276         if (fd != NULL) {
277                 dbg("reading '%s' as permissions file", udev_permissions_filename);
278         } else {
279                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
280                 return -ENODEV;
281         }
282
283         /* loop through the whole file */
284         while (1) {
285                 temp = fgets(line, sizeof(line), fd);
286                 if (temp == NULL)
287                         break;
288
289                 dbg_parse("read '%s'", temp);
290
291                 /* eat the whitespace at the beginning of the line */
292                 while (isspace(*temp))
293                         ++temp;
294
295                 /* empty line? */
296                 if ((*temp == '\0') || (*temp == '\n'))
297                         continue;
298
299                 /* see if this is a comment */
300                 if (*temp == COMMENT_CHARACTER)
301                         continue;
302
303                 memset(&dev, 0x00, sizeof(dev));
304
305                 /* parse the line */
306                 temp2 = strsep(&temp, ":");
307                 if (!temp2) {
308                         dbg("cannot parse line '%s'", line);
309                         continue;
310                 }
311                 strncpy(dev.name, temp2, sizeof(dev.name));
312
313                 temp2 = strsep(&temp, ":");
314                 if (!temp2) {
315                         dbg("cannot parse line '%s'", line);
316                         continue;
317                 }
318                 strncpy(dev.owner, temp2, sizeof(dev.owner));
319
320                 temp2 = strsep(&temp, ":");
321                 if (!temp2) {
322                         dbg("cannot parse line '%s'", line);
323                         continue;
324                 }
325                 strncpy(dev.group, temp2, sizeof(dev.owner));
326
327                 if (!temp) {
328                         dbg("cannot parse line: %s", line);
329                         continue;
330                 }
331                 dev.mode = strtol(temp, NULL, 8);
332
333                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
334                           dev.name, dev.owner, dev.group,
335                           dev.mode);
336                 retval = add_perm_dev(&dev);
337                 if (retval) {
338                         dbg("add_perm_dev returned with error %d", retval);
339                         goto exit;
340                 }
341         }
342
343 exit:
344         fclose(fd);
345         return retval;
346 }       
347
348