]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev_config.c
[PATCH] make dev.d call each directory in the directory chain of the device name...
[thirdparty/systemd.git] / udev_config.c
CommitLineData
e8baccca
GKH
1/*
2 * udev_config.c
3 *
4 * Userspace devfs
5 *
274812b5 6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
e8baccca
GKH
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 <stdlib.h>
28#include <string.h>
29#include <stdio.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <errno.h>
33#include <ctype.h>
34
c80da508 35#include "libsysfs/sysfs/libsysfs.h"
e8baccca 36#include "udev.h"
c81b35c0 37#include "udev_lib.h"
e8baccca 38#include "udev_version.h"
54988802 39#include "logging.h"
e8baccca 40#include "namedev.h"
e8baccca
GKH
41
42/* global variables */
43char sysfs_path[SYSFS_PATH_MAX];
e8baccca
GKH
44char udev_root[PATH_MAX];
45char udev_db_filename[PATH_MAX+NAME_MAX];
3836a3c4 46char udev_permissions_filename[PATH_MAX+NAME_MAX];
e8baccca
GKH
47char udev_rules_filename[PATH_MAX+NAME_MAX];
48char udev_config_filename[PATH_MAX+NAME_MAX];
765cbd97 49char default_mode_str[MODE_SIZE];
74c73ef9 50char default_owner_str[OWNER_SIZE];
51char default_group_str[GROUP_SIZE];
51a8bb2f 52int udev_log;
961e4784 53int udev_sleep;
bbbe503e 54int udev_dev_d;
e8baccca
GKH
55
56
51a8bb2f
GKH
57static int string_is_true(char *str)
58{
59 if (strcasecmp(str, "true") == 0)
60 return 1;
61 if (strcasecmp(str, "yes") == 0)
62 return 1;
63 return 0;
64}
65
e8baccca
GKH
66static void init_variables(void)
67{
3836a3c4
GKH
68 /* fill up the defaults.
69 * If any config values are specified, they will
70 * override these values. */
e8baccca 71 strfieldcpy(udev_root, UDEV_ROOT);
3836a3c4
GKH
72 strfieldcpy(udev_db_filename, UDEV_DB);
73 strfieldcpy(udev_config_filename, UDEV_CONFIG_FILE);
74 strfieldcpy(udev_rules_filename, UDEV_RULES_FILE);
75 strfieldcpy(udev_permissions_filename, UDEV_PERMISSION_FILE);
51a8bb2f 76 udev_log = string_is_true(UDEV_LOG_DEFAULT);
961e4784
GKH
77
78 udev_sleep = 1;
79 if (getenv("UDEV_NO_SLEEP") != NULL)
80 udev_sleep = 0;
bbbe503e
KS
81
82 udev_dev_d = 1;
83 if (getenv("UDEV_NO_DEVD") != NULL)
84 udev_dev_d = 0;
e8baccca
GKH
85}
86
87#define set_var(_name, _var) \
88 if (strcasecmp(variable, _name) == 0) { \
9b28a52a
KS
89 dbg_parse("%s='%s'", _name, value); \
90 strfieldcpy(_var, value);\
e8baccca
GKH
91 }
92
51a8bb2f
GKH
93#define set_bool(_name, _var) \
94 if (strcasecmp(variable, _name) == 0) { \
9b28a52a 95 dbg_parse("%s='%s'", _name, value); \
51a8bb2f
GKH
96 _var = string_is_true(value); \
97 }
98
274812b5
GKH
99int parse_get_pair(char **orig_string, char **left, char **right)
100{
101 char *temp;
102 char *string = *orig_string;
103
104 if (!string)
105 return -ENODEV;
106
107 /* eat any whitespace */
108 while (isspace(*string) || *string == ',')
109 ++string;
110
111 /* split based on '=' */
112 temp = strsep(&string, "=");
113 *left = temp;
114 if (!string)
115 return -ENODEV;
116
117 /* take the right side and strip off the '"' */
118 while (isspace(*string))
119 ++string;
120 if (*string == '"')
121 ++string;
122 else
123 return -ENODEV;
124
125 temp = strsep(&string, "\"");
126 if (!string || *temp == '\0')
127 return -ENODEV;
128 *right = temp;
129 *orig_string = string;
130
131 return 0;
132}
133
e8baccca
GKH
134static int parse_config_file(void)
135{
136 char line[255];
137 char *temp;
138 char *variable;
139 char *value;
c81b35c0
KS
140 char *buf;
141 size_t bufsize;
142 size_t cur;
143 size_t count;
144 int lineno;
e8baccca 145 int retval = 0;
c81b35c0
KS
146
147 if (file_map(udev_config_filename, &buf, &bufsize) == 0) {
e8baccca
GKH
148 dbg("reading '%s' as config file", udev_config_filename);
149 } else {
150 dbg("can't open '%s' as config file", udev_config_filename);
151 return -ENODEV;
152 }
153
154 /* loop through the whole file */
c81b35c0
KS
155 lineno = 0;
156 cur = 0;
e8baccca 157 while (1) {
c81b35c0
KS
158 count = buf_get_line(buf, bufsize, cur);
159
160 strncpy(line, buf + cur, count);
161 line[count] = '\0';
162 temp = line;
e8baccca
GKH
163 lineno++;
164
c81b35c0
KS
165 cur += count+1;
166 if (cur > bufsize)
167 break;
168
e8baccca
GKH
169 dbg_parse("read '%s'", temp);
170
171 /* eat the whitespace at the beginning of the line */
172 while (isspace(*temp))
173 ++temp;
174
175 /* empty line? */
176 if (*temp == 0x00)
177 continue;
178
179 /* see if this is a comment */
180 if (*temp == COMMENT_CHARACTER)
181 continue;
182
274812b5 183 retval = parse_get_pair(&temp, &variable, &value);
e8baccca
GKH
184 if (retval)
185 break;
186
187 dbg_parse("variable = '%s', value = '%s'", variable, value);
188
189 set_var("udev_root", udev_root);
190 set_var("udev_db", udev_db_filename);
191 set_var("udev_rules", udev_rules_filename);
3836a3c4 192 set_var("udev_permissions", udev_permissions_filename);
e8baccca 193 set_var("default_mode", default_mode_str);
74c73ef9 194 set_var("default_owner", default_owner_str);
195 set_var("default_group", default_group_str);
51a8bb2f 196 set_bool("udev_log", udev_log);
e8baccca
GKH
197 }
198 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
199 lineno, temp - line, temp);
c81b35c0
KS
200
201 file_unmap(buf, bufsize);
e8baccca
GKH
202 return retval;
203}
204
205static void get_dirs(void)
206{
207 char *temp;
e8baccca
GKH
208 int retval;
209
210 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
211 if (retval)
212 dbg("sysfs_get_mnt_path failed");
213
214 /* see if we should try to override any of the default values */
215 temp = getenv("UDEV_TEST");
216 if (temp != NULL) {
217 /* hm testing is happening, use the specified values, if they are present */
218 temp = getenv("SYSFS_PATH");
219 if (temp)
220 strfieldcpy(sysfs_path, temp);
e8baccca
GKH
221 temp = getenv("UDEV_CONFIG_FILE");
222 if (temp)
3836a3c4 223 strfieldcpy(udev_config_filename, temp);
e8baccca
GKH
224 }
225 dbg("sysfs_path='%s'", sysfs_path);
226
e8baccca
GKH
227 dbg_parse("udev_root = %s", udev_root);
228 dbg_parse("udev_config_filename = %s", udev_config_filename);
229 dbg_parse("udev_db_filename = %s", udev_db_filename);
230 dbg_parse("udev_rules_filename = %s", udev_rules_filename);
3836a3c4 231 dbg_parse("udev_permissions_filename = %s", udev_permissions_filename);
51a8bb2f 232 dbg_parse("udev_log = %d", udev_log);
e8baccca
GKH
233 parse_config_file();
234
235 dbg_parse("udev_root = %s", udev_root);
236 dbg_parse("udev_config_filename = %s", udev_config_filename);
237 dbg_parse("udev_db_filename = %s", udev_db_filename);
238 dbg_parse("udev_rules_filename = %s", udev_rules_filename);
3836a3c4 239 dbg_parse("udev_permissions_filename = %s", udev_permissions_filename);
51a8bb2f 240 dbg_parse("udev_log_str = %d", udev_log);
e8baccca
GKH
241}
242
243void udev_init_config(void)
244{
245 init_variables();
246 get_dirs();
247}
248
249