From f8cc16fd53151a7cb98ce997d490ccf510ed1562 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:18:50 +0200 Subject: [PATCH 1/9] signal-util: don't introduce symbols with double underscores ANSI C reserves identifiers beginning with an underscore for compiler internal stuff. We already invade that namespace plenty and probably should not. But even going for the doubly underscore prefixed namespace is a bit too much. Let's just rename the offending table as "static_signal_table[]", since it lists the static defined signals rather than the "dynamic" RTSIGMIN/RTSIGMAX signals. --- src/basic/signal-util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c index 34b2b27918..f945a96f0d 100644 --- a/src/basic/signal-util.c +++ b/src/basic/signal-util.c @@ -119,7 +119,7 @@ int sigprocmask_many(int how, sigset_t *old, ...) { return 0; } -static const char *const __signal_table[] = { +static const char *const static_signal_table[] = { [SIGHUP] = "HUP", [SIGINT] = "INT", [SIGQUIT] = "QUIT", @@ -155,13 +155,13 @@ static const char *const __signal_table[] = { [SIGSYS] = "SYS" }; -DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int); +DEFINE_PRIVATE_STRING_TABLE_LOOKUP(static_signal, int); const char *signal_to_string(int signo) { static thread_local char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int)]; const char *name; - name = __signal_to_string(signo); + name = static_signal_to_string(signo); if (name) return name; @@ -190,7 +190,7 @@ int signal_from_string(const char *s) { s += 3; /* Check that the input is a signal name. */ - signo = __signal_from_string(s); + signo = static_signal_from_string(s); if (signo > 0) return signo; From f0c4f944532603a1327df1049981a7b3e562eb51 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:21:36 +0200 Subject: [PATCH 2/9] sort-util: use comparison_fn_t instead of __compar_fn_t Let's avoid using the internal type of glibc, and rather use the one they officially export. https://www.gnu.org/software/libc/manual/html_node/Comparison-Functions.html --- src/basic/sort-util.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h index 49586a4a24..daa0305a60 100644 --- a/src/basic/sort-util.h +++ b/src/basic/sort-util.h @@ -20,7 +20,7 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, * that only if nmemb > 0. */ static inline void* bsearch_safe(const void *key, const void *base, - size_t nmemb, size_t size, __compar_fn_t compar) { + size_t nmemb, size_t size, comparison_fn_t compar) { if (nmemb <= 0) return NULL; @@ -32,14 +32,14 @@ static inline void* bsearch_safe(const void *key, const void *base, ({ \ const typeof(b[0]) *_k = k; \ int (*_func_)(const typeof(b[0])*, const typeof(b[0])*) = func; \ - bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_fn_t) _func_); \ + bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_fn_t) _func_); \ }) /** * Normal qsort requires base to be nonnull. Here were require * that only if nmemb > 0. */ -static inline void _qsort_safe(void *base, size_t nmemb, size_t size, __compar_fn_t compar) { +static inline void _qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) { if (nmemb <= 1) return; @@ -52,7 +52,7 @@ static inline void _qsort_safe(void *base, size_t nmemb, size_t size, __compar_f #define typesafe_qsort(p, n, func) \ ({ \ int (*_func_)(const typeof(p[0])*, const typeof(p[0])*) = func; \ - _qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \ + _qsort_safe((p), (n), sizeof((p)[0]), (comparison_fn_t) _func_); \ }) static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) { From 2ccd598635063abae2128540e6c29ce2f7b990ab Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:29:57 +0200 Subject: [PATCH 3/9] stub: also move magic string in stub into .sdmagic PE section We already did that for sd-boot, hence do it for sd-stub the same way. Also, move the __attribute__ stuff to the beginning of the statement, rather than the middle. Mostly just because we usually put it first for implementations for identifiers (for prototypes we put it last). --- src/boot/efi/boot.c | 2 +- src/boot/efi/stub.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index d82d679a47..eb07bf6898 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -25,7 +25,7 @@ #define TEXT_ATTR_SWAP(c) EFI_TEXT_ATTR(((c) & 0b11110000) >> 4, (c) & 0b1111) /* magic string to find in the binary image */ -static const char _used_ _section_(".sdmagic") magic[] = "#### LoaderInfo: systemd-boot " GIT_VERSION " ####"; +_used_ _section_(".sdmagic") static const char magic[] = "#### LoaderInfo: systemd-boot " GIT_VERSION " ####"; enum loader_type { LOADER_UNDEFINED, diff --git a/src/boot/efi/stub.c b/src/boot/efi/stub.c index dad0f61335..4662a1495d 100644 --- a/src/boot/efi/stub.c +++ b/src/boot/efi/stub.c @@ -14,7 +14,7 @@ #include "util.h" /* magic string to find in the binary image */ -static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " GIT_VERSION " ####"; +_used_ _section_(".sdmagic") static const char magic[] = "#### LoaderInfo: systemd-stub " GIT_VERSION " ####"; static EFI_STATUS combine_initrd( EFI_PHYSICAL_ADDRESS initrd_base, UINTN initrd_size, From 899c1c0a34eee851435349c4e4412347ca9907b5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:38:37 +0200 Subject: [PATCH 4/9] macro: also use trailing __ for alignof use in attributes While the underscore is optional, the docs say we should suffix and we do that everywher else. Do so here too. --- src/basic/macro.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/macro.h b/src/basic/macro.h index 6d5840a832..c05339832d 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -25,7 +25,7 @@ #define _public_ __attribute__((__visibility__("default"))) #define _hidden_ __attribute__((__visibility__("hidden"))) #define _weakref_(x) __attribute__((__weakref__(#x))) -#define _alignas_(x) __attribute__((__aligned__(__alignof(x)))) +#define _alignas_(x) __attribute__((__aligned__(__alignof__(x)))) #define _alignptr_ __attribute__((__aligned__(sizeof(void*)))) #define _warn_unused_result_ __attribute__((__warn_unused_result__)) From fe92eb795b4f380522cd2b2f75eb555f0c060306 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:39:25 +0200 Subject: [PATCH 5/9] network: use official bswap_32() rather than inofficial __bswap_32() The former is a macro for the latter, but let's use the official API (the one that has an API). --- src/basic/sparse-endian.h | 12 ++++++------ src/libsystemd-network/dhcp-identifier.c | 2 +- src/libsystemd-network/test-dhcp-client.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/basic/sparse-endian.h b/src/basic/sparse-endian.h index 9583dda9e5..c795d3da8f 100644 --- a/src/basic/sparse-endian.h +++ b/src/basic/sparse-endian.h @@ -55,9 +55,9 @@ typedef uint64_t __sd_bitwise be64_t; #undef le64toh #if __BYTE_ORDER == __LITTLE_ENDIAN -#define bswap_16_on_le(x) __bswap_16(x) -#define bswap_32_on_le(x) __bswap_32(x) -#define bswap_64_on_le(x) __bswap_64(x) +#define bswap_16_on_le(x) bswap_16(x) +#define bswap_32_on_le(x) bswap_32(x) +#define bswap_64_on_le(x) bswap_64(x) #define bswap_16_on_be(x) (x) #define bswap_32_on_be(x) (x) #define bswap_64_on_be(x) (x) @@ -65,9 +65,9 @@ typedef uint64_t __sd_bitwise be64_t; #define bswap_16_on_le(x) (x) #define bswap_32_on_le(x) (x) #define bswap_64_on_le(x) (x) -#define bswap_16_on_be(x) __bswap_16(x) -#define bswap_32_on_be(x) __bswap_32(x) -#define bswap_64_on_be(x) __bswap_64(x) +#define bswap_16_on_be(x) bswap_16(x) +#define bswap_32_on_be(x) bswap_32(x) +#define bswap_64_on_be(x) bswap_64(x) #endif static inline le16_t htole16(uint16_t value) { return (le16_t __sd_force) bswap_16_on_be(value); } diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index 3313e53f74..23021f7bad 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -207,7 +207,7 @@ int dhcp_identifier_set_iaid( if (legacy_unstable_byteorder) /* for historical reasons (a bug), the bits were swapped and thus * the result was endianness dependent. Preserve that behavior. */ - id32 = __bswap_32(id32); + id32 = bswap_32(id32); else /* the fixed behavior returns a stable byte order. Since LE is expected * to be more common, swap the bytes on LE to give the same as legacy diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index 10db7f6c4b..4428e54bc7 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -158,7 +158,7 @@ static void test_dhcp_identifier_set_iaid(void) { #if __BYTE_ORDER == __LITTLE_ENDIAN assert_se(iaid == iaid_legacy); #else - assert_se(iaid == __bswap_32(iaid_legacy)); + assert_se(iaid == bswap_32(iaid_legacy)); #endif } From 7fbae5b7069c67ab203d50a2d8295a4d970f10c5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:50:29 +0200 Subject: [PATCH 6/9] tree-wide: use C99 __func__ rather than obsolete __FUNCTION__ We use __func__ almost everywhere, but there are some holdouts. Fix that. --- src/core/selinux-access.c | 2 +- src/libsystemd-network/test-dhcp-client.c | 10 +++++----- src/libsystemd-network/test-ipv4ll.c | 4 ++-- src/libsystemd-network/test-ndisc-ra.c | 6 +++--- src/libsystemd-network/test-ndisc-rs.c | 4 ++-- src/udev/scsi_id/scsi_serial.c | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/core/selinux-access.c b/src/core/selinux-access.c index 984e324d74..513a4fb00e 100644 --- a/src/core/selinux-access.c +++ b/src/core/selinux-access.c @@ -126,7 +126,7 @@ _printf_(2, 3) static int log_callback(int type, const char *fmt, ...) { DISABLE_WARNING_FORMAT_NONLITERAL; log_internalv(LOG_AUTH | callback_type_to_priority(type), - 0, PROJECT_FILE, __LINE__, __FUNCTION__, + 0, PROJECT_FILE, __LINE__, __func__, fmt2, ap); REENABLE_WARNING; va_end(ap); diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index 4428e54bc7..4b11cdbc16 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -45,7 +45,7 @@ static void test_request_basic(sd_event *e) { sd_dhcp_client *client; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); /* Initialize client without Anonymize settings. */ r = sd_dhcp_client_new(&client, false); @@ -105,7 +105,7 @@ static void test_request_anonymize(sd_event *e) { sd_dhcp_client *client; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); /* Initialize client with Anonymize settings. */ r = sd_dhcp_client_new(&client, true); @@ -137,7 +137,7 @@ static void test_checksum(void) { }; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); assert_se(dhcp_packet_checksum((uint8_t*)&buf, 20) == be16toh(0x78ae)); } @@ -279,7 +279,7 @@ static void test_discover_message(sd_event *e) { int res, r; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); r = sd_dhcp_client_new(&client, false); assert_se(r >= 0); @@ -497,7 +497,7 @@ static void test_addr_acq(sd_event *e) { int res, r; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); r = sd_dhcp_client_new(&client, false); assert_se(r >= 0); diff --git a/src/libsystemd-network/test-ipv4ll.c b/src/libsystemd-network/test-ipv4ll.c index 7985185b33..e90e73459d 100644 --- a/src/libsystemd-network/test-ipv4ll.c +++ b/src/libsystemd-network/test-ipv4ll.c @@ -81,7 +81,7 @@ static void test_public_api_setters(sd_event *e) { .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); assert_se(sd_ipv4ll_new(&ll) == 0); assert_se(ll); @@ -130,7 +130,7 @@ static void test_basic_request(sd_event *e) { .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); assert_se(sd_ipv4ll_new(&ll) == 0); assert_se(sd_ipv4ll_start(ll) == -EINVAL); diff --git a/src/libsystemd-network/test-ndisc-ra.c b/src/libsystemd-network/test-ndisc-ra.c index 927e21b3cd..4713bc99b5 100644 --- a/src/libsystemd-network/test-ndisc-ra.c +++ b/src/libsystemd-network/test-ndisc-ra.c @@ -111,7 +111,7 @@ static int test_rs_hangcheck(sd_event_source *s, uint64_t usec, static void test_radv_prefix(void) { sd_radv_prefix *p; - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); assert_se(sd_radv_prefix_new(&p) >= 0); @@ -153,7 +153,7 @@ static void test_radv_prefix(void) { static void test_radv(void) { sd_radv *ra; - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); assert_se(sd_radv_new(&ra) >= 0); assert_se(ra); @@ -295,7 +295,7 @@ static void test_ra(void) { sd_radv *ra; unsigned i; - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); assert_se(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) >= 0); diff --git a/src/libsystemd-network/test-ndisc-rs.c b/src/libsystemd-network/test-ndisc-rs.c index 5e50453c35..ae26ee7e74 100644 --- a/src/libsystemd-network/test-ndisc-rs.c +++ b/src/libsystemd-network/test-ndisc-rs.c @@ -273,7 +273,7 @@ static void test_rs(void) { sd_ndisc *nd; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); send_ra_function = send_ra; @@ -366,7 +366,7 @@ static void test_timeout(void) { sd_ndisc *nd; if (verbose) - printf("* %s\n", __FUNCTION__); + printf("* %s\n", __func__); send_ra_function = test_timeout_value; diff --git a/src/udev/scsi_id/scsi_serial.c b/src/udev/scsi_id/scsi_serial.c index 489f5ad16a..bde2811dd7 100644 --- a/src/udev/scsi_id/scsi_serial.c +++ b/src/udev/scsi_id/scsi_serial.c @@ -237,7 +237,7 @@ static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io) { */ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "%s: called with no error", - __FUNCTION__); + __func__); log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x", dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status); @@ -255,7 +255,7 @@ static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io) { */ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "%s: called with no error", - __FUNCTION__); + __func__); log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x", dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status); From 95fe7b28d3ca353b549a1d5d23898743c51b66a7 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:50:54 +0200 Subject: [PATCH 7/9] ethtool-util: let's use userspace types in userspace code Using kernel types __u32 is fine for headers shared by the kernel, but if we define something in userspace and only use it in userspace, in our own .c files, let's stick to userspace fixed-length types. --- src/shared/ethtool-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/ethtool-util.c b/src/shared/ethtool-util.c index ee7be4635f..333e5a4879 100644 --- a/src/shared/ethtool-util.c +++ b/src/shared/ethtool-util.c @@ -756,7 +756,7 @@ int ethtool_set_features(int *ethtool_fd, const char *ifname, const int features static int get_glinksettings(int fd, struct ifreq *ifr, struct ethtool_link_usettings **ret) { struct ecmd { struct ethtool_link_settings req; - __u32 link_mode_data[3 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]; + uint32_t link_mode_data[3 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]; } ecmd = { .req.cmd = ETHTOOL_GLINKSETTINGS, }; @@ -857,7 +857,7 @@ static int get_gset(int fd, struct ifreq *ifr, struct ethtool_link_usettings **r static int set_slinksettings(int fd, struct ifreq *ifr, const struct ethtool_link_usettings *u) { struct { struct ethtool_link_settings req; - __u32 link_mode_data[3 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]; + uint32_t link_mode_data[3 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]; } ecmd = {}; unsigned offset; From 11c8b1f1031d368358286f4bb26abebd73cd2868 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 13:56:22 +0200 Subject: [PATCH 8/9] localed: use PROJECT_FILE rather than __FILE__ for logging All our log.h code uses PROJECT_FILE for this, let's hence use it here too. --- src/locale/localed.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/locale/localed.c b/src/locale/localed.c index df0eb030d4..c228385d0e 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -560,7 +560,7 @@ static void log_xkb(struct xkb_context *ctx, enum xkb_log_level lvl, const char fmt = strjoina("libxkbcommon: ", format); DISABLE_WARNING_FORMAT_NONLITERAL; - log_internalv(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, fmt, args); + log_internalv(LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__, fmt, args); REENABLE_WARNING; } From d8f16737005eb80b34df98d96f61f0b8542b098f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Oct 2021 14:03:00 +0200 Subject: [PATCH 9/9] sort-util: avoid using glibc's internal __compar_d_fn_t type --- src/basic/sort-util.c | 2 +- src/basic/sort-util.h | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/basic/sort-util.c b/src/basic/sort-util.c index a9c68b7e3e..e0fb9cf4a8 100644 --- a/src/basic/sort-util.c +++ b/src/basic/sort-util.c @@ -5,7 +5,7 @@ /* hey glibc, APIs with callbacks without a user pointer are so useless */ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, - __compar_d_fn_t compar, void *arg) { + comparison_userdata_fn_t compar, void *arg) { size_t l, u, idx; const void *p; int comparison; diff --git a/src/basic/sort-util.h b/src/basic/sort-util.h index daa0305a60..02a6784d99 100644 --- a/src/basic/sort-util.h +++ b/src/basic/sort-util.h @@ -5,14 +5,20 @@ #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 + * external type __compar_fn_t, but doesn't do anything similar for __compar_d_fn_t. Let's hence do that + * ourselves, picking a name that is obvious, but likely enough to not clash with glibc's choice of naming if + * they should ever add one. */ +typedef int (*comparison_userdata_fn_t)(const void *, const void *, void *); + void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, - __compar_d_fn_t compar, void *arg); + comparison_userdata_fn_t compar, void *arg); #define typesafe_bsearch_r(k, b, n, func, userdata) \ ({ \ const typeof(b[0]) *_k = k; \ int (*_func_)(const typeof(b[0])*, const typeof(b[0])*, typeof(userdata)) = func; \ - xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (__compar_d_fn_t) _func_, userdata); \ + xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_userdata_fn_t) _func_, userdata); \ }) /** @@ -55,7 +61,7 @@ static inline void _qsort_safe(void *base, size_t nmemb, size_t size, comparison _qsort_safe((p), (n), sizeof((p)[0]), (comparison_fn_t) _func_); \ }) -static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_d_fn_t compar, void *userdata) { +static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, comparison_userdata_fn_t compar, void *userdata) { if (nmemb <= 1) return; @@ -66,7 +72,7 @@ static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, __compar_ #define typesafe_qsort_r(p, n, func, userdata) \ ({ \ int (*_func_)(const typeof(p[0])*, const typeof(p[0])*, typeof(userdata)) = func; \ - qsort_r_safe((p), (n), sizeof((p)[0]), (__compar_d_fn_t) _func_, userdata); \ + qsort_r_safe((p), (n), sizeof((p)[0]), (comparison_userdata_fn_t) _func_, userdata); \ }) int cmp_int(const int *a, const int *b);