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