tst-mqueue7.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Test all open message queues descriptors are closed during exec*.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <mqueue.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/wait.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #define OPT_AFTEREXEC 20000
  27. static mqd_t after_exec = (mqd_t) -1;
  28. #define CMDLINE_OPTIONS \
  29. { "after-exec", required_argument, NULL, OPT_AFTEREXEC },
  30. #define CMDLINE_PROCESS \
  31. case OPT_AFTEREXEC: \
  32. after_exec = (mqd_t) strtoul (optarg, NULL, 0); \
  33. break;
  34. static int
  35. do_after_exec (void)
  36. {
  37. int result = 0;
  38. struct mq_attr attr;
  39. if (mq_getattr (after_exec, &attr) == 0)
  40. {
  41. puts ("mq_getattr after exec unexpectedly succeeded");
  42. result = 1;
  43. }
  44. else if (errno != EBADF)
  45. {
  46. printf ("mq_getattr after exec did not fail with EBADF: %m\n");
  47. result = 1;
  48. }
  49. return result;
  50. }
  51. static int
  52. do_test (int argc, char **argv)
  53. {
  54. if (after_exec != (mqd_t) -1)
  55. return do_after_exec ();
  56. char name[sizeof "/tst-mqueue7-" + sizeof (pid_t) * 3];
  57. snprintf (name, sizeof (name), "/tst-mqueue7-%u", getpid ());
  58. struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
  59. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
  60. if (q == (mqd_t) -1)
  61. {
  62. printf ("mq_open failed with: %m\n");
  63. return 0;
  64. }
  65. else if (mq_unlink (name) != 0)
  66. {
  67. printf ("mq_unlink failed with: %m\n");
  68. return 1;
  69. }
  70. if (mq_getattr (q, &attr) != 0)
  71. {
  72. printf ("mq_getattr failed: %m\n");
  73. return 1;
  74. }
  75. char after_exec_arg[sizeof "--after-exec=0x" + sizeof (long) * 3];
  76. snprintf (after_exec_arg, sizeof (after_exec_arg),
  77. "--after-exec=0x%lx", (long) q);
  78. const char *newargv[argc + 2];
  79. for (int i = 1; i < argc; ++i)
  80. newargv[i - 1] = argv[i];
  81. newargv[argc - 1] = "--direct";
  82. newargv[argc] = after_exec_arg;
  83. newargv[argc + 1] = NULL;
  84. /* Verify that exec* has the effect of mq_close (q). */
  85. execv (newargv[0], (char * const *) newargv);
  86. printf ("execv failed: %m\n");
  87. return 1;
  88. }
  89. #include "../test-skeleton.c"