2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
23 #include "libudev-private.h"
27 * @short_description: libudev context
29 * The context contains the default values read from the udev config file,
30 * and is passed to all library operations.
36 * Opaque object representing the library context.
40 void (*log_fn)(struct udev *udev,
41 int priority, const char *file, int line, const char *fn,
42 const char *format, va_list args);
44 struct udev_list properties_list;
48 void udev_log(struct udev *udev,
49 int priority, const char *file, int line, const char *fn,
50 const char *format, ...)
54 va_start(args, format);
55 udev->log_fn(udev, priority, file, line, fn, format, args);
59 static void log_stderr(struct udev *udev,
60 int priority, const char *file, int line, const char *fn,
61 const char *format, va_list args)
63 fprintf(stderr, "libudev: %s: ", fn);
64 vfprintf(stderr, format, args);
69 * @udev: udev library context
71 * Retrieve stored data pointer from library context. This might be useful
72 * to access from callbacks like a custom logging function.
74 * Returns: stored userdata
76 _public_ void *udev_get_userdata(struct udev *udev)
80 return udev->userdata;
85 * @udev: udev library context
86 * @userdata: data pointer
88 * Store custom @userdata in the library context.
90 _public_ void udev_set_userdata(struct udev *udev, void *userdata)
94 udev->userdata = userdata;
100 * Create udev library context. This reads the udev configuration
101 * file, and fills in the default values.
103 * The initial refcount is 1, and needs to be decremented to
104 * release the resources of the udev library context.
106 * Returns: a new udev library context
108 _public_ struct udev *udev_new(void)
114 udev = calloc(1, sizeof(struct udev));
118 udev->log_fn = log_stderr;
119 udev->log_priority = LOG_ERR;
120 udev_list_init(udev, &udev->properties_list, true);
122 f = fopen(SYSCONFDIR "/udev/udev.conf", "re");
124 char line[UTIL_LINE_SIZE];
127 while (fgets(line, sizeof(line), f)) {
136 while (isspace(key[0]))
139 /* comment or empty line */
140 if (key[0] == '#' || key[0] == '\0')
143 /* split key/value */
144 val = strchr(key, '=');
146 udev_err(udev, "missing <key>=<value> in " SYSCONFDIR "/udev/udev.conf[%i]; skip line\n", line_nr);
153 while (isspace(val[0]))
160 while (isspace(key[len-1]))
164 /* terminate value */
168 while (isspace(val[len-1]))
176 if (val[0] == '"' || val[0] == '\'') {
177 if (val[len-1] != val[0]) {
178 udev_err(udev, "inconsistent quoting in " SYSCONFDIR "/udev/udev.conf[%i]; skip line\n", line_nr);
185 if (strcmp(key, "udev_log") == 0) {
186 udev_set_log_priority(udev, util_log_priority(val));
193 /* environment overrides config */
194 env = getenv("UDEV_LOG");
196 udev_set_log_priority(udev, util_log_priority(env));
203 * @udev: udev library context
205 * Take a reference of the udev library context.
207 * Returns: the passed udev library context
209 _public_ struct udev *udev_ref(struct udev *udev)
219 * @udev: udev library context
221 * Drop a reference of the udev library context. If the refcount
222 * reaches zero, the resources of the context will be released.
225 _public_ struct udev *udev_unref(struct udev *udev)
230 if (udev->refcount > 0)
232 udev_list_cleanup(&udev->properties_list);
239 * @udev: udev library context
240 * @log_fn: function to be called for logging messages
242 * The built-in logging writes to stderr. It can be
243 * overridden by a custom function, to plug log messages
244 * into the users' logging functionality.
247 _public_ void udev_set_log_fn(struct udev *udev,
248 void (*log_fn)(struct udev *udev,
249 int priority, const char *file, int line, const char *fn,
250 const char *format, va_list args))
252 udev->log_fn = log_fn;
253 udev_dbg(udev, "custom logging function %p registered\n", log_fn);
257 * udev_get_log_priority:
258 * @udev: udev library context
260 * The initial logging priority is read from the udev config file
263 * Returns: the current logging priority
265 _public_ int udev_get_log_priority(struct udev *udev)
267 return udev->log_priority;
271 * udev_set_log_priority:
272 * @udev: udev library context
273 * @priority: the new logging priority
275 * Set the current logging priority. The value controls which messages
278 _public_ void udev_set_log_priority(struct udev *udev, int priority)
282 udev->log_priority = priority;
283 snprintf(num, sizeof(num), "%u", udev->log_priority);
284 udev_add_property(udev, "UDEV_LOG", num);
287 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
290 struct udev_list_entry *list_entry;
292 list_entry = udev_get_properties_list_entry(udev);
293 list_entry = udev_list_entry_get_by_name(list_entry, key);
294 if (list_entry != NULL)
295 udev_list_entry_delete(list_entry);
298 return udev_list_entry_add(&udev->properties_list, key, value);
301 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
303 return udev_list_get_entry(&udev->properties_list);