tst-mqueue9.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  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 <errno.h>
  16. #include <mqueue.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.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. printf ("mq_open failed with: %m\n");
  38. return 0;
  39. }
  40. else
  41. add_temp_mq (name);
  42. if (seteuid (1) != 0)
  43. {
  44. printf ("failed to seteuid (1): %m\n");
  45. mq_unlink (name);
  46. return 0;
  47. }
  48. int result = 0;
  49. if (mq_unlink (name) == 0)
  50. {
  51. puts ("mq_unlink unexpectedly succeeded");
  52. result = 1;
  53. }
  54. else if (errno != EACCES)
  55. {
  56. printf ("mq_unlink did not fail with EACCES: %m\n");
  57. result = 1;;
  58. }
  59. if (seteuid (0) != 0)
  60. {
  61. printf ("failed to seteuid (0): %m\n");
  62. result = 1;
  63. }
  64. if (mq_unlink (name) != 0)
  65. {
  66. printf ("mq_unlink failed with: %m\n");
  67. result = 1;
  68. }
  69. if (mq_close (q) != 0)
  70. {
  71. printf ("mq_close failed with: %m\n");
  72. result = 1;
  73. }
  74. return result;
  75. }
  76. #include "../test-skeleton.c"