Blame nptl/tst-bad-schedattr.c

Packit 6c4009
/* Test that pthread_create diagnoses invalid scheduling parameters.
Packit 6c4009
   Copyright (C) 2014-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 <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
thread_function (void *arg)
Packit 6c4009
{
Packit 6c4009
  abort ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
#if !defined SCHED_FIFO || !defined SCHED_OTHER
Packit 6c4009
  puts ("SCHED_FIFO or SCHED_OTHER not available at compile time");
Packit 6c4009
  return 0; /* 77 */
Packit 6c4009
#else
Packit 6c4009
Packit 6c4009
  int err;
Packit 6c4009
Packit 6c4009
#define TRY(func, arglist)                              \
Packit 6c4009
  if ((err = func arglist) != 0)                        \
Packit 6c4009
    {                                                   \
Packit 6c4009
      printf ("%s: %s\n", #func, strerror (err));       \
Packit 6c4009
      return 2;                                         \
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int fifo_max = sched_get_priority_max (SCHED_FIFO);
Packit 6c4009
  if (fifo_max == -1)
Packit 6c4009
    {
Packit 6c4009
      assert (errno == ENOTSUP || errno == ENOSYS);
Packit 6c4009
      puts ("SCHED_FIFO not supported, cannot test");
Packit 6c4009
      return 0; /* 77 */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int other_max = sched_get_priority_max (SCHED_OTHER);
Packit 6c4009
  if (other_max == -1)
Packit 6c4009
    {
Packit 6c4009
      assert (errno == ENOTSUP || errno == ENOSYS);
Packit 6c4009
      puts ("SCHED_OTHER not supported, cannot test");
Packit 6c4009
      return 0; /* 77 */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  assert (fifo_max > other_max);
Packit 6c4009
Packit 6c4009
  pthread_attr_t attr;
Packit 6c4009
  TRY (pthread_attr_init, (&attr));
Packit 6c4009
  TRY (pthread_attr_setinheritsched, (&attr, PTHREAD_EXPLICIT_SCHED));
Packit 6c4009
  TRY (pthread_attr_setschedpolicy, (&attr, SCHED_FIFO));
Packit 6c4009
Packit 6c4009
  /* This value is chosen so as to be valid for SCHED_FIFO but invalid for
Packit 6c4009
     SCHED_OTHER.  */
Packit 6c4009
  struct sched_param param = { .sched_priority = other_max + 1 };
Packit 6c4009
  TRY (pthread_attr_setschedparam, (&attr, &param));
Packit 6c4009
Packit 6c4009
  TRY (pthread_attr_setschedpolicy, (&attr, SCHED_OTHER));
Packit 6c4009
Packit 6c4009
  /* Now ATTR has a sched_param that is invalid for its policy.  */
Packit 6c4009
  pthread_t th;
Packit 6c4009
  err = pthread_create (&th, &attr, &thread_function, NULL);
Packit 6c4009
  if (err != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      printf ("pthread_create returned %d (%s), expected %d (EINVAL: %s)\n",
Packit 6c4009
              err, strerror (err), EINVAL, strerror (EINVAL));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"