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/>.
30 #include "libudev-private.h"
34 void (*log_fn)(struct udev *udev,
35 int priority, const char *file, int line, const char *fn,
36 const char *format, va_list args);
45 void udev_log(struct udev *udev,
46 int priority, const char *file, int line, const char *fn,
47 const char *format, ...)
51 if (priority > udev->log_priority)
54 va_start(args, format);
55 udev->log_fn(udev, priority, file, line, fn, format, args);
59 static void log_stderr(struct udev *udev,
60 int priority, const char *file, int line, const char *fn,
61 const char *format, va_list args)
63 fprintf(stderr, "libudev: %s: ", fn);
64 vfprintf(stderr, format, args);
67 void *udev_get_userdata(struct udev *udev)
71 return udev->userdata;
74 void udev_set_userdata(struct udev *udev, void *userdata)
78 udev->userdata = userdata;
84 * Create udev library context.
86 * The initial refcount is 1, and needs to be decremented to
87 * release the ressources of the udev library context.
89 * Returns: a new udev library context
91 struct udev *udev_new(void)
98 udev = malloc(sizeof(struct udev));
101 memset(udev, 0x00, (sizeof(struct udev)));
103 udev->log_fn = log_stderr;
104 udev->log_priority = LOG_ERR;
106 udev->dev_path = strdup(UDEV_PREFIX "/dev");
107 udev->sys_path = strdup("/sys");
108 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
109 if (udev->dev_path == NULL ||
110 udev->sys_path == NULL ||
114 /* settings by environment and config file */
115 env = getenv("SYSFS_PATH");
117 free(udev->sys_path);
118 udev->sys_path = strdup(env);
119 util_remove_trailing_chars(udev->sys_path, '/');
122 env = getenv("UDEV_RUN");
123 if (env != NULL && strcmp(env, "0") == 0)
126 env = getenv("UDEV_CONFIG_FILE");
129 config_file = strdup(env);
130 util_remove_trailing_chars(config_file, '/');
132 if (config_file == NULL)
134 f = fopen(config_file, "r");
136 char line[UTIL_LINE_SIZE];
139 while (fgets(line, sizeof(line), f)) {
148 while (isspace(key[0]))
151 /* comment or empty line */
152 if (key[0] == '#' || key[0] == '\0')
155 /* split key/value */
156 val = strchr(key, '=');
158 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
165 while (isspace(val[0]))
172 while (isspace(key[len-1]))
176 /* terminate value */
180 while (isspace(val[len-1]))
188 if (val[0] == '"' || val[0] == '\'') {
189 if (val[len-1] != val[0]) {
190 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
197 if (strcasecmp(key, "udev_log") == 0) {
198 udev->log_priority = util_log_priority(val);
201 if (strcasecmp(key, "udev_root") == 0) {
202 free(udev->dev_path);
203 udev->dev_path = strdup(val);
204 util_remove_trailing_chars(udev->dev_path, '/');
207 if (strcasecmp(key, "udev_rules") == 0) {
208 free(udev->rules_path);
209 udev->rules_path = strdup(val);
210 util_remove_trailing_chars(udev->rules_path, '/');
217 env = getenv("UDEV_ROOT");
219 free(udev->dev_path);
220 udev->dev_path = strdup(env);
221 util_remove_trailing_chars(udev->dev_path, '/');
224 env = getenv("UDEV_LOG");
226 udev->log_priority = util_log_priority(env);
228 if (udev->dev_path == NULL || udev->sys_path == NULL)
230 info(udev, "context %p created\n", udev);
231 info(udev, "log_priority=%d\n", udev->log_priority);
232 info(udev, "config_file='%s'\n", config_file);
233 info(udev, "dev_path='%s'\n", udev->dev_path);
234 info(udev, "sys_path='%s'\n", udev->sys_path);
235 if (udev->rules_path != NULL)
236 info(udev, "rules_path='%s'\n", udev->rules_path);
241 err(udev, "context creation failed\n");
248 * @udev: udev library context
250 * Take a reference of the udev library context.
252 * Returns: the passed udev library context
254 struct udev *udev_ref(struct udev *udev)
264 * @udev: udev library context
266 * Drop a reference of the udev library context. If the refcount
267 * reaches zero, the ressources of the context will be released.
270 void udev_unref(struct udev *udev)
275 if (udev->refcount > 0)
277 free(udev->dev_path);
278 free(udev->sys_path);
279 free(udev->rules_path);
280 info(udev, "context %p released\n", udev);
286 * @udev: udev library context
287 * @log_fn: function to be called for logging messages
289 * The built-in logging, which writes to stderr, it can be
290 * overridden by a custom function, to plug log messages
291 * into the users logging functionality.
294 void udev_set_log_fn(struct udev *udev,
295 void (*log_fn)(struct udev *udev,
296 int priority, const char *file, int line, const char *fn,
297 const char *format, va_list args))
299 udev->log_fn = log_fn;
300 info(udev, "custom logging function %p registered\n", udev);
303 int udev_get_log_priority(struct udev *udev)
305 return udev->log_priority;
308 void udev_set_log_priority(struct udev *udev, int priority)
310 udev->log_priority = priority;
313 const char *udev_get_rules_path(struct udev *udev)
315 return udev->rules_path;
318 int udev_get_run(struct udev *udev)
325 * @udev: udev library context
327 * Retrieve the sysfs mount point. The default is "/sys". For
328 * testing purposes, it can be overridden with the environment
329 * variable SYSFS_PATH.
331 * Returns: the sys mount point
333 const char *udev_get_sys_path(struct udev *udev)
337 return udev->sys_path;
342 * @udev: udev library context
344 * Retrieve the device directory path. The default value is "/dev",
345 * the actual value may be overridden in the udev configuration
348 * Returns: the device directory path
350 const char *udev_get_dev_path(struct udev *udev)
354 return udev->dev_path;