2 This file is part of systemd.
4 Copyright 2008-2014 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "libudev-private.h"
36 * @short_description: libudev context
38 * The context contains the default values read from the udev config file,
39 * and is passed to all library operations.
45 * Opaque object representing the library context.
49 void (*log_fn)(struct udev *udev,
50 int priority, const char *file, int line, const char *fn,
51 const char *format, va_list args);
53 struct udev_list properties_list;
58 * @udev: udev library context
60 * Retrieve stored data pointer from library context. This might be useful
61 * to access from callbacks.
63 * Returns: stored userdata
65 _public_ void *udev_get_userdata(struct udev *udev) {
68 return udev->userdata;
73 * @udev: udev library context
74 * @userdata: data pointer
76 * Store custom @userdata in the library context.
78 _public_ void udev_set_userdata(struct udev *udev, void *userdata) {
81 udev->userdata = userdata;
87 * Create udev library context. This reads the udev configuration
88 * file, and fills in the default values.
90 * The initial refcount is 1, and needs to be decremented to
91 * release the resources of the udev library context.
93 * Returns: a new udev library context
95 _public_ struct udev *udev_new(void) {
97 _cleanup_fclose_ FILE *f = NULL;
99 udev = new0(struct udev, 1);
103 udev_list_init(udev, &udev->properties_list, true);
105 f = fopen("/etc/udev/udev.conf", "re");
107 char line[UTIL_LINE_SIZE];
108 unsigned line_nr = 0;
110 while (fgets(line, sizeof(line), f)) {
119 while (isspace(key[0]))
122 /* comment or empty line */
123 if (key[0] == '#' || key[0] == '\0')
126 /* split key/value */
127 val = strchr(key, '=');
129 log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.\n", line_nr);
136 while (isspace(val[0]))
143 while (isspace(key[len-1]))
147 /* terminate value */
151 while (isspace(val[len-1]))
159 if (val[0] == '"' || val[0] == '\'') {
160 if (val[len-1] != val[0]) {
161 log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.\n", line_nr);
168 if (streq(key, "udev_log")) {
171 prio = util_log_priority(val);
173 log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.\n", line_nr, val);
175 log_set_max_level(prio);
186 * @udev: udev library context
188 * Take a reference of the udev library context.
190 * Returns: the passed udev library context
192 _public_ struct udev *udev_ref(struct udev *udev) {
201 * @udev: udev library context
203 * Drop a reference of the udev library context. If the refcount
204 * reaches zero, the resources of the context will be released.
206 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
208 _public_ struct udev *udev_unref(struct udev *udev) {
212 if (udev->refcount > 0)
214 udev_list_cleanup(&udev->properties_list);
221 * @udev: udev library context
222 * @log_fn: function to be called for log messages
224 * This function is deprecated.
227 _public_ void udev_set_log_fn(struct udev *udev,
228 void (*log_fn)(struct udev *udev,
229 int priority, const char *file, int line, const char *fn,
230 const char *format, va_list args)) {
235 * udev_get_log_priority:
236 * @udev: udev library context
238 * This function is deprecated.
241 _public_ int udev_get_log_priority(struct udev *udev) {
242 return log_get_max_level();
246 * udev_set_log_priority:
247 * @udev: udev library context
248 * @priority: the new log priority
250 * This function is deprecated.
253 _public_ void udev_set_log_priority(struct udev *udev, int priority) {
254 log_set_max_level(priority);
257 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value) {
259 struct udev_list_entry *list_entry;
261 list_entry = udev_get_properties_list_entry(udev);
262 list_entry = udev_list_entry_get_by_name(list_entry, key);
263 if (list_entry != NULL)
264 udev_list_entry_delete(list_entry);
267 return udev_list_entry_add(&udev->properties_list, key, value);
270 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev) {
271 return udev_list_get_entry(&udev->properties_list);