Blame posix/tst-vfork3.c

Packit 6c4009
/* Test for vfork functions.
Packit 6c4009
   Copyright (C) 2007-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
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 <fcntl.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
static void do_prepare (void);
Packit 6c4009
char *tmpdirname;
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#define PREPARE(argc, argv) do_prepare ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
run_script (const char *script, char *const argv[])
Packit 6c4009
{
Packit 6c4009
  for (size_t i = 0; i < 5; i++)
Packit 6c4009
    {
Packit 6c4009
      pid_t pid = vfork ();
Packit 6c4009
      if (pid < 0)
Packit 6c4009
	FAIL_EXIT1 ("vfork failed: %m");
Packit 6c4009
      else if (pid == 0)
Packit 6c4009
	{
Packit 6c4009
	  execvp (script, argv);
Packit 6c4009
	  _exit (errno);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      int status;
Packit 6c4009
      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
Packit 6c4009
	FAIL_EXIT1 ("waitpid failed");
Packit 6c4009
      else if (status != 0)
Packit 6c4009
	{
Packit 6c4009
	  if (WIFEXITED (status))
Packit 6c4009
	    FAIL_EXIT1 ("%s failed with status %d\n", script,
Packit 6c4009
			WEXITSTATUS (status));
Packit 6c4009
	  else
Packit 6c4009
	    FAIL_EXIT1 ("%s killed by signal %d\n", script,
Packit 6c4009
			WTERMSIG (status));
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  const char *path = getenv ("PATH");
Packit 6c4009
  if (path == NULL)
Packit 6c4009
    path = "/bin";
Packit 6c4009
  char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1];
Packit 6c4009
  strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path);
Packit 6c4009
  if (setenv ("PATH", pathbuf, 1) < 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("setenv failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Although manual states first argument should be the script name itself,
Packit 6c4009
     current execv{p,e} implementation allows it.  */
Packit 6c4009
  char *argv00[] = { NULL };
Packit 6c4009
  run_script ("script0.sh", argv00);
Packit 6c4009
Packit 6c4009
  char *argv01[] = { (char*) "script0.sh", NULL };
Packit 6c4009
  run_script ("script0.sh", argv01);
Packit 6c4009
Packit 6c4009
  char *argv1[] = { (char *) "script1.sh", (char *) "1", NULL };
Packit 6c4009
  run_script ("script1.sh", argv1);
Packit 6c4009
Packit 6c4009
  char *argv2[] = { (char *) "script2.sh", (char *) "2", NULL };
Packit 6c4009
  run_script ("script2.sh", argv2);
Packit 6c4009
Packit 6c4009
  /* Same as before but with execlp.  */
Packit 6c4009
  for (size_t i = 0; i < 5; i++)
Packit 6c4009
    {
Packit 6c4009
      pid_t pid = vfork ();
Packit 6c4009
      if (pid < 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("vfork failed: %m\n");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      else if (pid == 0)
Packit 6c4009
	{
Packit 6c4009
	  execlp ("script2.sh", "script2.sh", "3", NULL);
Packit 6c4009
	  _exit (errno);
Packit 6c4009
	}
Packit 6c4009
      int status;
Packit 6c4009
      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
Packit 6c4009
	{
Packit 6c4009
	  puts ("waitpid failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      else if (status != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("script2.sh failed with status %d\n", status);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  unsetenv ("PATH");
Packit 6c4009
  char *argv4[] = { (char *) "echo", (char *) "script 4", NULL };
Packit 6c4009
  run_script ("echo", argv4);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
create_script (const char *script, const char *contents, size_t size)
Packit 6c4009
{
Packit 6c4009
  int fd = open (script, O_WRONLY | O_CREAT, 0700);
Packit 6c4009
  if (fd < 0
Packit 6c4009
      || TEMP_FAILURE_RETRY (write (fd, contents, size)) != size
Packit 6c4009
      || fchmod (fd, S_IRUSR | S_IXUSR) < 0)
Packit 6c4009
    FAIL_EXIT1 ("could not write %s\n", script);
Packit 6c4009
  close (fd);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  size_t len = strlen (test_dir) + sizeof ("/tst-vfork3.XXXXXX");
Packit 6c4009
  tmpdirname = malloc (len);
Packit 6c4009
  if (tmpdirname == NULL)
Packit 6c4009
    FAIL_EXIT1 ("out of memory");
Packit 6c4009
  strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX");
Packit 6c4009
Packit 6c4009
  tmpdirname = mkdtemp (tmpdirname);
Packit 6c4009
  if (tmpdirname == NULL)
Packit 6c4009
    FAIL_EXIT1 ("could not create temporary directory");
Packit 6c4009
Packit 6c4009
  char script0[len + sizeof "/script0.sh"];
Packit 6c4009
  char script1[len + sizeof "/script1.sh"];
Packit 6c4009
  char script2[len + sizeof "/script2.sh"];
Packit 6c4009
Packit 6c4009
  strcpy (stpcpy (script0, tmpdirname), "/script0.sh");
Packit 6c4009
  strcpy (stpcpy (script1, tmpdirname), "/script1.sh");
Packit 6c4009
  strcpy (stpcpy (script2, tmpdirname), "/script2.sh");
Packit 6c4009
Packit 6c4009
  add_temp_file (tmpdirname);
Packit 6c4009
  add_temp_file (script0);
Packit 6c4009
  add_temp_file (script1);
Packit 6c4009
  add_temp_file (script2);
Packit 6c4009
Packit 6c4009
  const char content0[] = "#!/bin/sh\necho empty\n";
Packit 6c4009
  create_script (script0, content0, sizeof content0);
Packit 6c4009
Packit 6c4009
  const char content1[] = "#!/bin/sh\necho script $1\n";
Packit 6c4009
  create_script (script1, content1, sizeof content1);
Packit 6c4009
Packit 6c4009
  const char content2[] = "echo script $1\n";
Packit 6c4009
  create_script (script2, content2, sizeof content2);
Packit 6c4009
}