Skip to content

Commit b0c25f8

Browse files
committed
samples: posix: add environment variable sample app
Add a sample application to demonstrate some basic C and shell interfaces for manipulating environment variables. Signed-off-by: Christopher Friedt <[email protected]>
1 parent dab0cd6 commit b0c25f8

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

samples/posix/env/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(posix_env)
7+
8+
target_sources(app PRIVATE src/main.c)
9+
# For setenv() and unsetenv()
10+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
11+
# For getenv_r() visibility and testing
12+
target_compile_definitions(app PRIVATE _BSD_SOURCE)

samples/posix/env/README.rst

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. _posix-env-sample:
2+
3+
POSIX Environment Variables
4+
###########################
5+
6+
Overview
7+
********
8+
9+
In this sample application, the POSIX :c:func:`setenv`, function is used to populate several environment
10+
variables in C. Then, all environment variables are then printed.
11+
12+
If the user sets a new value for the ``ALERT`` environment variable, it is printed to standard
13+
output, and then cleared via :c:func:`unsetenv`.
14+
15+
Building and Running
16+
********************
17+
18+
This project outputs to the console. It can be built and executed on QEMU as follows:
19+
20+
.. zephyr-app-commands::
21+
:zephyr-app: samples/posix/env
22+
:host-os: unix
23+
:board: qemu_riscv32
24+
:goals: run
25+
:compact:
26+
27+
Sample Output
28+
=============
29+
30+
The program below shows sample output for a specific Zephyr build.
31+
32+
.. code-block:: console
33+
34+
BOARD=qemu_riscv32
35+
BUILD_VERSION=zephyr-v3.5.0-5372-g3a46f2d052c7
36+
ALERT=
37+
38+
Setting Environment Variables
39+
=============================
40+
41+
The shell command below shows how to create a new environment variable or update the value
42+
associated with an existing environment variable.
43+
44+
The C code that is part of this sample application displays the value associated with the
45+
``ALERT`` environment variable and then immediately unsets it.
46+
47+
.. code-block:: console
48+
49+
uart:~$ posix env set ALERT="Happy Friday!"
50+
uart:~$ ALERT="Happy Friday!"
51+
uart:~$ posix env set HOME="127.0.0.1"
52+
uart:~$
53+
54+
55+
Getting Environment Variables
56+
=============================
57+
58+
The shell command below may be used to display the value associated with one environment variable.
59+
60+
.. code-block:: console
61+
62+
uart:~$ posix env get BOARD
63+
qemu_riscv32
64+
65+
The shell command below may be used to display all environment variables and their associated
66+
values.
67+
68+
.. code-block:: console
69+
70+
uart:~$ posix env get
71+
BOARD=qemu_riscv32
72+
BUILD_VERSION=zephyr-v3.5.0-5372-g3a46f2d052c7
73+
ALERT=
74+
75+
Unsetting Environment Variables
76+
===============================
77+
78+
The shell command below may be used to unset environment variables.
79+
80+
.. code-block:: console
81+
82+
uart:~$ posix env unset BOARD

samples/posix/env/prj.conf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_POSIX_API=y
2+
CONFIG_SHELL=y
3+
CONFIG_POSIX_ENV_SHELL=y
4+
CONFIG_DYNAMIC_THREAD=y
5+
CONFIG_DYNAMIC_THREAD_POOL_SIZE=1

samples/posix/env/sample.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
sample:
2+
description: posix env sample
3+
name: posix env
4+
common:
5+
tags: posix env
6+
platform_exclude:
7+
- native_posix
8+
- native_posix_64
9+
integration_platforms:
10+
- qemu_riscv32
11+
harness: console
12+
harness_config:
13+
type: multi_line
14+
regex:
15+
- "BOARD=.*"
16+
- "BUILD_VERSION=.*"
17+
- "ALERT=.*"
18+
tests:
19+
sample.posix.env: {}

samples/posix/env/src/main.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2023 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "version.h"
8+
9+
#include <pthread.h>
10+
#include <stdbool.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <unistd.h>
15+
16+
#include <zephyr/sys/util.h>
17+
#include <zephyr/kernel.h>
18+
19+
#ifdef BUILD_VERSION
20+
#define VERSION_BUILD STRINGIFY(BUILD_VERSION)
21+
#else
22+
#define VERSION_BUILD KERNEL_VERSION_STRING
23+
#endif
24+
25+
#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_PICOLIBC)
26+
/* newlib headers seem to be missing this */
27+
int getenv_r(const char *name, char *val, size_t len);
28+
#endif
29+
30+
static void env(void)
31+
{
32+
extern char **environ;
33+
34+
if (environ != NULL) {
35+
for (char **envp = environ; *envp != NULL; ++envp) {
36+
printf("%s\n", *envp);
37+
}
38+
}
39+
}
40+
41+
static void *entry(void *arg)
42+
{
43+
static char alert_msg_buf[42];
44+
45+
setenv("BOARD", CONFIG_BOARD, 1);
46+
setenv("BUILD_VERSION", VERSION_BUILD, 1);
47+
setenv("ALERT", "", 1);
48+
49+
env();
50+
51+
while (true) {
52+
sleep(1);
53+
if (getenv_r("ALERT", alert_msg_buf, sizeof(alert_msg_buf) - 1) < 0 ||
54+
strlen(alert_msg_buf) == 0) {
55+
continue;
56+
}
57+
printf("ALERT=%s\n", alert_msg_buf);
58+
unsetenv("ALERT");
59+
}
60+
61+
return NULL;
62+
}
63+
64+
int main(void)
65+
{
66+
pthread_t th;
67+
68+
/* create a separate thread so that the shell can start */
69+
return pthread_create(&th, NULL, entry, NULL);
70+
}

0 commit comments

Comments
 (0)