libudev.c revision ff944daa019c1101d6464412e6682732ec11143a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * libudev - interface to udev device information
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * This library is free software; you can redistribute it and/or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * modify it under the terms of the GNU Lesser General Public
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * License as published by the Free Software Foundation; either
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * version 2.1 of the License, or (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdio.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdlib.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stddef.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdarg.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <string.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <ctype.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <time.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "libudev.h"
96aad8d15a324d0e956a4e5653a11a67b209b41aLennart Poettering#include "libudev-private.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "missing.h"
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering/**
51323288fc628a5cac50914df915545d685b793eLennart Poettering * SECTION:libudev
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * @short_description: libudev context
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering *
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * The context contains the default values read from the udev config file,
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * and is passed to all library operations.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering/**
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * udev:
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering *
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * Opaque object representing the library context.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstruct udev {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int refcount;
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering void (*log_fn)(struct udev *udev,
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering int priority, const char *file, int line, const char *fn,
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering const char *format, va_list args);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering void *userdata;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering struct udev_list properties_list;
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering int log_priority;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering};
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringvoid udev_log(struct udev *udev,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int priority, const char *file, int line, const char *fn,
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering const char *format, ...)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering{
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering va_list args;
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering va_start(args, format);
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering udev->log_fn(udev, priority, file, line, fn, format, args);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering va_end(args);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering}
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poetteringstatic void log_stderr(struct udev *udev,
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering int priority, const char *file, int line, const char *fn,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *format, va_list args)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering{
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering fprintf(stderr, "libudev: %s: ", fn);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering vfprintf(stderr, format, args);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/**
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * udev_get_userdata:
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * @udev: udev library context
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * Retrieve stored data pointer from library context. This might be useful
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * to access from callbacks like a custom logging function.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Returns: stored userdata
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering **/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ void *udev_get_userdata(struct udev *udev)
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering{
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (udev == NULL)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return udev->userdata;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/**
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * udev_set_userdata:
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * @udev: udev library context
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * @userdata: data pointer
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering *
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * Store custom @userdata in the library context.
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering **/
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering_public_ void udev_set_userdata(struct udev *udev, void *userdata)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (udev == NULL)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering udev->userdata = userdata;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering}
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering/**
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering * udev_new:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering *
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering * Create udev library context. This reads the udev configuration
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering * file, and fills in the default values.
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering *
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering * The initial refcount is 1, and needs to be decremented to
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering * release the resources of the udev library context.
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering *
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * Returns: a new udev library context
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering **/
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering_public_ struct udev *udev_new(void)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering{
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering struct udev *udev;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering const char *env;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering FILE *f;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering udev = calloc(1, sizeof(struct udev));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (udev == NULL)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return NULL;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering udev->refcount = 1;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering udev->log_fn = log_stderr;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev->log_priority = LOG_ERR;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev_list_init(udev, &udev->properties_list, true);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering f = fopen("/etc/udev/udev.conf", "re");
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (f != NULL) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering char line[UTIL_LINE_SIZE];
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering int line_nr = 0;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering while (fgets(line, sizeof(line), f)) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering size_t len;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering char *key;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering char *val;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering line_nr++;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering /* find key */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering key = line;
51323288fc628a5cac50914df915545d685b793eLennart Poettering while (isspace(key[0]))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering key++;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* comment or empty line */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (key[0] == '#' || key[0] == '\0')
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering continue;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* split key/value */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering val = strchr(key, '=');
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (val == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_err(udev, "missing <key>=<value> in /etc/udev/udev.conf[%i]; skip line\n", line_nr);
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering val[0] = '\0';
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering val++;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* find value */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering while (isspace(val[0]))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering val++;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* terminate key */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering len = strlen(key);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (len == 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
51323288fc628a5cac50914df915545d685b793eLennart Poettering while (isspace(key[len-1]))
51323288fc628a5cac50914df915545d685b793eLennart Poettering len--;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering key[len] = '\0';
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* terminate value */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering len = strlen(val);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering if (len == 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering while (isspace(val[len-1]))
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering len--;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering val[len] = '\0';
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering if (len == 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering /* unquote */
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering if (val[0] == '"' || val[0] == '\'') {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (val[len-1] != val[0]) {
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering udev_err(udev, "inconsistent quoting in /etc/udev/udev.conf[%i]; skip line\n", line_nr);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering }
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering val[len-1] = '\0';
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering val++;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (strcmp(key, "udev_log") == 0) {
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering udev_set_log_priority(udev, util_log_priority(val));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering fclose(f);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* environment overrides config */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering env = secure_getenv("UDEV_LOG");
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering if (env != NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_set_log_priority(udev, util_log_priority(env));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return udev;
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering}
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering/**
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering * udev_ref:
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * @udev: udev library context
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Take a reference of the udev library context.
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering *
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt * Returns: the passed udev library context
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering **/
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering_public_ struct udev *udev_ref(struct udev *udev)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (udev == NULL)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev->refcount++;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return udev;
51323288fc628a5cac50914df915545d685b793eLennart Poettering}
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering/**
51323288fc628a5cac50914df915545d685b793eLennart Poettering * udev_unref:
51323288fc628a5cac50914df915545d685b793eLennart Poettering * @udev: udev library context
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *
51323288fc628a5cac50914df915545d685b793eLennart Poettering * Drop a reference of the udev library context. If the refcount
51323288fc628a5cac50914df915545d685b793eLennart Poettering * reaches zero, the resources of the context will be released.
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
51323288fc628a5cac50914df915545d685b793eLennart Poettering **/
51323288fc628a5cac50914df915545d685b793eLennart Poettering_public_ struct udev *udev_unref(struct udev *udev)
51323288fc628a5cac50914df915545d685b793eLennart Poettering{
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (udev == NULL)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev->refcount--;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (udev->refcount > 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return udev;
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_list_cleanup(&udev->properties_list);
51323288fc628a5cac50914df915545d685b793eLennart Poettering free(udev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/**
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * udev_set_log_fn:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * @udev: udev library context
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * @log_fn: function to be called for logging messages
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *
51323288fc628a5cac50914df915545d685b793eLennart Poettering * The built-in logging writes to stderr. It can be
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * overridden by a custom function, to plug log messages
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * into the users' logging functionality.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering **/
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering_public_ void udev_set_log_fn(struct udev *udev,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering void (*log_fn)(struct udev *udev,
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int priority, const char *file, int line, const char *fn,
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering const char *format, va_list args))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev->log_fn = log_fn;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_dbg(udev, "custom logging function %p registered\n", log_fn);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering/**
51323288fc628a5cac50914df915545d685b793eLennart Poettering * udev_get_log_priority:
51323288fc628a5cac50914df915545d685b793eLennart Poettering * @udev: udev library context
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * The initial logging priority is read from the udev config file
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * at startup.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
51323288fc628a5cac50914df915545d685b793eLennart Poettering * Returns: the current logging priority
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering **/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ int udev_get_log_priority(struct udev *udev)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return udev->log_priority;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/**
966c66e34940001a40806142ecebaae61b478444Lennart Poettering * udev_set_log_priority:
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering * @udev: udev library context
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * @priority: the new logging priority
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering *
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering * Set the current logging priority. The value controls which messages
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * are logged.
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering **/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ void udev_set_log_priority(struct udev *udev, int priority)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char num[32];
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev->log_priority = priority;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering snprintf(num, sizeof(num), "%u", udev->log_priority);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_add_property(udev, "UDEV_LOG", num);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poetteringstruct udev_list_entry *udev_add_property(struct udev *udev, const char *key, const char *value)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (value == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_list_entry *list_entry;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering list_entry = udev_get_properties_list_entry(udev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering list_entry = udev_list_entry_get_by_name(list_entry, key);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (list_entry != NULL)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering udev_list_entry_delete(list_entry);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering return NULL;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return udev_list_entry_add(&udev->properties_list, key, value);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen}
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen
039a8725fdff1d71e9efd28f27741601c5b4235cLennart Poetteringstruct udev_list_entry *udev_get_properties_list_entry(struct udev *udev)
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen{
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen return udev_list_get_entry(&udev->properties_list);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen}
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen