From: Simon Kuenzer Date: Wed, 8 Feb 2023 16:01:41 +0000 (+0100) Subject: lib/posix-environ: Import from musl 1.2.3 X-Git-Tag: RELEASE-0.13.0~56 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=e9b7d00e5407dc0c4cfeb912b2f3329cdc5ff28b;p=unikraft%2Funikraft.git lib/posix-environ: Import from musl 1.2.3 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 Reviewed-by: Delia Pavel Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #868 --- diff --git a/lib/posix-environ/COPYING b/lib/posix-environ/COPYING new file mode 100644 index 000000000..c1628e9ac --- /dev/null +++ b/lib/posix-environ/COPYING @@ -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 index 000000000..15d302fa1 --- /dev/null +++ b/lib/posix-environ/clearenv.c @@ -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 +#include + +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 index 000000000..ae9b30788 --- /dev/null +++ b/lib/posix-environ/getenv.c @@ -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 +#include +#include + +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 index 000000000..dfa7b6921 --- /dev/null +++ b/lib/posix-environ/putenv.c @@ -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 +#include +#include + +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 index 000000000..bfcb13cd6 --- /dev/null +++ b/lib/posix-environ/setenv.c @@ -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 +#include +#include + +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 index 000000000..99c045dbf --- /dev/null +++ b/lib/posix-environ/unsetenv.c @@ -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 +#include +#include +#include + +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; +}