systemd: merge branch systemd into master

This commit is contained in:
Thomas Haller 2017-10-11 11:39:50 +02:00
commit 650a7022c1
37 changed files with 432 additions and 213 deletions

View file

@ -69,6 +69,22 @@ AC_CHECK_DECLS([
#include <string.h>
]])
AC_CHECK_HEADERS(sys/auxv.h)
AC_CHECK_DECLS([getrandom],
[AC_DEFINE([USE_SYS_RANDOM_H], [1], [sys/random.h is usable])
AC_DEFINE([HAVE_GETRANDOM], [1], [has getrandom])
],
[AC_CHECK_DECLS([getrandom],
[AC_DEFINE([USE_SYS_RANDOM_H], [0], [sys/random.h is usable])
AC_DEFINE([HAVE_GETRANDOM], [1], [has getrandom])],
[AC_DEFINE([USE_SYS_RANDOM_H], [0], [sys/random.h is usable])
AC_DEFINE([HAVE_GETRANDOM], [0], [has getrandom])],
[[#include <linux/random.h>
]])],
[[#include <sys/random.h>
]])
dnl
dnl translation support
dnl

View file

@ -32,6 +32,18 @@
#define CLOCK_BOOTTIME 7
#endif
#if defined(HAVE_DECL_EXPLICIT_BZERO) && HAVE_DECL_EXPLICIT_BZERO == 1
#define HAVE_EXPLICIT_BZERO 1
#else
#define HAVE_EXPLICIT_BZERO 0
#endif
#define ENABLE_DEBUG_HASHMAP 0
#ifndef HAVE_SYS_AUXV_H
#define HAVE_SYS_AUXV_H 0
#endif
/*****************************************************************************/
static inline NMLogLevel

View file

@ -428,7 +428,7 @@ char *octescape(const char *s, size_t len) {
for (f = s, t = r; f < s + len; f++) {
if (*f < ' ' || *f >= 127 || *f == '\\' || *f == '"') {
if (*f < ' ' || *f >= 127 || IN_SET(*f, '\\', '"')) {
*(t++) = '\\';
*(t++) = '0' + (*f >> 6);
*(t++) = '0' + ((*f >> 3) & 8);

View file

@ -154,7 +154,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
for (;; (*p)++, c = **p) {
if (c == 0)
goto finish_force_terminate;
else if ((c == '\'' || c == '"') && (flags & EXTRACT_QUOTES)) {
else if (IN_SET(c, '\'', '"') && (flags & EXTRACT_QUOTES)) {
quote = c;
break;
} else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {

View file

@ -32,6 +32,7 @@
#include "alloc-util.h"
#include "ctype.h"
#include "def.h"
#include "env-util.h"
#include "escape.h"
#include "fd-util.h"
@ -54,13 +55,18 @@
#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
#if 0 /* NM_IGNORED */
int write_string_stream_ts(
FILE *f,
const char *line,
WriteStringFileFlags flags,
struct timespec *ts) {
assert(f);
assert(line);
fputs(line, f);
if (enforce_newline && !endswith(line, "\n"))
if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
fputc('\n', f);
if (ts) {
@ -70,10 +76,18 @@ int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, stru
return -errno;
}
return fflush_and_check(f);
if (flags & WRITE_STRING_FILE_SYNC)
return fflush_sync_and_check(f);
else
return fflush_and_check(f);
}
static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) {
static int write_string_file_atomic(
const char *fn,
const char *line,
WriteStringFileFlags flags,
struct timespec *ts) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
@ -87,22 +101,28 @@ static int write_string_file_atomic(const char *fn, const char *line, bool enfor
(void) fchmod_umask(fileno(f), 0644);
r = write_string_stream(f, line, enforce_newline);
if (r >= 0 && do_fsync)
r = fflush_sync_and_check(f);
r = write_string_stream_ts(f, line, flags, ts);
if (r < 0)
goto fail;
if (r >= 0) {
if (rename(p, fn) < 0)
r = -errno;
if (rename(p, fn) < 0) {
r = -errno;
goto fail;
}
if (r < 0)
(void) unlink(p);
return 0;
fail:
(void) unlink(p);
return r;
}
int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
int write_string_file_ts(
const char *fn,
const char *line,
WriteStringFileFlags flags,
struct timespec *ts) {
_cleanup_fclose_ FILE *f = NULL;
int q, r;
@ -115,8 +135,7 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags
if (flags & WRITE_STRING_FILE_ATOMIC) {
assert(flags & WRITE_STRING_FILE_CREATE);
r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
flags & WRITE_STRING_FILE_SYNC);
r = write_string_file_atomic(fn, line, flags, ts);
if (r < 0)
goto fail;
@ -149,16 +168,10 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags
}
}
r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
r = write_string_stream_ts(f, line, flags, ts);
if (r < 0)
goto fail;
if (flags & WRITE_STRING_FILE_SYNC) {
r = fflush_sync_and_check(f);
if (r < 0)
return r;
}
return 0;
fail:
@ -179,7 +192,7 @@ fail:
int read_one_line_file(const char *fn, char **line) {
_cleanup_fclose_ FILE *f = NULL;
char t[LINE_MAX], *c;
int r;
assert(fn);
assert(line);
@ -188,22 +201,10 @@ int read_one_line_file(const char *fn, char **line) {
if (!f)
return -errno;
if (!fgets(t, sizeof(t), f)) {
if (ferror(f))
return errno > 0 ? -errno : -EIO;
t[0] = 0;
}
c = strdup(t);
if (!c)
return -ENOMEM;
truncate_nl(c);
*line = c;
return 0;
r = read_line(f, LONG_LINE_MAX, line);
return r < 0 ? r : 0;
}
#endif /* NM_IGNORED */
int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
_cleanup_fclose_ FILE *f = NULL;
@ -261,11 +262,11 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
if (st.st_size > READ_FULL_BYTES_MAX)
return -E2BIG;
/* Start with the right file size, but be prepared for
* files from /proc which generally report a file size
* of 0 */
/* Start with the right file size, but be prepared for files from /proc which generally report a file
* size of 0. Note that we increase the size to read here by one, so that the first read attempt
* already makes us notice the EOF. */
if (st.st_size > 0)
n = st.st_size;
n = st.st_size + 1;
}
l = 0;
@ -278,12 +279,13 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
return -ENOMEM;
buf = t;
errno = 0;
k = fread(buf + l, 1, n - l, f);
if (k > 0)
l += k;
if (ferror(f))
return -errno;
return errno > 0 ? -errno : -EIO;
if (feof(f))
break;
@ -893,7 +895,6 @@ int write_env_file(const char *fname, char **l) {
unlink(p);
return r;
}
#endif /* NM_IGNORED */
int executable_is_script(const char *path, char **interpreter) {
int r;
@ -923,6 +924,7 @@ int executable_is_script(const char *path, char **interpreter) {
*interpreter = ans;
return 1;
}
#endif /* NM_IGNORED */
/**
* Retrieve one field from a file like /proc/self/status. pattern
@ -1208,6 +1210,7 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
return 0;
}
#if 0 /* NM_IGNORED */
int tempfn_random(const char *p, const char *extra, char **ret) {
const char *fn;
char *t, *x;
@ -1250,7 +1253,6 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
return 0;
}
#if 0 /* NM_IGNORED */
int tempfn_random_child(const char *p, const char *extra, char **ret) {
char *t, *x;
uint64_t u;
@ -1535,4 +1537,78 @@ int mkdtemp_malloc(const char *template, char **ret) {
*ret = p;
return 0;
}
static inline void funlockfilep(FILE **f) {
funlockfile(*f);
}
int read_line(FILE *f, size_t limit, char **ret) {
_cleanup_free_ char *buffer = NULL;
size_t n = 0, allocated = 0, count = 0;
assert(f);
/* Something like a bounded version of getline().
*
* Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
* returned.
*
* Returns the number of bytes read from the files (i.e. including delimiters this hence usually differs from
* the number of characters in the returned string). When EOF is hit, 0 is returned.
*
* The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
* delimiters. If the limit is hit we fail and return -ENOBUFS.
*
* If a line shall be skipped ret may be initialized as NULL. */
if (ret) {
if (!GREEDY_REALLOC(buffer, allocated, 1))
return -ENOMEM;
}
{
_cleanup_(funlockfilep) FILE *flocked = f;
flockfile(f);
for (;;) {
int c;
if (n >= limit)
return -ENOBUFS;
errno = 0;
c = fgetc_unlocked(f);
if (c == EOF) {
/* if we read an error, and have no data to return, then propagate the error */
if (ferror_unlocked(f) && n == 0)
return errno > 0 ? -errno : -EIO;
break;
}
count++;
if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
break;
if (ret) {
if (!GREEDY_REALLOC(buffer, allocated, n + 2))
return -ENOMEM;
buffer[n] = (char) c;
}
n++;
}
}
if (ret) {
buffer[n] = 0;
*ret = buffer;
buffer = NULL;
}
return (int) count;
}
#endif /* NM_IGNORED */

View file

@ -36,9 +36,9 @@ typedef enum {
WRITE_STRING_FILE_SYNC = 1<<4,
} WriteStringFileFlags;
int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts);
static inline int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
return write_string_stream_ts(f, line, enforce_newline, NULL);
int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
return write_string_stream_ts(f, line, flags, NULL);
}
int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
@ -101,3 +101,5 @@ int link_tmpfile(int fd, const char *path, const char *target);
int read_nul_string(FILE *f, char **ret);
int mkdtemp_malloc(const char *template, char **ret);
int read_line(FILE *f, size_t limit, char **ret);

View file

@ -331,7 +331,7 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi
mkdir_parents(path, 0755);
fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY,
(mode == 0 || mode == MODE_INVALID) ? 0644 : mode);
IN_SET(mode, 0, MODE_INVALID) ? 0644 : mode);
if (fd < 0)
return -errno;
@ -366,22 +366,25 @@ int touch(const char *path) {
}
int symlink_idempotent(const char *from, const char *to) {
_cleanup_free_ char *p = NULL;
int r;
assert(from);
assert(to);
if (symlink(from, to) < 0) {
_cleanup_free_ char *p = NULL;
if (errno != EEXIST)
return -errno;
r = readlink_malloc(to, &p);
if (r < 0)
if (r == -EINVAL) /* Not a symlink? In that case return the original error we encountered: -EEXIST */
return -EEXIST;
if (r < 0) /* Any other error? In that case propagate it as is */
return r;
if (!streq(p, from))
return -EINVAL;
if (!streq(p, from)) /* Not the symlink we want it to be? In that case, propagate the original -EEXIST */
return -EEXIST;
}
return 0;

View file

@ -36,7 +36,7 @@
#include "strv.h"
#include "util.h"
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
#include <pthread.h>
#include "list.h"
#endif
@ -144,7 +144,7 @@ typedef uint8_t dib_raw_t;
#define DIB_FREE UINT_MAX
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
struct hashmap_debug_info {
LIST_FIELDS(struct hashmap_debug_info, debug_list);
unsigned max_entries; /* high watermark of n_entries */
@ -501,7 +501,7 @@ static void base_remove_entry(HashmapBase *h, unsigned idx) {
dibs = dib_raw_ptr(h);
assert(dibs[idx] != DIB_RAW_FREE);
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
h->debug.rem_count++;
h->debug.last_rem_idx = idx;
#endif
@ -510,7 +510,7 @@ static void base_remove_entry(HashmapBase *h, unsigned idx) {
/* Find the stop bucket ("right"). It is either free or has DIB == 0. */
for (right = next_idx(h, left); ; right = next_idx(h, right)) {
raw_dib = dibs[right];
if (raw_dib == 0 || raw_dib == DIB_RAW_FREE)
if (IN_SET(raw_dib, 0, DIB_RAW_FREE))
break;
/* The buckets are not supposed to be all occupied and with DIB > 0.
@ -580,7 +580,7 @@ static unsigned hashmap_iterate_in_insertion_order(OrderedHashmap *h, Iterator *
assert(e->p.b.key == i->next_key);
}
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
i->prev_idx = idx;
#endif
@ -637,7 +637,7 @@ static unsigned hashmap_iterate_in_internal_order(HashmapBase *h, Iterator *i) {
}
idx = i->idx;
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
i->prev_idx = idx;
#endif
@ -660,7 +660,7 @@ static unsigned hashmap_iterate_entry(HashmapBase *h, Iterator *i) {
return IDX_NIL;
}
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
if (i->idx == IDX_FIRST) {
i->put_count = h->debug.put_count;
i->rem_count = h->debug.rem_count;
@ -752,7 +752,7 @@ static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enu
shared_hash_key_initialized= true;
}
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
h->debug.func = func;
h->debug.file = file;
h->debug.line = line;
@ -809,7 +809,7 @@ static void hashmap_free_no_clear(HashmapBase *h) {
assert(!h->has_indirect);
assert(!h->n_direct_entries);
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0);
LIST_REMOVE(debug_list, hashmap_debug_list, &h->debug);
assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
@ -921,7 +921,7 @@ static bool hashmap_put_robin_hood(HashmapBase *h, unsigned idx,
dib_raw_t raw_dib, *dibs;
unsigned dib, distance;
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
h->debug.put_count++;
#endif
@ -929,7 +929,7 @@ static bool hashmap_put_robin_hood(HashmapBase *h, unsigned idx,
for (distance = 0; ; distance++) {
raw_dib = dibs[idx];
if (raw_dib == DIB_RAW_FREE || raw_dib == DIB_RAW_REHASH) {
if (IN_SET(raw_dib, DIB_RAW_FREE, DIB_RAW_REHASH)) {
if (raw_dib == DIB_RAW_REHASH)
bucket_move_entry(h, swap, idx, IDX_TMP);
@ -1014,7 +1014,7 @@ static int hashmap_base_put_boldly(HashmapBase *h, unsigned idx,
assert_se(hashmap_put_robin_hood(h, idx, swap) == false);
n_entries_inc(h);
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
h->debug.max_entries = MAX(h->debug.max_entries, n_entries(h));
#endif
@ -1242,7 +1242,7 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) {
idx = bucket_scan(h, hash, key);
if (idx != IDX_NIL) {
e = plain_bucket_at(h, idx);
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
/* Although the key is equal, the key pointer may have changed,
* and this would break our assumption for iterating. So count
* this operation as incompatible with iteration. */

View file

@ -58,7 +58,7 @@ typedef struct Set Set; /* Stores just keys */
typedef struct {
unsigned idx; /* index of an entry to be iterated next */
const void *next_key; /* expected value of that entry's key pointer */
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
unsigned put_count; /* hashmap's put_count recorded at start of iteration */
unsigned rem_count; /* hashmap's rem_count in previous iteration */
unsigned prev_idx; /* idx in previous iteration */
@ -89,7 +89,7 @@ typedef struct {
(Hashmap*)(h), \
(void)0)
#ifdef ENABLE_DEBUG_HASHMAP
#if ENABLE_DEBUG_HASHMAP
# define HASHMAP_DEBUG_PARAMS , const char *func, const char *file, int line
# define HASHMAP_DEBUG_SRC_ARGS , __func__, __FILE__, __LINE__
# define HASHMAP_DEBUG_PASS_ARGS , func, file, line

View file

@ -94,9 +94,7 @@ static bool hostname_valid_char(char c) {
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' ||
c == '_' ||
c == '.';
IN_SET(c, '-', '_', '.');
}
/**
@ -240,7 +238,7 @@ int read_hostname_config(const char *path, char **hostname) {
/* may have comments, ignore them */
FOREACH_LINE(l, f, return -errno) {
truncate_nl(l);
if (l[0] != '\0' && l[0] != '#') {
if (!IN_SET(l[0], '\0', '#')) {
/* found line with value */
name = hostname_cleanup(l);
name = strdup(name);

View file

@ -310,22 +310,22 @@ int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
return 0;
}
int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret) {
int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret) {
int r;
assert(s);
r = in_addr_from_string(AF_INET, s, ret);
if (r >= 0) {
if (family)
*family = AF_INET;
if (ret_family)
*ret_family = AF_INET;
return 0;
}
r = in_addr_from_string(AF_INET6, s, ret);
if (r >= 0) {
if (family)
*family = AF_INET6;
if (ret_family)
*ret_family = AF_INET6;
return 0;
}
@ -373,13 +373,13 @@ int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_u
return r;
}
unsigned char in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr) {
assert(addr);
return 32 - u32ctz(be32toh(addr->s_addr));
}
struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
assert(addr);
assert(prefixlen <= 32);
@ -392,7 +392,7 @@ struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char
return addr;
}
int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
uint8_t msb_octet = *(uint8_t*) addr;
/* addr may not be aligned, so make sure we only access it byte-wise */
@ -416,18 +416,18 @@ int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixl
return 0;
}
int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
unsigned char prefixlen;
int r;
assert(addr);
assert(mask);
r = in_addr_default_prefixlen(addr, &prefixlen);
r = in4_addr_default_prefixlen(addr, &prefixlen);
if (r < 0)
return r;
in_addr_prefixlen_to_netmask(mask, prefixlen);
in4_addr_prefixlen_to_netmask(mask, prefixlen);
return 0;
}
@ -438,7 +438,7 @@ int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen)
if (family == AF_INET) {
struct in_addr mask;
if (!in_addr_prefixlen_to_netmask(&mask, prefixlen))
if (!in4_addr_prefixlen_to_netmask(&mask, prefixlen))
return -EINVAL;
addr->in.s_addr &= mask.s_addr;
@ -468,10 +468,57 @@ int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen)
return -EAFNOSUPPORT;
}
int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, uint8_t *ret_prefixlen) {
int in_addr_prefix_covers(int family,
const union in_addr_union *prefix,
unsigned char prefixlen,
const union in_addr_union *address) {
union in_addr_union masked_prefix, masked_address;
int r;
assert(prefix);
assert(address);
masked_prefix = *prefix;
r = in_addr_mask(family, &masked_prefix, prefixlen);
if (r < 0)
return r;
masked_address = *address;
r = in_addr_mask(family, &masked_address, prefixlen);
if (r < 0)
return r;
return in_addr_equal(family, &masked_prefix, &masked_address);
}
int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret) {
uint8_t u;
int r;
if (!IN_SET(family, AF_INET, AF_INET6))
return -EAFNOSUPPORT;
r = safe_atou8(p, &u);
if (r < 0)
return r;
if (u > FAMILY_ADDRESS_SIZE(family) * 8)
return -ERANGE;
*ret = u;
return 0;
}
int in_addr_prefix_from_string(
const char *p,
int family,
union in_addr_union *ret_prefix,
unsigned char *ret_prefixlen) {
union in_addr_union buffer;
const char *e, *l;
uint8_t k;
unsigned char k;
int r;
assert(p);
@ -489,24 +536,59 @@ int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *r
if (r < 0)
return r;
k = FAMILY_ADDRESS_SIZE(family) * 8;
if (e) {
uint8_t n;
r = safe_atou8(e + 1, &n);
r = in_addr_parse_prefixlen(family, e+1, &k);
if (r < 0)
return r;
} else
k = FAMILY_ADDRESS_SIZE(family) * 8;
if (n > k)
return -ERANGE;
k = n;
}
*ret_prefix = buffer;
*ret_prefixlen = k;
if (ret_prefix)
*ret_prefix = buffer;
if (ret_prefixlen)
*ret_prefixlen = k;
return 0;
}
int in_addr_prefix_from_string_auto(
const char *p,
int *ret_family,
union in_addr_union *ret_prefix,
unsigned char *ret_prefixlen) {
union in_addr_union buffer;
const char *e, *l;
unsigned char k;
int family, r;
assert(p);
e = strchr(p, '/');
if (e)
l = strndupa(p, e - p);
else
l = p;
r = in_addr_from_string_auto(l, &family, &buffer);
if (r < 0)
return r;
if (e) {
r = in_addr_parse_prefixlen(family, e+1, &k);
if (r < 0)
return r;
} else
k = FAMILY_ADDRESS_SIZE(family) * 8;
if (ret_family)
*ret_family = family;
if (ret_prefix)
*ret_prefix = buffer;
if (ret_prefixlen)
*ret_prefixlen = k;
return 0;
}
#endif /* NM_IGNORED */

View file

@ -53,17 +53,20 @@ int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen);
int in_addr_to_string(int family, const union in_addr_union *u, char **ret);
int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret);
int in_addr_from_string(int family, const char *s, union in_addr_union *ret);
int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret);
int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret);
int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex);
unsigned char in_addr_netmask_to_prefixlen(const struct in_addr *addr);
struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen);
int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen);
int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask);
unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr);
struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen);
int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen);
int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask);
int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen);
int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, uint8_t *ret_prefixlen);
int in_addr_prefix_covers(int family, const union in_addr_union *prefix, unsigned char prefixlen, const union in_addr_union *address);
int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret);
int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
int in_addr_prefix_from_string_auto(const char *p, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
static inline size_t FAMILY_ADDRESS_SIZE(int family) {
assert(family == AF_INET || family == AF_INET6);
assert(IN_SET(family, AF_INET, AF_INET6));
return family == AF_INET6 ? 16 : 4;
}

View file

@ -40,14 +40,6 @@ int fd_wait_for_event(int fd, int event, usec_t timeout);
ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
#define IOVEC_SET_STRING(i, s) \
do { \
struct iovec *_i = &(i); \
char *_s = (char *)(s); \
_i->iov_base = _s; \
_i->iov_len = strlen(_s); \
} while (false)
static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
unsigned j;
size_t r = 0;
@ -93,3 +85,8 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
return FILE_SIZE_VALID(l);
}
#define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) }
#define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
#define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
#define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)

View file

@ -192,6 +192,15 @@ int log_format_iovec(
const char *format,
va_list ap) _printf_(6, 0);
int log_struct_iovec_internal(
int level,
int error,
const char *file,
int line,
const char *func,
const struct iovec input_iovec[],
size_t n_input_iovec);
/* This modifies the buffer passed! */
int log_dump_internal(
int level,
@ -276,6 +285,11 @@ void log_assert_failed_return_realm(
error, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
#define log_struct_iovec_errno(level, error, iovec, n_iovec) \
log_struct_iovec_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
error, __FILE__, __LINE__, __func__, iovec, n_iovec)
#define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
/* This modifies the buffer passed! */
#define log_dump(level, buffer) \
log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
@ -295,6 +309,7 @@ void log_received_signal(int level, const struct signalfd_siginfo *si);
void log_set_upgrade_syslog_to_journal(bool b);
void log_set_always_reopen_console(bool b);
void log_set_open_when_needed(bool b);
int log_syntax_internal(
const char *unit,

View file

@ -156,7 +156,7 @@ int parse_size(const char *t, uint64_t base, uint64_t *size) {
unsigned n_entries, start_pos = 0;
assert(t);
assert(base == 1000 || base == 1024);
assert(IN_SET(base, 1000, 1024));
assert(size);
if (base == 1000) {

View file

@ -134,8 +134,7 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
/* Skip the common part. */
for (;;) {
size_t a;
size_t b;
size_t a, b;
from_dir += strspn(from_dir, "/");
to_path += strspn(to_path, "/");
@ -147,7 +146,6 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
else
/* from_dir is a parent directory of to_path. */
r = strdup(to_path);
if (!r)
return -ENOMEM;
@ -178,21 +176,32 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
/* Count the number of necessary ".." elements. */
for (n_parents = 0;;) {
size_t w;
from_dir += strspn(from_dir, "/");
if (!*from_dir)
break;
from_dir += strcspn(from_dir, "/");
n_parents++;
w = strcspn(from_dir, "/");
/* If this includes ".." we can't do a simple series of "..", refuse */
if (w == 2 && from_dir[0] == '.' && from_dir[1] == '.')
return -EINVAL;
/* Count number of elements, except if they are "." */
if (w != 1 || from_dir[0] != '.')
n_parents++;
from_dir += w;
}
r = malloc(n_parents * 3 + strlen(to_path) + 1);
r = new(char, n_parents * 3 + strlen(to_path) + 1);
if (!r)
return -ENOMEM;
for (p = r; n_parents > 0; n_parents--, p += 3)
memcpy(p, "../", 3);
for (p = r; n_parents > 0; n_parents--)
p = mempcpy(p, "../", 3);
strcpy(p, to_path);
path_kill_slashes(r);

View file

@ -27,14 +27,16 @@
#include "string-util.h"
#include "time-util.h"
#if 0 /* NM_IGNORED */
#define DEFAULT_PATH_NORMAL "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
#define DEFAULT_PATH_SPLIT_USR DEFAULT_PATH_NORMAL ":/sbin:/bin"
#ifdef HAVE_SPLIT_USR
#if HAVE_SPLIT_USR
# define DEFAULT_PATH DEFAULT_PATH_SPLIT_USR
#else
# define DEFAULT_PATH DEFAULT_PATH_NORMAL
#endif
#endif /* NM_IGNORED */
bool is_path(const char *p) _pure_;
int path_split_and_make_absolute(const char *p, char ***ret);

View file

@ -36,9 +36,11 @@
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>
#ifdef HAVE_VALGRIND_VALGRIND_H
#if 0 /* NM_IGNORED */
#if HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
#endif
#endif /* NM_IGNORED */
#include "alloc-util.h"
#include "architecture.h"
@ -395,7 +397,7 @@ int is_kernel_thread(pid_t pid) {
bool eof;
FILE *f;
if (pid == 0 || pid == 1 || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
if (IN_SET(pid, 0, 1) || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
return 0;
assert(pid > 1);
@ -478,7 +480,7 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
assert(field);
assert(uid);
if (!pid_is_valid(pid))
if (pid < 0)
return -EINVAL;
p = procfs_file_alloca(pid, "status");
@ -691,8 +693,7 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_cod
log_debug("%s succeeded.", name);
return status.si_status;
} else if (status.si_code == CLD_KILLED ||
status.si_code == CLD_DUMPED) {
} else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED)) {
log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
return -EPROTO;
@ -791,7 +792,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
bool pid_is_unwaited(pid_t pid) {
/* Checks whether a PID is still valid at all, including a zombie */
if (!pid_is_valid(pid))
if (pid < 0)
return false;
if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */
@ -811,7 +812,7 @@ bool pid_is_alive(pid_t pid) {
/* Checks whether a PID is still valid and not a zombie */
if (!pid_is_valid(pid))
if (pid < 0)
return false;
if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
@ -821,7 +822,7 @@ bool pid_is_alive(pid_t pid) {
return true;
r = get_process_state(pid);
if (r == -ESRCH || r == 'Z')
if (IN_SET(r, -ESRCH, 'Z'))
return false;
return true;
@ -830,7 +831,7 @@ bool pid_is_alive(pid_t pid) {
int pid_from_same_root_fs(pid_t pid) {
const char *root;
if (!pid_is_valid(pid))
if (pid < 0)
return false;
if (pid == 0 || pid == getpid_cached())
@ -951,7 +952,7 @@ int opinionated_personality(unsigned long *ret) {
}
void valgrind_summary_hack(void) {
#ifdef HAVE_VALGRIND_VALGRIND_H
#if HAVE_VALGRIND_VALGRIND_H
if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
pid_t pid;
pid = raw_clone(SIGCHLD);
@ -1029,7 +1030,7 @@ pid_t getpid_cached(void) {
* objects were used across fork()s. With this caching the old behaviour is somewhat restored.
*
* https://bugzilla.redhat.com/show_bug.cgi?id=1443976
* https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=1d2bc2eae969543b89850e35e532f3144122d80a
* https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c579f48edba88380635ab98cb612030e3ed8691e
*/
current_value = __sync_val_compare_and_swap(&cached_pid, CACHED_PID_UNSET, CACHED_PID_BUSY);

View file

@ -28,11 +28,11 @@
#include <linux/random.h>
#include <stdint.h>
#ifdef HAVE_SYS_AUXV_H
#if HAVE_SYS_AUXV_H
# include <sys/auxv.h>
#endif
#ifdef USE_SYS_RANDOM_H
#if USE_SYS_RANDOM_H
# include <sys/random.h>
#else
# include <linux/random.h>
@ -45,7 +45,6 @@
#include "time-util.h"
int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
#if 0 /* NM_IGNORED */
static int have_syscall = -1;
_cleanup_close_ int fd = -1;
@ -92,10 +91,6 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
} else
return -errno;
}
#else /* NM_IGNORED */
_cleanup_close_ int fd = -1;
unsigned already_done = 0;
#endif /* NM_IGNORED */
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
@ -107,14 +102,14 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
void initialize_srand(void) {
static bool srand_called = false;
unsigned x;
#ifdef HAVE_SYS_AUXV_H
#if HAVE_SYS_AUXV_H
void *auxv;
#endif
if (srand_called)
return;
#ifdef HAVE_SYS_AUXV_H
#if HAVE_SYS_AUXV_H
/* The kernel provides us with 16 bytes of entropy in auxv, so let's
* try to make use of that to seed the pseudo-random generator. It's
* better than nothing... */

View file

@ -136,3 +136,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
#define _cleanup_set_free_ _cleanup_(set_freep)
#define _cleanup_set_free_free_ _cleanup_(set_free_freep)
int set_make(Set **ret, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS, void *add, ...);

View file

@ -51,7 +51,7 @@
#include "util.h"
#if 0 /* NM_IGNORED */
#ifdef ENABLE_IDN
#if ENABLE_IDN
# define IDN_FLAGS (NI_IDN|NI_IDN_USE_STD3_ASCII_RULES)
#else
# define IDN_FLAGS 0
@ -271,7 +271,7 @@ int socket_address_verify(const SocketAddress *a) {
if (a->sockaddr.in.sin_port == 0)
return -EINVAL;
if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM))
return -EINVAL;
return 0;
@ -283,7 +283,7 @@ int socket_address_verify(const SocketAddress *a) {
if (a->sockaddr.in6.sin6_port == 0)
return -EINVAL;
if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM))
return -EINVAL;
return 0;
@ -307,7 +307,7 @@ int socket_address_verify(const SocketAddress *a) {
}
}
if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM && a->type != SOCK_SEQPACKET)
if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
return -EINVAL;
return 0;
@ -317,7 +317,7 @@ int socket_address_verify(const SocketAddress *a) {
if (a->size != sizeof(struct sockaddr_nl))
return -EINVAL;
if (a->type != SOCK_RAW && a->type != SOCK_DGRAM)
if (!IN_SET(a->type, SOCK_RAW, SOCK_DGRAM))
return -EINVAL;
return 0;
@ -326,7 +326,7 @@ int socket_address_verify(const SocketAddress *a) {
if (a->size != sizeof(struct sockaddr_vm))
return -EINVAL;
if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM))
return -EINVAL;
return 0;
@ -367,8 +367,7 @@ bool socket_address_can_accept(const SocketAddress *a) {
assert(a);
return
a->type == SOCK_STREAM ||
a->type == SOCK_SEQPACKET;
IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
}
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
@ -896,7 +895,7 @@ bool ifname_valid(const char *p) {
if ((unsigned char) *p <= 32U)
return false;
if (*p == ':' || *p == '/')
if (IN_SET(*p, ':', '/'))
return false;
numeric = numeric && (*p >= '0' && *p <= '9');
@ -1085,7 +1084,7 @@ ssize_t next_datagram_size_fd(int fd) {
l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
if (l < 0) {
if (errno == EOPNOTSUPP || errno == EFAULT)
if (IN_SET(errno, EOPNOTSUPP, EFAULT))
goto fallback;
return -errno;

View file

@ -677,7 +677,7 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz) {
case STATE_BRACKET:
if (i >= *ibuf + isz || /* EOT */
(!(*i >= '0' && *i <= '9') && *i != ';' && *i != 'm')) {
(!(*i >= '0' && *i <= '9') && !IN_SET(*i, ';', 'm'))) {
fputc_unlocked('\x1B', f);
fputc_unlocked('[', f);
state = STATE_OTHER;
@ -830,7 +830,7 @@ int free_and_strdup(char **p, const char *s) {
return 1;
}
#if !HAVE_DECL_EXPLICIT_BZERO
#if !HAVE_EXPLICIT_BZERO
/*
* Pointer to memset is volatile so that compiler must de-reference
* the pointer and can't assume that it points to any function in

View file

@ -120,7 +120,7 @@ char *strjoin_real(const char *x, ...) _sentinel_;
({ \
const char *_appendees_[] = { a, __VA_ARGS__ }; \
char *_d_, *_p_; \
int _len_ = 0; \
size_t _len_ = 0; \
unsigned _i_; \
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
_len_ += strlen(_appendees_[_i_]); \
@ -189,7 +189,7 @@ static inline void *memmem_safe(const void *haystack, size_t haystacklen, const
return memmem(haystack, haystacklen, needle, needlelen);
}
#if !HAVE_DECL_EXPLICIT_BZERO
#if !HAVE_EXPLICIT_BZERO
void explicit_bzero(void *p, size_t l);
#endif

View file

@ -854,11 +854,11 @@ parse_usec:
}
from_tm:
x = mktime_or_timegm(&tm, utc);
if (x < 0)
if (weekday >= 0 && tm.tm_wday != weekday)
return -EINVAL;
if (weekday >= 0 && tm.tm_wday != weekday)
x = mktime_or_timegm(&tm, utc);
if (x < 0)
return -EINVAL;
ret = (usec_t) x * USEC_PER_SEC + x_usec;
@ -888,20 +888,18 @@ typedef struct ParseTimestampResult {
} ParseTimestampResult;
int parse_timestamp(const char *t, usec_t *usec) {
char *last_space, *timezone = NULL;
char *last_space, *tz = NULL;
ParseTimestampResult *shared, tmp;
int r;
pid_t pid;
last_space = strrchr(t, ' ');
if (last_space != NULL && timezone_is_valid(last_space + 1))
timezone = last_space + 1;
tz = last_space + 1;
if (timezone == NULL || endswith_no_case(t, " UTC"))
if (tz == NULL || endswith_no_case(t, " UTC"))
return parse_timestamp_impl(t, usec, false);
t = strndupa(t, last_space - t);
shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (shared == MAP_FAILED)
return negative_errno();
@ -915,14 +913,24 @@ int parse_timestamp(const char *t, usec_t *usec) {
}
if (pid == 0) {
if (setenv("TZ", timezone, 1) != 0) {
bool with_tz = true;
if (setenv("TZ", tz, 1) != 0) {
shared->return_value = negative_errno();
_exit(EXIT_FAILURE);
}
tzset();
shared->return_value = parse_timestamp_impl(t, &shared->usec, true);
/* If there is a timezone that matches the tzname fields, leave the parsing to the implementation.
* Otherwise just cut it off */
with_tz = !STR_IN_SET(tz, tzname[0], tzname[1]);
/*cut off the timezone if we dont need it*/
if (with_tz)
t = strndupa(t, last_space - t);
shared->return_value = parse_timestamp_impl(t, &shared->usec, with_tz);
_exit(EXIT_SUCCESS);
}
@ -1079,7 +1087,11 @@ int parse_sec(const char *t, usec_t *usec) {
}
int parse_sec_fix_0(const char *t, usec_t *usec) {
assert(t);
assert(usec);
t += strspn(t, WHITESPACE);
if (streq(t, "0")) {
*usec = USEC_INFINITY;
return 0;
@ -1306,7 +1318,7 @@ bool timezone_is_valid(const char *name) {
if (!(*p >= '0' && *p <= '9') &&
!(*p >= 'a' && *p <= 'z') &&
!(*p >= 'A' && *p <= 'Z') &&
!(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
!IN_SET(*p, '-', '_', '+', '/'))
return false;
if (*p == '/') {

View file

@ -75,7 +75,7 @@ static bool unichar_is_control(char32_t ch) {
'\t' is in C0 range, but more or less harmless and commonly used.
*/
return (ch < ' ' && ch != '\t' && ch != '\n') ||
return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
(0x7F <= ch && ch <= 0x9F);
}

View file

@ -383,7 +383,7 @@ int on_ac_power(void) {
device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (device < 0) {
if (errno == ENOENT || errno == ENOTDIR)
if (IN_SET(errno, ENOENT, ENOTDIR))
continue;
return -errno;

View file

@ -36,8 +36,8 @@ int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
size_t offset = 0;
int r;
assert(op == BOOTREQUEST || op == BOOTREPLY);
assert(arp_type == ARPHRD_ETHER || arp_type == ARPHRD_INFINIBAND);
assert(IN_SET(op, BOOTREQUEST, BOOTREPLY));
assert(IN_SET(arp_type, ARPHRD_ETHER, ARPHRD_INFINIBAND));
message->op = op;
message->htype = arp_type;

View file

@ -459,9 +459,7 @@ int sd_dhcp_client_set_mtu(sd_dhcp_client *client, uint32_t mtu) {
int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
assert_return(client, -EINVAL);
if (client->state != DHCP_STATE_BOUND &&
client->state != DHCP_STATE_RENEWING &&
client->state != DHCP_STATE_REBINDING)
if (!IN_SET(client->state, DHCP_STATE_BOUND, DHCP_STATE_RENEWING, DHCP_STATE_REBINDING))
return -EADDRNOTAVAIL;
if (ret)
@ -534,7 +532,7 @@ static int client_message_init(
assert(ret);
assert(_optlen);
assert(_optoffset);
assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
assert(IN_SET(type, DHCP_DISCOVER, DHCP_REQUEST));
optlen = DHCP_MIN_OPTIONS_SIZE;
size = sizeof(DHCPPacket) + optlen;
@ -713,8 +711,7 @@ static int client_send_discover(sd_dhcp_client *client) {
int r;
assert(client);
assert(client->state == DHCP_STATE_INIT ||
client->state == DHCP_STATE_SELECTING);
assert(IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_SELECTING));
r = client_message_init(client, &discover, DHCP_DISCOVER,
&optlen, &optoffset);
@ -1168,7 +1165,7 @@ static int client_start_delayed(sd_dhcp_client *client) {
}
client->fd = r;
if (client->state == DHCP_STATE_INIT || client->state == DHCP_STATE_INIT_REBOOT)
if (IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_INIT_REBOOT))
client->start_time = now(clock_boottime_or_monotonic());
return client_initialize_events(client, client_receive_message_raw);
@ -1694,7 +1691,7 @@ static int client_receive_message_udp(
len = recv(fd, message, buflen, 0);
if (len < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
return log_dhcp_client_errno(client, errno,
@ -1788,7 +1785,7 @@ static int client_receive_message_raw(
len = recvmsg(fd, &msg, 0);
if (len < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
return log_dhcp_client_errno(client, errno,

View file

@ -473,7 +473,7 @@ static int lease_parse_routes(
struct sd_dhcp_route *route = *routes + *routes_size;
int r;
r = in_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen);
r = in4_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen);
if (r < 0) {
log_debug("Failed to determine destination prefix length from class based IP, ignoring");
continue;
@ -1255,7 +1255,7 @@ int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) {
address.s_addr = lease->address;
/* fall back to the default subnet masks based on address class */
r = in_addr_default_subnet_mask(&address, &mask);
r = in4_addr_default_subnet_mask(&address, &mask);
if (r < 0)
return r;

View file

@ -944,7 +944,7 @@ static int client_receive_message(
len = recv(fd, message, buflen, 0);
if (len < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
return log_dhcp6_client_errno(client, errno, "Could not receive message from UDP socket: %m");

View file

@ -356,7 +356,7 @@ static int ipv4acd_on_packet(
n = recv(fd, &packet, sizeof(struct ether_arp), 0);
if (n < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
log_ipv4acd_errno(acd, errno, "Failed to read ARP packet: %m");

View file

@ -220,7 +220,7 @@ static int lldp_receive_datagram(sd_event_source *s, int fd, uint32_t revents, v
length = recv(fd, LLDP_NEIGHBOR_RAW(n), n->raw_size, MSG_DONTWAIT);
if (length < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
return log_lldp_errno(errno, "Failed to read LLDP datagram: %m");

View file

@ -1601,7 +1601,7 @@ _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
int r;
assert_return(s, -EINVAL);
assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
assert_return(IN_SET(m, SD_EVENT_OFF, SD_EVENT_ON, SD_EVENT_ONESHOT), -EINVAL);
assert_return(!event_pid_changed(s->event), -ECHILD);
/* If we are dead anyway, we are fine with turning off
@ -2054,7 +2054,7 @@ static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
ss = read(fd, &x, sizeof(x));
if (ss < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
return -errno;
@ -2143,10 +2143,7 @@ static int process_child(sd_event *e) {
return -errno;
if (s->child.siginfo.si_pid != 0) {
bool zombie =
s->child.siginfo.si_code == CLD_EXITED ||
s->child.siginfo.si_code == CLD_KILLED ||
s->child.siginfo.si_code == CLD_DUMPED;
bool zombie = IN_SET(s->child.siginfo.si_code, CLD_EXITED, CLD_KILLED, CLD_DUMPED);
if (!zombie && (s->child.options & WEXITED)) {
/* If the child isn't dead then let's
@ -2197,7 +2194,7 @@ static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
n = read(d->fd, &si, sizeof(si));
if (n < 0) {
if (errno == EAGAIN || errno == EINTR)
if (IN_SET(errno, EAGAIN, EINTR))
return read_one;
return -errno;
@ -2239,7 +2236,7 @@ static int source_dispatch(sd_event_source *s) {
* the event. */
saved_type = s->type;
if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
if (!IN_SET(s->type, SOURCE_DEFER, SOURCE_EXIT)) {
r = source_set_pending(s, false);
if (r < 0)
return r;
@ -2291,9 +2288,7 @@ static int source_dispatch(sd_event_source *s) {
case SOURCE_CHILD: {
bool zombie;
zombie = s->child.siginfo.si_code == CLD_EXITED ||
s->child.siginfo.si_code == CLD_KILLED ||
s->child.siginfo.si_code == CLD_DUMPED;
zombie = IN_SET(s->child.siginfo.si_code, CLD_EXITED, CLD_KILLED, CLD_DUMPED);
r = s->child.callback(s, &s->child.siginfo, s->userdata);

View file

@ -78,7 +78,7 @@ bool id128_is_valid(const char *s) {
for (i = 0; i < l; i++) {
char c = s[i];
if ((i == 8 || i == 13 || i == 18 || i == 23)) {
if (IN_SET(i, 8, 13, 18, 23)) {
if (c != '-')
return false;
} else {

View file

@ -70,7 +70,7 @@ _public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
if (i == 8)
is_guid = true;
else if (i == 13 || i == 18 || i == 23) {
else if (IN_SET(i, 13, 18, 23)) {
if (!is_guid)
return -EINVAL;
} else

View file

@ -19,12 +19,14 @@
#include "nm-sd-adapt.h"
#if defined(HAVE_LIBIDN2)
#if 0 /* NM_IGNORED */
#if HAVE_LIBIDN2
# include <idn2.h>
#elif defined(HAVE_LIBIDN)
#elif HAVE_LIBIDN
# include <idna.h>
# include <stringprep.h>
#endif
#endif
#include <endian.h>
#include <netinet/in.h>
@ -78,7 +80,7 @@ int dns_label_unescape(const char **name, char *dest, size_t sz) {
/* Ending NUL */
return -EINVAL;
else if (*n == '\\' || *n == '.') {
else if (IN_SET(*n, '\\', '.')) {
/* Escaped backslash or dot */
if (d)
@ -167,7 +169,7 @@ int dns_label_unescape_suffix(const char *name, const char **label_terminal, cha
}
terminal = *label_terminal;
assert(*terminal == '.' || *terminal == 0);
assert(IN_SET(*terminal, 0, '.'));
/* Skip current terminal character (and accept domain names ending it ".") */
if (*terminal == 0)
@ -232,7 +234,7 @@ int dns_label_escape(const char *p, size_t l, char *dest, size_t sz) {
q = dest;
while (l > 0) {
if (*p == '.' || *p == '\\') {
if (IN_SET(*p, '.', '\\')) {
/* Dot or backslash */
@ -244,8 +246,7 @@ int dns_label_escape(const char *p, size_t l, char *dest, size_t sz) {
sz -= 2;
} else if (*p == '_' ||
*p == '-' ||
} else if (IN_SET(*p, '_', '-') ||
(*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z')) {
@ -306,7 +307,7 @@ int dns_label_escape_new(const char *p, size_t l, char **ret) {
return r;
}
#ifdef HAVE_LIBIDN
#if HAVE_LIBIDN
int dns_label_apply_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max) {
_cleanup_free_ uint32_t *input = NULL;
size_t input_size, l;
@ -1281,7 +1282,7 @@ int dns_name_common_suffix(const char *a, const char *b, const char **ret) {
int dns_name_apply_idna(const char *name, char **ret) {
/* Return negative on error, 0 if not implemented, positive on success. */
#if defined(HAVE_LIBIDN2)
#if HAVE_LIBIDN2
int r;
_cleanup_free_ char *t = NULL;
@ -1321,7 +1322,7 @@ int dns_name_apply_idna(const char *name, char **ret) {
if (IN_SET(r, IDN2_TOO_BIG_DOMAIN, IDN2_TOO_BIG_LABEL))
return -ENOSPC;
return -EINVAL;
#elif defined(HAVE_LIBIDN)
#elif HAVE_LIBIDN
_cleanup_free_ char *buf = NULL;
size_t n = 0, allocated = 0;
bool first = true;

View file

@ -51,10 +51,12 @@ static inline int dns_name_parent(const char **name) {
return dns_label_unescape(name, NULL, DNS_LABEL_MAX);
}
#if defined(HAVE_LIBIDN)
#if 0 /* NM_IGNORED */
#if HAVE_LIBIDN
int dns_label_apply_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max);
int dns_label_undo_idna(const char *encoded, size_t encoded_size, char *decoded, size_t decoded_max);
#endif
#endif /* NM_IGNORED */
int dns_name_concat(const char *a, const char *b, char **ret);