Blame posix/tst-spawn2.c

Packit Service 82fcde
/* Further tests for spawn in case of invalid binary paths.
Packit Service 82fcde
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <spawn.h>
Packit Service 82fcde
#include <error.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <sys/wait.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  /* Check if posix_spawn correctly returns an error and an invalid pid
Packit Service 82fcde
     by trying to spawn an invalid binary.  */
Packit Service 82fcde
Packit Service 82fcde
  const char *program = "/path/to/invalid/binary";
Packit Service 82fcde
  char * const args[] = { 0 };
Packit Service 82fcde
  pid_t pid = -1;
Packit Service 82fcde
Packit Service 82fcde
  int ret = posix_spawn (&pid, program, 0, 0, args, environ);
Packit Service 82fcde
  if (ret != ENOENT)
Packit Service 82fcde
    {
Packit Service 82fcde
      errno = ret;
Packit Service 82fcde
      FAIL_EXIT1 ("posix_spawn: %m");
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* POSIX states the value returned on pid variable in case of an error
Packit Service 82fcde
     is not specified.  GLIBC will update the value iff the child
Packit Service 82fcde
     execution is successful.  */
Packit Service 82fcde
  if (pid != -1)
Packit Service 82fcde
    FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid);
Packit Service 82fcde
Packit Service 82fcde
  /* Check if no child is actually created.  */
Packit Service 82fcde
  ret = waitpid (-1, NULL, 0);
Packit Service 82fcde
  if (ret != -1 || errno != ECHILD)
Packit Service 82fcde
    FAIL_EXIT1 ("waitpid: %m)");
Packit Service 82fcde
Packit Service 82fcde
  /* Same as before, but with posix_spawnp.  */
Packit Service 82fcde
  char *args2[] = { (char*) program, 0 };
Packit Service 82fcde
Packit Service 82fcde
  ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
Packit Service 82fcde
  if (ret != ENOENT)
Packit Service 82fcde
    {
Packit Service 82fcde
      errno = ret;
Packit Service 82fcde
      FAIL_EXIT1 ("posix_spawnp: %m");
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  if (pid != -1)
Packit Service 82fcde
    FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid);
Packit Service 82fcde
Packit Service 82fcde
  ret = waitpid (-1, NULL, 0);
Packit Service 82fcde
  if (ret != -1 || errno != ECHILD)
Packit Service 82fcde
    FAIL_EXIT1 ("waitpid: %m)");
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>