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"
35 void udev_log(struct udev *udev,
36 int priority, const char *file, int line, const char *fn,
37 const char *format, ...)
41 va_start(args, format);
42 udev->log_fn(udev, priority, file, line, fn, format, args);
46 static void log_stderr(struct udev *udev,
47 int priority, const char *file, int line, const char *fn,
48 const char *format, va_list args)
53 if (getenv("LIBUDEV_DEBUG") != NULL)
60 fprintf(stderr, "libudev: %s: ", fn);
61 vfprintf(stderr, format, args);
65 /* glue to udev logging, needed until udev logging code is "fixed" */
67 void log_message(int priority, const char *format, ...)
71 va_start(args, format);
72 log_stderr(NULL, priority, NULL, 0, "", format, args);
80 * Create udev library context.
82 * The initial refcount is 1, and needs to be decremented to
83 * release the ressources of the udev library context.
85 * Returns: a new udev library context
87 struct udev *udev_new(void)
91 udev = malloc(sizeof(struct udev));
94 memset(udev, 0x00, (sizeof(struct udev)));
96 udev->log_fn = log_stderr;
99 log_info(udev, "context %p created\n", udev);
105 * @udev: udev library context
107 * Take a reference of the udev library context.
109 * Returns: the passed udev library context
111 struct udev *udev_ref(struct udev *udev)
119 * @udev: udev library context
121 * Drop a reference of the udev library context. If the refcount
122 * reaches zero, the ressources of the context will be released.
125 void udev_unref(struct udev *udev)
128 if (udev->refcount > 0)
131 log_info(udev, "context %p released\n", udev);
137 * @udev: udev library context
138 * @log_fn: function to be called for logging messages
140 * The built-in logging, which writes to stderr if the
141 * LIBUDEV_DEBUG environment variable is set, can be
142 * overridden by a custom function, to plug log messages
143 * into the users logging functionality.
146 void udev_set_log_fn(struct udev *udev,
147 void (*log_fn)(struct udev *udev,
148 int priority, const char *file, int line, const char *fn,
149 const char *format, va_list args))
151 udev->log_fn = log_fn;
152 log_info(udev, "custom logging function %p registered\n", udev);
157 * @udev: udev library context
159 * Retrieve the sysfs mount point. The default is "/sys". For
160 * testing purposes, it can be overridden with the environment
161 * variable SYSFS_PATH.
163 * Returns: the sys mount point
165 const char *udev_get_sys_path(struct udev *udev)
172 * @udev: udev library context
174 * Retrieve the device directory path. The default value is "/dev",
175 * the actual value may be overridden in the udev configuration
178 * Returns: the device directory path
180 const char *udev_get_dev_path(struct udev *udev)