2 This file is part of systemd.
4 Copyright 2008-2012 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;
57 void udev_log(struct udev *udev,
58 int priority, const char *file, int line, const char *fn,
59 const char *format, ...)
63 va_start(args, format);
64 udev->log_fn(udev, priority, file, line, fn, format, args);
69 static void log_stderr(struct udev *udev,
70 int priority, const char *file, int line, const char *fn,
71 const char *format, va_list args)
73 fprintf(stderr, "libudev: %s: ", fn);
74 vfprintf(stderr, format, args);
79 * @udev: udev library context
81 * Retrieve stored data pointer from library context. This might be useful
82 * to access from callbacks like a custom logging function.
84 * Returns: stored userdata
86 _public_ void *udev_get_userdata(struct udev *udev)
90 return udev->userdata;
95 * @udev: udev library context
96 * @userdata: data pointer
98 * Store custom @userdata in the library context.
100 _public_ void udev_set_userdata(struct udev *udev, void *userdata)
104 udev->userdata = userdata;
110 * Create udev library context. This reads the udev configuration
111 * file, and fills in the default values.
113 * The initial refcount is 1, and needs to be decremented to
114 * release the resources of the udev library context.
116 * Returns: a new udev library context
118 _public_ struct udev *udev_new(void)
122 _cleanup_free_ FILE *f = NULL;
124 udev = new0(struct udev, 1);
128 udev->log_fn = log_stderr;
129 udev->log_priority = LOG_INFO;
130 udev_list_init(udev, &udev->properties_list, true);
132 f = fopen("/etc/udev/udev.conf", "re");
134 char line[UTIL_LINE_SIZE];
135 unsigned line_nr = 0;
137 while (fgets(line, sizeof(line), f)) {
146 while (isspace(key[0]))
149 /* comment or empty line */
150 if (key[0] == '#' || key[0] == '\0')
153 /* split key/value */
154 val = strchr(key, '=');
156 udev_err(udev, "/etc/udev/udev.conf:%u: missing assignment, skipping line.\n", line_nr);
163 while (isspace(val[0]))
170 while (isspace(key[len-1]))
174 /* terminate value */
178 while (isspace(val[len-1]))
186 if (val[0] == '"' || val[0] == '\'') {
187 if (val[len-1] != val[0]) {
188 udev_err(udev, "/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.\n", line_nr);
195 if (streq(key, "udev_log")) {
198 prio = util_log_priority(val);
200 udev_err(udev, "/etc/udev/udev.conf:%u: invalid logging level '%s', ignoring.\n", line_nr, val);
202 udev_set_log_priority(udev, prio);
208 /* environment overrides config */
209 env = secure_getenv("UDEV_LOG");
213 prio = util_log_priority(env);
215 udev_err(udev, "$UDEV_LOG specifies invalid logging level '%s', ignoring.\n", env);
217 udev_set_log_priority(udev, prio);
225 * @udev: udev library context
227 * Take a reference of the udev library context.
229 * Returns: the passed udev library context
231 _public_ struct udev *udev_ref(struct udev *udev)
241 * @udev: udev library context
243 * Drop a reference of the udev library context. If the refcount
244 * reaches zero, the resources of the context will be released.
246 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
248 _public_ struct udev *udev_unref(struct udev *udev)
253 if (udev->refcount > 0)
255 udev_list_cleanup(&udev->properties_list);
262 * @udev: udev library context
263 * @log_fn: function to be called for logging messages
265 * The built-in logging writes to stderr. It can be
266 * overridden by a custom function, to plug log messages
267 * into the users' logging functionality.
270 _public_ void udev_set_log_fn(struct udev *udev,
271 void (*log_fn)(struct udev *udev,
272 int priority, const char *file, int line, const char *fn,
273 const char *format, va_list args))
275 udev->log_fn = log_fn;
276 udev_dbg(udev, "custom logging function %p registered\n", log_fn);
280 * udev_get_log_priority:
281 * @udev: udev library context
283 * The initial logging priority is read from the udev config file
286 * Returns: the current logging priority
288 _public_ int udev_get_log_priority(struct udev *udev)
290 return udev->log_priority;
294 * udev_set_log_priority:
295 * @udev: udev library context
296 * @priority: the new logging priority
298 * Set the current logging priority. The value controls which messages
301 _public_ 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 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
313 struct udev_list_entry *list_entry;
315 list_entry = udev_get_properties_list_entry(udev);
316 list_entry = udev_list_entry_get_by_name(list_entry, key);
317 if (list_entry != NULL)
318 udev_list_entry_delete(list_entry);
321 return udev_list_entry_add(&udev->properties_list, key, value);
324 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
326 return udev_list_get_entry(&udev->properties_list);