tst-bad-schedattr.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Test that pthread_create diagnoses invalid scheduling parameters.
  2. Copyright (C) 2014-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. static void *
  22. thread_function (void *arg)
  23. {
  24. abort ();
  25. }
  26. static int
  27. do_test (void)
  28. {
  29. #if !defined SCHED_FIFO || !defined SCHED_OTHER
  30. puts ("SCHED_FIFO or SCHED_OTHER not available at compile time");
  31. return 0; /* 77 */
  32. #else
  33. int err;
  34. #define TRY(func, arglist) \
  35. if ((err = func arglist) != 0) \
  36. { \
  37. printf ("%s: %s\n", #func, strerror (err)); \
  38. return 2; \
  39. }
  40. int fifo_max = sched_get_priority_max (SCHED_FIFO);
  41. if (fifo_max == -1)
  42. {
  43. assert (errno == ENOTSUP || errno == ENOSYS);
  44. puts ("SCHED_FIFO not supported, cannot test");
  45. return 0; /* 77 */
  46. }
  47. int other_max = sched_get_priority_max (SCHED_OTHER);
  48. if (other_max == -1)
  49. {
  50. assert (errno == ENOTSUP || errno == ENOSYS);
  51. puts ("SCHED_OTHER not supported, cannot test");
  52. return 0; /* 77 */
  53. }
  54. assert (fifo_max > other_max);
  55. pthread_attr_t attr;
  56. TRY (pthread_attr_init, (&attr));
  57. TRY (pthread_attr_setinheritsched, (&attr, PTHREAD_EXPLICIT_SCHED));
  58. TRY (pthread_attr_setschedpolicy, (&attr, SCHED_FIFO));
  59. /* This value is chosen so as to be valid for SCHED_FIFO but invalid for
  60. SCHED_OTHER. */
  61. struct sched_param param = { .sched_priority = other_max + 1 };
  62. TRY (pthread_attr_setschedparam, (&attr, &param));
  63. TRY (pthread_attr_setschedpolicy, (&attr, SCHED_OTHER));
  64. /* Now ATTR has a sched_param that is invalid for its policy. */
  65. pthread_t th;
  66. err = pthread_create (&th, &attr, &thread_function, NULL);
  67. if (err != EINVAL)
  68. {
  69. printf ("pthread_create returned %d (%s), expected %d (EINVAL: %s)\n",
  70. err, strerror (err), EINVAL, strerror (EINVAL));
  71. return 1;
  72. }
  73. return 0;
  74. #endif
  75. }
  76. #define TEST_FUNCTION do_test ()
  77. #include "../test-skeleton.c"