mq_notify.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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, 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. #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. fcntl (netlink_socket, F_SETFD, FD_CLOEXEC);
  134. }
  135. int err = 1;
  136. /* Initialize the barrier. */
  137. if (__builtin_expect (pthread_barrier_init (&notify_barrier, NULL, 2) == 0,
  138. 0))
  139. {
  140. /* Create the helper thread. */
  141. pthread_attr_t attr;
  142. (void) pthread_attr_init (&attr);
  143. (void) pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  144. /* We do not need much stack space, the bare minimum will be enough. */
  145. (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
  146. /* Temporarily block all signals so that the newly created
  147. thread inherits the mask. */
  148. sigset_t oss;
  149. int have_no_oss = change_sigmask (SIG_BLOCK, &oss);
  150. pthread_t th;
  151. err = pthread_create (&th, &attr, helper_thread, NULL);
  152. /* Reset the signal mask. */
  153. if (!have_no_oss)
  154. pthread_sigmask (SIG_SETMASK, &oss, NULL);
  155. (void) pthread_attr_destroy (&attr);
  156. if (err == 0)
  157. {
  158. static smallint added_atfork;
  159. if (added_atfork == 0
  160. && pthread_atfork (NULL, NULL, reset_once) != 0)
  161. {
  162. /* The child thread will call recv() which is a
  163. cancellation point. */
  164. (void) pthread_cancel (th);
  165. err = 1;
  166. }
  167. else
  168. added_atfork = 1;
  169. }
  170. }
  171. if (err != 0)
  172. {
  173. close_not_cancel_no_status (netlink_socket);
  174. netlink_socket = -1;
  175. }
  176. }
  177. /* Register notification upon message arrival to an empty message queue
  178. MQDES. */
  179. int
  180. mq_notify (mqd_t mqdes, const struct sigevent *notification)
  181. {
  182. /* Make sure the type is correctly defined. */
  183. assert (sizeof (union notify_data) == NOTIFY_COOKIE_LEN);
  184. /* Special treatment needed for SIGEV_THREAD. */
  185. if (notification == NULL || notification->sigev_notify != SIGEV_THREAD)
  186. return INLINE_SYSCALL (mq_notify, 2, mqdes, notification);
  187. /* The kernel cannot directly start threads. This will have to be
  188. done at userlevel. Since we cannot start threads from signal
  189. handlers we have to create a dedicated thread which waits for
  190. notifications for arriving messages and creates threads in
  191. response. */
  192. /* Initialize only once. */
  193. pthread_once (&once, init_mq_netlink);
  194. /* If we cannot create the netlink socket we cannot provide
  195. SIGEV_THREAD support. */
  196. if (__builtin_expect (netlink_socket == -1, 0))
  197. {
  198. __set_errno (ENOSYS);
  199. return -1;
  200. }
  201. /* Create the cookie. It will hold almost all the state. */
  202. union notify_data data;
  203. memset (&data, '\0', sizeof (data));
  204. data.fct = notification->sigev_notify_function;
  205. data.param = notification->sigev_value;
  206. if (notification->sigev_notify_attributes != NULL)
  207. {
  208. /* The thread attribute has to be allocated separately. */
  209. data.attr = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  210. if (data.attr == NULL)
  211. return -1;
  212. memcpy (data.attr, notification->sigev_notify_attributes,
  213. sizeof (pthread_attr_t));
  214. }
  215. /* Construct the new request. */
  216. struct sigevent se;
  217. se.sigev_notify = SIGEV_THREAD;
  218. se.sigev_signo = netlink_socket;
  219. se.sigev_value.sival_ptr = &data;
  220. /* Tell the kernel. */
  221. int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se);
  222. /* If it failed, free the allocated memory. */
  223. if (__builtin_expect (retval != 0, 0))
  224. free (data.attr);
  225. return retval;
  226. }
  227. #else
  228. # include <rt/mq_notify.c>
  229. #endif