chiark / gitweb /
fix typo in group assignment
[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_run;
48
49 static int get_key(char **line, char **key, char **value)
50 {
51         char *linepos;
52         char *temp;
53
54         linepos = *line;
55         if (!linepos)
56                 return -1;
57
58         /* skip whitespace */
59         while (isspace(linepos[0]))
60                 linepos++;
61
62         /* get the key */
63         *key = linepos;
64         while (1) {
65                 linepos++;
66                 if (linepos[0] == '\0')
67                         return -1;
68                 if (isspace(linepos[0]))
69                         break;
70                 if (linepos[0] == '=')
71                         break;
72         }
73
74         /* terminate key */
75         linepos[0] = '\0';
76         linepos++;
77
78         /* skip whitespace */
79         while (isspace(linepos[0]))
80                 linepos++;
81
82         /* get the value*/
83         if (linepos[0] == '"')
84                 linepos++;
85         else
86                 return -1;
87         *value = linepos;
88
89         temp = strchr(linepos, '"');
90         if (!temp)
91                 return -1;
92         temp[0] = '\0';
93
94         return 0;
95 }
96
97 static int parse_config_file(void)
98 {
99         char line[LINE_SIZE];
100         char *bufline;
101         char *linepos;
102         char *variable;
103         char *value;
104         char *buf;
105         size_t bufsize;
106         size_t cur;
107         size_t count;
108         int lineno;
109         int retval = 0;
110
111         if (file_map(udev_config_filename, &buf, &bufsize) != 0) {
112                 err("can't open '%s' as config file", udev_config_filename);
113                 return -ENODEV;
114         }
115
116         /* loop through the whole file */
117         lineno = 0;
118         cur = 0;
119         while (cur < bufsize) {
120                 count = buf_get_line(buf, bufsize, cur);
121                 bufline = &buf[cur];
122                 cur += count+1;
123                 lineno++;
124
125                 if (count >= sizeof(line)) {
126                         err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
127                         continue;
128                 }
129
130                 /* eat the whitespace */
131                 while ((count > 0) && isspace(bufline[0])) {
132                         bufline++;
133                         count--;
134                 }
135                 if (count == 0)
136                         continue;
137
138                 /* see if this is a comment */
139                 if (bufline[0] == COMMENT_CHARACTER)
140                         continue;
141
142                 strlcpy(line, bufline, count+1);
143
144                 linepos = line;
145                 retval = get_key(&linepos, &variable, &value);
146                 if (retval != 0) {
147                         err("error parsing %s, line %d:%d", udev_config_filename, lineno, (int) (linepos-line));
148                         continue;
149                 }
150
151                 if (strcasecmp(variable, "udev_root") == 0) {
152                         strlcpy(udev_root, value, sizeof(udev_root));
153                         remove_trailing_char(udev_root, '/');
154                         continue;
155                 }
156
157                 if (strcasecmp(variable, "udev_db") == 0) {
158                         strlcpy(udev_db_path, value, sizeof(udev_db_path));
159                         remove_trailing_char(udev_db_path, '/');
160                         continue;
161                 }
162
163                 if (strcasecmp(variable, "udev_rules") == 0) {
164                         strlcpy(udev_rules_filename, value, sizeof(udev_rules_filename));
165                         remove_trailing_char(udev_rules_filename, '/');
166                         continue;
167                 }
168
169                 if (strcasecmp(variable, "udev_log") == 0) {
170                         udev_log_priority = log_priority(value);
171                         continue;
172                 }
173         }
174
175         file_unmap(buf, bufsize);
176         return retval;
177 }
178
179 void udev_init_config(void)
180 {
181         const char *env;
182
183         strcpy(udev_root, UDEV_ROOT);
184         strcpy(udev_db_path, UDEV_DB);
185         strcpy(udev_config_filename, UDEV_CONFIG_FILE);
186         strcpy(udev_rules_filename, UDEV_RULES_FILE);
187         udev_log_priority = LOG_ERR;
188         udev_run = 1;
189         sysfs_get_mnt_path(sysfs_path, sizeof(sysfs_path));
190
191         /* disable RUN key execution */
192         env = getenv("UDEV_RUN");
193         if (env && !string_is_true(env))
194                 udev_run = 0;
195
196         env = getenv("UDEV_CONFIG_FILE");
197         if (env) {
198                 strlcpy(udev_config_filename, env, sizeof(udev_config_filename));
199                 remove_trailing_char(udev_config_filename, '/');
200         }
201
202         parse_config_file();
203
204         env = getenv("UDEV_LOG");
205         if (env)
206                 udev_log_priority = log_priority(env);
207
208         dbg("sysfs_path='%s'", sysfs_path);
209         dbg("UDEV_CONFIG_FILE='%s'", udev_config_filename);
210         dbg("udev_root='%s'", udev_root);
211         dbg("udev_db='%s'", udev_db_path);
212         dbg("udev_rules='%s'", udev_rules_filename);
213         dbg("udev_log=%d", udev_log_priority);
214 }