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"
28 * @short_description: libudev context
30 * The context contains the default values read from the udev config file,
31 * and is passed to all library operations.
37 * Opaque object representing the library context.
41 void (*log_fn)(struct udev *udev,
42 int priority, const char *file, int line, const char *fn,
43 const char *format, va_list args);
45 struct udev_list properties_list;
49 void udev_log(struct udev *udev,
50 int priority, const char *file, int line, const char *fn,
51 const char *format, ...)
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);
70 * @udev: udev library context
72 * Retrieve stored data pointer from library context. This might be useful
73 * to access from callbacks like a custom logging function.
75 * Returns: stored userdata
77 _public_ void *udev_get_userdata(struct udev *udev)
81 return udev->userdata;
86 * @udev: udev library context
87 * @userdata: data pointer
89 * Store custom @userdata in the library context.
91 _public_ void udev_set_userdata(struct udev *udev, void *userdata)
95 udev->userdata = userdata;
101 * Create udev library context. This reads the udev configuration
102 * file, and fills in the default values.
104 * The initial refcount is 1, and needs to be decremented to
105 * release the resources of the udev library context.
107 * Returns: a new udev library context
109 _public_ struct udev *udev_new(void)
115 udev = calloc(1, sizeof(struct udev));
119 udev->log_fn = log_stderr;
120 udev->log_priority = LOG_ERR;
121 udev_list_init(udev, &udev->properties_list, true);
123 f = fopen(SYSCONFDIR "/udev/udev.conf", "re");
125 char line[UTIL_LINE_SIZE];
128 while (fgets(line, sizeof(line), f)) {
137 while (isspace(key[0]))
140 /* comment or empty line */
141 if (key[0] == '#' || key[0] == '\0')
144 /* split key/value */
145 val = strchr(key, '=');
147 udev_err(udev, "missing <key>=<value> in " SYSCONFDIR "/udev/udev.conf[%i]; skip line\n", line_nr);
154 while (isspace(val[0]))
161 while (isspace(key[len-1]))
165 /* terminate value */
169 while (isspace(val[len-1]))
177 if (val[0] == '"' || val[0] == '\'') {
178 if (val[len-1] != val[0]) {
179 udev_err(udev, "inconsistent quoting in " SYSCONFDIR "/udev/udev.conf[%i]; skip line\n", line_nr);
186 if (strcmp(key, "udev_log") == 0) {
187 udev_set_log_priority(udev, util_log_priority(val));
194 /* environment overrides config */
195 env = secure_getenv("UDEV_LOG");
197 udev_set_log_priority(udev, util_log_priority(env));
204 * @udev: udev library context
206 * Take a reference of the udev library context.
208 * Returns: the passed udev library context
210 _public_ struct udev *udev_ref(struct udev *udev)
220 * @udev: udev library context
222 * Drop a reference of the udev library context. If the refcount
223 * reaches zero, the resources of the context will be released.
225 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
227 _public_ struct udev *udev_unref(struct udev *udev)
232 if (udev->refcount > 0)
234 udev_list_cleanup(&udev->properties_list);
241 * @udev: udev library context
242 * @log_fn: function to be called for logging messages
244 * The built-in logging writes to stderr. It can be
245 * overridden by a custom function, to plug log messages
246 * into the users' logging functionality.
249 _public_ void udev_set_log_fn(struct udev *udev,
250 void (*log_fn)(struct udev *udev,
251 int priority, const char *file, int line, const char *fn,
252 const char *format, va_list args))
254 udev->log_fn = log_fn;
255 udev_dbg(udev, "custom logging function %p registered\n", log_fn);
259 * udev_get_log_priority:
260 * @udev: udev library context
262 * The initial logging priority is read from the udev config file
265 * Returns: the current logging priority
267 _public_ int udev_get_log_priority(struct udev *udev)
269 return udev->log_priority;
273 * udev_set_log_priority:
274 * @udev: udev library context
275 * @priority: the new logging priority
277 * Set the current logging priority. The value controls which messages
280 _public_ void udev_set_log_priority(struct udev *udev, int priority)
284 udev->log_priority = priority;
285 snprintf(num, sizeof(num), "%u", udev->log_priority);
286 udev_add_property(udev, "UDEV_LOG", num);
289 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
292 struct udev_list_entry *list_entry;
294 list_entry = udev_get_properties_list_entry(udev);
295 list_entry = udev_list_entry_get_by_name(list_entry, key);
296 if (list_entry != NULL)
297 udev_list_entry_delete(list_entry);
300 return udev_list_entry_add(&udev->properties_list, key, value);
303 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
305 return udev_list_get_entry(&udev->properties_list);