2025-01-01 10:14:45 -08:00
|
|
|
/* Copyright (C) 1999-2025 Free Software Foundation, Inc.
|
1999-10-12 16:00:04 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1999-10-12 16:00:04 +00:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1999-10-12 16:00:04 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-06 22:40:42 -07:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
1999-10-12 16:00:04 +00:00
|
|
|
|
2004-06-09 18:33:36 +00:00
|
|
|
#include <errno.h>
|
1999-07-29 20:36:56 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-10-12 16:00:04 +00:00
|
|
|
#include <string.h>
|
Split DIAG_* macros to new header libc-diag.h.
Quite a few tests include libc-internal.h just for the DIAG_* macros.
Split those macros to their own file, which can be included safely in
_ISOMAC mode. I also moved ignore_value, since it seems logically
related, even though I didn't notice any tests needing it.
Also add -Wnonnull suppressions to two tests that _should_ have them,
but the error is masked when compiling against internal headers.
* include/libc-diag.h: New file. Define ignore_value,
DIAG_PUSH_NEEDS_COMMENT, DIAG_POP_NEEDS_COMMENT,
DIAG_IGNORE_NEEDS_COMMENT, and DIAG_IGNORE_Os_NEEDS_COMMENT here.
* include/libc-internal.h: Definitions of above macros moved from
here. Include libc-diag.h. Add copyright notice.
* malloc/tst-malloc.c, malloc/tst-memcheck.c, malloc/tst-realloc.c
* misc/tst-error1.c, posix/tst-dir.c, stdio-common/bug21.c
* stdio-common/scanf14.c, stdio-common/scanf4.c, stdio-common/scanf7.c
* stdio-common/test-vfprintf.c, stdio-common/tst-printf.c
* stdio-common/tst-printfsz.c, stdio-common/tst-sprintf.c
* stdio-common/tst-unlockedio.c, stdio-common/tstdiomisc.c
* stdlib/bug-getcontext.c, string/tester.c, string/tst-endian.c
* time/tst-strptime2.c, wcsmbs/tst-wcstof.c:
Include libc-diag.h instead of libc-internal.h.
* stdlib/tst-environ.c: Include libc-diag.h. Suppress -Wnonnull for
call to unsetenv (NULL).
* nptl/tst-mutex1.c: Include libc-diag.h. Suppress -Wnonnull for
call to pthread_mutexattr_destroy (NULL).
2016-11-20 20:46:30 -05:00
|
|
|
#include <libc-diag.h>
|
stdlib: Make getenv thread-safe in more cases
Async-signal-safety is preserved, too. In fact, getenv is fully
reentrant and can be called from the malloc call in setenv
(if a replacement malloc uses getenv during its initialization).
This is relatively easy to implement because even before this change,
setenv, unsetenv, clearenv, putenv do not deallocate the environment
strings themselves as they are removed from the environment.
The main changes are:
* Use release stores for environment array updates, following
the usual pattern for safely publishing immutable data
(in this case, the environment strings).
* Do not deallocate the environment array. Instead, keep older
versions around and adopt an exponential resizing policy. This
results in an amortized constant space leak per active environment
variable, but there already is such a leak for the variable itself
(and that is even length-dependent, and includes no-longer used
values).
* Add a seqlock-like mechanism to retry getenv if a concurrent
unsetenv is observed. Without that, it is possible that
getenv returns NULL for a variable that is never unset. This
is visible on some AArch64 implementations with the newly
added stdlib/tst-getenv-unsetenv test case. The mechanism
is not a pure seqlock because it tolerates one write from
unsetenv. This avoids the need for a second copy of the
environ array that getenv can read from a signal handler
that happens to interrupt an unsetenv call.
No manual updates are included with this patch because environ
usage with execve, posix_spawn, system is still not thread-safe
relative unsetenv. The new process may end up with an environment
that misses entries that were never unset. This is the same issue
described above for getenv.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-11-21 21:10:52 +01:00
|
|
|
#include <support/check.h>
|
1999-07-29 20:36:56 +00:00
|
|
|
|
|
|
|
#define VAR "FOOBAR"
|
|
|
|
|
|
|
|
char putenv_val[100] = VAR "=some longer value";
|
|
|
|
|
2014-11-05 15:24:08 +05:30
|
|
|
static int
|
|
|
|
do_test (void)
|
1999-07-29 20:36:56 +00:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
const char *valp;
|
|
|
|
|
|
|
|
/* First test: remove entry FOOBAR, whether it exists or not. */
|
|
|
|
unsetenv (VAR);
|
|
|
|
|
|
|
|
/* Now getting the value should fail. */
|
|
|
|
if (getenv (VAR) != NULL)
|
|
|
|
{
|
|
|
|
printf ("There should be no `%s' value\n", VAR);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now add a value, with the replace flag cleared. */
|
|
|
|
if (setenv (VAR, "one", 0) != 0)
|
|
|
|
{
|
|
|
|
printf ("setenv #1 failed: %m\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Getting this value should now be possible. */
|
|
|
|
valp = getenv (VAR);
|
stdlib: Make getenv thread-safe in more cases
Async-signal-safety is preserved, too. In fact, getenv is fully
reentrant and can be called from the malloc call in setenv
(if a replacement malloc uses getenv during its initialization).
This is relatively easy to implement because even before this change,
setenv, unsetenv, clearenv, putenv do not deallocate the environment
strings themselves as they are removed from the environment.
The main changes are:
* Use release stores for environment array updates, following
the usual pattern for safely publishing immutable data
(in this case, the environment strings).
* Do not deallocate the environment array. Instead, keep older
versions around and adopt an exponential resizing policy. This
results in an amortized constant space leak per active environment
variable, but there already is such a leak for the variable itself
(and that is even length-dependent, and includes no-longer used
values).
* Add a seqlock-like mechanism to retry getenv if a concurrent
unsetenv is observed. Without that, it is possible that
getenv returns NULL for a variable that is never unset. This
is visible on some AArch64 implementations with the newly
added stdlib/tst-getenv-unsetenv test case. The mechanism
is not a pure seqlock because it tolerates one write from
unsetenv. This avoids the need for a second copy of the
environ array that getenv can read from a signal handler
that happens to interrupt an unsetenv call.
No manual updates are included with this patch because environ
usage with execve, posix_spawn, system is still not thread-safe
relative unsetenv. The new process may end up with an environment
that misses entries that were never unset. This is the same issue
described above for getenv.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-11-21 21:10:52 +01:00
|
|
|
TEST_COMPARE_STRING (valp, "one");
|
1999-07-29 20:36:56 +00:00
|
|
|
|
|
|
|
/* Try to replace without the replace flag set. This should fail. */
|
|
|
|
if (setenv (VAR, "two", 0) != 0)
|
|
|
|
{
|
|
|
|
printf ("setenv #2 failed: %m\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The value shouldn't have changed. */
|
|
|
|
valp = getenv (VAR);
|
stdlib: Make getenv thread-safe in more cases
Async-signal-safety is preserved, too. In fact, getenv is fully
reentrant and can be called from the malloc call in setenv
(if a replacement malloc uses getenv during its initialization).
This is relatively easy to implement because even before this change,
setenv, unsetenv, clearenv, putenv do not deallocate the environment
strings themselves as they are removed from the environment.
The main changes are:
* Use release stores for environment array updates, following
the usual pattern for safely publishing immutable data
(in this case, the environment strings).
* Do not deallocate the environment array. Instead, keep older
versions around and adopt an exponential resizing policy. This
results in an amortized constant space leak per active environment
variable, but there already is such a leak for the variable itself
(and that is even length-dependent, and includes no-longer used
values).
* Add a seqlock-like mechanism to retry getenv if a concurrent
unsetenv is observed. Without that, it is possible that
getenv returns NULL for a variable that is never unset. This
is visible on some AArch64 implementations with the newly
added stdlib/tst-getenv-unsetenv test case. The mechanism
is not a pure seqlock because it tolerates one write from
unsetenv. This avoids the need for a second copy of the
environ array that getenv can read from a signal handler
that happens to interrupt an unsetenv call.
No manual updates are included with this patch because environ
usage with execve, posix_spawn, system is still not thread-safe
relative unsetenv. The new process may end up with an environment
that misses entries that were never unset. This is the same issue
described above for getenv.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-11-21 21:10:52 +01:00
|
|
|
TEST_COMPARE_STRING (valp, "one");
|
1999-07-29 20:36:56 +00:00
|
|
|
|
|
|
|
/* Now replace the value using putenv. */
|
|
|
|
if (putenv (putenv_val) != 0)
|
|
|
|
{
|
|
|
|
printf ("putenv #1 failed: %m\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The value should have changed now. */
|
|
|
|
valp = getenv (VAR);
|
|
|
|
if (valp == NULL || strcmp (valp, "some longer value") != 0)
|
|
|
|
{
|
|
|
|
printf ("getenv #4 failed (is \"%s\")\n", valp);
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now one tricky check: changing the variable passed in putenv should
|
|
|
|
change the environment. */
|
|
|
|
strcpy (&putenv_val[sizeof VAR], "a short one");
|
|
|
|
|
|
|
|
/* The value should have changed again. */
|
|
|
|
valp = getenv (VAR);
|
|
|
|
if (valp == NULL || strcmp (valp, "a short one") != 0)
|
|
|
|
{
|
|
|
|
puts ("getenv #5 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It should even be possible to rename the variable. */
|
|
|
|
strcpy (putenv_val, "XYZZY=some other value");
|
|
|
|
|
|
|
|
/* Now a lookup using the old name should fail. */
|
|
|
|
if (getenv (VAR) != NULL)
|
|
|
|
{
|
|
|
|
puts ("getenv #6 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* But using the new name it should work. */
|
|
|
|
valp = getenv ("XYZZY");
|
|
|
|
if (valp == NULL || strcmp (valp, "some other value") != 0)
|
|
|
|
{
|
|
|
|
puts ("getenv #7 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new variable with the old name. */
|
|
|
|
if (setenv (VAR, "a new value", 0) != 0)
|
|
|
|
{
|
|
|
|
printf ("setenv #3 failed: %m\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point a getenv call must return the new value. */
|
|
|
|
valp = getenv (VAR);
|
|
|
|
if (valp == NULL || strcmp (valp, "a new value") != 0)
|
|
|
|
{
|
|
|
|
puts ("getenv #8 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Black magic: rename the variable we added using putenv back. */
|
|
|
|
strcpy (putenv_val, VAR "=old name new value");
|
|
|
|
|
|
|
|
/* This is interesting. We have two variables with the same name.
|
|
|
|
Getting a value should return one of them. */
|
|
|
|
valp = getenv (VAR);
|
|
|
|
if (valp == NULL
|
|
|
|
|| (strcmp (valp, "a new value") != 0
|
|
|
|
&& strcmp (valp, "old name new value") != 0))
|
|
|
|
{
|
|
|
|
puts ("getenv #9 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* More fun ahead: we are now removing the variable. This should remove
|
1999-08-30 20:15:06 +00:00
|
|
|
both values. The cast is ok: this call should never put the string
|
|
|
|
in the environment and it should never modify it. */
|
|
|
|
putenv ((char *) VAR);
|
1999-07-29 20:36:56 +00:00
|
|
|
|
|
|
|
/* Getting the value should now fail. */
|
|
|
|
if (getenv (VAR) != NULL)
|
|
|
|
{
|
|
|
|
printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR));
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
1999-09-20 02:18:14 +00:00
|
|
|
/* Now a test with an environment variable that's one character long.
|
|
|
|
This is to test a special case in the getenv implementation. */
|
|
|
|
strcpy (putenv_val, "X=one character test");
|
|
|
|
if (putenv (putenv_val) != 0)
|
|
|
|
{
|
|
|
|
printf ("putenv #2 failed: %m\n");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
valp = getenv ("X");
|
|
|
|
if (valp == NULL || strcmp (valp, "one character test") != 0)
|
|
|
|
{
|
|
|
|
puts ("getenv #11 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
2004-06-09 18:33:36 +00:00
|
|
|
/* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
|
|
|
|
or if name contains '=' character. */
|
|
|
|
errno = 0;
|
|
|
|
if (setenv (NULL, "val", 1) >= 0 || errno != EINVAL)
|
|
|
|
{
|
|
|
|
puts ("setenv #4 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
|
|
|
|
{
|
|
|
|
puts ("setenv #5 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
|
|
|
|
{
|
|
|
|
puts ("setenv #6 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
Split DIAG_* macros to new header libc-diag.h.
Quite a few tests include libc-internal.h just for the DIAG_* macros.
Split those macros to their own file, which can be included safely in
_ISOMAC mode. I also moved ignore_value, since it seems logically
related, even though I didn't notice any tests needing it.
Also add -Wnonnull suppressions to two tests that _should_ have them,
but the error is masked when compiling against internal headers.
* include/libc-diag.h: New file. Define ignore_value,
DIAG_PUSH_NEEDS_COMMENT, DIAG_POP_NEEDS_COMMENT,
DIAG_IGNORE_NEEDS_COMMENT, and DIAG_IGNORE_Os_NEEDS_COMMENT here.
* include/libc-internal.h: Definitions of above macros moved from
here. Include libc-diag.h. Add copyright notice.
* malloc/tst-malloc.c, malloc/tst-memcheck.c, malloc/tst-realloc.c
* misc/tst-error1.c, posix/tst-dir.c, stdio-common/bug21.c
* stdio-common/scanf14.c, stdio-common/scanf4.c, stdio-common/scanf7.c
* stdio-common/test-vfprintf.c, stdio-common/tst-printf.c
* stdio-common/tst-printfsz.c, stdio-common/tst-sprintf.c
* stdio-common/tst-unlockedio.c, stdio-common/tstdiomisc.c
* stdlib/bug-getcontext.c, string/tester.c, string/tst-endian.c
* time/tst-strptime2.c, wcsmbs/tst-wcstof.c:
Include libc-diag.h instead of libc-internal.h.
* stdlib/tst-environ.c: Include libc-diag.h. Suppress -Wnonnull for
call to unsetenv (NULL).
* nptl/tst-mutex1.c: Include libc-diag.h. Suppress -Wnonnull for
call to pthread_mutexattr_destroy (NULL).
2016-11-20 20:46:30 -05:00
|
|
|
/* This deliberately tests supplying a null pointer to a function whose
|
|
|
|
argument is marked __attribute__ ((nonnull)). */
|
|
|
|
DIAG_PUSH_NEEDS_COMMENT;
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT(5, "-Wnonnull");
|
2004-06-09 18:33:36 +00:00
|
|
|
errno = 0;
|
|
|
|
if (unsetenv (NULL) >= 0 || errno != EINVAL)
|
|
|
|
{
|
|
|
|
puts ("unsetenv #1 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
Split DIAG_* macros to new header libc-diag.h.
Quite a few tests include libc-internal.h just for the DIAG_* macros.
Split those macros to their own file, which can be included safely in
_ISOMAC mode. I also moved ignore_value, since it seems logically
related, even though I didn't notice any tests needing it.
Also add -Wnonnull suppressions to two tests that _should_ have them,
but the error is masked when compiling against internal headers.
* include/libc-diag.h: New file. Define ignore_value,
DIAG_PUSH_NEEDS_COMMENT, DIAG_POP_NEEDS_COMMENT,
DIAG_IGNORE_NEEDS_COMMENT, and DIAG_IGNORE_Os_NEEDS_COMMENT here.
* include/libc-internal.h: Definitions of above macros moved from
here. Include libc-diag.h. Add copyright notice.
* malloc/tst-malloc.c, malloc/tst-memcheck.c, malloc/tst-realloc.c
* misc/tst-error1.c, posix/tst-dir.c, stdio-common/bug21.c
* stdio-common/scanf14.c, stdio-common/scanf4.c, stdio-common/scanf7.c
* stdio-common/test-vfprintf.c, stdio-common/tst-printf.c
* stdio-common/tst-printfsz.c, stdio-common/tst-sprintf.c
* stdio-common/tst-unlockedio.c, stdio-common/tstdiomisc.c
* stdlib/bug-getcontext.c, string/tester.c, string/tst-endian.c
* time/tst-strptime2.c, wcsmbs/tst-wcstof.c:
Include libc-diag.h instead of libc-internal.h.
* stdlib/tst-environ.c: Include libc-diag.h. Suppress -Wnonnull for
call to unsetenv (NULL).
* nptl/tst-mutex1.c: Include libc-diag.h. Suppress -Wnonnull for
call to pthread_mutexattr_destroy (NULL).
2016-11-20 20:46:30 -05:00
|
|
|
DIAG_POP_NEEDS_COMMENT;
|
2004-06-09 18:33:36 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (unsetenv ("") >= 0 || errno != EINVAL)
|
|
|
|
{
|
|
|
|
puts ("unsetenv #2 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (unsetenv ("x=y") >= 0 || errno != EINVAL)
|
|
|
|
{
|
|
|
|
puts ("unsetenv #3 failed");
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
|
1999-07-29 20:36:56 +00:00
|
|
|
return result;
|
|
|
|
}
|
2014-11-05 15:24:08 +05:30
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|