select: fix 64-bit timeout truncation on pre-time64 kernels
[musl] / src / mq / mq_notify.c
index a42888d..0e1e6c7 100644 (file)
@@ -10,6 +10,8 @@
 struct args {
        sem_t sem;
        int sock;
+       mqd_t mqd;
+       int err;
        const struct sigevent *sev;
 };
 
@@ -21,8 +23,19 @@ static void *start(void *p)
        int s = args->sock;
        void (*func)(union sigval) = args->sev->sigev_notify_function;
        union sigval val = args->sev->sigev_value;
+       struct sigevent sev2;
+       static const char zeros[32];
+       int err;
+
+       sev2.sigev_notify = SIGEV_THREAD;
+       sev2.sigev_signo = s;
+       sev2.sigev_value.sival_ptr = (void *)&zeros;
 
+       args->err = err = -__syscall(SYS_mq_notify, args->mqd, &sev2);
        sem_post(&args->sem);
+       if (err) return 0;
+
+       pthread_detach(pthread_self());
        n = recv(s, buf, sizeof(buf), MSG_NOSIGNAL|MSG_WAITALL);
        close(s);
        if (n==sizeof buf && buf[sizeof buf - 1] == 1)
@@ -36,9 +49,8 @@ int mq_notify(mqd_t mqd, const struct sigevent *sev)
        pthread_attr_t attr;
        pthread_t td;
        int s;
-       struct sigevent sev2;
-       static const char zeros[32];
        int cs;
+       sigset_t allmask, origmask;
 
        if (!sev || sev->sigev_notify != SIGEV_THREAD)
                return syscall(SYS_mq_notify, mqd, sev);
@@ -46,32 +58,35 @@ int mq_notify(mqd_t mqd, const struct sigevent *sev)
        s = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, 0);
        if (s < 0) return -1;
        args.sock = s;
+       args.mqd = mqd;
 
        if (sev->sigev_notify_attributes) attr = *sev->sigev_notify_attributes;
        else pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        sem_init(&args.sem, 0, 0);
 
+       sigfillset(&allmask);
+       pthread_sigmask(SIG_BLOCK, &allmask, &origmask);
        if (pthread_create(&td, &attr, start, &args)) {
                __syscall(SYS_close, s);
+               pthread_sigmask(SIG_SETMASK, &origmask, 0);
                errno = EAGAIN;
                return -1;
        }
+       pthread_sigmask(SIG_SETMASK, &origmask, 0);
 
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
        sem_wait(&args.sem);
-       pthread_setcancelstate(cs, 0);
        sem_destroy(&args.sem);
 
-       sev2.sigev_notify = SIGEV_THREAD;
-       sev2.sigev_signo = s;
-       sev2.sigev_value.sival_ptr = (void *)&zeros;
-
-       if (syscall(SYS_mq_notify, mqd, &sev2) < 0) {
-               pthread_cancel(td);
+       if (args.err) {
                __syscall(SYS_close, s);
+               pthread_join(td, 0);
+               pthread_setcancelstate(cs, 0);
+               errno = args.err;
                return -1;
        }
 
+       pthread_setcancelstate(cs, 0);
        return 0;
 }