sys_timerfd.c revision 1.1.2.1 1 1.1.2.1 thorpej /* $NetBSD: sys_timerfd.c,v 1.1.2.1 2020/12/14 16:53:36 thorpej Exp $ */
2 1.1.2.1 thorpej
3 1.1.2.1 thorpej /*-
4 1.1.2.1 thorpej * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1.2.1 thorpej * All rights reserved.
6 1.1.2.1 thorpej *
7 1.1.2.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 thorpej * by Jason R. Thorpe.
9 1.1.2.1 thorpej *
10 1.1.2.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 thorpej * modification, are permitted provided that the following conditions
12 1.1.2.1 thorpej * are met:
13 1.1.2.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.2.1 thorpej *
19 1.1.2.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 thorpej */
31 1.1.2.1 thorpej
32 1.1.2.1 thorpej #include <sys/cdefs.h>
33 1.1.2.1 thorpej __KERNEL_RCSID(0, "$NetBSD: sys_timerfd.c,v 1.1.2.1 2020/12/14 16:53:36 thorpej Exp $");
34 1.1.2.1 thorpej
35 1.1.2.1 thorpej /*
36 1.1.2.1 thorpej * timerfd
37 1.1.2.1 thorpej *
38 1.1.2.1 thorpej * Timerfd objects are similar to POSIX timers, except they are associated
39 1.1.2.1 thorpej * with a file descriptor rather than a process. Timerfd objects are
40 1.1.2.1 thorpej * created with the timerfd_create(2) system call, similar to timer_create(2).
41 1.1.2.1 thorpej * The timerfd analogues for timer_gettime(2) and timer_settime(2) are
42 1.1.2.1 thorpej * timerfd_gettime(2) and timerfd_settime(2), respectively.
43 1.1.2.1 thorpej *
44 1.1.2.1 thorpej * When a timerfd object's timer fires, an internal counter is incremented.
45 1.1.2.1 thorpej * When this timer is non-zero, the descriptor associated with the timerfd
46 1.1.2.1 thorpej * object is "readable". Note that this is slightly different than the
47 1.1.2.1 thorpej * POSIX timer "overrun" counter, which only increments if the timer fires
48 1.1.2.1 thorpej * again while the notification signal is already pending. Thus, we are
49 1.1.2.1 thorpej * responsible for incrementing the "overrun" counter each time the timerfd
50 1.1.2.1 thorpej * timer fires.
51 1.1.2.1 thorpej *
52 1.1.2.1 thorpej * This implementation is API compatible with the Linux timerfd interface.
53 1.1.2.1 thorpej */
54 1.1.2.1 thorpej
55 1.1.2.1 thorpej #include <sys/types.h>
56 1.1.2.1 thorpej #include <sys/condvar.h>
57 1.1.2.1 thorpej #include <sys/file.h>
58 1.1.2.1 thorpej #include <sys/filedesc.h>
59 1.1.2.1 thorpej #include <sys/kauth.h>
60 1.1.2.1 thorpej #include <sys/mutex.h>
61 1.1.2.1 thorpej #include <sys/poll.h>
62 1.1.2.1 thorpej #include <sys/proc.h>
63 1.1.2.1 thorpej #include <sys/select.h>
64 1.1.2.1 thorpej #include <sys/stat.h>
65 1.1.2.1 thorpej #include <sys/syscallargs.h>
66 1.1.2.1 thorpej #include <sys/timerfd.h>
67 1.1.2.1 thorpej #include <sys/uio.h>
68 1.1.2.1 thorpej
69 1.1.2.1 thorpej /* N.B. all timerfd state is protected by itimer_lock() */
70 1.1.2.1 thorpej struct timerfd {
71 1.1.2.1 thorpej struct itimer tfd_itimer;
72 1.1.2.1 thorpej kcondvar_t tfd_read_wait;
73 1.1.2.1 thorpej kcondvar_t tfd_restart_wait;
74 1.1.2.1 thorpej struct selinfo tfd_read_sel;
75 1.1.2.1 thorpej int64_t tfd_nwaiters;
76 1.1.2.1 thorpej bool tfd_cancel_on_set;
77 1.1.2.1 thorpej bool tfd_cancelled;
78 1.1.2.1 thorpej bool tfd_restarting;
79 1.1.2.1 thorpej
80 1.1.2.1 thorpej /*
81 1.1.2.1 thorpej * Information kept for stat(2).
82 1.1.2.1 thorpej */
83 1.1.2.1 thorpej struct timespec tfd_btime; /* time created */
84 1.1.2.1 thorpej struct timespec tfd_mtime; /* last timerfd_settime() */
85 1.1.2.1 thorpej struct timespec tfd_atime; /* last read */
86 1.1.2.1 thorpej };
87 1.1.2.1 thorpej
88 1.1.2.1 thorpej static void timerfd_wake(struct timerfd *);
89 1.1.2.1 thorpej
90 1.1.2.1 thorpej static inline uint64_t
91 1.1.2.1 thorpej timerfd_fire_count(const struct timerfd * const tfd)
92 1.1.2.1 thorpej {
93 1.1.2.1 thorpej return (unsigned int)tfd->tfd_itimer.it_overruns;
94 1.1.2.1 thorpej }
95 1.1.2.1 thorpej
96 1.1.2.1 thorpej static inline bool
97 1.1.2.1 thorpej timerfd_is_readable(const struct timerfd * const tfd)
98 1.1.2.1 thorpej {
99 1.1.2.1 thorpej return tfd->tfd_itimer.it_overruns != 0 || tfd->tfd_cancelled;
100 1.1.2.1 thorpej }
101 1.1.2.1 thorpej
102 1.1.2.1 thorpej /*
103 1.1.2.1 thorpej * timerfd_fire:
104 1.1.2.1 thorpej *
105 1.1.2.1 thorpej * Called when the timerfd's timer fires.
106 1.1.2.1 thorpej *
107 1.1.2.1 thorpej * Called from a callout with itimer lock held.
108 1.1.2.1 thorpej */
109 1.1.2.1 thorpej static void
110 1.1.2.1 thorpej timerfd_fire(struct itimer * const it)
111 1.1.2.1 thorpej {
112 1.1.2.1 thorpej struct timerfd * const tfd =
113 1.1.2.1 thorpej container_of(it, struct timerfd, tfd_itimer);
114 1.1.2.1 thorpej
115 1.1.2.1 thorpej it->it_overruns++;
116 1.1.2.1 thorpej timerfd_wake(tfd);
117 1.1.2.1 thorpej }
118 1.1.2.1 thorpej
119 1.1.2.1 thorpej /*
120 1.1.2.1 thorpej * timerfd_realtime_changed:
121 1.1.2.1 thorpej *
122 1.1.2.1 thorpej * Called when CLOCK_REALTIME is changed with clock_settime()
123 1.1.2.1 thorpej * or settimeofday().
124 1.1.2.1 thorpej *
125 1.1.2.1 thorpej * Called with itimer lock held.
126 1.1.2.1 thorpej */
127 1.1.2.1 thorpej static void
128 1.1.2.1 thorpej timerfd_realtime_changed(struct itimer * const it)
129 1.1.2.1 thorpej {
130 1.1.2.1 thorpej struct timerfd * const tfd =
131 1.1.2.1 thorpej container_of(it, struct timerfd, tfd_itimer);
132 1.1.2.1 thorpej
133 1.1.2.1 thorpej /* Should only be called when timer is armed. */
134 1.1.2.1 thorpej KASSERT(timespecisset(&it->it_time.it_value));
135 1.1.2.1 thorpej
136 1.1.2.1 thorpej if (tfd->tfd_cancel_on_set) {
137 1.1.2.1 thorpej tfd->tfd_cancelled = true;
138 1.1.2.1 thorpej timerfd_wake(tfd);
139 1.1.2.1 thorpej }
140 1.1.2.1 thorpej }
141 1.1.2.1 thorpej
142 1.1.2.1 thorpej static const struct itimer_ops timerfd_itimer_monotonic_ops = {
143 1.1.2.1 thorpej .ito_fire = timerfd_fire,
144 1.1.2.1 thorpej };
145 1.1.2.1 thorpej
146 1.1.2.1 thorpej static const struct itimer_ops timerfd_itimer_realtime_ops = {
147 1.1.2.1 thorpej .ito_fire = timerfd_fire,
148 1.1.2.1 thorpej .ito_realtime_changed = timerfd_realtime_changed,
149 1.1.2.1 thorpej };
150 1.1.2.1 thorpej
151 1.1.2.1 thorpej /*
152 1.1.2.1 thorpej * timerfd_create:
153 1.1.2.1 thorpej *
154 1.1.2.1 thorpej * Create a timerfd object.
155 1.1.2.1 thorpej */
156 1.1.2.1 thorpej static struct timerfd *
157 1.1.2.1 thorpej timerfd_create(clockid_t const clock_id, int const flags)
158 1.1.2.1 thorpej {
159 1.1.2.1 thorpej struct timerfd * const tfd = kmem_zalloc(sizeof(*tfd), KM_SLEEP);
160 1.1.2.1 thorpej
161 1.1.2.1 thorpej KASSERT(clock_id == CLOCK_REALTIME || clock_id == CLOCK_MONOTONIC);
162 1.1.2.1 thorpej
163 1.1.2.1 thorpej cv_init(&tfd->tfd_read_wait, "tfdread");
164 1.1.2.1 thorpej cv_init(&tfd->tfd_restart_wait, "tfdrstrt");
165 1.1.2.1 thorpej selinit(&tfd->tfd_read_sel);
166 1.1.2.1 thorpej getnanotime(&tfd->tfd_btime);
167 1.1.2.1 thorpej
168 1.1.2.1 thorpej /* Caller deals with TFD_CLOEXEC and TFD_NONBLOCK. */
169 1.1.2.1 thorpej
170 1.1.2.1 thorpej itimer_lock();
171 1.1.2.1 thorpej itimer_init(&tfd->tfd_itimer,
172 1.1.2.1 thorpej clock_id == CLOCK_REALTIME ? &timerfd_itimer_realtime_ops
173 1.1.2.1 thorpej : &timerfd_itimer_monotonic_ops,
174 1.1.2.1 thorpej clock_id, NULL);
175 1.1.2.1 thorpej itimer_unlock();
176 1.1.2.1 thorpej
177 1.1.2.1 thorpej return tfd;
178 1.1.2.1 thorpej }
179 1.1.2.1 thorpej
180 1.1.2.1 thorpej /*
181 1.1.2.1 thorpej * timerfd_destroy:
182 1.1.2.1 thorpej *
183 1.1.2.1 thorpej * Destroy a timerfd object.
184 1.1.2.1 thorpej */
185 1.1.2.1 thorpej static void
186 1.1.2.1 thorpej timerfd_destroy(struct timerfd * const tfd)
187 1.1.2.1 thorpej {
188 1.1.2.1 thorpej
189 1.1.2.1 thorpej KASSERT(tfd->tfd_nwaiters == 0);
190 1.1.2.1 thorpej KASSERT(tfd->tfd_restarting == false);
191 1.1.2.1 thorpej
192 1.1.2.1 thorpej itimer_lock();
193 1.1.2.1 thorpej itimer_poison(&tfd->tfd_itimer);
194 1.1.2.1 thorpej itimer_fini(&tfd->tfd_itimer); /* drops itimer lock */
195 1.1.2.1 thorpej
196 1.1.2.1 thorpej cv_destroy(&tfd->tfd_read_wait);
197 1.1.2.1 thorpej cv_destroy(&tfd->tfd_restart_wait);
198 1.1.2.1 thorpej
199 1.1.2.1 thorpej seldestroy(&tfd->tfd_read_sel);
200 1.1.2.1 thorpej
201 1.1.2.1 thorpej kmem_free(tfd, sizeof(*tfd));
202 1.1.2.1 thorpej }
203 1.1.2.1 thorpej
204 1.1.2.1 thorpej /*
205 1.1.2.1 thorpej * timerfd_wait:
206 1.1.2.1 thorpej *
207 1.1.2.1 thorpej * Block on a timerfd. Handles non-blocking, as well as
208 1.1.2.1 thorpej * the restart cases.
209 1.1.2.1 thorpej */
210 1.1.2.1 thorpej static int
211 1.1.2.1 thorpej timerfd_wait(struct timerfd * const tfd, int const fflag)
212 1.1.2.1 thorpej {
213 1.1.2.1 thorpej int error;
214 1.1.2.1 thorpej
215 1.1.2.1 thorpej if (fflag & FNONBLOCK) {
216 1.1.2.1 thorpej return EAGAIN;
217 1.1.2.1 thorpej }
218 1.1.2.1 thorpej
219 1.1.2.1 thorpej /*
220 1.1.2.1 thorpej * We're going to block. If there is a restart in-progress,
221 1.1.2.1 thorpej * wait for that to complete first.
222 1.1.2.1 thorpej */
223 1.1.2.1 thorpej while (tfd->tfd_restarting) {
224 1.1.2.1 thorpej cv_wait(&tfd->tfd_restart_wait, &itimer_mutex);
225 1.1.2.1 thorpej }
226 1.1.2.1 thorpej
227 1.1.2.1 thorpej tfd->tfd_nwaiters++;
228 1.1.2.1 thorpej KASSERT(tfd->tfd_nwaiters > 0);
229 1.1.2.1 thorpej error = cv_wait_sig(&tfd->tfd_read_wait, &itimer_mutex);
230 1.1.2.1 thorpej tfd->tfd_nwaiters--;
231 1.1.2.1 thorpej KASSERT(tfd->tfd_nwaiters >= 0);
232 1.1.2.1 thorpej
233 1.1.2.1 thorpej /*
234 1.1.2.1 thorpej * If a restart was triggered while we were asleep, we need
235 1.1.2.1 thorpej * to return ERESTART if no other error was returned. If we
236 1.1.2.1 thorpej * are the last waiter coming out of the restart drain, clear
237 1.1.2.1 thorpej * the condition.
238 1.1.2.1 thorpej */
239 1.1.2.1 thorpej if (tfd->tfd_restarting) {
240 1.1.2.1 thorpej if (error == 0) {
241 1.1.2.1 thorpej error = ERESTART;
242 1.1.2.1 thorpej }
243 1.1.2.1 thorpej if (tfd->tfd_nwaiters == 0) {
244 1.1.2.1 thorpej tfd->tfd_restarting = false;
245 1.1.2.1 thorpej cv_broadcast(&tfd->tfd_restart_wait);
246 1.1.2.1 thorpej }
247 1.1.2.1 thorpej }
248 1.1.2.1 thorpej
249 1.1.2.1 thorpej return error;
250 1.1.2.1 thorpej }
251 1.1.2.1 thorpej
252 1.1.2.1 thorpej /*
253 1.1.2.1 thorpej * timerfd_wake:
254 1.1.2.1 thorpej *
255 1.1.2.1 thorpej * Wake LWPs blocked on a timerfd.
256 1.1.2.1 thorpej */
257 1.1.2.1 thorpej static void
258 1.1.2.1 thorpej timerfd_wake(struct timerfd * const tfd)
259 1.1.2.1 thorpej {
260 1.1.2.1 thorpej
261 1.1.2.1 thorpej if (tfd->tfd_nwaiters) {
262 1.1.2.1 thorpej cv_broadcast(&tfd->tfd_read_wait);
263 1.1.2.1 thorpej }
264 1.1.2.1 thorpej selnotify(&tfd->tfd_read_sel, POLLIN | POLLRDNORM, NOTE_SUBMIT);
265 1.1.2.1 thorpej }
266 1.1.2.1 thorpej
267 1.1.2.1 thorpej /*
268 1.1.2.1 thorpej * timerfd file operations
269 1.1.2.1 thorpej */
270 1.1.2.1 thorpej
271 1.1.2.1 thorpej static int
272 1.1.2.1 thorpej timerfd_fop_read(file_t * const fp, off_t * const offset,
273 1.1.2.1 thorpej struct uio * const uio, kauth_cred_t const cred, int const flags)
274 1.1.2.1 thorpej {
275 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
276 1.1.2.1 thorpej struct itimer * const it = &tfd->tfd_itimer;
277 1.1.2.1 thorpej int const fflag = fp->f_flag;
278 1.1.2.1 thorpej uint64_t return_value;
279 1.1.2.1 thorpej int error;
280 1.1.2.1 thorpej
281 1.1.2.1 thorpej if (uio->uio_resid < sizeof(uint64_t)) {
282 1.1.2.1 thorpej return EINVAL;
283 1.1.2.1 thorpej }
284 1.1.2.1 thorpej
285 1.1.2.1 thorpej itimer_lock();
286 1.1.2.1 thorpej
287 1.1.2.1 thorpej while (!timerfd_is_readable(tfd)) {
288 1.1.2.1 thorpej if ((error = timerfd_wait(tfd, fflag)) != 0) {
289 1.1.2.1 thorpej itimer_unlock();
290 1.1.2.1 thorpej return error;
291 1.1.2.1 thorpej }
292 1.1.2.1 thorpej }
293 1.1.2.1 thorpej
294 1.1.2.1 thorpej if (tfd->tfd_cancelled) {
295 1.1.2.1 thorpej itimer_unlock();
296 1.1.2.1 thorpej return ECANCELED;
297 1.1.2.1 thorpej }
298 1.1.2.1 thorpej
299 1.1.2.1 thorpej return_value = timerfd_fire_count(tfd);
300 1.1.2.1 thorpej it->it_overruns = 0;
301 1.1.2.1 thorpej
302 1.1.2.1 thorpej getnanotime(&tfd->tfd_atime);
303 1.1.2.1 thorpej
304 1.1.2.1 thorpej itimer_unlock();
305 1.1.2.1 thorpej
306 1.1.2.1 thorpej error = uiomove(&return_value, sizeof(return_value), uio);
307 1.1.2.1 thorpej
308 1.1.2.1 thorpej return error;
309 1.1.2.1 thorpej }
310 1.1.2.1 thorpej
311 1.1.2.1 thorpej static int
312 1.1.2.1 thorpej timerfd_fop_ioctl(file_t * const fp, unsigned long const cmd, void * const data)
313 1.1.2.1 thorpej {
314 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
315 1.1.2.1 thorpej int error = 0;
316 1.1.2.1 thorpej
317 1.1.2.1 thorpej switch (cmd) {
318 1.1.2.1 thorpej case TFD_IOC_SET_TICKS: {
319 1.1.2.1 thorpej const uint64_t * const new_ticksp = data;
320 1.1.2.1 thorpej if (*new_ticksp > INT_MAX) {
321 1.1.2.1 thorpej return EINVAL;
322 1.1.2.1 thorpej }
323 1.1.2.1 thorpej itimer_lock();
324 1.1.2.1 thorpej tfd->tfd_itimer.it_overruns = (int)*new_ticksp;
325 1.1.2.1 thorpej itimer_unlock();
326 1.1.2.1 thorpej break;
327 1.1.2.1 thorpej }
328 1.1.2.1 thorpej
329 1.1.2.1 thorpej default:
330 1.1.2.1 thorpej error = EPASSTHROUGH;
331 1.1.2.1 thorpej }
332 1.1.2.1 thorpej
333 1.1.2.1 thorpej return error;
334 1.1.2.1 thorpej }
335 1.1.2.1 thorpej
336 1.1.2.1 thorpej static int
337 1.1.2.1 thorpej timerfd_fop_poll(file_t * const fp, int const events)
338 1.1.2.1 thorpej {
339 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
340 1.1.2.1 thorpej int revents = events & (POLLOUT | POLLWRNORM);
341 1.1.2.1 thorpej
342 1.1.2.1 thorpej if (events & (POLLIN | POLLRDNORM)) {
343 1.1.2.1 thorpej itimer_lock();
344 1.1.2.1 thorpej if (timerfd_is_readable(tfd)) {
345 1.1.2.1 thorpej revents |= events & (POLLIN | POLLRDNORM);
346 1.1.2.1 thorpej } else {
347 1.1.2.1 thorpej selrecord(curlwp, &tfd->tfd_read_sel);
348 1.1.2.1 thorpej }
349 1.1.2.1 thorpej itimer_unlock();
350 1.1.2.1 thorpej }
351 1.1.2.1 thorpej
352 1.1.2.1 thorpej return revents;
353 1.1.2.1 thorpej }
354 1.1.2.1 thorpej
355 1.1.2.1 thorpej static int
356 1.1.2.1 thorpej timerfd_fop_stat(file_t * const fp, struct stat * const st)
357 1.1.2.1 thorpej {
358 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
359 1.1.2.1 thorpej
360 1.1.2.1 thorpej memset(st, 0, sizeof(*st));
361 1.1.2.1 thorpej
362 1.1.2.1 thorpej itimer_lock();
363 1.1.2.1 thorpej st->st_size = (off_t)timerfd_fire_count(tfd);
364 1.1.2.1 thorpej st->st_atimespec = tfd->tfd_atime;
365 1.1.2.1 thorpej st->st_mtimespec = tfd->tfd_mtime;
366 1.1.2.1 thorpej itimer_unlock();
367 1.1.2.1 thorpej
368 1.1.2.1 thorpej st->st_blksize = sizeof(uint64_t);
369 1.1.2.1 thorpej st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
370 1.1.2.1 thorpej st->st_blocks = 1;
371 1.1.2.1 thorpej st->st_birthtimespec = st->st_ctimespec = tfd->tfd_btime;
372 1.1.2.1 thorpej st->st_uid = kauth_cred_geteuid(fp->f_cred);
373 1.1.2.1 thorpej st->st_gid = kauth_cred_getegid(fp->f_cred);
374 1.1.2.1 thorpej
375 1.1.2.1 thorpej return 0;
376 1.1.2.1 thorpej }
377 1.1.2.1 thorpej
378 1.1.2.1 thorpej static int
379 1.1.2.1 thorpej timerfd_fop_close(file_t * const fp)
380 1.1.2.1 thorpej {
381 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
382 1.1.2.1 thorpej
383 1.1.2.1 thorpej fp->f_timerfd = NULL;
384 1.1.2.1 thorpej timerfd_destroy(tfd);
385 1.1.2.1 thorpej
386 1.1.2.1 thorpej return 0;
387 1.1.2.1 thorpej }
388 1.1.2.1 thorpej
389 1.1.2.1 thorpej static void
390 1.1.2.1 thorpej timerfd_filt_read_detach(struct knote * const kn)
391 1.1.2.1 thorpej {
392 1.1.2.1 thorpej struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
393 1.1.2.1 thorpej
394 1.1.2.1 thorpej itimer_lock();
395 1.1.2.1 thorpej KASSERT(kn->kn_hook == tfd);
396 1.1.2.1 thorpej selremove_knote(&tfd->tfd_read_sel, kn);
397 1.1.2.1 thorpej itimer_unlock();
398 1.1.2.1 thorpej }
399 1.1.2.1 thorpej
400 1.1.2.1 thorpej static int
401 1.1.2.1 thorpej timerfd_filt_read(struct knote * const kn, long const hint)
402 1.1.2.1 thorpej {
403 1.1.2.1 thorpej struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
404 1.1.2.1 thorpej
405 1.1.2.1 thorpej if (hint & NOTE_SUBMIT) {
406 1.1.2.1 thorpej KASSERT(itimer_lock_held());
407 1.1.2.1 thorpej } else {
408 1.1.2.1 thorpej itimer_lock();
409 1.1.2.1 thorpej }
410 1.1.2.1 thorpej
411 1.1.2.1 thorpej kn->kn_data = (int64_t)timerfd_fire_count(tfd);
412 1.1.2.1 thorpej
413 1.1.2.1 thorpej if ((hint & NOTE_SUBMIT) == 0) {
414 1.1.2.1 thorpej itimer_unlock();
415 1.1.2.1 thorpej }
416 1.1.2.1 thorpej
417 1.1.2.1 thorpej return kn->kn_data != 0;
418 1.1.2.1 thorpej }
419 1.1.2.1 thorpej
420 1.1.2.1 thorpej static const struct filterops timerfd_read_filterops = {
421 1.1.2.1 thorpej .f_isfd = 1,
422 1.1.2.1 thorpej .f_detach = timerfd_filt_read_detach,
423 1.1.2.1 thorpej .f_event = timerfd_filt_read,
424 1.1.2.1 thorpej };
425 1.1.2.1 thorpej
426 1.1.2.1 thorpej static int
427 1.1.2.1 thorpej timerfd_fop_kqfilter(file_t * const fp, struct knote * const kn)
428 1.1.2.1 thorpej {
429 1.1.2.1 thorpej struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
430 1.1.2.1 thorpej struct selinfo *sel;
431 1.1.2.1 thorpej
432 1.1.2.1 thorpej switch (kn->kn_filter) {
433 1.1.2.1 thorpej case EVFILT_READ:
434 1.1.2.1 thorpej sel = &tfd->tfd_read_sel;
435 1.1.2.1 thorpej kn->kn_fop = &timerfd_read_filterops;
436 1.1.2.1 thorpej break;
437 1.1.2.1 thorpej
438 1.1.2.1 thorpej default:
439 1.1.2.1 thorpej return EINVAL;
440 1.1.2.1 thorpej }
441 1.1.2.1 thorpej
442 1.1.2.1 thorpej kn->kn_hook = tfd;
443 1.1.2.1 thorpej
444 1.1.2.1 thorpej itimer_lock();
445 1.1.2.1 thorpej selrecord_knote(sel, kn);
446 1.1.2.1 thorpej itimer_unlock();
447 1.1.2.1 thorpej
448 1.1.2.1 thorpej return 0;
449 1.1.2.1 thorpej }
450 1.1.2.1 thorpej
451 1.1.2.1 thorpej static void
452 1.1.2.1 thorpej timerfd_fop_restart(file_t * const fp)
453 1.1.2.1 thorpej {
454 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
455 1.1.2.1 thorpej
456 1.1.2.1 thorpej /*
457 1.1.2.1 thorpej * Unblock blocked reads in order to allow close() to complete.
458 1.1.2.1 thorpej * System calls return ERESTART so that the fd is revalidated.
459 1.1.2.1 thorpej */
460 1.1.2.1 thorpej
461 1.1.2.1 thorpej itimer_lock();
462 1.1.2.1 thorpej
463 1.1.2.1 thorpej if (tfd->tfd_nwaiters != 0) {
464 1.1.2.1 thorpej tfd->tfd_restarting = true;
465 1.1.2.1 thorpej cv_broadcast(&tfd->tfd_read_wait);
466 1.1.2.1 thorpej }
467 1.1.2.1 thorpej
468 1.1.2.1 thorpej itimer_unlock();
469 1.1.2.1 thorpej }
470 1.1.2.1 thorpej
471 1.1.2.1 thorpej static const struct fileops timerfd_fileops = {
472 1.1.2.1 thorpej .fo_name = "timerfd",
473 1.1.2.1 thorpej .fo_read = timerfd_fop_read,
474 1.1.2.1 thorpej .fo_write = fbadop_write,
475 1.1.2.1 thorpej .fo_ioctl = timerfd_fop_ioctl,
476 1.1.2.1 thorpej .fo_fcntl = fnullop_fcntl,
477 1.1.2.1 thorpej .fo_poll = timerfd_fop_poll,
478 1.1.2.1 thorpej .fo_stat = timerfd_fop_stat,
479 1.1.2.1 thorpej .fo_close = timerfd_fop_close,
480 1.1.2.1 thorpej .fo_kqfilter = timerfd_fop_kqfilter,
481 1.1.2.1 thorpej .fo_restart = timerfd_fop_restart,
482 1.1.2.1 thorpej };
483 1.1.2.1 thorpej
484 1.1.2.1 thorpej /*
485 1.1.2.1 thorpej * timerfd_create(2) system call
486 1.1.2.1 thorpej */
487 1.1.2.1 thorpej int
488 1.1.2.1 thorpej do_timerfd_create(struct lwp * const l, clockid_t const clock_id,
489 1.1.2.1 thorpej int const flags, register_t *retval)
490 1.1.2.1 thorpej {
491 1.1.2.1 thorpej file_t *fp;
492 1.1.2.1 thorpej int fd, error;
493 1.1.2.1 thorpej
494 1.1.2.1 thorpej if (flags & ~(TFD_CLOEXEC | TFD_NONBLOCK)) {
495 1.1.2.1 thorpej return EINVAL;
496 1.1.2.1 thorpej }
497 1.1.2.1 thorpej
498 1.1.2.1 thorpej switch (clock_id) {
499 1.1.2.1 thorpej case CLOCK_REALTIME:
500 1.1.2.1 thorpej case CLOCK_MONOTONIC:
501 1.1.2.1 thorpej /* allowed */
502 1.1.2.1 thorpej break;
503 1.1.2.1 thorpej
504 1.1.2.1 thorpej default:
505 1.1.2.1 thorpej return EINVAL;
506 1.1.2.1 thorpej }
507 1.1.2.1 thorpej
508 1.1.2.1 thorpej if ((error = fd_allocfile(&fp, &fd)) != 0) {
509 1.1.2.1 thorpej return error;
510 1.1.2.1 thorpej }
511 1.1.2.1 thorpej
512 1.1.2.1 thorpej fp->f_flag = FREAD;
513 1.1.2.1 thorpej if (flags & TFD_NONBLOCK) {
514 1.1.2.1 thorpej fp->f_flag |= FNONBLOCK;
515 1.1.2.1 thorpej }
516 1.1.2.1 thorpej fp->f_type = DTYPE_TIMERFD;
517 1.1.2.1 thorpej fp->f_ops = &timerfd_fileops;
518 1.1.2.1 thorpej fp->f_timerfd = timerfd_create(clock_id, flags);
519 1.1.2.1 thorpej fd_set_exclose(l, fd, !!(flags & TFD_CLOEXEC));
520 1.1.2.1 thorpej fd_affix(curproc, fp, fd);
521 1.1.2.1 thorpej
522 1.1.2.1 thorpej *retval = fd;
523 1.1.2.1 thorpej return 0;
524 1.1.2.1 thorpej }
525 1.1.2.1 thorpej
526 1.1.2.1 thorpej int
527 1.1.2.1 thorpej sys_timerfd_create(struct lwp *l, const struct sys_timerfd_create_args *uap,
528 1.1.2.1 thorpej register_t *retval)
529 1.1.2.1 thorpej {
530 1.1.2.1 thorpej /* {
531 1.1.2.1 thorpej syscallarg(clockid_t) clock_id;
532 1.1.2.1 thorpej syscallarg(int) flags;
533 1.1.2.1 thorpej } */
534 1.1.2.1 thorpej
535 1.1.2.1 thorpej return do_timerfd_create(l, SCARG(uap, clock_id), SCARG(uap, flags),
536 1.1.2.1 thorpej retval);
537 1.1.2.1 thorpej }
538 1.1.2.1 thorpej
539 1.1.2.1 thorpej /*
540 1.1.2.1 thorpej * timerfd_gettime(2) system call.
541 1.1.2.1 thorpej */
542 1.1.2.1 thorpej int
543 1.1.2.1 thorpej do_timerfd_gettime(struct lwp *l, int fd, struct itimerspec *curr_value,
544 1.1.2.1 thorpej register_t *retval)
545 1.1.2.1 thorpej {
546 1.1.2.1 thorpej file_t *fp;
547 1.1.2.1 thorpej
548 1.1.2.1 thorpej if ((fp = fd_getfile(fd)) == NULL) {
549 1.1.2.1 thorpej return EBADF;
550 1.1.2.1 thorpej }
551 1.1.2.1 thorpej
552 1.1.2.1 thorpej if (fp->f_ops != &timerfd_fileops) {
553 1.1.2.1 thorpej fd_putfile(fd);
554 1.1.2.1 thorpej return EINVAL;
555 1.1.2.1 thorpej }
556 1.1.2.1 thorpej
557 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
558 1.1.2.1 thorpej itimer_lock();
559 1.1.2.1 thorpej itimer_gettime(&tfd->tfd_itimer, curr_value);
560 1.1.2.1 thorpej itimer_unlock();
561 1.1.2.1 thorpej
562 1.1.2.1 thorpej fd_putfile(fd);
563 1.1.2.1 thorpej return 0;
564 1.1.2.1 thorpej }
565 1.1.2.1 thorpej
566 1.1.2.1 thorpej int
567 1.1.2.1 thorpej sys_timerfd_gettime(struct lwp *l, const struct sys_timerfd_gettime_args *uap,
568 1.1.2.1 thorpej register_t *retval)
569 1.1.2.1 thorpej {
570 1.1.2.1 thorpej /* {
571 1.1.2.1 thorpej syscallarg(int) fd;
572 1.1.2.1 thorpej syscallarg(struct itimerspec *) curr_value;
573 1.1.2.1 thorpej } */
574 1.1.2.1 thorpej
575 1.1.2.1 thorpej struct itimerspec oits;
576 1.1.2.1 thorpej int error;
577 1.1.2.1 thorpej
578 1.1.2.1 thorpej error = do_timerfd_gettime(l, SCARG(uap, fd), &oits, retval);
579 1.1.2.1 thorpej if (error == 0) {
580 1.1.2.1 thorpej error = copyout(&oits, SCARG(uap, curr_value), sizeof(oits));
581 1.1.2.1 thorpej }
582 1.1.2.1 thorpej return error;
583 1.1.2.1 thorpej }
584 1.1.2.1 thorpej
585 1.1.2.1 thorpej /*
586 1.1.2.1 thorpej * timerfd_settime(2) system call.
587 1.1.2.1 thorpej */
588 1.1.2.1 thorpej int
589 1.1.2.1 thorpej do_timerfd_settime(struct lwp *l, int fd, int flags,
590 1.1.2.1 thorpej const struct itimerspec *new_value, struct itimerspec *old_value,
591 1.1.2.1 thorpej register_t *retval)
592 1.1.2.1 thorpej {
593 1.1.2.1 thorpej file_t *fp;
594 1.1.2.1 thorpej int error;
595 1.1.2.1 thorpej
596 1.1.2.1 thorpej if (flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) {
597 1.1.2.1 thorpej return EINVAL;
598 1.1.2.1 thorpej }
599 1.1.2.1 thorpej
600 1.1.2.1 thorpej if ((fp = fd_getfile(fd)) == NULL) {
601 1.1.2.1 thorpej return EBADF;
602 1.1.2.1 thorpej }
603 1.1.2.1 thorpej
604 1.1.2.1 thorpej if (fp->f_ops != &timerfd_fileops) {
605 1.1.2.1 thorpej fd_putfile(fd);
606 1.1.2.1 thorpej return EINVAL;
607 1.1.2.1 thorpej }
608 1.1.2.1 thorpej
609 1.1.2.1 thorpej struct timerfd * const tfd = fp->f_timerfd;
610 1.1.2.1 thorpej struct itimer * const it = &tfd->tfd_itimer;
611 1.1.2.1 thorpej
612 1.1.2.1 thorpej itimer_lock();
613 1.1.2.1 thorpej
614 1.1.2.1 thorpej restart:
615 1.1.2.1 thorpej if (old_value != NULL) {
616 1.1.2.1 thorpej *old_value = it->it_time;
617 1.1.2.1 thorpej }
618 1.1.2.1 thorpej it->it_time = *new_value;
619 1.1.2.1 thorpej
620 1.1.2.1 thorpej /*
621 1.1.2.1 thorpej * If we've been passed a relative value, convert it to an
622 1.1.2.1 thorpej * absolute, as that's what the itimer facility expects for
623 1.1.2.1 thorpej * non-virtual timers. Also ensure that this doesn't set it
624 1.1.2.1 thorpej * to zero or lets it go negative.
625 1.1.2.1 thorpej * XXXJRT re-factor.
626 1.1.2.1 thorpej */
627 1.1.2.1 thorpej if (timespecisset(&it->it_time.it_value) &&
628 1.1.2.1 thorpej (flags & TFD_TIMER_ABSTIME) == 0) {
629 1.1.2.1 thorpej struct timespec now;
630 1.1.2.1 thorpej if (it->it_clockid == CLOCK_REALTIME) {
631 1.1.2.1 thorpej getnanotime(&now);
632 1.1.2.1 thorpej } else { /* CLOCK_MONOTONIC */
633 1.1.2.1 thorpej getnanouptime(&now);
634 1.1.2.1 thorpej }
635 1.1.2.1 thorpej timespecadd(&it->it_time.it_value, &now,
636 1.1.2.1 thorpej &it->it_time.it_value);
637 1.1.2.1 thorpej }
638 1.1.2.1 thorpej
639 1.1.2.1 thorpej error = itimer_settime(it);
640 1.1.2.1 thorpej if (error == ERESTART) {
641 1.1.2.1 thorpej goto restart;
642 1.1.2.1 thorpej }
643 1.1.2.1 thorpej KASSERT(error == 0);
644 1.1.2.1 thorpej
645 1.1.2.1 thorpej /* Reset the expirations counter. */
646 1.1.2.1 thorpej it->it_overruns = 0;
647 1.1.2.1 thorpej
648 1.1.2.1 thorpej if (it->it_clockid == CLOCK_REALTIME) {
649 1.1.2.1 thorpej tfd->tfd_cancelled = false;
650 1.1.2.1 thorpej tfd->tfd_cancel_on_set = !!(flags & TFD_TIMER_CANCEL_ON_SET);
651 1.1.2.1 thorpej }
652 1.1.2.1 thorpej
653 1.1.2.1 thorpej getnanotime(&tfd->tfd_mtime);
654 1.1.2.1 thorpej itimer_unlock();
655 1.1.2.1 thorpej
656 1.1.2.1 thorpej fd_putfile(fd);
657 1.1.2.1 thorpej return error;
658 1.1.2.1 thorpej }
659 1.1.2.1 thorpej
660 1.1.2.1 thorpej int
661 1.1.2.1 thorpej sys_timerfd_settime(struct lwp *l, const struct sys_timerfd_settime_args *uap,
662 1.1.2.1 thorpej register_t *retval)
663 1.1.2.1 thorpej {
664 1.1.2.1 thorpej /* {
665 1.1.2.1 thorpej syscallarg(int) fd;
666 1.1.2.1 thorpej syscallarg(int) flags;
667 1.1.2.1 thorpej syscallarg(const struct itimerspec *) new_value;
668 1.1.2.1 thorpej syscallarg(struct itimerspec *) old_value;
669 1.1.2.1 thorpej } */
670 1.1.2.1 thorpej
671 1.1.2.1 thorpej struct itimerspec nits, oits, *oitsp = NULL;
672 1.1.2.1 thorpej int error;
673 1.1.2.1 thorpej
674 1.1.2.1 thorpej error = copyin(SCARG(uap, new_value), &nits, sizeof(nits));
675 1.1.2.1 thorpej if (error) {
676 1.1.2.1 thorpej return error;
677 1.1.2.1 thorpej }
678 1.1.2.1 thorpej
679 1.1.2.1 thorpej if (SCARG(uap, old_value) != NULL) {
680 1.1.2.1 thorpej oitsp = &oits;
681 1.1.2.1 thorpej }
682 1.1.2.1 thorpej
683 1.1.2.1 thorpej error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags),
684 1.1.2.1 thorpej &nits, oitsp, retval);
685 1.1.2.1 thorpej if (error == 0 && oitsp != NULL) {
686 1.1.2.1 thorpej error = copyout(oitsp, SCARG(uap, old_value), sizeof(*oitsp));
687 1.1.2.1 thorpej }
688 1.1.2.1 thorpej return error;
689 1.1.2.1 thorpej }
690