blob: 9298a37c7958627118898915359b5a6bc101563d [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * timer_create.c - create a per-process timer.
3 */
4
5#include <errno.h>
6#include <signal.h>
7#include <stdlib.h>
8#include <string.h>
9#include <time.h>
10#include <sys/syscall.h>
11
12#include "kernel-posix-timers.h"
13
14#ifdef __NR_timer_create
15
16#ifndef offsetof
17# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
18#endif
19
20#define __NR___syscall_timer_create __NR_timer_create
21static __inline__ _syscall3(int, __syscall_timer_create, clockid_t, clock_id,
22 struct sigevent *, evp, kernel_timer_t *, ktimerid);
23
24/* Create a per-process timer */
25int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t * timerid)
26{
27 int retval;
28 kernel_timer_t ktimerid;
29 struct sigevent default_evp;
30 struct timer *newp;
31
32 if (evp == NULL) {
33 /*
34 * The kernel has to pass up the timer ID which is a userlevel object.
35 * Therefore we cannot leave it up to the kernel to determine it.
36 */
37 default_evp.sigev_notify = SIGEV_SIGNAL;
38 default_evp.sigev_signo = SIGALRM;
39 evp = &default_evp;
40 }
41
42 /* Notification via a thread is not supported yet */
43 if (__builtin_expect(evp->sigev_notify == SIGEV_THREAD, 1))
44 return -1;
45
46 /*
47 * We avoid allocating too much memory by basically using
48 * struct timer as a derived class with the first two elements
49 * being in the superclass. We only need these two elements here.
50 */
51 newp = malloc(offsetof(struct timer, thrfunc));
52 if (newp == NULL)
53 return -1; /* No memory */
54 default_evp.sigev_value.sival_ptr = newp;
55
56 retval = __syscall_timer_create(clock_id, evp, &ktimerid);
57 if (retval != -1) {
58 newp->sigev_notify = evp->sigev_notify;
59 newp->ktimerid = ktimerid;
60
61 *timerid = (timer_t) newp;
62 } else {
63 /* Cannot allocate the timer, fail */
64 free(newp);
65 retval = -1;
66 }
67
68 return retval;
69}
70
71#endif