chiark / gitweb /
[PATCH] handle netdev in udevruler
[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 /* 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
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "udev.h"
37 #include "udev_lib.h"
38 #include "udev_version.h"
39 #include "logging.h"
40 #include "namedev.h"
41
42 /* global variables */
43 char sysfs_path[SYSFS_PATH_MAX];
44 char udev_root[PATH_MAX];
45 char udev_db_filename[PATH_MAX+NAME_MAX];
46 char udev_permissions_filename[PATH_MAX+NAME_MAX];
47 char udev_rules_filename[PATH_MAX+NAME_MAX];
48 char udev_config_filename[PATH_MAX+NAME_MAX];
49 char default_mode_str[MODE_SIZE];
50 char default_owner_str[OWNER_SIZE];
51 char default_group_str[GROUP_SIZE];
52 int udev_log;
53 int udev_sleep;
54 int udev_dev_d;
55
56
57 static 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
66 static void init_variables(void)
67 {
68         /* fill up the defaults.  
69          * If any config values are specified, they will
70          * override these values. */
71         strfieldcpy(udev_root, UDEV_ROOT);
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);
76         udev_log = string_is_true(UDEV_LOG_DEFAULT);
77
78         udev_sleep = 1;
79         if (getenv("UDEV_NO_SLEEP") != NULL)
80                 udev_sleep = 0;
81
82         udev_dev_d = 1;
83         if (getenv("UDEV_NO_DEVD") != NULL)
84                 udev_dev_d = 0;
85 }
86
87 #define set_var(_name, _var)                            \
88         if (strcasecmp(variable, _name) == 0) {         \
89                 dbg_parse("%s='%s'", _name, value);     \
90                 strfieldcpy(_var, value);\
91         }
92
93 #define set_bool(_name, _var)                           \
94         if (strcasecmp(variable, _name) == 0) {         \
95                 dbg_parse("%s='%s'", _name, value);     \
96                 _var = string_is_true(value);           \
97         }
98
99 int 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
134 static int parse_config_file(void)
135 {
136         char line[255];
137         char *temp;
138         char *variable;
139         char *value;
140         char *buf;
141         size_t bufsize;
142         size_t cur;
143         size_t count;
144         int lineno;
145         int retval = 0;
146
147         if (file_map(udev_config_filename, &buf, &bufsize) == 0) {
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 */
155         lineno = 0;
156         cur = 0;
157         while (1) {
158                 count = buf_get_line(buf, bufsize, cur);
159
160                 strncpy(line, buf + cur, count);
161                 line[count] = '\0';
162                 temp = line;
163                 lineno++;
164
165                 cur += count+1;
166                 if (cur > bufsize)
167                         break;
168
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
183                 retval = parse_get_pair(&temp, &variable, &value);
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);
192                 set_var("udev_permissions", udev_permissions_filename);
193                 set_var("default_mode", default_mode_str);
194                 set_var("default_owner", default_owner_str);
195                 set_var("default_group", default_group_str);
196                 set_bool("udev_log", udev_log);
197         }
198         dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
199                   lineno, temp - line, temp);
200
201         file_unmap(buf, bufsize);
202         return retval;
203 }
204
205 static void get_dirs(void)
206 {
207         char *temp;
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);
221                 temp = getenv("UDEV_CONFIG_FILE");
222                 if (temp)
223                         strfieldcpy(udev_config_filename, temp);
224         }
225         dbg("sysfs_path='%s'", sysfs_path);
226
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);
231         dbg_parse("udev_permissions_filename = %s", udev_permissions_filename);
232         dbg_parse("udev_log = %d", udev_log);
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);
239         dbg_parse("udev_permissions_filename = %s", udev_permissions_filename);
240         dbg_parse("udev_log_str = %d", udev_log);
241 }
242
243 void udev_init_config(void)
244 {
245         init_variables();
246         get_dirs();
247 }
248
249