]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev.c
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/>.
29 #include "libudev-private.h"
31 #include "string-util.h"
35 * @short_description: libudev context
37 * The context contains the default values read from the udev config file,
38 * and is passed to all library operations.
44 * Opaque object representing the library context.
48 void (*log_fn
)(struct udev
*udev
,
49 int priority
, const char *file
, int line
, const char *fn
,
50 const char *format
, va_list args
);
56 * @udev: udev library context
58 * Retrieve stored data pointer from library context. This might be useful
59 * to access from callbacks.
61 * Returns: stored userdata
63 _public_
void *udev_get_userdata(struct udev
*udev
) {
66 return udev
->userdata
;
71 * @udev: udev library context
72 * @userdata: data pointer
74 * Store custom @userdata in the library context.
76 _public_
void udev_set_userdata(struct udev
*udev
, void *userdata
) {
79 udev
->userdata
= userdata
;
85 * Create udev library context. This reads the udev configuration
86 * file, and fills in the default values.
88 * The initial refcount is 1, and needs to be decremented to
89 * release the resources of the udev library context.
91 * Returns: a new udev library context
93 _public_
struct udev
*udev_new(void) {
95 _cleanup_fclose_
FILE *f
= NULL
;
97 udev
= new0(struct udev
, 1);
102 f
= fopen("/etc/udev/udev.conf", "re");
104 char line
[UTIL_LINE_SIZE
];
105 unsigned line_nr
= 0;
107 while (fgets(line
, sizeof(line
), f
)) {
116 while (isspace(key
[0]))
119 /* comment or empty line */
120 if (key
[0] == '#' || key
[0] == '\0')
123 /* split key/value */
124 val
= strchr(key
, '=');
126 log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", line_nr
);
133 while (isspace(val
[0]))
140 while (isspace(key
[len
-1]))
144 /* terminate value */
148 while (isspace(val
[len
-1]))
156 if (val
[0] == '"' || val
[0] == '\'') {
157 if (val
[len
-1] != val
[0]) {
158 log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr
);
165 if (streq(key
, "udev_log")) {
168 prio
= util_log_priority(val
);
170 log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr
, val
);
172 log_set_max_level(prio
);
183 * @udev: udev library context
185 * Take a reference of the udev library context.
187 * Returns: the passed udev library context
189 _public_
struct udev
*udev_ref(struct udev
*udev
) {
198 * @udev: udev library context
200 * Drop a reference of the udev library context. If the refcount
201 * reaches zero, the resources of the context will be released.
203 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
205 _public_
struct udev
*udev_unref(struct udev
*udev
) {
209 if (udev
->refcount
> 0)
217 * @udev: udev library context
218 * @log_fn: function to be called for log messages
220 * This function is deprecated.
223 _public_
void udev_set_log_fn(struct udev
*udev
,
224 void (*log_fn
)(struct udev
*udev
,
225 int priority
, const char *file
, int line
, const char *fn
,
226 const char *format
, va_list args
)) {
231 * udev_get_log_priority:
232 * @udev: udev library context
234 * This function is deprecated.
237 _public_
int udev_get_log_priority(struct udev
*udev
) {
238 return log_get_max_level();
242 * udev_set_log_priority:
243 * @udev: udev library context
244 * @priority: the new log priority
246 * This function is deprecated.
249 _public_
void udev_set_log_priority(struct udev
*udev
, int priority
) {
250 log_set_max_level(priority
);