chiark / gitweb /
[PATCH] add support for the default_mode variable, as it is documented...
[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 int get_pair(char **orig_string, char **left, char **right)
40 {
41         char *temp;
42         char *string = *orig_string;
43
44         if (!string)
45                 return -ENODEV;
46
47         /* eat any whitespace */
48         while (isspace(*string))
49                 ++string;
50
51         /* split based on '=' */
52         temp = strsep(&string, "=");
53         *left = temp;
54         if (!string)
55                 return -ENODEV;
56
57         /* take the right side and strip off the '"' */
58         while (isspace(*string))
59                 ++string;
60         if (*string == '"')
61                 ++string;
62         else
63                 return -ENODEV;
64
65         temp = strsep(&string, "\"");
66         if (!string || *temp == '\0')
67                 return -ENODEV;
68         *right = temp;
69         *orig_string = string;
70         
71         return 0;
72 }
73
74 static int get_value(const char *left, char **orig_string, char **ret_string)
75 {
76         int retval;
77         char *left_string;
78
79         retval = get_pair(orig_string, &left_string, ret_string);
80         if (retval)
81                 return retval;
82         if (strcasecmp(left_string, left) != 0)
83                 return -ENODEV;
84         return 0;
85 }
86
87 void dump_config_dev(struct config_device *dev)
88 {
89         switch (dev->type) {
90         case KERNEL_NAME:
91                 dbg_parse("KERNEL name='%s' ,"
92                           "owner='%s', group='%s', mode=%#o",
93                           dev->name, dev->owner, dev->group, dev->mode);
94                 break;
95         case LABEL:
96                 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', "
97                           "owner='%s', group='%s', mode=%#o",
98                           dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
99                           dev->owner, dev->group, dev->mode);
100                 break;
101         case NUMBER:
102                 dbg_parse("NUMBER name='%s', bus='%s', id='%s', "
103                           "owner='%s', group='%s', mode=%#o",
104                           dev->name, dev->bus, dev->id,
105                           dev->owner, dev->group, dev->mode);
106                 break;
107         case TOPOLOGY:
108                 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', "
109                           "owner='%s', group='%s', mode=%#o",
110                           dev->name, dev->bus, dev->place,
111                           dev->owner, dev->group, dev->mode);
112                 break;
113         case REPLACE:
114                 dbg_parse("REPLACE name=%s, kernel_name=%s, "
115                           "owner='%s', group='%s', mode=%#o",
116                           dev->name, dev->kernel_name,
117                           dev->owner, dev->group, dev->mode);
118                 break;
119         case CALLOUT:
120                 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', "
121                           "owner='%s', group='%s', mode=%#o",
122                           dev->name, dev->bus, dev->exec_program, dev->id,
123                           dev->owner, dev->group, dev->mode);
124                 break;
125         default:
126                 dbg_parse("unknown type of method");
127         }
128 }
129
130 void dump_config_dev_list(void)
131 {
132         struct list_head *tmp;
133
134         list_for_each(tmp, &config_device_list) {
135                 struct config_device *dev = list_entry(tmp, struct config_device, node);
136                 dump_config_dev(dev);
137         }
138 }
139         
140 int namedev_init_rules(void)
141 {
142         char line[255];
143         int lineno;
144         char *temp;
145         char *temp2;
146         char *temp3;
147         FILE *fd;
148         int retval = 0;
149         struct config_device dev;
150
151         fd = fopen(udev_rules_filename, "r");
152         if (fd != NULL) {
153                 dbg("reading '%s' as rules file", udev_rules_filename);
154         } else {
155                 dbg("can't open '%s' as a rules file", udev_rules_filename);
156                 return -ENODEV;
157         }
158
159         /* loop through the whole file */
160         lineno = 0;
161         while (1) {
162                 /* get a line */
163                 temp = fgets(line, sizeof(line), fd);
164                 if (temp == NULL)
165                         goto exit;
166                 lineno++;
167
168                 dbg_parse("read '%s'", temp);
169
170                 /* eat the whitespace at the beginning of the line */
171                 while (isspace(*temp))
172                         ++temp;
173
174                 /* empty line? */
175                 if (*temp == 0x00)
176                         continue;
177
178                 /* see if this is a comment */
179                 if (*temp == COMMENT_CHARACTER)
180                         continue;
181
182                 memset(&dev, 0x00, sizeof(struct config_device));
183
184                 /* parse the line */
185                 temp2 = strsep(&temp, ",");
186                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
187                         /* label type */
188                         dev.type = LABEL;
189
190                         /* BUS="bus" */
191                         retval = get_value("BUS", &temp, &temp3);
192                         if (retval)
193                                 break;
194                         strfieldcpy(dev.bus, temp3);
195
196                         /* file="value" */
197                         temp2 = strsep(&temp, ",");
198                         retval = get_pair(&temp, &temp2, &temp3);
199                         if (retval)
200                                 break;
201                         strfieldcpy(dev.sysfs_file, temp2);
202                         strfieldcpy(dev.sysfs_value, temp3);
203
204                         /* NAME="new_name" */
205                         temp2 = strsep(&temp, ",");
206                         retval = get_value("NAME", &temp, &temp3);
207                         if (retval)
208                                 break;
209                         strfieldcpy(dev.name, temp3);
210
211                         dbg_parse("LABEL name='%s', bus='%s', "
212                                   "sysfs_file='%s', sysfs_value='%s'",
213                                   dev.name, dev.bus, dev.sysfs_file,
214                                   dev.sysfs_value);
215                 }
216
217                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
218                         /* number type */
219                         dev.type = NUMBER;
220
221                         /* BUS="bus" */
222                         retval = get_value("BUS", &temp, &temp3);
223                         if (retval)
224                                 break;
225                         strfieldcpy(dev.bus, temp3);
226
227                         /* ID="id" */
228                         temp2 = strsep(&temp, ",");
229                         retval = get_value("ID", &temp, &temp3);
230                         if (retval)
231                                 break;
232                         strfieldcpy(dev.id, temp3);
233
234                         /* NAME="new_name" */
235                         temp2 = strsep(&temp, ",");
236                         retval = get_value("NAME", &temp, &temp3);
237                         if (retval)
238                                 break;
239                         strfieldcpy(dev.name, temp3);
240
241                         dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
242                                   dev.name, dev.bus, dev.id);
243                 }
244
245                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
246                         /* number type */
247                         dev.type = TOPOLOGY;
248
249                         /* BUS="bus" */
250                         retval = get_value("BUS", &temp, &temp3);
251                         if (retval)
252                                 break;
253                         strfieldcpy(dev.bus, temp3);
254
255                         /* PLACE="place" */
256                         temp2 = strsep(&temp, ",");
257                         retval = get_value("PLACE", &temp, &temp3);
258                         if (retval)
259                                 break;
260                         strfieldcpy(dev.place, temp3);
261
262                         /* NAME="new_name" */
263                         temp2 = strsep(&temp, ",");
264                         retval = get_value("NAME", &temp, &temp3);
265                         if (retval)
266                                 break;
267                         strfieldcpy(dev.name, temp3);
268
269                         dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
270                                   dev.name, dev.bus, dev.place);
271                 }
272
273                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
274                         /* number type */
275                         dev.type = REPLACE;
276
277                         /* KERNEL="kernel_name" */
278                         retval = get_value("KERNEL", &temp, &temp3);
279                         if (retval)
280                                 break;
281                         strfieldcpy(dev.kernel_name, temp3);
282
283                         /* NAME="new_name" */
284                         temp2 = strsep(&temp, ",");
285                         retval = get_value("NAME", &temp, &temp3);
286                         if (retval)
287                                 break;
288                         strfieldcpy(dev.name, temp3);
289                         dbg_parse("REPLACE name='%s', kernel_name='%s'",
290                                   dev.name, dev.kernel_name);
291                 }
292                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
293                         /* number type */
294                         dev.type = CALLOUT;
295
296                         /* BUS="bus" */
297                         retval = get_value("BUS", &temp, &temp3);
298                         if (retval)
299                                 break;
300                         strfieldcpy(dev.bus, temp3);
301
302                         /* PROGRAM="executable" */
303                         temp2 = strsep(&temp, ",");
304                         retval = get_value("PROGRAM", &temp, &temp3);
305                         if (retval)
306                                 break;
307                         strfieldcpy(dev.exec_program, temp3);
308
309                         /* ID="id" */
310                         temp2 = strsep(&temp, ",");
311                         retval = get_value("ID", &temp, &temp3);
312                         if (retval)
313                                 break;
314                         strfieldcpy(dev.id, temp3);
315
316                         /* NAME="new_name" */
317                         temp2 = strsep(&temp, ",");
318                         retval = get_value("NAME", &temp, &temp3);
319                         if (retval)
320                                 break;
321                         strfieldcpy(dev.name, temp3);
322                         dbg_parse("CALLOUT name='%s', program='%s'",
323                                   dev.name, dev.exec_program);
324                 }
325
326                 retval = add_config_dev(&dev);
327                 if (retval) {
328                         dbg("add_config_dev returned with error %d", retval);
329                         goto exit;
330                 }
331         }
332         dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_rules_filename,
333                   lineno, temp - line, temp);
334 exit:
335         fclose(fd);
336         return retval;
337 }       
338
339
340 int namedev_init_permissions(void)
341 {
342         char line[255];
343         char *temp;
344         char *temp2;
345         FILE *fd;
346         int retval = 0;
347         struct config_device dev;
348
349         fd = fopen(udev_permissions_filename, "r");
350         if (fd != NULL) {
351                 dbg("reading '%s' as permissions file", udev_permissions_filename);
352         } else {
353                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
354                 return -ENODEV;
355         }
356
357         /* loop through the whole file */
358         while (1) {
359                 temp = fgets(line, sizeof(line), fd);
360                 if (temp == NULL)
361                         break;
362
363                 dbg_parse("read '%s'", temp);
364
365                 /* eat the whitespace at the beginning of the line */
366                 while (isspace(*temp))
367                         ++temp;
368
369                 /* empty line? */
370                 if (*temp == 0x00)
371                         continue;
372
373                 /* see if this is a comment */
374                 if (*temp == COMMENT_CHARACTER)
375                         continue;
376
377                 memset(&dev, 0x00, sizeof(dev));
378
379                 /* parse the line */
380                 temp2 = strsep(&temp, ":");
381                 if (!temp2) {
382                         dbg("cannot parse line '%s'", line);
383                         continue;
384                 }
385                 strncpy(dev.name, temp2, sizeof(dev.name));
386
387                 temp2 = strsep(&temp, ":");
388                 if (!temp2) {
389                         dbg("cannot parse line '%s'", line);
390                         continue;
391                 }
392                 strncpy(dev.owner, temp2, sizeof(dev.owner));
393
394                 temp2 = strsep(&temp, ":");
395                 if (!temp2) {
396                         dbg("cannot parse line '%s'", line);
397                         continue;
398                 }
399                 strncpy(dev.group, temp2, sizeof(dev.owner));
400
401                 if (!temp) {
402                         dbg("cannot parse line: %s", line);
403                         continue;
404                 }
405                 dev.mode = strtol(temp, NULL, 8);
406
407                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
408                           dev.name, dev.owner, dev.group,
409                           dev.mode);
410                 retval = add_config_dev(&dev);
411                 if (retval) {
412                         dbg("add_config_dev returned with error %d", retval);
413                         goto exit;
414                 }
415         }
416
417 exit:
418         fclose(fd);
419         return retval;
420 }       
421
422