More header cleanups (#37381)
This commit is contained in:
+23
-1
@@ -4,7 +4,6 @@
|
||||
#include <alloca.h>
|
||||
#include <malloc.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
@@ -137,6 +136,29 @@ static inline void *memdup_suffix0_multiply(const void *p, size_t need, size_t s
|
||||
return memdup_suffix0(p, size * need);
|
||||
}
|
||||
|
||||
static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
|
||||
size_t m;
|
||||
|
||||
/* Round up allocation sizes a bit to some reasonable, likely larger value. This is supposed to be
|
||||
* used for cases which are likely called in an allocation loop of some form, i.e. that repetitively
|
||||
* grow stuff, for example strv_extend() and suchlike.
|
||||
*
|
||||
* Note the difference to GREEDY_REALLOC() here, as this helper operates on a single size value only,
|
||||
* and rounds up to next multiple of 2, needing no further counter.
|
||||
*
|
||||
* Note the benefits of direct ALIGN_POWER2() usage: type-safety for size_t, sane handling for very
|
||||
* small (i.e. <= 2) and safe handling for very large (i.e. > SSIZE_MAX) values. */
|
||||
|
||||
if (l <= 2)
|
||||
return 2; /* Never allocate less than 2 of something. */
|
||||
|
||||
m = ALIGN_POWER2(l);
|
||||
if (m == 0) /* overflow? */
|
||||
return l;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void* greedy_realloc(void **p, size_t need, size_t size);
|
||||
void* greedy_realloc0(void **p, size_t need, size_t size);
|
||||
void* greedy_realloc_append(void **p, size_t *n_p, const void *from, size_t n_from, size_t size);
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "ansi-color.h"
|
||||
#include "log.h"
|
||||
#include "process-util.h"
|
||||
#include "string-table.h"
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
|
||||
static volatile int cached_color_mode = _COLOR_MODE_INVALID;
|
||||
static volatile int cached_underline_enabled = -1;
|
||||
|
||||
bool underline_enabled(void) {
|
||||
|
||||
if (cached_underline_enabled < 0) {
|
||||
|
||||
/* The Linux console doesn't support underlining, turn it off, but only there. */
|
||||
|
||||
if (colors_enabled())
|
||||
cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux");
|
||||
else
|
||||
cached_underline_enabled = false;
|
||||
}
|
||||
|
||||
return cached_underline_enabled;
|
||||
}
|
||||
|
||||
void reset_ansi_feature_caches(void) {
|
||||
cached_color_mode = _COLOR_MODE_INVALID;
|
||||
cached_underline_enabled = -1;
|
||||
}
|
||||
|
||||
ColorMode parse_systemd_colors(void) {
|
||||
const char *e;
|
||||
|
||||
e = getenv("SYSTEMD_COLORS");
|
||||
if (!e)
|
||||
return _COLOR_MODE_INVALID;
|
||||
|
||||
ColorMode m = color_mode_from_string(e);
|
||||
if (m < 0)
|
||||
return log_debug_errno(m, "Failed to parse $SYSTEMD_COLORS value '%s', ignoring: %m", e);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static ColorMode get_color_mode_impl(void) {
|
||||
/* Returns the mode used to choose output colors. The possible modes are COLOR_OFF for no colors,
|
||||
* COLOR_16 for only the base 16 ANSI colors, COLOR_256 for more colors, and COLOR_24BIT for
|
||||
* unrestricted color output. */
|
||||
|
||||
/* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */
|
||||
ColorMode m = parse_systemd_colors();
|
||||
if (m >= 0)
|
||||
return m;
|
||||
|
||||
/* Next, check for the presence of $NO_COLOR; value is ignored. */
|
||||
if (getenv("NO_COLOR"))
|
||||
return COLOR_OFF;
|
||||
|
||||
/* If the above didn't work, we turn colors off unless we are on a TTY. And if we are on a TTY we
|
||||
* turn it off if $TERM is set to "dumb". There's one special tweak though: if we are PID 1 then we
|
||||
* do not check whether we are connected to a TTY, because we don't keep /dev/console open
|
||||
* continuously due to fear of SAK, and hence things are a bit weird. */
|
||||
if (getpid_cached() == 1 ? getenv_terminal_is_dumb() : terminal_is_dumb())
|
||||
return COLOR_OFF;
|
||||
|
||||
/* We failed to figure out any reason to *disable* colors. Let's see how many colors we shall use. */
|
||||
if (STRPTR_IN_SET(getenv("COLORTERM"),
|
||||
"truecolor",
|
||||
"24bit"))
|
||||
return COLOR_24BIT;
|
||||
|
||||
/* Note that the Linux console can only display 16 colors. We still enable 256 color mode
|
||||
* even for PID1 output though (which typically goes to the Linux console), since the Linux
|
||||
* console is able to parse the 256 color sequences and automatically map them to the closest
|
||||
* color in the 16 color palette (since kernel 3.16). Doing 256 colors is nice for people who
|
||||
* invoke systemd in a container or via a serial link or such, and use a true 256 color
|
||||
* terminal to do so. */
|
||||
return COLOR_256;
|
||||
}
|
||||
|
||||
ColorMode get_color_mode(void) {
|
||||
if (cached_color_mode < 0)
|
||||
cached_color_mode = get_color_mode_impl();
|
||||
|
||||
return cached_color_mode;
|
||||
}
|
||||
|
||||
|
||||
static const char* const color_mode_table[_COLOR_MODE_MAX] = {
|
||||
[COLOR_OFF] = "off",
|
||||
[COLOR_16] = "16",
|
||||
[COLOR_256] = "256",
|
||||
[COLOR_24BIT] = "24bit",
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_24BIT);
|
||||
+28
-1
@@ -1,7 +1,34 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include "terminal-util.h"
|
||||
#include <errno.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
/* Limits the use of ANSI colors to a subset. */
|
||||
typedef enum ColorMode {
|
||||
COLOR_OFF, /* No colors, monochrome output. */
|
||||
COLOR_16, /* Only the base 16 colors. */
|
||||
COLOR_256, /* Only 256 colors. */
|
||||
COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */
|
||||
_COLOR_MODE_MAX,
|
||||
_COLOR_MODE_INVALID = -EINVAL,
|
||||
} ColorMode;
|
||||
|
||||
const char* color_mode_to_string(ColorMode m) _const_;
|
||||
ColorMode color_mode_from_string(const char *s) _pure_;
|
||||
|
||||
ColorMode get_color_mode(void);
|
||||
static inline bool colors_enabled(void) {
|
||||
/* Returns true if colors are considered supported on our stdout. */
|
||||
return get_color_mode() != COLOR_OFF;
|
||||
}
|
||||
|
||||
ColorMode parse_systemd_colors(void);
|
||||
|
||||
bool underline_enabled(void);
|
||||
|
||||
void reset_ansi_feature_caches(void);
|
||||
|
||||
/* Regular colors */
|
||||
#define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <endian.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "arphrd-util.h"
|
||||
#include "assert-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
|
||||
@@ -1,20 +1,10 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/audit.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "audit-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "iovec-util.h"
|
||||
#include "macro.h"
|
||||
#include "parse-util.h"
|
||||
#include "process-util.h"
|
||||
#include "socket-util.h"
|
||||
#include "stat-util.h"
|
||||
#include "user-util.h"
|
||||
#include "virt.h"
|
||||
@@ -94,76 +84,3 @@ int audit_loginuid_from_pid(const PidRef *pid, uid_t *ret_uid) {
|
||||
|
||||
return parse_uid(s, ret_uid);
|
||||
}
|
||||
|
||||
static int try_audit_request(int fd) {
|
||||
struct iovec iov;
|
||||
struct msghdr mh;
|
||||
ssize_t n;
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
struct {
|
||||
struct nlmsghdr hdr;
|
||||
struct nlmsgerr err;
|
||||
} _packed_ msg = {
|
||||
.hdr.nlmsg_len = NLMSG_LENGTH(0),
|
||||
.hdr.nlmsg_type = AUDIT_GET_FEATURE,
|
||||
.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
|
||||
};
|
||||
iov = IOVEC_MAKE(&msg, msg.hdr.nlmsg_len);
|
||||
mh = (struct msghdr) {
|
||||
.msg_iov = &iov,
|
||||
.msg_iovlen = 1,
|
||||
};
|
||||
|
||||
if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
|
||||
return -errno;
|
||||
|
||||
iov.iov_len = sizeof(msg);
|
||||
|
||||
n = recvmsg_safe(fd, &mh, 0);
|
||||
if (n < 0)
|
||||
return n;
|
||||
if (n != NLMSG_LENGTH(sizeof(struct nlmsgerr)))
|
||||
return -EIO;
|
||||
|
||||
if (msg.hdr.nlmsg_type != NLMSG_ERROR)
|
||||
return -EINVAL;
|
||||
|
||||
return msg.err.error;
|
||||
}
|
||||
|
||||
bool use_audit(void) {
|
||||
static int cached_use = -1;
|
||||
int r;
|
||||
|
||||
if (cached_use >= 0)
|
||||
return cached_use;
|
||||
|
||||
_cleanup_close_ int fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT);
|
||||
if (fd < 0) {
|
||||
cached_use = !ERRNO_IS_PRIVILEGE(errno) && !ERRNO_IS_NOT_SUPPORTED(errno);
|
||||
if (cached_use)
|
||||
log_debug_errno(errno, "Unexpected error while creating audit socket, proceeding with its use: %m");
|
||||
else
|
||||
log_debug_errno(errno, "Won't talk to audit, because feature or privilege absent: %m");
|
||||
} else {
|
||||
/* If we try and use the audit fd but get -ECONNREFUSED, it is because we are not in the
|
||||
* initial user namespace, and the kernel does not have support for audit outside of the
|
||||
* initial user namespace (see
|
||||
* https://elixir.bootlin.com/linux/latest/C/ident/audit_netlink_ok).
|
||||
*
|
||||
* If we receive any other error, do not disable audit because we are not sure that the error
|
||||
* indicates that audit will not work in general. */
|
||||
r = try_audit_request(fd);
|
||||
if (r < 0) {
|
||||
cached_use = r != -ECONNREFUSED;
|
||||
log_debug_errno(r, cached_use ?
|
||||
"Failed to make request on audit fd, ignoring: %m" :
|
||||
"Won't talk to audit: %m");
|
||||
} else
|
||||
cached_use = true;
|
||||
}
|
||||
|
||||
return cached_use;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#if HAVE_AUDIT
|
||||
# include <libaudit.h>
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "errno-util.h"
|
||||
#include "log.h"
|
||||
#include "pidref.h"
|
||||
|
||||
#define AUDIT_SESSION_INVALID UINT32_MAX
|
||||
@@ -18,35 +11,6 @@
|
||||
int audit_session_from_pid(const PidRef *pid, uint32_t *ret_id);
|
||||
int audit_loginuid_from_pid(const PidRef *pid, uid_t *ret_uid);
|
||||
|
||||
bool use_audit(void);
|
||||
|
||||
static inline bool audit_session_is_valid(uint32_t id) {
|
||||
return id > 0 && id != AUDIT_SESSION_INVALID;
|
||||
}
|
||||
|
||||
/* The wrappers for audit_open() and audit_close() are inline functions so that we don't get a spurious
|
||||
* linkage to libaudit in libbasic, but we also don't need to create a separate source file for two very
|
||||
* short functions. */
|
||||
|
||||
static inline int close_audit_fd(int fd) {
|
||||
#if HAVE_AUDIT
|
||||
if (fd >= 0)
|
||||
audit_close(fd);
|
||||
#else
|
||||
assert(fd < 0);
|
||||
#endif
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static inline int open_audit_fd_or_warn(void) {
|
||||
int fd = -EBADF;
|
||||
|
||||
#if HAVE_AUDIT
|
||||
/* If the kernel lacks netlink or audit support, don't worry about it. */
|
||||
fd = audit_open();
|
||||
if (fd < 0)
|
||||
return log_full_errno(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING,
|
||||
errno, "Failed to connect to audit log, ignoring: %m");
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <elf.h>
|
||||
#include <link.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/auxv.h>
|
||||
|
||||
#include "build-path.h"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "capsule-util.h"
|
||||
#include "path-util.h"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#if HAVE_LZ4
|
||||
#include <lz4.h>
|
||||
#include <lz4hc.h>
|
||||
#include <lz4frame.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_XZ
|
||||
@@ -47,8 +49,9 @@ static DLSYM_PROTOTYPE(LZ4F_decompress) = NULL;
|
||||
static DLSYM_PROTOTYPE(LZ4F_freeCompressionContext) = NULL;
|
||||
static DLSYM_PROTOTYPE(LZ4F_freeDecompressionContext) = NULL;
|
||||
static DLSYM_PROTOTYPE(LZ4F_isError) = NULL;
|
||||
static DLSYM_PROTOTYPE(LZ4_compress_HC) = NULL;
|
||||
/* These are used in test-compress.c so we don't make them static. */
|
||||
DLSYM_PROTOTYPE(LZ4_compress_default) = NULL;
|
||||
DLSYM_PROTOTYPE(LZ4_compress_HC) = NULL;
|
||||
DLSYM_PROTOTYPE(LZ4_decompress_safe) = NULL;
|
||||
DLSYM_PROTOTYPE(LZ4_decompress_safe_partial) = NULL;
|
||||
DLSYM_PROTOTYPE(LZ4_versionNumber) = NULL;
|
||||
|
||||
@@ -6,11 +6,6 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if HAVE_LZ4
|
||||
#include <lz4.h>
|
||||
#include <lz4frame.h>
|
||||
#endif
|
||||
|
||||
#include "dlfcn-util.h"
|
||||
|
||||
typedef enum Compression {
|
||||
@@ -73,11 +68,6 @@ int decompress_stream_lz4(int fdf, int fdt, uint64_t max_size);
|
||||
int decompress_stream_zstd(int fdf, int fdt, uint64_t max_size);
|
||||
|
||||
#if HAVE_LZ4
|
||||
extern DLSYM_PROTOTYPE(LZ4_compress_default);
|
||||
extern DLSYM_PROTOTYPE(LZ4_decompress_safe);
|
||||
extern DLSYM_PROTOTYPE(LZ4_decompress_safe_partial);
|
||||
extern DLSYM_PROTOTYPE(LZ4_versionNumber);
|
||||
|
||||
int dlopen_lz4(void);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "errno-list.h"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "chase.h"
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysmacros.h>
|
||||
|
||||
#include "stdio-util.h"
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "macro.h"
|
||||
#include "path-util.h"
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "macro.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "errno-list.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -42,6 +43,12 @@ static inline void _reset_errno_(int *saved_errno) {
|
||||
PROTECT_ERRNO; \
|
||||
errno = ABS(value)
|
||||
|
||||
#define return_with_errno(r, err) \
|
||||
do { \
|
||||
errno = ABS(err); \
|
||||
return r; \
|
||||
} while (false)
|
||||
|
||||
static inline int negative_errno(void) {
|
||||
/* This helper should be used to shut up gcc if you know 'errno' is
|
||||
* negative. Instead of "return -errno;", use "return negative_errno();"
|
||||
@@ -103,8 +110,6 @@ static inline int errno_or_else(int fallback) {
|
||||
return ERRNO_IS_NEG_##name(-ABS(r)); \
|
||||
}
|
||||
|
||||
assert_cc(INT_MAX <= INTMAX_MAX);
|
||||
|
||||
/* For send()/recv() or read()/write(). */
|
||||
static inline bool ERRNO_IS_NEG_TRANSIENT(intmax_t r) {
|
||||
return IN_SET(r,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/sysmacros.h>
|
||||
|
||||
#include "hash-funcs.h"
|
||||
#include "path-util.h"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
/* sys/param.h from glibc unconditionally overrides the MIN() and MAX() macros which interferes with our own
|
||||
* MAX() macro. It also includes a bunch of other headers transitively so we don't want to include
|
||||
* sys/param.h in macro-fundamental.h unconditionally. We'd like to make including this file an error but
|
||||
* unfortunately <resolv.h> includes it. However, <resolv.h> doesn't actually make use of anything from
|
||||
* sys/param.h, so we override it with an empty file so it can't mess with our macros. */
|
||||
@@ -1,5 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "ioprio-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "string-table.h"
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "iovec-util.h"
|
||||
#include "iovec-wrapper.h"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "label.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <libintl.h>
|
||||
#include <locale.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
/* Some structures we reference but don't want to pull in headers for */
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "constants.h"
|
||||
#include "macro-fundamental.h"
|
||||
|
||||
@@ -33,15 +25,6 @@
|
||||
/* test harness */
|
||||
#define EXIT_TEST_SKIP 77
|
||||
|
||||
/* builtins */
|
||||
#if __SIZEOF_INT__ == 4
|
||||
#define BUILTIN_FFS_U32(x) __builtin_ffs(x);
|
||||
#elif __SIZEOF_LONG__ == 4
|
||||
#define BUILTIN_FFS_U32(x) __builtin_ffsl(x);
|
||||
#else
|
||||
#error "neither int nor long are four bytes long?!?"
|
||||
#endif
|
||||
|
||||
static inline uint64_t u64_multiply_safe(uint64_t a, uint64_t b) {
|
||||
if (_unlikely_(a != 0 && b > (UINT64_MAX / a)))
|
||||
return 0; /* overflow */
|
||||
@@ -67,29 +50,6 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
|
||||
return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL));
|
||||
}
|
||||
|
||||
static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
|
||||
size_t m;
|
||||
|
||||
/* Round up allocation sizes a bit to some reasonable, likely larger value. This is supposed to be
|
||||
* used for cases which are likely called in an allocation loop of some form, i.e. that repetitively
|
||||
* grow stuff, for example strv_extend() and suchlike.
|
||||
*
|
||||
* Note the difference to GREEDY_REALLOC() here, as this helper operates on a single size value only,
|
||||
* and rounds up to next multiple of 2, needing no further counter.
|
||||
*
|
||||
* Note the benefits of direct ALIGN_POWER2() usage: type-safety for size_t, sane handling for very
|
||||
* small (i.e. <= 2) and safe handling for very large (i.e. > SSIZE_MAX) values. */
|
||||
|
||||
if (l <= 2)
|
||||
return 2; /* Never allocate less than 2 of something. */
|
||||
|
||||
m = ALIGN_POWER2(l);
|
||||
if (m == 0) /* overflow? */
|
||||
return l;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/*
|
||||
* container_of - cast a member of a structure out to the containing structure
|
||||
* @ptr: the pointer to the member.
|
||||
@@ -103,12 +63,6 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
|
||||
(type*)( (char *)UNIQ_T(A, uniq) - offsetof(type, member) ); \
|
||||
})
|
||||
|
||||
#define return_with_errno(r, err) \
|
||||
do { \
|
||||
errno = ABS(err); \
|
||||
return r; \
|
||||
} while (false)
|
||||
|
||||
#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
|
||||
#define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
|
||||
#define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p)))
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <malloc.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -4,6 +4,7 @@ basic_sources = files(
|
||||
'MurmurHash2.c',
|
||||
'af-list.c',
|
||||
'alloc-util.c',
|
||||
'ansi-color.c',
|
||||
'architecture.c',
|
||||
'argv-util.c',
|
||||
'arphrd-util.c',
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "set.h"
|
||||
|
||||
#define NULSTR_FOREACH(i, l) \
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "macro.h"
|
||||
#include "ratelimit.h"
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
/* This is the same as glibc's internal __compar_d_fn_t type. glibc exports a public comparison_fn_t, for the
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "parse-util.h"
|
||||
#include "string-table.h"
|
||||
#include "string-util.h"
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "memory-util.h"
|
||||
#include "strv-fundamental.h"
|
||||
|
||||
typedef enum ExtractFlags ExtractFlags;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include "sd-id128.h"
|
||||
|
||||
@@ -60,8 +60,6 @@ static volatile unsigned cached_lines = 0;
|
||||
|
||||
static volatile int cached_on_tty = -1;
|
||||
static volatile int cached_on_dev_null = -1;
|
||||
static volatile int cached_color_mode = _COLOR_MODE_INVALID;
|
||||
static volatile int cached_underline_enabled = -1;
|
||||
|
||||
bool isatty_safe(int fd) {
|
||||
assert(fd >= 0);
|
||||
@@ -1433,10 +1431,10 @@ void reset_terminal_feature_caches(void) {
|
||||
cached_columns = 0;
|
||||
cached_lines = 0;
|
||||
|
||||
cached_color_mode = _COLOR_MODE_INVALID;
|
||||
cached_underline_enabled = -1;
|
||||
cached_on_tty = -1;
|
||||
cached_on_dev_null = -1;
|
||||
|
||||
reset_ansi_feature_caches();
|
||||
}
|
||||
|
||||
bool on_tty(void) {
|
||||
@@ -1753,72 +1751,6 @@ bool terminal_is_dumb(void) {
|
||||
return getenv_terminal_is_dumb();
|
||||
}
|
||||
|
||||
static const char* const color_mode_table[_COLOR_MODE_MAX] = {
|
||||
[COLOR_OFF] = "off",
|
||||
[COLOR_16] = "16",
|
||||
[COLOR_256] = "256",
|
||||
[COLOR_24BIT] = "24bit",
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_24BIT);
|
||||
|
||||
static ColorMode parse_systemd_colors(void) {
|
||||
const char *e;
|
||||
|
||||
e = getenv("SYSTEMD_COLORS");
|
||||
if (!e)
|
||||
return _COLOR_MODE_INVALID;
|
||||
|
||||
ColorMode m = color_mode_from_string(e);
|
||||
if (m < 0)
|
||||
return log_debug_errno(m, "Failed to parse $SYSTEMD_COLORS value '%s', ignoring: %m", e);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static ColorMode get_color_mode_impl(void) {
|
||||
/* Returns the mode used to choose output colors. The possible modes are COLOR_OFF for no colors,
|
||||
* COLOR_16 for only the base 16 ANSI colors, COLOR_256 for more colors, and COLOR_24BIT for
|
||||
* unrestricted color output. */
|
||||
|
||||
/* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */
|
||||
ColorMode m = parse_systemd_colors();
|
||||
if (m >= 0)
|
||||
return m;
|
||||
|
||||
/* Next, check for the presence of $NO_COLOR; value is ignored. */
|
||||
if (getenv("NO_COLOR"))
|
||||
return COLOR_OFF;
|
||||
|
||||
/* If the above didn't work, we turn colors off unless we are on a TTY. And if we are on a TTY we
|
||||
* turn it off if $TERM is set to "dumb". There's one special tweak though: if we are PID 1 then we
|
||||
* do not check whether we are connected to a TTY, because we don't keep /dev/console open
|
||||
* continuously due to fear of SAK, and hence things are a bit weird. */
|
||||
if (getpid_cached() == 1 ? getenv_terminal_is_dumb() : terminal_is_dumb())
|
||||
return COLOR_OFF;
|
||||
|
||||
/* We failed to figure out any reason to *disable* colors. Let's see how many colors we shall use. */
|
||||
if (STRPTR_IN_SET(getenv("COLORTERM"),
|
||||
"truecolor",
|
||||
"24bit"))
|
||||
return COLOR_24BIT;
|
||||
|
||||
/* Note that the Linux console can only display 16 colors. We still enable 256 color mode
|
||||
* even for PID1 output though (which typically goes to the Linux console), since the Linux
|
||||
* console is able to parse the 256 color sequences and automatically map them to the closest
|
||||
* color in the 16 color palette (since kernel 3.16). Doing 256 colors is nice for people who
|
||||
* invoke systemd in a container or via a serial link or such, and use a true 256 color
|
||||
* terminal to do so. */
|
||||
return COLOR_256;
|
||||
}
|
||||
|
||||
ColorMode get_color_mode(void) {
|
||||
if (cached_color_mode < 0)
|
||||
cached_color_mode = get_color_mode_impl();
|
||||
|
||||
return cached_color_mode;
|
||||
}
|
||||
|
||||
bool dev_console_colors_enabled(void) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
ColorMode m;
|
||||
@@ -1843,21 +1775,6 @@ bool dev_console_colors_enabled(void) {
|
||||
return !streq_ptr(s, "dumb");
|
||||
}
|
||||
|
||||
bool underline_enabled(void) {
|
||||
|
||||
if (cached_underline_enabled < 0) {
|
||||
|
||||
/* The Linux console doesn't support underlining, turn it off, but only there. */
|
||||
|
||||
if (colors_enabled())
|
||||
cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux");
|
||||
else
|
||||
cached_underline_enabled = false;
|
||||
}
|
||||
|
||||
return cached_underline_enabled;
|
||||
}
|
||||
|
||||
int vt_restore(int fd) {
|
||||
|
||||
static const struct vt_mode mode = {
|
||||
|
||||
@@ -129,26 +129,6 @@ bool on_tty(void);
|
||||
bool getenv_terminal_is_dumb(void);
|
||||
bool terminal_is_dumb(void);
|
||||
|
||||
/* Limits the use of ANSI colors to a subset. */
|
||||
typedef enum ColorMode {
|
||||
COLOR_OFF, /* No colors, monochrome output. */
|
||||
COLOR_16, /* Only the base 16 colors. */
|
||||
COLOR_256, /* Only 256 colors. */
|
||||
COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */
|
||||
_COLOR_MODE_MAX,
|
||||
_COLOR_MODE_INVALID = -EINVAL,
|
||||
} ColorMode;
|
||||
|
||||
const char* color_mode_to_string(ColorMode m) _const_;
|
||||
ColorMode color_mode_from_string(const char *s) _pure_;
|
||||
|
||||
ColorMode get_color_mode(void);
|
||||
static inline bool colors_enabled(void) {
|
||||
/* Returns true if colors are considered supported on our stdout. */
|
||||
return get_color_mode() != COLOR_OFF;
|
||||
}
|
||||
|
||||
bool underline_enabled(void);
|
||||
bool dev_console_colors_enabled(void);
|
||||
|
||||
int get_ctty_devnr(pid_t pid, dev_t *ret);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "stat-util.h"
|
||||
#include "string-table.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "virt.h"
|
||||
|
||||
enum {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "bcd.h"
|
||||
#include "bootspec-fundamental.h"
|
||||
#include "console.h"
|
||||
@@ -27,6 +29,7 @@
|
||||
#include "secure-boot.h"
|
||||
#include "shim.h"
|
||||
#include "smbios.h"
|
||||
#include "strv-fundamental.h"
|
||||
#include "ticks.h"
|
||||
#include "tpm2-pcr.h"
|
||||
#include "uki.h"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "efi-string.h"
|
||||
|
||||
#if SD_BOOT
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "chid.h"
|
||||
#include "devicetree.h"
|
||||
#include "efi-firmware.h"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "cpio.h"
|
||||
#include "device-path-util.h"
|
||||
#include "devicetree.h"
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
#if HAVE_AUDIT
|
||||
# include <stdbool.h>
|
||||
|
||||
# include "audit-util.h"
|
||||
# include "libaudit-util.h"
|
||||
# include "capability-util.h"
|
||||
|
||||
static bool initialized = false;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "conf-files.h"
|
||||
#include "copy.h"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "unit-dependency-atom.h"
|
||||
|
||||
static const UnitDependencyAtom atom_map[_UNIT_DEPENDENCY_MAX] = {
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdalign.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* This header unconditionally defines MAX() so include it here already so
|
||||
* it won't override our own definition of MAX() that we define later in this
|
||||
* file. */
|
||||
#if !SD_BOOT
|
||||
#include <sys/param.h>
|
||||
#endif
|
||||
|
||||
/* Temporarily disable some warnings */
|
||||
#define DISABLE_WARNING_DEPRECATED_DECLARATIONS \
|
||||
_Pragma("GCC diagnostic push"); \
|
||||
|
||||
@@ -120,12 +120,6 @@ static inline void *memory_startswith(const void *p, size_t sz, const sd_char *t
|
||||
return (uint8_t*) p + n;
|
||||
}
|
||||
|
||||
#define _STRV_FOREACH(s, l, i) \
|
||||
for (typeof(*(l)) *s, *i = (l); (s = i) && *i; i++)
|
||||
|
||||
#define STRV_FOREACH(s, l) \
|
||||
_STRV_FOREACH(s, l, UNIQ_T(i, UNIQ))
|
||||
|
||||
static inline bool ascii_isdigit(sd_char a) {
|
||||
/* A pure ASCII, locale independent version of isdigit() */
|
||||
return a >= '0' && a <= '9';
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include "macro-fundamental.h"
|
||||
|
||||
#define _STRV_FOREACH(s, l, i) \
|
||||
for (typeof(*(l)) *s, *i = (l); (s = i) && *i; i++)
|
||||
|
||||
#define STRV_FOREACH(s, l) \
|
||||
_STRV_FOREACH(s, l, UNIQ_T(i, UNIQ))
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "dns-domain.h"
|
||||
#include "fd-util.h"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#if HAVE_BZIP2
|
||||
#include <bzlib.h>
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <zlib.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "assert-util.h"
|
||||
#include "btrfs-util.h"
|
||||
#include "qcow2-util.h"
|
||||
#include "sparse-endian.h"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "sd-bus.h"
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "bus-signature.h"
|
||||
#include "bus-type.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "sd-id128.h"
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
@@ -47,13 +47,10 @@ on 1 byte), but shoehorning those bytes into integers efficiently is messy.
|
||||
*/
|
||||
/* #define SELF_TEST 1 */
|
||||
|
||||
#include <endian.h> /* attempt to define endianness */
|
||||
#include <stdint.h> /* defines uint32_t etc */
|
||||
#include <stdio.h> /* defines printf for tests */
|
||||
#include <sys/param.h> /* attempt to define endianness */
|
||||
#include <time.h> /* defines time_t for timings in the test */
|
||||
#ifdef linux
|
||||
# include <endian.h> /* attempt to define endianness */
|
||||
#endif
|
||||
|
||||
_Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <sys/sysmacros.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "devnum-util.h"
|
||||
#include "env-util.h"
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "device-private.h"
|
||||
#include "device-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "libudev-device-internal.h"
|
||||
#include "libudev-list-internal.h"
|
||||
#include "parse-util.h"
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "device-enumerator-private.h"
|
||||
#include "device-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "libudev-device-internal.h"
|
||||
#include "libudev-list-internal.h"
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "sd-hwdb.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "assert-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "hwdb-util.h"
|
||||
#include "libudev-list-internal.h"
|
||||
#include "log.h"
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "libudev.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "io-util.h"
|
||||
#include "udev-util.h"
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "libudev.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "log.h"
|
||||
#include "string-util.h"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "in-addr-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "ansi-color.h"
|
||||
#include "journal-internal.h"
|
||||
#include "log.h"
|
||||
#include "logs-show.h"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* Copyright © 2019 VMware, Inc. */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "assert-util.h"
|
||||
#include "extract-word.h"
|
||||
#include "fileio.h"
|
||||
#include "parse-util.h"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "sd-device.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
/* DNS record types, taken from
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "dlfcn-util.h"
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#if HAVE_BLKID
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "blkid-util.h"
|
||||
#include "string-util.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/if_bonding.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "bus-locator.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "color-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "alloc-util.h"
|
||||
#include "apparmor-util.h"
|
||||
#include "architecture.h"
|
||||
#include "audit-util.h"
|
||||
#include "battery-util.h"
|
||||
#include "bitfield.h"
|
||||
#include "blockdev-util.h"
|
||||
@@ -44,6 +43,7 @@
|
||||
#include "id128-util.h"
|
||||
#include "ima-util.h"
|
||||
#include "initrd-util.h"
|
||||
#include "libaudit-util.h"
|
||||
#include "limits-util.h"
|
||||
#include "list.h"
|
||||
#include "macro.h"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <sys/file.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/xattr.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
typedef enum CoredumpFilter {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
# include <stringprep.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#if HAVE_LIBIDN2 || HAVE_LIBIDN
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <limits.h>
|
||||
|
||||
#if defined(__FreeBSD_kernel__)
|
||||
# define INIT_FIFO "/etc/.initctl"
|
||||
@@ -25,13 +25,6 @@
|
||||
#define INIT_CMD_UNSETENV 7
|
||||
|
||||
#define INIT_CMD_CHANGECONS 12345
|
||||
|
||||
#ifdef MAXHOSTNAMELEN
|
||||
# define INITRQ_HLEN MAXHOSTNAMELEN
|
||||
#else
|
||||
# define INITRQ_HLEN 64
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This is what BSD 4.4 uses when talking to init.
|
||||
* Linux doesn't use this right now.
|
||||
@@ -39,7 +32,7 @@
|
||||
struct init_request_bsd {
|
||||
char gen_id[8]; /* Beats me.. telnetd uses "fe" */
|
||||
char tty_id[16]; /* Tty name minus /dev/tty */
|
||||
char host[INITRQ_HLEN]; /* Hostname */
|
||||
char host[HOST_NAME_MAX]; /* Hostname */
|
||||
char term_type[16]; /* Terminal type */
|
||||
int signal; /* Signal to send */
|
||||
int pid; /* Process to send to */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/if_link.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "dlfcn-util.h"
|
||||
|
||||
#if HAVE_LIBARCHIVE
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/audit.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#if HAVE_AUDIT
|
||||
# include <libaudit.h>
|
||||
#endif
|
||||
|
||||
#include "fd-util.h"
|
||||
#include "iovec-util.h"
|
||||
#include "libaudit-util.h"
|
||||
#include "log.h"
|
||||
#include "socket-util.h"
|
||||
|
||||
static int try_audit_request(int fd) {
|
||||
struct iovec iov;
|
||||
struct msghdr mh;
|
||||
ssize_t n;
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
struct {
|
||||
struct nlmsghdr hdr;
|
||||
struct nlmsgerr err;
|
||||
} _packed_ msg = {
|
||||
.hdr.nlmsg_len = NLMSG_LENGTH(0),
|
||||
.hdr.nlmsg_type = AUDIT_GET_FEATURE,
|
||||
.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
|
||||
};
|
||||
iov = IOVEC_MAKE(&msg, msg.hdr.nlmsg_len);
|
||||
mh = (struct msghdr) {
|
||||
.msg_iov = &iov,
|
||||
.msg_iovlen = 1,
|
||||
};
|
||||
|
||||
if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
|
||||
return -errno;
|
||||
|
||||
iov.iov_len = sizeof(msg);
|
||||
|
||||
n = recvmsg_safe(fd, &mh, 0);
|
||||
if (n < 0)
|
||||
return n;
|
||||
if (n != NLMSG_LENGTH(sizeof(struct nlmsgerr)))
|
||||
return -EIO;
|
||||
|
||||
if (msg.hdr.nlmsg_type != NLMSG_ERROR)
|
||||
return -EINVAL;
|
||||
|
||||
return msg.err.error;
|
||||
}
|
||||
|
||||
bool use_audit(void) {
|
||||
static int cached_use = -1;
|
||||
int r;
|
||||
|
||||
if (cached_use >= 0)
|
||||
return cached_use;
|
||||
|
||||
_cleanup_close_ int fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT);
|
||||
if (fd < 0) {
|
||||
cached_use = !ERRNO_IS_PRIVILEGE(errno) && !ERRNO_IS_NOT_SUPPORTED(errno);
|
||||
if (cached_use)
|
||||
log_debug_errno(errno, "Unexpected error while creating audit socket, proceeding with its use: %m");
|
||||
else
|
||||
log_debug_errno(errno, "Won't talk to audit, because feature or privilege absent: %m");
|
||||
} else {
|
||||
/* If we try and use the audit fd but get -ECONNREFUSED, it is because we are not in the
|
||||
* initial user namespace, and the kernel does not have support for audit outside of the
|
||||
* initial user namespace (see
|
||||
* https://elixir.bootlin.com/linux/latest/C/ident/audit_netlink_ok).
|
||||
*
|
||||
* If we receive any other error, do not disable audit because we are not sure that the error
|
||||
* indicates that audit will not work in general. */
|
||||
r = try_audit_request(fd);
|
||||
if (r < 0) {
|
||||
cached_use = r != -ECONNREFUSED;
|
||||
log_debug_errno(r, cached_use ?
|
||||
"Failed to make request on audit fd, ignoring: %m" :
|
||||
"Won't talk to audit: %m");
|
||||
} else
|
||||
cached_use = true;
|
||||
}
|
||||
|
||||
return cached_use;
|
||||
}
|
||||
|
||||
int close_audit_fd(int fd) {
|
||||
#if HAVE_AUDIT
|
||||
if (fd >= 0)
|
||||
audit_close(fd);
|
||||
#else
|
||||
assert(fd < 0);
|
||||
#endif
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
int open_audit_fd_or_warn(void) {
|
||||
int fd = -EBADF;
|
||||
|
||||
#if HAVE_AUDIT
|
||||
/* If the kernel lacks netlink or audit support, don't worry about it. */
|
||||
fd = audit_open();
|
||||
if (fd < 0)
|
||||
return log_full_errno(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING,
|
||||
errno, "Failed to connect to audit log, ignoring: %m");
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool use_audit(void);
|
||||
|
||||
int close_audit_fd(int fd);
|
||||
int open_audit_fd_or_warn(void);
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "iovec-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -106,6 +106,7 @@ shared_sources = files(
|
||||
'killall.c',
|
||||
'label-util.c',
|
||||
'libarchive-util.c',
|
||||
'libaudit-util.c',
|
||||
'libcrypt-util.c',
|
||||
'libfido2-util.c',
|
||||
'libmount-util.c',
|
||||
@@ -332,6 +333,7 @@ libshared_name = 'systemd-shared-@0@'.format(shared_lib_tag)
|
||||
|
||||
libshared_deps = [threads,
|
||||
libacl,
|
||||
libaudit,
|
||||
libblkid,
|
||||
libcap,
|
||||
libcrypt,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "memory-util.h"
|
||||
#include "random-util.h"
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "assert-util.h"
|
||||
#include "label-util.h"
|
||||
#include "macro.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "assert-util.h"
|
||||
#include "fileio.h"
|
||||
#include "macro.h"
|
||||
#include "smbios11.h"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "path-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "sysupdate-instance.h"
|
||||
#include "sysupdate-pattern.h"
|
||||
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
|
||||
#include <getopt.h>
|
||||
|
||||
#if HAVE_AUDIT
|
||||
# include <libaudit.h>
|
||||
#endif
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "audit-util.h"
|
||||
#include "build.h"
|
||||
#include "chase.h"
|
||||
#include "conf-files.h"
|
||||
@@ -18,6 +21,7 @@
|
||||
#include "fs-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "image-policy.h"
|
||||
#include "libaudit-util.h"
|
||||
#include "libcrypt-util.h"
|
||||
#include "main-func.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user