2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
22 #include "libudev-private.h"
26 void (*log_fn)(struct udev *udev,
27 int priority, const char *file, int line, const char *fn,
28 const char *format, va_list args);
33 struct udev_list_node properties_list;
38 void udev_log(struct udev *udev,
39 int priority, const char *file, int line, const char *fn,
40 const char *format, ...)
44 if (priority > udev->log_priority)
47 va_start(args, format);
48 udev->log_fn(udev, priority, file, line, fn, format, args);
52 static void log_stderr(struct udev *udev,
53 int priority, const char *file, int line, const char *fn,
54 const char *format, va_list args)
56 fprintf(stderr, "libudev: %s: ", fn);
57 vfprintf(stderr, format, args);
60 void *udev_get_userdata(struct udev *udev)
64 return udev->userdata;
67 void udev_set_userdata(struct udev *udev, void *userdata)
71 udev->userdata = userdata;
77 * Create udev library context.
79 * The initial refcount is 1, and needs to be decremented to
80 * release the resources of the udev library context.
82 * Returns: a new udev library context
84 struct udev *udev_new(void)
91 udev = calloc(1, sizeof(struct udev));
95 udev->log_fn = log_stderr;
96 udev->log_priority = LOG_ERR;
97 udev_list_init(&udev->properties_list);
99 udev->dev_path = strdup(UDEV_PREFIX "/dev");
100 udev->sys_path = strdup("/sys");
101 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
102 if (udev->dev_path == NULL ||
103 udev->sys_path == NULL ||
107 /* settings by environment and config file */
108 env = getenv("SYSFS_PATH");
110 free(udev->sys_path);
111 udev->sys_path = strdup(env);
112 util_remove_trailing_chars(udev->sys_path, '/');
113 udev_add_property(udev, "SYSFS_PATH", udev->sys_path);
116 env = getenv("UDEV_RUN");
117 if (env != NULL && strcmp(env, "0") == 0)
120 env = getenv("UDEV_CONFIG_FILE");
123 config_file = strdup(env);
124 util_remove_trailing_chars(config_file, '/');
126 if (config_file == NULL)
128 f = fopen(config_file, "r");
130 char line[UTIL_LINE_SIZE];
133 while (fgets(line, sizeof(line), f)) {
142 while (isspace(key[0]))
145 /* comment or empty line */
146 if (key[0] == '#' || key[0] == '\0')
149 /* split key/value */
150 val = strchr(key, '=');
152 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
159 while (isspace(val[0]))
166 while (isspace(key[len-1]))
170 /* terminate value */
174 while (isspace(val[len-1]))
182 if (val[0] == '"' || val[0] == '\'') {
183 if (val[len-1] != val[0]) {
184 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
191 if (strcasecmp(key, "udev_log") == 0) {
192 udev_set_log_priority(udev, util_log_priority(val));
195 if (strcasecmp(key, "udev_root") == 0) {
196 free(udev->dev_path);
197 udev->dev_path = strdup(val);
198 util_remove_trailing_chars(udev->dev_path, '/');
201 if (strcasecmp(key, "udev_rules") == 0) {
202 free(udev->rules_path);
203 udev->rules_path = strdup(val);
204 util_remove_trailing_chars(udev->rules_path, '/');
211 env = getenv("UDEV_ROOT");
213 free(udev->dev_path);
214 udev->dev_path = strdup(env);
215 util_remove_trailing_chars(udev->dev_path, '/');
216 udev_add_property(udev, "UDEV_ROOT", udev->dev_path);
219 env = getenv("UDEV_LOG");
221 udev_set_log_priority(udev, util_log_priority(env));
223 if (udev->dev_path == NULL || udev->sys_path == NULL)
225 dbg(udev, "context %p created\n", udev);
226 dbg(udev, "log_priority=%d\n", udev->log_priority);
227 dbg(udev, "config_file='%s'\n", config_file);
228 dbg(udev, "dev_path='%s'\n", udev->dev_path);
229 dbg(udev, "sys_path='%s'\n", udev->sys_path);
230 if (udev->rules_path != NULL)
231 dbg(udev, "rules_path='%s'\n", udev->rules_path);
236 err(udev, "context creation failed\n");
243 * @udev: udev library context
245 * Take a reference of the udev library context.
247 * Returns: the passed udev library context
249 struct udev *udev_ref(struct udev *udev)
259 * @udev: udev library context
261 * Drop a reference of the udev library context. If the refcount
262 * reaches zero, the resources of the context will be released.
265 void udev_unref(struct udev *udev)
270 if (udev->refcount > 0)
272 udev_list_cleanup_entries(udev, &udev->properties_list);
273 free(udev->dev_path);
274 free(udev->sys_path);
275 free(udev->rules_path);
276 dbg(udev, "context %p released\n", udev);
282 * @udev: udev library context
283 * @log_fn: function to be called for logging messages
285 * The built-in logging writes to stderr. It can be
286 * overridden by a custom function, to plug log messages
287 * into the users' logging functionality.
290 void udev_set_log_fn(struct udev *udev,
291 void (*log_fn)(struct udev *udev,
292 int priority, const char *file, int line, const char *fn,
293 const char *format, va_list args))
295 udev->log_fn = log_fn;
296 info(udev, "custom logging function %p registered\n", udev);
299 int udev_get_log_priority(struct udev *udev)
301 return udev->log_priority;
304 void udev_set_log_priority(struct udev *udev, int priority)
308 udev->log_priority = priority;
309 snprintf(num, sizeof(num), "%u", udev->log_priority);
310 udev_add_property(udev, "UDEV_LOG", num);
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;
357 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
360 struct udev_list_entry *list_entry;
362 list_entry = udev_get_properties_list_entry(udev);
363 list_entry = udev_list_entry_get_by_name(list_entry, key);
364 if (list_entry != NULL)
365 udev_list_entry_delete(list_entry);
368 return udev_list_entry_add(udev, &udev->properties_list, key, value, 1, 0);
371 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
373 return udev_list_get_entry(&udev->properties_list);