mq_notify.c 7.9 KB

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