2000-05-29 07:31:53 +00:00
|
|
|
/* Tests for spawn.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2000-2021 Free Software Foundation, Inc.
|
2001-07-06 04:58:11 +00:00
|
|
|
This file is part of the GNU C Library.
|
2000-05-29 07:31:53 +00:00
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
|
|
|
|
|
|
|
|
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.
|
2000-05-29 07:31:53 +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.
|
2000-05-29 07:31:53 +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-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <getopt.h>
|
2000-05-29 07:31:53 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/param.h>
|
2018-09-19 18:12:05 +00:00
|
|
|
|
2017-09-26 16:40:09 +00:00
|
|
|
#include <support/check.h>
|
|
|
|
#include <support/xunistd.h>
|
2018-09-19 18:12:05 +00:00
|
|
|
#include <support/temp_file.h>
|
|
|
|
#include <support/support.h>
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Nonzero if the program gets called via `exec'. */
|
|
|
|
static int restart;
|
|
|
|
|
|
|
|
|
|
|
|
#define CMDLINE_OPTIONS \
|
|
|
|
{ "restart", no_argument, &restart, 1 },
|
|
|
|
|
|
|
|
/* Name of the temporary files. */
|
|
|
|
static char *name1;
|
|
|
|
static char *name2;
|
|
|
|
static char *name3;
|
2018-09-19 19:14:34 +00:00
|
|
|
static char *name5;
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2017-05-08 12:57:59 +00:00
|
|
|
/* Descriptors for the temporary files. */
|
|
|
|
static int temp_fd1 = -1;
|
|
|
|
static int temp_fd2 = -1;
|
|
|
|
static int temp_fd3 = -1;
|
2018-09-19 19:14:34 +00:00
|
|
|
static int temp_fd5 = -1;
|
2017-05-08 12:57:59 +00:00
|
|
|
|
2000-05-29 07:31:53 +00:00
|
|
|
/* The contents of our files. */
|
|
|
|
static const char fd1string[] = "This file should get closed";
|
|
|
|
static const char fd2string[] = "This file should stay opened";
|
|
|
|
static const char fd3string[] = "This file will be opened";
|
2018-09-19 19:14:34 +00:00
|
|
|
static const char fd5string[] = "This file should stay opened (O_CLOEXEC)";
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* We have a preparation function. */
|
2018-09-19 18:12:05 +00:00
|
|
|
static void
|
2000-05-29 07:31:53 +00:00
|
|
|
do_prepare (int argc, char *argv[])
|
|
|
|
{
|
2017-05-08 12:57:59 +00:00
|
|
|
/* We must not open any files in the restart case. */
|
|
|
|
if (restart)
|
|
|
|
return;
|
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_VERIFY_EXIT ((temp_fd1 = create_temp_file ("spawn", &name1)) != -1);
|
|
|
|
TEST_VERIFY_EXIT ((temp_fd2 = create_temp_file ("spawn", &name2)) != -1);
|
|
|
|
TEST_VERIFY_EXIT ((temp_fd3 = create_temp_file ("spawn", &name3)) != -1);
|
2018-09-19 19:14:34 +00:00
|
|
|
TEST_VERIFY_EXIT ((temp_fd5 = create_temp_file ("spawn", &name5)) != -1);
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
TEST_VERIFY_EXIT ((flags = fcntl (temp_fd5, F_GETFD, &flags)) != -1);
|
|
|
|
TEST_COMPARE (fcntl (temp_fd5, F_SETFD, flags | FD_CLOEXEC), 0);
|
2000-05-29 07:31:53 +00:00
|
|
|
}
|
2018-09-19 18:12:05 +00:00
|
|
|
#define PREPARE do_prepare
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
|
2018-09-19 19:14:34 +00:00
|
|
|
const char *fd4s, const char *name, const char *fd5s)
|
2000-05-29 07:31:53 +00:00
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
int fd1;
|
|
|
|
int fd2;
|
|
|
|
int fd3;
|
|
|
|
int fd4;
|
2018-09-19 19:14:34 +00:00
|
|
|
int fd5;
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* First get the descriptors. */
|
|
|
|
fd1 = atol (fd1s);
|
|
|
|
fd2 = atol (fd2s);
|
|
|
|
fd3 = atol (fd3s);
|
|
|
|
fd4 = atol (fd4s);
|
2018-09-19 19:14:34 +00:00
|
|
|
fd5 = atol (fd5s);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* Sanity check. */
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_VERIFY_EXIT (fd1 != fd2);
|
|
|
|
TEST_VERIFY_EXIT (fd1 != fd3);
|
|
|
|
TEST_VERIFY_EXIT (fd1 != fd4);
|
|
|
|
TEST_VERIFY_EXIT (fd2 != fd3);
|
|
|
|
TEST_VERIFY_EXIT (fd2 != fd4);
|
|
|
|
TEST_VERIFY_EXIT (fd3 != fd4);
|
2018-09-19 19:14:34 +00:00
|
|
|
TEST_VERIFY_EXIT (fd4 != fd5);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* First the easy part: read from the file descriptor which is
|
|
|
|
supposed to be open. */
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (xlseek (fd2, 0, SEEK_CUR), strlen (fd2string));
|
2000-05-29 07:31:53 +00:00
|
|
|
/* The duped descriptor must have the same position. */
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), strlen (fd2string));
|
|
|
|
TEST_COMPARE (xlseek (fd2, 0, SEEK_SET), 0);
|
|
|
|
TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), 0);
|
|
|
|
TEST_COMPARE (read (fd2, buf, sizeof buf), strlen (fd2string));
|
|
|
|
TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (fd2string));
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* Now read from the third file. */
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (read (fd3, buf, sizeof buf), strlen (fd3string));
|
|
|
|
TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (fd3string));
|
2000-05-29 07:31:53 +00:00
|
|
|
/* Try to write to the file. This should not be allowed. */
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (write (fd3, "boo!", 4), -1);
|
|
|
|
TEST_COMPARE (errno, EBADF);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* Now try to read the first file. First make sure it is not opened. */
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (lseek (fd1, 0, SEEK_CUR), (off_t) -1);
|
|
|
|
TEST_COMPARE (errno, EBADF);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* Now open the file and read it. */
|
2018-09-19 18:12:05 +00:00
|
|
|
fd1 = xopen (name, O_RDONLY, 0600);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (read (fd1, buf, sizeof buf), strlen (fd1string));
|
|
|
|
TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (fd1string));
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2018-09-19 19:14:34 +00:00
|
|
|
TEST_COMPARE (xlseek (fd5, 0, SEEK_SET), 0);
|
|
|
|
TEST_COMPARE (read (fd5, buf, sizeof buf), strlen (fd5string));
|
|
|
|
TEST_COMPARE_BLOB (fd5string, strlen (fd5string), buf, strlen (fd5string));
|
|
|
|
|
2000-05-29 07:31:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
static int
|
2000-05-29 07:31:53 +00:00
|
|
|
do_test (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int fd4;
|
|
|
|
int status;
|
|
|
|
posix_spawn_file_actions_t actions;
|
|
|
|
char fd1name[18];
|
|
|
|
char fd2name[18];
|
|
|
|
char fd3name[18];
|
|
|
|
char fd4name[18];
|
2018-09-19 19:14:34 +00:00
|
|
|
char fd5name[18];
|
2014-06-11 21:12:52 +00:00
|
|
|
char *name3_copy;
|
2019-01-05 13:58:47 +00:00
|
|
|
char *spargv[13];
|
2013-01-10 22:14:55 +00:00
|
|
|
int i;
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
/* We must have
|
2013-01-10 22:14:55 +00:00
|
|
|
- one or four parameters left if called initially
|
|
|
|
+ path for ld.so optional
|
|
|
|
+ "--library-path" optional
|
|
|
|
+ the library path optional
|
2000-05-29 07:31:53 +00:00
|
|
|
+ the application name
|
2018-09-19 19:14:34 +00:00
|
|
|
- six parameters left if called through re-execution
|
2000-05-29 07:31:53 +00:00
|
|
|
+ file descriptor number which is supposed to be closed
|
|
|
|
+ the open file descriptor
|
|
|
|
+ the newly opened file descriptor
|
2018-09-19 19:14:34 +00:00
|
|
|
+ the duped second descriptor
|
2000-05-29 07:31:53 +00:00
|
|
|
+ the name of the closed descriptor
|
2019-01-05 13:58:47 +00:00
|
|
|
+ the duped fourth file descriptor which O_CLOEXEC should be
|
2018-09-19 19:14:34 +00:00
|
|
|
remove by adddup2.
|
2000-05-29 07:31:53 +00:00
|
|
|
*/
|
2018-09-19 19:14:34 +00:00
|
|
|
if (argc != (restart ? 7 : 2) && argc != (restart ? 7 : 5))
|
2018-09-19 18:12:05 +00:00
|
|
|
FAIL_EXIT1 ("wrong number of arguments (%d)", argc);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
|
|
|
if (restart)
|
2018-09-19 19:14:34 +00:00
|
|
|
return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5],
|
|
|
|
argv[6]);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2018-09-19 19:14:34 +00:00
|
|
|
/* Prepare the test. We are creating four files: two which file descriptor
|
2000-05-29 07:31:53 +00:00
|
|
|
will be marked with FD_CLOEXEC, another which is not. */
|
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
/* Write something in the files. */
|
|
|
|
xwrite (temp_fd1, fd1string, strlen (fd1string));
|
|
|
|
xwrite (temp_fd2, fd2string, strlen (fd2string));
|
|
|
|
xwrite (temp_fd3, fd3string, strlen (fd3string));
|
2018-09-19 19:14:34 +00:00
|
|
|
xwrite (temp_fd5, fd5string, strlen (fd5string));
|
2018-09-19 18:12:05 +00:00
|
|
|
|
|
|
|
/* Close the third file. It'll be opened by `spawn'. */
|
|
|
|
xclose (temp_fd3);
|
|
|
|
|
|
|
|
/* Tell `spawn' what to do. */
|
|
|
|
TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
|
|
|
|
/* Close `temp_fd1'. */
|
|
|
|
TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, temp_fd1), 0);
|
|
|
|
/* We want to open the third file. */
|
|
|
|
name3_copy = xstrdup (name3);
|
|
|
|
TEST_COMPARE (posix_spawn_file_actions_addopen (&actions, temp_fd3,
|
|
|
|
name3_copy,
|
|
|
|
O_RDONLY, 0666),
|
|
|
|
0);
|
|
|
|
/* Overwrite the name to check that a copy has been made. */
|
|
|
|
memset (name3_copy, 'X', strlen (name3_copy));
|
|
|
|
|
|
|
|
/* We dup the second descriptor. */
|
2018-09-19 19:14:34 +00:00
|
|
|
fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, MAX (temp_fd3, temp_fd5)))) + 1;
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4),
|
|
|
|
0);
|
|
|
|
|
2018-09-19 19:14:34 +00:00
|
|
|
/* We clear the O_CLOEXEC on fourth descriptor, so it should be
|
|
|
|
stay open on child. */
|
|
|
|
TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd5,
|
|
|
|
temp_fd5),
|
|
|
|
0);
|
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
/* Now spawn the process. */
|
|
|
|
snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
|
|
|
|
snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
|
|
|
|
snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
|
|
|
|
snprintf (fd4name, sizeof fd4name, "%d", fd4);
|
2018-09-19 19:14:34 +00:00
|
|
|
snprintf (fd5name, sizeof fd5name, "%d", temp_fd5);
|
2018-09-19 18:12:05 +00:00
|
|
|
|
2018-09-19 19:14:34 +00:00
|
|
|
for (i = 0; i < (argc == (restart ? 7 : 5) ? 4 : 1); i++)
|
2018-09-19 18:12:05 +00:00
|
|
|
spargv[i] = argv[i + 1];
|
|
|
|
spargv[i++] = (char *) "--direct";
|
|
|
|
spargv[i++] = (char *) "--restart";
|
|
|
|
spargv[i++] = fd1name;
|
|
|
|
spargv[i++] = fd2name;
|
|
|
|
spargv[i++] = fd3name;
|
|
|
|
spargv[i++] = fd4name;
|
|
|
|
spargv[i++] = name1;
|
2018-09-19 19:14:34 +00:00
|
|
|
spargv[i++] = fd5name;
|
2018-09-19 18:12:05 +00:00
|
|
|
spargv[i] = NULL;
|
|
|
|
|
|
|
|
TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ),
|
|
|
|
0);
|
|
|
|
|
2019-02-01 10:03:35 +00:00
|
|
|
/* Wait for the children. */
|
|
|
|
TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
|
|
|
|
TEST_VERIFY (WIFEXITED (status));
|
|
|
|
TEST_VERIFY (!WIFSIGNALED (status));
|
|
|
|
TEST_COMPARE (WEXITSTATUS (status), 0);
|
|
|
|
|
2018-09-19 18:12:05 +00:00
|
|
|
/* Same test but with a NULL pid argument. */
|
|
|
|
TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ),
|
|
|
|
0);
|
|
|
|
|
|
|
|
/* Cleanup. */
|
|
|
|
TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
|
|
|
|
free (name3_copy);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2017-09-26 16:40:09 +00:00
|
|
|
/* Wait for the children. */
|
|
|
|
xwaitpid (-1, &status, 0);
|
|
|
|
TEST_VERIFY (WIFEXITED (status));
|
|
|
|
TEST_VERIFY (!WIFSIGNALED (status));
|
2018-09-19 18:12:05 +00:00
|
|
|
TEST_COMPARE (WEXITSTATUS (status), 0);
|
2000-05-29 07:31:53 +00:00
|
|
|
|
2017-09-26 16:40:09 +00:00
|
|
|
return 0;
|
2000-05-29 07:31:53 +00:00
|
|
|
}
|
2018-09-19 18:12:05 +00:00
|
|
|
|
|
|
|
#define TEST_FUNCTION_ARGV do_test
|
|
|
|
#include <support/test-driver.c>
|