Skip to content

Commit b237a16

Browse files
committed
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
1 parent 5c72049 commit b237a16

File tree

11 files changed

+82
-82
lines changed

11 files changed

+82
-82
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@ libshared_la_SOURCES = \
10231023
src/shared/output-mode.c \
10241024
src/shared/gpt.h \
10251025
src/shared/udev-util.h \
1026+
src/shared/udev-util.c \
10261027
src/shared/linux/auto_dev-ioctl.h \
10271028
src/shared/linux-3.13/dm-ioctl.h \
10281029
src/shared/initreq.h \

src/libudev/libudev.c

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -103,82 +103,6 @@ _public_ struct udev *udev_new(void) {
103103
}
104104
udev->refcount = 1;
105105

106-
f = fopen("/etc/udev/udev.conf", "re");
107-
if (f != NULL) {
108-
char line[UTIL_LINE_SIZE];
109-
unsigned line_nr = 0;
110-
111-
while (fgets(line, sizeof(line), f)) {
112-
size_t len;
113-
char *key;
114-
char *val;
115-
116-
line_nr++;
117-
118-
/* find key */
119-
key = line;
120-
while (isspace(key[0]))
121-
key++;
122-
123-
/* comment or empty line */
124-
if (key[0] == '#' || key[0] == '\0')
125-
continue;
126-
127-
/* split key/value */
128-
val = strchr(key, '=');
129-
if (val == NULL) {
130-
log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", line_nr);
131-
continue;
132-
}
133-
val[0] = '\0';
134-
val++;
135-
136-
/* find value */
137-
while (isspace(val[0]))
138-
val++;
139-
140-
/* terminate key */
141-
len = strlen(key);
142-
if (len == 0)
143-
continue;
144-
while (isspace(key[len-1]))
145-
len--;
146-
key[len] = '\0';
147-
148-
/* terminate value */
149-
len = strlen(val);
150-
if (len == 0)
151-
continue;
152-
while (isspace(val[len-1]))
153-
len--;
154-
val[len] = '\0';
155-
156-
if (len == 0)
157-
continue;
158-
159-
/* unquote */
160-
if (val[0] == '"' || val[0] == '\'') {
161-
if (len == 1 || val[len-1] != val[0]) {
162-
log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr);
163-
continue;
164-
}
165-
val[len-1] = '\0';
166-
val++;
167-
}
168-
169-
if (streq(key, "udev_log")) {
170-
int prio;
171-
172-
prio = util_log_priority(val);
173-
if (prio < 0)
174-
log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr, val);
175-
else
176-
log_set_max_level(prio);
177-
continue;
178-
}
179-
}
180-
}
181-
182106
return udev;
183107
}
184108

src/shared/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ shared_sources = '''
8989
tests.c
9090
tests.h
9191
udev-util.h
92+
udev-util.c
9293
uid-range.c
9394
uid-range.h
9495
utmp-wtmp.h

src/shared/udev-util.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***
2+
This file is part of systemd.
3+
4+
Copyright 2017 Zbigniew Jędrzejewski-Szmek
5+
6+
systemd is free software; you can redistribute it and/or modify it
7+
under the terms of the GNU Lesser General Public License as published by
8+
the Free Software Foundation; either version 2.1 of the License, or
9+
(at your option) any later version.
10+
11+
systemd is distributed in the hope that it will be useful, but
12+
WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public License
17+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
18+
***/
19+
20+
#include <string.h>
21+
22+
#include "fileio.h"
23+
#include "log.h"
24+
#include "string-util.h"
25+
#include "udev-util.h"
26+
27+
int udev_parse_config(void) {
28+
_cleanup_free_ char *val = NULL;
29+
const char *log;
30+
size_t n;
31+
int r;
32+
33+
r = parse_env_file("/etc/udev/udev.conf", NEWLINE, "udev_log", &val, NULL);
34+
if (r == -ENOENT || !val)
35+
return 0;
36+
if (r < 0)
37+
return r;
38+
39+
/* unquote */
40+
n = strlen(val);
41+
if (n >= 2 &&
42+
((val[0] == '"' && val[n-1] == '"') ||
43+
(val[0] == '\'' && val[n-1] == '\''))) {
44+
val[n - 1] = '\0';
45+
log = val + 1;
46+
} else
47+
log = val;
48+
49+
/* we set the udev log level here explicitly, this is supposed
50+
* to regulate the code in libudev/ and udev/. */
51+
r = log_set_max_level_from_string_realm(LOG_REALM_UDEV, log);
52+
if (r < 0)
53+
log_debug_errno(r, "/etc/udev/udev.conf: failed to set udev log level '%s', ignoring: %m", log);
54+
55+
return 0;
56+
}

src/shared/udev-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref);
4242
#define _cleanup_udev_ctrl_msg_unref_ _cleanup_(udev_ctrl_msg_unrefp)
4343
#define _cleanup_udev_monitor_unref_ _cleanup_(udev_monitor_unrefp)
4444
#define _cleanup_udev_list_cleanup_ _cleanup_(udev_list_cleanup)
45+
46+
int udev_parse_config(void);

src/udev/ata_id/ata_id.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ int main(int argc, char *argv[])
427427
{}
428428
};
429429

430+
log_set_target(LOG_TARGET_AUTO);
431+
udev_parse_config();
430432
log_parse_environment();
431433
log_open();
432434

src/udev/cdrom_id/cdrom_id.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "libudev-private.h"
4040
#include "random-util.h"
41+
#include "udev-util.h"
4142

4243
/* device info */
4344
static unsigned int cd_cd_rom;
@@ -843,8 +844,7 @@ static int cd_media_toc(struct udev *udev, int fd)
843844
return 0;
844845
}
845846

846-
int main(int argc, char *argv[])
847-
{
847+
int main(int argc, char *argv[]) {
848848
struct udev *udev;
849849
static const struct option options[] = {
850850
{ "lock-media", no_argument, NULL, 'l' },
@@ -862,6 +862,8 @@ int main(int argc, char *argv[])
862862
int cnt;
863863
int rc = 0;
864864

865+
log_set_target(LOG_TARGET_AUTO);
866+
udev_parse_config();
865867
log_parse_environment();
866868
log_open();
867869

src/udev/collect/collect.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "macro.h"
3030
#include "stdio-util.h"
3131
#include "string-util.h"
32+
#include "udev-util.h"
3233

3334
#define BUFSIZE 16
3435
#define UDEV_ALARM_TIMEOUT 180
@@ -361,6 +362,11 @@ int main(int argc, char **argv)
361362
int prune = 0;
362363
char tmpdir[UTIL_PATH_SIZE];
363364

365+
log_set_target(LOG_TARGET_AUTO);
366+
udev_parse_config();
367+
log_parse_environment();
368+
log_open();
369+
364370
udev = udev_new();
365371
if (udev == NULL) {
366372
ret = EXIT_FAILURE;

src/udev/scsi_id/scsi_id.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ int main(int argc, char **argv)
577577
int newargc;
578578
char **newargv = NULL;
579579

580+
log_set_target(LOG_TARGET_AUTO);
581+
udev_parse_config();
580582
log_parse_environment();
581583
log_open();
582584

src/udev/udevadm.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "selinux-util.h"
2424
#include "string-util.h"
2525
#include "udev.h"
26+
#include "udev-util.h"
2627

2728
static int adm_version(struct udev *udev, int argc, char *argv[]) {
2829
printf("%s\n", PACKAGE_VERSION);
@@ -87,14 +88,16 @@ int main(int argc, char *argv[]) {
8788
unsigned int i;
8889
int rc = 1, c;
8990

90-
udev = udev_new();
91-
if (udev == NULL)
92-
goto out;
93-
91+
udev_parse_config();
9492
log_parse_environment();
9593
log_open();
94+
9695
mac_selinux_init();
9796

97+
udev = udev_new();
98+
if (udev == NULL)
99+
goto out;
100+
98101
while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
99102
switch (c) {
100103

0 commit comments

Comments
 (0)