]>
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 "alloc-util.h"
31 #include "libudev-private.h"
33 #include "string-util.h"
37 * @short_description: libudev context
39 * The context contains the default values read from the udev config file,
40 * and is passed to all library operations.
46 * Opaque object representing the library context.
50 void (*log_fn
)(struct udev
*udev
,
51 int priority
, const char *file
, int line
, const char *fn
,
52 const char *format
, va_list args
);
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);
104 f
= fopen("/etc/udev/udev.conf", "re");
106 char line
[UTIL_LINE_SIZE
];
107 unsigned line_nr
= 0;
109 while (fgets(line
, sizeof(line
), f
)) {
118 while (isspace(key
[0]))
121 /* comment or empty line */
122 if (key
[0] == '#' || key
[0] == '\0')
125 /* split key/value */
126 val
= strchr(key
, '=');
128 log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", line_nr
);
135 while (isspace(val
[0]))
142 while (isspace(key
[len
-1]))
146 /* terminate value */
150 while (isspace(val
[len
-1]))
158 if (val
[0] == '"' || val
[0] == '\'') {
159 if (val
[len
-1] != val
[0]) {
160 log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr
);
167 if (streq(key
, "udev_log")) {
170 prio
= util_log_priority(val
);
172 log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr
, val
);
174 log_set_max_level(prio
);
185 * @udev: udev library context
187 * Take a reference of the udev library context.
189 * Returns: the passed udev library context
191 _public_
struct udev
*udev_ref(struct udev
*udev
) {
200 * @udev: udev library context
202 * Drop a reference of the udev library context. If the refcount
203 * reaches zero, the resources of the context will be released.
205 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
207 _public_
struct udev
*udev_unref(struct udev
*udev
) {
211 if (udev
->refcount
> 0)
219 * @udev: udev library context
220 * @log_fn: function to be called for log messages
222 * This function is deprecated.
225 _public_
void udev_set_log_fn(struct udev
*udev
,
226 void (*log_fn
)(struct udev
*udev
,
227 int priority
, const char *file
, int line
, const char *fn
,
228 const char *format
, va_list args
)) {
233 * udev_get_log_priority:
234 * @udev: udev library context
236 * This function is deprecated.
239 _public_
int udev_get_log_priority(struct udev
*udev
) {
240 return log_get_max_level();
244 * udev_set_log_priority:
245 * @udev: udev library context
246 * @priority: the new log priority
248 * This function is deprecated.
251 _public_
void udev_set_log_priority(struct udev
*udev
, int priority
) {
252 log_set_max_level(priority
);