Blame posix/tst-spawn3.c

Packit 6c4009
/* Check posix_spawn add file actions.
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 <spawn.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <sys/resource.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <paths.h>
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* The test checks if posix_spawn open file action close the file descriptor
Packit 6c4009
     before opening a new one in case the input file descriptor is already
Packit 6c4009
     opened.  It does by exhausting all file descriptors on the process before
Packit 6c4009
     issue posix_spawn.  It then issues a posix_spawn for '/bin/sh echo $$'
Packit 6c4009
     and add two rules:
Packit 6c4009
Packit 6c4009
     1. Redirect stdout to a temporary filepath
Packit 6c4009
     2. Redirect stderr to stdout
Packit 6c4009
Packit 6c4009
     If the implementation does not close the file 1. will fail with
Packit 6c4009
     EMFILE.  */
Packit 6c4009
Packit 6c4009
  struct rlimit rl;
Packit 6c4009
  int max_fd = 24;
Packit 6c4009
  int ret;
Packit 6c4009
Packit 6c4009
  /* Set maximum number of file descriptor to a low value to avoid open
Packit 6c4009
     too many files in environments where RLIMIT_NOFILE is large and to
Packit 6c4009
     limit the array size to track the opened file descriptors.  */
Packit 6c4009
Packit 6c4009
  if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
Packit 6c4009
    FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
Packit 6c4009
Packit 6c4009
  max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd);
Packit 6c4009
  rl.rlim_cur = max_fd;
Packit 6c4009
Packit 6c4009
  if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
Packit 6c4009
    FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
Packit 6c4009
Packit 6c4009
  /* Exhauste the file descriptor limit with temporary files.  */
Packit 6c4009
  int files[max_fd];
Packit 6c4009
  int nfiles = 0;
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      int fd = create_temp_file ("tst-spawn3.", NULL);
Packit 6c4009
      if (fd == -1)
Packit 6c4009
	{
Packit 6c4009
	  if (errno != EMFILE)
Packit 6c4009
	    FAIL_EXIT1 ("create_temp_file: %m");
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
      files[nfiles++] = fd;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  posix_spawn_file_actions_t a;
Packit 6c4009
  if (posix_spawn_file_actions_init (&a) != 0)
Packit 6c4009
    FAIL_EXIT1 ("posix_spawn_file_actions_init");
Packit 6c4009
Packit 6c4009
  /* Executes a /bin/sh echo $$ 2>&1 > ${objpfx}tst-spawn3.pid .  */
Packit 6c4009
  const char pidfile[] = OBJPFX "tst-spawn3.pid";
Packit 6c4009
  if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY |
Packit 6c4009
					O_CREAT | O_TRUNC, 0644) != 0)
Packit 6c4009
    FAIL_EXIT1 ("posix_spawn_file_actions_addopen");
Packit 6c4009
Packit 6c4009
  if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0)
Packit 6c4009
    FAIL_EXIT1 ("posix_spawn_file_actions_adddup2");
Packit 6c4009
Packit 6c4009
  /* Since execve (called by posix_spawn) might require to open files to
Packit 6c4009
     actually execute the shell script, setup to close the temporary file
Packit 6c4009
     descriptors.  */
Packit 6c4009
  for (int i=0; i
Packit 6c4009
    {
Packit 6c4009
      if (posix_spawn_file_actions_addclose (&a, files[i]))
Packit 6c4009
	FAIL_EXIT1 ("posix_spawn_file_actions_addclose");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char *spawn_argv[] = { (char *) _PATH_BSHELL, (char *) "-c",
Packit 6c4009
			 (char *) "echo $$", NULL };
Packit 6c4009
  pid_t pid;
Packit 6c4009
  if ((ret = posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL))
Packit 6c4009
       != 0)
Packit 6c4009
    {
Packit 6c4009
      errno = ret;
Packit 6c4009
      FAIL_EXIT1 ("posix_spawn: %m");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int status;
Packit 6c4009
  int err = waitpid (pid, &status, 0);
Packit 6c4009
  if (err != pid)
Packit 6c4009
    FAIL_EXIT1 ("waitpid: %m");
Packit 6c4009
Packit 6c4009
  /* Close the temporary files descriptor so it can check posix_spawn
Packit 6c4009
     output.  */
Packit 6c4009
  for (int i=0; i
Packit 6c4009
    {
Packit 6c4009
      if (close (files[i]))
Packit 6c4009
	FAIL_EXIT1 ("close: %m");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int pidfd = open (pidfile, O_RDONLY);
Packit 6c4009
  if (pidfd == -1)
Packit 6c4009
    FAIL_EXIT1 ("open: %m");
Packit 6c4009
Packit 6c4009
  char buf[64];
Packit 6c4009
  ssize_t n;
Packit 6c4009
  if ((n = read (pidfd, buf, sizeof (buf))) < 0)
Packit 6c4009
    FAIL_EXIT1 ("read: %m");
Packit 6c4009
Packit 6c4009
  unlink (pidfile);
Packit 6c4009
Packit 6c4009
  /* We only expect to read the PID.  */
Packit 6c4009
  char *endp;
Packit 6c4009
  long int rpid = strtol (buf, &endp, 10);
Packit 6c4009
  if (*endp != '\n')
Packit 6c4009
    FAIL_EXIT1 ("*endp != \'n\'");
Packit 6c4009
  if (endp == buf)
Packit 6c4009
    FAIL_EXIT1 ("read empty line");
Packit 6c4009
Packit 6c4009
  if (rpid != pid)
Packit 6c4009
    FAIL_EXIT1 ("found \"%s\", expected pid %ld\n", buf, (long int) pid);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>