chiark / gitweb /
[PATCH] Update the man page to show the new config file, it's format, and how to...
[elogind.git] / udev_config.c
1 /*
2  * udev_config.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 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 "udev.h"
36 #include "udev_version.h"
37 #include "namedev.h"
38 #include "libsysfs/libsysfs.h"
39
40 /* global variables */
41 char sysfs_path[SYSFS_PATH_MAX];
42 char udev_config_dir[PATH_MAX];
43 char udev_root[PATH_MAX];
44 char udev_db_filename[PATH_MAX+NAME_MAX];
45 char udev_permission_filename[PATH_MAX+NAME_MAX];
46 char udev_rules_filename[PATH_MAX+NAME_MAX];
47 char udev_config_filename[PATH_MAX+NAME_MAX];
48 char default_mode_str[NAME_MAX];
49
50
51 static void init_variables(void)
52 {
53         strfieldcpy(udev_root, UDEV_ROOT);
54         strfieldcpy(udev_config_dir, UDEV_CONFIG_DIR);
55 }
56
57 #define set_var(_name, _var)                            \
58         if (strcasecmp(variable, _name) == 0) {         \
59                 dbg_parse("%s = '%s'", _name, value);   \
60                 strncpy(_var, value, sizeof(_var));     \
61         }
62
63 static int parse_config_file(void)
64 {
65         char line[255];
66         char *temp;
67         char *variable;
68         char *value;
69         FILE *fd;
70         int lineno = 0;
71         int retval = 0;
72         
73         fd = fopen(udev_config_filename, "r");
74         if (fd != NULL) {
75                 dbg("reading '%s' as config file", udev_config_filename);
76         } else {
77                 dbg("can't open '%s' as config file", udev_config_filename);
78                 return -ENODEV;
79         }
80
81         /* loop through the whole file */
82         while (1) {
83                 /* get a line */
84                 temp = fgets(line, sizeof(line), fd);
85                 if (temp == NULL)
86                         goto exit;
87                 lineno++;
88
89                 dbg_parse("read '%s'", temp);
90
91                 /* eat the whitespace at the beginning of the line */
92                 while (isspace(*temp))
93                         ++temp;
94
95                 /* empty line? */
96                 if (*temp == 0x00)
97                         continue;
98
99                 /* see if this is a comment */
100                 if (*temp == COMMENT_CHARACTER)
101                         continue;
102
103                 retval = get_pair(&temp, &variable, &value);
104                 if (retval)
105                         break;
106                 
107                 dbg_parse("variable = '%s', value = '%s'", variable, value);
108
109                 set_var("udev_root", udev_root);
110                 set_var("udev_db", udev_db_filename);
111                 set_var("udev_rules", udev_rules_filename);
112                 set_var("udev_permissions", udev_permission_filename);
113                 set_var("default_mode", default_mode_str);
114         }
115         dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
116                   lineno, temp - line, temp);
117 exit:
118         fclose(fd);
119         return retval;
120 }
121
122 static void get_dirs(void)
123 {
124         char *temp;
125         char *udev_db = UDEV_DB;
126         char *udev_config = UDEV_CONFIG_FILE;
127         char *udev_rules = UDEV_RULES_FILE;
128         char *udev_permission = UDEV_PERMISSION_FILE;
129         int retval;
130
131         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
132         if (retval)
133                 dbg("sysfs_get_mnt_path failed");
134
135         /* see if we should try to override any of the default values */
136         temp = getenv("UDEV_TEST");
137         if (temp != NULL) {
138                 /* hm testing is happening, use the specified values, if they are present */
139                 temp = getenv("SYSFS_PATH");
140                 if (temp)
141                         strfieldcpy(sysfs_path, temp);
142                 temp = getenv("UDEV_CONFIG_DIR");
143                 if (temp)
144                         strfieldcpy(udev_config_dir, temp);
145                 temp = getenv("UDEV_ROOT");
146                 if (temp)
147                         strfieldcpy(udev_root, temp);
148                 temp = getenv("UDEV_DB");
149                 if (temp)
150                         udev_db = temp;
151                 temp = getenv("UDEV_CONFIG_FILE");
152                 if (temp)
153                         udev_config = temp;
154                 temp = getenv("UDEV_RULES_FILE");
155                 if (temp)
156                         udev_rules = temp;
157                 temp = getenv("UDEV_PERMISSION_FILE");
158                 if (temp)
159                         udev_permission = temp;
160         }
161         dbg("sysfs_path='%s'", sysfs_path);
162
163         strncpy(udev_db_filename, udev_root, sizeof(udev_db_filename));
164         strncat(udev_db_filename, udev_db, sizeof(udev_db_filename));
165
166         strncpy(udev_config_filename, udev_config_dir, sizeof(udev_config_filename));
167         strncat(udev_config_filename, udev_config, sizeof(udev_config_filename));
168         
169         strncpy(udev_rules_filename, udev_config_dir, sizeof(udev_permission_filename));
170         strncat(udev_rules_filename, udev_rules, sizeof(udev_permission_filename));
171
172         strncpy(udev_permission_filename, udev_config_dir, sizeof(udev_permission_filename));
173         strncat(udev_permission_filename, udev_permission, sizeof(udev_permission_filename));
174
175         dbg_parse("udev_root = %s", udev_root);
176         dbg_parse("udev_config_filename = %s", udev_config_filename);
177         dbg_parse("udev_db_filename = %s", udev_db_filename);
178         dbg_parse("udev_rules_filename = %s", udev_rules_filename);
179         dbg_parse("udev_permission_filename = %s", udev_permission_filename);
180         parse_config_file();
181
182         dbg_parse("udev_root = %s", udev_root);
183         dbg_parse("udev_config_filename = %s", udev_config_filename);
184         dbg_parse("udev_db_filename = %s", udev_db_filename);
185         dbg_parse("udev_rules_filename = %s", udev_rules_filename);
186         dbg_parse("udev_permission_filename = %s", udev_permission_filename);
187 }
188
189 void udev_init_config(void)
190 {
191         init_variables();
192         get_dirs();
193 }
194
195