]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Rip out setting of the log level from udev_new and put it in a new function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 23 Feb 2017 08:16:44 +0000 (03:16 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 8 May 2017 02:49:12 +0000 (22:49 -0400)
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
src/libudev/libudev.c
src/shared/meson.build
src/shared/udev-util.c [new file with mode: 0644]
src/shared/udev-util.h
src/udev/ata_id/ata_id.c
src/udev/cdrom_id/cdrom_id.c
src/udev/collect/collect.c
src/udev/scsi_id/scsi_id.c
src/udev/udevadm.c
src/udev/udevd.c

index c29660ce078d1253e8a24948608caf23b8802e2f..341492089eba6b5c39ae2ab30291ef736e1837a2 100644 (file)
@@ -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 \
index d8e13288b025ca533dd67fb168e729d40b778db7..5f2225f402a5943577a20ceb3cb18f8c13af15e7 100644 (file)
@@ -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;
 }
 
index b684e5b5436fbc6ed49671f4f569c9c73288f216..a9a5b4a6d469048c416373c097714a609317b19a 100644 (file)
@@ -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 (file)
index 0000000..f708dcf
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+***/
+
+#include <string.h>
+
+#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;
+}
index ca0889f8a6a52f3a9fe47756fd489152e4c984d2..a415be249e4fe3f8ac3b91619939b8c1f69a49b9 100644 (file)
@@ -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);
index 1e414664ce74bc12551fef57ad4711d789be522a..ad152b9d316413272b132c7f377d689e514d33b1 100644 (file)
@@ -427,6 +427,8 @@ int main(int argc, char *argv[])
                 {}
         };
 
+        log_set_target(LOG_TARGET_AUTO);
+        udev_parse_config();
         log_parse_environment();
         log_open();
 
index 72f284f71082cf51da598a0da1db88f8c53ed412..1f906a85258f511dc36830d9a96d14e2dbfbd212 100644 (file)
@@ -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();
 
index 0e973cd521893dbc3befbedeec35416ff9fc5baa..0b3c0f969a67368c2f1bf809ddf9a53f5f0c29eb 100644 (file)
@@ -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;
index eba382a82dc6ff1fa7d490d3f14b7d1ca0f98689..3c3d7a6b3315b2672e362fc0b91d8e7bbbc58802 100644 (file)
@@ -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();
 
index 492b2f4c256496cd30b28872a102410d291bf65f..befc3bad7b764733946e8f61858d821aa8e5e2cc 100644 (file)
@@ -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 = udev_new();
-        if (udev == NULL)
-                goto out;
-
+        udev_parse_config();
         log_parse_environment();
         log_open();
+
         mac_selinux_init();
 
+        udev = udev_new();
+        if (udev == NULL)
+                goto out;
+
         while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
                 switch (c) {
 
index 56b8c1ec55435bf9dd9b22ffdef4c80e8ef56249..3af06c68b23f3a138cd1ac44a1052504fcfa852d 100644 (file)
@@ -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();