Blame nptl/tst-cancel4-common.h

Packit 6c4009
/* Common definition for tst-cancel4_* tests.
Packit 6c4009
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 <pthread.h>
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
Packit 6c4009
/* Pipe descriptors.  */
Packit 6c4009
static int fds[2];
Packit 6c4009
Packit 6c4009
/* Temporary file descriptor, to be closed after each round.  */
Packit 6c4009
static int tempfd = -1;
Packit 6c4009
static int tempfd2 = -1;
Packit 6c4009
/* Name of temporary file to be removed after each round.  */
Packit 6c4009
static char *tempfname;
Packit 6c4009
/* Temporary message queue.  */
Packit 6c4009
static int tempmsg = -1;
Packit 6c4009
Packit 6c4009
/* Often used barrier for two threads.  */
Packit 6c4009
static pthread_barrier_t b2;
Packit 6c4009
Packit 6c4009
/* The WRITE_BUFFER_SIZE value needs to be chosen such that if we set
Packit 6c4009
   the socket send buffer size to '1', a write of this size on that
Packit 6c4009
   socket will block.
Packit 6c4009
Packit 6c4009
   The Linux kernel imposes a minimum send socket buffer size which
Packit 6c4009
   has changed over the years.  As of Linux 3.10 the value is:
Packit 6c4009
Packit 6c4009
     2 * (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
Packit 6c4009
Packit 6c4009
   which is attempting to make sure that with standard MTUs,
Packit 6c4009
   TCP can always queue up at least 2 full sized packets.
Packit 6c4009
Packit 6c4009
   Furthermore, there is logic in the socket send paths that
Packit 6c4009
   will allow one more packet (of any size) to be queued up as
Packit 6c4009
   long as some socket buffer space remains.   Blocking only
Packit 6c4009
   occurs when we try to queue up a new packet and the send
Packit 6c4009
   buffer space has already been fully consumed.
Packit 6c4009
Packit 6c4009
   Therefore we must set this value to the largest possible value of
Packit 6c4009
   the formula above (and since it depends upon the size of "struct
Packit 6c4009
   sk_buff", it is dependent upon machine word size etc.) plus some
Packit 6c4009
   slack space.  */
Packit 6c4009
Packit 6c4009
#define WRITE_BUFFER_SIZE 16384
Packit 6c4009
Packit 6c4009
/* Set the send buffer of socket S to 1 byte so any send operation
Packit 6c4009
   done with WRITE_BUFFER_SIZE bytes will force syscall blocking.  */
Packit 6c4009
static void
Packit 6c4009
set_socket_buffer (int s)
Packit 6c4009
{
Packit 6c4009
  int val = 1;
Packit 6c4009
  socklen_t len = sizeof(val);
Packit 6c4009
Packit 6c4009
  TEST_VERIFY_EXIT (setsockopt (s, SOL_SOCKET, SO_SNDBUF, &val,
Packit 6c4009
		    sizeof(val)) == 0);
Packit 6c4009
  TEST_VERIFY_EXIT (getsockopt (s, SOL_SOCKET, SO_SNDBUF, &val, &len) == 0);
Packit 6c4009
  TEST_VERIFY_EXIT (val < WRITE_BUFFER_SIZE);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Cleanup handling test.  */
Packit 6c4009
static int cl_called;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
cl (void *arg)
Packit 6c4009
{
Packit 6c4009
  ++cl_called;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Named pipe used to check for blocking open.  It should be closed
Packit 6c4009
   after the cancellation handling.  */
Packit 6c4009
static char fifoname[] = "/tmp/tst-cancel4-fifo-XXXXXX";
Packit 6c4009
static int fifofd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((used))
Packit 6c4009
cl_fifo (void *arg)
Packit 6c4009
{
Packit 6c4009
  ++cl_called;
Packit 6c4009
Packit 6c4009
  unlink (fifoname);
Packit 6c4009
  close (fifofd);
Packit 6c4009
  fifofd = -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct cancel_tests
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  void *(*tf) (void *);
Packit 6c4009
  int nb;
Packit 6c4009
  int only_early;
Packit 6c4009
};
Packit 6c4009
#define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }