Blame posix/tst-execvpe6.c

Packit 6c4009
/* Check execvpe script argument handling.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <stdio.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/param.h>
Packit 6c4009
Packit 6c4009
static char *fname1;
Packit 6c4009
static char *fname2;
Packit 6c4009
static char *logname;
Packit 6c4009
Packit 6c4009
static void do_prepare (void);
Packit 6c4009
#define PREPARE(argc, argv) do_prepare ()
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  int logfd = create_temp_file ("logfile", &logname);
Packit 6c4009
  close (logfd);
Packit 6c4009
Packit 6c4009
  int fd1 = create_temp_file ("testscript", &fname1);
Packit 6c4009
  dprintf (fd1, "echo foo $1 $2 $3 > %s\n", logname);
Packit 6c4009
  fchmod (fd1, 0700);
Packit 6c4009
  close (fd1);
Packit 6c4009
Packit 6c4009
  int fd2 = create_temp_file ("testscript", &fname2);
Packit 6c4009
  dprintf (fd2, "echo foo > %s\n", logname);
Packit 6c4009
  fchmod (fd2, 0700);
Packit 6c4009
  close (fd2);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
run_script (const char *fname, char *args[])
Packit 6c4009
{
Packit 6c4009
  /* We want to test the `execvpe' function.  To do this we restart the
Packit 6c4009
     program with an additional parameter.  */
Packit 6c4009
  int status;
Packit 6c4009
  pid_t pid = fork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      execvpe (fname, args, NULL);
Packit 6c4009
Packit 6c4009
      puts ("Cannot exec");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  else if (pid == (pid_t) -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("Cannot fork");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Wait for the child.  */
Packit 6c4009
  if (waitpid (pid, &status, 0) != pid)
Packit 6c4009
    {
Packit 6c4009
      puts ("Wrong child");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (WTERMSIG (status) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("Child terminated incorrectly");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_output (const char *expected)
Packit 6c4009
{
Packit 6c4009
  /* Check log output.  */
Packit 6c4009
  FILE *arq = fopen (logname, "r");
Packit 6c4009
  if (arq == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("Error opening output file");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char line[128];
Packit 6c4009
  if (fgets (line, sizeof (line), arq) == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("Error reading output file");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  fclose (arq);
Packit 6c4009
Packit 6c4009
  if (strcmp (line, expected) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("Output file different than expected");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if  (setenv ("PATH", test_dir, 1) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("setenv failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* First check resulting script run with some arguments results in correct
Packit 6c4009
     output file.  */
Packit 6c4009
  char *args1[] = { fname1, (char*) "1", (char *) "2", (char *) "3", NULL };
Packit 6c4009
  if (run_script (fname1,args1))
Packit 6c4009
    return 1;
Packit 6c4009
  if (check_output ("foo 1 2 3\n"))
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  /* Same as before but with an expected empty argument list.  */
Packit 6c4009
  char *args2[] = { fname2, NULL };
Packit 6c4009
  if (run_script (fname2, args2))
Packit 6c4009
    return 1;
Packit 6c4009
  if (check_output ("foo\n"))
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  /* Same as before but with an empty argument list.  */
Packit 6c4009
  char *args3[] = { NULL };
Packit 6c4009
  if (run_script (fname2, args3))
Packit 6c4009
    return 1;
Packit 6c4009
  if (check_output ("foo\n"))
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}