From: Simon Kuenzer Date: Thu, 9 Feb 2023 20:54:40 +0000 (+0100) Subject: lib/posix-environ: Register and make it compile X-Git-Tag: RELEASE-0.13.0~55 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=a46c0c1d63a71742e79b4fdd1bf690df560abf8d;p=unikraft%2Funikraft.git lib/posix-environ: Register and make it compile This commit integrates and enables compiling of the imported environment helper functions (see previous commit). Signed-off-by: Simon Kuenzer Reviewed-by: Delia Pavel Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #868 --- diff --git a/lib/Makefile.uk b/lib/Makefile.uk index 01b8be253..89cda95a5 100644 --- a/lib/Makefile.uk +++ b/lib/Makefile.uk @@ -9,6 +9,7 @@ $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/devfs)) $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/fdt)) $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/isrlib)) $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/nolibc)) +$(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/posix-environ)) $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/posix-event)) $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/posix-libdl)) $(eval $(call _import_lib,$(CONFIG_UK_BASE)/lib/posix-mmap)) diff --git a/lib/nolibc/include/stdlib.h b/lib/nolibc/include/stdlib.h index 745515210..994a93e7d 100644 --- a/lib/nolibc/include/stdlib.h +++ b/lib/nolibc/include/stdlib.h @@ -122,6 +122,14 @@ void exit(int status) __noreturn; void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); +#if CONFIG_LIBPOSIX_ENVIRON +int setenv(const char *name, const char *value, int overwrite); +int unsetenv(const char *name); +int clearenv(void); +int putenv(char *string); +char *getenv(const char *name); +#endif /* CONFIG_LIBPOSIX_ENVIRON */ + #if CONFIG_LIBPOSIX_PROCESS int system(const char *command); #endif diff --git a/lib/posix-environ/Config.uk b/lib/posix-environ/Config.uk new file mode 100644 index 000000000..b8ecb7e72 --- /dev/null +++ b/lib/posix-environ/Config.uk @@ -0,0 +1,4 @@ +config LIBPOSIX_ENVIRON + bool "posix-environ: Environment variables" + default n + select LIBNOLIBC if !HAVE_LIBC diff --git a/lib/posix-environ/Makefile.uk b/lib/posix-environ/Makefile.uk new file mode 100644 index 000000000..9e36517fa --- /dev/null +++ b/lib/posix-environ/Makefile.uk @@ -0,0 +1,8 @@ +$(eval $(call addlib_s,libposix_environ,$(CONFIG_LIBPOSIX_ENVIRON))) + +LIBPOSIX_ENVIRON_SRCS-y += $(LIBPOSIX_ENVIRON_BASE)/setenv.c +LIBPOSIX_ENVIRON_SRCS-y += $(LIBPOSIX_ENVIRON_BASE)/unsetenv.c +LIBPOSIX_ENVIRON_SRCS-y += $(LIBPOSIX_ENVIRON_BASE)/clearenv.c +LIBPOSIX_ENVIRON_SRCS-y += $(LIBPOSIX_ENVIRON_BASE)/putenv.c +LIBPOSIX_ENVIRON_SRCS-y += $(LIBPOSIX_ENVIRON_BASE)/getenv.c +LIBPOSIX_ENVIRON_SRCS-y += $(LIBPOSIX_ENVIRON_BASE)/environ.c diff --git a/lib/posix-environ/clearenv.c b/lib/posix-environ/clearenv.c index 15d302fa1..ab1558c65 100644 --- a/lib/posix-environ/clearenv.c +++ b/lib/posix-environ/clearenv.c @@ -13,9 +13,7 @@ #define _GNU_SOURCE #include #include - -static void dummy(char *old, char *new) {} -weak_alias(dummy, __env_rm_add); +#include "environ.h" int clearenv() { diff --git a/lib/posix-environ/environ.c b/lib/posix-environ/environ.c new file mode 100644 index 000000000..0ff3a69f6 --- /dev/null +++ b/lib/posix-environ/environ.c @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors. + * Licensed under the BSD-3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + */ +#include +#include "environ.h" + +char **__environ = __NULL; diff --git a/lib/posix-environ/environ.h b/lib/posix-environ/environ.h new file mode 100644 index 000000000..ae7915728 --- /dev/null +++ b/lib/posix-environ/environ.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors. + * Licensed under the BSD-3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + */ +#ifndef __ENVIRON_H__ +#define __ENVIRON_H__ + +#include +#include + +extern char **__environ; + +int __putenv(char *s, size_t l, char *r); +void __env_rm_add(char *old, char *new); + +#endif /* __ENVIRON_H__ */ diff --git a/lib/posix-environ/exportsyms.uk b/lib/posix-environ/exportsyms.uk new file mode 100644 index 000000000..b2a9dc00f --- /dev/null +++ b/lib/posix-environ/exportsyms.uk @@ -0,0 +1,6 @@ +setenv +unsetenv +clearenv +putenv +getenv +__environ diff --git a/lib/posix-environ/getenv.c b/lib/posix-environ/getenv.c index ae9b30788..16fc82229 100644 --- a/lib/posix-environ/getenv.c +++ b/lib/posix-environ/getenv.c @@ -10,13 +10,15 @@ * File: src/env/getenv.c */ +#define _GNU_SOURCE #include #include #include +#include "environ.h" char *getenv(const char *name) { - size_t l = __strchrnul(name, '=') - name; + size_t l = strchrnul(name, '=') - name; if (l && !name[l] && __environ) for (char **e = __environ; *e; e++) if (!strncmp(name, *e, l) && l[*e] == '=') diff --git a/lib/posix-environ/putenv.c b/lib/posix-environ/putenv.c index dfa7b6921..7a0cee18d 100644 --- a/lib/posix-environ/putenv.c +++ b/lib/posix-environ/putenv.c @@ -10,12 +10,11 @@ * File: src/env/putenv.c */ +#define _GNU_SOURCE #include #include #include - -static void dummy(char *old, char *new) {} -weak_alias(dummy, __env_rm_add); +#include "environ.h" int __putenv(char *s, size_t l, char *r) { @@ -52,7 +51,7 @@ oom: int putenv(char *s) { - size_t l = __strchrnul(s, '=') - s; + size_t l = strchrnul(s, '=') - s; if (!l || !s[l]) return unsetenv(s); return __putenv(s, l, 0); } diff --git a/lib/posix-environ/setenv.c b/lib/posix-environ/setenv.c index bfcb13cd6..4de28d7c8 100644 --- a/lib/posix-environ/setenv.c +++ b/lib/posix-environ/setenv.c @@ -10,9 +10,11 @@ * File: src/env/setenv.c */ +#define _GNU_SOURCE #include #include #include +#include "environ.h" void __env_rm_add(char *old, char *new) { @@ -38,7 +40,7 @@ int setenv(const char *var, const char *value, int overwrite) char *s; size_t l1, l2; - if (!var || !(l1 = __strchrnul(var, '=') - var) || var[l1]) { + if (!var || !(l1 = strchrnul(var, '=') - var) || var[l1]) { errno = EINVAL; return -1; } diff --git a/lib/posix-environ/unsetenv.c b/lib/posix-environ/unsetenv.c index 99c045dbf..963a9fb5d 100644 --- a/lib/posix-environ/unsetenv.c +++ b/lib/posix-environ/unsetenv.c @@ -10,17 +10,16 @@ * File: src/env/unsetenv.c */ +#define _GNU_SOURCE #include #include #include #include - -static void dummy(char *old, char *new) {} -weak_alias(dummy, __env_rm_add); +#include "environ.h" int unsetenv(const char *name) { - size_t l = __strchrnul(name, '=') - name; + size_t l = strchrnul(name, '=') - name; if (!l || name[l]) { errno = EINVAL; return -1;