1 | /* Copyright (C) 2004-2021 Free Software Foundation, Inc. |
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 |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
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> |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | #include <sysdep.h> |
28 | #include <unistd.h> |
29 | #include <sys/socket.h> |
30 | #include <not-cancel.h> |
31 | #include <pthreadP.h> |
32 | #include <shlib-compat.h> |
33 | |
34 | /* Defined in the kernel headers: */ |
35 | #define NOTIFY_COOKIE_LEN 32 /* Length of the cookie used. */ |
36 | #define NOTIFY_WOKENUP 1 /* Code for notifcation. */ |
37 | #define NOTIFY_REMOVED 2 /* Code for closed message queue |
38 | of de-notifcation. */ |
39 | |
40 | |
41 | /* Data structure for the queued notification requests. */ |
42 | union notify_data |
43 | { |
44 | struct |
45 | { |
46 | void (*fct) (union sigval); /* The function to run. */ |
47 | union sigval param; /* The parameter to pass. */ |
48 | pthread_attr_t *attr; /* Attributes to create the thread with. */ |
49 | /* NB: on 64-bit machines the struct as a size of 24 bytes. Which means |
50 | byte 31 can still be used for returning the status. */ |
51 | }; |
52 | char raw[NOTIFY_COOKIE_LEN]; |
53 | }; |
54 | |
55 | |
56 | /* Keep track of the initialization. */ |
57 | static pthread_once_t once = PTHREAD_ONCE_INIT; |
58 | |
59 | |
60 | /* The netlink socket. */ |
61 | static int netlink_socket = -1; |
62 | |
63 | |
64 | /* Barrier used to make sure data passed to the new thread is not |
65 | resused by the parent. */ |
66 | static pthread_barrier_t notify_barrier; |
67 | |
68 | |
69 | /* Modify the signal mask. We move this into a separate function so |
70 | that the stack space needed for sigset_t is not deducted from what |
71 | the thread can use. */ |
72 | static int |
73 | __attribute__ ((noinline)) |
74 | change_sigmask (int how, sigset_t *oss) |
75 | { |
76 | sigset_t ss; |
77 | sigfillset (&ss); |
78 | return __pthread_sigmask (how, &ss, oss); |
79 | } |
80 | |
81 | |
82 | /* The function used for the notification. */ |
83 | static void * |
84 | notification_function (void *arg) |
85 | { |
86 | /* Copy the function and parameter so that the parent thread can go |
87 | on with its life. */ |
88 | volatile union notify_data *data = (volatile union notify_data *) arg; |
89 | void (*fct) (union sigval) = data->fct; |
90 | union sigval param = data->param; |
91 | |
92 | /* Let the parent go. */ |
93 | (void) __pthread_barrier_wait (¬ify_barrier); |
94 | |
95 | /* Make the thread detached. */ |
96 | __pthread_detach (__pthread_self ()); |
97 | |
98 | /* The parent thread has all signals blocked. This is probably a |
99 | bit surprising for this thread. So we unblock all of them. */ |
100 | (void) change_sigmask (SIG_UNBLOCK, NULL); |
101 | |
102 | /* Now run the user code. */ |
103 | fct (param); |
104 | |
105 | /* And we are done. */ |
106 | return NULL; |
107 | } |
108 | |
109 | |
110 | /* Helper thread. */ |
111 | static void * |
112 | helper_thread (void *arg) |
113 | { |
114 | while (1) |
115 | { |
116 | union notify_data data; |
117 | |
118 | ssize_t n = __recv (netlink_socket, &data, sizeof (data), |
119 | MSG_NOSIGNAL | MSG_WAITALL); |
120 | if (n < NOTIFY_COOKIE_LEN) |
121 | continue; |
122 | |
123 | if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_WOKENUP) |
124 | { |
125 | /* Just create the thread as instructed. There is no way to |
126 | report a problem with creating a thread. */ |
127 | pthread_t th; |
128 | if (__pthread_create (&th, data.attr, notification_function, &data) |
129 | == 0) |
130 | /* Since we passed a pointer to DATA to the new thread we have |
131 | to wait until it is done with it. */ |
132 | (void) __pthread_barrier_wait (¬ify_barrier); |
133 | } |
134 | else if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_REMOVED) |
135 | { |
136 | /* The only state we keep is the copy of the thread attributes. */ |
137 | __pthread_attr_destroy (data.attr); |
138 | free (data.attr); |
139 | } |
140 | } |
141 | return NULL; |
142 | } |
143 | |
144 | |
145 | void |
146 | __mq_notify_fork_subprocess (void) |
147 | { |
148 | once = PTHREAD_ONCE_INIT; |
149 | } |
150 | |
151 | |
152 | static void |
153 | init_mq_netlink (void) |
154 | { |
155 | /* This code might be called a second time after fork(). The file |
156 | descriptor is inherited from the parent. */ |
157 | if (netlink_socket == -1) |
158 | { |
159 | /* Just a normal netlink socket, not bound. */ |
160 | netlink_socket = __socket (AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, 0); |
161 | /* No need to do more if we have no socket. */ |
162 | if (netlink_socket == -1) |
163 | return; |
164 | } |
165 | |
166 | int err = 1; |
167 | |
168 | /* Initialize the barrier. */ |
169 | if (__pthread_barrier_init (¬ify_barrier, NULL, 2) == 0) |
170 | { |
171 | /* Create the helper thread. */ |
172 | pthread_attr_t attr; |
173 | __pthread_attr_init (&attr); |
174 | __pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
175 | /* We do not need much stack space, the bare minimum will be enough. */ |
176 | __pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr)); |
177 | |
178 | /* Temporarily block all signals so that the newly created |
179 | thread inherits the mask. */ |
180 | sigset_t oss; |
181 | int have_no_oss = change_sigmask (SIG_BLOCK, &oss); |
182 | |
183 | pthread_t th; |
184 | err = __pthread_create (&th, &attr, helper_thread, NULL); |
185 | |
186 | /* Reset the signal mask. */ |
187 | if (!have_no_oss) |
188 | __pthread_sigmask (SIG_SETMASK, &oss, NULL); |
189 | |
190 | __pthread_attr_destroy (&attr); |
191 | } |
192 | |
193 | if (err != 0) |
194 | { |
195 | __close_nocancel_nostatus (netlink_socket); |
196 | netlink_socket = -1; |
197 | } |
198 | } |
199 | |
200 | |
201 | /* Register notification upon message arrival to an empty message queue |
202 | MQDES. */ |
203 | int |
204 | __mq_notify (mqd_t mqdes, const struct sigevent *notification) |
205 | { |
206 | /* Make sure the type is correctly defined. */ |
207 | assert (sizeof (union notify_data) == NOTIFY_COOKIE_LEN); |
208 | |
209 | /* Special treatment needed for SIGEV_THREAD. */ |
210 | if (notification == NULL || notification->sigev_notify != SIGEV_THREAD) |
211 | return INLINE_SYSCALL (mq_notify, 2, mqdes, notification); |
212 | |
213 | /* The kernel cannot directly start threads. This will have to be |
214 | done at userlevel. Since we cannot start threads from signal |
215 | handlers we have to create a dedicated thread which waits for |
216 | notifications for arriving messages and creates threads in |
217 | response. */ |
218 | |
219 | /* Initialize only once. */ |
220 | __pthread_once (&once, init_mq_netlink); |
221 | |
222 | /* If we cannot create the netlink socket we cannot provide |
223 | SIGEV_THREAD support. */ |
224 | if (__glibc_unlikely (netlink_socket == -1)) |
225 | { |
226 | __set_errno (ENOSYS); |
227 | return -1; |
228 | } |
229 | |
230 | /* Create the cookie. It will hold almost all the state. */ |
231 | union notify_data data; |
232 | memset (&data, '\0', sizeof (data)); |
233 | data.fct = notification->sigev_notify_function; |
234 | data.param = notification->sigev_value; |
235 | |
236 | if (notification->sigev_notify_attributes != NULL) |
237 | { |
238 | /* The thread attribute has to be allocated separately. */ |
239 | data.attr = (pthread_attr_t *) malloc (sizeof (pthread_attr_t)); |
240 | if (data.attr == NULL) |
241 | return -1; |
242 | |
243 | int ret = __pthread_attr_copy (data.attr, |
244 | notification->sigev_notify_attributes); |
245 | if (ret != 0) |
246 | { |
247 | free (data.attr); |
248 | __set_errno (ret); |
249 | return -1; |
250 | } |
251 | } |
252 | |
253 | /* Construct the new request. */ |
254 | struct sigevent se; |
255 | se.sigev_notify = SIGEV_THREAD; |
256 | se.sigev_signo = netlink_socket; |
257 | se.sigev_value.sival_ptr = &data; |
258 | |
259 | /* Tell the kernel. */ |
260 | int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se); |
261 | |
262 | /* If it failed, free the allocated memory. */ |
263 | if (retval != 0 && data.attr != NULL) |
264 | { |
265 | __pthread_attr_destroy (data.attr); |
266 | free (data.attr); |
267 | } |
268 | |
269 | return retval; |
270 | } |
271 | versioned_symbol (libc, __mq_notify, mq_notify, GLIBC_2_34); |
272 | libc_hidden_ver (__mq_notify, mq_notify) |
273 | #if OTHER_SHLIB_COMPAT (librt, GLIBC_2_3_4, GLIBC_2_34) |
274 | compat_symbol (librt, __mq_notify, mq_notify, GLIBC_2_3_4); |
275 | #endif |
276 | |