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 int selinux_initialized;
48 security_context_t selinux_prev_scontext;
53 void udev_log(struct udev *udev,
54 int priority, const char *file, int line, const char *fn,
55 const char *format, ...)
59 if (priority > udev->log_priority)
62 va_start(args, format);
63 udev->log_fn(udev, priority, file, line, fn, format, args);
67 static void log_stderr(struct udev *udev,
68 int priority, const char *file, int line, const char *fn,
69 const char *format, va_list args)
71 fprintf(stderr, "libudev: %s: ", fn);
72 vfprintf(stderr, format, args);
75 static void selinux_init(struct udev *udev)
79 * record the present security context, for file-creation
80 * restoration creation purposes.
82 udev->selinux_enabled = (is_selinux_enabled() > 0);
83 info(udev, "selinux=%i\n", udev->selinux_enabled);
84 if (udev->selinux_enabled) {
85 matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
86 if (getfscreatecon(&udev->selinux_prev_scontext) < 0) {
87 err(udev, "getfscreatecon failed\n");
88 udev->selinux_prev_scontext = NULL;
91 udev->selinux_initialized = 1;
95 void *udev_get_userdata(struct udev *udev)
99 return udev->userdata;
102 void udev_set_userdata(struct udev *udev, void *userdata)
106 udev->userdata = userdata;
109 static void selinux_exit(struct udev *udev)
112 if (!udev->selinux_initialized)
114 if (udev->selinux_enabled) {
115 freecon(udev->selinux_prev_scontext);
116 udev->selinux_prev_scontext = NULL;
121 void udev_selinux_lsetfilecon(struct udev *udev, const char *file, unsigned int mode)
124 if (!udev->selinux_initialized)
126 if (udev->selinux_enabled) {
127 security_context_t scontext = NULL;
129 if (matchpathcon(file, mode, &scontext) < 0) {
130 err(udev, "matchpathcon(%s) failed\n", file);
133 if (lsetfilecon(file, scontext) < 0)
134 err(udev, "setfilecon %s failed: %s\n", file, strerror(errno));
140 void udev_selinux_setfscreatecon(struct udev *udev, const char *file, unsigned int mode)
143 if (!udev->selinux_initialized)
145 if (udev->selinux_enabled) {
146 security_context_t scontext = NULL;
148 if (matchpathcon(file, mode, &scontext) < 0) {
149 err(udev, "matchpathcon(%s) failed\n", file);
152 if (setfscreatecon(scontext) < 0)
153 err(udev, "setfscreatecon %s failed: %s\n", file, strerror(errno));
159 void udev_selinux_resetfscreatecon(struct udev *udev)
162 if (!udev->selinux_initialized)
164 if (udev->selinux_enabled) {
165 if (setfscreatecon(udev->selinux_prev_scontext) < 0)
166 err(udev, "setfscreatecon failed: %s\n", strerror(errno));
174 * Create udev library context.
176 * The initial refcount is 1, and needs to be decremented to
177 * release the ressources of the udev library context.
179 * Returns: a new udev library context
181 struct udev *udev_new(void)
188 udev = malloc(sizeof(struct udev));
191 memset(udev, 0x00, (sizeof(struct udev)));
193 udev->log_fn = log_stderr;
194 udev->log_priority = LOG_ERR;
196 udev->dev_path = strdup(UDEV_PREFIX "/dev");
197 udev->sys_path = strdup("/sys");
198 config_file = strdup(SYSCONFDIR "/udev/udev.conf");
199 if (udev->dev_path == NULL ||
200 udev->sys_path == NULL ||
204 /* settings by environment and config file */
205 env = getenv("SYSFS_PATH");
207 free(udev->sys_path);
208 udev->sys_path = strdup(env);
209 util_remove_trailing_chars(udev->sys_path, '/');
212 env = getenv("UDEV_RUN");
213 if (env != NULL && strcmp(env, "0") == 0)
216 env = getenv("UDEV_CONFIG_FILE");
219 config_file = strdup(env);
220 util_remove_trailing_chars(config_file, '/');
222 if (config_file == NULL)
224 f = fopen(config_file, "r");
226 char line[UTIL_LINE_SIZE];
229 while (fgets(line, sizeof(line), f)) {
238 while (isspace(key[0]))
241 /* comment or empty line */
242 if (key[0] == '#' || key[0] == '\0')
245 /* split key/value */
246 val = strchr(key, '=');
248 err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
255 while (isspace(val[0]))
262 while (isspace(key[len-1]))
266 /* terminate value */
270 while (isspace(val[len-1]))
278 if (val[0] == '"' || val[0] == '\'') {
279 if (val[len-1] != val[0]) {
280 err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
287 if (strcasecmp(key, "udev_log") == 0) {
288 udev->log_priority = util_log_priority(val);
291 if (strcasecmp(key, "udev_root") == 0) {
292 free(udev->dev_path);
293 udev->dev_path = strdup(val);
294 util_remove_trailing_chars(udev->dev_path, '/');
297 if (strcasecmp(key, "udev_rules") == 0) {
298 free(udev->rules_path);
299 udev->rules_path = strdup(val);
300 util_remove_trailing_chars(udev->rules_path, '/');
307 env = getenv("UDEV_ROOT");
309 free(udev->dev_path);
310 udev->dev_path = strdup(env);
311 util_remove_trailing_chars(udev->dev_path, '/');
314 env = getenv("UDEV_LOG");
316 udev->log_priority = util_log_priority(env);
318 if (udev->dev_path == NULL || udev->sys_path == NULL)
320 info(udev, "context %p created\n", udev);
321 info(udev, "log_priority=%d\n", udev->log_priority);
322 info(udev, "config_file='%s'\n", config_file);
323 info(udev, "dev_path='%s'\n", udev->dev_path);
324 info(udev, "sys_path='%s'\n", udev->sys_path);
325 if (udev->rules_path != NULL)
326 info(udev, "rules_path='%s'\n", udev->rules_path);
331 err(udev, "context creation failed\n");
338 * @udev: udev library context
340 * Take a reference of the udev library context.
342 * Returns: the passed udev library context
344 struct udev *udev_ref(struct udev *udev)
354 * @udev: udev library context
356 * Drop a reference of the udev library context. If the refcount
357 * reaches zero, the ressources of the context will be released.
360 void udev_unref(struct udev *udev)
365 if (udev->refcount > 0)
368 free(udev->dev_path);
369 free(udev->sys_path);
370 free(udev->rules_path);
371 info(udev, "context %p released\n", udev);
377 * @udev: udev library context
378 * @log_fn: function to be called for logging messages
380 * The built-in logging, which writes to stderr if the
381 * LIBUDEV_DEBUG environment variable is set, can be
382 * overridden by a custom function, to plug log messages
383 * into the users logging functionality.
386 void udev_set_log_fn(struct udev *udev,
387 void (*log_fn)(struct udev *udev,
388 int priority, const char *file, int line, const char *fn,
389 const char *format, va_list args))
391 udev->log_fn = log_fn;
392 info(udev, "custom logging function %p registered\n", udev);
395 int udev_get_log_priority(struct udev *udev)
397 return udev->log_priority;
400 void udev_set_log_priority(struct udev *udev, int priority)
402 udev->log_priority = priority;
405 const char *udev_get_rules_path(struct udev *udev)
407 return udev->rules_path;
410 int udev_get_run(struct udev *udev)
417 * @udev: udev library context
419 * Retrieve the sysfs mount point. The default is "/sys". For
420 * testing purposes, it can be overridden with the environment
421 * variable SYSFS_PATH.
423 * Returns: the sys mount point
425 const char *udev_get_sys_path(struct udev *udev)
429 return udev->sys_path;
434 * @udev: udev library context
436 * Retrieve the device directory path. The default value is "/dev",
437 * the actual value may be overridden in the udev configuration
440 * Returns: the device directory path
442 const char *udev_get_dev_path(struct udev *udev)
446 return udev->dev_path;