chiark / gitweb /
[PATCH] udev_rules.c: don't change sysfs_device while walking up the device chain
[elogind.git] / udev_config.c
1 /*
2  * udev_config.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003,2004 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 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <syslog.h>
32
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
35 #include "udev.h"
36 #include "udev_utils.h"
37 #include "udev_version.h"
38 #include "logging.h"
39
40 /* global variables */
41 char sysfs_path[PATH_SIZE];
42 char udev_root[PATH_SIZE];
43 char udev_db_path[PATH_SIZE];
44 char udev_config_filename[PATH_SIZE];
45 char udev_rules_filename[PATH_SIZE];
46 int udev_log_priority;
47 int udev_dev_d;
48 int udev_hotplug_d;
49
50 static int string_is_true(const char *str)
51 {
52         if (strcasecmp(str, "true") == 0)
53                 return 1;
54         if (strcasecmp(str, "yes") == 0)
55                 return 1;
56         if (strcasecmp(str, "1") == 0)
57                 return 1;
58         return 0;
59 }
60
61 static int log_priority(const char *priority)
62 {
63         char *endptr;
64         int prio;
65
66         prio = strtol(priority, &endptr, 10);
67         if (endptr[0] == '\0')
68                 return prio;
69         if (strncasecmp(priority, "err", 3) == 0)
70                 return LOG_ERR;
71         if (strcasecmp(priority, "info") == 0)
72                 return LOG_INFO;
73         if (strcasecmp(priority, "debug") == 0)
74                 return LOG_DEBUG;
75         if (string_is_true(priority))
76                 return LOG_ERR;
77
78         return 0;
79 }
80
81 static int get_key(char **line, char **key, char **value)
82 {
83         char *linepos;
84         char *temp;
85
86         linepos = *line;
87         if (!linepos)
88                 return -1;
89
90         /* skip whitespace */
91         while (isspace(linepos[0]))
92                 linepos++;
93
94         /* get the key */
95         *key = linepos;
96         while (1) {
97                 linepos++;
98                 if (linepos[0] == '\0')
99                         return -1;
100                 if (isspace(linepos[0]))
101                         break;
102                 if (linepos[0] == '=')
103                         break;
104         }
105
106         /* terminate key */
107         linepos[0] = '\0';
108         linepos++;
109
110         /* skip whitespace */
111         while (isspace(linepos[0]))
112                 linepos++;
113
114         /* get the value*/
115         if (linepos[0] == '"')
116                 linepos++;
117         else
118                 return -1;
119         *value = linepos;
120
121         temp = strchr(linepos, '"');
122         if (!temp)
123                 return -1;
124         temp[0] = '\0';
125
126         return 0;
127 }
128
129 static int parse_config_file(void)
130 {
131         char line[LINE_SIZE];
132         char *bufline;
133         char *linepos;
134         char *variable;
135         char *value;
136         char *buf;
137         size_t bufsize;
138         size_t cur;
139         size_t count;
140         int lineno;
141         int retval = 0;
142
143         if (file_map(udev_config_filename, &buf, &bufsize) != 0) {
144                 err("can't open '%s' as config file", udev_config_filename);
145                 return -ENODEV;
146         }
147
148         /* loop through the whole file */
149         lineno = 0;
150         cur = 0;
151         while (cur < bufsize) {
152                 count = buf_get_line(buf, bufsize, cur);
153                 bufline = &buf[cur];
154                 cur += count+1;
155                 lineno++;
156
157                 if (count >= sizeof(line)) {
158                         err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
159                         continue;
160                 }
161
162                 /* eat the whitespace */
163                 while ((count > 0) && isspace(bufline[0])) {
164                         bufline++;
165                         count--;
166                 }
167                 if (count == 0)
168                         continue;
169
170                 /* see if this is a comment */
171                 if (bufline[0] == COMMENT_CHARACTER)
172                         continue;
173
174                 strlcpy(line, bufline, count+1);
175
176                 linepos = line;
177                 retval = get_key(&linepos, &variable, &value);
178                 if (retval != 0) {
179                         err("error parsing %s, line %d:%d", udev_config_filename, lineno, (int) (linepos-line));
180                         continue;
181                 }
182
183                 if (strcasecmp(variable, "udev_root") == 0) {
184                         strlcpy(udev_root, value, sizeof(udev_root));
185                         remove_trailing_char(udev_root, '/');
186                         continue;
187                 }
188
189                 if (strcasecmp(variable, "udev_db") == 0) {
190                         strlcpy(udev_db_path, value, sizeof(udev_db_path));
191                         remove_trailing_char(udev_db_path, '/');
192                         continue;
193                 }
194
195                 if (strcasecmp(variable, "udev_rules") == 0) {
196                         strlcpy(udev_rules_filename, value, sizeof(udev_rules_filename));
197                         remove_trailing_char(udev_rules_filename, '/');
198                         continue;
199                 }
200
201                 if (strcasecmp(variable, "udev_log") == 0) {
202                         udev_log_priority = log_priority(value);
203                         continue;
204                 }
205         }
206
207         file_unmap(buf, bufsize);
208         return retval;
209 }
210
211 void udev_init_config(void)
212 {
213         const char *env;
214
215         strcpy(udev_root, UDEV_ROOT);
216         strcpy(udev_db_path, UDEV_DB);
217         strcpy(udev_config_filename, UDEV_CONFIG_FILE);
218         strcpy(udev_rules_filename, UDEV_RULES_FILE);
219         udev_log_priority = LOG_ERR;
220         udev_dev_d = 1;
221         udev_hotplug_d = 1;
222         sysfs_get_mnt_path(sysfs_path, sizeof(sysfs_path));
223
224         env = getenv("UDEV_NO_DEVD");
225         if (env && string_is_true(env))
226                 udev_dev_d = 0;
227
228         env = getenv("UDEV_NO_HOTPLUGD");
229         if (env && string_is_true(env))
230                 udev_hotplug_d = 0;
231
232         env = getenv("UDEV_CONFIG_FILE");
233         if (env) {
234                 strlcpy(udev_config_filename, env, sizeof(udev_config_filename));
235                 remove_trailing_char(udev_config_filename, '/');
236         }
237
238         parse_config_file();
239
240         env = getenv("UDEV_LOG");
241         if (env)
242                 udev_log_priority = log_priority(env);
243
244         dbg("sysfs_path='%s'", sysfs_path);
245         dbg("UDEV_CONFIG_FILE='%s'", udev_config_filename);
246         dbg("udev_root='%s'", udev_root);
247         dbg("udev_db='%s'", udev_db_path);
248         dbg("udev_rules='%s'", udev_rules_filename);
249         dbg("udev_log=%d", udev_log_priority);
250 }