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 va_start(args, format);
45 udev->log_fn(udev, priority, file, line, fn, format, args);
49 static void log_stderr(struct udev *udev,
50 int priority, const char *file, int line, const char *fn,
51 const char *format, va_list args)
53 fprintf(stderr, "libudev: %s: ", fn);
54 vfprintf(stderr, format, args);
57 void *udev_get_userdata(struct udev *udev)
61 return udev->userdata;
64 void udev_set_userdata(struct udev *udev, void *userdata)
68 udev->userdata = userdata;
74 * Create udev library context.
76 * The initial refcount is 1, and needs to be decremented to
77 * release the resources of the udev library context.
79 * Returns: a new udev library context
81 struct udev *udev_new(void)
88 udev = calloc(1, sizeof(struct udev));
92 udev->log_fn = log_stderr;
93 udev->log_priority = LOG_ERR;
94 udev_list_init(&udev->properties_list);
96 udev->dev_path = strdup(UDEV_PREFIX "/dev");
97 udev->sys_path = strdup("/sys");
98 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
99 if (udev->dev_path == NULL ||
100 udev->sys_path == NULL ||
104 /* settings by environment and config file */
105 env = getenv("SYSFS_PATH");
107 free(udev->sys_path);
108 udev->sys_path = strdup(env);
109 util_remove_trailing_chars(udev->sys_path, '/');
110 udev_add_property(udev, "SYSFS_PATH", udev->sys_path);
113 env = getenv("UDEV_RUN");
114 if (env != NULL && strcmp(env, "0") == 0)
117 env = getenv("UDEV_CONFIG_FILE");
120 config_file = strdup(env);
121 util_remove_trailing_chars(config_file, '/');
123 if (config_file == NULL)
125 f = fopen(config_file, "r");
127 char line[UTIL_LINE_SIZE];
130 while (fgets(line, sizeof(line), f)) {
139 while (isspace(key[0]))
142 /* comment or empty line */
143 if (key[0] == '#' || key[0] == '\0')
146 /* split key/value */
147 val = strchr(key, '=');
149 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
156 while (isspace(val[0]))
163 while (isspace(key[len-1]))
167 /* terminate value */
171 while (isspace(val[len-1]))
179 if (val[0] == '"' || val[0] == '\'') {
180 if (val[len-1] != val[0]) {
181 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
188 if (strcmp(key, "udev_log") == 0) {
189 udev_set_log_priority(udev, util_log_priority(val));
192 if (strcmp(key, "udev_root") == 0) {
193 free(udev->dev_path);
194 udev->dev_path = strdup(val);
195 util_remove_trailing_chars(udev->dev_path, '/');
198 if (strcmp(key, "udev_rules") == 0) {
199 free(udev->rules_path);
200 udev->rules_path = strdup(val);
201 util_remove_trailing_chars(udev->rules_path, '/');
208 env = getenv("UDEV_ROOT");
210 free(udev->dev_path);
211 udev->dev_path = strdup(env);
212 util_remove_trailing_chars(udev->dev_path, '/');
213 udev_add_property(udev, "UDEV_ROOT", udev->dev_path);
216 env = getenv("UDEV_LOG");
218 udev_set_log_priority(udev, util_log_priority(env));
220 if (udev->dev_path == NULL || udev->sys_path == NULL)
222 dbg(udev, "context %p created\n", udev);
223 dbg(udev, "log_priority=%d\n", udev->log_priority);
224 dbg(udev, "config_file='%s'\n", config_file);
225 dbg(udev, "dev_path='%s'\n", udev->dev_path);
226 dbg(udev, "sys_path='%s'\n", udev->sys_path);
227 if (udev->rules_path != NULL)
228 dbg(udev, "rules_path='%s'\n", udev->rules_path);
233 err(udev, "context creation failed\n");
240 * @udev: udev library context
242 * Take a reference of the udev library context.
244 * Returns: the passed udev library context
246 struct udev *udev_ref(struct udev *udev)
256 * @udev: udev library context
258 * Drop a reference of the udev library context. If the refcount
259 * reaches zero, the resources of the context will be released.
262 void udev_unref(struct udev *udev)
267 if (udev->refcount > 0)
269 udev_list_cleanup_entries(udev, &udev->properties_list);
270 free(udev->dev_path);
271 free(udev->sys_path);
272 free(udev->rules_path);
273 dbg(udev, "context %p released\n", udev);
279 * @udev: udev library context
280 * @log_fn: function to be called for logging messages
282 * The built-in logging writes to stderr. It can be
283 * overridden by a custom function, to plug log messages
284 * into the users' logging functionality.
287 void udev_set_log_fn(struct udev *udev,
288 void (*log_fn)(struct udev *udev,
289 int priority, const char *file, int line, const char *fn,
290 const char *format, va_list args))
292 udev->log_fn = log_fn;
293 info(udev, "custom logging function %p registered\n", udev);
296 int udev_get_log_priority(struct udev *udev)
298 return udev->log_priority;
301 void udev_set_log_priority(struct udev *udev, int priority)
305 udev->log_priority = priority;
306 snprintf(num, sizeof(num), "%u", udev->log_priority);
307 udev_add_property(udev, "UDEV_LOG", num);
310 const char *udev_get_rules_path(struct udev *udev)
312 return udev->rules_path;
315 int udev_get_run(struct udev *udev)
322 * @udev: udev library context
324 * Retrieve the sysfs mount point. The default is "/sys". For
325 * testing purposes, it can be overridden with the environment
326 * variable SYSFS_PATH.
328 * Returns: the sys mount point
330 const char *udev_get_sys_path(struct udev *udev)
334 return udev->sys_path;
339 * @udev: udev library context
341 * Retrieve the device directory path. The default value is "/dev",
342 * the actual value may be overridden in the udev configuration
345 * Returns: the device directory path
347 const char *udev_get_dev_path(struct udev *udev)
351 return udev->dev_path;
354 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
357 struct udev_list_entry *list_entry;
359 list_entry = udev_get_properties_list_entry(udev);
360 list_entry = udev_list_entry_get_by_name(list_entry, key);
361 if (list_entry != NULL)
362 udev_list_entry_delete(list_entry);
365 return udev_list_entry_add(udev, &udev->properties_list, key, value, 1, 0);
368 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
370 return udev_list_get_entry(&udev->properties_list);