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 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 Bot 0c2104
#include <wait.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/xunistd.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 Bot 0c2104
/* Prototype for our test function.  */
Packit Bot 0c2104
extern void do_prepare (int argc, char *argv[]);
Packit Bot 0c2104
extern int do_test (int argc, char *argv[]);
Packit Bot 0c2104
Packit Bot 0c2104
/* We have a preparation function.  */
Packit Bot 0c2104
#define PREPARE do_prepare
Packit Bot 0c2104
Packit Bot 0c2104
#include "../test-skeleton.c"
Packit Bot 0c2104
Packit Bot 0c2104
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 Bot 0c2104
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 Bot 0c2104
  temp_fd1 = create_temp_file ("spawn", &name1);
Packit Bot 0c2104
  temp_fd2 = create_temp_file ("spawn", &name2);
Packit Bot 0c2104
  temp_fd3 = create_temp_file ("spawn", &name3);
Packit Bot 0c2104
  if (temp_fd1 < 0 || temp_fd2 < 0 || temp_fd3 < 0)
Packit Bot 0c2104
    exit (1);
Packit 6c4009
}
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 Bot 0c2104
  if (fd1 == fd2)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
Packit Bot 0c2104
  if (fd1 == fd3)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same");
Packit Bot 0c2104
  if (fd1 == fd4)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same");
Packit Bot 0c2104
  if (fd2 == fd3)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same");
Packit Bot 0c2104
  if (fd2 == fd4)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same");
Packit Bot 0c2104
  if (fd3 == fd4)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same");
Packit 6c4009
Packit 6c4009
  /* First the easy part: read from the file descriptor which is
Packit 6c4009
     supposed to be open.  */
Packit Bot 0c2104
  if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
Packit Bot 0c2104
    error (EXIT_FAILURE, errno, "file 2 not in right position");
Packit 6c4009
  /* The duped descriptor must have the same position.  */
Packit Bot 0c2104
  if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string))
Packit Bot 0c2104
    error (EXIT_FAILURE, errno, "file 4 not in right position");
Packit Bot 0c2104
  if (lseek (fd2, 0, SEEK_SET) != 0)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "cannot reset position in file 2");
Packit Bot 0c2104
  if (lseek (fd4, 0, SEEK_CUR) != 0)
Packit Bot 0c2104
    error (EXIT_FAILURE, errno, "file 4 not set back, too");
Packit Bot 0c2104
  if (read (fd2, buf, sizeof buf) != strlen (fd2string))
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "cannot read file 2");
Packit Bot 0c2104
  if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "file 2 does not match");
Packit 6c4009
Packit 6c4009
  /* Now read from the third file.  */
Packit Bot 0c2104
  if (read (fd3, buf, sizeof buf) != strlen (fd3string))
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "cannot read file 3");
Packit Bot 0c2104
  if (memcmp (fd3string, buf, strlen (fd3string)) != 0)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "file 3 does not match");
Packit 6c4009
  /* Try to write to the file.  This should not be allowed.  */
Packit Bot 0c2104
  if (write (fd3, "boo!", 4) != -1 || errno != EBADF)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "file 3 is writable");
Packit 6c4009
Packit 6c4009
  /* Now try to read the first file.  First make sure it is not opened.  */
Packit Bot 0c2104
  if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
Packit 6c4009
Packit 6c4009
  /* Now open the file and read it.  */
Packit Bot 0c2104
  fd1 = open (name, O_RDONLY);
Packit Bot 0c2104
  if (fd1 == -1)
Packit Bot 0c2104
    error (EXIT_FAILURE, errno,
Packit Bot 0c2104
	   "cannot open first file \"%s\" for verification", name);
Packit 6c4009
Packit Bot 0c2104
  if (read (fd1, buf, sizeof buf) != strlen (fd1string))
Packit Bot 0c2104
    error (EXIT_FAILURE, errno, "cannot read file 1");
Packit Bot 0c2104
  if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
Packit Bot 0c2104
    error (EXIT_FAILURE, 0, "file 1 does not match");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit Bot 0c2104
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 Bot 0c2104
    error (EXIT_FAILURE, 0, "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 Bot 0c2104
   /* Write something in the files.  */
Packit Bot 0c2104
   if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "cannot write to first file");
Packit Bot 0c2104
   if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "cannot write to second file");
Packit Bot 0c2104
   if (write (temp_fd3, fd3string, strlen (fd3string)) != strlen (fd3string))
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "cannot write to third file");
Packit Bot 0c2104
Packit Bot 0c2104
   /* Close the third file.  It'll be opened by `spawn'.  */
Packit Bot 0c2104
   close (temp_fd3);
Packit Bot 0c2104
Packit Bot 0c2104
   /* Tell `spawn' what to do.  */
Packit Bot 0c2104
   if (posix_spawn_file_actions_init (&actions) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init");
Packit Bot 0c2104
   /* Close `temp_fd1'.  */
Packit Bot 0c2104
   if (posix_spawn_file_actions_addclose (&actions, temp_fd1) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose");
Packit Bot 0c2104
   /* We want to open the third file.  */
Packit Bot 0c2104
   name3_copy = strdup (name3);
Packit Bot 0c2104
   if (name3_copy == NULL)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "strdup");
Packit Bot 0c2104
   if (posix_spawn_file_actions_addopen (&actions, temp_fd3, name3_copy,
Packit Bot 0c2104
					 O_RDONLY, 0666) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen");
Packit Bot 0c2104
   /* Overwrite the name to check that a copy has been made.  */
Packit Bot 0c2104
   memset (name3_copy, 'X', strlen (name3_copy));
Packit Bot 0c2104
Packit Bot 0c2104
   /* We dup the second descriptor.  */
Packit Bot 0c2104
   fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1;
Packit Bot 0c2104
   if (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2");
Packit Bot 0c2104
Packit Bot 0c2104
   /* Now spawn the process.  */
Packit Bot 0c2104
   snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
Packit Bot 0c2104
   snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
Packit Bot 0c2104
   snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
Packit Bot 0c2104
   snprintf (fd4name, sizeof fd4name, "%d", fd4);
Packit Bot 0c2104
Packit Bot 0c2104
   for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++)
Packit Bot 0c2104
     spargv[i] = argv[i + 1];
Packit Bot 0c2104
   spargv[i++] = (char *) "--direct";
Packit Bot 0c2104
   spargv[i++] = (char *) "--restart";
Packit Bot 0c2104
   spargv[i++] = fd1name;
Packit Bot 0c2104
   spargv[i++] = fd2name;
Packit Bot 0c2104
   spargv[i++] = fd3name;
Packit Bot 0c2104
   spargv[i++] = fd4name;
Packit Bot 0c2104
   spargv[i++] = name1;
Packit Bot 0c2104
   spargv[i] = NULL;
Packit Bot 0c2104
Packit Bot 0c2104
   if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn");
Packit Bot 0c2104
Packit Bot 0c2104
   /* Same test but with a NULL pid argument.  */
Packit Bot 0c2104
   if (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn");
Packit Bot 0c2104
Packit Bot 0c2104
   /* Cleanup.  */
Packit Bot 0c2104
   if (posix_spawn_file_actions_destroy (&actions) != 0)
Packit Bot 0c2104
     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy");
Packit Bot 0c2104
   free (name3_copy);
Packit Service b179e8
Packit Service 597acf
  /* Wait for the children.  */
Packit Bot 0c2104
  TEST_VERIFY (xwaitpid (pid, &status, 0) == pid);
Packit Service 597acf
  TEST_VERIFY (WIFEXITED (status));
Packit Service 597acf
  TEST_VERIFY (!WIFSIGNALED (status));
Packit Bot 0c2104
  TEST_VERIFY (WEXITSTATUS (status) == 0);
Packit Service b179e8
Packit 6c4009
  xwaitpid (-1, &status, 0);
Packit 6c4009
  TEST_VERIFY (WIFEXITED (status));
Packit 6c4009
  TEST_VERIFY (!WIFSIGNALED (status));
Packit Bot 0c2104
  TEST_VERIFY (WEXITSTATUS (status) == 0);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}