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