1
0
mirror of https://github.com/systemd/systemd synced 2026-03-23 07:14:53 +01:00

Compare commits

..

6 Commits

Author SHA1 Message Date
Yu Watanabe
8bdef77400
Merge pull request #20251 from keszybz/test-format-lifetime
Add test for format_lifetime() and fix prefix
2021-07-20 06:13:50 +09:00
monosans
99c6a49c70 log-generator: count arguments as offset from an iterator 2021-07-19 19:39:40 +01:00
Zbigniew Jędrzejewski-Szmek
c543f4d7dd basic/time-util: inline one more variable declaration 2021-07-19 19:58:13 +02:00
Zbigniew Jędrzejewski-Szmek
971f369761 udev-event: drop unused assignments
clang's static analyzer reports:
  Value stored to 'l' is never read
2021-07-19 18:54:26 +01:00
Zbigniew Jędrzejewski-Szmek
a35f3128e6 networkd: fix and simplify format_lifetime()
We would copy "forever" into the buffer. This is a fairly common case, so let's
do a microoptimization and return a static string. (All callers use the return
pointer, so this works just as well.)

The prefix "for " was not displayed, because the pointer to the part of the
buffer after "for " was returned. (Maybe it's just me, but I find strpcpy()
and associated functions really hard to use… I always have to look up what the
do exactly and what the return value is.)

A simple test is added.
2021-07-19 19:43:57 +02:00
Yu Watanabe
2d302d88e4 network: configure address with requested lifetime
When assigning the same address provided by a dynamic addressing
protocol, the new lifetime is stored on Request::Address, but not
Address object in Link object, which can be obtained by address_get().
So, we need to configure address with Address object in Request.

Fixes #20245.
2021-07-19 17:38:16 +01:00
7 changed files with 76 additions and 54 deletions

View File

@ -553,14 +553,12 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
/* Let's see if we should shows this in dot notation */
if (t < USEC_PER_MINUTE && b > 0) {
usec_t cc;
signed char j;
signed char j = 0;
j = 0;
for (cc = table[i].usec; cc > 1; cc /= 10)
for (usec_t cc = table[i].usec; cc > 1; cc /= 10)
j++;
for (cc = accuracy; cc > 1; cc /= 10) {
for (usec_t cc = accuracy; cc > 1; cc /= 10) {
b /= 10;
j--;
}

View File

@ -32,10 +32,6 @@ _SOURCE_REALTIME_TIMESTAMP={source_realtime_ts}
DATA={data}
"""
m = 0x198603b12d7
realtime_ts = 1404101101501873
monotonic_ts = 1753961140951
source_realtime_ts = 1404101101483516
priority = 3
facility = 6
@ -55,18 +51,14 @@ for i in range(OPTIONS.n):
data = '{:0{}}'.format(counter, OPTIONS.data_size)
counter += 1
entry = template.format(m=m,
realtime_ts=realtime_ts,
monotonic_ts=monotonic_ts,
source_realtime_ts=source_realtime_ts,
entry = template.format(m=0x198603b12d7 + i,
realtime_ts=1404101101501873 + i,
monotonic_ts=1753961140951 + i,
source_realtime_ts=1404101101483516 + i,
priority=priority,
facility=facility,
message=message,
data=data)
m += 1
realtime_ts += 1
monotonic_ts += 1
source_realtime_ts += 1
bytes += len(entry)

View File

@ -268,6 +268,12 @@ fuzzers += [
]
tests += [
[['src/network/test-networkd-address.c'],
[libnetworkd_core,
libsystemd_network],
[],
network_includes],
[['src/network/test-networkd-conf.c'],
[libnetworkd_core,
libsystemd_network],

View File

@ -621,19 +621,17 @@ int manager_has_address(Manager *manager, int family, const union in_addr_union
return false;
}
char *format_lifetime(char *buf, size_t l, uint32_t lifetime) {
char *p = buf;
const char* format_lifetime(char *buf, size_t l, uint32_t lifetime) {
assert(buf);
assert(l > 0);
assert(l > 4);
if (lifetime == CACHE_INFO_INFINITY_LIFE_TIME) {
strscpy(buf, l, "forever");
if (lifetime == CACHE_INFO_INFINITY_LIFE_TIME)
return "forever";
sprintf(buf, "for ");
/* format_timespan() never fails */
assert_se(format_timespan(buf + 4, l - 4, lifetime * USEC_PER_SEC, USEC_PER_SEC));
return buf;
}
l -= strpcpy(&p, l, "for ");
return format_timespan(p, l, lifetime * USEC_PER_SEC, USEC_PER_SEC);
}
static void log_address_debug(const Address *address, const char *str, const Link *link) {
@ -1273,17 +1271,17 @@ int request_process_address(Request *req) {
if (r <= 0)
return r;
r = address_get(link, req->address, &a);
if (r < 0)
return r;
r = address_configure(a, link, req->netlink_handler);
r = address_configure(req->address, link, req->netlink_handler);
if (r < 0)
return r;
/* To prevent a double decrement on failure in after_configure(). */
req->message_counter = NULL;
r = address_get(link, req->address, &a);
if (r < 0)
return r;
if (req->after_configure) {
r = req->after_configure(req, a);
if (r < 0)

View File

@ -50,7 +50,7 @@ typedef struct Address {
address_ready_callback_t callback;
} Address;
char *format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_result_;
const char* format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_result_;
/* Note: the lifetime of the compound literal is the immediately surrounding block,
* see C11 §6.5.2.5, and
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
@ -58,7 +58,7 @@ char *format_lifetime(char *buf, size_t l, uint32_t lifetime) _warn_unused_resul
format_lifetime((char[FORMAT_TIMESPAN_MAX+STRLEN("for ")]){}, FORMAT_TIMESPAN_MAX+STRLEN("for "), lifetime)
int address_new(Address **ret);
Address *address_free(Address *address);
Address* address_free(Address *address);
int address_get(Link *link, const Address *in, Address **ret);
int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg);
int address_remove(const Address *address, Link *link);

View File

@ -0,0 +1,28 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "networkd-address.h"
#include "tests.h"
static void test_FORMAT_LIFETIME_one(uint32_t lifetime, const char *expected) {
const char *t = FORMAT_LIFETIME(lifetime);
log_debug("%"PRIu32 "\"%s\" (expected \"%s\")", lifetime, t, expected);
assert_se(streq(t, expected));
}
static void test_FORMAT_LIFETIME(void) {
log_info("/* %s */", __func__);
test_FORMAT_LIFETIME_one(0, "for 0");
test_FORMAT_LIFETIME_one(1, "for 1s");
test_FORMAT_LIFETIME_one(3 * (USEC_PER_WEEK/USEC_PER_SEC), "for 3w");
test_FORMAT_LIFETIME_one(CACHE_INFO_INFINITY_LIFE_TIME, "forever");
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
test_FORMAT_LIFETIME();
return 0;
}

View File

@ -247,13 +247,13 @@ static ssize_t udev_event_subst_format(
r = sd_device_get_devpath(dev, &val);
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
case FORMAT_SUBST_KERNEL:
r = sd_device_get_sysname(dev, &val);
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
case FORMAT_SUBST_KERNEL_NUMBER:
r = sd_device_get_sysnum(dev, &val);
@ -261,7 +261,7 @@ static ssize_t udev_event_subst_format(
goto null_terminate;
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
case FORMAT_SUBST_ID:
if (!event->dev_parent)
@ -269,7 +269,7 @@ static ssize_t udev_event_subst_format(
r = sd_device_get_sysname(event->dev_parent, &val);
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
case FORMAT_SUBST_DRIVER:
if (!event->dev_parent)
@ -279,7 +279,7 @@ static ssize_t udev_event_subst_format(
goto null_terminate;
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
case FORMAT_SUBST_MAJOR:
case FORMAT_SUBST_MINOR: {
@ -288,7 +288,7 @@ static ssize_t udev_event_subst_format(
r = sd_device_get_devnum(dev, &devnum);
if (r < 0 && r != -ENOENT)
return r;
l = strpcpyf(&s, l, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum));
strpcpyf(&s, l, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum));
break;
}
case FORMAT_SUBST_RESULT: {
@ -307,7 +307,7 @@ static ssize_t udev_event_subst_format(
}
if (index == 0)
l = strpcpy(&s, l, event->program_result);
strpcpy(&s, l, event->program_result);
else {
const char *start, *p;
unsigned i;
@ -329,11 +329,11 @@ static ssize_t udev_event_subst_format(
start = p;
/* %c{2+} copies the whole string from the second part on */
if (has_plus)
l = strpcpy(&s, l, start);
strpcpy(&s, l, start);
else {
while (*p && !strchr(WHITESPACE, *p))
p++;
l = strnpcpy(&s, l, start, p - start);
strnpcpy(&s, l, start, p - start);
}
}
break;
@ -367,7 +367,7 @@ static ssize_t udev_event_subst_format(
count = udev_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
if (count > 0)
log_device_debug(dev, "%i character(s) replaced", count);
l = strpcpy(&s, l, vbuf);
strpcpy(&s, l, vbuf);
break;
}
case FORMAT_SUBST_PARENT:
@ -381,7 +381,7 @@ static ssize_t udev_event_subst_format(
goto null_terminate;
if (r < 0)
return r;
l = strpcpy(&s, l, val + STRLEN("/dev/"));
strpcpy(&s, l, val + STRLEN("/dev/"));
break;
case FORMAT_SUBST_DEVNODE:
r = sd_device_get_devname(dev, &val);
@ -389,34 +389,34 @@ static ssize_t udev_event_subst_format(
goto null_terminate;
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
case FORMAT_SUBST_NAME:
if (event->name)
l = strpcpy(&s, l, event->name);
strpcpy(&s, l, event->name);
else if (sd_device_get_devname(dev, &val) >= 0)
l = strpcpy(&s, l, val + STRLEN("/dev/"));
strpcpy(&s, l, val + STRLEN("/dev/"));
else {
r = sd_device_get_sysname(dev, &val);
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
}
break;
case FORMAT_SUBST_LINKS:
FOREACH_DEVICE_DEVLINK(dev, val)
if (s == dest)
l = strpcpy(&s, l, val + STRLEN("/dev/"));
strpcpy(&s, l, val + STRLEN("/dev/"));
else
l = strpcpyl(&s, l, " ", val + STRLEN("/dev/"), NULL);
strpcpyl(&s, l, " ", val + STRLEN("/dev/"), NULL);
if (s == dest)
goto null_terminate;
break;
case FORMAT_SUBST_ROOT:
l = strpcpy(&s, l, "/dev");
strpcpy(&s, l, "/dev");
break;
case FORMAT_SUBST_SYS:
l = strpcpy(&s, l, "/sys");
strpcpy(&s, l, "/sys");
break;
case FORMAT_SUBST_ENV:
if (isempty(attr))
@ -426,7 +426,7 @@ static ssize_t udev_event_subst_format(
goto null_terminate;
if (r < 0)
return r;
l = strpcpy(&s, l, val);
strpcpy(&s, l, val);
break;
default:
assert_not_reached("Unknown format substitution type");