2018-07-06 05:10:41 +00:00
|
|
|
|
/* Run a test case in an isolated namespace.
|
2023-01-06 21:08:04 +00:00
|
|
|
|
Copyright (C) 2018-2023 Free Software Foundation, Inc.
|
2018-07-06 05:10:41 +00:00
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
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-07 05:40:42 +00:00
|
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
#include <array_length.h>
|
2018-07-06 05:10:41 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <utime.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <error.h>
|
2018-11-02 14:36:09 +00:00
|
|
|
|
#include <libc-pointer-arith.h>
|
2023-09-27 19:20:13 +00:00
|
|
|
|
#include <ftw.h>
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <support/support.h>
|
|
|
|
|
#include <support/xunistd.h>
|
2021-01-29 13:30:19 +00:00
|
|
|
|
#include <support/capture_subprocess.h>
|
2018-07-06 05:10:41 +00:00
|
|
|
|
#include "check.h"
|
|
|
|
|
#include "test-driver.h"
|
|
|
|
|
|
|
|
|
|
#ifndef __linux__
|
|
|
|
|
#define mount(s,t,fs,f,d) no_mount()
|
|
|
|
|
int no_mount (void)
|
|
|
|
|
{
|
|
|
|
|
FAIL_UNSUPPORTED("mount not supported; port needed");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int verbose = 0;
|
|
|
|
|
|
|
|
|
|
/* Running a test in a container is tricky. There are two main
|
|
|
|
|
categories of things to do:
|
|
|
|
|
|
|
|
|
|
1. "Once" actions, like setting up the container and doing an
|
|
|
|
|
install into it.
|
|
|
|
|
|
|
|
|
|
2. "Per-test" actions, like copying in support files and
|
|
|
|
|
configuring the container.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"Once" actions:
|
|
|
|
|
|
|
|
|
|
* mkdir $buildroot/testroot.pristine/
|
|
|
|
|
* install into it
|
2020-01-23 14:45:00 +00:00
|
|
|
|
* default glibc install
|
|
|
|
|
* create /bin for /bin/sh
|
|
|
|
|
* create $(complocaledir) so localedef tests work with default paths.
|
|
|
|
|
* install /bin/sh, /bin/echo, and /bin/true.
|
2018-07-06 05:10:41 +00:00
|
|
|
|
* rsync to $buildroot/testroot.root/
|
|
|
|
|
|
|
|
|
|
"Per-test" actions:
|
|
|
|
|
* maybe rsync to $buildroot/testroot.root/
|
|
|
|
|
* copy support files and test binary
|
|
|
|
|
* chroot/unshare
|
|
|
|
|
* set up any mounts (like /proc)
|
2021-01-29 13:30:19 +00:00
|
|
|
|
* run ldconfig
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
Magic files:
|
|
|
|
|
|
|
|
|
|
For test $srcdir/foo/mytest.c we look for $srcdir/foo/mytest.root
|
|
|
|
|
and, if found...
|
|
|
|
|
|
|
|
|
|
* mytest.root/ is rsync'd into container
|
|
|
|
|
* mytest.root/preclean.req causes fresh rsync (with delete) before
|
|
|
|
|
test if present
|
2018-12-04 05:03:12 +00:00
|
|
|
|
* mytest.root/mytest.script has a list of "commands" to run:
|
2018-07-06 05:10:41 +00:00
|
|
|
|
syntax:
|
|
|
|
|
# comment
|
2022-03-29 03:53:33 +00:00
|
|
|
|
pidns <comment>
|
2018-12-04 05:03:12 +00:00
|
|
|
|
su
|
2018-07-06 05:10:41 +00:00
|
|
|
|
mv FILE FILE
|
|
|
|
|
cp FILE FILE
|
|
|
|
|
rm FILE
|
2020-02-03 19:49:25 +00:00
|
|
|
|
cwd PATH
|
|
|
|
|
exec FILE
|
2020-01-23 14:45:00 +00:00
|
|
|
|
mkdirp MODE DIR
|
|
|
|
|
|
|
|
|
|
variables:
|
|
|
|
|
$B/ build dir, equivalent to $(common-objpfx)
|
|
|
|
|
$S/ source dir, equivalent to $(srcdir)
|
|
|
|
|
$I/ install dir, equivalent to $(prefix)
|
|
|
|
|
$L/ library dir (in container), equivalent to $(libdir)
|
|
|
|
|
$complocaledir/ compiled locale dir, equivalent to $(complocaledir)
|
|
|
|
|
/ container's root
|
|
|
|
|
|
|
|
|
|
If FILE begins with any of these variables then they will be
|
|
|
|
|
substituted for the described value.
|
|
|
|
|
|
|
|
|
|
The goal is to expose as many of the runtime's configured paths
|
|
|
|
|
via variables so they can be used to setup the container environment
|
|
|
|
|
before execution reaches the test.
|
|
|
|
|
|
2018-12-04 05:03:12 +00:00
|
|
|
|
details:
|
|
|
|
|
- '#': A comment.
|
2022-03-29 03:53:33 +00:00
|
|
|
|
- 'pidns': Require a separate PID namespace, prints comment if it can't
|
|
|
|
|
(default is a shared pid namespace)
|
2018-12-04 05:03:12 +00:00
|
|
|
|
- 'su': Enables running test as root in the container.
|
|
|
|
|
- 'mv': A minimal move files command.
|
|
|
|
|
- 'cp': A minimal copy files command.
|
|
|
|
|
- 'rm': A minimal remove files command.
|
2020-02-03 19:49:25 +00:00
|
|
|
|
- 'cwd': set test working directory
|
|
|
|
|
- 'exec': change test binary location (may end in /)
|
2020-01-23 14:45:00 +00:00
|
|
|
|
- 'mkdirp': A minimal "mkdir -p FILE" command.
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
* mytest.root/postclean.req causes fresh rsync (with delete) after
|
|
|
|
|
test if present
|
|
|
|
|
|
2021-01-29 13:30:19 +00:00
|
|
|
|
* mytest.root/ldconfig.run causes ldconfig to be issued prior
|
|
|
|
|
test execution (to setup the initial ld.so.cache).
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
Note that $srcdir/foo/mytest.script may be used instead of a
|
|
|
|
|
$srcdir/foo/mytest.root/mytest.script in the sysroot template, if
|
|
|
|
|
there is no other reason for a sysroot.
|
|
|
|
|
|
|
|
|
|
Design goals:
|
|
|
|
|
|
|
|
|
|
* independent of other packages which may not be installed (like
|
|
|
|
|
rsync or Docker, or even "cp")
|
|
|
|
|
|
|
|
|
|
* Simple, easy to review code (i.e. prefer simple naive code over
|
|
|
|
|
complex efficient code)
|
|
|
|
|
|
2022-03-29 03:53:33 +00:00
|
|
|
|
* The current implementation is parallel-make-safe, but only in
|
2018-07-06 05:10:41 +00:00
|
|
|
|
that it uses a lock to prevent parallel access to the testroot. */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Utility Functions */
|
|
|
|
|
|
|
|
|
|
/* Like xunlink, but it's OK if the file already doesn't exist. */
|
|
|
|
|
void
|
|
|
|
|
maybe_xunlink (const char *path)
|
|
|
|
|
{
|
|
|
|
|
int rv = unlink (path);
|
|
|
|
|
if (rv < 0 && errno != ENOENT)
|
|
|
|
|
FAIL_EXIT1 ("unlink (\"%s\"): %m", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Like xmkdir, but it's OK if the directory already exists. */
|
|
|
|
|
void
|
|
|
|
|
maybe_xmkdir (const char *path, mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
|
|
if (stat (path, &st) == 0
|
|
|
|
|
&& S_ISDIR (st.st_mode))
|
|
|
|
|
return;
|
|
|
|
|
xmkdir (path, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Temporarily concatenate multiple strings into one. Allows up to 10
|
2020-02-03 19:49:25 +00:00
|
|
|
|
temporary results; use xstrdup () if you need them to be
|
2018-07-06 05:10:41 +00:00
|
|
|
|
permanent. */
|
|
|
|
|
static char *
|
|
|
|
|
concat (const char *str, ...)
|
|
|
|
|
{
|
|
|
|
|
/* Assume initialized to NULL/zero. */
|
|
|
|
|
static char *bufs[10];
|
|
|
|
|
static size_t buflens[10];
|
|
|
|
|
static int bufn = 0;
|
|
|
|
|
int n;
|
|
|
|
|
size_t len;
|
|
|
|
|
va_list ap, ap2;
|
|
|
|
|
char *cp;
|
|
|
|
|
char *next;
|
|
|
|
|
|
|
|
|
|
va_start (ap, str);
|
|
|
|
|
va_copy (ap2, ap);
|
|
|
|
|
|
|
|
|
|
n = bufn;
|
|
|
|
|
bufn = (bufn + 1) % 10;
|
|
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
|
|
while ((next = va_arg (ap, char *)) != NULL)
|
|
|
|
|
len = len + strlen (next);
|
|
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
if (bufs[n] == NULL)
|
|
|
|
|
{
|
|
|
|
|
bufs[n] = xmalloc (len + 1); /* NUL */
|
|
|
|
|
buflens[n] = len + 1;
|
|
|
|
|
}
|
|
|
|
|
else if (buflens[n] < len + 1)
|
|
|
|
|
{
|
|
|
|
|
bufs[n] = xrealloc (bufs[n], len + 1); /* NUL */
|
|
|
|
|
buflens[n] = len + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strcpy (bufs[n], str);
|
|
|
|
|
cp = strchr (bufs[n], '\0');
|
|
|
|
|
while ((next = va_arg (ap2, char *)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
strcpy (cp, next);
|
|
|
|
|
cp = strchr (cp, '\0');
|
|
|
|
|
}
|
|
|
|
|
*cp = 0;
|
|
|
|
|
va_end (ap2);
|
|
|
|
|
|
|
|
|
|
return bufs[n];
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 19:45:35 +00:00
|
|
|
|
#ifdef CLONE_NEWNS
|
2022-03-29 03:53:33 +00:00
|
|
|
|
/* Like the above, but put spaces between words. Caller frees. */
|
|
|
|
|
static char *
|
|
|
|
|
concat_words (char **words, int num_words)
|
|
|
|
|
{
|
|
|
|
|
int len = 0;
|
|
|
|
|
int i;
|
|
|
|
|
char *rv, *p;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_words; i ++)
|
|
|
|
|
{
|
|
|
|
|
len += strlen (words[i]);
|
|
|
|
|
len ++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = rv = (char *) xmalloc (len);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_words; i ++)
|
|
|
|
|
{
|
|
|
|
|
if (i > 0)
|
|
|
|
|
p = stpcpy (p, " ");
|
|
|
|
|
p = stpcpy (p, words[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2022-04-11 19:45:35 +00:00
|
|
|
|
#endif
|
2022-03-29 03:53:33 +00:00
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
/* Try to mount SRC onto DEST. */
|
|
|
|
|
static void
|
|
|
|
|
trymount (const char *src, const char *dest)
|
|
|
|
|
{
|
2022-03-29 03:53:33 +00:00
|
|
|
|
if (mount (src, dest, "", MS_BIND | MS_REC, NULL) < 0)
|
2018-07-06 05:10:41 +00:00
|
|
|
|
FAIL_EXIT1 ("can't mount %s onto %s\n", src, dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Special case of above for devices like /dev/zero where we have to
|
|
|
|
|
mount a device over a device, not a directory over a directory. */
|
|
|
|
|
static void
|
|
|
|
|
devmount (const char *new_root_path, const char *which)
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
fd = open (concat (new_root_path, "/dev/", which, NULL),
|
|
|
|
|
O_CREAT | O_TRUNC | O_RDWR, 0777);
|
|
|
|
|
xclose (fd);
|
|
|
|
|
|
|
|
|
|
trymount (concat ("/dev/", which, NULL),
|
|
|
|
|
concat (new_root_path, "/dev/", which, NULL));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 13:37:47 +00:00
|
|
|
|
/* Returns true if the string "looks like" an environment variable
|
2018-07-06 05:10:41 +00:00
|
|
|
|
being set. */
|
|
|
|
|
static int
|
|
|
|
|
is_env_setting (const char *a)
|
|
|
|
|
{
|
|
|
|
|
int count_name = 0;
|
|
|
|
|
|
|
|
|
|
while (*a)
|
|
|
|
|
{
|
|
|
|
|
if (isalnum (*a) || *a == '_')
|
|
|
|
|
++count_name;
|
|
|
|
|
else if (*a == '=' && count_name > 0)
|
|
|
|
|
return 1;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
++a;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Break the_line into words and store in the_words. Max nwords,
|
|
|
|
|
returns actual count. */
|
|
|
|
|
static int
|
|
|
|
|
tokenize (char *the_line, char **the_words, int nwords)
|
|
|
|
|
{
|
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
|
|
while (nwords > 0)
|
|
|
|
|
{
|
|
|
|
|
/* Skip leading whitespace, if any. */
|
|
|
|
|
while (*the_line && isspace (*the_line))
|
|
|
|
|
++the_line;
|
|
|
|
|
|
|
|
|
|
/* End of line? */
|
|
|
|
|
if (*the_line == 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
/* THE_LINE points to a non-whitespace character, so we have a
|
|
|
|
|
word. */
|
|
|
|
|
*the_words = the_line;
|
|
|
|
|
++the_words;
|
|
|
|
|
nwords--;
|
|
|
|
|
++rv;
|
|
|
|
|
|
|
|
|
|
/* Skip leading whitespace, if any. */
|
|
|
|
|
while (*the_line && ! isspace (*the_line))
|
|
|
|
|
++the_line;
|
|
|
|
|
|
|
|
|
|
/* We now point at the trailing NUL *or* some whitespace. */
|
|
|
|
|
if (*the_line == 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
/* It was whitespace, skip and keep tokenizing. */
|
|
|
|
|
*the_line++ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We get here if we filled the words buffer. */
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Mini-RSYNC implementation. Optimize later. */
|
|
|
|
|
|
|
|
|
|
/* A few routines for an "rsync buffer" which stores the paths we're
|
|
|
|
|
working on. We continuously grow and shrink the paths in each
|
|
|
|
|
buffer so there's lot of re-use. */
|
|
|
|
|
|
|
|
|
|
/* We rely on "initialized to zero" to set these up. */
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
char *buf;
|
|
|
|
|
size_t len;
|
|
|
|
|
size_t size;
|
|
|
|
|
} path_buf;
|
|
|
|
|
|
|
|
|
|
static path_buf spath, dpath;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
r_setup (char *path, path_buf * pb)
|
|
|
|
|
{
|
|
|
|
|
size_t len = strlen (path);
|
|
|
|
|
if (pb->buf == NULL || pb->size < len + 1)
|
|
|
|
|
{
|
|
|
|
|
/* Round up. This is an arbitrary number, just to keep from
|
|
|
|
|
reallocing too often. */
|
|
|
|
|
size_t sz = ALIGN_UP (len + 1, 512);
|
|
|
|
|
if (pb->buf == NULL)
|
|
|
|
|
pb->buf = (char *) xmalloc (sz);
|
|
|
|
|
else
|
|
|
|
|
pb->buf = (char *) xrealloc (pb->buf, sz);
|
|
|
|
|
if (pb->buf == NULL)
|
|
|
|
|
FAIL_EXIT1 ("Out of memory while rsyncing\n");
|
|
|
|
|
|
|
|
|
|
pb->size = sz;
|
|
|
|
|
}
|
|
|
|
|
strcpy (pb->buf, path);
|
|
|
|
|
pb->len = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
r_append (const char *path, path_buf * pb)
|
|
|
|
|
{
|
|
|
|
|
size_t len = strlen (path) + pb->len;
|
|
|
|
|
if (pb->size < len + 1)
|
|
|
|
|
{
|
|
|
|
|
/* Round up */
|
|
|
|
|
size_t sz = ALIGN_UP (len + 1, 512);
|
|
|
|
|
pb->buf = (char *) xrealloc (pb->buf, sz);
|
|
|
|
|
if (pb->buf == NULL)
|
|
|
|
|
FAIL_EXIT1 ("Out of memory while rsyncing\n");
|
|
|
|
|
|
|
|
|
|
pb->size = sz;
|
|
|
|
|
}
|
|
|
|
|
strcpy (pb->buf + pb->len, path);
|
|
|
|
|
pb->len = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
file_exists (char *path)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat (path, &st) == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 19:20:13 +00:00
|
|
|
|
static int
|
|
|
|
|
unlink_cb (const char *fpath, const struct stat *sb, int typeflag,
|
|
|
|
|
struct FTW *ftwbuf)
|
|
|
|
|
{
|
|
|
|
|
return remove (fpath);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
static void
|
|
|
|
|
recursive_remove (char *path)
|
|
|
|
|
{
|
2023-09-27 19:20:13 +00:00
|
|
|
|
int r = nftw (path, unlink_cb, 1000, FTW_DEPTH | FTW_PHYS);
|
|
|
|
|
if (r == -1)
|
|
|
|
|
FAIL_EXIT1 ("recursive_remove failed");
|
2018-07-06 05:10:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Used for both rsync and the mytest.script "cp" command. */
|
|
|
|
|
static void
|
|
|
|
|
copy_one_file (const char *sname, const char *dname)
|
|
|
|
|
{
|
|
|
|
|
int sfd, dfd;
|
|
|
|
|
struct stat st;
|
|
|
|
|
struct utimbuf times;
|
|
|
|
|
|
|
|
|
|
sfd = open (sname, O_RDONLY);
|
|
|
|
|
if (sfd < 0)
|
|
|
|
|
FAIL_EXIT1 ("unable to open %s for reading\n", sname);
|
|
|
|
|
|
|
|
|
|
if (fstat (sfd, &st) < 0)
|
|
|
|
|
FAIL_EXIT1 ("unable to fstat %s\n", sname);
|
|
|
|
|
|
|
|
|
|
dfd = open (dname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
|
|
|
|
|
if (dfd < 0)
|
|
|
|
|
FAIL_EXIT1 ("unable to open %s for writing\n", dname);
|
|
|
|
|
|
2018-08-31 20:07:47 +00:00
|
|
|
|
xcopy_file_range (sfd, 0, dfd, 0, st.st_size, 0);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
xclose (sfd);
|
|
|
|
|
xclose (dfd);
|
|
|
|
|
|
|
|
|
|
if (chmod (dname, st.st_mode & 0777) < 0)
|
|
|
|
|
FAIL_EXIT1 ("chmod %s: %s\n", dname, strerror (errno));
|
|
|
|
|
|
|
|
|
|
times.actime = st.st_atime;
|
|
|
|
|
times.modtime = st.st_mtime;
|
|
|
|
|
if (utime (dname, ×) < 0)
|
|
|
|
|
FAIL_EXIT1 ("utime %s: %s\n", dname, strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We don't check *everything* about the two files to see if a copy is
|
|
|
|
|
needed, just the minimum to make sure we get the latest copy. */
|
|
|
|
|
static int
|
|
|
|
|
need_sync (char *ap, char *bp, struct stat *a, struct stat *b)
|
|
|
|
|
{
|
|
|
|
|
if ((a->st_mode & S_IFMT) != (b->st_mode & S_IFMT))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
if (S_ISLNK (a->st_mode))
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
char *al, *bl;
|
|
|
|
|
|
|
|
|
|
if (a->st_size != b->st_size)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
al = xreadlink (ap);
|
|
|
|
|
bl = xreadlink (bp);
|
|
|
|
|
rv = strcmp (al, bl);
|
|
|
|
|
free (al);
|
|
|
|
|
free (bl);
|
|
|
|
|
if (rv == 0)
|
|
|
|
|
return 0; /* links are same */
|
|
|
|
|
return 1; /* links differ */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
{
|
|
|
|
|
if (a->st_size != b->st_size)
|
|
|
|
|
printf ("SIZE\n");
|
|
|
|
|
if ((a->st_mode & 0777) != (b->st_mode & 0777))
|
|
|
|
|
printf ("MODE\n");
|
|
|
|
|
if (a->st_mtime != b->st_mtime)
|
|
|
|
|
printf ("TIME\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a->st_size == b->st_size
|
|
|
|
|
&& ((a->st_mode & 0777) == (b->st_mode & 0777))
|
|
|
|
|
&& a->st_mtime == b->st_mtime)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync_1 (path_buf * src, path_buf * dest, int and_delete, int force_copies)
|
2018-07-06 05:10:41 +00:00
|
|
|
|
{
|
|
|
|
|
DIR *dir;
|
|
|
|
|
struct dirent *de;
|
|
|
|
|
struct stat s, d;
|
|
|
|
|
|
|
|
|
|
r_append ("/", src);
|
|
|
|
|
r_append ("/", dest);
|
|
|
|
|
|
|
|
|
|
if (verbose)
|
2021-03-11 17:50:02 +00:00
|
|
|
|
printf ("sync %s to %s%s%s\n", src->buf, dest->buf,
|
|
|
|
|
and_delete ? " and delete" : "",
|
|
|
|
|
force_copies ? " (forced)" : "");
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
size_t staillen = src->len;
|
|
|
|
|
|
|
|
|
|
size_t dtaillen = dest->len;
|
|
|
|
|
|
|
|
|
|
dir = opendir (src->buf);
|
|
|
|
|
|
|
|
|
|
while ((de = readdir (dir)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (de->d_name, ".") == 0
|
|
|
|
|
|| strcmp (de->d_name, "..") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
src->len = staillen;
|
|
|
|
|
r_append (de->d_name, src);
|
|
|
|
|
dest->len = dtaillen;
|
|
|
|
|
r_append (de->d_name, dest);
|
|
|
|
|
|
|
|
|
|
s.st_mode = ~0;
|
|
|
|
|
d.st_mode = ~0;
|
|
|
|
|
|
|
|
|
|
if (lstat (src->buf, &s) != 0)
|
|
|
|
|
FAIL_EXIT1 ("%s obtained by readdir, but stat failed.\n", src->buf);
|
|
|
|
|
|
|
|
|
|
/* It's OK if this one fails, since we know the file might be
|
|
|
|
|
missing. */
|
|
|
|
|
lstat (dest->buf, &d);
|
|
|
|
|
|
2021-03-11 17:50:02 +00:00
|
|
|
|
if (! force_copies && ! need_sync (src->buf, dest->buf, &s, &d))
|
2018-07-06 05:10:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (S_ISDIR (s.st_mode))
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync_1 (src, dest, and_delete, force_copies);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (d.st_mode != ~0)
|
|
|
|
|
switch (d.st_mode & S_IFMT)
|
|
|
|
|
{
|
|
|
|
|
case S_IFDIR:
|
|
|
|
|
if (!S_ISDIR (s.st_mode))
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("-D %s\n", dest->buf);
|
|
|
|
|
recursive_remove (dest->buf);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("-F %s\n", dest->buf);
|
|
|
|
|
maybe_xunlink (dest->buf);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (s.st_mode & S_IFMT)
|
|
|
|
|
{
|
|
|
|
|
case S_IFREG:
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("+F %s\n", dest->buf);
|
|
|
|
|
copy_one_file (src->buf, dest->buf);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S_IFDIR:
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("+D %s\n", dest->buf);
|
|
|
|
|
maybe_xmkdir (dest->buf, (s.st_mode & 0777) | 0700);
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync_1 (src, dest, and_delete, force_copies);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S_IFLNK:
|
|
|
|
|
{
|
|
|
|
|
char *lp;
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("+L %s\n", dest->buf);
|
|
|
|
|
lp = xreadlink (src->buf);
|
|
|
|
|
xsymlink (lp, dest->buf);
|
|
|
|
|
free (lp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir (dir);
|
|
|
|
|
src->len = staillen;
|
|
|
|
|
src->buf[staillen] = 0;
|
|
|
|
|
dest->len = dtaillen;
|
|
|
|
|
dest->buf[dtaillen] = 0;
|
|
|
|
|
|
|
|
|
|
if (!and_delete)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* The rest of this function removes any files/directories in DEST
|
|
|
|
|
that do not exist in SRC. This is triggered as part of a
|
|
|
|
|
preclean or postsclean step. */
|
|
|
|
|
|
|
|
|
|
dir = opendir (dest->buf);
|
|
|
|
|
|
|
|
|
|
while ((de = readdir (dir)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (de->d_name, ".") == 0
|
|
|
|
|
|| strcmp (de->d_name, "..") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
src->len = staillen;
|
|
|
|
|
r_append (de->d_name, src);
|
|
|
|
|
dest->len = dtaillen;
|
|
|
|
|
r_append (de->d_name, dest);
|
|
|
|
|
|
|
|
|
|
s.st_mode = ~0;
|
|
|
|
|
d.st_mode = ~0;
|
|
|
|
|
|
|
|
|
|
lstat (src->buf, &s);
|
|
|
|
|
|
|
|
|
|
if (lstat (dest->buf, &d) != 0)
|
|
|
|
|
FAIL_EXIT1 ("%s obtained by readdir, but stat failed.\n", dest->buf);
|
|
|
|
|
|
|
|
|
|
if (s.st_mode == ~0)
|
|
|
|
|
{
|
|
|
|
|
/* dest exists and src doesn't, clean it. */
|
|
|
|
|
switch (d.st_mode & S_IFMT)
|
|
|
|
|
{
|
|
|
|
|
case S_IFDIR:
|
|
|
|
|
if (!S_ISDIR (s.st_mode))
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("-D %s\n", dest->buf);
|
|
|
|
|
recursive_remove (dest->buf);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (verbose)
|
|
|
|
|
printf ("-F %s\n", dest->buf);
|
|
|
|
|
maybe_xunlink (dest->buf);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir (dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync (char *src, char *dest, int and_delete, int force_copies)
|
2018-07-06 05:10:41 +00:00
|
|
|
|
{
|
|
|
|
|
r_setup (src, &spath);
|
|
|
|
|
r_setup (dest, &dpath);
|
|
|
|
|
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync_1 (&spath, &dpath, and_delete, force_copies);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-12-04 05:03:12 +00:00
|
|
|
|
|
|
|
|
|
/* See if we can detect what the user needs to do to get unshare
|
|
|
|
|
support working for us. */
|
|
|
|
|
void
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
check_for_unshare_hints (int require_pidns)
|
2018-12-04 05:03:12 +00:00
|
|
|
|
{
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
static struct {
|
|
|
|
|
const char *path;
|
|
|
|
|
int bad_value, good_value, for_pidns;
|
|
|
|
|
} files[] = {
|
|
|
|
|
/* Default Debian Linux disables user namespaces, but allows a way
|
|
|
|
|
to enable them. */
|
|
|
|
|
{ "/proc/sys/kernel/unprivileged_userns_clone", 0, 1, 0 },
|
|
|
|
|
/* ALT Linux has an alternate way of doing the same. */
|
|
|
|
|
{ "/proc/sys/kernel/userns_restrict", 1, 0, 0 },
|
|
|
|
|
/* Linux kernel >= 4.9 has a configurable limit on the number of
|
|
|
|
|
each namespace. Some distros set the limit to zero to disable the
|
|
|
|
|
corresponding namespace as a "security policy". */
|
|
|
|
|
{ "/proc/sys/user/max_user_namespaces", 0, 1024, 0 },
|
|
|
|
|
{ "/proc/sys/user/max_mnt_namespaces", 0, 1024, 0 },
|
|
|
|
|
{ "/proc/sys/user/max_pid_namespaces", 0, 1024, 1 },
|
|
|
|
|
};
|
2018-12-04 05:03:12 +00:00
|
|
|
|
FILE *f;
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
int i, val;
|
2018-12-04 05:03:12 +00:00
|
|
|
|
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
for (i = 0; i < array_length (files); i++)
|
2018-12-04 05:03:12 +00:00
|
|
|
|
{
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
if (!require_pidns && files[i].for_pidns)
|
|
|
|
|
continue;
|
2018-12-04 05:03:12 +00:00
|
|
|
|
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
f = fopen (files[i].path, "r");
|
|
|
|
|
if (f == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
val = -1; /* Sentinel. */
|
2023-06-01 14:27:47 +00:00
|
|
|
|
int cnt = fscanf (f, "%d", &val);
|
|
|
|
|
if (cnt == 1 && val != files[i].bad_value)
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
printf ("To enable test-container, please run this as root:\n");
|
|
|
|
|
printf (" echo %d > %s\n", files[i].good_value, files[i].path);
|
2018-12-04 05:03:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 13:30:19 +00:00
|
|
|
|
static void
|
|
|
|
|
run_ldconfig (void *x __attribute__((unused)))
|
|
|
|
|
{
|
|
|
|
|
char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
|
|
|
|
|
char *args[] = { prog, NULL };
|
|
|
|
|
|
|
|
|
|
execv (args[0], args);
|
|
|
|
|
FAIL_EXIT1 ("execv: %m");
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
pid_t child;
|
|
|
|
|
char *pristine_root_path;
|
|
|
|
|
char *new_root_path;
|
|
|
|
|
char *new_cwd_path;
|
|
|
|
|
char *new_objdir_path;
|
|
|
|
|
char *new_srcdir_path;
|
|
|
|
|
char **new_child_proc;
|
2020-02-03 19:49:25 +00:00
|
|
|
|
char *new_child_exec;
|
2018-07-06 05:10:41 +00:00
|
|
|
|
char *command_root;
|
|
|
|
|
char *command_base;
|
|
|
|
|
char *command_basename;
|
|
|
|
|
char *so_base;
|
|
|
|
|
int do_postclean = 0;
|
2021-01-29 13:30:19 +00:00
|
|
|
|
bool do_ldconfig = false;
|
2020-02-03 19:49:25 +00:00
|
|
|
|
char *change_cwd = NULL;
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
2019-10-02 18:46:46 +00:00
|
|
|
|
int pipes[2];
|
|
|
|
|
char pid_buf[20];
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
uid_t original_uid;
|
|
|
|
|
gid_t original_gid;
|
2018-12-04 05:03:12 +00:00
|
|
|
|
/* If set, the test runs as root instead of the user running the testsuite. */
|
|
|
|
|
int be_su = 0;
|
2022-03-29 03:53:33 +00:00
|
|
|
|
int require_pidns = 0;
|
2022-04-11 19:45:35 +00:00
|
|
|
|
#ifdef CLONE_NEWNS
|
2022-03-29 03:53:33 +00:00
|
|
|
|
const char *pidns_comment = NULL;
|
2022-04-11 19:45:35 +00:00
|
|
|
|
#endif
|
2022-03-29 03:53:33 +00:00
|
|
|
|
int do_proc_mounts = 0;
|
2018-07-06 05:10:41 +00:00
|
|
|
|
int UMAP;
|
|
|
|
|
int GMAP;
|
|
|
|
|
/* Used for "%lld %lld 1" so need not be large. */
|
|
|
|
|
char tmp[100];
|
|
|
|
|
struct stat st;
|
|
|
|
|
int lock_fd;
|
|
|
|
|
|
|
|
|
|
setbuf (stdout, NULL);
|
|
|
|
|
|
|
|
|
|
/* The command line we're expecting looks like this:
|
|
|
|
|
env <set some vars> ld.so <library path> test-binary
|
|
|
|
|
|
|
|
|
|
We need to peel off any "env" or "ld.so" portion of the command
|
|
|
|
|
line, and keep track of which env vars we should preserve and
|
|
|
|
|
which we drop. */
|
|
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
2020-02-03 19:49:25 +00:00
|
|
|
|
fprintf (stderr, "Usage: test-container <program to run> <args...>\n");
|
2018-07-06 05:10:41 +00:00
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp (argv[1], "-v") == 0)
|
|
|
|
|
{
|
|
|
|
|
verbose = 1;
|
|
|
|
|
++argv;
|
|
|
|
|
--argc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp (argv[1], "env") == 0)
|
|
|
|
|
{
|
|
|
|
|
++argv;
|
|
|
|
|
--argc;
|
|
|
|
|
while (is_env_setting (argv[1]))
|
|
|
|
|
{
|
2023-05-20 13:37:47 +00:00
|
|
|
|
/* If there are variables we do NOT want to propagate, this
|
2018-07-06 05:10:41 +00:00
|
|
|
|
is where the test for them goes. */
|
|
|
|
|
{
|
|
|
|
|
/* Need to keep these. Note that putenv stores a
|
|
|
|
|
pointer to our argv. */
|
|
|
|
|
putenv (argv[1]);
|
|
|
|
|
}
|
|
|
|
|
++argv;
|
|
|
|
|
--argc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-04 11:07:29 +00:00
|
|
|
|
if (strcmp (argv[1], support_objdir_elf_ldso) == 0)
|
2018-07-06 05:10:41 +00:00
|
|
|
|
{
|
|
|
|
|
++argv;
|
|
|
|
|
--argc;
|
|
|
|
|
while (argv[1][0] == '-')
|
|
|
|
|
{
|
|
|
|
|
if (strcmp (argv[1], "--library-path") == 0)
|
|
|
|
|
{
|
|
|
|
|
++argv;
|
|
|
|
|
--argc;
|
|
|
|
|
}
|
|
|
|
|
++argv;
|
|
|
|
|
--argc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 19:49:25 +00:00
|
|
|
|
pristine_root_path = xstrdup (concat (support_objdir_root,
|
2018-07-06 05:10:41 +00:00
|
|
|
|
"/testroot.pristine", NULL));
|
2020-02-03 19:49:25 +00:00
|
|
|
|
new_root_path = xstrdup (concat (support_objdir_root,
|
2018-07-06 05:10:41 +00:00
|
|
|
|
"/testroot.root", NULL));
|
|
|
|
|
new_cwd_path = get_current_dir_name ();
|
|
|
|
|
new_child_proc = argv + 1;
|
2020-02-03 19:49:25 +00:00
|
|
|
|
new_child_exec = argv[1];
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
lock_fd = open (concat (pristine_root_path, "/lock.fd", NULL),
|
|
|
|
|
O_CREAT | O_TRUNC | O_RDWR, 0666);
|
|
|
|
|
if (lock_fd < 0)
|
|
|
|
|
FAIL_EXIT1 ("Cannot create testroot lock.\n");
|
|
|
|
|
|
|
|
|
|
while (flock (lock_fd, LOCK_EX) != 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
|
FAIL_EXIT1 ("Cannot lock testroot.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xmkdirp (new_root_path, 0755);
|
|
|
|
|
|
|
|
|
|
/* We look for extra setup info in a subdir in the same spot as the
|
|
|
|
|
test, with the same name but a ".root" extension. This is that
|
|
|
|
|
directory. We try to look in the source tree if the path we're
|
|
|
|
|
given refers to the build tree, but we rely on the path to be
|
|
|
|
|
absolute. This is what the glibc makefiles do. */
|
|
|
|
|
command_root = concat (argv[1], ".root", NULL);
|
|
|
|
|
if (strncmp (command_root, support_objdir_root,
|
|
|
|
|
strlen (support_objdir_root)) == 0
|
|
|
|
|
&& command_root[strlen (support_objdir_root)] == '/')
|
|
|
|
|
command_root = concat (support_srcdir_root,
|
|
|
|
|
argv[1] + strlen (support_objdir_root),
|
|
|
|
|
".root", NULL);
|
2020-02-03 19:49:25 +00:00
|
|
|
|
command_root = xstrdup (command_root);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
/* This cuts off the ".root" we appended above. */
|
2020-02-03 19:49:25 +00:00
|
|
|
|
command_base = xstrdup (command_root);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
command_base[strlen (command_base) - 5] = 0;
|
|
|
|
|
|
|
|
|
|
/* This is the basename of the test we're running. */
|
|
|
|
|
command_basename = strrchr (command_base, '/');
|
|
|
|
|
if (command_basename == NULL)
|
|
|
|
|
command_basename = command_base;
|
|
|
|
|
else
|
|
|
|
|
++command_basename;
|
|
|
|
|
|
|
|
|
|
/* Shared object base directory. */
|
2020-02-03 19:49:25 +00:00
|
|
|
|
so_base = xstrdup (argv[1]);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
if (strrchr (so_base, '/') != NULL)
|
|
|
|
|
strrchr (so_base, '/')[1] = 0;
|
|
|
|
|
|
|
|
|
|
if (file_exists (concat (command_root, "/postclean.req", NULL)))
|
|
|
|
|
do_postclean = 1;
|
|
|
|
|
|
2021-01-29 13:30:19 +00:00
|
|
|
|
if (file_exists (concat (command_root, "/ldconfig.run", NULL)))
|
|
|
|
|
do_ldconfig = true;
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
rsync (pristine_root_path, new_root_path,
|
2021-03-11 17:50:02 +00:00
|
|
|
|
file_exists (concat (command_root, "/preclean.req", NULL)), 0);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
if (stat (command_root, &st) >= 0
|
|
|
|
|
&& S_ISDIR (st.st_mode))
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync (command_root, new_root_path, 0, 1);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
2020-02-03 19:49:25 +00:00
|
|
|
|
new_objdir_path = xstrdup (concat (new_root_path,
|
2018-07-06 05:10:41 +00:00
|
|
|
|
support_objdir_root, NULL));
|
2020-02-03 19:49:25 +00:00
|
|
|
|
new_srcdir_path = xstrdup (concat (new_root_path,
|
2018-07-06 05:10:41 +00:00
|
|
|
|
support_srcdir_root, NULL));
|
|
|
|
|
|
|
|
|
|
/* new_cwd_path starts with '/' so no "/" needed between the two. */
|
|
|
|
|
xmkdirp (concat (new_root_path, new_cwd_path, NULL), 0755);
|
|
|
|
|
xmkdirp (new_srcdir_path, 0755);
|
|
|
|
|
xmkdirp (new_objdir_path, 0755);
|
|
|
|
|
|
|
|
|
|
original_uid = getuid ();
|
|
|
|
|
original_gid = getgid ();
|
|
|
|
|
|
|
|
|
|
/* Handle the cp/mv/rm "script" here. */
|
|
|
|
|
{
|
|
|
|
|
char *the_line = NULL;
|
|
|
|
|
size_t line_len = 0;
|
|
|
|
|
char *fname = concat (command_root, "/",
|
|
|
|
|
command_basename, ".script", NULL);
|
|
|
|
|
char *the_words[3];
|
|
|
|
|
FILE *f = fopen (fname, "r");
|
|
|
|
|
|
|
|
|
|
if (verbose && f)
|
|
|
|
|
fprintf (stderr, "running %s\n", fname);
|
|
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
|
{
|
|
|
|
|
/* Try foo.script instead of foo.root/foo.script, as a shortcut. */
|
|
|
|
|
fname = concat (command_base, ".script", NULL);
|
|
|
|
|
f = fopen (fname, "r");
|
|
|
|
|
if (verbose && f)
|
|
|
|
|
fprintf (stderr, "running %s\n", fname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Note that we do NOT look for a Makefile-generated foo.script in
|
|
|
|
|
the build directory. If that is ever needed, this is the place
|
|
|
|
|
to add it. */
|
|
|
|
|
|
|
|
|
|
/* This is where we "interpret" the mini-script which is <test>.script. */
|
|
|
|
|
if (f != NULL)
|
|
|
|
|
{
|
|
|
|
|
while (getline (&the_line, &line_len, f) > 0)
|
|
|
|
|
{
|
|
|
|
|
int nt = tokenize (the_line, the_words, 3);
|
|
|
|
|
int i;
|
|
|
|
|
|
2020-01-23 14:45:00 +00:00
|
|
|
|
/* Expand variables. */
|
2018-07-06 05:10:41 +00:00
|
|
|
|
for (i = 1; i < nt; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (memcmp (the_words[i], "$B/", 3) == 0)
|
|
|
|
|
the_words[i] = concat (support_objdir_root,
|
|
|
|
|
the_words[i] + 2, NULL);
|
|
|
|
|
else if (memcmp (the_words[i], "$S/", 3) == 0)
|
|
|
|
|
the_words[i] = concat (support_srcdir_root,
|
|
|
|
|
the_words[i] + 2, NULL);
|
|
|
|
|
else if (memcmp (the_words[i], "$I/", 3) == 0)
|
|
|
|
|
the_words[i] = concat (new_root_path,
|
|
|
|
|
support_install_prefix,
|
|
|
|
|
the_words[i] + 2, NULL);
|
|
|
|
|
else if (memcmp (the_words[i], "$L/", 3) == 0)
|
|
|
|
|
the_words[i] = concat (new_root_path,
|
|
|
|
|
support_libdir_prefix,
|
|
|
|
|
the_words[i] + 2, NULL);
|
2020-01-23 14:45:00 +00:00
|
|
|
|
else if (memcmp (the_words[i], "$complocaledir/", 15) == 0)
|
|
|
|
|
the_words[i] = concat (new_root_path,
|
|
|
|
|
support_complocaledir_prefix,
|
|
|
|
|
the_words[i] + 14, NULL);
|
2020-02-03 19:49:25 +00:00
|
|
|
|
/* "exec" and "cwd" use inside-root paths. */
|
|
|
|
|
else if (strcmp (the_words[0], "exec") != 0
|
|
|
|
|
&& strcmp (the_words[0], "cwd") != 0
|
|
|
|
|
&& the_words[i][0] == '/')
|
2018-07-06 05:10:41 +00:00
|
|
|
|
the_words[i] = concat (new_root_path,
|
|
|
|
|
the_words[i], NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nt == 3 && the_words[2][strlen (the_words[2]) - 1] == '/')
|
|
|
|
|
{
|
|
|
|
|
char *r = strrchr (the_words[1], '/');
|
|
|
|
|
if (r)
|
|
|
|
|
the_words[2] = concat (the_words[2], r + 1, NULL);
|
|
|
|
|
else
|
|
|
|
|
the_words[2] = concat (the_words[2], the_words[1], NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 14:45:00 +00:00
|
|
|
|
/* Run the following commands in the_words[0] with NT number of
|
|
|
|
|
arguments (including the command). */
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
if (nt == 2 && strcmp (the_words[0], "so") == 0)
|
|
|
|
|
{
|
|
|
|
|
the_words[2] = concat (new_root_path, support_libdir_prefix,
|
|
|
|
|
"/", the_words[1], NULL);
|
|
|
|
|
the_words[1] = concat (so_base, the_words[1], NULL);
|
|
|
|
|
copy_one_file (the_words[1], the_words[2]);
|
|
|
|
|
}
|
|
|
|
|
else if (nt == 3 && strcmp (the_words[0], "cp") == 0)
|
|
|
|
|
{
|
|
|
|
|
copy_one_file (the_words[1], the_words[2]);
|
|
|
|
|
}
|
|
|
|
|
else if (nt == 3 && strcmp (the_words[0], "mv") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (rename (the_words[1], the_words[2]) < 0)
|
|
|
|
|
FAIL_EXIT1 ("rename %s -> %s: %s", the_words[1],
|
|
|
|
|
the_words[2], strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
else if (nt == 3 && strcmp (the_words[0], "chmod") == 0)
|
|
|
|
|
{
|
|
|
|
|
long int m;
|
2020-04-29 20:31:29 +00:00
|
|
|
|
errno = 0;
|
2018-07-06 05:10:41 +00:00
|
|
|
|
m = strtol (the_words[1], NULL, 0);
|
2020-04-29 20:31:29 +00:00
|
|
|
|
TEST_COMPARE (errno, 0);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
if (chmod (the_words[2], m) < 0)
|
|
|
|
|
FAIL_EXIT1 ("chmod %s: %s\n",
|
|
|
|
|
the_words[2], strerror (errno));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (nt == 2 && strcmp (the_words[0], "rm") == 0)
|
|
|
|
|
{
|
|
|
|
|
maybe_xunlink (the_words[1]);
|
|
|
|
|
}
|
2020-02-03 19:49:25 +00:00
|
|
|
|
else if (nt >= 2 && strcmp (the_words[0], "exec") == 0)
|
|
|
|
|
{
|
|
|
|
|
/* The first argument is the desired location and name
|
|
|
|
|
of the test binary as we wish to exec it; we will
|
|
|
|
|
copy the binary there. The second (optional)
|
|
|
|
|
argument is the value to pass as argv[0], it
|
|
|
|
|
defaults to the same as the first argument. */
|
|
|
|
|
char *new_exec_path = the_words[1];
|
|
|
|
|
|
|
|
|
|
/* If the new exec path ends with a slash, that's the
|
|
|
|
|
* directory, and use the old test base name. */
|
|
|
|
|
if (new_exec_path [strlen(new_exec_path) - 1] == '/')
|
|
|
|
|
new_exec_path = concat (new_exec_path,
|
|
|
|
|
basename (new_child_proc[0]),
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* new_child_proc is in the build tree, so has the
|
|
|
|
|
same path inside the chroot as outside. The new
|
|
|
|
|
exec path is, by definition, relative to the
|
|
|
|
|
chroot. */
|
|
|
|
|
copy_one_file (new_child_proc[0], concat (new_root_path,
|
|
|
|
|
new_exec_path,
|
|
|
|
|
NULL));
|
|
|
|
|
|
|
|
|
|
new_child_exec = xstrdup (new_exec_path);
|
|
|
|
|
if (the_words[2])
|
|
|
|
|
new_child_proc[0] = xstrdup (the_words[2]);
|
|
|
|
|
else
|
|
|
|
|
new_child_proc[0] = new_child_exec;
|
|
|
|
|
}
|
|
|
|
|
else if (nt == 2 && strcmp (the_words[0], "cwd") == 0)
|
|
|
|
|
{
|
|
|
|
|
change_cwd = xstrdup (the_words[1]);
|
|
|
|
|
}
|
2018-12-04 05:03:12 +00:00
|
|
|
|
else if (nt == 1 && strcmp (the_words[0], "su") == 0)
|
|
|
|
|
{
|
|
|
|
|
be_su = 1;
|
|
|
|
|
}
|
2022-03-29 03:53:33 +00:00
|
|
|
|
else if (nt >= 1 && strcmp (the_words[0], "pidns") == 0)
|
|
|
|
|
{
|
|
|
|
|
require_pidns = 1;
|
2022-04-11 19:45:35 +00:00
|
|
|
|
#ifdef CLONE_NEWNS
|
2022-03-29 03:53:33 +00:00
|
|
|
|
if (nt > 1)
|
|
|
|
|
pidns_comment = concat_words (the_words + 1, nt - 1);
|
2022-04-11 19:45:35 +00:00
|
|
|
|
#endif
|
2022-03-29 03:53:33 +00:00
|
|
|
|
}
|
2020-01-23 14:45:00 +00:00
|
|
|
|
else if (nt == 3 && strcmp (the_words[0], "mkdirp") == 0)
|
|
|
|
|
{
|
|
|
|
|
long int m;
|
|
|
|
|
errno = 0;
|
|
|
|
|
m = strtol (the_words[1], NULL, 0);
|
|
|
|
|
TEST_COMPARE (errno, 0);
|
|
|
|
|
xmkdirp (the_words[2], m);
|
|
|
|
|
}
|
2018-07-06 05:10:41 +00:00
|
|
|
|
else if (nt > 0 && the_words[0][0] != '#')
|
|
|
|
|
{
|
2020-02-03 19:49:25 +00:00
|
|
|
|
fprintf (stderr, "\033[31minvalid [%s]\033[0m\n", the_words[0]);
|
|
|
|
|
exit (1);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose (f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 17:39:47 +00:00
|
|
|
|
if (do_postclean)
|
|
|
|
|
{
|
|
|
|
|
pid_t pc_pid = fork ();
|
|
|
|
|
|
|
|
|
|
if (pc_pid < 0)
|
|
|
|
|
{
|
|
|
|
|
FAIL_EXIT1 ("Can't fork for post-clean");
|
|
|
|
|
}
|
|
|
|
|
else if (pc_pid > 0)
|
|
|
|
|
{
|
|
|
|
|
/* Parent. */
|
|
|
|
|
int status;
|
|
|
|
|
waitpid (pc_pid, &status, 0);
|
|
|
|
|
|
|
|
|
|
/* Child has exited, we can post-clean the test root. */
|
|
|
|
|
printf("running post-clean rsync\n");
|
2021-03-11 17:50:02 +00:00
|
|
|
|
rsync (pristine_root_path, new_root_path, 1, 0);
|
2018-12-05 17:39:47 +00:00
|
|
|
|
|
|
|
|
|
if (WIFEXITED (status))
|
|
|
|
|
exit (WEXITSTATUS (status));
|
|
|
|
|
|
|
|
|
|
if (WIFSIGNALED (status))
|
|
|
|
|
{
|
|
|
|
|
printf ("%%SIGNALLED%%\n");
|
|
|
|
|
exit (77);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf ("%%EXITERROR%%\n");
|
|
|
|
|
exit (78);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Child continues. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is the last point in the program where we're still in the
|
|
|
|
|
"normal" namespace. */
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
#ifdef CLONE_NEWNS
|
|
|
|
|
/* The unshare here gives us our own spaces and capabilities. */
|
2022-03-29 03:53:33 +00:00
|
|
|
|
if (unshare (CLONE_NEWUSER | CLONE_NEWNS
|
|
|
|
|
| (require_pidns ? CLONE_NEWPID : 0)) < 0)
|
2018-07-06 05:10:41 +00:00
|
|
|
|
{
|
2018-08-28 12:02:47 +00:00
|
|
|
|
/* Older kernels may not support all the options, or security
|
|
|
|
|
policy may block this call. */
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
if (errno == EINVAL || errno == EPERM || errno == ENOSPC)
|
2018-12-04 05:03:12 +00:00
|
|
|
|
{
|
|
|
|
|
int saved_errno = errno;
|
test-container: return UNSUPPORTED for ENOSPC on clone()
Since Linux 4.9, the kernel provides
/proc/sys/user/max_{mnt,pid,user}_namespace as a limitation of number of
namespaces. Some distros (for example, Slint Linux 14.2.1) set them (or
only max_user_namespace) to zero as a "security policy" for disabling
namespaces.
The clone() call will set errno to ENOSPC under such a limitation. We
didn't check ENOSPC in the code so the test will FAIL, and report:
unable to unshare user/fs: No space left on device
This message is, unfortunately, very unhelpful. It leads people to
check the memory or disk space, instead of finding the real issue.
To improve the situation, we should check for ENOSPC and return
UNSUPPORTED as the test result. Also refactor check_for_unshare_hints()
to emit a proper message telling people how to make the test work, if
they really need to run the namespaced tests.
Reported-by: Philippe Delavalade <philippe.delavalade@orange.fr>
URL: https://lists.linuxfromscratch.org/sympa/arc/lfs-support/2022-06/msg00022.html
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: DJ Delorie <dj@redhat.com>
2022-06-28 10:44:03 +00:00
|
|
|
|
if (errno == EPERM || errno == ENOSPC)
|
|
|
|
|
check_for_unshare_hints (require_pidns);
|
2018-12-04 05:03:12 +00:00
|
|
|
|
FAIL_UNSUPPORTED ("unable to unshare user/fs: %s", strerror (saved_errno));
|
|
|
|
|
}
|
2022-03-29 03:53:33 +00:00
|
|
|
|
/* We're about to exit anyway, it's "safe" to call unshare again
|
|
|
|
|
just to see if the CLONE_NEWPID caused the error. */
|
|
|
|
|
else if (require_pidns && unshare (CLONE_NEWUSER | CLONE_NEWNS) >= 0)
|
|
|
|
|
FAIL_EXIT1 ("unable to unshare pid ns: %s : %s", strerror (errno),
|
|
|
|
|
pidns_comment ? pidns_comment : "required by test");
|
2018-07-06 05:10:41 +00:00
|
|
|
|
else
|
|
|
|
|
FAIL_EXIT1 ("unable to unshare user/fs: %s", strerror (errno));
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* Some targets may not support unshare at all. */
|
|
|
|
|
FAIL_UNSUPPORTED ("unshare support missing");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Some systems, by default, all mounts leak out of the namespace. */
|
|
|
|
|
if (mount ("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)
|
|
|
|
|
FAIL_EXIT1 ("could not create a private mount namespace\n");
|
|
|
|
|
|
|
|
|
|
trymount (support_srcdir_root, new_srcdir_path);
|
|
|
|
|
trymount (support_objdir_root, new_objdir_path);
|
|
|
|
|
|
2022-03-29 03:53:33 +00:00
|
|
|
|
/* It may not be possible to mount /proc directly. */
|
|
|
|
|
if (! require_pidns)
|
|
|
|
|
{
|
|
|
|
|
char *new_proc = concat (new_root_path, "/proc", NULL);
|
|
|
|
|
xmkdirp (new_proc, 0755);
|
|
|
|
|
trymount ("/proc", new_proc);
|
|
|
|
|
do_proc_mounts = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
xmkdirp (concat (new_root_path, "/dev", NULL), 0755);
|
|
|
|
|
devmount (new_root_path, "null");
|
|
|
|
|
devmount (new_root_path, "zero");
|
|
|
|
|
devmount (new_root_path, "urandom");
|
|
|
|
|
|
|
|
|
|
/* We're done with the "old" root, switch to the new one. */
|
|
|
|
|
if (chroot (new_root_path) < 0)
|
|
|
|
|
FAIL_EXIT1 ("Can't chroot to %s - ", new_root_path);
|
|
|
|
|
|
|
|
|
|
if (chdir (new_cwd_path) < 0)
|
|
|
|
|
FAIL_EXIT1 ("Can't cd to new %s - ", new_cwd_path);
|
|
|
|
|
|
2019-10-02 18:46:46 +00:00
|
|
|
|
/* This is to pass the "outside" PID to the child, which will be PID
|
|
|
|
|
1. */
|
|
|
|
|
if (pipe2 (pipes, O_CLOEXEC) < 0)
|
|
|
|
|
FAIL_EXIT1 ("Can't create pid pipe");
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
/* To complete the containerization, we need to fork () at least
|
|
|
|
|
once. We can't exec, nor can we somehow link the new child to
|
2023-05-20 13:37:47 +00:00
|
|
|
|
our parent. So we run the child and propagate it's exit status
|
2018-07-06 05:10:41 +00:00
|
|
|
|
up. */
|
|
|
|
|
child = fork ();
|
|
|
|
|
if (child < 0)
|
|
|
|
|
FAIL_EXIT1 ("Unable to fork");
|
|
|
|
|
else if (child > 0)
|
|
|
|
|
{
|
|
|
|
|
/* Parent. */
|
|
|
|
|
int status;
|
2019-10-02 18:46:46 +00:00
|
|
|
|
|
|
|
|
|
/* Send the child's "outside" pid to it. */
|
2023-06-01 16:40:05 +00:00
|
|
|
|
xwrite (pipes[1], &child, sizeof(child));
|
2019-10-02 18:46:46 +00:00
|
|
|
|
close (pipes[0]);
|
|
|
|
|
close (pipes[1]);
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
waitpid (child, &status, 0);
|
|
|
|
|
|
|
|
|
|
if (WIFEXITED (status))
|
|
|
|
|
exit (WEXITSTATUS (status));
|
|
|
|
|
|
|
|
|
|
if (WIFSIGNALED (status))
|
|
|
|
|
{
|
|
|
|
|
printf ("%%SIGNALLED%%\n");
|
|
|
|
|
exit (77);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf ("%%EXITERROR%%\n");
|
|
|
|
|
exit (78);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The rest is the child process, which is now PID 1 and "in" the
|
|
|
|
|
new root. */
|
|
|
|
|
|
2021-01-29 13:30:19 +00:00
|
|
|
|
if (do_ldconfig)
|
|
|
|
|
{
|
|
|
|
|
struct support_capture_subprocess result =
|
|
|
|
|
support_capture_subprocess (run_ldconfig, NULL);
|
|
|
|
|
support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 18:46:46 +00:00
|
|
|
|
/* Get our "outside" pid from our parent. We use this to help with
|
|
|
|
|
debugging from outside the container. */
|
2023-06-14 08:52:06 +00:00
|
|
|
|
xread (pipes[0], &child, sizeof(child));
|
|
|
|
|
|
2019-10-02 18:46:46 +00:00
|
|
|
|
close (pipes[0]);
|
|
|
|
|
close (pipes[1]);
|
|
|
|
|
sprintf (pid_buf, "%lu", (long unsigned)child);
|
|
|
|
|
setenv ("PID_OUTSIDE_CONTAINER", pid_buf, 0);
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
maybe_xmkdir ("/tmp", 0755);
|
|
|
|
|
|
2022-03-29 03:53:33 +00:00
|
|
|
|
if (require_pidns)
|
2018-07-06 05:10:41 +00:00
|
|
|
|
{
|
2022-03-29 03:53:33 +00:00
|
|
|
|
/* Now that we're pid 1 (effectively "root") we can mount /proc */
|
|
|
|
|
maybe_xmkdir ("/proc", 0777);
|
|
|
|
|
if (mount ("proc", "/proc", "proc", 0, NULL) != 0)
|
|
|
|
|
{
|
|
|
|
|
/* This happens if we're trying to create a nested container,
|
|
|
|
|
like if the build is running under podman, and we lack
|
2023-05-20 13:37:47 +00:00
|
|
|
|
privileges.
|
2022-03-29 03:53:33 +00:00
|
|
|
|
|
|
|
|
|
Ideally we would WARN here, but that would just add noise to
|
|
|
|
|
*every* test-container test, and the ones that care should
|
2023-05-20 13:37:47 +00:00
|
|
|
|
have their own relevant diagnostics.
|
2022-03-29 03:53:33 +00:00
|
|
|
|
|
|
|
|
|
FAIL_EXIT1 ("Unable to mount /proc: "); */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
do_proc_mounts = 1;
|
2018-07-06 05:10:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 03:53:33 +00:00
|
|
|
|
if (do_proc_mounts)
|
|
|
|
|
{
|
|
|
|
|
/* We map our original UID to the same UID in the container so we
|
|
|
|
|
can own our own files normally. */
|
|
|
|
|
UMAP = open ("/proc/self/uid_map", O_WRONLY);
|
|
|
|
|
if (UMAP < 0)
|
|
|
|
|
FAIL_EXIT1 ("can't write to /proc/self/uid_map\n");
|
|
|
|
|
|
|
|
|
|
sprintf (tmp, "%lld %lld 1\n",
|
|
|
|
|
(long long) (be_su ? 0 : original_uid), (long long) original_uid);
|
2023-06-01 16:40:05 +00:00
|
|
|
|
xwrite (UMAP, tmp, strlen (tmp));
|
2022-03-29 03:53:33 +00:00
|
|
|
|
xclose (UMAP);
|
|
|
|
|
|
|
|
|
|
/* We must disable setgroups () before we can map our groups, else we
|
|
|
|
|
get EPERM. */
|
|
|
|
|
GMAP = open ("/proc/self/setgroups", O_WRONLY);
|
|
|
|
|
if (GMAP >= 0)
|
|
|
|
|
{
|
|
|
|
|
/* We support kernels old enough to not have this. */
|
2023-06-01 16:40:05 +00:00
|
|
|
|
xwrite (GMAP, "deny\n", 5);
|
2022-03-29 03:53:33 +00:00
|
|
|
|
xclose (GMAP);
|
|
|
|
|
}
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
2022-03-29 03:53:33 +00:00
|
|
|
|
/* We map our original GID to the same GID in the container so we
|
|
|
|
|
can own our own files normally. */
|
|
|
|
|
GMAP = open ("/proc/self/gid_map", O_WRONLY);
|
|
|
|
|
if (GMAP < 0)
|
|
|
|
|
FAIL_EXIT1 ("can't write to /proc/self/gid_map\n");
|
|
|
|
|
|
|
|
|
|
sprintf (tmp, "%lld %lld 1\n",
|
|
|
|
|
(long long) (be_su ? 0 : original_gid), (long long) original_gid);
|
2023-06-01 16:40:05 +00:00
|
|
|
|
xwrite (GMAP, tmp, strlen (tmp));
|
2022-03-29 03:53:33 +00:00
|
|
|
|
xclose (GMAP);
|
|
|
|
|
}
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
2020-02-03 19:49:25 +00:00
|
|
|
|
if (change_cwd)
|
|
|
|
|
{
|
|
|
|
|
if (chdir (change_cwd) < 0)
|
|
|
|
|
FAIL_EXIT1 ("Can't cd to %s inside container - ", change_cwd);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 05:10:41 +00:00
|
|
|
|
/* Now run the child. */
|
2020-02-03 19:49:25 +00:00
|
|
|
|
execvp (new_child_exec, new_child_proc);
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
/* Or don't run the child? */
|
2020-03-11 00:05:25 +00:00
|
|
|
|
FAIL_EXIT1 ("Unable to exec %s: %s\n", new_child_exec, strerror (errno));
|
2018-07-06 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
/* Because gcc won't know error () never returns... */
|
|
|
|
|
exit (EXIT_UNSUPPORTED);
|
|
|
|
|
}
|