tst-mqueue3.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Test SIGEV_THREAD handling for POSIX message queues.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@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 <mqueue.h>
  19. #include <signal.h>
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/mman.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. #if _POSIX_THREADS
  29. # include <pthread.h>
  30. static pid_t pid;
  31. static mqd_t m;
  32. static const char message[] = "hello";
  33. # define MAXMSG 10
  34. # define MSGSIZE 10
  35. # define UNIQUE 42
  36. static void
  37. fct (union sigval s)
  38. {
  39. /* Put the mq in non-blocking mode. */
  40. struct mq_attr attr;
  41. if (mq_getattr (m, &attr) != 0)
  42. {
  43. printf ("%s: mq_getattr failed: %m\n", __FUNCTION__);
  44. exit (1);
  45. }
  46. attr.mq_flags |= O_NONBLOCK;
  47. if (mq_setattr (m, &attr, NULL) != 0)
  48. {
  49. printf ("%s: mq_setattr failed: %m\n", __FUNCTION__);
  50. exit (1);
  51. }
  52. /* Check the values. */
  53. if (attr.mq_maxmsg != MAXMSG)
  54. {
  55. printf ("%s: mq_maxmsg wrong: is %ld, expecte %d\n",
  56. __FUNCTION__, attr.mq_maxmsg, MAXMSG);
  57. exit (1);
  58. }
  59. if (attr.mq_msgsize != MAXMSG)
  60. {
  61. printf ("%s: mq_msgsize wrong: is %ld, expecte %d\n",
  62. __FUNCTION__, attr.mq_msgsize, MSGSIZE);
  63. exit (1);
  64. }
  65. /* Read the message. */
  66. char buf[attr.mq_msgsize];
  67. ssize_t n = TEMP_FAILURE_RETRY (mq_receive (m, buf, attr.mq_msgsize, NULL));
  68. if (n != sizeof (message))
  69. {
  70. printf ("%s: length of message wrong: is %zd, expected %zu\n",
  71. __FUNCTION__, n, sizeof (message));
  72. exit (1);
  73. }
  74. if (memcmp (buf, message, sizeof (message)) != 0)
  75. {
  76. printf ("%s: message wrong: is \"%s\", expected \"%s\"\n",
  77. __FUNCTION__, buf, message);
  78. exit (1);
  79. }
  80. exit (UNIQUE);
  81. }
  82. int
  83. do_test (void)
  84. {
  85. char tmpfname[] = "/tmp/tst-mqueue3-barrier.XXXXXX";
  86. int fd = mkstemp (tmpfname);
  87. if (fd == -1)
  88. {
  89. printf ("cannot open temporary file: %m\n");
  90. return 1;
  91. }
  92. /* Make sure it is always removed. */
  93. unlink (tmpfname);
  94. /* Create one page of data. */
  95. size_t ps = sysconf (_SC_PAGESIZE);
  96. char data[ps];
  97. memset (data, '\0', ps);
  98. /* Write the data to the file. */
  99. if (write (fd, data, ps) != (ssize_t) ps)
  100. {
  101. puts ("short write");
  102. return 1;
  103. }
  104. void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  105. if (mem == MAP_FAILED)
  106. {
  107. printf ("mmap failed: %m\n");
  108. return 1;
  109. }
  110. pthread_barrier_t *b;
  111. b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
  112. & ~(__alignof (pthread_barrier_t) - 1));
  113. pthread_barrierattr_t a;
  114. if (pthread_barrierattr_init (&a) != 0)
  115. {
  116. puts ("barrierattr_init failed");
  117. return 1;
  118. }
  119. if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
  120. {
  121. puts ("barrierattr_setpshared failed, could not test");
  122. return 0;
  123. }
  124. if (pthread_barrier_init (b, &a, 2) != 0)
  125. {
  126. puts ("barrier_init failed");
  127. return 1;
  128. }
  129. if (pthread_barrierattr_destroy (&a) != 0)
  130. {
  131. puts ("barrierattr_destroy failed");
  132. return 1;
  133. }
  134. /* Name for the message queue. */
  135. char mqname[sizeof ("/tst-mqueue3-") + 3 * sizeof (pid_t)];
  136. snprintf (mqname, sizeof (mqname) - 1, "/tst-mqueue3-%ld",
  137. (long int) getpid ());
  138. /* Create the message queue. */
  139. struct mq_attr attr = { .mq_maxmsg = MAXMSG, .mq_msgsize = MSGSIZE };
  140. m = mq_open (mqname, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  141. if (m == -1)
  142. {
  143. if (errno == ENOSYS)
  144. {
  145. puts ("not implemented");
  146. return 0;
  147. }
  148. puts ("mq_open failed");
  149. return 1;
  150. }
  151. /* Unlink the message queue right away. */
  152. if (mq_unlink (mqname) != 0)
  153. {
  154. puts ("mq_unlink failed");
  155. return 1;
  156. }
  157. pid = fork ();
  158. if (pid == -1)
  159. {
  160. puts ("fork failed");
  161. return 1;
  162. }
  163. if (pid == 0)
  164. {
  165. /* Request notification via thread. */
  166. struct sigevent ev;
  167. ev.sigev_notify = SIGEV_THREAD;
  168. ev.sigev_notify_function = fct;
  169. ev.sigev_value.sival_ptr = NULL;
  170. ev.sigev_notify_attributes = NULL;
  171. /* Tell the kernel. */
  172. if (mq_notify (m,&ev) != 0)
  173. {
  174. puts ("mq_notify failed");
  175. exit (1);
  176. }
  177. /* Tell the parent we are ready. */
  178. (void) pthread_barrier_wait (b);
  179. /* Make sure the process goes away eventually. */
  180. alarm (10);
  181. /* Do nothing forever. */
  182. while (1)
  183. pause ();
  184. }
  185. /* Wait for the child process to register to notification method. */
  186. (void) pthread_barrier_wait (b);
  187. /* Send the message. */
  188. if (mq_send (m, message, sizeof (message), 1) != 0)
  189. {
  190. kill (pid, SIGKILL);
  191. puts ("mq_send failed");
  192. return 1;
  193. }
  194. int r;
  195. if (TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)) != pid)
  196. {
  197. kill (pid, SIGKILL);
  198. puts ("waitpid failed");
  199. return 1;
  200. }
  201. return WIFEXITED (r) && WEXITSTATUS (r) == UNIQUE ? 0 : 1;
  202. }
  203. # define TEST_FUNCTION do_test ()
  204. #else
  205. # define TEST_FUNCTION 0
  206. #endif
  207. #include "../test-skeleton.c"