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