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