/*
* libudev - interface to udev device information
*
- * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
char *sys_path;
char *dev_path;
char *rules_path;
+ char *run_config_path;
+ char *run_path;
struct udev_list_node properties_list;
int log_priority;
- int run;
};
void udev_log(struct udev *udev,
* @udev: udev library context
*
* Retrieve stored data pointer from library context. This might be useful
- * to access from callbacks.
+ * to access from callbacks like a custom logging function.
*
* Returns: stored userdata
**/
udev->userdata = userdata;
}
+static char *set_value(char **s, const char *v)
+{
+ free(*s);
+ *s = strdup(v);
+ util_remove_trailing_chars(*s, '/');
+ return *s;
+}
+
/**
* udev_new:
*
{
struct udev *udev;
const char *env;
- char *config_file;
+ char *config_file = NULL;
FILE *f;
udev = calloc(1, sizeof(struct udev));
udev->log_fn = log_stderr;
udev->log_priority = LOG_ERR;
udev_list_init(&udev->properties_list);
- udev->run = 1;
- udev->dev_path = strdup("/dev");
- udev->sys_path = strdup("/sys");
- config_file = strdup(SYSCONFDIR "/udev/udev.conf");
- if (udev->dev_path == NULL ||
- udev->sys_path == NULL ||
- config_file == 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);
- util_remove_trailing_chars(udev->sys_path, '/');
- udev_add_property(udev, "SYSFS_PATH", udev->sys_path);
- }
-
- env = getenv("UDEV_RUN");
- if (env != NULL && strcmp(env, "0") == 0)
- udev->run = 0;
+ /* custom config file */
env = getenv("UDEV_CONFIG_FILE");
if (env != NULL) {
- free(config_file);
- config_file = strdup(env);
- util_remove_trailing_chars(config_file, '/');
+ udev_add_property(udev, "UDEV_CONFIG_FILE", udev->dev_path);
+ if (set_value(&config_file, env) == NULL)
+ goto err;
}
+
+ /* default config file */
+ if (config_file == NULL)
+ config_file = strdup(SYSCONFDIR "/udev/udev.conf");
if (config_file == NULL)
goto err;
- f = fopen(config_file, "r");
+
+ f = fopen(config_file, "re");
if (f != NULL) {
char line[UTIL_LINE_SIZE];
int line_nr = 0;
continue;
}
if (strcmp(key, "udev_root") == 0) {
- free(udev->dev_path);
- udev->dev_path = strdup(val);
- util_remove_trailing_chars(udev->dev_path, '/');
+ set_value(&udev->dev_path, val);
+ continue;
+ }
+ if (strcmp(key, "udev_run") == 0) {
+ set_value(&udev->run_config_path, val);
continue;
}
if (strcmp(key, "udev_rules") == 0) {
- free(udev->rules_path);
- udev->rules_path = strdup(val);
- util_remove_trailing_chars(udev->rules_path, '/');
+ set_value(&udev->rules_path, val);
continue;
}
}
fclose(f);
}
+ /* environment overwrites config */
+ env = getenv("UDEV_LOG");
+ if (env != NULL)
+ udev_set_log_priority(udev, util_log_priority(env));
+
env = getenv("UDEV_ROOT");
if (env != NULL) {
- free(udev->dev_path);
- udev->dev_path = strdup(env);
- util_remove_trailing_chars(udev->dev_path, '/');
+ set_value(&udev->dev_path, env);
udev_add_property(udev, "UDEV_ROOT", udev->dev_path);
}
- env = getenv("UDEV_LOG");
- if (env != NULL)
- udev_set_log_priority(udev, util_log_priority(env));
+ env = getenv("SYSFS_PATH");
+ if (env != NULL) {
+ set_value(&udev->sys_path, env);
+ udev_add_property(udev, "SYSFS_PATH", udev->sys_path);
+ }
+
+ /* set defaults */
+ if (udev->dev_path == NULL)
+ if (set_value(&udev->dev_path, "/dev") == NULL)
+ goto err;
+
+ if (udev->sys_path == NULL)
+ if (set_value(&udev->sys_path, "/sys") == NULL)
+ goto err;
+
+ if (udev->run_config_path == NULL)
+ if (set_value(&udev->run_config_path, "/run/udev") == NULL)
+ goto err;
- if (udev->dev_path == NULL || udev->sys_path == NULL)
- goto err;
dbg(udev, "context %p created\n", udev);
dbg(udev, "log_priority=%d\n", udev->log_priority);
dbg(udev, "config_file='%s'\n", config_file);
dbg(udev, "dev_path='%s'\n", udev->dev_path);
dbg(udev, "sys_path='%s'\n", udev->sys_path);
+ dbg(udev, "run_path='%s'\n", udev->run_config_path);
if (udev->rules_path != NULL)
dbg(udev, "rules_path='%s'\n", udev->rules_path);
free(config_file);
free(udev->dev_path);
free(udev->sys_path);
free(udev->rules_path);
+ free(udev->run_path);
+ free(udev->run_config_path);
dbg(udev, "context %p released\n", udev);
free(udev);
}
* udev_get_log_priority:
* @udev: udev library context
*
- * The initial syslog priortity is read from the udev config file
+ * The initial logging priority is read from the udev config file
* at startup.
*
- * Returns: the current syslog priority
+ * Returns: the current logging priority
**/
int udev_get_log_priority(struct udev *udev)
{
/**
* udev_set_log_priority:
* @udev: udev library context
- * @priority: the new syslog priority
+ * @priority: the new logging priority
*
- * Set the current syslog priority. The value controls which messages
- * are send to syslog.
+ * Set the current logging priority. The value controls which messages
+ * are logged.
**/
void udev_set_log_priority(struct udev *udev, int priority)
{
return udev->rules_path;
}
-int udev_get_run(struct udev *udev)
-{
- return udev->run;
-}
-
/**
* udev_get_sys_path:
* @udev: udev library context
return udev->dev_path;
}
+const char *udev_get_run_config_path(struct udev *udev)
+{
+ return udev->run_config_path;
+}
+
+/**
+ * udev_get_run_path:
+ * @udev: udev library context
+ *
+ * Retrieve the udev runtime directory path. The default is "/run/udev".
+ *
+ * Returns: the runtime directory path
+ **/
+const char *udev_get_run_path(struct udev *udev)
+{
+ if (udev->run_path != NULL)
+ return udev->run_path;
+
+ /* check if configured path exists */
+ if (access(udev->run_config_path, F_OK) < 0) {
+ char filename[UTIL_PATH_SIZE];
+
+ /* fall back to /dev/.udev if that exists */
+ util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev", NULL);
+ if (access(filename, F_OK) >= 0)
+ if (set_value(&udev->run_path, filename) != NULL)
+ return udev->run_path;
+ }
+
+ /* use default path */
+ set_value(&udev->run_path, udev->run_config_path);
+ if (udev->run_path == NULL)
+ return udev->run_config_path;
+ return udev->run_path;
+}
+
+const char *udev_set_run_path(struct udev *udev, const char *path)
+{
+ return set_value(&udev->run_path, path);
+}
+
struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
{
if (value == NULL) {