123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include <assert.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <mqueue.h>
- #include <pthread.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sysdep.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <not-cancel.h>
- #ifdef __NR_mq_notify
- #define NOTIFY_COOKIE_LEN 32
- #define NOTIFY_WOKENUP 1
- #define NOTIFY_REMOVED 2
- union notify_data
- {
- struct
- {
- void (*fct) (union sigval);
- union sigval param;
- pthread_attr_t *attr;
-
- };
- char raw[NOTIFY_COOKIE_LEN];
- };
- static pthread_once_t once = PTHREAD_ONCE_INIT;
- static int netlink_socket = -1;
- static pthread_barrier_t notify_barrier;
- static int
- __attribute__ ((noinline))
- change_sigmask (int how, sigset_t *oss)
- {
- sigset_t ss;
- __sigfillset (&ss);
- return pthread_sigmask (how, &ss, oss);
- }
- static void *
- notification_function (void *arg)
- {
-
- __volatile__ union notify_data *data = (__volatile__ union notify_data *) arg;
- void (*fct) (union sigval) = data->fct;
- union sigval param = data->param;
-
- (void) pthread_barrier_wait (¬ify_barrier);
-
- (void) pthread_detach (pthread_self ());
-
- (void) change_sigmask (SIG_UNBLOCK, NULL);
-
- fct (param);
-
- return NULL;
- }
- static void *
- helper_thread (void *arg)
- {
- while (1)
- {
- union notify_data data;
- ssize_t n = recv (netlink_socket, &data, sizeof (data),
- MSG_NOSIGNAL | MSG_WAITALL);
- if (n < NOTIFY_COOKIE_LEN)
- continue;
- if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_WOKENUP)
- {
-
- pthread_t th;
- if (__builtin_expect (pthread_create (&th, data.attr,
- notification_function, &data)
- == 0, 0))
-
- (void) pthread_barrier_wait (¬ify_barrier);
- }
- else if (data.raw[NOTIFY_COOKIE_LEN - 1] == NOTIFY_REMOVED)
-
- free (data.attr);
- }
- return NULL;
- }
- static void
- reset_once (void)
- {
- once = PTHREAD_ONCE_INIT;
- }
- static void
- init_mq_netlink (void)
- {
-
- if (netlink_socket == -1)
- {
-
- netlink_socket = socket (AF_NETLINK, SOCK_RAW, 0);
-
- if (netlink_socket == -1)
- return;
-
- if (fcntl (netlink_socket, F_SETFD, FD_CLOEXEC) != 0)
- goto errout;
- }
- int err = 1;
-
- if (__builtin_expect (pthread_barrier_init (¬ify_barrier, NULL, 2) == 0,
- 0))
- {
-
- pthread_attr_t attr;
- (void) pthread_attr_init (&attr);
- (void) pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
-
- (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
-
- sigset_t oss;
- int have_no_oss = change_sigmask (SIG_BLOCK, &oss);
- pthread_t th;
- err = pthread_create (&th, &attr, helper_thread, NULL);
-
- if (!have_no_oss)
- pthread_sigmask (SIG_SETMASK, &oss, NULL);
- (void) pthread_attr_destroy (&attr);
- if (err == 0)
- {
- static smallint added_atfork;
- if (added_atfork == 0
- && pthread_atfork (NULL, NULL, reset_once) != 0)
- {
-
- (void) pthread_cancel (th);
- err = 1;
- }
- else
- added_atfork = 1;
- }
- }
- if (err != 0)
- {
- errout:
- close_not_cancel_no_status (netlink_socket);
- netlink_socket = -1;
- }
- }
- int
- mq_notify (mqd_t mqdes, const struct sigevent *notification)
- {
-
- assert (sizeof (union notify_data) == NOTIFY_COOKIE_LEN);
-
- if (notification == NULL || notification->sigev_notify != SIGEV_THREAD)
- return INLINE_SYSCALL (mq_notify, 2, mqdes, notification);
-
-
- pthread_once (&once, init_mq_netlink);
-
- if (__builtin_expect (netlink_socket == -1, 0))
- {
- __set_errno (ENOSYS);
- return -1;
- }
-
- union notify_data data;
- memset (&data, '\0', sizeof (data));
- data.fct = notification->sigev_notify_function;
- data.param = notification->sigev_value;
- if (notification->sigev_notify_attributes != NULL)
- {
-
- data.attr = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
- if (data.attr == NULL)
- return -1;
- memcpy (data.attr, notification->sigev_notify_attributes,
- sizeof (pthread_attr_t));
- }
-
- struct sigevent se;
- se.sigev_notify = SIGEV_THREAD;
- se.sigev_signo = netlink_socket;
- se.sigev_value.sival_ptr = &data;
-
- int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se);
-
- if (__builtin_expect (retval != 0, 0))
- free (data.attr);
- return retval;
- }
- #else
- # include <rt/mq_notify.c>
- #endif
|