chiark / gitweb /
[PATCH] fix udevd exec
[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 "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 int get_pair(char **orig_string, char **left, char **right)
56 {
57         char *temp;
58         char *string = *orig_string;
59
60         if (!string)
61                 return -ENODEV;
62
63         /* eat any whitespace */
64         while (isspace(*string) || *string == ',')
65                 ++string;
66
67         /* split based on '=' */
68         temp = strsep(&string, "=");
69         *left = temp;
70         if (!string)
71                 return -ENODEV;
72
73         /* take the right side and strip off the '"' */
74         while (isspace(*string))
75                 ++string;
76         if (*string == '"')
77                 ++string;
78         else
79                 return -ENODEV;
80
81         temp = strsep(&string, "\"");
82         if (!string || *temp == '\0')
83                 return -ENODEV;
84         *right = temp;
85         *orig_string = string;
86         
87         return 0;
88 }
89
90 void dump_config_dev(struct config_device *dev)
91 {
92         /*FIXME dump all sysfs's */
93         dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
94                   "sysfs_file[0]='%s', sysfs_value[0]='%s', "
95                   "kernel='%s', program='%s', result='%s'",
96                   dev->name, dev->symlink, dev->bus, dev->place, dev->id,
97                   dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
98                   dev->kernel, dev->program, dev->result);
99 }
100
101 void dump_config_dev_list(void)
102 {
103         struct config_device *dev;
104
105         list_for_each_entry(dev, &config_device_list, node)
106                 dump_config_dev(dev);
107 }
108
109 void dump_perm_dev(struct perm_device *dev)
110 {
111         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
112                   dev->name, dev->owner, dev->group, dev->mode);
113 }
114
115 void dump_perm_dev_list(void)
116 {
117         struct perm_device *dev;
118
119         list_for_each_entry(dev, &perm_device_list, node)
120                 dump_perm_dev(dev);
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                 dev.config_line = lineno;
253                 retval = add_config_dev(&dev);
254                 if (retval) {
255                         dbg("add_config_dev returned with error %d", retval);
256                         continue;
257 error:
258                         dbg("%s:%d:%d: parse error, rule skipped",
259                                   udev_rules_filename, lineno, temp - line);
260                 }
261         }
262 exit:
263         fclose(fd);
264         return retval;
265 }
266
267 int namedev_init_permissions(void)
268 {
269         char line[255];
270         char *temp;
271         char *temp2;
272         FILE *fd;
273         int retval = 0;
274         struct perm_device dev;
275
276         fd = fopen(udev_permissions_filename, "r");
277         if (fd != NULL) {
278                 dbg("reading '%s' as permissions file", udev_permissions_filename);
279         } else {
280                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
281                 return -ENODEV;
282         }
283
284         /* loop through the whole file */
285         while (1) {
286                 temp = fgets(line, sizeof(line), fd);
287                 if (temp == NULL)
288                         break;
289
290                 dbg_parse("read '%s'", temp);
291
292                 /* eat the whitespace at the beginning of the line */
293                 while (isspace(*temp))
294                         ++temp;
295
296                 /* empty line? */
297                 if ((*temp == '\0') || (*temp == '\n'))
298                         continue;
299
300                 /* see if this is a comment */
301                 if (*temp == COMMENT_CHARACTER)
302                         continue;
303
304                 memset(&dev, 0x00, sizeof(dev));
305
306                 /* parse the line */
307                 temp2 = strsep(&temp, ":");
308                 if (!temp2) {
309                         dbg("cannot parse line '%s'", line);
310                         continue;
311                 }
312                 strncpy(dev.name, temp2, sizeof(dev.name));
313
314                 temp2 = strsep(&temp, ":");
315                 if (!temp2) {
316                         dbg("cannot parse line '%s'", line);
317                         continue;
318                 }
319                 strncpy(dev.owner, temp2, sizeof(dev.owner));
320
321                 temp2 = strsep(&temp, ":");
322                 if (!temp2) {
323                         dbg("cannot parse line '%s'", line);
324                         continue;
325                 }
326                 strncpy(dev.group, temp2, sizeof(dev.group));
327
328                 if (!temp) {
329                         dbg("cannot parse line: %s", line);
330                         continue;
331                 }
332                 dev.mode = strtol(temp, NULL, 8);
333
334                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
335                           dev.name, dev.owner, dev.group,
336                           dev.mode);
337                 retval = add_perm_dev(&dev);
338                 if (retval) {
339                         dbg("add_perm_dev returned with error %d", retval);
340                         goto exit;
341                 }
342         }
343
344 exit:
345         fclose(fd);
346         return retval;
347 }
348