tst-mqueue9.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <mqueue.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include "tst-mqueue.h"
  23. #define TEST_FUNCTION do_test ()
  24. static int
  25. do_test (void)
  26. {
  27. if (geteuid () != 0)
  28. {
  29. puts ("this test requires root");
  30. return 0;
  31. }
  32. char name[sizeof "/tst-mqueue9-" + sizeof (pid_t) * 3];
  33. snprintf (name, sizeof (name), "/tst-mqueue9-%u", getpid ());
  34. struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
  35. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  36. if (q == (mqd_t) -1)
  37. {
  38. printf ("mq_open failed with: %m\n");
  39. return 0;
  40. }
  41. else
  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"