fix signal-based timers with null sigevent argument
[musl] / src / time / timer_create.c
1 #include <time.h>
2 #include "pthread_impl.h"
3
4 struct ksigevent {
5         union sigval sigev_value;
6         int sigev_signo;
7         int sigev_notify;
8         int sigev_tid;
9 };
10
11 struct start_args {
12         pthread_barrier_t b;
13         struct sigevent *sev;
14 };
15
16 void __sigtimer_handler(pthread_t self)
17 {
18         int st;
19         void (*notify)(union sigval) = (void (*)(union sigval))self->start;
20         union sigval val = { .sival_ptr = self->start_arg };
21
22         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
23         notify(val);
24         pthread_setcancelstate(st, 0);
25 }
26
27 static void cleanup(void *p)
28 {
29         pthread_t self = p;
30         __syscall(SYS_timer_delete, self->result);
31 }
32
33 static void *start(void *arg)
34 {
35         pthread_t self = __pthread_self();
36         struct start_args *args = arg;
37
38         /* Reuse no-longer-needed thread structure fields to avoid
39          * needing the timer address in the signal handler. */
40         self->start = (void *(*)(void *))args->sev->sigev_notify_function;
41         self->start_arg = args->sev->sigev_value.sival_ptr;
42         self->result = (void *)-1;
43
44         pthread_cleanup_push(cleanup, self);
45         pthread_barrier_wait(&args->b);
46         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
47         /* Loop on async-signal-safe cancellation point */
48         for (;;) sleep(1000000000);
49         pthread_cleanup_pop(0);
50         return 0;
51 }
52
53 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
54 {
55         pthread_t td;
56         pthread_attr_t attr;
57         int r;
58         struct start_args args;
59         struct ksigevent ksev, *ksevp=0;
60         int timerid;
61
62         switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
63         case SIGEV_NONE:
64         case SIGEV_SIGNAL:
65                 if (evp) {
66                         ksev.sigev_value = evp->sigev_value;
67                         ksev.sigev_signo = evp->sigev_signo;
68                         ksev.sigev_notify = evp->sigev_notify;
69                         ksev.sigev_tid = 0;
70                         ksevp = &ksev;
71                 }
72                 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
73                         return -1;
74                 *res = (void *)timerid;
75                 break;
76         case SIGEV_THREAD:
77                 if (evp->sigev_notify_attributes)
78                         attr = *evp->sigev_notify_attributes;
79                 else
80                         pthread_attr_init(&attr);
81                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
82                 pthread_barrier_init(&args.b, 0, 2);
83                 args.sev = evp;
84                 r = pthread_create(&td, &attr, start, &args);
85                 if (r) {
86                         errno = r;
87                         return -1;
88                 }
89                 ksev.sigev_value.sival_ptr = 0;
90                 ksev.sigev_signo = SIGCANCEL;
91                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
92                 ksev.sigev_tid = td->tid;
93                 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
94                 pthread_barrier_wait(&args.b);
95                 if (r < 0) {
96                         pthread_cancel(td);
97                         return -1;
98                 }
99                 td->result = (void *)timerid;
100                 *res = td;
101                 break;
102         default:
103                 errno = EINVAL;
104                 return -1;
105         }
106
107         return 0;
108 }