mq_notify.c 7.9 KB

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