2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 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.
23 #include "libudev-private.h"
27 * @short_description: libudev context
29 * The context contains the default values read from the udev config file,
30 * and is passed to all library operations.
36 * Opaque object representing the library context.
40 void (*log_fn)(struct udev *udev,
41 int priority, const char *file, int line, const char *fn,
42 const char *format, va_list args);
47 unsigned long long rules_path_ts[4];
50 struct udev_list properties_list;
54 void udev_log(struct udev *udev,
55 int priority, const char *file, int line, const char *fn,
56 const char *format, ...)
60 va_start(args, format);
61 udev->log_fn(udev, priority, file, line, fn, format, args);
65 static void log_stderr(struct udev *udev,
66 int priority, const char *file, int line, const char *fn,
67 const char *format, va_list args)
69 fprintf(stderr, "libudev: %s: ", fn);
70 vfprintf(stderr, format, args);
75 * @udev: udev library context
77 * Retrieve stored data pointer from library context. This might be useful
78 * to access from callbacks like a custom logging function.
80 * Returns: stored userdata
82 UDEV_EXPORT void *udev_get_userdata(struct udev *udev)
86 return udev->userdata;
91 * @udev: udev library context
92 * @userdata: data pointer
94 * Store custom @userdata in the library context.
96 UDEV_EXPORT void udev_set_userdata(struct udev *udev, void *userdata)
100 udev->userdata = userdata;
103 static char *set_value(char **s, const char *v)
107 util_remove_trailing_chars(*s, '/');
114 * Create udev library context. This reads the udev configuration
115 * file, and fills in the default values.
117 * The initial refcount is 1, and needs to be decremented to
118 * release the resources of the udev library context.
120 * Returns: a new udev library context
122 UDEV_EXPORT struct udev *udev_new(void)
126 char *config_file = NULL;
129 udev = calloc(1, sizeof(struct udev));
133 udev->log_fn = log_stderr;
134 udev->log_priority = LOG_ERR;
135 udev_list_init(udev, &udev->properties_list, true);
137 /* custom config file */
138 env = getenv("UDEV_CONFIG_FILE");
140 if (set_value(&config_file, env) == NULL)
142 udev_add_property(udev, "UDEV_CONFIG_FILE", config_file);
145 /* default config file */
146 if (config_file == NULL)
147 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
148 if (config_file == NULL)
151 f = fopen(config_file, "re");
153 char line[UTIL_LINE_SIZE];
156 while (fgets(line, sizeof(line), f)) {
165 while (isspace(key[0]))
168 /* comment or empty line */
169 if (key[0] == '#' || key[0] == '\0')
172 /* split key/value */
173 val = strchr(key, '=');
175 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
182 while (isspace(val[0]))
189 while (isspace(key[len-1]))
193 /* terminate value */
197 while (isspace(val[len-1]))
205 if (val[0] == '"' || val[0] == '\'') {
206 if (val[len-1] != val[0]) {
207 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
214 if (strcmp(key, "udev_log") == 0) {
215 udev_set_log_priority(udev, util_log_priority(val));
218 if (strcmp(key, "udev_root") == 0) {
219 set_value(&udev->dev_path, val);
222 if (strcmp(key, "udev_run") == 0) {
223 set_value(&udev->run_path, val);
226 if (strcmp(key, "udev_sys") == 0) {
227 set_value(&udev->sys_path, val);
230 if (strcmp(key, "udev_rules") == 0) {
231 set_value(&udev->rules_path[0], val);
232 udev->rules_path_count = 1;
239 /* environment overrides config */
240 env = getenv("UDEV_LOG");
242 udev_set_log_priority(udev, util_log_priority(env));
245 if (udev->dev_path == NULL)
246 if (set_value(&udev->dev_path, "/dev") == NULL)
249 if (udev->sys_path == NULL)
250 if (set_value(&udev->sys_path, "/sys") == NULL)
253 if (udev->run_path == NULL)
254 if (set_value(&udev->run_path, "/run/udev") == NULL)
257 if (udev->rules_path[0] == NULL) {
258 /* /usr/lib/udev -- system rules */
259 udev->rules_path[0] = strdup(PKGLIBEXECDIR "/rules.d");
260 if (!udev->rules_path[0])
263 /* /run/udev -- runtime rules */
264 if (asprintf(&udev->rules_path[2], "%s/rules.d", udev->run_path) < 0)
267 /* /etc/udev -- local administration rules */
268 udev->rules_path[1] = strdup(SYSCONFDIR "/udev/rules.d");
269 if (!udev->rules_path[1])
272 udev->rules_path_count = 3;
275 dbg(udev, "context %p created\n", udev);
276 dbg(udev, "log_priority=%d\n", udev->log_priority);
277 dbg(udev, "config_file='%s'\n", config_file);
278 dbg(udev, "dev_path='%s'\n", udev->dev_path);
279 dbg(udev, "sys_path='%s'\n", udev->sys_path);
280 dbg(udev, "run_path='%s'\n", udev->run_path);
281 dbg(udev, "rules_path='%s':'%s':'%s'\n", udev->rules_path[0], udev->rules_path[1], udev->rules_path[2]);
286 err(udev, "context creation failed\n");
293 * @udev: udev library context
295 * Take a reference of the udev library context.
297 * Returns: the passed udev library context
299 UDEV_EXPORT struct udev *udev_ref(struct udev *udev)
309 * @udev: udev library context
311 * Drop a reference of the udev library context. If the refcount
312 * reaches zero, the resources of the context will be released.
315 UDEV_EXPORT void udev_unref(struct udev *udev)
320 if (udev->refcount > 0)
322 udev_list_cleanup(&udev->properties_list);
323 free(udev->dev_path);
324 free(udev->sys_path);
325 free(udev->rules_path[0]);
326 free(udev->rules_path[1]);
327 free(udev->rules_path[2]);
328 free(udev->run_path);
329 dbg(udev, "context %p released\n", udev);
335 * @udev: udev library context
336 * @log_fn: function to be called for logging messages
338 * The built-in logging writes to stderr. It can be
339 * overridden by a custom function, to plug log messages
340 * into the users' logging functionality.
343 UDEV_EXPORT void udev_set_log_fn(struct udev *udev,
344 void (*log_fn)(struct udev *udev,
345 int priority, const char *file, int line, const char *fn,
346 const char *format, va_list args))
348 udev->log_fn = log_fn;
349 info(udev, "custom logging function %p registered\n", log_fn);
353 * udev_get_log_priority:
354 * @udev: udev library context
356 * The initial logging priority is read from the udev config file
359 * Returns: the current logging priority
361 UDEV_EXPORT int udev_get_log_priority(struct udev *udev)
363 return udev->log_priority;
367 * udev_set_log_priority:
368 * @udev: udev library context
369 * @priority: the new logging priority
371 * Set the current logging priority. The value controls which messages
374 UDEV_EXPORT void udev_set_log_priority(struct udev *udev, int priority)
378 udev->log_priority = priority;
379 snprintf(num, sizeof(num), "%u", udev->log_priority);
380 udev_add_property(udev, "UDEV_LOG", num);
383 int udev_get_rules_path(struct udev *udev, char **path[], unsigned long long *stamp_usec[])
385 *path = udev->rules_path;
387 *stamp_usec = udev->rules_path_ts;
388 return udev->rules_path_count;
393 * @udev: udev library context
395 * Retrieve the sysfs mount point. The default is "/sys". For
396 * testing purposes, it can be overridden with udev_sys=
397 * in the udev configuration file.
399 * Returns: the sys mount point
401 UDEV_EXPORT const char *udev_get_sys_path(struct udev *udev)
405 return udev->sys_path;
410 * @udev: udev library context
412 * Retrieve the device directory path. The default value is "/dev",
413 * the actual value may be overridden in the udev configuration
416 * Returns: the device directory path
418 UDEV_EXPORT const char *udev_get_dev_path(struct udev *udev)
422 return udev->dev_path;
427 * @udev: udev library context
429 * Retrieve the udev runtime directory path. The default is "/run/udev".
431 * Returns: the runtime directory path
433 UDEV_EXPORT const char *udev_get_run_path(struct udev *udev)
437 return udev->run_path;
440 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
443 struct udev_list_entry *list_entry;
445 list_entry = udev_get_properties_list_entry(udev);
446 list_entry = udev_list_entry_get_by_name(list_entry, key);
447 if (list_entry != NULL)
448 udev_list_entry_delete(list_entry);
451 return udev_list_entry_add(&udev->properties_list, key, value);
454 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
456 return udev_list_get_entry(&udev->properties_list);