]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev_config.c
move default rules from /etc/udev/rules.d/ to /lib/udev/rules.d/
[thirdparty/systemd.git] / udev_config.c
CommitLineData
e8baccca 1/*
27b77df4
KS
2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
e8baccca
GKH
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
27b77df4 16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
e8baccca
GKH
17 *
18 */
19
e8baccca
GKH
20#include <stdlib.h>
21#include <string.h>
22#include <stdio.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <errno.h>
26#include <ctype.h>
6b493a20 27#include <syslog.h>
e8baccca
GKH
28
29#include "udev.h"
e8baccca
GKH
30
31/* global variables */
63f61c5c 32char udev_root[PATH_SIZE];
63f61c5c 33char udev_config_filename[PATH_SIZE];
9dd0c257 34char udev_rules_dir[PATH_SIZE];
6b493a20 35int udev_log_priority;
821d0ec8 36int udev_run;
e8baccca 37
28ce66de
KS
38static int get_key(char **line, char **key, char **value)
39{
40 char *linepos;
41 char *temp;
42
43 linepos = *line;
44 if (!linepos)
45 return -1;
46
47 /* skip whitespace */
48 while (isspace(linepos[0]))
49 linepos++;
50
51 /* get the key */
52 *key = linepos;
53 while (1) {
54 linepos++;
55 if (linepos[0] == '\0')
56 return -1;
57 if (isspace(linepos[0]))
58 break;
59 if (linepos[0] == '=')
60 break;
61 }
62
63 /* terminate key */
64 linepos[0] = '\0';
65 linepos++;
66
67 /* skip whitespace */
68 while (isspace(linepos[0]))
69 linepos++;
70
71 /* get the value*/
72 if (linepos[0] == '"')
73 linepos++;
74 else
75 return -1;
76 *value = linepos;
77
78 temp = strchr(linepos, '"');
79 if (!temp)
80 return -1;
81 temp[0] = '\0';
82
83 return 0;
84}
85
e8baccca
GKH
86static int parse_config_file(void)
87{
3e441450 88 char line[LINE_SIZE];
89 char *bufline;
28ce66de 90 char *linepos;
e8baccca
GKH
91 char *variable;
92 char *value;
c81b35c0
KS
93 char *buf;
94 size_t bufsize;
95 size_t cur;
96 size_t count;
97 int lineno;
e8baccca 98 int retval = 0;
c81b35c0 99
63f61c5c 100 if (file_map(udev_config_filename, &buf, &bufsize) != 0) {
c70560fe 101 err("can't open '%s' as config file: %s\n", udev_config_filename, strerror(errno));
e8baccca
GKH
102 return -ENODEV;
103 }
104
105 /* loop through the whole file */
c81b35c0
KS
106 lineno = 0;
107 cur = 0;
3e441450 108 while (cur < bufsize) {
c81b35c0 109 count = buf_get_line(buf, bufsize, cur);
3e441450 110 bufline = &buf[cur];
c81b35c0 111 cur += count+1;
3e441450 112 lineno++;
e8baccca 113
3e441450 114 /* eat the whitespace */
3db7fa27 115 while ((count > 0) && isspace(bufline[0])) {
3e441450 116 bufline++;
117 count--;
118 }
3db7fa27
KS
119 if (count == 0)
120 continue;
3e441450 121
e8baccca 122 /* see if this is a comment */
3e441450 123 if (bufline[0] == COMMENT_CHARACTER)
e8baccca
GKH
124 continue;
125
16511863 126 if (count >= sizeof(line)) {
c70560fe 127 err("line too long, conf line skipped %s, line %d\n", udev_config_filename, lineno);
16511863
HH
128 continue;
129 }
130
13d11705
KS
131 memcpy(line, bufline, count);
132 line[count] = '\0';
3e441450 133
28ce66de
KS
134 linepos = line;
135 retval = get_key(&linepos, &variable, &value);
136 if (retval != 0) {
c70560fe 137 err("error parsing %s, line %d:%d\n", udev_config_filename, lineno, (int)(linepos-line));
28ce66de
KS
138 continue;
139 }
aef6bb13 140
aef6bb13 141 if (strcasecmp(variable, "udev_root") == 0) {
63f61c5c 142 strlcpy(udev_root, value, sizeof(udev_root));
b2c6818d 143 remove_trailing_chars(udev_root, '/');
aef6bb13
KS
144 continue;
145 }
146
aef6bb13 147 if (strcasecmp(variable, "udev_rules") == 0) {
9dd0c257
KS
148 strlcpy(udev_rules_dir, value, sizeof(udev_rules_dir));
149 remove_trailing_chars(udev_rules_dir, '/');
aef6bb13
KS
150 continue;
151 }
152
aef6bb13 153 if (strcasecmp(variable, "udev_log") == 0) {
6b493a20 154 udev_log_priority = log_priority(value);
aef6bb13
KS
155 continue;
156 }
e8baccca 157 }
c81b35c0
KS
158
159 file_unmap(buf, bufsize);
e8baccca
GKH
160 return retval;
161}
162
1aa1e248 163void udev_config_init(void)
e8baccca 164{
6b493a20 165 const char *env;
e8baccca 166
6b493a20 167 strcpy(udev_config_filename, UDEV_CONFIG_FILE);
282988c4
KS
168 strcpy(udev_root, UDEV_ROOT);
169 udev_rules_dir[0] = '\0';
6b493a20 170 udev_log_priority = LOG_ERR;
821d0ec8 171 udev_run = 1;
e8baccca 172
821d0ec8
KS
173 /* disable RUN key execution */
174 env = getenv("UDEV_RUN");
175 if (env && !string_is_true(env))
176 udev_run = 0;
177
6b493a20
KS
178 env = getenv("UDEV_CONFIG_FILE");
179 if (env) {
180 strlcpy(udev_config_filename, env, sizeof(udev_config_filename));
b2c6818d 181 remove_trailing_chars(udev_config_filename, '/');
6b493a20 182 }
e8baccca 183
e8baccca 184 parse_config_file();
6b493a20 185
2796c47b
MI
186 env = getenv("UDEV_ROOT");
187 if (env) {
188 strlcpy(udev_root, env, sizeof(udev_root));
189 remove_trailing_chars(udev_root, '/');
190 }
191
6b493a20
KS
192 env = getenv("UDEV_LOG");
193 if (env)
194 udev_log_priority = log_priority(env);
195
c70560fe
KS
196 dbg("UDEV_CONFIG_FILE='%s'\n", udev_config_filename);
197 dbg("udev_root='%s'\n", udev_root);
282988c4 198 dbg("udev_rules_dir='%s'\n", udev_rules_dir);
c70560fe 199 dbg("udev_log=%d\n", udev_log_priority);
e8baccca 200}