More header cleanups (#37381)

This commit is contained in:
Daan De Meyer
2025-05-09 17:22:33 +02:00
committed by GitHub
107 changed files with 436 additions and 318 deletions
+23 -1
View File
@@ -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);
+98
View File
@@ -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
View File
@@ -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. */
+1
View File
@@ -2,6 +2,7 @@
#pragma once
#include <endian.h>
#include <errno.h>
#include "macro.h"
+1
View File
@@ -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
View File
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <limits.h>
#include <stdio.h>
#include "assert-util.h"
-83
View File
@@ -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;
}
-36
View File
@@ -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;
}
+1
View File
@@ -2,6 +2,7 @@
#include <elf.h>
#include <link.h>
#include <stdlib.h>
#include <sys/auxv.h>
#include "build-path.h"
+2
View File
@@ -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"
+1
View File
@@ -2,6 +2,7 @@
#pragma once
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
+4 -1
View File
@@ -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;
-10
View File
@@ -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
View File
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <errno.h>
#include <stdbool.h>
#include "errno-list.h"
+1
View File
@@ -2,6 +2,7 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "alloc-util.h"
#include "chase.h"
+1
View File
@@ -4,6 +4,7 @@
#include <inttypes.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include "stdio-util.h"
+1
View File
@@ -5,6 +5,7 @@
#include <errno.h>
#include <stdbool.h>
#include "assert-util.h"
#include "macro.h"
#include "path-util.h"
+1
View File
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <unistd.h>
#include "assert-util.h"
#include "macro.h"
#include "string.h"
+1
View File
@@ -3,6 +3,7 @@
#include <errno.h>
#include <string.h>
#include "assert-util.h"
#include "errno-list.h"
#include "macro.h"
+7 -2
View File
@@ -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,
+1
View File
@@ -2,6 +2,7 @@
#pragma once
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
+1
View File
@@ -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"
+1
View File
@@ -2,6 +2,7 @@
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
+8
View File
@@ -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. */
+3
View File
@@ -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"
+3
View File
@@ -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"
+1
View File
@@ -3,6 +3,7 @@
#include <errno.h>
#include <stddef.h>
#include "assert-util.h"
#include "label.h"
#include "macro.h"
+1
View File
@@ -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>
+2
View File
@@ -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 */
-46
View File
@@ -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)))
+1
View File
@@ -2,6 +2,7 @@
#pragma once
#include <inttypes.h>
#include <limits.h>
#include <malloc.h>
#include <stdbool.h>
#include <string.h>
+1
View File
@@ -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',
+1
View File
@@ -6,6 +6,7 @@
#include <stdbool.h>
#include <string.h>
#include "assert-util.h"
#include "set.h"
#define NULSTR_FOREACH(i, l) \
+1
View File
@@ -3,6 +3,7 @@
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include "macro.h"
+3
View File
@@ -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"
+2
View File
@@ -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
View File
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <unistd.h>
#include "alloc-util.h"
+1
View File
@@ -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
View File
@@ -2,6 +2,8 @@
#pragma once
#include <errno.h>
#include "macro.h"
#include "memory-util.h"
+2
View File
@@ -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"
+1
View File
@@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include "alloc-util.h"
#include "macro.h"
+1
View File
@@ -9,6 +9,7 @@
#include "alloc-util.h"
#include "hashmap.h"
#include "memory-util.h"
#include "strv-fundamental.h"
typedef enum ExtractFlags ExtractFlags;
+1
View File
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <limits.h>
#include <syslog.h>
#include "sd-id128.h"
+2 -85
View File
@@ -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 = {
-20
View File
@@ -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
View File
@@ -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>
+1
View File
@@ -24,6 +24,7 @@
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "virt.h"
enum {