1
0
mirror of https://github.com/systemd/systemd synced 2026-04-01 04:34:51 +02:00

Compare commits

...

6 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek
3b3113b87c
locale-util: two fixlets for supporting musl (#39689) 2025-11-12 10:38:29 +01:00
Yu Watanabe
6b656a5b40 test-bus-error: use STRERROR() at several more places 2025-11-12 14:17:44 +08:00
Yu Watanabe
9b55c4b859 tree-wide: drop redundant inclusion of linux/prctl.h
sys/prctl.h anyway includes linux/prctl.h and actually these .c files
includes sys/prctl.h. Hence, it is not necessary to explicitly include
linux/prctl.h.
2025-11-12 14:17:44 +08:00
Yu Watanabe
907ce9c315 libc: drop unnecessary __THROW attribute
It is for C++, not necessary for us.
2025-11-12 14:17:44 +08:00
Yu Watanabe
361beb82a5 musl: locale-util: explicitly check existence of locale file
musl's newlocale() always provides a locale object even the requested
locale does not exist. Let's explicitly check the existence of the
requested locale file.
2025-11-12 11:43:13 +09:00
Yu Watanabe
b2a2f670ae musl: locale-util: introduce musl specific locale enumerator
Both add_locales_from_archive() and add_locales_from_libdir() are glibc
specific, and the logic cannot be applied when built with musl.
2025-11-12 11:43:13 +09:00
10 changed files with 65 additions and 29 deletions

View File

@ -1,6 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/prctl.h>
#include <stdatomic.h>
#include <stdio.h>
#include <sys/prctl.h>

View File

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "dirent-util.h"
#include "env-util.h"
@ -54,6 +55,7 @@ static char* normalize_locale(const char *name) {
return strdup(name);
}
#ifdef __GLIBC__
static int add_locales_from_archive(Set *locales) {
/* Stolen from glibc... */
@ -182,6 +184,34 @@ static int add_locales_from_libdir(Set *locales) {
return 0;
}
#else
static int add_locales_for_musl(Set *locales) {
int r;
assert(locales);
_cleanup_closedir_ DIR *dir = opendir("/usr/share/i18n/locales/musl/");
if (!dir)
return errno == ENOENT ? 0 : -errno;
FOREACH_DIRENT(de, dir, return -errno) {
if (de->d_type != DT_REG)
continue;
char *z = normalize_locale(de->d_name);
if (!z)
return -ENOMEM;
r = set_consume(locales, z);
if (r < 0)
return r;
}
return 0;
}
#endif
int get_locales(char ***ret) {
_cleanup_set_free_ Set *locales = NULL;
int r;
@ -190,6 +220,7 @@ int get_locales(char ***ret) {
if (!locales)
return -ENOMEM;
#ifdef __GLIBC__
r = add_locales_from_archive(locales);
if (r < 0 && r != -ENOENT)
return r;
@ -197,6 +228,11 @@ int get_locales(char ***ret) {
r = add_locales_from_libdir(locales);
if (r < 0)
return r;
#else
r = add_locales_for_musl(locales);
if (r < 0)
return r;
#endif
char *locale;
SET_FOREACH(locale, locales) {
@ -264,11 +300,25 @@ int locale_is_installed(const char *name) {
if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
return true;
#ifdef __GLIBC__
_cleanup_(freelocalep) locale_t loc = newlocale(LC_ALL_MASK, name, (locale_t) 0);
if (loc == (locale_t) 0)
return errno == ENOMEM ? -ENOMEM : false;
return true;
#else
/* musl also has C.UTF-8 as builtin */
if (streq(name, "C.UTF-8"))
return true;
/* musl's newlocale() always succeeds and provides a fake locale object even when the locale does
* not exist. Hence, we need to explicitly check if the locale file exists. */
_cleanup_free_ char *p = path_join("/usr/share/i18n/locales/musl/", name);
if (!p)
return -ENOMEM;
return access(p, F_OK) >= 0;
#endif
}
static bool is_locale_utf8_impl(void) {

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <grp.h>
#include <linux/prctl.h>
#include <linux/sched.h>
#include <linux/securebits.h>
#include <poll.h>

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
#include <linux/prctl.h>
#include <poll.h>
#include <sys/mman.h>
#include <sys/mount.h>

View File

@ -1,10 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <features.h>
#include <linux/if.h> /* IWYU pragma: export */
#define IF_NAMESIZE 16
extern unsigned int if_nametoindex(const char *__ifname) __THROW;
extern char *if_indextoname(unsigned int __ifindex, char __ifname[IF_NAMESIZE]) __THROW;
extern unsigned int if_nametoindex(const char *__ifname);
extern char *if_indextoname(unsigned int __ifindex, char __ifname[IF_NAMESIZE]);

View File

@ -2,7 +2,6 @@
#pragma once
#include <fcntl.h>
#include <features.h>
#include <linux/fs.h>
#include <linux/mount.h> /* IWYU pragma: export */
#include <stddef.h>
@ -29,20 +28,20 @@ enum
};
/* Mount a filesystem. */
extern int mount(const char *__special_file, const char *__dir, const char *__fstype, unsigned long int __rwflag, const void *__data) __THROW;
extern int mount(const char *__special_file, const char *__dir, const char *__fstype, unsigned long int __rwflag, const void *__data);
/* Unmount a filesystem. */
extern int umount(const char *__special_file) __THROW;
extern int umount(const char *__special_file);
/* Unmount a filesystem. Force unmounting if FLAGS is set to MNT_FORCE. */
extern int umount2(const char *__special_file, int __flags) __THROW;
extern int umount2(const char *__special_file, int __flags);
/* Open the filesystem referenced by FS_NAME so it can be configured for
mouting. */
/* Defined since glibc-2.36.
* Supported since kernel v5.2 (24dcb3d90a1f67fe08c68a004af37df059d74005). */
#if HAVE_FSOPEN
extern int fsopen(const char *__fs_name, unsigned int __flags) __THROW;
extern int fsopen(const char *__fs_name, unsigned int __flags);
#else
int missing_fsopen(const char *fsname, unsigned flags);
# define fsopen missing_fsopen
@ -53,7 +52,7 @@ int missing_fsopen(const char *fsname, unsigned flags);
/* Defined since glibc-2.36.
* Supported since kernel v5.2 (93766fbd2696c2c4453dd8e1070977e9cd4e6b6d). */
#if HAVE_FSMOUNT
extern int fsmount(int __fd, unsigned int __flags, unsigned int __ms_flags) __THROW;
extern int fsmount(int __fd, unsigned int __flags, unsigned int __ms_flags);
#else
int missing_fsmount(int fd, unsigned flags, unsigned ms_flags);
# define fsmount missing_fsmount
@ -65,7 +64,7 @@ int missing_fsmount(int fd, unsigned flags, unsigned ms_flags);
/* Defined since glibc-2.36.
* Supported since kernel v5.2 (2db154b3ea8e14b04fee23e3fdfd5e9d17fbc6ae). */
#if HAVE_MOVE_MOUNT
extern int move_mount(int __from_dfd, const char *__from_pathname, int __to_dfd, const char *__to_pathname, unsigned int flags) __THROW;
extern int move_mount(int __from_dfd, const char *__from_pathname, int __to_dfd, const char *__to_pathname, unsigned int flags);
#else
int missing_move_mount(int from_dfd, const char *from_pathname, int to_dfd, const char *to_pathname, unsigned flags);
# define move_mount missing_move_mount
@ -76,7 +75,7 @@ int missing_move_mount(int from_dfd, const char *from_pathname, int to_dfd, cons
/* Defined since glibc-2.36.
* Supported since kernel v5.2 (ecdab150fddb42fe6a739335257949220033b782). */
#if HAVE_FSCONFIG
extern int fsconfig(int __fd, unsigned int __cmd, const char *__key, const void *__value, int __aux) __THROW;
extern int fsconfig(int __fd, unsigned int __cmd, const char *__key, const void *__value, int __aux);
#else
int missing_fsconfig(int fd, unsigned cmd, const char *key, const void *value, int aux);
# define fsconfig missing_fsconfig
@ -86,7 +85,7 @@ int missing_fsconfig(int fd, unsigned cmd, const char *key, const void *value, i
/* Defined since glibc-2.36.
* Supported since kernel v5.2 (a07b20004793d8926f78d63eb5980559f7813404). */
#if HAVE_OPEN_TREE
extern int open_tree(int __dfd, const char *__filename, unsigned int __flags) __THROW;
extern int open_tree(int __dfd, const char *__filename, unsigned int __flags);
#else
int missing_open_tree(int dfd, const char *filename, unsigned flags);
# define open_tree missing_open_tree
@ -100,7 +99,7 @@ int missing_open_tree(int dfd, const char *filename, unsigned flags);
/* Defined since glibc-2.36.
* Supported since kernel v5.12 (2a1867219c7b27f928e2545782b86daaf9ad50bd). */
#if HAVE_MOUNT_SETATTR
extern int mount_setattr(int __dfd, const char *__path, unsigned int __flags, struct mount_attr *__uattr, size_t __usize) __THROW;
extern int mount_setattr(int __dfd, const char *__path, unsigned int __flags, struct mount_attr *__uattr, size_t __usize);
#else
int missing_mount_setattr(int dfd, const char *path, unsigned flags, struct mount_attr *attr, size_t size);
# define mount_setattr missing_mount_setattr
@ -109,7 +108,7 @@ int missing_mount_setattr(int dfd, const char *path, unsigned flags, struct moun
/* Not defined in glibc yet as of glibc-2.41.
* Supported since kernel v6.15 (c4a16820d90199409c9bf01c4f794e1e9e8d8fd8). */
#if HAVE_OPEN_TREE_ATTR
extern int open_tree_attr(int __dfd, const char *__filename, unsigned int __flags, struct mount_attr *__uattr, size_t __usize) __THROW;
extern int open_tree_attr(int __dfd, const char *__filename, unsigned int __flags, struct mount_attr *__uattr, size_t __usize);
#else
int missing_open_tree_attr(int dfd, const char *filename, unsigned int flags, struct mount_attr *attr, size_t size);
# define open_tree_attr missing_open_tree_attr

View File

@ -229,9 +229,7 @@ TEST(sd_bus_error_set_errnof) {
assert_se(sd_bus_error_set_errnof(&error, EACCES, NULL) == -EACCES);
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED));
errno = EACCES;
assert_se(asprintf(&str, "%m") >= 0);
assert_se(streq(error.message, str));
ASSERT_STREQ(error.message, STRERROR(EACCES));
assert_se(error._need_free == 0);
str = mfree(str);
@ -239,9 +237,7 @@ TEST(sd_bus_error_set_errnof) {
assert_se(sd_bus_error_set_errnof(&error, ENOANO, NULL) == -ENOANO);
assert_se(sd_bus_error_has_name(&error, "System.Error.ENOANO"));
errno = ENOANO;
assert_se(asprintf(&str, "%m") >= 0);
assert_se(streq(error.message, str));
ASSERT_STREQ(error.message, STRERROR(ENOANO));
assert_se(error._need_free == 1);
str = mfree(str);
@ -249,9 +245,7 @@ TEST(sd_bus_error_set_errnof) {
assert_se(sd_bus_error_set_errnof(&error, 100000, NULL) == -100000);
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FAILED));
errno = 100000;
assert_se(asprintf(&str, "%m") >= 0);
assert_se(streq(error.message, str));
ASSERT_STREQ(error.message, STRERROR(100000));
assert_se(error._need_free == 1);
str = mfree(str);

View File

@ -1,6 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/prctl.h>
#include <netinet/in.h>
#include <pwd.h>
#include <stdlib.h>

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fnmatch.h>
#include <linux/prctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
#include <linux/prctl.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/prctl.h>