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/>.
28 #include "libudev-private.h"
33 * @short_description: libudev context
35 * The context contains the default values read from the udev config file,
36 * and is passed to all library operations.
42 * Opaque object representing the library context.
46 void (*log_fn)(struct udev *udev,
47 int priority, const char *file, int line, const char *fn,
48 const char *format, va_list args);
54 * @udev: udev library context
56 * Retrieve stored data pointer from library context. This might be useful
57 * to access from callbacks.
59 * Returns: stored userdata
61 _public_ void *udev_get_userdata(struct udev *udev) {
64 return udev->userdata;
69 * @udev: udev library context
70 * @userdata: data pointer
72 * Store custom @userdata in the library context.
74 _public_ void udev_set_userdata(struct udev *udev, void *userdata) {
77 udev->userdata = userdata;
83 * Create udev library context. This reads the udev configuration
84 * file, and fills in the default values.
86 * The initial refcount is 1, and needs to be decremented to
87 * release the resources of the udev library context.
89 * Returns: a new udev library context
91 _public_ struct udev *udev_new(void) {
93 _cleanup_fclose_ FILE *f = NULL;
95 udev = new0(struct udev, 1);
100 f = fopen("/etc/udev/udev.conf", "re");
102 char line[UTIL_LINE_SIZE];
103 unsigned line_nr = 0;
105 while (fgets(line, sizeof(line), f)) {
114 while (isspace(key[0]))
117 /* comment or empty line */
118 if (key[0] == '#' || key[0] == '\0')
121 /* split key/value */
122 val = strchr(key, '=');
124 log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", line_nr);
131 while (isspace(val[0]))
138 while (isspace(key[len-1]))
142 /* terminate value */
146 while (isspace(val[len-1]))
154 if (val[0] == '"' || val[0] == '\'') {
155 if (val[len-1] != val[0]) {
156 log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr);
163 if (streq(key, "udev_log")) {
166 prio = util_log_priority(val);
168 log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr, val);
170 log_set_max_level(prio);
181 * @udev: udev library context
183 * Take a reference of the udev library context.
185 * Returns: the passed udev library context
187 _public_ struct udev *udev_ref(struct udev *udev) {
196 * @udev: udev library context
198 * Drop a reference of the udev library context. If the refcount
199 * reaches zero, the resources of the context will be released.
201 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
203 _public_ struct udev *udev_unref(struct udev *udev) {
207 if (udev->refcount > 0)
215 * @udev: udev library context
216 * @log_fn: function to be called for log messages
218 * This function is deprecated.
221 _public_ void udev_set_log_fn(struct udev *udev,
222 void (*log_fn)(struct udev *udev,
223 int priority, const char *file, int line, const char *fn,
224 const char *format, va_list args)) {
229 * udev_get_log_priority:
230 * @udev: udev library context
232 * This function is deprecated.
235 _public_ int udev_get_log_priority(struct udev *udev) {
236 return log_get_max_level();
240 * udev_set_log_priority:
241 * @udev: udev library context
242 * @priority: the new log priority
244 * This function is deprecated.
247 _public_ void udev_set_log_priority(struct udev *udev, int priority) {
248 log_set_max_level(priority);