sys_timerfd.c revision 1.7 1 1.7 thorpej /* $NetBSD: sys_timerfd.c,v 1.7 2021/11/24 16:35:33 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.7 thorpej __KERNEL_RCSID(0, "$NetBSD: sys_timerfd.c,v 1.7 2021/11/24 16:35:33 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 struct selinfo tfd_read_sel;
75 1.2 thorpej int64_t tfd_nwaiters;
76 1.2 thorpej bool tfd_cancel_on_set;
77 1.2 thorpej bool tfd_cancelled;
78 1.2 thorpej bool tfd_restarting;
79 1.2 thorpej
80 1.2 thorpej /*
81 1.2 thorpej * Information kept for stat(2).
82 1.2 thorpej */
83 1.2 thorpej struct timespec tfd_btime; /* time created */
84 1.2 thorpej struct timespec tfd_mtime; /* last timerfd_settime() */
85 1.2 thorpej struct timespec tfd_atime; /* last read */
86 1.2 thorpej };
87 1.2 thorpej
88 1.2 thorpej static void timerfd_wake(struct timerfd *);
89 1.2 thorpej
90 1.2 thorpej static inline uint64_t
91 1.2 thorpej timerfd_fire_count(const struct timerfd * const tfd)
92 1.2 thorpej {
93 1.2 thorpej return (unsigned int)tfd->tfd_itimer.it_overruns;
94 1.2 thorpej }
95 1.2 thorpej
96 1.2 thorpej static inline bool
97 1.2 thorpej timerfd_is_readable(const struct timerfd * const tfd)
98 1.2 thorpej {
99 1.2 thorpej return tfd->tfd_itimer.it_overruns != 0 || tfd->tfd_cancelled;
100 1.2 thorpej }
101 1.2 thorpej
102 1.2 thorpej /*
103 1.2 thorpej * timerfd_fire:
104 1.2 thorpej *
105 1.2 thorpej * Called when the timerfd's timer fires.
106 1.2 thorpej *
107 1.2 thorpej * Called from a callout with itimer lock held.
108 1.2 thorpej */
109 1.2 thorpej static void
110 1.2 thorpej timerfd_fire(struct itimer * const it)
111 1.2 thorpej {
112 1.2 thorpej struct timerfd * const tfd =
113 1.2 thorpej container_of(it, struct timerfd, tfd_itimer);
114 1.2 thorpej
115 1.2 thorpej it->it_overruns++;
116 1.2 thorpej timerfd_wake(tfd);
117 1.2 thorpej }
118 1.2 thorpej
119 1.2 thorpej /*
120 1.2 thorpej * timerfd_realtime_changed:
121 1.2 thorpej *
122 1.2 thorpej * Called when CLOCK_REALTIME is changed with clock_settime()
123 1.2 thorpej * or settimeofday().
124 1.2 thorpej *
125 1.2 thorpej * Called with itimer lock held.
126 1.2 thorpej */
127 1.2 thorpej static void
128 1.2 thorpej timerfd_realtime_changed(struct itimer * const it)
129 1.2 thorpej {
130 1.2 thorpej struct timerfd * const tfd =
131 1.2 thorpej container_of(it, struct timerfd, tfd_itimer);
132 1.2 thorpej
133 1.2 thorpej /* Should only be called when timer is armed. */
134 1.2 thorpej KASSERT(timespecisset(&it->it_time.it_value));
135 1.2 thorpej
136 1.2 thorpej if (tfd->tfd_cancel_on_set) {
137 1.2 thorpej tfd->tfd_cancelled = true;
138 1.2 thorpej timerfd_wake(tfd);
139 1.2 thorpej }
140 1.2 thorpej }
141 1.2 thorpej
142 1.2 thorpej static const struct itimer_ops timerfd_itimer_monotonic_ops = {
143 1.2 thorpej .ito_fire = timerfd_fire,
144 1.2 thorpej };
145 1.2 thorpej
146 1.2 thorpej static const struct itimer_ops timerfd_itimer_realtime_ops = {
147 1.2 thorpej .ito_fire = timerfd_fire,
148 1.2 thorpej .ito_realtime_changed = timerfd_realtime_changed,
149 1.2 thorpej };
150 1.2 thorpej
151 1.2 thorpej /*
152 1.2 thorpej * timerfd_create:
153 1.2 thorpej *
154 1.2 thorpej * Create a timerfd object.
155 1.2 thorpej */
156 1.2 thorpej static struct timerfd *
157 1.2 thorpej timerfd_create(clockid_t const clock_id, int const flags)
158 1.2 thorpej {
159 1.2 thorpej struct timerfd * const tfd = kmem_zalloc(sizeof(*tfd), KM_SLEEP);
160 1.2 thorpej
161 1.2 thorpej KASSERT(clock_id == CLOCK_REALTIME || clock_id == CLOCK_MONOTONIC);
162 1.2 thorpej
163 1.2 thorpej cv_init(&tfd->tfd_read_wait, "tfdread");
164 1.2 thorpej selinit(&tfd->tfd_read_sel);
165 1.2 thorpej getnanotime(&tfd->tfd_btime);
166 1.2 thorpej
167 1.2 thorpej /* Caller deals with TFD_CLOEXEC and TFD_NONBLOCK. */
168 1.2 thorpej
169 1.2 thorpej itimer_lock();
170 1.2 thorpej itimer_init(&tfd->tfd_itimer,
171 1.2 thorpej clock_id == CLOCK_REALTIME ? &timerfd_itimer_realtime_ops
172 1.2 thorpej : &timerfd_itimer_monotonic_ops,
173 1.2 thorpej clock_id, NULL);
174 1.2 thorpej itimer_unlock();
175 1.2 thorpej
176 1.2 thorpej return tfd;
177 1.2 thorpej }
178 1.2 thorpej
179 1.2 thorpej /*
180 1.2 thorpej * timerfd_destroy:
181 1.2 thorpej *
182 1.2 thorpej * Destroy a timerfd object.
183 1.2 thorpej */
184 1.2 thorpej static void
185 1.2 thorpej timerfd_destroy(struct timerfd * const tfd)
186 1.2 thorpej {
187 1.2 thorpej
188 1.2 thorpej KASSERT(tfd->tfd_nwaiters == 0);
189 1.2 thorpej
190 1.2 thorpej itimer_lock();
191 1.2 thorpej itimer_poison(&tfd->tfd_itimer);
192 1.2 thorpej itimer_fini(&tfd->tfd_itimer); /* drops itimer lock */
193 1.2 thorpej
194 1.2 thorpej cv_destroy(&tfd->tfd_read_wait);
195 1.2 thorpej
196 1.2 thorpej seldestroy(&tfd->tfd_read_sel);
197 1.2 thorpej
198 1.2 thorpej kmem_free(tfd, sizeof(*tfd));
199 1.2 thorpej }
200 1.2 thorpej
201 1.2 thorpej /*
202 1.2 thorpej * timerfd_wait:
203 1.2 thorpej *
204 1.2 thorpej * Block on a timerfd. Handles non-blocking, as well as
205 1.2 thorpej * the restart cases.
206 1.2 thorpej */
207 1.2 thorpej static int
208 1.2 thorpej timerfd_wait(struct timerfd * const tfd, int const fflag)
209 1.2 thorpej {
210 1.2 thorpej extern kmutex_t itimer_mutex; /* XXX */
211 1.2 thorpej int error;
212 1.2 thorpej
213 1.2 thorpej if (fflag & FNONBLOCK) {
214 1.2 thorpej return EAGAIN;
215 1.2 thorpej }
216 1.2 thorpej
217 1.2 thorpej /*
218 1.7 thorpej * We're going to block. Check if we need to return ERESTART.
219 1.2 thorpej */
220 1.7 thorpej if (tfd->tfd_restarting) {
221 1.7 thorpej return ERESTART;
222 1.2 thorpej }
223 1.2 thorpej
224 1.2 thorpej tfd->tfd_nwaiters++;
225 1.2 thorpej KASSERT(tfd->tfd_nwaiters > 0);
226 1.2 thorpej error = cv_wait_sig(&tfd->tfd_read_wait, &itimer_mutex);
227 1.2 thorpej tfd->tfd_nwaiters--;
228 1.2 thorpej KASSERT(tfd->tfd_nwaiters >= 0);
229 1.2 thorpej
230 1.2 thorpej /*
231 1.2 thorpej * If a restart was triggered while we were asleep, we need
232 1.7 thorpej * to return ERESTART if no other error was returned.
233 1.2 thorpej */
234 1.2 thorpej if (tfd->tfd_restarting) {
235 1.2 thorpej if (error == 0) {
236 1.2 thorpej error = ERESTART;
237 1.2 thorpej }
238 1.2 thorpej }
239 1.2 thorpej
240 1.2 thorpej return error;
241 1.2 thorpej }
242 1.2 thorpej
243 1.2 thorpej /*
244 1.2 thorpej * timerfd_wake:
245 1.2 thorpej *
246 1.2 thorpej * Wake LWPs blocked on a timerfd.
247 1.2 thorpej */
248 1.2 thorpej static void
249 1.2 thorpej timerfd_wake(struct timerfd * const tfd)
250 1.2 thorpej {
251 1.2 thorpej
252 1.2 thorpej if (tfd->tfd_nwaiters) {
253 1.2 thorpej cv_broadcast(&tfd->tfd_read_wait);
254 1.2 thorpej }
255 1.2 thorpej selnotify(&tfd->tfd_read_sel, POLLIN | POLLRDNORM, NOTE_SUBMIT);
256 1.2 thorpej }
257 1.2 thorpej
258 1.2 thorpej /*
259 1.2 thorpej * timerfd file operations
260 1.2 thorpej */
261 1.2 thorpej
262 1.2 thorpej static int
263 1.2 thorpej timerfd_fop_read(file_t * const fp, off_t * const offset,
264 1.2 thorpej struct uio * const uio, kauth_cred_t const cred, int const flags)
265 1.2 thorpej {
266 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
267 1.2 thorpej struct itimer * const it = &tfd->tfd_itimer;
268 1.2 thorpej int const fflag = fp->f_flag;
269 1.2 thorpej uint64_t return_value;
270 1.2 thorpej int error;
271 1.2 thorpej
272 1.2 thorpej if (uio->uio_resid < sizeof(uint64_t)) {
273 1.2 thorpej return EINVAL;
274 1.2 thorpej }
275 1.2 thorpej
276 1.2 thorpej itimer_lock();
277 1.2 thorpej
278 1.2 thorpej while (!timerfd_is_readable(tfd)) {
279 1.2 thorpej if ((error = timerfd_wait(tfd, fflag)) != 0) {
280 1.2 thorpej itimer_unlock();
281 1.2 thorpej return error;
282 1.2 thorpej }
283 1.2 thorpej }
284 1.2 thorpej
285 1.2 thorpej if (tfd->tfd_cancelled) {
286 1.2 thorpej itimer_unlock();
287 1.2 thorpej return ECANCELED;
288 1.2 thorpej }
289 1.2 thorpej
290 1.2 thorpej return_value = timerfd_fire_count(tfd);
291 1.2 thorpej it->it_overruns = 0;
292 1.2 thorpej
293 1.2 thorpej getnanotime(&tfd->tfd_atime);
294 1.2 thorpej
295 1.2 thorpej itimer_unlock();
296 1.2 thorpej
297 1.2 thorpej error = uiomove(&return_value, sizeof(return_value), uio);
298 1.2 thorpej
299 1.2 thorpej return error;
300 1.2 thorpej }
301 1.2 thorpej
302 1.2 thorpej static int
303 1.2 thorpej timerfd_fop_ioctl(file_t * const fp, unsigned long const cmd, void * const data)
304 1.2 thorpej {
305 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
306 1.2 thorpej int error = 0;
307 1.2 thorpej
308 1.2 thorpej switch (cmd) {
309 1.2 thorpej case TFD_IOC_SET_TICKS: {
310 1.2 thorpej const uint64_t * const new_ticksp = data;
311 1.2 thorpej if (*new_ticksp > INT_MAX) {
312 1.2 thorpej return EINVAL;
313 1.2 thorpej }
314 1.2 thorpej itimer_lock();
315 1.2 thorpej tfd->tfd_itimer.it_overruns = (int)*new_ticksp;
316 1.2 thorpej itimer_unlock();
317 1.2 thorpej break;
318 1.2 thorpej }
319 1.2 thorpej
320 1.2 thorpej default:
321 1.2 thorpej error = EPASSTHROUGH;
322 1.2 thorpej }
323 1.2 thorpej
324 1.2 thorpej return error;
325 1.2 thorpej }
326 1.2 thorpej
327 1.2 thorpej static int
328 1.2 thorpej timerfd_fop_poll(file_t * const fp, int const events)
329 1.2 thorpej {
330 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
331 1.2 thorpej int revents = events & (POLLOUT | POLLWRNORM);
332 1.2 thorpej
333 1.2 thorpej if (events & (POLLIN | POLLRDNORM)) {
334 1.2 thorpej itimer_lock();
335 1.2 thorpej if (timerfd_is_readable(tfd)) {
336 1.2 thorpej revents |= events & (POLLIN | POLLRDNORM);
337 1.2 thorpej } else {
338 1.2 thorpej selrecord(curlwp, &tfd->tfd_read_sel);
339 1.2 thorpej }
340 1.2 thorpej itimer_unlock();
341 1.2 thorpej }
342 1.2 thorpej
343 1.2 thorpej return revents;
344 1.2 thorpej }
345 1.2 thorpej
346 1.2 thorpej static int
347 1.2 thorpej timerfd_fop_stat(file_t * const fp, struct stat * const st)
348 1.2 thorpej {
349 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
350 1.2 thorpej
351 1.2 thorpej memset(st, 0, sizeof(*st));
352 1.2 thorpej
353 1.2 thorpej itimer_lock();
354 1.2 thorpej st->st_size = (off_t)timerfd_fire_count(tfd);
355 1.2 thorpej st->st_atimespec = tfd->tfd_atime;
356 1.2 thorpej st->st_mtimespec = tfd->tfd_mtime;
357 1.2 thorpej itimer_unlock();
358 1.2 thorpej
359 1.2 thorpej st->st_blksize = sizeof(uint64_t);
360 1.2 thorpej st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
361 1.2 thorpej st->st_blocks = 1;
362 1.2 thorpej st->st_birthtimespec = tfd->tfd_btime;
363 1.2 thorpej st->st_ctimespec = st->st_mtimespec;
364 1.2 thorpej st->st_uid = kauth_cred_geteuid(fp->f_cred);
365 1.2 thorpej st->st_gid = kauth_cred_getegid(fp->f_cred);
366 1.2 thorpej
367 1.2 thorpej return 0;
368 1.2 thorpej }
369 1.2 thorpej
370 1.2 thorpej static int
371 1.2 thorpej timerfd_fop_close(file_t * const fp)
372 1.2 thorpej {
373 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
374 1.2 thorpej
375 1.2 thorpej fp->f_timerfd = NULL;
376 1.2 thorpej timerfd_destroy(tfd);
377 1.2 thorpej
378 1.2 thorpej return 0;
379 1.2 thorpej }
380 1.2 thorpej
381 1.2 thorpej static void
382 1.2 thorpej timerfd_filt_read_detach(struct knote * const kn)
383 1.2 thorpej {
384 1.2 thorpej struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
385 1.2 thorpej
386 1.2 thorpej itimer_lock();
387 1.2 thorpej KASSERT(kn->kn_hook == tfd);
388 1.2 thorpej selremove_knote(&tfd->tfd_read_sel, kn);
389 1.2 thorpej itimer_unlock();
390 1.2 thorpej }
391 1.2 thorpej
392 1.2 thorpej static int
393 1.2 thorpej timerfd_filt_read(struct knote * const kn, long const hint)
394 1.2 thorpej {
395 1.2 thorpej struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
396 1.6 thorpej int rv;
397 1.2 thorpej
398 1.2 thorpej if (hint & NOTE_SUBMIT) {
399 1.2 thorpej KASSERT(itimer_lock_held());
400 1.2 thorpej } else {
401 1.2 thorpej itimer_lock();
402 1.2 thorpej }
403 1.2 thorpej
404 1.2 thorpej kn->kn_data = (int64_t)timerfd_fire_count(tfd);
405 1.6 thorpej rv = kn->kn_data != 0;
406 1.2 thorpej
407 1.2 thorpej if ((hint & NOTE_SUBMIT) == 0) {
408 1.2 thorpej itimer_unlock();
409 1.2 thorpej }
410 1.2 thorpej
411 1.6 thorpej return rv;
412 1.2 thorpej }
413 1.2 thorpej
414 1.2 thorpej static const struct filterops timerfd_read_filterops = {
415 1.5 thorpej .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
416 1.2 thorpej .f_detach = timerfd_filt_read_detach,
417 1.2 thorpej .f_event = timerfd_filt_read,
418 1.2 thorpej };
419 1.2 thorpej
420 1.2 thorpej static int
421 1.2 thorpej timerfd_fop_kqfilter(file_t * const fp, struct knote * const kn)
422 1.2 thorpej {
423 1.2 thorpej struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
424 1.2 thorpej struct selinfo *sel;
425 1.2 thorpej
426 1.2 thorpej switch (kn->kn_filter) {
427 1.2 thorpej case EVFILT_READ:
428 1.2 thorpej sel = &tfd->tfd_read_sel;
429 1.2 thorpej kn->kn_fop = &timerfd_read_filterops;
430 1.2 thorpej break;
431 1.2 thorpej
432 1.2 thorpej default:
433 1.2 thorpej return EINVAL;
434 1.2 thorpej }
435 1.2 thorpej
436 1.2 thorpej kn->kn_hook = tfd;
437 1.2 thorpej
438 1.2 thorpej itimer_lock();
439 1.2 thorpej selrecord_knote(sel, kn);
440 1.2 thorpej itimer_unlock();
441 1.2 thorpej
442 1.2 thorpej return 0;
443 1.2 thorpej }
444 1.2 thorpej
445 1.2 thorpej static void
446 1.2 thorpej timerfd_fop_restart(file_t * const fp)
447 1.2 thorpej {
448 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
449 1.2 thorpej
450 1.2 thorpej /*
451 1.2 thorpej * Unblock blocked reads in order to allow close() to complete.
452 1.2 thorpej * System calls return ERESTART so that the fd is revalidated.
453 1.2 thorpej */
454 1.2 thorpej
455 1.2 thorpej itimer_lock();
456 1.2 thorpej
457 1.2 thorpej if (tfd->tfd_nwaiters != 0) {
458 1.2 thorpej tfd->tfd_restarting = true;
459 1.2 thorpej cv_broadcast(&tfd->tfd_read_wait);
460 1.2 thorpej }
461 1.2 thorpej
462 1.2 thorpej itimer_unlock();
463 1.2 thorpej }
464 1.2 thorpej
465 1.2 thorpej static const struct fileops timerfd_fileops = {
466 1.2 thorpej .fo_name = "timerfd",
467 1.2 thorpej .fo_read = timerfd_fop_read,
468 1.2 thorpej .fo_write = fbadop_write,
469 1.2 thorpej .fo_ioctl = timerfd_fop_ioctl,
470 1.2 thorpej .fo_fcntl = fnullop_fcntl,
471 1.2 thorpej .fo_poll = timerfd_fop_poll,
472 1.2 thorpej .fo_stat = timerfd_fop_stat,
473 1.2 thorpej .fo_close = timerfd_fop_close,
474 1.2 thorpej .fo_kqfilter = timerfd_fop_kqfilter,
475 1.2 thorpej .fo_restart = timerfd_fop_restart,
476 1.2 thorpej };
477 1.2 thorpej
478 1.2 thorpej /*
479 1.2 thorpej * timerfd_create(2) system call
480 1.2 thorpej */
481 1.2 thorpej int
482 1.2 thorpej do_timerfd_create(struct lwp * const l, clockid_t const clock_id,
483 1.2 thorpej int const flags, register_t *retval)
484 1.2 thorpej {
485 1.2 thorpej file_t *fp;
486 1.2 thorpej int fd, error;
487 1.2 thorpej
488 1.2 thorpej if (flags & ~(TFD_CLOEXEC | TFD_NONBLOCK)) {
489 1.2 thorpej return EINVAL;
490 1.2 thorpej }
491 1.2 thorpej
492 1.2 thorpej switch (clock_id) {
493 1.2 thorpej case CLOCK_REALTIME:
494 1.2 thorpej case CLOCK_MONOTONIC:
495 1.2 thorpej /* allowed */
496 1.2 thorpej break;
497 1.2 thorpej
498 1.2 thorpej default:
499 1.2 thorpej return EINVAL;
500 1.2 thorpej }
501 1.2 thorpej
502 1.2 thorpej if ((error = fd_allocfile(&fp, &fd)) != 0) {
503 1.2 thorpej return error;
504 1.2 thorpej }
505 1.2 thorpej
506 1.2 thorpej fp->f_flag = FREAD;
507 1.2 thorpej if (flags & TFD_NONBLOCK) {
508 1.2 thorpej fp->f_flag |= FNONBLOCK;
509 1.2 thorpej }
510 1.2 thorpej fp->f_type = DTYPE_TIMERFD;
511 1.2 thorpej fp->f_ops = &timerfd_fileops;
512 1.2 thorpej fp->f_timerfd = timerfd_create(clock_id, flags);
513 1.2 thorpej fd_set_exclose(l, fd, !!(flags & TFD_CLOEXEC));
514 1.2 thorpej fd_affix(curproc, fp, fd);
515 1.2 thorpej
516 1.2 thorpej *retval = fd;
517 1.2 thorpej return 0;
518 1.2 thorpej }
519 1.2 thorpej
520 1.2 thorpej int
521 1.2 thorpej sys_timerfd_create(struct lwp *l, const struct sys_timerfd_create_args *uap,
522 1.2 thorpej register_t *retval)
523 1.2 thorpej {
524 1.2 thorpej /* {
525 1.2 thorpej syscallarg(clockid_t) clock_id;
526 1.2 thorpej syscallarg(int) flags;
527 1.2 thorpej } */
528 1.2 thorpej
529 1.2 thorpej return do_timerfd_create(l, SCARG(uap, clock_id), SCARG(uap, flags),
530 1.2 thorpej retval);
531 1.2 thorpej }
532 1.2 thorpej
533 1.2 thorpej /*
534 1.2 thorpej * timerfd_gettime(2) system call.
535 1.2 thorpej */
536 1.2 thorpej int
537 1.2 thorpej do_timerfd_gettime(struct lwp *l, int fd, struct itimerspec *curr_value,
538 1.2 thorpej register_t *retval)
539 1.2 thorpej {
540 1.2 thorpej file_t *fp;
541 1.2 thorpej
542 1.2 thorpej if ((fp = fd_getfile(fd)) == NULL) {
543 1.2 thorpej return EBADF;
544 1.2 thorpej }
545 1.2 thorpej
546 1.2 thorpej if (fp->f_ops != &timerfd_fileops) {
547 1.2 thorpej fd_putfile(fd);
548 1.2 thorpej return EINVAL;
549 1.2 thorpej }
550 1.2 thorpej
551 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
552 1.2 thorpej itimer_lock();
553 1.2 thorpej itimer_gettime(&tfd->tfd_itimer, curr_value);
554 1.2 thorpej itimer_unlock();
555 1.2 thorpej
556 1.2 thorpej fd_putfile(fd);
557 1.2 thorpej return 0;
558 1.2 thorpej }
559 1.2 thorpej
560 1.2 thorpej int
561 1.2 thorpej sys_timerfd_gettime(struct lwp *l, const struct sys_timerfd_gettime_args *uap,
562 1.2 thorpej register_t *retval)
563 1.2 thorpej {
564 1.2 thorpej /* {
565 1.2 thorpej syscallarg(int) fd;
566 1.2 thorpej syscallarg(struct itimerspec *) curr_value;
567 1.2 thorpej } */
568 1.2 thorpej
569 1.2 thorpej struct itimerspec oits;
570 1.2 thorpej int error;
571 1.2 thorpej
572 1.2 thorpej error = do_timerfd_gettime(l, SCARG(uap, fd), &oits, retval);
573 1.2 thorpej if (error == 0) {
574 1.2 thorpej error = copyout(&oits, SCARG(uap, curr_value), sizeof(oits));
575 1.2 thorpej }
576 1.2 thorpej return error;
577 1.2 thorpej }
578 1.2 thorpej
579 1.2 thorpej /*
580 1.2 thorpej * timerfd_settime(2) system call.
581 1.2 thorpej */
582 1.2 thorpej int
583 1.2 thorpej do_timerfd_settime(struct lwp *l, int fd, int flags,
584 1.2 thorpej const struct itimerspec *new_value, struct itimerspec *old_value,
585 1.2 thorpej register_t *retval)
586 1.2 thorpej {
587 1.2 thorpej file_t *fp;
588 1.2 thorpej int error;
589 1.2 thorpej
590 1.2 thorpej if (flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) {
591 1.2 thorpej return EINVAL;
592 1.2 thorpej }
593 1.2 thorpej
594 1.2 thorpej if ((fp = fd_getfile(fd)) == NULL) {
595 1.2 thorpej return EBADF;
596 1.2 thorpej }
597 1.2 thorpej
598 1.2 thorpej if (fp->f_ops != &timerfd_fileops) {
599 1.2 thorpej fd_putfile(fd);
600 1.2 thorpej return EINVAL;
601 1.2 thorpej }
602 1.2 thorpej
603 1.2 thorpej struct timerfd * const tfd = fp->f_timerfd;
604 1.2 thorpej struct itimer * const it = &tfd->tfd_itimer;
605 1.2 thorpej
606 1.2 thorpej itimer_lock();
607 1.2 thorpej
608 1.2 thorpej restart:
609 1.2 thorpej if (old_value != NULL) {
610 1.2 thorpej *old_value = it->it_time;
611 1.2 thorpej }
612 1.2 thorpej it->it_time = *new_value;
613 1.2 thorpej
614 1.2 thorpej /*
615 1.2 thorpej * If we've been passed a relative value, convert it to an
616 1.2 thorpej * absolute, as that's what the itimer facility expects for
617 1.2 thorpej * non-virtual timers. Also ensure that this doesn't set it
618 1.2 thorpej * to zero or lets it go negative.
619 1.2 thorpej * XXXJRT re-factor.
620 1.2 thorpej */
621 1.2 thorpej if (timespecisset(&it->it_time.it_value) &&
622 1.2 thorpej (flags & TFD_TIMER_ABSTIME) == 0) {
623 1.2 thorpej struct timespec now;
624 1.2 thorpej if (it->it_clockid == CLOCK_REALTIME) {
625 1.2 thorpej getnanotime(&now);
626 1.2 thorpej } else { /* CLOCK_MONOTONIC */
627 1.2 thorpej getnanouptime(&now);
628 1.2 thorpej }
629 1.2 thorpej timespecadd(&it->it_time.it_value, &now,
630 1.2 thorpej &it->it_time.it_value);
631 1.2 thorpej }
632 1.2 thorpej
633 1.2 thorpej error = itimer_settime(it);
634 1.2 thorpej if (error == ERESTART) {
635 1.2 thorpej goto restart;
636 1.2 thorpej }
637 1.2 thorpej KASSERT(error == 0);
638 1.2 thorpej
639 1.2 thorpej /* Reset the expirations counter. */
640 1.2 thorpej it->it_overruns = 0;
641 1.2 thorpej
642 1.2 thorpej if (it->it_clockid == CLOCK_REALTIME) {
643 1.2 thorpej tfd->tfd_cancelled = false;
644 1.2 thorpej tfd->tfd_cancel_on_set = !!(flags & TFD_TIMER_CANCEL_ON_SET);
645 1.2 thorpej }
646 1.2 thorpej
647 1.2 thorpej getnanotime(&tfd->tfd_mtime);
648 1.2 thorpej itimer_unlock();
649 1.2 thorpej
650 1.2 thorpej fd_putfile(fd);
651 1.2 thorpej return error;
652 1.2 thorpej }
653 1.2 thorpej
654 1.2 thorpej int
655 1.2 thorpej sys_timerfd_settime(struct lwp *l, const struct sys_timerfd_settime_args *uap,
656 1.2 thorpej register_t *retval)
657 1.2 thorpej {
658 1.2 thorpej /* {
659 1.2 thorpej syscallarg(int) fd;
660 1.2 thorpej syscallarg(int) flags;
661 1.2 thorpej syscallarg(const struct itimerspec *) new_value;
662 1.2 thorpej syscallarg(struct itimerspec *) old_value;
663 1.2 thorpej } */
664 1.2 thorpej
665 1.2 thorpej struct itimerspec nits, oits, *oitsp = NULL;
666 1.2 thorpej int error;
667 1.2 thorpej
668 1.2 thorpej error = copyin(SCARG(uap, new_value), &nits, sizeof(nits));
669 1.2 thorpej if (error) {
670 1.2 thorpej return error;
671 1.2 thorpej }
672 1.2 thorpej
673 1.2 thorpej if (SCARG(uap, old_value) != NULL) {
674 1.2 thorpej oitsp = &oits;
675 1.2 thorpej }
676 1.2 thorpej
677 1.2 thorpej error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags),
678 1.2 thorpej &nits, oitsp, retval);
679 1.2 thorpej if (error == 0 && oitsp != NULL) {
680 1.2 thorpej error = copyout(oitsp, SCARG(uap, old_value), sizeof(*oitsp));
681 1.2 thorpej }
682 1.2 thorpej return error;
683 1.2 thorpej }
684