tst-mqueue9.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (C) 2004-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <mqueue.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <support/check.h>
  21. #include "tst-mqueue.h"
  22. #define TEST_FUNCTION do_test ()
  23. static int
  24. do_test (void)
  25. {
  26. if (geteuid () != 0)
  27. {
  28. puts ("this test requires root");
  29. return 0;
  30. }
  31. char name[sizeof "/tst-mqueue9-" + sizeof (pid_t) * 3];
  32. snprintf (name, sizeof (name), "/tst-mqueue9-%u", getpid ());
  33. struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
  34. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  35. if (q == (mqd_t) -1)
  36. {
  37. if (errno == ENOSYS)
  38. FAIL_UNSUPPORTED ("mq_open not supported");
  39. printf ("mq_open failed with: %m\n");
  40. return 1;
  41. }
  42. add_temp_mq (name);
  43. if (seteuid (1) != 0)
  44. {
  45. printf ("failed to seteuid (1): %m\n");
  46. mq_unlink (name);
  47. return 0;
  48. }
  49. int result = 0;
  50. if (mq_unlink (name) == 0)
  51. {
  52. puts ("mq_unlink unexpectedly succeeded");
  53. result = 1;
  54. }
  55. else if (errno != EACCES)
  56. {
  57. printf ("mq_unlink did not fail with EACCES: %m\n");
  58. result = 1;;
  59. }
  60. if (seteuid (0) != 0)
  61. {
  62. printf ("failed to seteuid (0): %m\n");
  63. result = 1;
  64. }
  65. if (mq_unlink (name) != 0)
  66. {
  67. printf ("mq_unlink failed with: %m\n");
  68. result = 1;
  69. }
  70. if (mq_close (q) != 0)
  71. {
  72. printf ("mq_close failed with: %m\n");
  73. result = 1;
  74. }
  75. return result;
  76. }
  77. #include "../test-skeleton.c"