From 137394402cf8f22b45837f40dcb5a5efaf3bde44 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Tue, 4 Jul 2023 15:15:15 +0800 Subject: [PATCH] posix: implement uname Add implementation for posix uname. Signed-off-by: Yong Cong Sin --- include/zephyr/posix/sys/utsname.h | 80 ++++++++++++++++++++++++++++++ lib/posix/CMakeLists.txt | 1 + lib/posix/Kconfig | 7 +++ lib/posix/utsname.c | 24 +++++++++ 4 files changed, 112 insertions(+) create mode 100644 include/zephyr/posix/sys/utsname.h create mode 100644 lib/posix/utsname.c diff --git a/include/zephyr/posix/sys/utsname.h b/include/zephyr/posix/sys/utsname.h new file mode 100644 index 000000000000000..11a3cbf8231f450 --- /dev/null +++ b/include/zephyr/posix/sys/utsname.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023 Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_ +#define ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_ + +#include +#include +#include +#include "version.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* sysname */ +#define SYS_NAMELEN sizeof("Zephyr") + +/* nodename */ +#ifdef CONFIG_NET_HOSTNAME_ENABLE +#define NODE_NAMELEN NET_HOSTNAME_MAX_LEN +#else +#define NODE_NAMELEN SYS_NAMELEN +#endif + +/* release */ +#define RELEASE_NAMELEN sizeof(KERNEL_VERSION_STRING) + +/* version */ +#if defined(__DATE__) && defined(__TIME__) +#define DATETIME_LEN (sizeof(__DATE__) + sizeof(__TIME__)) +#else +#define DATETIME_LEN 0 +#endif + +#ifdef BUILD_VERSION +#define VERSION_BUILD STRINGIFY(BUILD_VERSION) +#else +#define VERSION_BUILD KERNEL_VERSION_STRING +#endif +#define VERSION_BUILD_LEN sizeof(VERSION_BUILD) + +/** + * In case anyone wondered if this length has been adjusted for '\0', it will + * be formatted in utsname.c as: + * "%s %s %s", VERSION_BUILD, __DATE__, __TIME__ + * the '\0' of VERSION_BUILD_LEN & __DATE__ will be used for the space that + * follows, '\0' of __TIME__ will be used for the \0 of the formatted VERSION + * string. In the end the size of the array should fit the string snugly. + */ +#define VERSION_NAMELEN (VERSION_BUILD_LEN + DATETIME_LEN) + +/* machine */ +#define MACHINE_NAMELEN sizeof(CONFIG_ARCH) + +struct utsname { + char sysname[SYS_NAMELEN]; + char nodename[NODE_NAMELEN]; + char release[RELEASE_NAMELEN]; + char version[VERSION_NAMELEN]; + char machine[MACHINE_NAMELEN]; +}; + +int uname(struct utsname *name); + +#undef SYS_NAMELEN +#undef NODE_NAMELEN +#undef RELEASE_NAMELEN +#undef VERSION_NAMELEN +#undef MACHINE_NAMELEN +#undef DATETIME_LEN +#undef VERSION_BUILD_LEN + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_ */ diff --git a/lib/posix/CMakeLists.txt b/lib/posix/CMakeLists.txt index cd6caf6b25eb72b..5eed19616ece536 100644 --- a/lib/posix/CMakeLists.txt +++ b/lib/posix/CMakeLists.txt @@ -37,6 +37,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c) zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c) zephyr_library_sources_ifdef(CONFIG_EVENTFD eventfd.c) zephyr_library_sources_ifdef(CONFIG_FNMATCH fnmatch.c) +zephyr_library_sources_ifdef(CONFIG_POSIX_UNAME utsname.c) add_subdirectory_ifdef(CONFIG_GETOPT getopt) zephyr_library_include_directories( diff --git a/lib/posix/Kconfig b/lib/posix/Kconfig index 7f510210d9c67c1..2d43dbb239f45f0 100644 --- a/lib/posix/Kconfig +++ b/lib/posix/Kconfig @@ -182,3 +182,10 @@ config FNMATCH help Match filenames using the the fnmatch function. For example, the pattern "*.c" matches the filename "hello.c". + +config POSIX_UNAME + bool "Support for uname" + default y if POSIX_API + help + The uname() function shall store information identifying the current + system in the structure pointed to by name. diff --git a/lib/posix/utsname.c b/lib/posix/utsname.c new file mode 100644 index 000000000000000..ece0717ef7327db --- /dev/null +++ b/lib/posix/utsname.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +int uname(struct utsname *name) +{ + strncpy(name->sysname, "Zephyr", sizeof(name->sysname)); + strncpy(name->nodename, net_hostname_get(), sizeof(name->nodename)); + strncpy(name->release, KERNEL_VERSION_STRING, sizeof(name->release)); +#if defined(__DATE__) && defined(__TIME__) + snprintf(name->version, sizeof(name->version), "%s %s %s", VERSION_BUILD, __DATE__, __TIME__); +#else + strncpy(name->version, VERSION_BUILD, sizeof(name->version)); +#endif + strncpy(name->machine, CONFIG_ARCH, sizeof(name->machine)); + + return 0; +}