Skip to content

Commit

Permalink
posix: implement uname
Browse files Browse the repository at this point in the history
Add implementation for posix uname.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin committed Jul 4, 2023
1 parent ff2d9cf commit 1373944
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
80 changes: 80 additions & 0 deletions include/zephyr/posix/sys/utsname.h
Original file line number Diff line number Diff line change
@@ -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 <errno.h>
#include <zephyr/toolchain/common.h>
#include <zephyr/net/hostname.h>
#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_ */
1 change: 1 addition & 0 deletions lib/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions lib/posix/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
24 changes: 24 additions & 0 deletions lib/posix/utsname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 Meta
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/posix/sys/utsname.h>
#include <stdio.h>
#include <string.h>

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;
}

0 comments on commit 1373944

Please sign in to comment.