mq_notify.c 7.8 KB

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