Blame misc/tst-pselect.c

Packit 6c4009
/* Copyright (C) 2006-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 <errno.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/select.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static volatile int handler_called;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
handler (int sig)
Packit 6c4009
{
Packit 6c4009
  handler_called = 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  struct sigaction sa;
Packit 6c4009
  sa.sa_handler = handler;
Packit 6c4009
  sa.sa_flags = 0;
Packit 6c4009
  sigemptyset (&sa.sa_mask);
Packit 6c4009
Packit 6c4009
  if (sigaction (SIGUSR1, &sa, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("sigaction failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  sa.sa_handler = SIG_IGN;
Packit 6c4009
  if (sigaction (SIGCHLD, &sa, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd sigaction failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  sigset_t ss_usr1;
Packit 6c4009
  sigemptyset (&ss_usr1);
Packit 6c4009
  sigaddset (&ss_usr1, SIGUSR1);
Packit 6c4009
  if (sigprocmask (SIG_BLOCK, &ss_usr1, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("sigprocmask failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int fds[2][2];
Packit 6c4009
Packit 6c4009
  if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("pipe failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd_set rfds;
Packit 6c4009
  FD_ZERO (&rfds);
Packit 6c4009
Packit 6c4009
  sigset_t ss;
Packit 6c4009
  sigprocmask (SIG_SETMASK, NULL, &ss);
Packit 6c4009
  sigdelset (&ss, SIGUSR1);
Packit 6c4009
Packit 6c4009
  struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
Packit 6c4009
Packit 6c4009
  pid_t parent = getpid ();
Packit 6c4009
  pid_t p = fork ();
Packit 6c4009
  if (p == 0)
Packit 6c4009
    {
Packit 6c4009
      close (fds[0][1]);
Packit 6c4009
      close (fds[1][0]);
Packit 6c4009
Packit 6c4009
      FD_SET (fds[0][0], &rfds);
Packit 6c4009
Packit 6c4009
      int e;
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  if (getppid () != parent)
Packit 6c4009
	    exit (2);
Packit 6c4009
Packit 6c4009
	  errno = 0;
Packit 6c4009
	  e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
Packit 6c4009
	}
Packit 6c4009
      while (e == 0);
Packit 6c4009
Packit 6c4009
      if (e != -1)
Packit 6c4009
	{
Packit 6c4009
	  puts ("child: pselect did not fail");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
      if (errno != EINTR)
Packit 6c4009
	{
Packit 6c4009
	  puts ("child: pselect did not set errno to EINTR");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
Packit 6c4009
Packit 6c4009
      exit (0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fds[0][0]);
Packit 6c4009
  close (fds[1][1]);
Packit 6c4009
Packit 6c4009
  FD_SET (fds[1][0], &rfds);
Packit 6c4009
Packit 6c4009
  kill (p, SIGUSR1);
Packit 6c4009
Packit 6c4009
  int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
Packit 6c4009
  if (e == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: pselect failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (e != 1)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: pselect did not report readable fd");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (!FD_ISSET (fds[1][0], &rfds))
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: pselect reports wrong fd");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"