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