Home | History | Annotate | Line # | Download | only in kern
sys_timerfd.c revision 1.11
      1  1.11  riastrad /*	$NetBSD: sys_timerfd.c,v 1.11 2024/12/19 23:50:22 riastradh 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.11  riastrad __KERNEL_RCSID(0, "$NetBSD: sys_timerfd.c,v 1.11 2024/12/19 23:50:22 riastradh 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.8   thorpej 	case FIONBIO:
    310   1.8   thorpej 		break;
    311   1.8   thorpej 
    312   1.8   thorpej 	case FIONREAD:
    313   1.8   thorpej 		itimer_lock();
    314   1.8   thorpej 		*(int *)data = timerfd_is_readable(tfd) ? sizeof(uint64_t) : 0;
    315   1.8   thorpej 		itimer_unlock();
    316   1.8   thorpej 		break;
    317   1.8   thorpej 
    318   1.2   thorpej 	case TFD_IOC_SET_TICKS: {
    319   1.2   thorpej 		const uint64_t * const new_ticksp = data;
    320   1.2   thorpej 		if (*new_ticksp > INT_MAX) {
    321   1.2   thorpej 			return EINVAL;
    322   1.2   thorpej 		}
    323   1.2   thorpej 		itimer_lock();
    324   1.2   thorpej 		tfd->tfd_itimer.it_overruns = (int)*new_ticksp;
    325   1.2   thorpej 		itimer_unlock();
    326   1.2   thorpej 		break;
    327   1.2   thorpej 	    }
    328   1.2   thorpej 
    329   1.2   thorpej 	default:
    330   1.2   thorpej 		error = EPASSTHROUGH;
    331   1.2   thorpej 	}
    332   1.2   thorpej 
    333   1.2   thorpej 	return error;
    334   1.2   thorpej }
    335   1.2   thorpej 
    336   1.2   thorpej static int
    337   1.2   thorpej timerfd_fop_poll(file_t * const fp, int const events)
    338   1.2   thorpej {
    339   1.2   thorpej 	struct timerfd * const tfd = fp->f_timerfd;
    340   1.9  riastrad 	int revents = 0;
    341   1.2   thorpej 
    342   1.2   thorpej 	if (events & (POLLIN | POLLRDNORM)) {
    343   1.2   thorpej 		itimer_lock();
    344   1.2   thorpej 		if (timerfd_is_readable(tfd)) {
    345   1.2   thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    346   1.2   thorpej 		} else {
    347   1.2   thorpej 			selrecord(curlwp, &tfd->tfd_read_sel);
    348   1.2   thorpej 		}
    349   1.2   thorpej 		itimer_unlock();
    350   1.2   thorpej 	}
    351   1.2   thorpej 
    352   1.2   thorpej 	return revents;
    353   1.2   thorpej }
    354   1.2   thorpej 
    355   1.2   thorpej static int
    356   1.2   thorpej timerfd_fop_stat(file_t * const fp, struct stat * const st)
    357   1.2   thorpej {
    358   1.2   thorpej 	struct timerfd * const tfd = fp->f_timerfd;
    359   1.2   thorpej 
    360   1.2   thorpej 	memset(st, 0, sizeof(*st));
    361   1.2   thorpej 
    362   1.2   thorpej 	itimer_lock();
    363   1.2   thorpej 	st->st_size = (off_t)timerfd_fire_count(tfd);
    364   1.2   thorpej 	st->st_atimespec = tfd->tfd_atime;
    365   1.2   thorpej 	st->st_mtimespec = tfd->tfd_mtime;
    366   1.2   thorpej 	itimer_unlock();
    367   1.2   thorpej 
    368   1.2   thorpej 	st->st_blksize = sizeof(uint64_t);
    369   1.2   thorpej 	st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
    370   1.2   thorpej 	st->st_blocks = 1;
    371   1.2   thorpej 	st->st_birthtimespec = tfd->tfd_btime;
    372   1.2   thorpej 	st->st_ctimespec = st->st_mtimespec;
    373   1.2   thorpej 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    374   1.2   thorpej 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    375   1.2   thorpej 
    376   1.2   thorpej 	return 0;
    377   1.2   thorpej }
    378   1.2   thorpej 
    379   1.2   thorpej static int
    380   1.2   thorpej timerfd_fop_close(file_t * const fp)
    381   1.2   thorpej {
    382   1.2   thorpej 	struct timerfd * const tfd = fp->f_timerfd;
    383   1.2   thorpej 
    384   1.2   thorpej 	fp->f_timerfd = NULL;
    385   1.2   thorpej 	timerfd_destroy(tfd);
    386   1.2   thorpej 
    387   1.2   thorpej 	return 0;
    388   1.2   thorpej }
    389   1.2   thorpej 
    390   1.2   thorpej static void
    391   1.2   thorpej timerfd_filt_read_detach(struct knote * const kn)
    392   1.2   thorpej {
    393   1.2   thorpej 	struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
    394   1.2   thorpej 
    395   1.2   thorpej 	itimer_lock();
    396   1.2   thorpej 	KASSERT(kn->kn_hook == tfd);
    397   1.2   thorpej 	selremove_knote(&tfd->tfd_read_sel, kn);
    398   1.2   thorpej 	itimer_unlock();
    399   1.2   thorpej }
    400   1.2   thorpej 
    401   1.2   thorpej static int
    402   1.2   thorpej timerfd_filt_read(struct knote * const kn, long const hint)
    403   1.2   thorpej {
    404   1.2   thorpej 	struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
    405   1.6   thorpej 	int rv;
    406   1.2   thorpej 
    407   1.2   thorpej 	if (hint & NOTE_SUBMIT) {
    408   1.2   thorpej 		KASSERT(itimer_lock_held());
    409   1.2   thorpej 	} else {
    410   1.2   thorpej 		itimer_lock();
    411   1.2   thorpej 	}
    412   1.2   thorpej 
    413   1.2   thorpej 	kn->kn_data = (int64_t)timerfd_fire_count(tfd);
    414   1.6   thorpej 	rv = kn->kn_data != 0;
    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.6   thorpej 	return rv;
    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.10  riastrad 	struct itimerspec value = *new_value;
    597   1.2   thorpej 	file_t *fp;
    598   1.2   thorpej 	int error;
    599   1.2   thorpej 
    600   1.2   thorpej 	if (flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) {
    601   1.2   thorpej 		return EINVAL;
    602   1.2   thorpej 	}
    603  1.10  riastrad 	if (itimespecfix(&value.it_value) != 0 ||
    604  1.10  riastrad 	    itimespecfix(&value.it_interval) != 0) {
    605  1.10  riastrad 		return EINVAL;
    606  1.10  riastrad 	}
    607   1.2   thorpej 
    608   1.2   thorpej 	if ((fp = fd_getfile(fd)) == NULL) {
    609   1.2   thorpej 		return EBADF;
    610   1.2   thorpej 	}
    611   1.2   thorpej 
    612   1.2   thorpej 	if (fp->f_ops != &timerfd_fileops) {
    613   1.2   thorpej 		fd_putfile(fd);
    614   1.2   thorpej 		return EINVAL;
    615   1.2   thorpej 	}
    616   1.2   thorpej 
    617   1.2   thorpej 	struct timerfd * const tfd = fp->f_timerfd;
    618   1.2   thorpej 	struct itimer * const it = &tfd->tfd_itimer;
    619   1.2   thorpej 
    620   1.2   thorpej 	itimer_lock();
    621   1.2   thorpej 
    622   1.2   thorpej  restart:
    623   1.2   thorpej 	if (old_value != NULL) {
    624  1.11  riastrad 		itimer_gettime(it, old_value);
    625   1.2   thorpej 	}
    626  1.10  riastrad 	it->it_time = value;
    627   1.2   thorpej 
    628   1.2   thorpej 	/*
    629   1.2   thorpej 	 * If we've been passed a relative value, convert it to an
    630   1.2   thorpej 	 * absolute, as that's what the itimer facility expects for
    631   1.2   thorpej 	 * non-virtual timers.  Also ensure that this doesn't set it
    632   1.2   thorpej 	 * to zero or lets it go negative.
    633   1.2   thorpej 	 * XXXJRT re-factor.
    634   1.2   thorpej 	 */
    635   1.2   thorpej 	if (timespecisset(&it->it_time.it_value) &&
    636   1.2   thorpej 	    (flags & TFD_TIMER_ABSTIME) == 0) {
    637   1.2   thorpej 		struct timespec now;
    638   1.2   thorpej 		if (it->it_clockid == CLOCK_REALTIME) {
    639   1.2   thorpej 			getnanotime(&now);
    640   1.2   thorpej 		} else { /* CLOCK_MONOTONIC */
    641   1.2   thorpej 			getnanouptime(&now);
    642   1.2   thorpej 		}
    643   1.2   thorpej 		timespecadd(&it->it_time.it_value, &now,
    644   1.2   thorpej 		    &it->it_time.it_value);
    645   1.2   thorpej 	}
    646   1.2   thorpej 
    647   1.2   thorpej 	error = itimer_settime(it);
    648   1.2   thorpej 	if (error == ERESTART) {
    649   1.2   thorpej 		goto restart;
    650   1.2   thorpej 	}
    651   1.2   thorpej 	KASSERT(error == 0);
    652   1.2   thorpej 
    653   1.2   thorpej 	/* Reset the expirations counter. */
    654   1.2   thorpej 	it->it_overruns = 0;
    655   1.2   thorpej 
    656   1.2   thorpej 	if (it->it_clockid == CLOCK_REALTIME) {
    657   1.2   thorpej 		tfd->tfd_cancelled = false;
    658   1.2   thorpej 		tfd->tfd_cancel_on_set = !!(flags & TFD_TIMER_CANCEL_ON_SET);
    659   1.2   thorpej 	}
    660   1.2   thorpej 
    661   1.2   thorpej 	getnanotime(&tfd->tfd_mtime);
    662   1.2   thorpej 	itimer_unlock();
    663   1.2   thorpej 
    664   1.2   thorpej 	fd_putfile(fd);
    665   1.2   thorpej 	return error;
    666   1.2   thorpej }
    667   1.2   thorpej 
    668   1.2   thorpej int
    669   1.2   thorpej sys_timerfd_settime(struct lwp *l, const struct sys_timerfd_settime_args *uap,
    670   1.2   thorpej     register_t *retval)
    671   1.2   thorpej {
    672   1.2   thorpej 	/* {
    673   1.2   thorpej 		syscallarg(int) fd;
    674   1.2   thorpej 		syscallarg(int) flags;
    675   1.2   thorpej 		syscallarg(const struct itimerspec *) new_value;
    676   1.2   thorpej 		syscallarg(struct itimerspec *) old_value;
    677   1.2   thorpej 	} */
    678   1.2   thorpej 
    679   1.2   thorpej 	struct itimerspec nits, oits, *oitsp = NULL;
    680   1.2   thorpej 	int error;
    681   1.2   thorpej 
    682   1.2   thorpej 	error = copyin(SCARG(uap, new_value), &nits, sizeof(nits));
    683   1.2   thorpej 	if (error) {
    684   1.2   thorpej 		return error;
    685   1.2   thorpej 	}
    686   1.2   thorpej 
    687   1.2   thorpej 	if (SCARG(uap, old_value) != NULL) {
    688   1.2   thorpej 		oitsp = &oits;
    689   1.2   thorpej 	}
    690   1.2   thorpej 
    691   1.2   thorpej 	error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags),
    692   1.2   thorpej 	    &nits, oitsp, retval);
    693   1.2   thorpej 	if (error == 0 && oitsp != NULL) {
    694   1.2   thorpej 		error = copyout(oitsp, SCARG(uap, old_value), sizeof(*oitsp));
    695   1.2   thorpej 	}
    696   1.2   thorpej 	return error;
    697   1.2   thorpej }
    698