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.
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
30 #include <sys/socket.h>
31 #include <not-cancel.h>
36 /* Defined in the kernel headers: */
37 #define NOTIFY_COOKIE_LEN 32 /* Length of the cookie used. */
38 #define NOTIFY_WOKENUP 1 /* Code for notifcation. */
39 #define NOTIFY_REMOVED 2 /* Code for closed message queue
43 /* Data structure for the queued notification requests. */
48 void (*fct
) (union sigval
); /* The function to run. */
49 union sigval param
; /* The parameter to pass. */
50 pthread_attr_t
*attr
; /* Attributes to create the thread with. */
51 /* NB: on 64-bit machines the struct as a size of 24 bytes. Which means
52 byte 31 can still be used for returning the status. */
54 char raw
[NOTIFY_COOKIE_LEN
];
58 /* Keep track of the initialization. */
59 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
62 /* The netlink socket. */
63 static int netlink_socket
= -1;
66 /* Barrier used to make sure data passed to the new thread is not
67 resused by the parent. */
68 static pthread_barrier_t notify_barrier
;
71 /* Modify the signal mask. We move this into a separate function so
72 that the stack space needed for sigset_t is not deducted from what
73 the thread can use. */
75 __attribute__ ((noinline
))
76 change_sigmask (int how
, sigset_t
*oss
)
80 return pthread_sigmask (how
, &ss
, oss
);
84 /* The function used for the notification. */
86 notification_function (void *arg
)
88 /* Copy the function and parameter so that the parent thread can go
90 volatile union notify_data
*data
= (volatile union notify_data
*) arg
;
91 void (*fct
) (union sigval
) = data
->fct
;
92 union sigval param
= data
->param
;
94 /* Let the parent go. */
95 (void) pthread_barrier_wait (¬ify_barrier
);
97 /* Make the thread detached. */
98 (void) pthread_detach (pthread_self ());
100 /* The parent thread has all signals blocked. This is probably a
101 bit surprising for this thread. So we unblock all of them. */
102 (void) change_sigmask (SIG_UNBLOCK
, NULL
);
104 /* Now run the user code. */
107 /* And we are done. */
114 helper_thread (void *arg
)
118 union notify_data data
;
120 ssize_t n
= recv (netlink_socket
, &data
, sizeof (data
),
121 MSG_NOSIGNAL
| MSG_WAITALL
);
122 if (n
< NOTIFY_COOKIE_LEN
)
125 if (data
.raw
[NOTIFY_COOKIE_LEN
- 1] == NOTIFY_WOKENUP
)
127 /* Just create the thread as instructed. There is no way to
128 report a problem with creating a thread. */
130 if (__builtin_expect (pthread_create (&th
, data
.attr
,
131 notification_function
, &data
)
133 /* Since we passed a pointer to DATA to the new thread we have
134 to wait until it is done with it. */
135 (void) pthread_barrier_wait (¬ify_barrier
);
137 else if (data
.raw
[NOTIFY_COOKIE_LEN
- 1] == NOTIFY_REMOVED
)
138 /* The only state we keep is the copy of the thread attributes. */
148 once
= PTHREAD_ONCE_INIT
;
153 init_mq_netlink (void)
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)
159 /* Just a normal netlink socket, not bound. */
160 netlink_socket
= socket (AF_NETLINK
, SOCK_RAW
, 0);
161 /* No need to do more if we have no socket. */
162 if (netlink_socket
== -1)
165 /* Make sure the descriptor is closed on exec. */
166 if (fcntl (netlink_socket
, F_SETFD
, FD_CLOEXEC
) != 0)
172 /* Initialize the barrier. */
173 if (__builtin_expect (pthread_barrier_init (¬ify_barrier
, NULL
, 2) == 0,
176 /* Create the helper thread. */
178 (void) pthread_attr_init (&attr
);
179 (void) pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
180 /* We do not need much stack space, the bare minimum will be enough. */
181 (void) pthread_attr_setstacksize (&attr
, PTHREAD_STACK_MIN
);
183 /* Temporarily block all signals so that the newly created
184 thread inherits the mask. */
186 int have_no_oss
= change_sigmask (SIG_BLOCK
, &oss
);
189 err
= pthread_create (&th
, &attr
, helper_thread
, NULL
);
191 /* Reset the signal mask. */
193 pthread_sigmask (SIG_SETMASK
, &oss
, NULL
);
195 (void) pthread_attr_destroy (&attr
);
199 static int added_atfork
;
201 if (added_atfork
== 0
202 && pthread_atfork (NULL
, NULL
, reset_once
) != 0)
204 /* The child thread will call recv() which is a
205 cancellation point. */
206 (void) pthread_cancel (th
);
217 close_not_cancel_no_status (netlink_socket
);
223 /* Register notification upon message arrival to an empty message queue
226 mq_notify (mqd_t mqdes
, const struct sigevent
*notification
)
228 /* Make sure the type is correctly defined. */
229 assert (sizeof (union notify_data
) == NOTIFY_COOKIE_LEN
);
231 /* Special treatment needed for SIGEV_THREAD. */
232 if (notification
== NULL
|| notification
->sigev_notify
!= SIGEV_THREAD
)
233 return INLINE_SYSCALL (mq_notify
, 2, mqdes
, notification
);
235 /* The kernel cannot directly start threads. This will have to be
236 done at userlevel. Since we cannot start threads from signal
237 handlers we have to create a dedicated thread which waits for
238 notifications for arriving messages and creates threads in
241 /* Initialize only once. */
242 pthread_once (&once
, init_mq_netlink
);
244 /* If we cannot create the netlink socket we cannot provide
245 SIGEV_THREAD support. */
246 if (__builtin_expect (netlink_socket
== -1, 0))
248 __set_errno (ENOSYS
);
252 /* Create the cookie. It will hold almost all the state. */
253 union notify_data data
;
254 memset (&data
, '\0', sizeof (data
));
255 data
.fct
= notification
->sigev_notify_function
;
256 data
.param
= notification
->sigev_value
;
258 if (notification
->sigev_notify_attributes
!= NULL
)
260 /* The thread attribute has to be allocated separately. */
261 data
.attr
= (pthread_attr_t
*) malloc (sizeof (pthread_attr_t
));
262 if (data
.attr
== NULL
)
265 memcpy (data
.attr
, notification
->sigev_notify_attributes
,
266 sizeof (pthread_attr_t
));
269 /* Construct the new request. */
271 se
.sigev_notify
= SIGEV_THREAD
;
272 se
.sigev_signo
= netlink_socket
;
273 se
.sigev_value
.sival_ptr
= &data
;
275 /* Tell the kernel. */
276 int retval
= INLINE_SYSCALL (mq_notify
, 2, mqdes
, &se
);
278 /* If it failed, free the allocated memory. */
279 if (__builtin_expect (retval
!= 0, 0))
286 # include <rt/mq_notify.c>