]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2004-2014 Free Software Foundation, Inc. |
1b82c6c7 UD |
2 | This file is part of the GNU C Library. |
3 | Contribute by Ulrich Drepper <drepper@redhat.com>, 2004. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
1b82c6c7 UD |
18 | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <fcntl.h> | |
22 | #include <mqueue.h> | |
23 | #include <pthread.h> | |
24 | #include <signal.h> | |
1683daeb | 25 | #include <stdlib.h> |
1b82c6c7 UD |
26 | #include <string.h> |
27 | #include <sysdep.h> | |
28 | #include <unistd.h> | |
29 | #include <sys/socket.h> | |
30 | #include <not-cancel.h> | |
8ccf22f9 | 31 | #include <kernel-features.h> |
c473bd1c | 32 | #include <nptl/pthreadP.h> |
1b82c6c7 UD |
33 | |
34 | ||
35 | #ifdef __NR_mq_notify | |
36 | ||
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. */ | |
42 | ||
43 | ||
44 | /* Data structure for the queued notification requests. */ | |
45 | union notify_data | |
46 | { | |
47 | struct | |
48 | { | |
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. */ | |
54 | }; | |
55 | char raw[NOTIFY_COOKIE_LEN]; | |
56 | }; | |
57 | ||
58 | ||
59 | /* Keep track of the initialization. */ | |
60 | static pthread_once_t once = PTHREAD_ONCE_INIT; | |
61 | ||
62 | ||
63 | /* The netlink socket. */ | |
64 | static int netlink_socket = -1; | |
65 | ||
66 | ||
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; | |
70 | ||
71 | ||
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) | |
78 | { | |
79 | sigset_t ss; | |
80 | sigfillset (&ss); | |
81 | return pthread_sigmask (how, &ss, oss); | |
82 | } | |
83 | ||
84 | ||
85 | /* The function used for the notification. */ | |
86 | static void * | |
87 | notification_function (void *arg) | |
88 | { | |
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; | |
94 | ||
95 | /* Let the parent go. */ | |
96 | (void) pthread_barrier_wait (¬ify_barrier); | |
97 | ||
98 | /* Make the thread detached. */ | |
99 | (void) pthread_detach (pthread_self ()); | |
100 | ||
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); | |
104 | ||
105 | /* Now run the user code. */ | |
106 | fct (param); | |
107 | ||
108 | /* And we are done. */ | |
109 | return NULL; | |
110 | } | |
111 | ||
112 | ||
113 | /* Helper thread. */ | |
114 | static void * | |
115 | helper_thread (void *arg) | |
116 | { | |
117 | while (1) | |
118 | { | |
119 | union notify_data data; | |
120 | ||
121 | ssize_t n = recv (netlink_socket, &data, sizeof (data), | |
122 | MSG_NOSIGNAL | MSG_WAITALL); | |
123 | if (n < NOTIFY_COOKIE_LEN) | |
124 | continue; | |
125 | ||
126 | if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_WOKENUP) | |
127 | { | |
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 (¬ify_barrier); | |
137 | } | |
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); | |
141 | } | |
f93fa7d4 | 142 | return NULL; |
1b82c6c7 UD |
143 | } |
144 | ||
145 | ||
146 | static void | |
147 | reset_once (void) | |
148 | { | |
149 | once = PTHREAD_ONCE_INIT; | |
150 | } | |
151 | ||
152 | ||
153 | static void | |
154 | init_mq_netlink (void) | |
155 | { | |
8ccf22f9 UD |
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 | |
166 | ||
1b82c6c7 UD |
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) | |
170 | { | |
171 | /* Just a normal netlink socket, not bound. */ | |
7b91359b | 172 | if (have_sock_cloexec >= 0) |
8ccf22f9 UD |
173 | { |
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 | |
180 | } | |
181 | if (have_sock_cloexec < 0) | |
182 | netlink_socket = socket (AF_NETLINK, SOCK_RAW, 0); | |
1b82c6c7 UD |
183 | /* No need to do more if we have no socket. */ |
184 | if (netlink_socket == -1) | |
185 | return; | |
186 | ||
187 | /* Make sure the descriptor is closed on exec. */ | |
8ccf22f9 UD |
188 | if (have_sock_cloexec < 0 |
189 | && fcntl (netlink_socket, F_SETFD, FD_CLOEXEC) != 0) | |
1b82c6c7 UD |
190 | goto errout; |
191 | } | |
192 | ||
193 | int err = 1; | |
194 | ||
195 | /* Initialize the barrier. */ | |
196 | if (__builtin_expect (pthread_barrier_init (¬ify_barrier, NULL, 2) == 0, | |
197 | 0)) | |
198 | { | |
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. */ | |
ee9e0640 | 204 | (void) pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr)); |
1b82c6c7 UD |
205 | |
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); | |
210 | ||
211 | pthread_t th; | |
212 | err = pthread_create (&th, &attr, helper_thread, NULL); | |
213 | ||
214 | /* Reset the signal mask. */ | |
215 | if (!have_no_oss) | |
216 | pthread_sigmask (SIG_SETMASK, &oss, NULL); | |
217 | ||
218 | (void) pthread_attr_destroy (&attr); | |
219 | ||
220 | if (err == 0) | |
221 | { | |
222 | static int added_atfork; | |
223 | ||
224 | if (added_atfork == 0 | |
225 | && pthread_atfork (NULL, NULL, reset_once) != 0) | |
226 | { | |
227 | /* The child thread will call recv() which is a | |
228 | cancellation point. */ | |
229 | (void) pthread_cancel (th); | |
230 | err = 1; | |
231 | } | |
232 | else | |
233 | added_atfork = 1; | |
234 | } | |
235 | } | |
236 | ||
237 | if (err != 0) | |
238 | { | |
239 | errout: | |
240 | close_not_cancel_no_status (netlink_socket); | |
241 | netlink_socket = -1; | |
242 | } | |
243 | } | |
244 | ||
245 | ||
246 | /* Register notification upon message arrival to an empty message queue | |
247 | MQDES. */ | |
248 | int | |
249 | mq_notify (mqd_t mqdes, const struct sigevent *notification) | |
250 | { | |
251 | /* Make sure the type is correctly defined. */ | |
252 | assert (sizeof (union notify_data) == NOTIFY_COOKIE_LEN); | |
253 | ||
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); | |
257 | ||
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. */ | |
263 | ||
264 | /* Initialize only once. */ | |
265 | pthread_once (&once, init_mq_netlink); | |
266 | ||
267 | /* If we cannot create the netlink socket we cannot provide | |
268 | SIGEV_THREAD support. */ | |
a1ffb40e | 269 | if (__glibc_unlikely (netlink_socket == -1)) |
1b82c6c7 UD |
270 | { |
271 | __set_errno (ENOSYS); | |
272 | return -1; | |
273 | } | |
274 | ||
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; | |
280 | ||
281 | if (notification->sigev_notify_attributes != NULL) | |
282 | { | |
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; | |
287 | ||
288 | memcpy (data.attr, notification->sigev_notify_attributes, | |
289 | sizeof (pthread_attr_t)); | |
290 | } | |
291 | ||
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; | |
297 | ||
298 | /* Tell the kernel. */ | |
299 | int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se); | |
300 | ||
301 | /* If it failed, free the allocated memory. */ | |
a1ffb40e | 302 | if (__glibc_unlikely (retval != 0)) |
1b82c6c7 UD |
303 | free (data.attr); |
304 | ||
305 | return retval; | |
306 | } | |
307 | ||
308 | #else | |
2826ac7e | 309 | # include <rt/mq_notify.c> |
1b82c6c7 | 310 | #endif |