chiark / gitweb /
a752b33050cb5b939ceba85cba674ea3cdb703ea
[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 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_config(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         dbg("opening '%s' to read as config", udev_config_filename);
152         fd = fopen(udev_config_filename, "r");
153         if (fd == NULL) {
154                 dbg("can't open '%s'", udev_config_filename);
155                 return -ENODEV;
156         }
157
158         /* loop through the whole file */
159         lineno = 0;
160         while (1) {
161                 /* get a line */
162                 temp = fgets(line, sizeof(line), fd);
163                 if (temp == NULL)
164                         goto exit;
165                 lineno++;
166
167                 dbg_parse("read '%s'", temp);
168
169                 /* eat the whitespace at the beginning of the line */
170                 while (isspace(*temp))
171                         ++temp;
172
173                 /* empty line? */
174                 if (*temp == 0x00)
175                         continue;
176
177                 /* see if this is a comment */
178                 if (*temp == COMMENT_CHARACTER)
179                         continue;
180
181                 memset(&dev, 0x00, sizeof(struct config_device));
182
183                 /* parse the line */
184                 temp2 = strsep(&temp, ",");
185                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
186                         /* label type */
187                         dev.type = LABEL;
188
189                         /* BUS="bus" */
190                         retval = get_value("BUS", &temp, &temp3);
191                         if (retval)
192                                 break;
193                         strfieldcpy(dev.bus, temp3);
194
195                         /* file="value" */
196                         temp2 = strsep(&temp, ",");
197                         retval = get_pair(&temp, &temp2, &temp3);
198                         if (retval)
199                                 break;
200                         strfieldcpy(dev.sysfs_file, temp2);
201                         strfieldcpy(dev.sysfs_value, temp3);
202
203                         /* NAME="new_name" */
204                         temp2 = strsep(&temp, ",");
205                         retval = get_value("NAME", &temp, &temp3);
206                         if (retval)
207                                 break;
208                         strfieldcpy(dev.name, temp3);
209
210                         dbg_parse("LABEL name='%s', bus='%s', "
211                                   "sysfs_file='%s', sysfs_value='%s'",
212                                   dev.name, dev.bus, dev.sysfs_file,
213                                   dev.sysfs_value);
214                 }
215
216                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
217                         /* number type */
218                         dev.type = NUMBER;
219
220                         /* BUS="bus" */
221                         retval = get_value("BUS", &temp, &temp3);
222                         if (retval)
223                                 break;
224                         strfieldcpy(dev.bus, temp3);
225
226                         /* ID="id" */
227                         temp2 = strsep(&temp, ",");
228                         retval = get_value("ID", &temp, &temp3);
229                         if (retval)
230                                 break;
231                         strfieldcpy(dev.id, temp3);
232
233                         /* NAME="new_name" */
234                         temp2 = strsep(&temp, ",");
235                         retval = get_value("NAME", &temp, &temp3);
236                         if (retval)
237                                 break;
238                         strfieldcpy(dev.name, temp3);
239
240                         dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
241                                   dev.name, dev.bus, dev.id);
242                 }
243
244                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
245                         /* number type */
246                         dev.type = TOPOLOGY;
247
248                         /* BUS="bus" */
249                         retval = get_value("BUS", &temp, &temp3);
250                         if (retval)
251                                 break;
252                         strfieldcpy(dev.bus, temp3);
253
254                         /* PLACE="place" */
255                         temp2 = strsep(&temp, ",");
256                         retval = get_value("PLACE", &temp, &temp3);
257                         if (retval)
258                                 break;
259                         strfieldcpy(dev.place, temp3);
260
261                         /* NAME="new_name" */
262                         temp2 = strsep(&temp, ",");
263                         retval = get_value("NAME", &temp, &temp3);
264                         if (retval)
265                                 break;
266                         strfieldcpy(dev.name, temp3);
267
268                         dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
269                                   dev.name, dev.bus, dev.place);
270                 }
271
272                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
273                         /* number type */
274                         dev.type = REPLACE;
275
276                         /* KERNEL="kernel_name" */
277                         retval = get_value("KERNEL", &temp, &temp3);
278                         if (retval)
279                                 break;
280                         strfieldcpy(dev.kernel_name, temp3);
281
282                         /* NAME="new_name" */
283                         temp2 = strsep(&temp, ",");
284                         retval = get_value("NAME", &temp, &temp3);
285                         if (retval)
286                                 break;
287                         strfieldcpy(dev.name, temp3);
288                         dbg_parse("REPLACE name='%s', kernel_name='%s'",
289                                   dev.name, dev.kernel_name);
290                 }
291                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
292                         /* number type */
293                         dev.type = CALLOUT;
294
295                         /* BUS="bus" */
296                         retval = get_value("BUS", &temp, &temp3);
297                         if (retval)
298                                 break;
299                         strfieldcpy(dev.bus, temp3);
300
301                         /* PROGRAM="executable" */
302                         temp2 = strsep(&temp, ",");
303                         retval = get_value("PROGRAM", &temp, &temp3);
304                         if (retval)
305                                 break;
306                         strfieldcpy(dev.exec_program, temp3);
307
308                         /* ID="id" */
309                         temp2 = strsep(&temp, ",");
310                         retval = get_value("ID", &temp, &temp3);
311                         if (retval)
312                                 break;
313                         strfieldcpy(dev.id, temp3);
314
315                         /* NAME="new_name" */
316                         temp2 = strsep(&temp, ",");
317                         retval = get_value("NAME", &temp, &temp3);
318                         if (retval)
319                                 break;
320                         strfieldcpy(dev.name, temp3);
321                         dbg_parse("CALLOUT name='%s', program='%s'",
322                                   dev.name, dev.exec_program);
323                 }
324
325                 retval = add_config_dev(&dev);
326                 if (retval) {
327                         dbg("add_config_dev returned with error %d", retval);
328                         goto exit;
329                 }
330         }
331         dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
332                   lineno, temp - line, temp);
333 exit:
334         fclose(fd);
335         return retval;
336 }       
337
338
339 int namedev_init_permissions(void)
340 {
341         char line[255];
342         char *temp;
343         char *temp2;
344         FILE *fd;
345         int retval = 0;
346         struct config_device dev;
347
348         dbg("opening '%s' to read as permissions config", udev_config_permission_filename);
349         fd = fopen(udev_config_permission_filename, "r");
350         if (fd == NULL) {
351                 dbg("can't open '%s'", udev_config_permission_filename);
352                 return -ENODEV;
353         }
354
355         /* loop through the whole file */
356         while (1) {
357                 temp = fgets(line, sizeof(line), fd);
358                 if (temp == NULL)
359                         break;
360
361                 dbg_parse("read '%s'", temp);
362
363                 /* eat the whitespace at the beginning of the line */
364                 while (isspace(*temp))
365                         ++temp;
366
367                 /* empty line? */
368                 if (*temp == 0x00)
369                         continue;
370
371                 /* see if this is a comment */
372                 if (*temp == COMMENT_CHARACTER)
373                         continue;
374
375                 memset(&dev, 0x00, sizeof(dev));
376
377                 /* parse the line */
378                 temp2 = strsep(&temp, ":");
379                 if (!temp2) {
380                         dbg("cannot parse line '%s'", line);
381                         continue;
382                 }
383                 strncpy(dev.name, temp2, sizeof(dev.name));
384
385                 temp2 = strsep(&temp, ":");
386                 if (!temp2) {
387                         dbg("cannot parse line '%s'", line);
388                         continue;
389                 }
390                 strncpy(dev.owner, temp2, sizeof(dev.owner));
391
392                 temp2 = strsep(&temp, ":");
393                 if (!temp2) {
394                         dbg("cannot parse line '%s'", line);
395                         continue;
396                 }
397                 strncpy(dev.group, temp2, sizeof(dev.owner));
398
399                 if (!temp) {
400                         dbg("cannot parse line: %s", line);
401                         continue;
402                 }
403                 dev.mode = strtol(temp, NULL, 8);
404
405                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
406                           dev.name, dev.owner, dev.group,
407                           dev.mode);
408                 retval = add_config_dev(&dev);
409                 if (retval) {
410                         dbg("add_config_dev returned with error %d", retval);
411                         goto exit;
412                 }
413         }
414
415 exit:
416         fclose(fd);
417         return retval;
418 }       
419
420