]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-environ: Import from musl 1.2.3
authorSimon Kuenzer <simon@unikraft.io>
Wed, 8 Feb 2023 16:01:41 +0000 (17:01 +0100)
committerUnikraft <monkey@unikraft.io>
Sun, 7 May 2023 16:44:41 +0000 (16:44 +0000)
This commit imports needed code from musl 1.2.3. This concentrates mostly
on the following helper functions for environment variables:
 - `clearenv()`
 - `getenv()`
 - `putenv()`
 - `setenv()`
 - `unsetenv()`

Checkpatch-Ignore: FUNCTION_WITHOUT_ARGS
Checkpatch-Ignore: LINE_SPACING
Checkpatch-Ignore: SPACING
Checkpatch-Ignore: TRAILING_STATEMENTS
Checkpatch-Ignore: SIZEOF_PARENTHESIS
Checkpatch-Ignore: TYPO_SPELLING
Signed-off-by: Simon Kuenzer <simon@unikraft.io>
Reviewed-by: Delia Pavel <delia_maria.pavel@stud.acs.upb.ro>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #868

lib/posix-environ/COPYING [new file with mode: 0644]
lib/posix-environ/clearenv.c [new file with mode: 0644]
lib/posix-environ/getenv.c [new file with mode: 0644]
lib/posix-environ/putenv.c [new file with mode: 0644]
lib/posix-environ/setenv.c [new file with mode: 0644]
lib/posix-environ/unsetenv.c [new file with mode: 0644]

diff --git a/lib/posix-environ/COPYING b/lib/posix-environ/COPYING
new file mode 100644 (file)
index 0000000..c1628e9
--- /dev/null
@@ -0,0 +1,193 @@
+musl as a whole is licensed under the following standard MIT license:
+
+----------------------------------------------------------------------
+Copyright © 2005-2020 Rich Felker, et al.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+----------------------------------------------------------------------
+
+Authors/contributors include:
+
+A. Wilcox
+Ada Worcester
+Alex Dowad
+Alex Suykov
+Alexander Monakov
+Andre McCurdy
+Andrew Kelley
+Anthony G. Basile
+Aric Belsito
+Arvid Picciani
+Bartosz Brachaczek
+Benjamin Peterson
+Bobby Bingham
+Boris Brezillon
+Brent Cook
+Chris Spiegel
+Clément Vasseur
+Daniel Micay
+Daniel Sabogal
+Daurnimator
+David Carlier
+David Edelsohn
+Denys Vlasenko
+Dmitry Ivanov
+Dmitry V. Levin
+Drew DeVault
+Emil Renner Berthing
+Fangrui Song
+Felix Fietkau
+Felix Janda
+Gianluca Anzolin
+Hauke Mehrtens
+He X
+Hiltjo Posthuma
+Isaac Dunham
+Jaydeep Patil
+Jens Gustedt
+Jeremy Huntwork
+Jo-Philipp Wich
+Joakim Sindholt
+John Spencer
+Julien Ramseier
+Justin Cormack
+Kaarle Ritvanen
+Khem Raj
+Kylie McClain
+Leah Neukirchen
+Luca Barbato
+Luka Perkov
+M Farkas-Dyck (Strake)
+Mahesh Bodapati
+Markus Wichmann
+Masanori Ogino
+Michael Clark
+Michael Forney
+Mikhail Kremnyov
+Natanael Copa
+Nicholas J. Kain
+orc
+Pascal Cuoq
+Patrick Oppenlander
+Petr Hosek
+Petr Skocik
+Pierre Carrier
+Reini Urban
+Rich Felker
+Richard Pennington
+Ryan Fairfax
+Samuel Holland
+Segev Finer
+Shiz
+sin
+Solar Designer
+Stefan Kristiansson
+Stefan O'Rear
+Szabolcs Nagy
+Timo Teräs
+Trutz Behn
+Valentin Ochs
+Will Dietz
+William Haddon
+William Pitcock
+
+Portions of this software are derived from third-party works licensed
+under terms compatible with the above MIT license:
+
+The TRE regular expression implementation (src/regex/reg* and
+src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed
+under a 2-clause BSD license (license text in the source files). The
+included version has been heavily modified by Rich Felker in 2012, in
+the interests of size, simplicity, and namespace cleanliness.
+
+Much of the math library code (src/math/* and src/complex/*) is
+Copyright © 1993,2004 Sun Microsystems or
+Copyright © 2003-2011 David Schultz or
+Copyright © 2003-2009 Steven G. Kargl or
+Copyright © 2003-2009 Bruce D. Evans or
+Copyright © 2008 Stephen L. Moshier or
+Copyright © 2017-2018 Arm Limited
+and labelled as such in comments in the individual source files. All
+have been licensed under extremely permissive terms.
+
+The ARM memcpy code (src/string/arm/memcpy.S) is Copyright © 2008
+The Android Open Source Project and is licensed under a two-clause BSD
+license. It was taken from Bionic libc, used on Android.
+
+The AArch64 memcpy and memset code (src/string/aarch64/*) are
+Copyright © 1999-2019, Arm Limited.
+
+The implementation of DES for crypt (src/crypt/crypt_des.c) is
+Copyright © 1994 David Burren. It is licensed under a BSD license.
+
+The implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was
+originally written by Solar Designer and placed into the public
+domain. The code also comes with a fallback permissive license for use
+in jurisdictions that may not recognize the public domain.
+
+The smoothsort implementation (src/stdlib/qsort.c) is Copyright © 2011
+Valentin Ochs and is licensed under an MIT-style license.
+
+The x86_64 port was written by Nicholas J. Kain and is licensed under
+the standard MIT terms.
+
+The mips and microblaze ports were originally written by Richard
+Pennington for use in the ellcc project. The original code was adapted
+by Rich Felker for build system and code conventions during upstream
+integration. It is licensed under the standard MIT terms.
+
+The mips64 port was contributed by Imagination Technologies and is
+licensed under the standard MIT terms.
+
+The powerpc port was also originally written by Richard Pennington,
+and later supplemented and integrated by John Spencer. It is licensed
+under the standard MIT terms.
+
+All other files which have no copyright comments are original works
+produced specifically for use as part of this library, written either
+by Rich Felker, the main author of the library, or by one or more
+contibutors listed above. Details on authorship of individual files
+can be found in the git version control history of the project. The
+omission of copyright and license comments in each file is in the
+interest of source tree size.
+
+In addition, permission is hereby granted for all public header files
+(include/* and arch/*/bits/*) and crt files intended to be linked into
+applications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit
+the copyright notice and permission notice otherwise required by the
+license, and to use these files without any requirement of
+attribution. These files include substantial contributions from:
+
+Bobby Bingham
+John Spencer
+Nicholas J. Kain
+Rich Felker
+Richard Pennington
+Stefan Kristiansson
+Szabolcs Nagy
+
+all of whom have explicitly granted such permission.
+
+This file previously contained text expressing a belief that most of
+the files covered by the above exception were sufficiently trivial not
+to be subject to copyright, resulting in confusion over whether it
+negated the permissions granted in the license. In the spirit of
+permissive licensing, and of not having licensing issues being an
+obstacle to adoption, that text has been removed.
diff --git a/lib/posix-environ/clearenv.c b/lib/posix-environ/clearenv.c
new file mode 100644 (file)
index 0000000..15d302f
--- /dev/null
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: MIT */
+/* Copyright © 2005-2020 Rich Felker, et al.
+ * Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the MIT License (the "License", see COPYING.md).
+ * You may not use this file except in compliance with the License.
+ */
+/*
+ * The code in this file was derived from musl 1.2.3:
+ * Source: https://www.musl-libc.org/
+ * File: src/env/clearenv.c
+ */
+
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <unistd.h>
+
+static void dummy(char *old, char *new) {}
+weak_alias(dummy, __env_rm_add);
+
+int clearenv()
+{
+       char **e = __environ;
+       __environ = 0;
+       if (e) while (*e) __env_rm_add(*e++, 0);
+       return 0;
+}
diff --git a/lib/posix-environ/getenv.c b/lib/posix-environ/getenv.c
new file mode 100644 (file)
index 0000000..ae9b307
--- /dev/null
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: MIT */
+/* Copyright © 2005-2020 Rich Felker, et al.
+ * Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the MIT License (the "License", see COPYING.md).
+ * You may not use this file except in compliance with the License.
+ */
+/*
+ * The code in this file was derived from musl 1.2.3:
+ * Source: https://www.musl-libc.org/
+ * File: src/env/getenv.c
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char *getenv(const char *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] == '=')
+                               return *e + l+1;
+       return 0;
+}
diff --git a/lib/posix-environ/putenv.c b/lib/posix-environ/putenv.c
new file mode 100644 (file)
index 0000000..dfa7b69
--- /dev/null
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: MIT */
+/* Copyright © 2005-2020 Rich Felker, et al.
+ * Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the MIT License (the "License", see COPYING.md).
+ * You may not use this file except in compliance with the License.
+ */
+/*
+ * The code in this file was derived from musl 1.2.3:
+ * Source: https://www.musl-libc.org/
+ * File: src/env/putenv.c
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void dummy(char *old, char *new) {}
+weak_alias(dummy, __env_rm_add);
+
+int __putenv(char *s, size_t l, char *r)
+{
+       size_t i=0;
+       if (__environ) {
+               for (char **e = __environ; *e; e++, i++)
+                       if (!strncmp(s, *e, l+1)) {
+                               char *tmp = *e;
+                               *e = s;
+                               __env_rm_add(tmp, r);
+                               return 0;
+                       }
+       }
+       static char **oldenv;
+       char **newenv;
+       if (__environ == oldenv) {
+               newenv = realloc(oldenv, sizeof *newenv * (i+2));
+               if (!newenv) goto oom;
+       } else {
+               newenv = malloc(sizeof *newenv * (i+2));
+               if (!newenv) goto oom;
+               if (i) memcpy(newenv, __environ, sizeof *newenv * i);
+               free(oldenv);
+       }
+       newenv[i] = s;
+       newenv[i+1] = 0;
+       __environ = oldenv = newenv;
+       if (r) __env_rm_add(0, r);
+       return 0;
+oom:
+       free(r);
+       return -1;
+}
+
+int putenv(char *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
new file mode 100644 (file)
index 0000000..bfcb13c
--- /dev/null
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: MIT */
+/* Copyright © 2005-2020 Rich Felker, et al.
+ * Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the MIT License (the "License", see COPYING.md).
+ * You may not use this file except in compliance with the License.
+ */
+/*
+ * The code in this file was derived from musl 1.2.3:
+ * Source: https://www.musl-libc.org/
+ * File: src/env/setenv.c
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+void __env_rm_add(char *old, char *new)
+{
+       static char **env_alloced;
+       static size_t env_alloced_n;
+       for (size_t i=0; i < env_alloced_n; i++)
+               if (env_alloced[i] == old) {
+                       env_alloced[i] = new;
+                       free(old);
+                       return;
+               } else if (!env_alloced[i] && new) {
+                       env_alloced[i] = new;
+                       new = 0;
+               }
+       if (!new) return;
+       char **t = realloc(env_alloced, sizeof *t * (env_alloced_n+1));
+       if (!t) return;
+       (env_alloced = t)[env_alloced_n++] = new;
+}
+
+int setenv(const char *var, const char *value, int overwrite)
+{
+       char *s;
+       size_t l1, l2;
+
+       if (!var || !(l1 = __strchrnul(var, '=') - var) || var[l1]) {
+               errno = EINVAL;
+               return -1;
+       }
+       if (!overwrite && getenv(var)) return 0;
+
+       l2 = strlen(value);
+       s = malloc(l1+l2+2);
+       if (!s) return -1;
+       memcpy(s, var, l1);
+       s[l1] = '=';
+       memcpy(s+l1+1, value, l2+1);
+       return __putenv(s, l1, s);
+}
diff --git a/lib/posix-environ/unsetenv.c b/lib/posix-environ/unsetenv.c
new file mode 100644 (file)
index 0000000..99c045d
--- /dev/null
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/* Copyright © 2005-2020 Rich Felker, et al.
+ * Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the MIT License (the "License", see COPYING.md).
+ * You may not use this file except in compliance with the License.
+ */
+/*
+ * The code in this file was derived from musl 1.2.3:
+ * Source: https://www.musl-libc.org/
+ * File: src/env/unsetenv.c
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+static void dummy(char *old, char *new) {}
+weak_alias(dummy, __env_rm_add);
+
+int unsetenv(const char *name)
+{
+       size_t l = __strchrnul(name, '=') - name;
+       if (!l || name[l]) {
+               errno = EINVAL;
+               return -1;
+       }
+       if (__environ) {
+               char **e = __environ, **eo = e;
+               for (; *e; e++)
+                       if (!strncmp(name, *e, l) && l[*e] == '=')
+                               __env_rm_add(*e, 0);
+                       else if (eo != e)
+                               *eo++ = *e;
+                       else
+                               eo++;
+               if (eo != e) *eo = 0;
+       }
+       return 0;
+}