1 /*
2 * Copyright (c) 2023 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <string.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/net/hostname.h>
10 #include <zephyr/posix/sys/utsname.h>
11 #include <zephyr/version.h>
12
13 #ifdef CONFIG_NET_HOSTNAME_ENABLE
14 #define UTSNAME_NODENAME CONFIG_NET_HOSTNAME
15 #else
16 #define UTSNAME_NODENAME "zephyr"
17 #endif
18
19 #if defined(__DATE__) && defined(__TIME__)
20 #define UTSNAME_VERSION(_ver) _ver " " __DATE__ " " __TIME__
21 #else
22 #define UTSNAME_VERSION(_ver) _ver
23 #endif
24
25 #if defined(BUILD_VERSION) && !IS_EMPTY(BUILD_VERSION)
26 #define VERSION_BUILD STRINGIFY(BUILD_VERSION)
27 #else
28 #define VERSION_BUILD KERNEL_VERSION_STRING
29 #endif
30
31 #define UTSNAME_INITIALIZER(_sys, _node, _rel, _ver, _mach) \
32 { \
33 .sysname = _sys, .nodename = _node, .release = _rel, \
34 .version = UTSNAME_VERSION(_ver), .machine = _mach, \
35 }
36
37 static const struct utsname z_name = UTSNAME_INITIALIZER(
38 "Zephyr", UTSNAME_NODENAME, KERNEL_VERSION_STRING, VERSION_BUILD, CONFIG_ARCH);
39
40 BUILD_ASSERT(sizeof(z_name.sysname) >= sizeof("Zephyr"));
41 BUILD_ASSERT(sizeof(z_name.release) >= sizeof(KERNEL_VERSION_STRING));
42 BUILD_ASSERT(sizeof(z_name.version) >= sizeof(UTSNAME_VERSION(VERSION_BUILD)));
43 BUILD_ASSERT(sizeof(z_name.machine) >= sizeof(CONFIG_ARCH));
44
uname(struct utsname * name)45 int uname(struct utsname *name)
46 {
47 memcpy(name, &z_name, sizeof(*name));
48 if (IS_ENABLED(CONFIG_NET_HOSTNAME_ENABLE)) {
49 strncpy(name->nodename, net_hostname_get(), sizeof(name->nodename));
50 name->nodename[sizeof(name->nodename) - 1] = '\0';
51 }
52 return 0;
53 }
54