X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibudev.c;h=be24329adc85b2edb858fd0b0b2bb0e1acd7b80a;hb=912541b0246ef315d4d851237483b98c9dd3f992;hp=f0f59e3a4dad229d9bde0719d392a6d0ca6627bc;hpb=ebda27438b66d179c4ba4ac74bbe20df2d57446e;p=elogind.git diff --git a/src/libudev.c b/src/libudev.c index f0f59e3a4..be24329ad 100644 --- a/src/libudev.c +++ b/src/libudev.c @@ -36,38 +36,38 @@ * Opaque object representing the library context. */ struct udev { - int refcount; - void (*log_fn)(struct udev *udev, - int priority, const char *file, int line, const char *fn, - const char *format, va_list args); - void *userdata; - char *sys_path; - char *dev_path; - 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 refcount; + void (*log_fn)(struct udev *udev, + int priority, const char *file, int line, const char *fn, + const char *format, va_list args); + void *userdata; + char *sys_path; + char *dev_path; + 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; }; void udev_log(struct udev *udev, - int priority, const char *file, int line, const char *fn, - const char *format, ...) + int priority, const char *file, int line, const char *fn, + const char *format, ...) { - va_list args; + va_list args; - va_start(args, format); - udev->log_fn(udev, priority, file, line, fn, format, args); - va_end(args); + va_start(args, format); + udev->log_fn(udev, priority, file, line, fn, format, args); + va_end(args); } static void log_stderr(struct udev *udev, - int priority, const char *file, int line, const char *fn, - const char *format, va_list args) + int priority, const char *file, int line, const char *fn, + const char *format, va_list args) { - fprintf(stderr, "libudev: %s: ", fn); - vfprintf(stderr, format, args); + fprintf(stderr, "libudev: %s: ", fn); + vfprintf(stderr, format, args); } /** @@ -81,9 +81,9 @@ static void log_stderr(struct udev *udev, **/ UDEV_EXPORT void *udev_get_userdata(struct udev *udev) { - if (udev == NULL) - return NULL; - return udev->userdata; + if (udev == NULL) + return NULL; + return udev->userdata; } /** @@ -95,17 +95,17 @@ UDEV_EXPORT void *udev_get_userdata(struct udev *udev) **/ UDEV_EXPORT void udev_set_userdata(struct udev *udev, void *userdata) { - if (udev == NULL) - return; - udev->userdata = 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; + free(*s); + *s = strdup(v); + util_remove_trailing_chars(*s, '/'); + return *s; } /** @@ -121,171 +121,171 @@ static char *set_value(char **s, const char *v) **/ UDEV_EXPORT struct udev *udev_new(void) { - struct udev *udev; - const char *env; - char *config_file = NULL; - FILE *f; - - udev = calloc(1, sizeof(struct udev)); - if (udev == NULL) - return NULL; - udev->refcount = 1; - udev->log_fn = log_stderr; - udev->log_priority = LOG_ERR; - udev_list_init(udev, &udev->properties_list, true); - - /* custom config file */ - env = getenv("UDEV_CONFIG_FILE"); - if (env != NULL) { - 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, "re"); - if (f != NULL) { - char line[UTIL_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 = 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 (strcmp(key, "udev_log") == 0) { - udev_set_log_priority(udev, util_log_priority(val)); - continue; - } - if (strcmp(key, "udev_root") == 0) { - 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) { - set_value(&udev->rules_path[0], val); - udev->rules_path_count = 1; - continue; - } - } - fclose(f); - } - - /* environment overwrites config */ - env = getenv("UDEV_LOG"); - if (env != NULL) - udev_set_log_priority(udev, util_log_priority(env)); - - /* 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(PKGLIBEXECDIR "/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); - 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; + struct udev *udev; + const char *env; + char *config_file = NULL; + FILE *f; + + udev = calloc(1, sizeof(struct udev)); + if (udev == NULL) + return NULL; + udev->refcount = 1; + udev->log_fn = log_stderr; + udev->log_priority = LOG_ERR; + udev_list_init(udev, &udev->properties_list, true); + + /* custom config file */ + env = getenv("UDEV_CONFIG_FILE"); + if (env != NULL) { + 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, "re"); + if (f != NULL) { + char line[UTIL_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 = 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 (strcmp(key, "udev_log") == 0) { + udev_set_log_priority(udev, util_log_priority(val)); + continue; + } + if (strcmp(key, "udev_root") == 0) { + 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) { + set_value(&udev->rules_path[0], val); + udev->rules_path_count = 1; + continue; + } + } + fclose(f); + } + + /* environment overwrites config */ + env = getenv("UDEV_LOG"); + if (env != NULL) + udev_set_log_priority(udev, util_log_priority(env)); + + /* 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(PKGLIBEXECDIR "/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); + 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: - free(config_file); - err(udev, "context creation failed\n"); - udev_unref(udev); - return NULL; + free(config_file); + err(udev, "context creation failed\n"); + udev_unref(udev); + return NULL; } /** @@ -298,10 +298,10 @@ err: **/ UDEV_EXPORT struct udev *udev_ref(struct udev *udev) { - if (udev == NULL) - return NULL; - udev->refcount++; - return udev; + if (udev == NULL) + return NULL; + udev->refcount++; + return udev; } /** @@ -314,20 +314,20 @@ UDEV_EXPORT struct udev *udev_ref(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(&udev->properties_list); - free(udev->dev_path); - free(udev->sys_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); + if (udev == NULL) + return; + udev->refcount--; + if (udev->refcount > 0) + return; + udev_list_cleanup(&udev->properties_list); + free(udev->dev_path); + free(udev->sys_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); } /** @@ -341,12 +341,12 @@ UDEV_EXPORT void udev_unref(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)) + 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", log_fn); + udev->log_fn = log_fn; + info(udev, "custom logging function %p registered\n", log_fn); } /** @@ -360,7 +360,7 @@ UDEV_EXPORT void udev_set_log_fn(struct udev *udev, **/ UDEV_EXPORT int udev_get_log_priority(struct udev *udev) { - return udev->log_priority; + return udev->log_priority; } /** @@ -373,19 +373,19 @@ UDEV_EXPORT int udev_get_log_priority(struct udev *udev) **/ UDEV_EXPORT void udev_set_log_priority(struct udev *udev, int priority) { - char num[32]; + char num[32]; - udev->log_priority = priority; - snprintf(num, sizeof(num), "%u", udev->log_priority); - udev_add_property(udev, "UDEV_LOG", num); + udev->log_priority = priority; + snprintf(num, sizeof(num), "%u", udev->log_priority); + udev_add_property(udev, "UDEV_LOG", num); } int udev_get_rules_path(struct udev *udev, char **path[], unsigned long long *stamp_usec[]) { - *path = udev->rules_path; - if (stamp_usec) - *stamp_usec = udev->rules_path_ts; - return udev->rules_path_count; + *path = udev->rules_path; + if (stamp_usec) + *stamp_usec = udev->rules_path_ts; + return udev->rules_path_count; } /** @@ -400,9 +400,9 @@ int udev_get_rules_path(struct udev *udev, char **path[], unsigned long long *st **/ UDEV_EXPORT const char *udev_get_sys_path(struct udev *udev) { - if (udev == NULL) - return NULL; - return udev->sys_path; + if (udev == NULL) + return NULL; + return udev->sys_path; } /** @@ -417,9 +417,9 @@ UDEV_EXPORT const char *udev_get_sys_path(struct udev *udev) **/ UDEV_EXPORT const char *udev_get_dev_path(struct udev *udev) { - if (udev == NULL) - return NULL; - return udev->dev_path; + if (udev == NULL) + return NULL; + return udev->dev_path; } /** @@ -432,26 +432,26 @@ UDEV_EXPORT const char *udev_get_dev_path(struct udev *udev) **/ UDEV_EXPORT const char *udev_get_run_path(struct udev *udev) { - if (udev == NULL) - return NULL; - return udev->run_path; + 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) { - struct udev_list_entry *list_entry; - - list_entry = udev_get_properties_list_entry(udev); - list_entry = udev_list_entry_get_by_name(list_entry, key); - if (list_entry != NULL) - udev_list_entry_delete(list_entry); - return NULL; - } - return udev_list_entry_add(&udev->properties_list, key, value); + if (value == NULL) { + struct udev_list_entry *list_entry; + + list_entry = udev_get_properties_list_entry(udev); + list_entry = udev_list_entry_get_by_name(list_entry, key); + if (list_entry != NULL) + udev_list_entry_delete(list_entry); + return NULL; + } + return udev_list_entry_add(&udev->properties_list, key, value); } struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev) { - return udev_list_get_entry(&udev->properties_list); + return udev_list_get_entry(&udev->properties_list); }