2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / mq_notify.c
blob49ddeae052c0be3601f5cd73d4bf6850f7a25c54
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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <mqueue.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sysdep.h>
29 #include <unistd.h>
30 #include <sys/socket.h>
31 #include <not-cancel.h>
32 #include <kernel-features.h>
35 #ifdef __NR_mq_notify
37 /* Defined in the kernel headers: */
38 #define NOTIFY_COOKIE_LEN 32 /* Length of the cookie used. */
39 #define NOTIFY_WOKENUP 1 /* Code for notifcation. */
40 #define NOTIFY_REMOVED 2 /* Code for closed message queue
41 of de-notifcation. */
44 /* Data structure for the queued notification requests. */
45 union notify_data
47 struct
49 void (*fct) (union sigval); /* The function to run. */
50 union sigval param; /* The parameter to pass. */
51 pthread_attr_t *attr; /* Attributes to create the thread with. */
52 /* NB: on 64-bit machines the struct as a size of 24 bytes. Which means
53 byte 31 can still be used for returning the status. */
55 char raw[NOTIFY_COOKIE_LEN];
59 /* Keep track of the initialization. */
60 static pthread_once_t once = PTHREAD_ONCE_INIT;
63 /* The netlink socket. */
64 static int netlink_socket = -1;
67 /* Barrier used to make sure data passed to the new thread is not
68 resused by the parent. */
69 static pthread_barrier_t notify_barrier;
72 /* Modify the signal mask. We move this into a separate function so
73 that the stack space needed for sigset_t is not deducted from what
74 the thread can use. */
75 static int
76 __attribute__ ((noinline))
77 change_sigmask (int how, sigset_t *oss)
79 sigset_t ss;
80 sigfillset (&ss);
81 return pthread_sigmask (how, &ss, oss);
85 /* The function used for the notification. */
86 static void *
87 notification_function (void *arg)
89 /* Copy the function and parameter so that the parent thread can go
90 on with its life. */
91 volatile union notify_data *data = (volatile union notify_data *) arg;
92 void (*fct) (union sigval) = data->fct;
93 union sigval param = data->param;
95 /* Let the parent go. */
96 (void) pthread_barrier_wait (&notify_barrier);
98 /* Make the thread detached. */
99 (void) pthread_detach (pthread_self ());
101 /* The parent thread has all signals blocked. This is probably a
102 bit surprising for this thread. So we unblock all of them. */
103 (void) change_sigmask (SIG_UNBLOCK, NULL);
105 /* Now run the user code. */
106 fct (param);
108 /* And we are done. */
109 return NULL;
113 /* Helper thread. */
114 static void *
115 helper_thread (void *arg)
117 while (1)
119 union notify_data data;
121 ssize_t n = recv (netlink_socket, &data, sizeof (data),
122 MSG_NOSIGNAL | MSG_WAITALL);
123 if (n < NOTIFY_COOKIE_LEN)
124 continue;
126 if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_WOKENUP)
128 /* Just create the thread as instructed. There is no way to
129 report a problem with creating a thread. */
130 pthread_t th;
131 if (__builtin_expect (pthread_create (&th, data.attr,
132 notification_function, &data)
133 == 0, 0))
134 /* Since we passed a pointer to DATA to the new thread we have
135 to wait until it is done with it. */
136 (void) pthread_barrier_wait (&notify_barrier);
138 else if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_REMOVED)
139 /* The only state we keep is the copy of the thread attributes. */
140 free (data.attr);
142 return NULL;
146 static void
147 reset_once (void)
149 once = PTHREAD_ONCE_INIT;
153 static void
154 init_mq_netlink (void)
156 #ifdef SOCK_CLOEXEC
157 # ifndef __ASSUME_SOCK_CLOEXEC
158 static int have_sock_cloexec;
159 # else
160 # define have_sock_cloexec 1
161 # endif
162 #else
163 # define have_sock_cloexec -1
164 # define SOCK_CLOEXEC 0
165 #endif
167 /* This code might be called a second time after fork(). The file
168 descriptor is inherited from the parent. */
169 if (netlink_socket == -1)
171 /* Just a normal netlink socket, not bound. */
172 if (have_sock_cloexec >= 0)
174 netlink_socket = socket (AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, 0);
175 #if defined SOCK_CLOEXEC && !defined __ASSUME_SOCK_CLOEXEC
176 if (have_sock_cloexec == 0)
177 have_sock_cloexec = (netlink_socket != -1 || errno != EINVAL
178 ? 1 : -1);
179 #endif
181 if (have_sock_cloexec < 0)
182 netlink_socket = socket (AF_NETLINK, SOCK_RAW, 0);
183 /* No need to do more if we have no socket. */
184 if (netlink_socket == -1)
185 return;
187 /* Make sure the descriptor is closed on exec. */
188 if (have_sock_cloexec < 0
189 && fcntl (netlink_socket, F_SETFD, FD_CLOEXEC) != 0)
190 goto errout;
193 int err = 1;
195 /* Initialize the barrier. */
196 if (__builtin_expect (pthread_barrier_init (&notify_barrier, NULL, 2) == 0,
199 /* Create the helper thread. */
200 pthread_attr_t attr;
201 (void) pthread_attr_init (&attr);
202 (void) pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
203 /* We do not need much stack space, the bare minimum will be enough. */
204 (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
206 /* Temporarily block all signals so that the newly created
207 thread inherits the mask. */
208 sigset_t oss;
209 int have_no_oss = change_sigmask (SIG_BLOCK, &oss);
211 pthread_t th;
212 err = pthread_create (&th, &attr, helper_thread, NULL);
214 /* Reset the signal mask. */
215 if (!have_no_oss)
216 pthread_sigmask (SIG_SETMASK, &oss, NULL);
218 (void) pthread_attr_destroy (&attr);
220 if (err == 0)
222 static int added_atfork;
224 if (added_atfork == 0
225 && pthread_atfork (NULL, NULL, reset_once) != 0)
227 /* The child thread will call recv() which is a
228 cancellation point. */
229 (void) pthread_cancel (th);
230 err = 1;
232 else
233 added_atfork = 1;
237 if (err != 0)
239 errout:
240 close_not_cancel_no_status (netlink_socket);
241 netlink_socket = -1;
246 /* Register notification upon message arrival to an empty message queue
247 MQDES. */
249 mq_notify (mqd_t mqdes, const struct sigevent *notification)
251 /* Make sure the type is correctly defined. */
252 assert (sizeof (union notify_data) == NOTIFY_COOKIE_LEN);
254 /* Special treatment needed for SIGEV_THREAD. */
255 if (notification == NULL || notification->sigev_notify != SIGEV_THREAD)
256 return INLINE_SYSCALL (mq_notify, 2, mqdes, notification);
258 /* The kernel cannot directly start threads. This will have to be
259 done at userlevel. Since we cannot start threads from signal
260 handlers we have to create a dedicated thread which waits for
261 notifications for arriving messages and creates threads in
262 response. */
264 /* Initialize only once. */
265 pthread_once (&once, init_mq_netlink);
267 /* If we cannot create the netlink socket we cannot provide
268 SIGEV_THREAD support. */
269 if (__builtin_expect (netlink_socket == -1, 0))
271 __set_errno (ENOSYS);
272 return -1;
275 /* Create the cookie. It will hold almost all the state. */
276 union notify_data data;
277 memset (&data, '\0', sizeof (data));
278 data.fct = notification->sigev_notify_function;
279 data.param = notification->sigev_value;
281 if (notification->sigev_notify_attributes != NULL)
283 /* The thread attribute has to be allocated separately. */
284 data.attr = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
285 if (data.attr == NULL)
286 return -1;
288 memcpy (data.attr, notification->sigev_notify_attributes,
289 sizeof (pthread_attr_t));
292 /* Construct the new request. */
293 struct sigevent se;
294 se.sigev_notify = SIGEV_THREAD;
295 se.sigev_signo = netlink_socket;
296 se.sigev_value.sival_ptr = &data;
298 /* Tell the kernel. */
299 int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se);
301 /* If it failed, free the allocated memory. */
302 if (__builtin_expect (retval != 0, 0))
303 free (data.attr);
305 return retval;
308 #else
309 # include <rt/mq_notify.c>
310 #endif