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);
47 security_context_t selinux_prev_scontext;
52 void udev_log(struct udev *udev,
53 int priority, const char *file, int line, const char *fn,
54 const char *format, ...)
58 if (priority > udev->log_priority)
61 va_start(args, format);
62 udev->log_fn(udev, priority, file, line, fn, format, args);
66 static void log_stderr(struct udev *udev,
67 int priority, const char *file, int line, const char *fn,
68 const char *format, va_list args)
70 fprintf(stderr, "libudev: %s: ", fn);
71 vfprintf(stderr, format, args);
74 static void selinux_init(struct udev *udev)
78 * record the present security context, for file-creation
79 * restoration creation purposes.
81 udev->selinux_enabled = (is_selinux_enabled() > 0);
82 info(udev, "selinux=%i\n", udev->selinux_enabled);
83 if (udev->selinux_enabled) {
84 matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
85 if (getfscreatecon(&udev->selinux_prev_scontext) < 0) {
86 err(udev, "getfscreatecon failed\n");
87 udev->selinux_prev_scontext = NULL;
93 void *udev_get_userdata(struct udev *udev)
97 return udev->userdata;
100 void udev_set_userdata(struct udev *udev, void *userdata)
104 udev->userdata = userdata;
107 static void selinux_exit(struct udev *udev)
110 if (udev->selinux_enabled) {
111 freecon(udev->selinux_prev_scontext);
112 udev->selinux_prev_scontext = NULL;
117 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
120 if (udev->selinux_enabled) {
121 security_context_t scontext = NULL;
123 if (matchpathcon(file, mode, &scontext) < 0) {
124 err(udev, "matchpathcon(%s) failed\n", file);
127 if (lsetfilecon(file, scontext) < 0)
128 err(udev, "setfilecon %s failed: %s\n", file, strerror(errno));
134 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
137 if (udev->selinux_enabled) {
138 security_context_t scontext = NULL;
140 if (matchpathcon(file, mode, &scontext) < 0) {
141 err(udev, "matchpathcon(%s) failed\n", file);
144 if (setfscreatecon(scontext) < 0)
145 err(udev, "setfscreatecon %s failed: %s\n", file, strerror(errno));
151 void udev_selinux_resetfscreatecon(struct udev *udev)
154 if (udev->selinux_enabled) {
155 if (setfscreatecon(udev->selinux_prev_scontext) < 0)
156 err(udev, "setfscreatecon failed: %s\n", strerror(errno));
164 * Create udev library context.
166 * The initial refcount is 1, and needs to be decremented to
167 * release the ressources of the udev library context.
169 * Returns: a new udev library context
171 struct udev *udev_new(void)
178 udev = malloc(sizeof(struct udev));
181 memset(udev, 0x00, (sizeof(struct udev)));
183 udev->log_fn = log_stderr;
184 udev->log_priority = LOG_ERR;
186 udev->dev_path = strdup(UDEV_PREFIX "/dev");
187 udev->sys_path = strdup("/sys");
188 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
189 if (udev->dev_path == NULL ||
190 udev->sys_path == NULL ||
194 /* settings by environment and config file */
195 env = getenv("SYSFS_PATH");
197 free(udev->sys_path);
198 udev->sys_path = strdup(env);
199 util_remove_trailing_chars(udev->sys_path, '/');
202 env = getenv("UDEV_RUN");
203 if (env != NULL && strcmp(env, "0") == 0)
206 env = getenv("UDEV_CONFIG_FILE");
209 config_file = strdup(env);
210 util_remove_trailing_chars(config_file, '/');
212 if (config_file == NULL)
214 f = fopen(config_file, "r");
216 char line[UTIL_LINE_SIZE];
219 while (fgets(line, sizeof(line), f)) {
228 while (isspace(key[0]))
231 /* comment or empty line */
232 if (key[0] == '#' || key[0] == '\0')
235 /* split key/value */
236 val = strchr(key, '=');
238 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
245 while (isspace(val[0]))
252 while (isspace(key[len-1]))
256 /* terminate value */
260 while (isspace(val[len-1]))
268 if (val[0] == '"' || val[0] == '\'') {
269 if (val[len-1] != val[0]) {
270 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
277 if (strcasecmp(key, "udev_log") == 0) {
278 udev->log_priority = util_log_priority(val);
281 if (strcasecmp(key, "udev_root") == 0) {
282 free(udev->dev_path);
283 udev->dev_path = strdup(val);
284 util_remove_trailing_chars(udev->dev_path, '/');
287 if (strcasecmp(key, "udev_rules") == 0) {
288 free(udev->rules_path);
289 udev->rules_path = strdup(val);
290 util_remove_trailing_chars(udev->rules_path, '/');
297 env = getenv("UDEV_ROOT");
299 free(udev->dev_path);
300 udev->dev_path = strdup(env);
301 util_remove_trailing_chars(udev->dev_path, '/');
304 env = getenv("UDEV_LOG");
306 udev->log_priority = util_log_priority(env);
308 if (udev->dev_path == NULL || udev->sys_path == NULL)
311 info(udev, "context %p created\n", udev);
312 info(udev, "log_priority=%d\n", udev->log_priority);
313 info(udev, "config_file='%s'\n", config_file);
314 info(udev, "dev_path='%s'\n", udev->dev_path);
315 info(udev, "sys_path='%s'\n", udev->sys_path);
316 if (udev->rules_path != NULL)
317 info(udev, "rules_path='%s'\n", udev->rules_path);
322 err(udev, "context creation failed\n");
329 * @udev: udev library context
331 * Take a reference of the udev library context.
333 * Returns: the passed udev library context
335 struct udev *udev_ref(struct udev *udev)
345 * @udev: udev library context
347 * Drop a reference of the udev library context. If the refcount
348 * reaches zero, the ressources of the context will be released.
351 void udev_unref(struct udev *udev)
356 if (udev->refcount > 0)
359 free(udev->dev_path);
360 free(udev->sys_path);
361 free(udev->rules_path);
362 info(udev, "context %p released\n", udev);
368 * @udev: udev library context
369 * @log_fn: function to be called for logging messages
371 * The built-in logging, which writes to stderr if the
372 * LIBUDEV_DEBUG environment variable is set, can be
373 * overridden by a custom function, to plug log messages
374 * into the users logging functionality.
377 void udev_set_log_fn(struct udev *udev,
378 void (*log_fn)(struct udev *udev,
379 int priority, const char *file, int line, const char *fn,
380 const char *format, va_list args))
382 udev->log_fn = log_fn;
383 info(udev, "custom logging function %p registered\n", udev);
386 int udev_get_log_priority(struct udev *udev)
388 return udev->log_priority;
391 void udev_set_log_priority(struct udev *udev, int priority)
393 udev->log_priority = priority;
396 const char *udev_get_rules_path(struct udev *udev)
398 return udev->rules_path;
401 int udev_get_run(struct udev *udev)
408 * @udev: udev library context
410 * Retrieve the sysfs mount point. The default is "/sys". For
411 * testing purposes, it can be overridden with the environment
412 * variable SYSFS_PATH.
414 * Returns: the sys mount point
416 const char *udev_get_sys_path(struct udev *udev)
420 return udev->sys_path;
425 * @udev: udev library context
427 * Retrieve the device directory path. The default value is "/dev",
428 * the actual value may be overridden in the udev configuration
431 * Returns: the device directory path
433 const char *udev_get_dev_path(struct udev *udev)
437 return udev->dev_path;