Blame posix/tst-exec.c

Packit 6c4009
/* Tests for exec.
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 <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <wait.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
/* Prototype for our test function.  */
Packit 6c4009
extern void do_prepare (int argc, char *argv[]);
Packit 6c4009
extern int do_test (int argc, char *argv[]);
Packit 6c4009
Packit 6c4009
/* We have a preparation function.  */
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Name of the temporary files.  */
Packit 6c4009
static char *name1;
Packit 6c4009
static char *name2;
Packit 6c4009
Packit 6c4009
/* File descriptors for these temporary files.  */
Packit 6c4009
static int temp_fd1 = -1;
Packit 6c4009
static int temp_fd2 = -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
Packit 6c4009
Packit 6c4009
/* We have a preparation function.  */
Packit 6c4009
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 6c4009
  temp_fd1 = create_temp_file ("exec", &name1);
Packit 6c4009
  temp_fd2 = create_temp_file ("exec", &name2);
Packit 6c4009
  if (temp_fd1 < 0 || temp_fd2 < 0)
Packit 6c4009
    exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
handle_restart (const char *fd1s, const char *fd2s, const char *name)
Packit 6c4009
{
Packit 6c4009
  char buf[100];
Packit 6c4009
  int fd1;
Packit 6c4009
  int fd2;
Packit 6c4009
Packit 6c4009
  /* First get the descriptors.  */
Packit 6c4009
  fd1 = atol (fd1s);
Packit 6c4009
  fd2 = atol (fd2s);
Packit 6c4009
Packit 6c4009
  /* Sanity check.  */
Packit 6c4009
  if (fd1 == fd2)
Packit 6c4009
    error (EXIT_FAILURE, 0, "value of fd1 and fd2 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 6c4009
  if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
Packit 6c4009
    error (EXIT_FAILURE, errno, "file 2 not in right position");
Packit 6c4009
  if (lseek (fd2, 0, SEEK_SET) != 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "cannot reset position in file 2");
Packit 6c4009
  if (read (fd2, buf, sizeof buf) != strlen (fd2string))
Packit 6c4009
    error (EXIT_FAILURE, 0, "cannot read file 2");
Packit 6c4009
  if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "file 2 does not match");
Packit 6c4009
Packit 6c4009
  /* No try to read the first file.  First make sure it is not opened.  */
Packit 6c4009
  if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
Packit 6c4009
    error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
Packit 6c4009
Packit 6c4009
  /* Now open the file and read it.  */
Packit 6c4009
  fd1 = open (name, O_RDONLY);
Packit 6c4009
  if (fd1 == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno,
Packit 6c4009
	   "cannot open first file \"%s\" for verification", name);
Packit 6c4009
Packit 6c4009
  if (read (fd1, buf, sizeof buf) != strlen (fd1string))
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot read file 1");
Packit 6c4009
  if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "file 1 does not match");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  pid_t pid;
Packit 6c4009
  int flags;
Packit 6c4009
  int status;
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
     - three 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 name of the closed desriptor
Packit 6c4009
  */
Packit 6c4009
Packit 6c4009
  if (restart)
Packit 6c4009
    {
Packit 6c4009
      if (argc != 4)
Packit 6c4009
	error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
Packit 6c4009
Packit 6c4009
      return handle_restart (argv[1], argv[2], argv[3]);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (argc != 2 && argc != 5)
Packit 6c4009
    error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
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 6c4009
   /* Set the bit.  */
Packit 6c4009
   flags = fcntl (temp_fd1, F_GETFD, 0);
Packit 6c4009
   if (flags < 0)
Packit 6c4009
     error (EXIT_FAILURE, errno, "cannot get flags");
Packit 6c4009
   flags |= FD_CLOEXEC;
Packit 6c4009
   if (fcntl (temp_fd1, F_SETFD, flags) < 0)
Packit 6c4009
     error (EXIT_FAILURE, errno, "cannot set flags");
Packit 6c4009
Packit 6c4009
   /* Write something in the files.  */
Packit 6c4009
   if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
Packit 6c4009
     error (EXIT_FAILURE, errno, "cannot write to first file");
Packit 6c4009
   if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
Packit 6c4009
     error (EXIT_FAILURE, errno, "cannot write to second file");
Packit 6c4009
Packit 6c4009
  /* We want to test the `exec' function.  To do this we restart the program
Packit 6c4009
     with an additional parameter.  But first create another process.  */
Packit 6c4009
  pid = fork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      char fd1name[18];
Packit 6c4009
      char fd2name[18];
Packit 6c4009
Packit 6c4009
      snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
Packit 6c4009
      snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
Packit 6c4009
Packit 6c4009
      /* This is the child.  Construct the command line.  */
Packit 6c4009
      if (argc == 5)
Packit 6c4009
	execl (argv[1], argv[1], argv[2], argv[3], argv[4], "--direct",
Packit 6c4009
	       "--restart", fd1name, fd2name, name1, NULL);
Packit 6c4009
      else
Packit 6c4009
	execl (argv[1], argv[1], "--direct",
Packit 6c4009
	       "--restart", fd1name, fd2name, name1, NULL);
Packit 6c4009
Packit 6c4009
      error (EXIT_FAILURE, errno, "cannot exec");
Packit 6c4009
    }
Packit 6c4009
  else if (pid == (pid_t) -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot fork");
Packit 6c4009
Packit 6c4009
  /* Wait for the child.  */
Packit 6c4009
  if (waitpid (pid, &status, 0) != pid)
Packit 6c4009
    error (EXIT_FAILURE, errno, "wrong child");
Packit 6c4009
Packit 6c4009
  if (WTERMSIG (status) != 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "Child terminated incorrectly");
Packit 6c4009
  status = WEXITSTATUS (status);
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}