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/>.
31 #include <selinux/selinux.h>
35 #include "libudev-private.h"
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);
49 security_context_t selinux_prev_scontext;
54 void udev_log(struct udev *udev,
55 int priority, const char *file, int line, const char *fn,
56 const char *format, ...)
60 if (priority > udev->log_priority)
63 va_start(args, format);
64 udev->log_fn(udev, priority, file, line, fn, format, args);
68 static void log_stderr(struct udev *udev,
69 int priority, const char *file, int line, const char *fn,
70 const char *format, va_list args)
72 fprintf(stderr, "libudev: %s: ", fn);
73 vfprintf(stderr, format, args);
76 static void selinux_init(struct udev *udev)
80 * record the present security context, for file-creation
81 * restoration creation purposes.
83 udev->selinux_enabled = (is_selinux_enabled() > 0);
84 info(udev, "selinux=%i\n", udev->selinux_enabled);
85 if (udev->selinux_enabled) {
86 matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
87 if (getfscreatecon(&udev->selinux_prev_scontext) < 0) {
88 err(udev, "getfscreatecon failed\n");
89 udev->selinux_prev_scontext = NULL;
95 static void selinux_exit(struct udev *udev)
98 if (udev->selinux_enabled) {
99 freecon(udev->selinux_prev_scontext);
100 udev->selinux_prev_scontext = NULL;
105 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
108 if (udev->selinux_enabled) {
109 security_context_t scontext = NULL;
111 if (matchpathcon(file, mode, &scontext) < 0) {
112 err(udev, "matchpathcon(%s) failed\n", file);
115 if (lsetfilecon(file, scontext) < 0)
116 err(udev, "setfilecon %s failed: %s\n", file, strerror(errno));
122 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
125 if (udev->selinux_enabled) {
126 security_context_t scontext = NULL;
128 if (matchpathcon(file, mode, &scontext) < 0) {
129 err(udev, "matchpathcon(%s) failed\n", file);
132 if (setfscreatecon(scontext) < 0)
133 err(udev, "setfscreatecon %s failed: %s\n", file, strerror(errno));
139 void udev_selinux_resetfscreatecon(struct udev *udev)
142 if (udev->selinux_enabled) {
143 if (setfscreatecon(udev->selinux_prev_scontext) < 0)
144 err(udev, "setfscreatecon failed: %s\n", strerror(errno));
152 * Create udev library context.
154 * The initial refcount is 1, and needs to be decremented to
155 * release the ressources of the udev library context.
157 * Returns: a new udev library context
159 struct udev *udev_new(void)
166 udev = malloc(sizeof(struct udev));
169 memset(udev, 0x00, (sizeof(struct udev)));
171 udev->log_fn = log_stderr;
172 udev->log_priority = LOG_ERR;
174 udev->dev_path = strdup(UDEV_PREFIX "/dev");
175 udev->sys_path = strdup("/sys");
176 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
177 if (udev->dev_path == NULL ||
178 udev->sys_path == NULL ||
182 /* settings by environment and config file */
183 env = getenv("SYSFS_PATH");
185 free(udev->sys_path);
186 udev->sys_path = strdup(env);
187 util_remove_trailing_chars(udev->sys_path, '/');
190 env = getenv("UDEV_RUN");
191 if (env != NULL && strcmp(env, "0") == 0)
194 env = getenv("UDEV_CONFIG_FILE");
197 config_file = strdup(env);
198 util_remove_trailing_chars(config_file, '/');
200 if (config_file == NULL)
202 f = fopen(config_file, "r");
204 char line[LINE_SIZE];
207 while (fgets(line, sizeof(line), f)) {
216 while (isspace(key[0]))
219 /* comment or empty line */
220 if (key[0] == '#' || key[0] == '\0')
223 /* split key/value */
224 val = strchr(key, '=');
226 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
233 while (isspace(val[0]))
240 while (isspace(key[len-1]))
244 /* terminate value */
248 while (isspace(val[len-1]))
256 if (val[0] == '"' || val[0] == '\'') {
257 if (val[len-1] != val[0]) {
258 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
265 if (strcasecmp(key, "udev_log") == 0) {
266 udev->log_priority = util_log_priority(val);
269 if (strcasecmp(key, "udev_root") == 0) {
270 free(udev->dev_path);
271 udev->dev_path = strdup(val);
272 util_remove_trailing_chars(udev->dev_path, '/');
275 if (strcasecmp(key, "udev_rules") == 0) {
276 free(udev->rules_path);
277 udev->rules_path = strdup(val);
278 util_remove_trailing_chars(udev->rules_path, '/');
285 env = getenv("UDEV_ROOT");
287 free(udev->dev_path);
288 udev->dev_path = strdup(env);
289 util_remove_trailing_chars(udev->dev_path, '/');
292 env = getenv("UDEV_LOG");
294 udev->log_priority = util_log_priority(env);
296 if (udev->dev_path == NULL || udev->sys_path == NULL)
299 info(udev, "context %p created\n", udev);
300 info(udev, "log_priority=%d\n", udev->log_priority);
301 info(udev, "config_file='%s'\n", config_file);
302 info(udev, "dev_path='%s'\n", udev->dev_path);
303 info(udev, "sys_path='%s'\n", udev->sys_path);
304 if (udev->rules_path != NULL)
305 info(udev, "rules_path='%s'\n", udev->rules_path);
310 err(udev, "context creation failed\n");
317 * @udev: udev library context
319 * Take a reference of the udev library context.
321 * Returns: the passed udev library context
323 struct udev *udev_ref(struct udev *udev)
333 * @udev: udev library context
335 * Drop a reference of the udev library context. If the refcount
336 * reaches zero, the ressources of the context will be released.
339 void udev_unref(struct udev *udev)
344 if (udev->refcount > 0)
347 free(udev->dev_path);
348 free(udev->sys_path);
349 free(udev->rules_path);
350 info(udev, "context %p released\n", udev);
356 * @udev: udev library context
357 * @log_fn: function to be called for logging messages
359 * The built-in logging, which writes to stderr if the
360 * LIBUDEV_DEBUG environment variable is set, can be
361 * overridden by a custom function, to plug log messages
362 * into the users logging functionality.
365 void udev_set_log_fn(struct udev *udev,
366 void (*log_fn)(struct udev *udev,
367 int priority, const char *file, int line, const char *fn,
368 const char *format, va_list args))
370 udev->log_fn = log_fn;
371 info(udev, "custom logging function %p registered\n", udev);
374 int udev_get_log_priority(struct udev *udev)
376 return udev->log_priority;
379 void udev_set_log_priority(struct udev *udev, int priority)
381 udev->log_priority = priority;
384 const char *udev_get_rules_path(struct udev *udev)
386 return udev->rules_path;
389 int udev_get_run(struct udev *udev)
396 * @udev: udev library context
398 * Retrieve the sysfs mount point. The default is "/sys". For
399 * testing purposes, it can be overridden with the environment
400 * variable SYSFS_PATH.
402 * Returns: the sys mount point
404 const char *udev_get_sys_path(struct udev *udev)
408 return udev->sys_path;
413 * @udev: udev library context
415 * Retrieve the device directory path. The default value is "/dev",
416 * the actual value may be overridden in the udev configuration
419 * Returns: the device directory path
421 const char *udev_get_dev_path(struct udev *udev)
425 return udev->dev_path;