mq_notify.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contribute by Ulrich Drepper <drepper@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 <assert.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <mqueue.h>
  19. #include <pthread.h>
  20. #include <signal.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sysdep.h>
  24. #include <unistd.h>
  25. #include <sys/socket.h>
  26. #include <not-cancel.h>
  27. #ifdef __NR_mq_notify
  28. /* Defined in the kernel headers: */
  29. #define NOTIFY_COOKIE_LEN 32 /* Length of the cookie used. */
  30. #define NOTIFY_WOKENUP 1 /* Code for notifcation. */
  31. #define NOTIFY_REMOVED 2 /* Code for closed message queue
  32. of de-notifcation. */
  33. /* Data structure for the queued notification requests. */
  34. union notify_data
  35. {
  36. struct
  37. {
  38. void (*fct) (union sigval); /* The function to run. */
  39. union sigval param; /* The parameter to pass. */
  40. pthread_attr_t *attr; /* Attributes to create the thread with. */
  41. /* NB: on 64-bit machines the struct as a size of 24 bytes. Which means
  42. byte 31 can still be used for returning the status. */
  43. };
  44. char raw[NOTIFY_COOKIE_LEN];
  45. };
  46. /* Keep track of the initialization. */
  47. static pthread_once_t once = PTHREAD_ONCE_INIT;
  48. /* The netlink socket. */
  49. static int netlink_socket = -1;
  50. /* Barrier used to make sure data passed to the new thread is not
  51. resused by the parent. */
  52. static pthread_barrier_t notify_barrier;
  53. /* Modify the signal mask. We move this into a separate function so
  54. that the stack space needed for sigset_t is not deducted from what
  55. the thread can use. */
  56. static int
  57. __attribute__ ((noinline))
  58. change_sigmask (int how, sigset_t *oss)
  59. {
  60. sigset_t ss;
  61. __sigfillset (&ss);
  62. return pthread_sigmask (how, &ss, oss);
  63. }
  64. /* The function used for the notification. */
  65. static void *
  66. notification_function (void *arg)
  67. {
  68. /* Copy the function and parameter so that the parent thread can go
  69. on with its life. */
  70. __volatile__ union notify_data *data = (__volatile__ union notify_data *) arg;
  71. void (*fct) (union sigval) = data->fct;
  72. union sigval param = data->param;
  73. /* Let the parent go. */
  74. (void) pthread_barrier_wait (&notify_barrier);
  75. /* Make the thread detached. */
  76. (void) pthread_detach (pthread_self ());
  77. /* The parent thread has all signals blocked. This is probably a
  78. bit surprising for this thread. So we unblock all of them. */
  79. (void) change_sigmask (SIG_UNBLOCK, NULL);
  80. /* Now run the user code. */
  81. fct (param);
  82. /* And we are done. */
  83. return NULL;
  84. }
  85. /* Helper thread. */
  86. static void *
  87. helper_thread (void *arg)
  88. {
  89. while (1)
  90. {
  91. union notify_data data;
  92. ssize_t n = recv (netlink_socket, &data, sizeof (data),
  93. MSG_NOSIGNAL | MSG_WAITALL);
  94. if (n < NOTIFY_COOKIE_LEN)
  95. continue;
  96. if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_WOKENUP)
  97. {
  98. /* Just create the thread as instructed. There is no way to
  99. report a problem with creating a thread. */
  100. pthread_t th;
  101. if (__builtin_expect (pthread_create (&th, data.attr,
  102. notification_function, &data)
  103. == 0, 0))
  104. /* Since we passed a pointer to DATA to the new thread we have
  105. to wait until it is done with it. */
  106. (void) pthread_barrier_wait (&notify_barrier);
  107. }
  108. else if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_REMOVED)
  109. /* The only state we keep is the copy of the thread attributes. */
  110. free (data.attr);
  111. }
  112. return NULL;
  113. }
  114. static void
  115. reset_once (void)
  116. {
  117. once = PTHREAD_ONCE_INIT;
  118. }
  119. static void
  120. init_mq_netlink (void)
  121. {
  122. /* This code might be called a second time after fork(). The file
  123. descriptor is inherited from the parent. */
  124. if (netlink_socket == -1)
  125. {
  126. /* Just a normal netlink socket, not bound. */
  127. netlink_socket = socket (AF_NETLINK, SOCK_RAW, 0);
  128. /* No need to do more if we have no socket. */
  129. if (netlink_socket == -1)
  130. return;
  131. /* Make sure the descriptor is closed on exec. */
  132. fcntl (netlink_socket, F_SETFD, FD_CLOEXEC);
  133. }
  134. int err = 1;
  135. /* Initialize the barrier. */
  136. if (__builtin_expect (pthread_barrier_init (&notify_barrier, NULL, 2) == 0,
  137. 0))
  138. {
  139. /* Create the helper thread. */
  140. pthread_attr_t attr;
  141. (void) pthread_attr_init (&attr);
  142. (void) pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  143. /* We do not need much stack space, the bare minimum will be enough. */
  144. (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
  145. /* Temporarily block all signals so that the newly created
  146. thread inherits the mask. */
  147. sigset_t oss;
  148. int have_no_oss = change_sigmask (SIG_BLOCK, &oss);
  149. pthread_t th;
  150. err = pthread_create (&th, &attr, helper_thread, NULL);
  151. /* Reset the signal mask. */
  152. if (!have_no_oss)
  153. pthread_sigmask (SIG_SETMASK, &oss, NULL);
  154. (void) pthread_attr_destroy (&attr);
  155. if (err == 0)
  156. {
  157. static smallint added_atfork;
  158. if (added_atfork == 0
  159. && pthread_atfork (NULL, NULL, reset_once) != 0)
  160. {
  161. /* The child thread will call recv() which is a
  162. cancellation point. */
  163. (void) pthread_cancel (th);
  164. err = 1;
  165. }
  166. else
  167. added_atfork = 1;
  168. }
  169. }
  170. if (err != 0)
  171. {
  172. close_not_cancel_no_status (netlink_socket);
  173. netlink_socket = -1;
  174. }
  175. }
  176. /* Register notification upon message arrival to an empty message queue
  177. MQDES. */
  178. int
  179. mq_notify (mqd_t mqdes, const struct sigevent *notification)
  180. {
  181. /* Make sure the type is correctly defined. */
  182. assert (sizeof (union notify_data) == NOTIFY_COOKIE_LEN);
  183. /* Special treatment needed for SIGEV_THREAD. */
  184. if (notification == NULL || notification->sigev_notify != SIGEV_THREAD)
  185. return INLINE_SYSCALL (mq_notify, 2, mqdes, notification);
  186. /* The kernel cannot directly start threads. This will have to be
  187. done at userlevel. Since we cannot start threads from signal
  188. handlers we have to create a dedicated thread which waits for
  189. notifications for arriving messages and creates threads in
  190. response. */
  191. /* Initialize only once. */
  192. pthread_once (&once, init_mq_netlink);
  193. /* If we cannot create the netlink socket we cannot provide
  194. SIGEV_THREAD support. */
  195. if (__builtin_expect (netlink_socket == -1, 0))
  196. {
  197. __set_errno (ENOSYS);
  198. return -1;
  199. }
  200. /* Create the cookie. It will hold almost all the state. */
  201. union notify_data data;
  202. memset (&data, '\0', sizeof (data));
  203. data.fct = notification->sigev_notify_function;
  204. data.param = notification->sigev_value;
  205. if (notification->sigev_notify_attributes != NULL)
  206. {
  207. /* The thread attribute has to be allocated separately. */
  208. data.attr = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  209. if (data.attr == NULL)
  210. return -1;
  211. memcpy (data.attr, notification->sigev_notify_attributes,
  212. sizeof (pthread_attr_t));
  213. }
  214. /* Construct the new request. */
  215. struct sigevent se;
  216. se.sigev_notify = SIGEV_THREAD;
  217. se.sigev_signo = netlink_socket;
  218. se.sigev_value.sival_ptr = &data;
  219. /* Tell the kernel. */
  220. int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se);
  221. /* If it failed, free the allocated memory. */
  222. if (__builtin_expect (retval != 0, 0))
  223. free (data.attr);
  224. return retval;
  225. }
  226. #else
  227. # include <rt/mq_notify.c>
  228. #endif