Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sysfs: read address bits from sysfs file #1966

Merged
merged 2 commits into from Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions include/pathnames.h
Expand Up @@ -120,6 +120,7 @@
#define _PATH_SYS_CLASS "/sys/class"
#define _PATH_SYS_SCSI "/sys/bus/scsi"
#define _PATH_SYS_CPU_BYTEORDER "/sys/kernel/cpu_byteorder"
#define _PATH_SYS_ADDRESS_BITS "/sys/kernel/address_bits"

#define _PATH_SYS_SELINUX "/sys/fs/selinux"
#define _PATH_SYS_APPARMOR "/sys/kernel/security/apparmor"
Expand Down
1 change: 1 addition & 0 deletions include/sysfs.h
Expand Up @@ -117,5 +117,6 @@ enum sysfs_byteorder {
};

extern enum sysfs_byteorder sysfs_get_byteorder(struct path_cxt *pc);
extern int sysfs_get_address_bits(struct path_cxt *pc);

#endif /* UTIL_LINUX_SYSFS_H */
13 changes: 13 additions & 0 deletions lib/sysfs.c
Expand Up @@ -1103,6 +1103,19 @@ enum sysfs_byteorder sysfs_get_byteorder(struct path_cxt *pc)
return ret;
}

int sysfs_get_address_bits(struct path_cxt *pc)
{
int rc;
int address_bits;

rc = ul_path_scanf(pc, _PATH_SYS_ADDRESS_BITS, "%d", &address_bits);
if (rc < 0)
return rc;
if (address_bits < 0)
return -EINVAL;
return address_bits;
}


#ifdef TEST_PROGRAM_SYSFS
#include <errno.h>
Expand Down
1 change: 1 addition & 0 deletions meson.build
Expand Up @@ -1439,6 +1439,7 @@ exe = executable(
'setarch',
setarch_sources,
include_directories : includes,
link_with : [lib_common],
install_dir : usrbin_exec_dir,
install : true)
exes += exe
Expand Down
1 change: 1 addition & 0 deletions sys-utils/Makemodule.am
Expand Up @@ -231,6 +231,7 @@ usrbin_exec_PROGRAMS += setarch
MANPAGES += sys-utils/setarch.8
dist_noinst_DATA += sys-utils/setarch.8.adoc
setarch_SOURCES = sys-utils/setarch.c
setarch_LDADD = $(LDADD) libcommon.la

SETARCH_LINKS = uname26 linux32 linux64

Expand Down
5 changes: 4 additions & 1 deletion sys-utils/setarch.c
Expand Up @@ -35,6 +35,7 @@
#include "nls.h"
#include "c.h"
#include "closestream.h"
#include "sysfs.h"

#ifndef HAVE_PERSONALITY
# include <syscall.h>
Expand Down Expand Up @@ -232,7 +233,9 @@ static struct arch_domain *init_arch_domains(void)
if (!strcmp(un.machine, transitions[i].target_arch))
break;
if (transitions[i].perval < 0) {
unsigned long wrdsz = CHAR_BIT * sizeof(void *);
int wrdsz = sysfs_get_address_bits(NULL);
if (wrdsz < 0)
wrdsz = CHAR_BIT * sizeof(void *);
if (wrdsz == 32 || wrdsz == 64) {
/* fill up the place holder */
transitions[i].perval = wrdsz == 32 ? PER_LINUX32 : PER_LINUX;
Expand Down