summaryrefslogtreecommitdiff
path: root/udev/lib
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2008-09-06 15:45:31 +0200
committerKay Sievers <kay.sievers@vrfy.org>2008-09-06 15:45:31 +0200
commit7d563a17f3967890331daf08d43f2f005418139b (patch)
tree88b0c35d258a5b51fcadc1c0fc96ff80dfcaeacf /udev/lib
parentcf8ec631a23eae532541bfeeccce20f0d978404d (diff)
use libudev code, unify logging, pass udev context around everywhere
Diffstat (limited to 'udev/lib')
-rw-r--r--udev/lib/Makefile.am1
-rw-r--r--udev/lib/exported_symbols2
-rw-r--r--udev/lib/libudev-device.c24
-rw-r--r--udev/lib/libudev-enumerate.c8
-rw-r--r--udev/lib/libudev-monitor.c18
-rw-r--r--udev/lib/libudev-private.h28
-rw-r--r--udev/lib/libudev.c216
-rw-r--r--udev/lib/libudev.h4
8 files changed, 231 insertions, 70 deletions
diff --git a/udev/lib/Makefile.am b/udev/lib/Makefile.am
index f1f5fb74b1..b7dd373d5e 100644
--- a/udev/lib/Makefile.am
+++ b/udev/lib/Makefile.am
@@ -33,7 +33,6 @@ libudev_la_SOURCES =\
../udev_utils_file.c \
../udev_sysfs.c \
../udev_device.c \
- ../udev_config.c \
../udev_db.c \
../udev_sysdeps.c
diff --git a/udev/lib/exported_symbols b/udev/lib/exported_symbols
index 4db23b2251..66275f4f9c 100644
--- a/udev/lib/exported_symbols
+++ b/udev/lib/exported_symbols
@@ -3,6 +3,8 @@ udev_new
udev_ref
udev_unref
udev_set_log_fn
+udev_get_log_priority
+udev_set_log_priority
udev_get_sys_path
udev_get_dev_path
udev_device_new_from_devpath
diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c
index 629cadbc47..43ed2f3dbf 100644
--- a/udev/lib/libudev-device.c
+++ b/udev/lib/libudev-device.c
@@ -58,7 +58,7 @@ struct udev_device *device_init(struct udev *udev)
udev_device->udev = udev;
INIT_LIST_HEAD(&udev_device->link_list);
INIT_LIST_HEAD(&udev_device->env_list);
- log_info(udev_device->udev, "udev_device: %p created\n", udev_device);
+ info(udev_device->udev, "udev_device: %p created\n", udev_device);
return udev_device;
}
@@ -101,7 +101,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
if (udev_device == NULL)
return NULL;
- udevice = udev_device_init();
+ udevice = udev_device_init(udev);
if (udevice == NULL) {
free(udev_device);
return NULL;
@@ -109,13 +109,13 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
/* resolve possible symlink to real path */
strlcpy(path, devpath, sizeof(path));
- sysfs_resolve_link(path, sizeof(path));
+ sysfs_resolve_link(udev, path, sizeof(path));
device_set_devpath(udev_device, devpath);
- log_info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
+ info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
err = udev_db_get_device(udevice, path);
if (err >= 0)
- log_info(udev, "device %p filled with udev database data\n", udev_device);
+ info(udev, "device %p filled with udev database data\n", udev_device);
if (udevice->name[0] != '\0')
asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
@@ -126,11 +126,11 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
strlcpy(name, udev_get_dev_path(udev), sizeof(name));
strlcat(name, "/", sizeof(name));
strlcat(name, name_loop->name, sizeof(name));
- name_list_add(&udev_device->link_list, name, 0);
+ name_list_add(udev, &udev_device->link_list, name, 0);
}
list_for_each_entry(name_loop, &udevice->env_list, node)
- name_list_add(&udev_device->env_list, name_loop->name, 0);
+ name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
udev_device_cleanup(udevice);
return udev_device;
@@ -185,9 +185,9 @@ void udev_device_unref(struct udev_device *udev_device)
free(udev_device->syspath);
free(udev_device->devname);
free(udev_device->subsystem);
- name_list_cleanup(&udev_device->link_list);
- name_list_cleanup(&udev_device->env_list);
- log_info(udev_device->udev, "udev_device: %p released\n", udev_device);
+ name_list_cleanup(udev_device->udev, &udev_device->link_list);
+ name_list_cleanup(udev_device->udev, &udev_device->env_list);
+ info(udev_device->udev, "udev_device: %p released\n", udev_device);
free(udev_device);
}
@@ -359,14 +359,14 @@ int device_set_devname(struct udev_device *udev_device, const char *devname)
int device_add_devlink(struct udev_device *udev_device, const char *devlink)
{
- if (name_list_add(&udev_device->link_list, devlink, 0) == NULL)
+ if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
return -ENOMEM;
return 0;
}
int device_add_property(struct udev_device *udev_device, const char *property)
{
- if (name_list_add(&udev_device->env_list, property, 0) == NULL)
+ if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
return -ENOMEM;
return 0;
}
diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c
index e3198665fc..6aa04d8f9b 100644
--- a/udev/lib/libudev-enumerate.c
+++ b/udev/lib/libudev-enumerate.c
@@ -58,8 +58,8 @@ static int devices_scan_subsystem(struct udev *udev,
strlcpy(devpath, &path[len], sizeof(devpath));
strlcat(devpath, "/", sizeof(devpath));
strlcat(devpath, dent->d_name, sizeof(devpath));
- sysfs_resolve_link(devpath, sizeof(devpath));
- name_list_add(device_list, devpath, 1);
+ sysfs_resolve_link(udev, devpath, sizeof(devpath));
+ name_list_add(udev, device_list, devpath, 1);
}
closedir(dir);
return 0;
@@ -101,7 +101,7 @@ static int devices_delay(struct udev *udev, const char *devpath)
for (i = 0; delay_device_list[i] != NULL; i++) {
if (strstr(devpath, delay_device_list[i]) != NULL) {
- log_info(udev, "delaying: %s\n", devpath);
+ info(udev, "delaying: %s\n", devpath);
return 1;
}
}
@@ -158,7 +158,7 @@ int udev_devices_enumerate(struct udev *udev, const char *subsystem,
INIT_LIST_HEAD(&device_list);
/* if we have /sys/subsystem/, forget all the old stuff */
- strlcpy(base, sysfs_path, sizeof(base));
+ strlcpy(base, udev_get_sys_path(udev), sizeof(base));
strlcat(base, "/subsystem", sizeof(base));
if (stat(base, &statbuf) == 0) {
devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c
index 92617e8888..09845576d9 100644
--- a/udev/lib/libudev-monitor.c
+++ b/udev/lib/libudev-monitor.c
@@ -86,13 +86,13 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
udev_monitor->socket = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (udev_monitor->socket == -1) {
- log_err(udev, "error getting socket: %s\n", strerror(errno));
+ err(udev, "error getting socket: %s\n", strerror(errno));
free(udev_monitor);
return NULL;
}
if (bind(udev_monitor->socket, (struct sockaddr *) &saddr, addrlen) < 0) {
- log_err(udev, "bind failed: %s\n", strerror(errno));
+ err(udev, "bind failed: %s\n", strerror(errno));
close(udev_monitor->socket);
free(udev_monitor);
return NULL;
@@ -100,7 +100,7 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
/* enable receiving of the sender credentials */
setsockopt(udev_monitor->socket, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
- log_info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
+ info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
return udev_monitor;
}
@@ -138,7 +138,7 @@ void udev_monitor_unref(struct udev_monitor *udev_monitor)
if (udev_monitor->refcount > 0)
return;
close(udev_monitor->socket);
- log_info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
+ info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
free(udev_monitor);
}
@@ -212,32 +212,32 @@ struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
if (recvmsg(udev_monitor->socket, &smsg, 0) < 0) {
if (errno != EINTR)
- log_info(udev_monitor->udev, "unable to receive message");
+ info(udev_monitor->udev, "unable to receive message");
return NULL;
}
cmsg = CMSG_FIRSTHDR(&smsg);
cred = (struct ucred *)CMSG_DATA (cmsg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
- log_info(udev_monitor->udev, "no sender credentials received, message ignored");
+ info(udev_monitor->udev, "no sender credentials received, message ignored");
return NULL;
}
if (cred->uid != 0) {
- log_info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
+ info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
return NULL;
}
/* skip header */
bufpos = strlen(buf) + 1;
if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
- log_info(udev_monitor->udev, "invalid message length");
+ info(udev_monitor->udev, "invalid message length");
return NULL;
}
/* check message header */
if (strstr(buf, "@/") == NULL) {
- log_info(udev_monitor->udev, "unrecognized message header");
+ info(udev_monitor->udev, "unrecognized message header");
return NULL;
}
diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h
index ffdf571513..ef26ec7e11 100644
--- a/udev/lib/libudev-private.h
+++ b/udev/lib/libudev-private.h
@@ -20,31 +20,39 @@
#ifndef _LIBUDEV_PRIVATE_H_
#define _LIBUDEV_PRIVATE_H_
+#include "config.h"
+
+#include <syslog.h>
#include "libudev.h"
#include "../udev.h"
#ifdef USE_LOG
-#define log_dbg(udev, arg...) \
+#ifdef USE_DEBUG
+#define dbg(udev, arg...) \
udev_log(udev, LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, ## arg)
+#else
+#define dbg(format, arg...) do { } while (0)
+#endif /* USE_DEBUG */
-#define log_info(udev, arg...) \
+#define info(udev, arg...) \
udev_log(udev, LOG_INFO, __FILE__, __LINE__, __FUNCTION__, ## arg)
-#define log_err(udev, arg...) \
+#define err(udev, arg...) \
udev_log(udev, LOG_ERR, __FILE__, __LINE__, __FUNCTION__, ## arg)
+#else
+#define dbg(format, arg...) do { } while (0)
+#define info(format, arg...) do { } while (0)
+#define err(format, arg...) do { } while (0)
+#endif
+/* libudev */
void udev_log(struct udev *udev,
int priority, const char *file, int line, const char *fn,
const char *format, ...)
__attribute__ ((format(printf, 6, 7)));
-#else
-#define log_dbg(format, arg...) do { } while (0)
-#define log_info(format, arg...) do { } while (0)
-#define log_err(format, arg...) do { } while (0)
-#endif
-
-/* libudev */
extern struct udev_device *device_init(struct udev *udev);
+extern const char *udev_get_rules_path(struct udev *udev);
+extern int udev_get_run(struct udev *udev);
/* libudev-device */
extern int device_set_devpath(struct udev_device *udev_device, const char *devpath);
diff --git a/udev/lib/libudev.c b/udev/lib/libudev.c
index a8a8a5ac3a..5a00f44de9 100644
--- a/udev/lib/libudev.c
+++ b/udev/lib/libudev.c
@@ -26,8 +26,7 @@
#include <unistd.h>
#include <errno.h>
#include <string.h>
-#include <dirent.h>
-#include <sys/stat.h>
+#include <ctype.h>
#include "libudev.h"
#include "libudev-private.h"
@@ -38,6 +37,11 @@ struct udev {
void (*log_fn)(struct udev *udev,
int priority, const char *file, int line, const char *fn,
const char *format, va_list args);
+ char *sys_path;
+ char *dev_path;
+ char *rules_path;
+ int log_priority;
+ int run:1;
};
void udev_log(struct udev *udev,
@@ -46,6 +50,9 @@ void udev_log(struct udev *udev,
{
va_list args;
+ if (priority > udev->log_priority)
+ return;
+
va_start(args, format);
udev->log_fn(udev, priority, file, line, fn, format, args);
va_end(args);
@@ -55,32 +62,9 @@ static void log_stderr(struct udev *udev,
int priority, const char *file, int line, const char *fn,
const char *format, va_list args)
{
- static int log = -1;
-
- if (log == -1) {
- if (getenv("LIBUDEV_DEBUG") != NULL)
- log = 1;
- else
- log = 0;
- }
-
- if (log == 1) {
- fprintf(stderr, "libudev: %s: ", fn);
- vfprintf(stderr, format, args);
- }
-}
-
-/* glue to udev logging, needed until udev logging code is "fixed" */
-#ifdef USE_LOG
-void log_message(int priority, const char *format, ...)
-{
- va_list args;
-
- va_start(args, format);
- log_stderr(NULL, priority, NULL, 0, "", format, args);
- va_end(args);
+ fprintf(stderr, "libudev: %s: ", fn);
+ vfprintf(stderr, format, args);
}
-#endif
/**
* udev_new:
@@ -95,17 +79,159 @@ void log_message(int priority, const char *format, ...)
struct udev *udev_new(void)
{
struct udev *udev;
+ const char *env;
+ char *config_file;
+ FILE *f;
udev = malloc(sizeof(struct udev));
if (udev == NULL)
return NULL;
memset(udev, 0x00, (sizeof(struct udev)));
+
+ sysfs_init();
+
+ /* defaults */
+ config_file = NULL;
udev->refcount = 1;
udev->log_fn = log_stderr;
- udev_config_init();
- sysfs_init();
- log_info(udev, "context %p created\n", udev);
+ udev->log_priority = LOG_ERR;
+ udev->run = 1;
+ udev->dev_path = strdup(UDEV_PREFIX "/dev");
+ udev->sys_path = strdup("/sys");
+ config_file = strdup(SYSCONFDIR "/udev/udev.conf");
+
+ if (udev->dev_path == NULL || udev->sys_path == NULL)
+ goto err;
+
+ /* settings by environment and config file */
+ env = getenv("SYSFS_PATH");
+ if (env != NULL) {
+ free(udev->sys_path);
+ udev->sys_path = strdup(env);
+ remove_trailing_chars(udev->sys_path, '/');
+ }
+
+ env = getenv("UDEV_RUN");
+ if (env != NULL && !string_is_true(env))
+ udev->run = 0;
+
+ env = getenv("UDEV_CONFIG_FILE");
+ if (env != NULL) {
+ free(config_file);
+ config_file = strdup(env);
+ remove_trailing_chars(config_file, '/');
+ }
+ if (config_file == NULL)
+ goto err;
+ f = fopen(config_file, "r");
+ if (f != NULL) {
+ char line[LINE_SIZE];
+ int line_nr = 0;
+
+ while (fgets(line, sizeof(line), f)) {
+ size_t len;
+ char *key;
+ char *val;
+
+ line_nr++;
+
+ /* find key */
+ key = line;
+ while (isspace(key[0]))
+ key++;
+
+ /* comment or empty line */
+ if (key[0] == '#' || key[0] == '\0')
+ continue;
+
+ /* split key/value */
+ val = strchr(key, '=');
+ if (val == NULL) {
+ err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr);
+ continue;
+ }
+ val[0] = '\0';
+ val++;
+
+ /* find value */
+ while (isspace(val[0]))
+ val++;
+
+ /* terminate key */
+ len = strlen(key);
+ if (len == 0)
+ continue;
+ while (isspace(key[len-1]))
+ len--;
+ key[len] = '\0';
+
+ /* terminate value */
+ len = strlen(val);
+ if (len == 0)
+ continue;
+ while (isspace(val[len-1]))
+ len--;
+ val[len] = '\0';
+
+ if (len == 0)
+ continue;
+
+ /* unquote */
+ if (val[0] == '"' || val[0] == '\'') {
+ if (val[len-1] != val[0]) {
+ err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr);
+ continue;
+ }
+ val[len-1] = '\0';
+ val++;
+ }
+
+ if (strcasecmp(key, "udev_log") == 0) {
+ udev->log_priority = log_priority(val);
+ continue;
+ }
+ if (strcasecmp(key, "udev_root") == 0) {
+ free(udev->dev_path);
+ udev->dev_path = strdup(val);
+ remove_trailing_chars(udev->dev_path, '/');
+ continue;
+ }
+ if (strcasecmp(key, "udev_rules") == 0) {
+ free(udev->rules_path);
+ udev->rules_path = strdup(val);
+ remove_trailing_chars(udev->rules_path, '/');
+ continue;
+ }
+ }
+ fclose(f);
+ }
+ free(config_file);
+
+ env = getenv("UDEV_ROOT");
+ if (env != NULL) {
+ free(udev->dev_path);
+ udev->dev_path = strdup(env);
+ remove_trailing_chars(udev->dev_path, '/');
+ }
+
+ env = getenv("UDEV_LOG");
+ if (env != NULL)
+ udev->log_priority = log_priority(env);
+
+ if (udev->dev_path == NULL || udev->sys_path == NULL)
+ goto err;
+
+ info(udev, "context %p created\n", udev);
+ info(udev, "log_priority=%d\n", udev->log_priority);
+ info(udev, "dev_path='%s'\n", udev->dev_path);
+ if (udev->rules_path != NULL)
+ info(udev, "rules_path='%s'\n", udev->rules_path);
+
return udev;
+err:
+ err(udev, "context creation failed\n");
+ udev_unref(udev);
+ return NULL;
}
/**
@@ -140,7 +266,9 @@ void udev_unref(struct udev *udev)
if (udev->refcount > 0)
return;
sysfs_cleanup();
- log_info(udev, "context %p released\n", udev);
+ free(udev->dev_path);
+ free(udev->sys_path);
+ info(udev, "context %p released\n", udev);
free(udev);
}
@@ -161,7 +289,27 @@ void udev_set_log_fn(struct udev *udev,
const char *format, va_list args))
{
udev->log_fn = log_fn;
- log_info(udev, "custom logging function %p registered\n", udev);
+ info(udev, "custom logging function %p registered\n", udev);
+}
+
+int udev_get_log_priority(struct udev *udev)
+{
+ return udev->log_priority;
+}
+
+void udev_set_log_priority(struct udev *udev, int priority)
+{
+ udev->log_priority = priority;
+}
+
+const char *udev_get_rules_path(struct udev *udev)
+{
+ return udev->rules_path;
+}
+
+int udev_get_run(struct udev *udev)
+{
+ return udev->run;
}
/**
@@ -178,7 +326,7 @@ const char *udev_get_sys_path(struct udev *udev)
{
if (udev == NULL)
return NULL;
- return sysfs_path;
+ return udev->sys_path;
}
/**
@@ -195,5 +343,5 @@ const char *udev_get_dev_path(struct udev *udev)
{
if (udev == NULL)
return NULL;
- return udev_root;
+ return udev->dev_path;
}
diff --git a/udev/lib/libudev.h b/udev/lib/libudev.h
index 30753a19a7..98652c5904 100644
--- a/udev/lib/libudev.h
+++ b/udev/lib/libudev.h
@@ -20,6 +20,8 @@
#ifndef _LIBUDEV_H_
#define _LIBUDEV_H_
+#include <stdarg.h>
+
/* this will stay as long as the DeviceKit integration of udev is work in progress */
#if !defined _LIBUDEV_COMPILATION && !defined LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
#error "#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE is needed to use this experimental library version"
@@ -36,6 +38,8 @@ extern void udev_set_log_fn(struct udev *udev,
void (*log_fn)(struct udev *udev,
int priority, const char *file, int line, const char *fn,
const char *format, va_list args));
+extern int udev_get_log_priority(struct udev *udev);
+extern void udev_set_log_priority(struct udev *udev, int priority);
extern const char *udev_get_sys_path(struct udev *udev);
extern const char *udev_get_dev_path(struct udev *udev);