提交 d3d76fbd 编写于 作者: T Thomas Weißschuh 提交者: Greg Kroah-Hartman

kernel/ksysfs.c: export kernel cpu byteorder

Certain files in procfs are formatted in byteorder-dependent formats.
For example the IP addresses in /proc/net/udp.

When using emulation like qemu-user, applications are not guaranteed to
be using the same byteorder as the kernel.
Therefore the kernel needs to provide a way for applications to discover
the byteorder used in API-filesystems.
Using systemcalls is not enough because these are intercepted and
translated by the emulation.

Also this makes it easier for non-compiled applications like
shellscripts to discover the byteorder.
Signed-off-by: NThomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/r/20221103152407.3348-1-linux@weissschuh.netSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
上级 52c4d11f
What: /sys/kernel/cpu_byteorder
Date: February 2023
KernelVersion: 6.2
Contact: Thomas Weißschuh <linux@weissschuh.net>
Description:
The endianness of the running kernel.
Access: Read
Valid values:
"little", "big"
Users: util-linux
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
*/ */
#include <asm/byteorder.h>
#include <linux/kobject.h> #include <linux/kobject.h>
#include <linux/string.h> #include <linux/string.h>
#include <linux/sysfs.h> #include <linux/sysfs.h>
...@@ -20,6 +21,14 @@ ...@@ -20,6 +21,14 @@
#include <linux/rcupdate.h> /* rcu_expedited and rcu_normal */ #include <linux/rcupdate.h> /* rcu_expedited and rcu_normal */
#if defined(__LITTLE_ENDIAN)
#define CPU_BYTEORDER_STRING "little"
#elif defined(__BIG_ENDIAN)
#define CPU_BYTEORDER_STRING "big"
#else
#error Unknown byteorder
#endif
#define KERNEL_ATTR_RO(_name) \ #define KERNEL_ATTR_RO(_name) \
static struct kobj_attribute _name##_attr = __ATTR_RO(_name) static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
...@@ -34,6 +43,14 @@ static ssize_t uevent_seqnum_show(struct kobject *kobj, ...@@ -34,6 +43,14 @@ static ssize_t uevent_seqnum_show(struct kobject *kobj,
} }
KERNEL_ATTR_RO(uevent_seqnum); KERNEL_ATTR_RO(uevent_seqnum);
/* cpu byteorder */
static ssize_t cpu_byteorder_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return sysfs_emit(buf, "%s\n", CPU_BYTEORDER_STRING);
}
KERNEL_ATTR_RO(cpu_byteorder);
#ifdef CONFIG_UEVENT_HELPER #ifdef CONFIG_UEVENT_HELPER
/* uevent helper program, used during early boot */ /* uevent helper program, used during early boot */
static ssize_t uevent_helper_show(struct kobject *kobj, static ssize_t uevent_helper_show(struct kobject *kobj,
...@@ -215,6 +232,7 @@ EXPORT_SYMBOL_GPL(kernel_kobj); ...@@ -215,6 +232,7 @@ EXPORT_SYMBOL_GPL(kernel_kobj);
static struct attribute * kernel_attrs[] = { static struct attribute * kernel_attrs[] = {
&fscaps_attr.attr, &fscaps_attr.attr,
&uevent_seqnum_attr.attr, &uevent_seqnum_attr.attr,
&cpu_byteorder_attr.attr,
#ifdef CONFIG_UEVENT_HELPER #ifdef CONFIG_UEVENT_HELPER
&uevent_helper_attr.attr, &uevent_helper_attr.attr,
#endif #endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册