This source file includes following definitions.
- udev_log
- _printf_
- udev_get_userdata
- udev_set_userdata
- udev_new
- udev_ref
- udev_unref
- udev_set_log_fn
- udev_get_log_priority
- udev_set_log_priority
- udev_add_property
- udev_get_properties_list_entry
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "libudev.h"
#include "libudev-private.h"
#include "missing.h"
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;
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, ...)
{
va_list args;
va_start(args, format);
udev->log_fn(udev, priority, file, line, fn, format, args);
va_end(args);
}
_printf_(6,0)
static void log_stderr(struct udev *udev,
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);
}
_public_ void *udev_get_userdata(struct udev *udev)
{
if (udev == NULL)
return NULL;
return udev->userdata;
}
_public_ void udev_set_userdata(struct udev *udev, void *userdata)
{
if (udev == NULL)
return;
udev->userdata = userdata;
}
_public_ struct udev *udev_new(void)
{
struct udev *udev;
const char *env;
_cleanup_fclose_ FILE *f = NULL;
udev = new0(struct udev, 1);
if (udev == NULL)
return NULL;
udev->refcount = 1;
udev->log_fn = log_stderr;
udev->log_priority = LOG_INFO;
udev_list_init(udev, &udev->properties_list, true);
f = fopen("/etc/udev/udev.conf", "re");
if (f != NULL) {
char line[UTIL_LINE_SIZE];
unsigned line_nr = 0;
while (fgets(line, sizeof(line), f)) {
size_t len;
char *key;
char *val;
line_nr++;
key = line;
while (isspace(key[0]))
key++;
if (key[0] == '#' || key[0] == '\0')
continue;
val = strchr(key, '=');
if (val == NULL) {
udev_err(udev, "/etc/udev/udev.conf:%u: missing assignment, skipping line.\n", line_nr);
continue;
}
val[0] = '\0';
val++;
while (isspace(val[0]))
val++;
len = strlen(key);
if (len == 0)
continue;
while (isspace(key[len-1]))
len--;
key[len] = '\0';
len = strlen(val);
if (len == 0)
continue;
while (isspace(val[len-1]))
len--;
val[len] = '\0';
if (len == 0)
continue;
if (val[0] == '"' || val[0] == '\'') {
if (val[len-1] != val[0]) {
udev_err(udev, "/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.\n", line_nr);
continue;
}
val[len-1] = '\0';
val++;
}
if (streq(key, "udev_log")) {
int prio;
prio = util_log_priority(val);
if (prio < 0)
udev_err(udev, "/etc/udev/udev.conf:%u: invalid logging level '%s', ignoring.\n", line_nr, val);
else
udev_set_log_priority(udev, prio);
continue;
}
}
}
env = secure_getenv("UDEV_LOG");
if (env != NULL) {
int prio;
prio = util_log_priority(env);
if (prio < 0)
udev_err(udev, "$UDEV_LOG specifies invalid logging level '%s', ignoring.\n", env);
else
udev_set_log_priority(udev, prio);
}
return udev;
}
_public_ struct udev *udev_ref(struct udev *udev)
{
if (udev == NULL)
return NULL;
udev->refcount++;
return udev;
}
_public_ struct udev *udev_unref(struct udev *udev)
{
if (udev == NULL)
return NULL;
udev->refcount--;
if (udev->refcount > 0)
return udev;
udev_list_cleanup(&udev->properties_list);
free(udev);
return NULL;
}
_public_ 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;
udev_dbg(udev, "custom logging function %p registered\n", log_fn);
}
_public_ int udev_get_log_priority(struct udev *udev)
{
return udev->log_priority;
}
_public_ void udev_set_log_priority(struct udev *udev, int priority)
{
char num[32];
udev->log_priority = priority;
snprintf(num, sizeof(num), "%u", udev->log_priority);
udev_add_property(udev, "UDEV_LOG", num);
}
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);
}
struct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
{
return udev_list_get_entry(&udev->properties_list);
}