From ff524019efda6d504dbdddbe5ba6aa3cffb2a7b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 22 Feb 2017 22:57:34 -0500 Subject: [PATCH 1/4] basic/log: split max log level into multiple "realms" The single log level is split into an array of log levels. Which index in the array is used can be determined for each compilation unit separately by setting a macro before including log.h. All compilation units use the same index (LOG_REALM_SYSTEMD), so there should be no functional change. v2: - the "realm" is squished into the level (upper bits that are not used by priority or facility), and unsquished later in functions in log.c. v3: - rename REALM_PLUS_LEVEL to LOG_REALM_PLUS_LEVEL and REALM to LOG_REALM_REMOVE_LEVEL. --- src/basic/log.c | 94 +++++++++++++++++++++++++++++---------------- src/basic/log.h | 83 ++++++++++++++++++++++++++++++--------- src/test/test-log.c | 9 +++++ 3 files changed, 134 insertions(+), 52 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 0d0ced00bd..62c9f1a78f 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -58,7 +58,8 @@ #define SNDBUF_SIZE (8*1024*1024) static LogTarget log_target = LOG_TARGET_CONSOLE; -static int log_max_level = LOG_INFO; +static int log_max_level[] = {LOG_INFO, LOG_INFO}; +assert_cc(ELEMENTSOF(log_max_level) == _LOG_REALM_MAX); static int log_facility = LOG_DAEMON; static int console_fd = STDERR_FILENO; @@ -315,10 +316,11 @@ void log_forget_fds(void) { console_fd = kmsg_fd = syslog_fd = journal_fd = -1; } -void log_set_max_level(int level) { +void log_set_max_level_realm(LogRealm realm, int level) { assert((level & LOG_PRIMASK) == level); + assert(realm < ELEMENTSOF(log_max_level)); - log_max_level = level; + log_max_level[realm] = level; } void log_set_facility(int facility) { @@ -636,13 +638,14 @@ int log_dispatch_internal( } int log_dump_internal( - int level, - int error, - const char *file, - int line, - const char *func, - char *buffer) { + int level, + int error, + const char *file, + int line, + const char *func, + char *buffer) { + LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); PROTECT_ERRNO; /* This modifies the buffer... */ @@ -650,13 +653,13 @@ int log_dump_internal( if (error < 0) error = -error; - if (_likely_(LOG_PRI(level) > log_max_level)) + if (_likely_(LOG_PRI(level) > log_max_level[realm])) return -error; return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer); } -int log_internalv( +int log_internalv_realm( int level, int error, const char *file, @@ -665,13 +668,14 @@ int log_internalv( const char *format, va_list ap) { - PROTECT_ERRNO; + LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); char buffer[LINE_MAX]; + PROTECT_ERRNO; if (error < 0) error = -error; - if (_likely_(LOG_PRI(level) > log_max_level)) + if (_likely_(LOG_PRI(level) > log_max_level[realm])) return -error; /* Make sure that %m maps to the specified error */ @@ -683,7 +687,7 @@ int log_internalv( return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer); } -int log_internal( +int log_internal_realm( int level, int error, const char *file, @@ -695,7 +699,7 @@ int log_internal( int r; va_start(ap, format); - r = log_internalv(level, error, file, line, func, format, ap); + r = log_internalv_realm(level, error, file, line, func, format, ap); va_end(ap); return r; @@ -721,7 +725,7 @@ int log_object_internalv( if (error < 0) error = -error; - if (_likely_(LOG_PRI(level) > log_max_level)) + if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD])) return -error; /* Make sure that %m maps to the specified error */ @@ -779,8 +783,9 @@ static void log_assert( const char *format) { static char buffer[LINE_MAX]; + LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); - if (_likely_(LOG_PRI(level) > log_max_level)) + if (_likely_(LOG_PRI(level) > log_max_level[realm])) return; DISABLE_WARNING_FORMAT_NONLITERAL; @@ -792,23 +797,42 @@ static void log_assert( log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer); } -noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) { - log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting."); +noreturn void log_assert_failed_realm( + LogRealm realm, + const char *text, + const char *file, + int line, + const char *func) { + log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func, + "Assertion '%s' failed at %s:%u, function %s(). Aborting."); abort(); } -noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) { - log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); +noreturn void log_assert_failed_unreachable_realm( + LogRealm realm, + const char *text, + const char *file, + int line, + const char *func) { + log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func, + "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); abort(); } -void log_assert_failed_return(const char *text, const char *file, int line, const char *func) { +void log_assert_failed_return_realm( + LogRealm realm, + const char *text, + const char *file, + int line, + const char *func) { PROTECT_ERRNO; - log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring."); + log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func, + "Assertion '%s' failed at %s:%u, function %s(). Ignoring."); } -int log_oom_internal(const char *file, int line, const char *func) { - log_internal(LOG_ERR, ENOMEM, file, line, func, "Out of memory."); +int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) { + log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR), + ENOMEM, file, line, func, "Out of memory."); return -ENOMEM; } @@ -868,13 +892,14 @@ int log_struct_internal( char buf[LINE_MAX]; bool found = false; + LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); PROTECT_ERRNO; va_list ap; if (error < 0) error = -error; - if (_likely_(LOG_PRI(level) > log_max_level)) + if (_likely_(LOG_PRI(level) > log_max_level[realm])) return -error; if (log_target == LOG_TARGET_NULL) @@ -958,14 +983,14 @@ int log_set_target_from_string(const char *e) { return 0; } -int log_set_max_level_from_string(const char *e) { +int log_set_max_level_from_string_realm(LogRealm realm, const char *e) { int t; t = log_level_from_string(e); if (t < 0) return -EINVAL; - log_set_max_level(t); + log_set_max_level_realm(realm, t); return 0; } @@ -1013,7 +1038,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat return 0; } -void log_parse_environment(void) { +void log_parse_environment_realm(LogRealm realm) { const char *e; if (get_ctty_devnr(0, NULL) < 0) @@ -1026,7 +1051,7 @@ void log_parse_environment(void) { log_warning("Failed to parse log target '%s'. Ignoring.", e); e = secure_getenv("SYSTEMD_LOG_LEVEL"); - if (e && log_set_max_level_from_string(e) < 0) + if (e && log_set_max_level_from_string_realm(realm, e) < 0) log_warning("Failed to parse log level '%s'. Ignoring.", e); e = secure_getenv("SYSTEMD_LOG_COLOR"); @@ -1042,8 +1067,8 @@ LogTarget log_get_target(void) { return log_target; } -int log_get_max_level(void) { - return log_max_level; +int log_get_max_level_realm(LogRealm realm) { + return log_max_level[realm]; } void log_show_color(bool b) { @@ -1147,7 +1172,7 @@ int log_syntax_internal( if (error < 0) error = -error; - if (_likely_(LOG_PRI(level) > log_max_level)) + if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD])) return -error; if (log_target == LOG_TARGET_NULL) @@ -1164,7 +1189,8 @@ int log_syntax_internal( unit_fmt = getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s"; return log_struct_internal( - level, error, + LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level), + error, file, line, func, "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR, "CONFIG_FILE=%s", config_file, diff --git a/src/basic/log.h b/src/basic/log.h index b3e4060b5d..57463fbb83 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -31,6 +31,16 @@ #include "macro.h" +typedef enum LogRealm { + LOG_REALM_SYSTEMD, + LOG_REALM_UDEV, + _LOG_REALM_MAX, +} LogRealm; + +#ifndef LOG_REALM +# define LOG_REALM LOG_REALM_SYSTEMD +#endif + typedef enum LogTarget{ LOG_TARGET_CONSOLE, LOG_TARGET_CONSOLE_PREFIXED, @@ -44,14 +54,24 @@ typedef enum LogTarget{ LOG_TARGET_NULL, _LOG_TARGET_MAX, _LOG_TARGET_INVALID = -1 -} LogTarget; +} LogTarget; + +#define LOG_REALM_PLUS_LEVEL(realm, level) \ + ((realm) << 10 | (level)) +#define LOG_REALM_REMOVE_LEVEL(realm_level) \ + ((realm_level >> 10)) void log_set_target(LogTarget target); -void log_set_max_level(int level); +void log_set_max_level_realm(LogRealm realm, int level); +#define log_set_max_level(level) \ + log_set_max_level_realm(LOG_REALM, (level)) + void log_set_facility(int facility); int log_set_target_from_string(const char *e); -int log_set_max_level_from_string(const char *e); +int log_set_max_level_from_string_realm(LogRealm realm, const char *e); +#define log_set_max_level_from_string(e) \ + log_set_max_level_from_string_realm(LOG_REALM, (e)) void log_show_color(bool b); bool log_get_show_color(void) _pure_; @@ -62,7 +82,9 @@ int log_show_color_from_string(const char *e); int log_show_location_from_string(const char *e); LogTarget log_get_target(void) _pure_; -int log_get_max_level(void) _pure_; +int log_get_max_level_realm(LogRealm realm) _pure_; +#define log_get_max_level() \ + log_get_max_level_realm(LOG_REALM) int log_open(void); void log_close(void); @@ -73,7 +95,9 @@ void log_close_journal(void); void log_close_kmsg(void); void log_close_console(void); -void log_parse_environment(void); +void log_parse_environment_realm(LogRealm realm); +#define log_parse_environment() \ + log_parse_environment_realm(LOG_REALM) int log_dispatch_internal( int level, @@ -87,15 +111,17 @@ int log_dispatch_internal( const char *extra_field, char *buffer); -int log_internal( +int log_internal_realm( int level, int error, const char *file, int line, const char *func, const char *format, ...) _printf_(6,7); +#define log_internal(level, ...) \ + log_internal_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__) -int log_internalv( +int log_internalv_realm( int level, int error, const char *file, @@ -103,7 +129,10 @@ int log_internalv( const char *func, const char *format, va_list ap) _printf_(6,0); +#define log_internalv(level, ...) \ + log_internalv_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__) +/* Realm is fixed to LOG_REALM_SYSTEMD for those */ int log_object_internal( int level, int error, @@ -138,6 +167,7 @@ int log_struct_internal( const char *format, ...) _printf_(6,0) _sentinel_; int log_oom_internal( + LogRealm realm, const char *file, int line, const char *func); @@ -161,37 +191,50 @@ int log_dump_internal( char *buffer); /* Logging for various assertions */ -noreturn void log_assert_failed( +noreturn void log_assert_failed_realm( + LogRealm realm, const char *text, const char *file, int line, const char *func); +#define log_assert_failed(text, ...) \ + log_assert_failed_realm(LOG_REALM, (text), __VA_ARGS__) -noreturn void log_assert_failed_unreachable( +noreturn void log_assert_failed_unreachable_realm( + LogRealm realm, const char *text, const char *file, int line, const char *func); +#define log_assert_failed_unreachable(text, ...) \ + log_assert_failed_unreachable_realm(LOG_REALM, (text), __VA_ARGS__) -void log_assert_failed_return( +void log_assert_failed_return_realm( + LogRealm realm, const char *text, const char *file, int line, const char *func); +#define log_assert_failed_return(text, ...) \ + log_assert_failed_return_realm(LOG_REALM, (text), __VA_ARGS__) #define log_dispatch(level, error, buffer) \ log_dispatch_internal(level, error, __FILE__, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer) /* Logging with level */ -#define log_full_errno(level, error, ...) \ +#define log_full_errno_realm(realm, level, error, ...) \ ({ \ int _level = (level), _e = (error); \ - (log_get_max_level() >= LOG_PRI(_level)) \ - ? log_internal(_level, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \ + (log_get_max_level_realm((realm)) >= LOG_PRI(_level)) \ + ? log_internal_realm(LOG_REALM_PLUS_LEVEL((realm), _level), _e, \ + __FILE__, __LINE__, __func__, __VA_ARGS__) \ : -abs(_e); \ }) -#define log_full(level, ...) log_full_errno(level, 0, __VA_ARGS__) +#define log_full_errno(level, error, ...) \ + log_full_errno_realm(LOG_REALM, (level), (error), __VA_ARGS__) + +#define log_full(level, ...) log_full_errno((level), 0, __VA_ARGS__) /* Normal logging */ #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) @@ -216,13 +259,17 @@ void log_assert_failed_return( #endif /* Structured logging */ -#define log_struct(level, ...) log_struct_internal(level, 0, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define log_struct_errno(level, error, ...) log_struct_internal(level, error, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define log_struct_errno(level, error, ...) \ + log_struct_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \ + error, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__) /* This modifies the buffer passed! */ -#define log_dump(level, buffer) log_dump_internal(level, 0, __FILE__, __LINE__, __func__, buffer) +#define log_dump(level, buffer) \ + log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \ + 0, __FILE__, __LINE__, __func__, buffer) -#define log_oom() log_oom_internal(__FILE__, __LINE__, __func__) +#define log_oom() log_oom_internal(LOG_REALM, __FILE__, __LINE__, __func__) bool log_on_console(void) _pure_; diff --git a/src/test/test-log.c b/src/test/test-log.c index ae9e113efb..626d2c6135 100644 --- a/src/test/test-log.c +++ b/src/test/test-log.c @@ -24,6 +24,15 @@ #include "log.h" #include "util.h" +assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_FTP | LOG_DEBUG)) + == LOG_REALM_SYSTEMD); +assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_LOCAL7 | LOG_DEBUG)) + == LOG_REALM_UDEV); +assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_LOCAL3 | LOG_DEBUG) & LOG_FACMASK) + == LOG_LOCAL3); +assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_USER | LOG_INFO) & LOG_PRIMASK) + == LOG_INFO); + int main(int argc, char* argv[]) { log_set_target(LOG_TARGET_CONSOLE); From 5c72049f91b82c8edb64840343df41329f81c7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 22 Feb 2017 23:13:22 -0500 Subject: [PATCH 2/4] udev: use LOG_REALM_UDEV in all udev code Any call to set/query/use the log level in the code with LOG_REALM=LOG_REALM_UDEV refers to log_max_level[1]. In particular this means that systemd code using the libudev library uses does not set the log level for log calls done in libudev. Fixes #4525. v2: - also update meson's meson.build --- Makefile.am | 31 ++++++++++++++++++++++++++++++- meson.build | 1 + src/udev/meson.build | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index d565978266..c29660ce07 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3913,7 +3913,8 @@ gperf_gperf_sources += \ libudev_core_la_CFLAGS = \ $(AM_CFLAGS) \ $(BLKID_CFLAGS) \ - $(KMOD_CFLAGS) + $(KMOD_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV libudev_core_la_LIBADD = \ libsystemd-network.la \ @@ -3945,6 +3946,10 @@ endif systemd_udevd_SOURCES = \ src/udev/udevd.c +systemd_udevd_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + systemd_udevd_LDADD = \ libudev-core.la \ libbasic.la @@ -4088,6 +4093,10 @@ EXTRA_DIST += \ ata_id_SOURCES = \ src/udev/ata_id/ata_id.c +ata_id_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + ata_id_LDADD = \ libshared.la @@ -4098,6 +4107,10 @@ udevlibexec_PROGRAMS += \ cdrom_id_SOURCES = \ src/udev/cdrom_id/cdrom_id.c +cdrom_id_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + cdrom_id_LDADD = \ libshared.la @@ -4111,6 +4124,10 @@ dist_udevrules_DATA += \ collect_SOURCES = \ src/udev/collect/collect.c +collect_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + collect_LDADD = \ libshared.la @@ -4124,6 +4141,10 @@ scsi_id_SOURCES =\ src/udev/scsi_id/scsi.h \ src/udev/scsi_id/scsi_id.h +scsi_id_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + scsi_id_LDADD = \ libshared.la @@ -4137,6 +4158,10 @@ EXTRA_DIST += \ v4l_id_SOURCES = \ src/udev/v4l_id/v4l_id.c +v4l_id_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + v4l_id_LDADD = \ libshared.la @@ -4152,6 +4177,10 @@ mtd_probe_SOURCES = \ src/udev/mtd_probe/mtd_probe.h \ src/udev/mtd_probe/probe_smartmedia.c +mtd_probe_CFLAGS = \ + $(AM_CFLAGS) \ + -DLOG_REALM=LOG_REALM_UDEV + dist_udevrules_DATA += \ rules/75-probe_mtd.rules diff --git a/meson.build b/meson.build index 14a20530d4..6d62ec7fab 100644 --- a/meson.build +++ b/meson.build @@ -2055,6 +2055,7 @@ public_programs += [exe] exe = executable('systemd-udevd', systemd_udevd_sources, include_directories : includes, + c_args : ['-DLOG_REALM=LOG_REALM_UDEV'], link_with : [libudev_core, libsystemd_network, libudev_internal], diff --git a/src/udev/meson.build b/src/udev/meson.build index 9692ddc1e9..9067de97dc 100644 --- a/src/udev/meson.build +++ b/src/udev/meson.build @@ -130,6 +130,7 @@ foreach prog : [['ata_id/ata_id.c'], executable(prog[0].split('/')[0], prog, include_directories : includes, + c_args : ['-DLOG_REALM=LOG_REALM_UDEV'], link_with : [libudev_internal], install_rpath : udev_rpath, install : true, From b237a168dfb981bdd9bcfe7a1002be7eb1b8da72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 23 Feb 2017 03:16:44 -0500 Subject: [PATCH 3/4] Rip out setting of the log level from udev_new and put it in a new function This function is internal to systemd code, so external users of libudev will not see those log messages. I think this is better. If we want to allow that, the function could be put in libudev and exported. v2: check that the string is more than one char before stripping quotes --- Makefile.am | 1 + src/libudev/libudev.c | 76 ------------------------------------ src/shared/meson.build | 1 + src/shared/udev-util.c | 56 ++++++++++++++++++++++++++ src/shared/udev-util.h | 2 + src/udev/ata_id/ata_id.c | 2 + src/udev/cdrom_id/cdrom_id.c | 6 ++- src/udev/collect/collect.c | 6 +++ src/udev/scsi_id/scsi_id.c | 2 + src/udev/udevadm.c | 11 ++++-- src/udev/udevd.c | 1 + 11 files changed, 82 insertions(+), 82 deletions(-) create mode 100644 src/shared/udev-util.c diff --git a/Makefile.am b/Makefile.am index c29660ce07..341492089e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1023,6 +1023,7 @@ libshared_la_SOURCES = \ src/shared/output-mode.c \ src/shared/gpt.h \ src/shared/udev-util.h \ + src/shared/udev-util.c \ src/shared/linux/auto_dev-ioctl.h \ src/shared/linux-3.13/dm-ioctl.h \ src/shared/initreq.h \ diff --git a/src/libudev/libudev.c b/src/libudev/libudev.c index d8e13288b0..5f2225f402 100644 --- a/src/libudev/libudev.c +++ b/src/libudev/libudev.c @@ -103,82 +103,6 @@ _public_ struct udev *udev_new(void) { } udev->refcount = 1; - 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++; - - /* 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) { - log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", 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 (len == 1 || val[len-1] != val[0]) { - log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr); - continue; - } - val[len-1] = '\0'; - val++; - } - - if (streq(key, "udev_log")) { - int prio; - - prio = util_log_priority(val); - if (prio < 0) - log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr, val); - else - log_set_max_level(prio); - continue; - } - } - } - return udev; } diff --git a/src/shared/meson.build b/src/shared/meson.build index b684e5b543..a9a5b4a6d4 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -89,6 +89,7 @@ shared_sources = ''' tests.c tests.h udev-util.h + udev-util.c uid-range.c uid-range.h utmp-wtmp.h diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c new file mode 100644 index 0000000000..f708dcfa14 --- /dev/null +++ b/src/shared/udev-util.c @@ -0,0 +1,56 @@ +/*** + This file is part of systemd. + + Copyright 2017 Zbigniew Jędrzejewski-Szmek + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include + +#include "fileio.h" +#include "log.h" +#include "string-util.h" +#include "udev-util.h" + +int udev_parse_config(void) { + _cleanup_free_ char *val = NULL; + const char *log; + size_t n; + int r; + + r = parse_env_file("/etc/udev/udev.conf", NEWLINE, "udev_log", &val, NULL); + if (r == -ENOENT || !val) + return 0; + if (r < 0) + return r; + + /* unquote */ + n = strlen(val); + if (n >= 2 && + ((val[0] == '"' && val[n-1] == '"') || + (val[0] == '\'' && val[n-1] == '\''))) { + val[n - 1] = '\0'; + log = val + 1; + } else + log = val; + + /* we set the udev log level here explicitly, this is supposed + * to regulate the code in libudev/ and udev/. */ + r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log); + if (r < 0) + log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log); + + return 0; +} diff --git a/src/shared/udev-util.h b/src/shared/udev-util.h index ca0889f8a6..a415be249e 100644 --- a/src/shared/udev-util.h +++ b/src/shared/udev-util.h @@ -42,3 +42,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref); #define _cleanup_udev_ctrl_msg_unref_ _cleanup_(udev_ctrl_msg_unrefp) #define _cleanup_udev_monitor_unref_ _cleanup_(udev_monitor_unrefp) #define _cleanup_udev_list_cleanup_ _cleanup_(udev_list_cleanup) + +int udev_parse_config(void); diff --git a/src/udev/ata_id/ata_id.c b/src/udev/ata_id/ata_id.c index 1e414664ce..ad152b9d31 100644 --- a/src/udev/ata_id/ata_id.c +++ b/src/udev/ata_id/ata_id.c @@ -427,6 +427,8 @@ int main(int argc, char *argv[]) {} }; + log_set_target(LOG_TARGET_AUTO); + udev_parse_config(); log_parse_environment(); log_open(); diff --git a/src/udev/cdrom_id/cdrom_id.c b/src/udev/cdrom_id/cdrom_id.c index 72f284f710..1f906a8525 100644 --- a/src/udev/cdrom_id/cdrom_id.c +++ b/src/udev/cdrom_id/cdrom_id.c @@ -38,6 +38,7 @@ #include "libudev-private.h" #include "random-util.h" +#include "udev-util.h" /* device info */ static unsigned int cd_cd_rom; @@ -843,8 +844,7 @@ static int cd_media_toc(struct udev *udev, int fd) return 0; } -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { struct udev *udev; static const struct option options[] = { { "lock-media", no_argument, NULL, 'l' }, @@ -862,6 +862,8 @@ int main(int argc, char *argv[]) int cnt; int rc = 0; + log_set_target(LOG_TARGET_AUTO); + udev_parse_config(); log_parse_environment(); log_open(); diff --git a/src/udev/collect/collect.c b/src/udev/collect/collect.c index 0e973cd521..0b3c0f969a 100644 --- a/src/udev/collect/collect.c +++ b/src/udev/collect/collect.c @@ -29,6 +29,7 @@ #include "macro.h" #include "stdio-util.h" #include "string-util.h" +#include "udev-util.h" #define BUFSIZE 16 #define UDEV_ALARM_TIMEOUT 180 @@ -361,6 +362,11 @@ int main(int argc, char **argv) int prune = 0; char tmpdir[UTIL_PATH_SIZE]; + log_set_target(LOG_TARGET_AUTO); + udev_parse_config(); + log_parse_environment(); + log_open(); + udev = udev_new(); if (udev == NULL) { ret = EXIT_FAILURE; diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c index eba382a82d..3c3d7a6b33 100644 --- a/src/udev/scsi_id/scsi_id.c +++ b/src/udev/scsi_id/scsi_id.c @@ -577,6 +577,8 @@ int main(int argc, char **argv) int newargc; char **newargv = NULL; + log_set_target(LOG_TARGET_AUTO); + udev_parse_config(); log_parse_environment(); log_open(); diff --git a/src/udev/udevadm.c b/src/udev/udevadm.c index 492b2f4c25..befc3bad7b 100644 --- a/src/udev/udevadm.c +++ b/src/udev/udevadm.c @@ -23,6 +23,7 @@ #include "selinux-util.h" #include "string-util.h" #include "udev.h" +#include "udev-util.h" static int adm_version(struct udev *udev, int argc, char *argv[]) { printf("%s\n", PACKAGE_VERSION); @@ -87,14 +88,16 @@ int main(int argc, char *argv[]) { unsigned int i; int rc = 1, c; + udev_parse_config(); + log_parse_environment(); + log_open(); + + mac_selinux_init(); + udev = udev_new(); if (udev == NULL) goto out; - log_parse_environment(); - log_open(); - mac_selinux_init(); - while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0) switch (c) { diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 56b8c1ec55..3af06c68b2 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -1663,6 +1663,7 @@ int main(int argc, char *argv[]) { int r; log_set_target(LOG_TARGET_AUTO); + udev_parse_config(); log_parse_environment(); log_open(); From 3cf339236488fce333023268fd5eb6fab33453cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 23 Feb 2017 03:18:42 -0500 Subject: [PATCH 4/4] udev/collect: remove now-unused struct udev --- src/udev/collect/collect.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/udev/collect/collect.c b/src/udev/collect/collect.c index 0b3c0f969a..57dfb016f9 100644 --- a/src/udev/collect/collect.c +++ b/src/udev/collect/collect.c @@ -344,9 +344,7 @@ static void everybody(void) } } -int main(int argc, char **argv) -{ - struct udev *udev; +int main(int argc, char **argv) { static const struct option options[] = { { "add", no_argument, NULL, 'a' }, { "remove", no_argument, NULL, 'r' }, @@ -367,12 +365,6 @@ int main(int argc, char **argv) log_parse_environment(); log_open(); - udev = udev_new(); - if (udev == NULL) { - ret = EXIT_FAILURE; - goto exit; - } - for (;;) { int option; @@ -392,26 +384,23 @@ int main(int argc, char **argv) break; case 'h': usage(); - goto exit; + return 0; default: - ret = 1; - goto exit; + return 1; } } argi = optind; if (argi + 2 > argc) { printf("Missing parameter(s)\n"); - ret = 1; - goto exit; + return 1; } checkpoint = argv[argi++]; us = argv[argi++]; if (signal(SIGALRM, sig_alrm) == SIG_ERR) { fprintf(stderr, "Cannot set SIGALRM: %m\n"); - ret = 2; - goto exit; + return 2; } udev_list_node_init(&bunch); @@ -491,7 +480,5 @@ out: everybody(); if (ret >= 0) printf("COLLECT_%s=%d\n", checkpoint, ret); -exit: - udev_unref(udev); return ret; }