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);
41 struct udev_list_node properties_list;
46 void udev_log(struct udev *udev,
47 int priority, const char *file, int line, const char *fn,
48 const char *format, ...)
52 if (priority > udev->log_priority)
55 va_start(args, format);
56 udev->log_fn(udev, priority, file, line, fn, format, args);
60 static void log_stderr(struct udev *udev,
61 int priority, const char *file, int line, const char *fn,
62 const char *format, va_list args)
64 fprintf(stderr, "libudev: %s: ", fn);
65 vfprintf(stderr, format, args);
68 void *udev_get_userdata(struct udev *udev)
72 return udev->userdata;
75 void udev_set_userdata(struct udev *udev, void *userdata)
79 udev->userdata = userdata;
85 * Create udev library context.
87 * The initial refcount is 1, and needs to be decremented to
88 * release the ressources of the udev library context.
90 * Returns: a new udev library context
92 struct udev *udev_new(void)
99 udev = malloc(sizeof(struct udev));
102 memset(udev, 0x00, (sizeof(struct udev)));
104 udev->log_fn = log_stderr;
105 udev->log_priority = LOG_ERR;
106 udev_list_init(&udev->properties_list);
108 udev->dev_path = strdup(UDEV_PREFIX "/dev");
109 udev->sys_path = strdup("/sys");
110 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
111 if (udev->dev_path == NULL ||
112 udev->sys_path == NULL ||
116 /* settings by environment and config file */
117 env = getenv("SYSFS_PATH");
119 free(udev->sys_path);
120 udev->sys_path = strdup(env);
121 util_remove_trailing_chars(udev->sys_path, '/');
122 udev_add_property(udev, "SYSFS_PATH", udev->sys_path);
125 env = getenv("UDEV_RUN");
126 if (env != NULL && strcmp(env, "0") == 0)
129 env = getenv("UDEV_CONFIG_FILE");
132 config_file = strdup(env);
133 util_remove_trailing_chars(config_file, '/');
135 if (config_file == NULL)
137 f = fopen(config_file, "r");
139 char line[UTIL_LINE_SIZE];
142 while (fgets(line, sizeof(line), f)) {
151 while (isspace(key[0]))
154 /* comment or empty line */
155 if (key[0] == '#' || key[0] == '\0')
158 /* split key/value */
159 val = strchr(key, '=');
161 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
168 while (isspace(val[0]))
175 while (isspace(key[len-1]))
179 /* terminate value */
183 while (isspace(val[len-1]))
191 if (val[0] == '"' || val[0] == '\'') {
192 if (val[len-1] != val[0]) {
193 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
200 if (strcasecmp(key, "udev_log") == 0) {
201 udev_set_log_priority(udev, util_log_priority(val));
204 if (strcasecmp(key, "udev_root") == 0) {
205 free(udev->dev_path);
206 udev->dev_path = strdup(val);
207 util_remove_trailing_chars(udev->dev_path, '/');
210 if (strcasecmp(key, "udev_rules") == 0) {
211 free(udev->rules_path);
212 udev->rules_path = strdup(val);
213 util_remove_trailing_chars(udev->rules_path, '/');
220 env = getenv("UDEV_ROOT");
222 free(udev->dev_path);
223 udev->dev_path = strdup(env);
224 util_remove_trailing_chars(udev->dev_path, '/');
225 udev_add_property(udev, "UDEV_ROOT", udev->dev_path);
228 env = getenv("UDEV_LOG");
230 udev_set_log_priority(udev, util_log_priority(env));
232 if (udev->dev_path == NULL || udev->sys_path == NULL)
234 info(udev, "context %p created\n", udev);
235 info(udev, "log_priority=%d\n", udev->log_priority);
236 info(udev, "config_file='%s'\n", config_file);
237 info(udev, "dev_path='%s'\n", udev->dev_path);
238 info(udev, "sys_path='%s'\n", udev->sys_path);
239 if (udev->rules_path != NULL)
240 info(udev, "rules_path='%s'\n", udev->rules_path);
245 err(udev, "context creation failed\n");
252 * @udev: udev library context
254 * Take a reference of the udev library context.
256 * Returns: the passed udev library context
258 struct udev *udev_ref(struct udev *udev)
268 * @udev: udev library context
270 * Drop a reference of the udev library context. If the refcount
271 * reaches zero, the ressources of the context will be released.
274 void udev_unref(struct udev *udev)
279 if (udev->refcount > 0)
281 udev_list_cleanup_entries(udev, &udev->properties_list);
282 free(udev->dev_path);
283 free(udev->sys_path);
284 free(udev->rules_path);
285 info(udev, "context %p released\n", udev);
291 * @udev: udev library context
292 * @log_fn: function to be called for logging messages
294 * The built-in logging, which writes to stderr, it can be
295 * overridden by a custom function, to plug log messages
296 * into the users logging functionality.
299 void udev_set_log_fn(struct udev *udev,
300 void (*log_fn)(struct udev *udev,
301 int priority, const char *file, int line, const char *fn,
302 const char *format, va_list args))
304 udev->log_fn = log_fn;
305 info(udev, "custom logging function %p registered\n", udev);
308 int udev_get_log_priority(struct udev *udev)
310 return udev->log_priority;
313 void udev_set_log_priority(struct udev *udev, int priority)
317 udev->log_priority = priority;
318 snprintf(num, sizeof(num), "%u", udev->log_priority);
319 udev_add_property(udev, "UDEV_LOG", num);
322 const char *udev_get_rules_path(struct udev *udev)
324 return udev->rules_path;
327 int udev_get_run(struct udev *udev)
334 * @udev: udev library context
336 * Retrieve the sysfs mount point. The default is "/sys". For
337 * testing purposes, it can be overridden with the environment
338 * variable SYSFS_PATH.
340 * Returns: the sys mount point
342 const char *udev_get_sys_path(struct udev *udev)
346 return udev->sys_path;
351 * @udev: udev library context
353 * Retrieve the device directory path. The default value is "/dev",
354 * the actual value may be overridden in the udev configuration
357 * Returns: the device directory path
359 const char *udev_get_dev_path(struct udev *udev)
363 return udev->dev_path;
366 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
369 struct udev_list_entry *list_entry;
371 list_entry = udev_get_properties_list_entry(udev);
372 list_entry = udev_list_entry_get_by_name(list_entry, key);
373 if (list_entry != NULL)
374 udev_list_entry_remove(list_entry);
377 return udev_list_entry_add(udev, &udev->properties_list, key, value, 1, 0);
380 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
382 return udev_list_get_entry(&udev->properties_list);