2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "libudev-private.h"
37 void (*log_fn)(struct udev *udev,
38 int priority, const char *file, int line, const char *fn,
39 const char *format, va_list args);
47 void udev_log(struct udev *udev,
48 int priority, const char *file, int line, const char *fn,
49 const char *format, ...)
53 if (priority > udev->log_priority)
56 va_start(args, format);
57 udev->log_fn(udev, priority, file, line, fn, format, args);
61 static void log_stderr(struct udev *udev,
62 int priority, const char *file, int line, const char *fn,
63 const char *format, va_list args)
65 fprintf(stderr, "libudev: %s: ", fn);
66 vfprintf(stderr, format, args);
72 * Create udev library context.
74 * The initial refcount is 1, and needs to be decremented to
75 * release the ressources of the udev library context.
77 * Returns: a new udev library context
79 struct udev *udev_new(void)
86 udev = malloc(sizeof(struct udev));
89 memset(udev, 0x00, (sizeof(struct udev)));
96 udev->log_fn = log_stderr;
97 udev->log_priority = LOG_ERR;
99 udev->dev_path = strdup(UDEV_PREFIX "/dev");
100 udev->sys_path = strdup("/sys");
101 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
103 if (udev->dev_path == NULL || udev->sys_path == NULL)
106 /* settings by environment and config file */
107 env = getenv("SYSFS_PATH");
109 free(udev->sys_path);
110 udev->sys_path = strdup(env);
111 remove_trailing_chars(udev->sys_path, '/');
114 env = getenv("UDEV_RUN");
115 if (env != NULL && !string_is_true(env))
118 env = getenv("UDEV_CONFIG_FILE");
121 config_file = strdup(env);
122 remove_trailing_chars(config_file, '/');
124 if (config_file == NULL)
126 f = fopen(config_file, "r");
128 char line[LINE_SIZE];
131 while (fgets(line, sizeof(line), f)) {
140 while (isspace(key[0]))
143 /* comment or empty line */
144 if (key[0] == '#' || key[0] == '\0')
147 /* split key/value */
148 val = strchr(key, '=');
150 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
157 while (isspace(val[0]))
164 while (isspace(key[len-1]))
168 /* terminate value */
172 while (isspace(val[len-1]))
180 if (val[0] == '"' || val[0] == '\'') {
181 if (val[len-1] != val[0]) {
182 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
189 if (strcasecmp(key, "udev_log") == 0) {
190 udev->log_priority = log_priority(val);
193 if (strcasecmp(key, "udev_root") == 0) {
194 free(udev->dev_path);
195 udev->dev_path = strdup(val);
196 remove_trailing_chars(udev->dev_path, '/');
199 if (strcasecmp(key, "udev_rules") == 0) {
200 free(udev->rules_path);
201 udev->rules_path = strdup(val);
202 remove_trailing_chars(udev->rules_path, '/');
210 env = getenv("UDEV_ROOT");
212 free(udev->dev_path);
213 udev->dev_path = strdup(env);
214 remove_trailing_chars(udev->dev_path, '/');
217 env = getenv("UDEV_LOG");
219 udev->log_priority = log_priority(env);
221 if (udev->dev_path == NULL || udev->sys_path == NULL)
224 info(udev, "context %p created\n", udev);
225 info(udev, "log_priority=%d\n", udev->log_priority);
226 info(udev, "dev_path='%s'\n", udev->dev_path);
227 if (udev->rules_path != NULL)
228 info(udev, "rules_path='%s'\n", udev->rules_path);
232 err(udev, "context creation failed\n");
239 * @udev: udev library context
241 * Take a reference of the udev library context.
243 * Returns: the passed udev library context
245 struct udev *udev_ref(struct udev *udev)
255 * @udev: udev library context
257 * Drop a reference of the udev library context. If the refcount
258 * reaches zero, the ressources of the context will be released.
261 void udev_unref(struct udev *udev)
266 if (udev->refcount > 0)
269 free(udev->dev_path);
270 free(udev->sys_path);
271 info(udev, "context %p released\n", udev);
277 * @udev: udev library context
278 * @log_fn: function to be called for logging messages
280 * The built-in logging, which writes to stderr if the
281 * LIBUDEV_DEBUG environment variable is set, can be
282 * overridden by a custom function, to plug log messages
283 * into the users logging functionality.
286 void udev_set_log_fn(struct udev *udev,
287 void (*log_fn)(struct udev *udev,
288 int priority, const char *file, int line, const char *fn,
289 const char *format, va_list args))
291 udev->log_fn = log_fn;
292 info(udev, "custom logging function %p registered\n", udev);
295 int udev_get_log_priority(struct udev *udev)
297 return udev->log_priority;
300 void udev_set_log_priority(struct udev *udev, int priority)
302 udev->log_priority = priority;
305 const char *udev_get_rules_path(struct udev *udev)
307 return udev->rules_path;
310 int udev_get_run(struct udev *udev)
317 * @udev: udev library context
319 * Retrieve the sysfs mount point. The default is "/sys". For
320 * testing purposes, it can be overridden with the environment
321 * variable SYSFS_PATH.
323 * Returns: the sys mount point
325 const char *udev_get_sys_path(struct udev *udev)
329 return udev->sys_path;
334 * @udev: udev library context
336 * Retrieve the device directory path. The default value is "/dev",
337 * the actual value may be overridden in the udev configuration
340 * Returns: the device directory path
342 const char *udev_get_dev_path(struct udev *udev)
346 return udev->dev_path;