Skip to content

Commit 35f7216

Browse files
committed
clock-util: be more tolerant in parsing /etc/adjtime
As we default to "hardware clock is in UTC" if /etc/adjtime is not present, it also makes sense to have that default if /etc/adjtime contains only one or two lines. Drop the "gibberish" test case, as this was just EIO because of not containing three lines, which is already contained in other tests. clock_is_localtime() never actually validated the format of the first two lines, and there is little point in doing that. This addresses the reading half of issue systemd#2638.
1 parent 6369641 commit 35f7216

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/basic/clock-util.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ int clock_is_localtime(const char* adjtime_path) {
9191
fgets(line, sizeof(line), f) &&
9292
fgets(line, sizeof(line), f);
9393
if (!b)
94-
return -EIO;
94+
/* less than three lines -> default to UTC */
95+
return 0;
9596

9697
truncate_nl(line);
9798
return streq(line, "LOCAL");
9899

99100
} else if (errno != ENOENT)
100101
return -errno;
101102

103+
/* adjtime not present -> default to UTC */
102104
return 0;
103105
}
104106

src/test/test-clock.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ static void test_clock_is_localtime(void) {
4141
/* no final EOL */
4242
{"0.0 0 0\n0\nUTC", 0},
4343
{"0.0 0 0\n0\nLOCAL", 1},
44+
/* empty value -> defaults to UTC */
45+
{"0.0 0 0\n0\n", 0},
4446
/* unknown value -> defaults to UTC */
4547
{"0.0 0 0\n0\nFOO\n", 0},
46-
/* gibberish */
47-
{"br0ken", -EIO},
48+
/* no third line */
49+
{"0.0 0 0", 0},
50+
{"0.0 0 0\n", 0},
51+
{"0.0 0 0\n0", 0},
4852
};
4953

5054
/* without an adjtime file we default to UTC */
@@ -75,11 +79,9 @@ static void test_clock_is_localtime_system(void) {
7579

7680
if (access("/etc/adjtime", F_OK) == 0) {
7781
log_info("/etc/adjtime exists, clock_is_localtime() == %i", r);
78-
/* we cannot assert much if /etc/adjtime exists, just that we
79-
* expect either an answer, or an EIO if the local file really
80-
* is badly malformed. I. e. we don't expect any other error
81-
* code or crash. */
82-
assert(r == 0 || r == 1 || r == -EIO);
82+
/* if /etc/adjtime exists we expect some answer, no error or
83+
* crash */
84+
assert(r == 0 || r == 1);
8385
} else
8486
/* default is UTC if there is no /etc/adjtime */
8587
assert(r == 0);

0 commit comments

Comments
 (0)