chiark / gitweb /
[PATCH] change devfs disk name rule from 'disk' to 'disc'
[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'", dev->name);
92                 break;
93         case LABEL:
94                 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s'",
95                           dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value);
96                 break;
97         case NUMBER:
98                 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
99                           dev->name, dev->bus, dev->id);
100                 break;
101         case TOPOLOGY:
102                 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
103                           dev->name, dev->bus, dev->place);
104                 break;
105         case REPLACE:
106                 dbg_parse("REPLACE name=%s, kernel_name=%s",
107                           dev->name, dev->kernel_name);
108                 break;
109         case CALLOUT:
110                 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
111                           dev->name, dev->bus, dev->exec_program, dev->id);
112                 break;
113         default:
114                 dbg_parse("unknown type of method");
115         }
116 }
117
118 void dump_config_dev_list(void)
119 {
120         struct list_head *tmp;
121
122         list_for_each(tmp, &config_device_list) {
123                 struct config_device *dev = list_entry(tmp, struct config_device, node);
124                 dump_config_dev(dev);
125         }
126 }
127
128 void dump_perm_dev(struct perm_device *dev)
129 {
130         dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
131                   dev->name, dev->owner, dev->group, dev->mode);
132 }
133
134 void dump_perm_dev_list(void)
135 {
136         struct list_head *tmp;
137
138         list_for_each(tmp, &perm_device_list) {
139                 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
140                 dump_perm_dev(dev);
141         }
142 }
143
144
145 int namedev_init_rules(void)
146 {
147         char line[255];
148         int lineno;
149         char *temp;
150         char *temp2;
151         char *temp3;
152         FILE *fd;
153         int retval = 0;
154         struct config_device dev;
155
156         fd = fopen(udev_rules_filename, "r");
157         if (fd != NULL) {
158                 dbg("reading '%s' as rules file", udev_rules_filename);
159         } else {
160                 dbg("can't open '%s' as a rules file", udev_rules_filename);
161                 return -ENODEV;
162         }
163
164         /* loop through the whole file */
165         lineno = 0;
166         while (1) {
167                 /* get a line */
168                 temp = fgets(line, sizeof(line), fd);
169                 if (temp == NULL)
170                         goto exit;
171                 lineno++;
172
173                 dbg_parse("read '%s'", temp);
174
175                 /* eat the whitespace at the beginning of the line */
176                 while (isspace(*temp))
177                         ++temp;
178
179                 /* empty line? */
180                 if (*temp == 0x00)
181                         continue;
182
183                 /* see if this is a comment */
184                 if (*temp == COMMENT_CHARACTER)
185                         continue;
186
187                 memset(&dev, 0x00, sizeof(struct config_device));
188
189                 /* parse the line */
190                 temp2 = strsep(&temp, ",");
191                 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
192                         /* label type */
193                         dev.type = LABEL;
194
195                         /* BUS="bus" */
196                         retval = get_value("BUS", &temp, &temp3);
197                         if (retval)
198                                 break;
199                         strfieldcpy(dev.bus, temp3);
200
201                         /* file="value" */
202                         temp2 = strsep(&temp, ",");
203                         retval = get_pair(&temp, &temp2, &temp3);
204                         if (retval)
205                                 break;
206                         strfieldcpy(dev.sysfs_file, temp2);
207                         strfieldcpy(dev.sysfs_value, temp3);
208
209                         /* NAME="new_name" */
210                         temp2 = strsep(&temp, ",");
211                         retval = get_value("NAME", &temp, &temp3);
212                         if (retval)
213                                 break;
214                         strfieldcpy(dev.name, temp3);
215
216                         /* SYMLINK="name" */
217                         temp2 = strsep(&temp, ",");
218                         retval = get_value("SYMLINK", &temp, &temp3);
219                         if (retval == 0)
220                                 strfieldcpy(dev.symlink, temp3);
221
222                         dbg_parse("LABEL name='%s', bus='%s', "
223                                   "sysfs_file='%s', sysfs_value='%s', symlink='%s'",
224                                   dev.name, dev.bus, dev.sysfs_file,
225                                   dev.sysfs_value, dev.symlink);
226                 }
227
228                 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
229                         /* number type */
230                         dev.type = NUMBER;
231
232                         /* BUS="bus" */
233                         retval = get_value("BUS", &temp, &temp3);
234                         if (retval)
235                                 break;
236                         strfieldcpy(dev.bus, temp3);
237
238                         /* ID="id" */
239                         temp2 = strsep(&temp, ",");
240                         retval = get_value("ID", &temp, &temp3);
241                         if (retval)
242                                 break;
243                         strfieldcpy(dev.id, temp3);
244
245                         /* NAME="new_name" */
246                         temp2 = strsep(&temp, ",");
247                         retval = get_value("NAME", &temp, &temp3);
248                         if (retval)
249                                 break;
250                         strfieldcpy(dev.name, temp3);
251
252                         /* SYMLINK="name" */
253                         temp2 = strsep(&temp, ",");
254                         retval = get_value("SYMLINK", &temp, &temp3);
255                         if (retval == 0)
256                                 strfieldcpy(dev.symlink, temp3);
257
258                         dbg_parse("NUMBER name='%s', bus='%s', id='%s', symlink='%s'",
259                                   dev.name, dev.bus, dev.id, dev.symlink);
260                 }
261
262                 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
263                         /* number type */
264                         dev.type = TOPOLOGY;
265
266                         /* BUS="bus" */
267                         retval = get_value("BUS", &temp, &temp3);
268                         if (retval)
269                                 break;
270                         strfieldcpy(dev.bus, temp3);
271
272                         /* PLACE="place" */
273                         temp2 = strsep(&temp, ",");
274                         retval = get_value("PLACE", &temp, &temp3);
275                         if (retval)
276                                 break;
277                         strfieldcpy(dev.place, temp3);
278
279                         /* NAME="new_name" */
280                         temp2 = strsep(&temp, ",");
281                         retval = get_value("NAME", &temp, &temp3);
282                         if (retval)
283                                 break;
284                         strfieldcpy(dev.name, temp3);
285
286                         /* SYMLINK="name" */
287                         temp2 = strsep(&temp, ",");
288                         retval = get_value("SYMLINK", &temp, &temp3);
289                         if (retval == 0)
290                                 strfieldcpy(dev.symlink, temp3);
291
292                         dbg_parse("TOPOLOGY name='%s', bus='%s', "
293                                   "place='%s', symlink='%s'",
294                                   dev.name, dev.bus, dev.place, dev.symlink);
295                 }
296
297                 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
298                         /* number type */
299                         dev.type = REPLACE;
300
301                         /* KERNEL="kernel_name" */
302                         retval = get_value("KERNEL", &temp, &temp3);
303                         if (retval)
304                                 break;
305                         strfieldcpy(dev.kernel_name, temp3);
306
307                         /* NAME="new_name" */
308                         temp2 = strsep(&temp, ",");
309                         retval = get_value("NAME", &temp, &temp3);
310                         if (retval)
311                                 break;
312                         strfieldcpy(dev.name, temp3);
313
314                         /* SYMLINK="name" */
315                         temp2 = strsep(&temp, ",");
316                         retval = get_value("SYMLINK", &temp, &temp3);
317                         if (retval == 0)
318                                 strfieldcpy(dev.symlink, temp3);
319
320                         dbg_parse("REPLACE name='%s', kernel_name='%s', symlink='%s'",
321                                   dev.name, dev.kernel_name, dev.symlink);
322                 }
323
324                 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
325                         /* number type */
326                         dev.type = CALLOUT;
327
328                         /* BUS="bus" */
329                         retval = get_value("BUS", &temp, &temp3);
330                         if (retval)
331                                 break;
332                         strfieldcpy(dev.bus, temp3);
333
334                         /* PROGRAM="executable" */
335                         temp2 = strsep(&temp, ",");
336                         retval = get_value("PROGRAM", &temp, &temp3);
337                         if (retval)
338                                 break;
339                         strfieldcpy(dev.exec_program, temp3);
340
341                         /* ID="id" */
342                         temp2 = strsep(&temp, ",");
343                         retval = get_value("ID", &temp, &temp3);
344                         if (retval)
345                                 break;
346                         strfieldcpy(dev.id, temp3);
347
348                         /* NAME="new_name" */
349                         temp2 = strsep(&temp, ",");
350                         retval = get_value("NAME", &temp, &temp3);
351                         if (retval)
352                                 break;
353                         strfieldcpy(dev.name, temp3);
354
355                         /* SYMLINK="name" */
356                         temp2 = strsep(&temp, ",");
357                         retval = get_value("SYMLINK", &temp, &temp3);
358                         if (retval == 0)
359                                 strfieldcpy(dev.symlink, temp3);
360
361                         dbg_parse("CALLOUT name='%s', bus='%s', program='%s', "
362                                   "id='%s', symlink='%s'",
363                                   dev.name, dev.bus, dev.exec_program,
364                                   dev.id, dev.symlink);
365                 }
366
367                 retval = add_config_dev(&dev);
368                 if (retval) {
369                         dbg("add_config_dev returned with error %d", retval);
370                         goto exit;
371                 }
372         }
373         dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_rules_filename,
374                   lineno, temp - line, temp);
375 exit:
376         fclose(fd);
377         return retval;
378 }       
379
380
381 int namedev_init_permissions(void)
382 {
383         char line[255];
384         char *temp;
385         char *temp2;
386         FILE *fd;
387         int retval = 0;
388         struct perm_device dev;
389
390         fd = fopen(udev_permissions_filename, "r");
391         if (fd != NULL) {
392                 dbg("reading '%s' as permissions file", udev_permissions_filename);
393         } else {
394                 dbg("can't open '%s' as permissions file", udev_permissions_filename);
395                 return -ENODEV;
396         }
397
398         /* loop through the whole file */
399         while (1) {
400                 temp = fgets(line, sizeof(line), fd);
401                 if (temp == NULL)
402                         break;
403
404                 dbg_parse("read '%s'", temp);
405
406                 /* eat the whitespace at the beginning of the line */
407                 while (isspace(*temp))
408                         ++temp;
409
410                 /* empty line? */
411                 if (*temp == 0x00)
412                         continue;
413
414                 /* see if this is a comment */
415                 if (*temp == COMMENT_CHARACTER)
416                         continue;
417
418                 memset(&dev, 0x00, sizeof(dev));
419
420                 /* parse the line */
421                 temp2 = strsep(&temp, ":");
422                 if (!temp2) {
423                         dbg("cannot parse line '%s'", line);
424                         continue;
425                 }
426                 strncpy(dev.name, temp2, sizeof(dev.name));
427
428                 temp2 = strsep(&temp, ":");
429                 if (!temp2) {
430                         dbg("cannot parse line '%s'", line);
431                         continue;
432                 }
433                 strncpy(dev.owner, temp2, sizeof(dev.owner));
434
435                 temp2 = strsep(&temp, ":");
436                 if (!temp2) {
437                         dbg("cannot parse line '%s'", line);
438                         continue;
439                 }
440                 strncpy(dev.group, temp2, sizeof(dev.owner));
441
442                 if (!temp) {
443                         dbg("cannot parse line: %s", line);
444                         continue;
445                 }
446                 dev.mode = strtol(temp, NULL, 8);
447
448                 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
449                           dev.name, dev.owner, dev.group,
450                           dev.mode);
451                 retval = add_perm_dev(&dev);
452                 if (retval) {
453                         dbg("add_perm_dev returned with error %d", retval);
454                         goto exit;
455                 }
456         }
457
458 exit:
459         fclose(fd);
460         return retval;
461 }       
462
463