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/>.
29 #include <selinux/selinux.h>
33 #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);
46 security_context_t selinux_prev_scontext;
51 void udev_log(struct udev *udev,
52 int priority, const char *file, int line, const char *fn,
53 const char *format, ...)
57 if (priority > udev->log_priority)
60 va_start(args, format);
61 udev->log_fn(udev, priority, file, line, fn, format, args);
65 static void log_stderr(struct udev *udev,
66 int priority, const char *file, int line, const char *fn,
67 const char *format, va_list args)
69 fprintf(stderr, "libudev: %s: ", fn);
70 vfprintf(stderr, format, args);
73 static void selinux_init(struct udev *udev)
77 * record the present security context, for file-creation
78 * restoration creation purposes.
80 udev->selinux_enabled = (is_selinux_enabled() > 0);
81 info(udev, "selinux=%i\n", udev->selinux_enabled);
82 if (udev->selinux_enabled) {
83 matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
84 if (getfscreatecon(&udev->selinux_prev_scontext) < 0) {
85 err(udev, "getfscreatecon failed\n");
86 udev->selinux_prev_scontext = NULL;
92 static void selinux_exit(struct udev *udev)
95 if (udev->selinux_enabled) {
96 freecon(udev->selinux_prev_scontext);
97 udev->selinux_prev_scontext = NULL;
102 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
105 if (udev->selinux_enabled) {
106 security_context_t scontext = NULL;
108 if (matchpathcon(file, mode, &scontext) < 0) {
109 err(udev, "matchpathcon(%s) failed\n", file);
112 if (lsetfilecon(file, scontext) < 0)
113 err(udev, "setfilecon %s failed: %s\n", file, strerror(errno));
119 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
122 if (udev->selinux_enabled) {
123 security_context_t scontext = NULL;
125 if (matchpathcon(file, mode, &scontext) < 0) {
126 err(udev, "matchpathcon(%s) failed\n", file);
129 if (setfscreatecon(scontext) < 0)
130 err(udev, "setfscreatecon %s failed: %s\n", file, strerror(errno));
136 void udev_selinux_resetfscreatecon(struct udev *udev)
139 if (udev->selinux_enabled) {
140 if (setfscreatecon(udev->selinux_prev_scontext) < 0)
141 err(udev, "setfscreatecon failed: %s\n", strerror(errno));
149 * Create udev library context.
151 * The initial refcount is 1, and needs to be decremented to
152 * release the ressources of the udev library context.
154 * Returns: a new udev library context
156 struct udev *udev_new(void)
163 udev = malloc(sizeof(struct udev));
166 memset(udev, 0x00, (sizeof(struct udev)));
168 udev->log_fn = log_stderr;
169 udev->log_priority = LOG_ERR;
171 udev->dev_path = strdup(UDEV_PREFIX "/dev");
172 udev->sys_path = strdup("/sys");
173 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
174 if (udev->dev_path == NULL ||
175 udev->sys_path == NULL ||
179 /* settings by environment and config file */
180 env = getenv("SYSFS_PATH");
182 free(udev->sys_path);
183 udev->sys_path = strdup(env);
184 util_remove_trailing_chars(udev->sys_path, '/');
187 env = getenv("UDEV_RUN");
188 if (env != NULL && strcmp(env, "0") == 0)
191 env = getenv("UDEV_CONFIG_FILE");
194 config_file = strdup(env);
195 util_remove_trailing_chars(config_file, '/');
197 if (config_file == NULL)
199 f = fopen(config_file, "r");
201 char line[UTIL_LINE_SIZE];
204 while (fgets(line, sizeof(line), f)) {
213 while (isspace(key[0]))
216 /* comment or empty line */
217 if (key[0] == '#' || key[0] == '\0')
220 /* split key/value */
221 val = strchr(key, '=');
223 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
230 while (isspace(val[0]))
237 while (isspace(key[len-1]))
241 /* terminate value */
245 while (isspace(val[len-1]))
253 if (val[0] == '"' || val[0] == '\'') {
254 if (val[len-1] != val[0]) {
255 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
262 if (strcasecmp(key, "udev_log") == 0) {
263 udev->log_priority = util_log_priority(val);
266 if (strcasecmp(key, "udev_root") == 0) {
267 free(udev->dev_path);
268 udev->dev_path = strdup(val);
269 util_remove_trailing_chars(udev->dev_path, '/');
272 if (strcasecmp(key, "udev_rules") == 0) {
273 free(udev->rules_path);
274 udev->rules_path = strdup(val);
275 util_remove_trailing_chars(udev->rules_path, '/');
282 env = getenv("UDEV_ROOT");
284 free(udev->dev_path);
285 udev->dev_path = strdup(env);
286 util_remove_trailing_chars(udev->dev_path, '/');
289 env = getenv("UDEV_LOG");
291 udev->log_priority = util_log_priority(env);
293 if (udev->dev_path == NULL || udev->sys_path == NULL)
296 info(udev, "context %p created\n", udev);
297 info(udev, "log_priority=%d\n", udev->log_priority);
298 info(udev, "config_file='%s'\n", config_file);
299 info(udev, "dev_path='%s'\n", udev->dev_path);
300 info(udev, "sys_path='%s'\n", udev->sys_path);
301 if (udev->rules_path != NULL)
302 info(udev, "rules_path='%s'\n", udev->rules_path);
307 err(udev, "context creation failed\n");
314 * @udev: udev library context
316 * Take a reference of the udev library context.
318 * Returns: the passed udev library context
320 struct udev *udev_ref(struct udev *udev)
330 * @udev: udev library context
332 * Drop a reference of the udev library context. If the refcount
333 * reaches zero, the ressources of the context will be released.
336 void udev_unref(struct udev *udev)
341 if (udev->refcount > 0)
344 free(udev->dev_path);
345 free(udev->sys_path);
346 free(udev->rules_path);
347 info(udev, "context %p released\n", udev);
353 * @udev: udev library context
354 * @log_fn: function to be called for logging messages
356 * The built-in logging, which writes to stderr if the
357 * LIBUDEV_DEBUG environment variable is set, can be
358 * overridden by a custom function, to plug log messages
359 * into the users logging functionality.
362 void udev_set_log_fn(struct udev *udev,
363 void (*log_fn)(struct udev *udev,
364 int priority, const char *file, int line, const char *fn,
365 const char *format, va_list args))
367 udev->log_fn = log_fn;
368 info(udev, "custom logging function %p registered\n", udev);
371 int udev_get_log_priority(struct udev *udev)
373 return udev->log_priority;
376 void udev_set_log_priority(struct udev *udev, int priority)
378 udev->log_priority = priority;
381 const char *udev_get_rules_path(struct udev *udev)
383 return udev->rules_path;
386 int udev_get_run(struct udev *udev)
393 * @udev: udev library context
395 * Retrieve the sysfs mount point. The default is "/sys". For
396 * testing purposes, it can be overridden with the environment
397 * variable SYSFS_PATH.
399 * Returns: the sys mount point
401 const char *udev_get_sys_path(struct udev *udev)
405 return udev->sys_path;
410 * @udev: udev library context
412 * Retrieve the device directory path. The default value is "/dev",
413 * the actual value may be overridden in the udev configuration
416 * Returns: the device directory path
418 const char *udev_get_dev_path(struct udev *udev)
422 return udev->dev_path;