2007-01-04 07:02:17 +08:00
|
|
|
/* Test for vfork functions.
|
2023-01-07 05:08:04 +08:00
|
|
|
Copyright (C) 2007-2023 Free Software Foundation, Inc.
|
2007-01-04 07:02:17 +08: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
|
2012-02-10 07:18:22 +08: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-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2007-01-04 07:02:17 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <mcheck.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2017-02-17 06:31:42 +08:00
|
|
|
#include <sys/stat.h>
|
2007-01-04 07:02:17 +08:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
static int do_test (void);
|
|
|
|
static void do_prepare (void);
|
|
|
|
char *tmpdirname;
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#define PREPARE(argc, argv) do_prepare ()
|
|
|
|
#include "../test-skeleton.c"
|
|
|
|
|
2016-11-23 02:23:28 +08:00
|
|
|
static void
|
|
|
|
run_script (const char *script, char *const argv[])
|
2007-01-04 07:02:17 +08:00
|
|
|
{
|
2016-11-23 02:23:28 +08:00
|
|
|
for (size_t i = 0; i < 5; i++)
|
2007-01-04 07:02:17 +08:00
|
|
|
{
|
|
|
|
pid_t pid = vfork ();
|
|
|
|
if (pid < 0)
|
2016-11-23 02:23:28 +08:00
|
|
|
FAIL_EXIT1 ("vfork failed: %m");
|
2007-01-04 07:02:17 +08:00
|
|
|
else if (pid == 0)
|
|
|
|
{
|
2016-11-23 02:23:28 +08:00
|
|
|
execvp (script, argv);
|
2007-01-04 07:02:17 +08:00
|
|
|
_exit (errno);
|
|
|
|
}
|
2016-11-23 02:23:28 +08:00
|
|
|
|
2007-01-04 07:02:17 +08:00
|
|
|
int status;
|
|
|
|
if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
|
2016-11-23 02:23:28 +08:00
|
|
|
FAIL_EXIT1 ("waitpid failed");
|
2007-01-04 07:02:17 +08:00
|
|
|
else if (status != 0)
|
|
|
|
{
|
|
|
|
if (WIFEXITED (status))
|
2016-11-23 02:23:28 +08:00
|
|
|
FAIL_EXIT1 ("%s failed with status %d\n", script,
|
|
|
|
WEXITSTATUS (status));
|
2007-01-04 07:02:17 +08:00
|
|
|
else
|
2016-11-23 02:23:28 +08:00
|
|
|
FAIL_EXIT1 ("%s killed by signal %d\n", script,
|
|
|
|
WTERMSIG (status));
|
2007-01-04 07:02:17 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-23 02:23:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
mtrace ();
|
2007-01-04 07:02:17 +08:00
|
|
|
|
2016-11-23 02:23:28 +08:00
|
|
|
const char *path = getenv ("PATH");
|
|
|
|
if (path == NULL)
|
|
|
|
path = "/bin";
|
|
|
|
char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1];
|
|
|
|
strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path);
|
|
|
|
if (setenv ("PATH", pathbuf, 1) < 0)
|
2007-01-04 07:02:17 +08:00
|
|
|
{
|
2016-11-23 02:23:28 +08:00
|
|
|
puts ("setenv failed");
|
|
|
|
return 1;
|
2007-01-04 07:02:17 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 02:23:28 +08:00
|
|
|
/* Although manual states first argument should be the script name itself,
|
|
|
|
current execv{p,e} implementation allows it. */
|
|
|
|
char *argv00[] = { NULL };
|
|
|
|
run_script ("script0.sh", argv00);
|
|
|
|
|
|
|
|
char *argv01[] = { (char*) "script0.sh", NULL };
|
|
|
|
run_script ("script0.sh", argv01);
|
|
|
|
|
|
|
|
char *argv1[] = { (char *) "script1.sh", (char *) "1", NULL };
|
|
|
|
run_script ("script1.sh", argv1);
|
|
|
|
|
|
|
|
char *argv2[] = { (char *) "script2.sh", (char *) "2", NULL };
|
|
|
|
run_script ("script2.sh", argv2);
|
|
|
|
|
|
|
|
/* Same as before but with execlp. */
|
|
|
|
for (size_t i = 0; i < 5; i++)
|
2007-01-04 07:02:17 +08:00
|
|
|
{
|
|
|
|
pid_t pid = vfork ();
|
|
|
|
if (pid < 0)
|
|
|
|
{
|
|
|
|
printf ("vfork failed: %m\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (pid == 0)
|
|
|
|
{
|
|
|
|
execlp ("script2.sh", "script2.sh", "3", NULL);
|
|
|
|
_exit (errno);
|
|
|
|
}
|
|
|
|
int status;
|
|
|
|
if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
|
|
|
|
{
|
|
|
|
puts ("waitpid failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (status != 0)
|
|
|
|
{
|
|
|
|
printf ("script2.sh failed with status %d\n", status);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsetenv ("PATH");
|
2016-11-23 02:23:28 +08:00
|
|
|
char *argv4[] = { (char *) "echo", (char *) "script 4", NULL };
|
|
|
|
run_script ("echo", argv4);
|
2007-01-04 07:02:17 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:23:28 +08:00
|
|
|
static void
|
|
|
|
create_script (const char *script, const char *contents, size_t size)
|
|
|
|
{
|
|
|
|
int fd = open (script, O_WRONLY | O_CREAT, 0700);
|
|
|
|
if (fd < 0
|
|
|
|
|| TEMP_FAILURE_RETRY (write (fd, contents, size)) != size
|
|
|
|
|| fchmod (fd, S_IRUSR | S_IXUSR) < 0)
|
|
|
|
FAIL_EXIT1 ("could not write %s\n", script);
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
|
2007-01-04 07:02:17 +08:00
|
|
|
static void
|
|
|
|
do_prepare (void)
|
|
|
|
{
|
|
|
|
size_t len = strlen (test_dir) + sizeof ("/tst-vfork3.XXXXXX");
|
|
|
|
tmpdirname = malloc (len);
|
2016-11-23 02:23:28 +08:00
|
|
|
if (tmpdirname == NULL)
|
|
|
|
FAIL_EXIT1 ("out of memory");
|
2007-01-04 07:02:17 +08:00
|
|
|
strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX");
|
|
|
|
|
|
|
|
tmpdirname = mkdtemp (tmpdirname);
|
|
|
|
if (tmpdirname == NULL)
|
2016-11-23 02:23:28 +08:00
|
|
|
FAIL_EXIT1 ("could not create temporary directory");
|
|
|
|
|
|
|
|
char script0[len + sizeof "/script0.sh"];
|
|
|
|
char script1[len + sizeof "/script1.sh"];
|
|
|
|
char script2[len + sizeof "/script2.sh"];
|
2007-01-04 07:02:17 +08:00
|
|
|
|
2016-11-23 02:23:28 +08:00
|
|
|
strcpy (stpcpy (script0, tmpdirname), "/script0.sh");
|
2007-01-04 07:02:17 +08:00
|
|
|
strcpy (stpcpy (script1, tmpdirname), "/script1.sh");
|
|
|
|
strcpy (stpcpy (script2, tmpdirname), "/script2.sh");
|
|
|
|
|
2017-05-08 20:57:59 +08:00
|
|
|
add_temp_file (tmpdirname);
|
2016-11-23 02:23:28 +08:00
|
|
|
add_temp_file (script0);
|
2007-01-04 07:02:17 +08:00
|
|
|
add_temp_file (script1);
|
|
|
|
add_temp_file (script2);
|
2016-11-23 02:23:28 +08:00
|
|
|
|
|
|
|
const char content0[] = "#!/bin/sh\necho empty\n";
|
|
|
|
create_script (script0, content0, sizeof content0);
|
2007-01-04 07:02:17 +08:00
|
|
|
|
|
|
|
const char content1[] = "#!/bin/sh\necho script $1\n";
|
2016-11-23 02:23:28 +08:00
|
|
|
create_script (script1, content1, sizeof content1);
|
2007-01-04 07:02:17 +08:00
|
|
|
|
|
|
|
const char content2[] = "echo script $1\n";
|
2016-11-23 02:23:28 +08:00
|
|
|
create_script (script2, content2, sizeof content2);
|
2007-01-04 07:02:17 +08:00
|
|
|
}
|