Blame posix/tst-spawn.c

Packit 6c4009
/* Tests for spawn.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit Service 0e824a
#include <stdio.h>
Packit Service 0e824a
#include <getopt.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <spawn.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/param.h>
Packit Service 0e824a
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/xunistd.h>
Packit Service 0e824a
#include <support/temp_file.h>
Packit Service 0e824a
#include <support/support.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Nonzero if the program gets called via `exec'.  */
Packit 6c4009
static int restart;
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define CMDLINE_OPTIONS \
Packit 6c4009
  { "restart", no_argument, &restart, 1 },
Packit 6c4009
Packit 6c4009
/* Name of the temporary files.  */
Packit 6c4009
static char *name1;
Packit 6c4009
static char *name2;
Packit 6c4009
static char *name3;
Packit 6c4009
Packit 6c4009
/* Descriptors for the temporary files.  */
Packit 6c4009
static int temp_fd1 = -1;
Packit 6c4009
static int temp_fd2 = -1;
Packit 6c4009
static int temp_fd3 = -1;
Packit 6c4009
Packit 6c4009
/* The contents of our files.  */
Packit 6c4009
static const char fd1string[] = "This file should get closed";
Packit 6c4009
static const char fd2string[] = "This file should stay opened";
Packit 6c4009
static const char fd3string[] = "This file will be opened";
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We have a preparation function.  */
Packit Service 0e824a
static void
Packit 6c4009
do_prepare (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  /* We must not open any files in the restart case.  */
Packit 6c4009
  if (restart)
Packit 6c4009
    return;
Packit 6c4009
Packit Service 0e824a
  TEST_VERIFY_EXIT ((temp_fd1 = create_temp_file ("spawn", &name1)) != -1);
Packit Service 0e824a
  TEST_VERIFY_EXIT ((temp_fd2 = create_temp_file ("spawn", &name2)) != -1);
Packit Service 0e824a
  TEST_VERIFY_EXIT ((temp_fd3 = create_temp_file ("spawn", &name3)) != -1);
Packit 6c4009
}
Packit Service 0e824a
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
Packit 6c4009
		const char *fd4s, const char *name)
Packit 6c4009
{
Packit 6c4009
  char buf[100];
Packit 6c4009
  int fd1;
Packit 6c4009
  int fd2;
Packit 6c4009
  int fd3;
Packit 6c4009
  int fd4;
Packit 6c4009
Packit 6c4009
  /* First get the descriptors.  */
Packit 6c4009
  fd1 = atol (fd1s);
Packit 6c4009
  fd2 = atol (fd2s);
Packit 6c4009
  fd3 = atol (fd3s);
Packit 6c4009
  fd4 = atol (fd4s);
Packit 6c4009
Packit 6c4009
  /* Sanity check.  */
Packit Service 0e824a
  TEST_VERIFY_EXIT (fd1 != fd2);
Packit Service 0e824a
  TEST_VERIFY_EXIT (fd1 != fd3);
Packit Service 0e824a
  TEST_VERIFY_EXIT (fd1 != fd4);
Packit Service 0e824a
  TEST_VERIFY_EXIT (fd2 != fd3);
Packit Service 0e824a
  TEST_VERIFY_EXIT (fd2 != fd4);
Packit Service 0e824a
  TEST_VERIFY_EXIT (fd3 != fd4);
Packit 6c4009
Packit 6c4009
  /* First the easy part: read from the file descriptor which is
Packit 6c4009
     supposed to be open.  */
Packit Service 0e824a
  TEST_COMPARE (xlseek (fd2, 0, SEEK_CUR), strlen (fd2string));
Packit 6c4009
  /* The duped descriptor must have the same position.  */
Packit Service 0e824a
  TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), strlen (fd2string));
Packit Service 0e824a
  TEST_COMPARE (xlseek (fd2, 0, SEEK_SET), 0);
Packit Service 0e824a
  TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), 0);
Packit Service 0e824a
  TEST_COMPARE (read (fd2, buf, sizeof buf), strlen (fd2string));
Packit Service 0e824a
  TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (fd2string));
Packit 6c4009
Packit 6c4009
  /* Now read from the third file.  */
Packit Service 0e824a
  TEST_COMPARE (read (fd3, buf, sizeof buf), strlen (fd3string));
Packit Service 0e824a
  TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (fd3string));
Packit 6c4009
  /* Try to write to the file.  This should not be allowed.  */
Packit Service 0e824a
  TEST_COMPARE (write (fd3, "boo!", 4), -1);
Packit Service 0e824a
  TEST_COMPARE (errno, EBADF);
Packit 6c4009
Packit 6c4009
  /* Now try to read the first file.  First make sure it is not opened.  */
Packit Service 0e824a
  TEST_COMPARE (lseek (fd1, 0, SEEK_CUR), (off_t) -1);
Packit Service 0e824a
  TEST_COMPARE (errno, EBADF);
Packit 6c4009
Packit 6c4009
  /* Now open the file and read it.  */
Packit Service 0e824a
  fd1 = xopen (name, O_RDONLY, 0600);
Packit 6c4009
Packit Service 0e824a
  TEST_COMPARE (read (fd1, buf, sizeof buf), strlen (fd1string));
Packit Service 0e824a
  TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (fd1string));
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit Service 0e824a
static int
Packit 6c4009
do_test (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  pid_t pid;
Packit 6c4009
  int fd4;
Packit 6c4009
  int status;
Packit 6c4009
  posix_spawn_file_actions_t actions;
Packit 6c4009
  char fd1name[18];
Packit 6c4009
  char fd2name[18];
Packit 6c4009
  char fd3name[18];
Packit 6c4009
  char fd4name[18];
Packit 6c4009
  char *name3_copy;
Packit 6c4009
  char *spargv[12];
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  /* We must have
Packit 6c4009
     - one or four parameters left if called initially
Packit 6c4009
       + path for ld.so		optional
Packit 6c4009
       + "--library-path"	optional
Packit 6c4009
       + the library path	optional
Packit 6c4009
       + the application name
Packit 6c4009
     - five parameters left if called through re-execution
Packit 6c4009
       + file descriptor number which is supposed to be closed
Packit 6c4009
       + the open file descriptor
Packit 6c4009
       + the newly opened file descriptor
Packit 6c4009
       + thhe duped second descriptor
Packit 6c4009
       + the name of the closed descriptor
Packit 6c4009
  */
Packit 6c4009
  if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5))
Packit Service 0e824a
    FAIL_EXIT1 ("wrong number of arguments (%d)", argc);
Packit 6c4009
Packit 6c4009
  if (restart)
Packit 6c4009
    return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);
Packit 6c4009
Packit 6c4009
  /* Prepare the test.  We are creating two files: one which file descriptor
Packit 6c4009
     will be marked with FD_CLOEXEC, another which is not.  */
Packit 6c4009
Packit Service 0e824a
  /* Write something in the files.  */
Packit Service 0e824a
  xwrite (temp_fd1, fd1string, strlen (fd1string));
Packit Service 0e824a
  xwrite (temp_fd2, fd2string, strlen (fd2string));
Packit Service 0e824a
  xwrite (temp_fd3, fd3string, strlen (fd3string));
Packit Service 0e824a
Packit Service 0e824a
  /* Close the third file.  It'll be opened by `spawn'.  */
Packit Service 0e824a
  xclose (temp_fd3);
Packit Service 0e824a
Packit Service 0e824a
  /* Tell `spawn' what to do.  */
Packit Service 0e824a
  TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
Packit Service 0e824a
  /* Close `temp_fd1'.  */
Packit Service 0e824a
  TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, temp_fd1), 0);
Packit Service 0e824a
  /* We want to open the third file.  */
Packit Service 0e824a
  name3_copy = xstrdup (name3);
Packit Service 0e824a
  TEST_COMPARE (posix_spawn_file_actions_addopen (&actions, temp_fd3,
Packit Service 0e824a
						  name3_copy,
Packit Service 0e824a
						  O_RDONLY, 0666),
Packit Service 0e824a
		0);
Packit Service 0e824a
  /* Overwrite the name to check that a copy has been made.  */
Packit Service 0e824a
  memset (name3_copy, 'X', strlen (name3_copy));
Packit Service 0e824a
Packit Service 0e824a
  /* We dup the second descriptor.  */
Packit Service 0e824a
  fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1;
Packit Service 0e824a
  TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4),
Packit Service 0e824a
	        0);
Packit Service 0e824a
Packit Service 0e824a
  /* Now spawn the process.  */
Packit Service 0e824a
  snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
Packit Service 0e824a
  snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
Packit Service 0e824a
  snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
Packit Service 0e824a
  snprintf (fd4name, sizeof fd4name, "%d", fd4);
Packit Service 0e824a
Packit Service 0e824a
  for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++)
Packit Service 0e824a
    spargv[i] = argv[i + 1];
Packit Service 0e824a
  spargv[i++] = (char *) "--direct";
Packit Service 0e824a
  spargv[i++] = (char *) "--restart";
Packit Service 0e824a
  spargv[i++] = fd1name;
Packit Service 0e824a
  spargv[i++] = fd2name;
Packit Service 0e824a
  spargv[i++] = fd3name;
Packit Service 0e824a
  spargv[i++] = fd4name;
Packit Service 0e824a
  spargv[i++] = name1;
Packit Service 0e824a
  spargv[i] = NULL;
Packit Service 0e824a
Packit Service 0e824a
  TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ),
Packit Service 0e824a
		0);
Packit Service 0e824a
Packit Service 69278f
  /* Wait for the children.  */
Packit Service 69278f
  TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
Packit Service 69278f
  TEST_VERIFY (WIFEXITED (status));
Packit Service 69278f
  TEST_VERIFY (!WIFSIGNALED (status));
Packit Service 69278f
  TEST_COMPARE (WEXITSTATUS (status), 0);
Packit Service 69278f
Packit Service 0e824a
  /* Same test but with a NULL pid argument.  */
Packit Service 0e824a
  TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ),
Packit Service 0e824a
		0);
Packit Service 0e824a
Packit Service 0e824a
  /* Cleanup.  */
Packit Service 0e824a
  TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
Packit Service 0e824a
  free (name3_copy);
Packit Service ce1c21
Packit Service f9d307
  /* Wait for the children.  */
Packit 6c4009
  xwaitpid (-1, &status, 0);
Packit 6c4009
  TEST_VERIFY (WIFEXITED (status));
Packit 6c4009
  TEST_VERIFY (!WIFSIGNALED (status));
Packit Service 0e824a
  TEST_COMPARE (WEXITSTATUS (status), 0);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit Service 0e824a
Packit Service 0e824a
#define TEST_FUNCTION_ARGV do_test
Packit Service 0e824a
#include <support/test-driver.c>