]> git.ipfire.org Git - thirdparty/systemd.git/blame - namedev_parse.c
[PATCH] a bug in linefeed removal
[thirdparty/systemd.git] / namedev_parse.c
CommitLineData
19feb351
GKH
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
e8baccca 39int get_pair(char **orig_string, char **left, char **right)
19feb351
GKH
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
74static 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
87void dump_config_dev(struct config_device *dev)
88{
89 switch (dev->type) {
90 case KERNEL_NAME:
61219c75 91 dbg_parse("KERNEL name='%s'", dev->name);
19feb351
GKH
92 break;
93 case LABEL:
61219c75
GKH
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);
19feb351
GKH
96 break;
97 case NUMBER:
61219c75
GKH
98 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
99 dev->name, dev->bus, dev->id);
19feb351
GKH
100 break;
101 case TOPOLOGY:
61219c75
GKH
102 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
103 dev->name, dev->bus, dev->place);
19feb351
GKH
104 break;
105 case REPLACE:
61219c75
GKH
106 dbg_parse("REPLACE name=%s, kernel_name=%s",
107 dev->name, dev->kernel_name);
19feb351
GKH
108 break;
109 case CALLOUT:
61219c75
GKH
110 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
111 dev->name, dev->bus, dev->exec_program, dev->id);
19feb351
GKH
112 break;
113 default:
114 dbg_parse("unknown type of method");
115 }
116}
117
118void 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}
61219c75
GKH
127
128void 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
134void 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
e8baccca 145int namedev_init_rules(void)
19feb351
GKH
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
e8baccca
GKH
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);
19feb351
GKH
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 dbg_parse("LABEL name='%s', bus='%s', "
217 "sysfs_file='%s', sysfs_value='%s'",
218 dev.name, dev.bus, dev.sysfs_file,
219 dev.sysfs_value);
220 }
221
222 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
223 /* number type */
224 dev.type = NUMBER;
225
226 /* BUS="bus" */
227 retval = get_value("BUS", &temp, &temp3);
228 if (retval)
229 break;
230 strfieldcpy(dev.bus, temp3);
231
232 /* ID="id" */
233 temp2 = strsep(&temp, ",");
234 retval = get_value("ID", &temp, &temp3);
235 if (retval)
236 break;
237 strfieldcpy(dev.id, temp3);
238
239 /* NAME="new_name" */
240 temp2 = strsep(&temp, ",");
241 retval = get_value("NAME", &temp, &temp3);
242 if (retval)
243 break;
244 strfieldcpy(dev.name, temp3);
245
246 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
247 dev.name, dev.bus, dev.id);
248 }
249
250 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
251 /* number type */
252 dev.type = TOPOLOGY;
253
254 /* BUS="bus" */
255 retval = get_value("BUS", &temp, &temp3);
256 if (retval)
257 break;
258 strfieldcpy(dev.bus, temp3);
259
260 /* PLACE="place" */
261 temp2 = strsep(&temp, ",");
262 retval = get_value("PLACE", &temp, &temp3);
263 if (retval)
264 break;
265 strfieldcpy(dev.place, temp3);
266
267 /* NAME="new_name" */
268 temp2 = strsep(&temp, ",");
269 retval = get_value("NAME", &temp, &temp3);
270 if (retval)
271 break;
272 strfieldcpy(dev.name, temp3);
273
274 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
275 dev.name, dev.bus, dev.place);
276 }
277
278 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
279 /* number type */
280 dev.type = REPLACE;
281
282 /* KERNEL="kernel_name" */
283 retval = get_value("KERNEL", &temp, &temp3);
284 if (retval)
285 break;
286 strfieldcpy(dev.kernel_name, temp3);
287
288 /* NAME="new_name" */
289 temp2 = strsep(&temp, ",");
290 retval = get_value("NAME", &temp, &temp3);
291 if (retval)
292 break;
293 strfieldcpy(dev.name, temp3);
294 dbg_parse("REPLACE name='%s', kernel_name='%s'",
295 dev.name, dev.kernel_name);
296 }
297 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
298 /* number type */
299 dev.type = CALLOUT;
300
301 /* BUS="bus" */
302 retval = get_value("BUS", &temp, &temp3);
303 if (retval)
304 break;
305 strfieldcpy(dev.bus, temp3);
306
307 /* PROGRAM="executable" */
308 temp2 = strsep(&temp, ",");
309 retval = get_value("PROGRAM", &temp, &temp3);
310 if (retval)
311 break;
312 strfieldcpy(dev.exec_program, temp3);
313
314 /* ID="id" */
315 temp2 = strsep(&temp, ",");
316 retval = get_value("ID", &temp, &temp3);
317 if (retval)
318 break;
319 strfieldcpy(dev.id, temp3);
320
321 /* NAME="new_name" */
322 temp2 = strsep(&temp, ",");
323 retval = get_value("NAME", &temp, &temp3);
324 if (retval)
325 break;
326 strfieldcpy(dev.name, temp3);
327 dbg_parse("CALLOUT name='%s', program='%s'",
328 dev.name, dev.exec_program);
329 }
330
331 retval = add_config_dev(&dev);
332 if (retval) {
333 dbg("add_config_dev returned with error %d", retval);
334 goto exit;
335 }
336 }
e8baccca 337 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_rules_filename,
19feb351
GKH
338 lineno, temp - line, temp);
339exit:
340 fclose(fd);
341 return retval;
342}
343
344
345int namedev_init_permissions(void)
346{
347 char line[255];
348 char *temp;
349 char *temp2;
350 FILE *fd;
351 int retval = 0;
61219c75 352 struct perm_device dev;
19feb351 353
3836a3c4 354 fd = fopen(udev_permissions_filename, "r");
e8baccca 355 if (fd != NULL) {
3836a3c4 356 dbg("reading '%s' as permissions file", udev_permissions_filename);
e8baccca 357 } else {
3836a3c4 358 dbg("can't open '%s' as permissions file", udev_permissions_filename);
19feb351
GKH
359 return -ENODEV;
360 }
361
362 /* loop through the whole file */
363 while (1) {
364 temp = fgets(line, sizeof(line), fd);
365 if (temp == NULL)
366 break;
367
368 dbg_parse("read '%s'", temp);
369
370 /* eat the whitespace at the beginning of the line */
371 while (isspace(*temp))
372 ++temp;
373
374 /* empty line? */
375 if (*temp == 0x00)
376 continue;
377
378 /* see if this is a comment */
379 if (*temp == COMMENT_CHARACTER)
380 continue;
381
382 memset(&dev, 0x00, sizeof(dev));
383
384 /* parse the line */
385 temp2 = strsep(&temp, ":");
386 if (!temp2) {
387 dbg("cannot parse line '%s'", line);
388 continue;
389 }
390 strncpy(dev.name, temp2, sizeof(dev.name));
391
392 temp2 = strsep(&temp, ":");
393 if (!temp2) {
394 dbg("cannot parse line '%s'", line);
395 continue;
396 }
397 strncpy(dev.owner, temp2, sizeof(dev.owner));
398
399 temp2 = strsep(&temp, ":");
400 if (!temp2) {
401 dbg("cannot parse line '%s'", line);
402 continue;
403 }
404 strncpy(dev.group, temp2, sizeof(dev.owner));
405
406 if (!temp) {
407 dbg("cannot parse line: %s", line);
408 continue;
409 }
410 dev.mode = strtol(temp, NULL, 8);
411
412 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
413 dev.name, dev.owner, dev.group,
414 dev.mode);
61219c75 415 retval = add_perm_dev(&dev);
19feb351
GKH
416 if (retval) {
417 dbg("add_config_dev returned with error %d", retval);
418 goto exit;
419 }
420 }
421
422exit:
423 fclose(fd);
424 return retval;
425}
426
427