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/>.
32 #include "libudev-private.h"
37 void (*log_fn)(struct udev *udev,
38 int priority, const char *file, int line, const char *fn,
39 const char *format, va_list args);
42 void udev_log(struct udev *udev,
43 int priority, const char *file, int line, const char *fn,
44 const char *format, ...)
48 va_start(args, format);
49 udev->log_fn(udev, priority, file, line, fn, format, args);
53 static void log_stderr(struct udev *udev,
54 int priority, const char *file, int line, const char *fn,
55 const char *format, va_list args)
60 if (getenv("LIBUDEV_DEBUG") != NULL)
67 fprintf(stderr, "libudev: %s: ", fn);
68 vfprintf(stderr, format, args);
72 /* glue to udev logging, needed until udev logging code is "fixed" */
74 void log_message(int priority, const char *format, ...)
78 va_start(args, format);
79 log_stderr(NULL, priority, NULL, 0, "", format, args);
87 * Create udev library context.
89 * The initial refcount is 1, and needs to be decremented to
90 * release the ressources of the udev library context.
92 * Returns: a new udev library context
94 struct udev *udev_new(void)
98 udev = malloc(sizeof(struct udev));
101 memset(udev, 0x00, (sizeof(struct udev)));
103 udev->log_fn = log_stderr;
106 log_info(udev, "context %p created\n", udev);
112 * @udev: udev library context
114 * Take a reference of the udev library context.
116 * Returns: the passed udev library context
118 struct udev *udev_ref(struct udev *udev)
128 * @udev: udev library context
130 * Drop a reference of the udev library context. If the refcount
131 * reaches zero, the ressources of the context will be released.
134 void udev_unref(struct udev *udev)
139 if (udev->refcount > 0)
142 log_info(udev, "context %p released\n", udev);
148 * @udev: udev library context
149 * @log_fn: function to be called for logging messages
151 * The built-in logging, which writes to stderr if the
152 * LIBUDEV_DEBUG environment variable is set, can be
153 * overridden by a custom function, to plug log messages
154 * into the users logging functionality.
157 void udev_set_log_fn(struct udev *udev,
158 void (*log_fn)(struct udev *udev,
159 int priority, const char *file, int line, const char *fn,
160 const char *format, va_list args))
162 udev->log_fn = log_fn;
163 log_info(udev, "custom logging function %p registered\n", udev);
168 * @udev: udev library context
170 * Retrieve the sysfs mount point. The default is "/sys". For
171 * testing purposes, it can be overridden with the environment
172 * variable SYSFS_PATH.
174 * Returns: the sys mount point
176 const char *udev_get_sys_path(struct udev *udev)
185 * @udev: udev library context
187 * Retrieve the device directory path. The default value is "/dev",
188 * the actual value may be overridden in the udev configuration
191 * Returns: the device directory path
193 const char *udev_get_dev_path(struct udev *udev)