2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "libudev-private.h"
38 void (*log_fn)(struct udev *udev,
39 int priority, const char *file, int line, const char *fn,
40 const char *format, va_list args);
43 void udev_log(struct udev *udev,
44 int priority, const char *file, int line, const char *fn,
45 const char *format, ...)
49 va_start(args, format);
50 udev->log_fn(udev, priority, file, line, fn, format, args);
54 static void log_stderr(struct udev *udev,
55 int priority, const char *file, int line, const char *fn,
56 const char *format, va_list args)
61 if (getenv("LIBUDEV_DEBUG") != NULL)
68 fprintf(stderr, "libudev: %s: ", fn);
69 vfprintf(stderr, format, args);
73 /* glue to udev logging, needed until udev logging code is "fixed" */
75 void log_message(int priority, const char *format, ...)
79 va_start(args, format);
80 log_stderr(NULL, priority, NULL, 0, "", format, args);
88 * Create udev library context.
90 * The initial refcount is 1, and needs to be decremented to
91 * release the ressources of the udev library context.
93 * Returns: a new udev library context
95 struct udev *udev_new(void)
99 udev = malloc(sizeof(struct udev));
102 memset(udev, 0x00, (sizeof(struct udev)));
104 udev->log_fn = log_stderr;
107 log_info(udev, "context %p created\n", udev);
113 * @udev: udev library context
115 * Take a reference of the udev library context.
117 * Returns: the passed udev library context
119 struct udev *udev_ref(struct udev *udev)
129 * @udev: udev library context
131 * Drop a reference of the udev library context. If the refcount
132 * reaches zero, the ressources of the context will be released.
135 void udev_unref(struct udev *udev)
140 if (udev->refcount > 0)
143 log_info(udev, "context %p released\n", udev);
149 * @udev: udev library context
150 * @log_fn: function to be called for logging messages
152 * The built-in logging, which writes to stderr if the
153 * LIBUDEV_DEBUG environment variable is set, can be
154 * overridden by a custom function, to plug log messages
155 * into the users logging functionality.
158 void udev_set_log_fn(struct udev *udev,
159 void (*log_fn)(struct udev *udev,
160 int priority, const char *file, int line, const char *fn,
161 const char *format, va_list args))
163 udev->log_fn = log_fn;
164 log_info(udev, "custom logging function %p registered\n", udev);
169 * @udev: udev library context
171 * Retrieve the sysfs mount point. The default is "/sys". For
172 * testing purposes, it can be overridden with the environment
173 * variable SYSFS_PATH.
175 * Returns: the sys mount point
177 const char *udev_get_sys_path(struct udev *udev)
186 * @udev: udev library context
188 * Retrieve the device directory path. The default value is "/dev",
189 * the actual value may be overridden in the udev configuration
192 * Returns: the device directory path
194 const char *udev_get_dev_path(struct udev *udev)