Merge pull request #3201 from ssahani/net-word

networkd lib: cleanup FOREACH_WORD
This commit is contained in:
Lennart Poettering 2016-05-06 11:57:12 +02:00
commit b37bf74411
2 changed files with 40 additions and 39 deletions

View File

@ -225,8 +225,6 @@ int config_parse_ifnames(const char *unit,
void *userdata) { void *userdata) {
char ***sv = data; char ***sv = data;
const char *word, *state;
size_t l;
int r; int r;
assert(filename); assert(filename);
@ -234,22 +232,25 @@ int config_parse_ifnames(const char *unit,
assert(rvalue); assert(rvalue);
assert(data); assert(data);
FOREACH_WORD(word, l, rvalue, state) { for (;;) {
char *n; _cleanup_free_ char *word = NULL;
n = strndup(word, l); r = extract_first_word(&rvalue, &word, NULL, 0);
if (!n) if (r < 0)
return log_oom(); return r;
if (r == 0)
break;
if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) { if (!ascii_is_valid(word) || strlen(word) >= IFNAMSIZ) {
log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue); log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
free(n);
return 0; return 0;
} }
r = strv_consume(sv, n); r = strv_push(sv, word);
if (r < 0) if (r < 0)
return log_oom(); return log_oom();
word = NULL;
} }
return 0; return 0;
@ -380,28 +381,28 @@ void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size) {
int deserialize_in_addrs(struct in_addr **ret, const char *string) { int deserialize_in_addrs(struct in_addr **ret, const char *string) {
_cleanup_free_ struct in_addr *addresses = NULL; _cleanup_free_ struct in_addr *addresses = NULL;
int size = 0; int size = 0;
const char *word, *state;
size_t len;
assert(ret); assert(ret);
assert(string); assert(string);
FOREACH_WORD(word, len, string, state) { for (;;) {
_cleanup_free_ char *addr_str = NULL; _cleanup_free_ char *word = NULL;
struct in_addr *new_addresses; struct in_addr *new_addresses;
int r; int r;
r = extract_first_word(&string, &word, NULL, 0);
if (r < 0)
return r;
if (r == 0)
break;
new_addresses = realloc(addresses, (size + 1) * sizeof(struct in_addr)); new_addresses = realloc(addresses, (size + 1) * sizeof(struct in_addr));
if (!new_addresses) if (!new_addresses)
return -ENOMEM; return -ENOMEM;
else else
addresses = new_addresses; addresses = new_addresses;
addr_str = strndup(word, len); r = inet_pton(AF_INET, word, &(addresses[size]));
if (!addr_str)
return -ENOMEM;
r = inet_pton(AF_INET, addr_str, &(addresses[size]));
if (r <= 0) if (r <= 0)
continue; continue;
@ -431,28 +432,28 @@ void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses,
int deserialize_in6_addrs(struct in6_addr **ret, const char *string) { int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
_cleanup_free_ struct in6_addr *addresses = NULL; _cleanup_free_ struct in6_addr *addresses = NULL;
int size = 0; int size = 0;
const char *word, *state;
size_t len;
assert(ret); assert(ret);
assert(string); assert(string);
FOREACH_WORD(word, len, string, state) { for (;;) {
_cleanup_free_ char *addr_str = NULL; _cleanup_free_ char *word = NULL;
struct in6_addr *new_addresses; struct in6_addr *new_addresses;
int r; int r;
r = extract_first_word(&string, &word, NULL, 0);
if (r < 0)
return r;
if (r == 0)
break;
new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr)); new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr));
if (!new_addresses) if (!new_addresses)
return -ENOMEM; return -ENOMEM;
else else
addresses = new_addresses; addresses = new_addresses;
addr_str = strndup(word, len); r = inet_pton(AF_INET6, word, &(addresses[size]));
if (!addr_str)
return -ENOMEM;
r = inet_pton(AF_INET6, addr_str, &(addresses[size]));
if (r <= 0) if (r <= 0)
continue; continue;
@ -493,29 +494,29 @@ void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, siz
int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) { int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
_cleanup_free_ struct sd_dhcp_route *routes = NULL; _cleanup_free_ struct sd_dhcp_route *routes = NULL;
size_t size = 0, allocated = 0; size_t size = 0, allocated = 0;
const char *word, *state;
size_t len;
assert(ret); assert(ret);
assert(ret_size); assert(ret_size);
assert(ret_allocated); assert(ret_allocated);
assert(string); assert(string);
FOREACH_WORD(word, len, string, state) { /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
/* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */ for (;;) {
_cleanup_free_ char* entry = NULL; _cleanup_free_ char *word = NULL;
char *tok, *tok_end; char *tok, *tok_end;
unsigned n; unsigned n;
int r; int r;
r = extract_first_word(&string, &word, NULL, 0);
if (r < 0)
return r;
if (r == 0)
break;
if (!GREEDY_REALLOC(routes, allocated, size + 1)) if (!GREEDY_REALLOC(routes, allocated, size + 1))
return -ENOMEM; return -ENOMEM;
entry = strndup(word, len); tok = word;
if (!entry)
return -ENOMEM;
tok = entry;
/* get the subnet */ /* get the subnet */
tok_end = strchr(tok, '/'); tok_end = strchr(tok, '/');

View File

@ -512,7 +512,7 @@ int route_configure(Route *route, Link *link,
if (r < 0) if (r < 0)
return log_error_errno(r, "Could not set route table: %m"); return log_error_errno(r, "Could not set route table: %m");
/* Table attribute to allow allow more than 256. */ /* Table attribute to allow more than 256. */
r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table)); r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
if (r < 0) if (r < 0)
return log_error_errno(r, "Could not append RTA_TABLE attribute: %m"); return log_error_errno(r, "Could not append RTA_TABLE attribute: %m");