chiark / gitweb /
remove mknod() logic and rely on 'devtmpfs'
[elogind.git] / libudev / libudev.c
index 190913871ea9b0ff5671010e1650bfec212ab11e..ca2ac5d03069fe0c4322faa3f7ba5ea6ea3a220f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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
@@ -17,6 +17,7 @@
 #include <errno.h>
 #include <string.h>
 #include <ctype.h>
+#include <time.h>
 
 #include "libudev.h"
 #include "libudev-private.h"
@@ -42,10 +43,12 @@ struct udev {
        void *userdata;
        char *sys_path;
        char *dev_path;
-       char *rules_path;
-       struct udev_list_node properties_list;
+       char *rules_path[4];
+       unsigned long long rules_path_ts[4];
+       int rules_path_count;
+       char *run_path;
+       struct udev_list properties_list;
        int log_priority;
-       int run;
 };
 
 void udev_log(struct udev *udev,
@@ -72,11 +75,11 @@ static void log_stderr(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
  **/
-void *udev_get_userdata(struct udev *udev)
+UDEV_EXPORT void *udev_get_userdata(struct udev *udev)
 {
        if (udev == NULL)
                return NULL;
@@ -90,13 +93,21 @@ void *udev_get_userdata(struct udev *udev)
  *
  * Store custom @userdata in the library context.
  **/
-void udev_set_userdata(struct udev *udev, void *userdata)
+UDEV_EXPORT void udev_set_userdata(struct udev *udev, void *userdata)
 {
        if (udev == NULL)
                return;
        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:
  *
@@ -108,11 +119,11 @@ void udev_set_userdata(struct udev *udev, void *userdata)
  *
  * Returns: a new udev library context
  **/
-struct udev *udev_new(void)
+UDEV_EXPORT struct udev *udev_new(void)
 {
        struct udev *udev;
        const char *env;
-       char *config_file;
+       char *config_file = NULL;
        FILE *f;
 
        udev = calloc(1, sizeof(struct udev));
@@ -121,38 +132,23 @@ struct udev *udev_new(void)
        udev->refcount = 1;
        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;
+       udev_list_init(udev, &udev->properties_list, true);
 
+       /* custom config file */
        env = getenv("UDEV_CONFIG_FILE");
        if (env != NULL) {
-               free(config_file);
-               config_file = strdup(env);
-               util_remove_trailing_chars(config_file, '/');
+               if (set_value(&config_file, env) == NULL)
+                       goto err;
+               udev_add_property(udev, "UDEV_CONFIG_FILE", config_file);
        }
+
+       /* 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;
@@ -220,42 +216,69 @@ struct udev *udev_new(void)
                                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_path, val);
+                               continue;
+                       }
+                       if (strcmp(key, "udev_sys") == 0) {
+                               set_value(&udev->sys_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[0], val);
+                               udev->rules_path_count = 1;
                                continue;
                        }
                }
                fclose(f);
        }
 
-       env = getenv("UDEV_ROOT");
-       if (env != NULL) {
-               free(udev->dev_path);
-               udev->dev_path = strdup(env);
-               util_remove_trailing_chars(udev->dev_path, '/');
-               udev_add_property(udev, "UDEV_ROOT", udev->dev_path);
-       }
-
+       /* environment overwrites config */
        env = getenv("UDEV_LOG");
        if (env != NULL)
                udev_set_log_priority(udev, util_log_priority(env));
 
-       if (udev->dev_path == NULL || udev->sys_path == NULL)
-               goto err;
+       /* 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_path == NULL)
+               if (set_value(&udev->run_path, "/run/udev") == NULL)
+                       goto err;
+
+       if (udev->rules_path[0] == NULL) {
+               /* /usr/lib/udev -- system rules */
+               udev->rules_path[0] = strdup(LIBEXECDIR "/rules.d");
+               if (!udev->rules_path[0])
+                       goto err;
+
+               /* /etc/udev -- local administration rules */
+               udev->rules_path[1] = strdup(SYSCONFDIR "/udev/rules.d");
+               if (!udev->rules_path[1])
+                       goto err;
+
+               /* /run/udev -- runtime rules */
+               if (asprintf(&udev->rules_path[2], "%s/rules.d", udev->run_path) < 0)
+                       goto err;
+
+               udev->rules_path_count = 3;
+       }
+
        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);
-       if (udev->rules_path != NULL)
-               dbg(udev, "rules_path='%s'\n", udev->rules_path);
+       dbg(udev, "run_path='%s'\n", udev->run_path);
+       dbg(udev, "rules_path='%s':'%s':'%s'\n", udev->rules_path[0], udev->rules_path[1], udev->rules_path[2]);
        free(config_file);
        return udev;
 err:
@@ -273,7 +296,7 @@ err:
  *
  * Returns: the passed udev library context
  **/
-struct udev *udev_ref(struct udev *udev)
+UDEV_EXPORT struct udev *udev_ref(struct udev *udev)
 {
        if (udev == NULL)
                return NULL;
@@ -289,17 +312,20 @@ struct udev *udev_ref(struct udev *udev)
  * reaches zero, the resources of the context will be released.
  *
  **/
-void udev_unref(struct udev *udev)
+UDEV_EXPORT void udev_unref(struct udev *udev)
 {
        if (udev == NULL)
                return;
        udev->refcount--;
        if (udev->refcount > 0)
                return;
-       udev_list_cleanup_entries(udev, &udev->properties_list);
+       udev_list_cleanup(&udev->properties_list);
        free(udev->dev_path);
        free(udev->sys_path);
-       free(udev->rules_path);
+       free(udev->rules_path[0]);
+       free(udev->rules_path[1]);
+       free(udev->rules_path[2]);
+       free(udev->run_path);
        dbg(udev, "context %p released\n", udev);
        free(udev);
 }
@@ -314,25 +340,25 @@ void udev_unref(struct udev *udev)
  * into the users' logging functionality.
  *
  **/
-void udev_set_log_fn(struct udev *udev,
+UDEV_EXPORT 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))
 {
        udev->log_fn = log_fn;
-       info(udev, "custom logging function %p registered\n", udev);
+       info(udev, "custom logging function %p registered\n", log_fn);
 }
 
 /**
  * udev_get_log_priority:
  * @udev: udev library context
  *
- * The initial syslog priority 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_EXPORT int udev_get_log_priority(struct udev *udev)
 {
        return udev->log_priority;
 }
@@ -340,12 +366,12 @@ 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)
+UDEV_EXPORT void udev_set_log_priority(struct udev *udev, int priority)
 {
        char num[32];
 
@@ -354,14 +380,12 @@ void udev_set_log_priority(struct udev *udev, int priority)
        udev_add_property(udev, "UDEV_LOG", num);
 }
 
-const char *udev_get_rules_path(struct udev *udev)
-{
-       return udev->rules_path;
-}
-
-int udev_get_run(struct udev *udev)
+int udev_get_rules_path(struct udev *udev, char **path[], unsigned long long *stamp_usec[])
 {
-       return udev->run;
+       *path = udev->rules_path;
+       if (stamp_usec)
+               *stamp_usec = udev->rules_path_ts;
+       return udev->rules_path_count;
 }
 
 /**
@@ -369,12 +393,12 @@ int udev_get_run(struct udev *udev)
  * @udev: udev library context
  *
  * Retrieve the sysfs mount point. The default is "/sys". For
- * testing purposes, it can be overridden with the environment
- * variable SYSFS_PATH.
+ * testing purposes, it can be overridden with udev_sys=
+ * in the udev configuration file.
  *
  * Returns: the sys mount point
  **/
-const char *udev_get_sys_path(struct udev *udev)
+UDEV_EXPORT const char *udev_get_sys_path(struct udev *udev)
 {
        if (udev == NULL)
                return NULL;
@@ -391,13 +415,28 @@ const char *udev_get_sys_path(struct udev *udev)
  *
  * Returns: the device directory path
  **/
-const char *udev_get_dev_path(struct udev *udev)
+UDEV_EXPORT const char *udev_get_dev_path(struct udev *udev)
 {
        if (udev == NULL)
                return NULL;
        return udev->dev_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
+ **/
+UDEV_EXPORT const char *udev_get_run_path(struct udev *udev)
+{
+       if (udev == NULL)
+               return NULL;
+       return udev->run_path;
+}
+
 struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
 {
        if (value == NULL) {
@@ -409,7 +448,7 @@ struct udev_list_entry *udev_add_property(struct udev *udev, const char *key, co
                        udev_list_entry_delete(list_entry);
                return NULL;
        }
-       return udev_list_entry_add(udev, &udev->properties_list, key, value, 1, 0);
+       return udev_list_entry_add(&udev->properties_list, key, value);
 }
 
 struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)