Home | History | Annotate | Line # | Download | only in dev
clockctl.c revision 1.28.12.2
      1 /*      $NetBSD: clockctl.c,v 1.28.12.2 2012/10/30 17:20:49 yamt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Emmanuel Dreyfus.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.28.12.2 2012/10/30 17:20:49 yamt Exp $");
     35 
     36 #include "opt_ntp.h"
     37 #include "opt_compat_netbsd.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/errno.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/device.h>
     45 #include <sys/time.h>
     46 #include <sys/conf.h>
     47 #ifdef NTP
     48 #include <sys/timex.h>
     49 #endif /* NTP */
     50 #include <sys/kauth.h>
     51 
     52 #include <sys/clockctl.h>
     53 #ifdef COMPAT_50
     54 #include <compat/sys/clockctl.h>
     55 #endif
     56 
     57 dev_type_ioctl(clockctlioctl);
     58 
     59 const struct cdevsw clockctl_cdevsw = {
     60 	nullopen, nullclose, noread, nowrite, clockctlioctl,
     61 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
     62 };
     63 
     64 static kauth_listener_t clockctl_listener;
     65 
     66 static int
     67 clockctl_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
     68     void *arg0, void *arg1, void *arg2, void *arg3)
     69 {
     70 	int result;
     71 	enum kauth_system_req req;
     72 	bool device_context;
     73 
     74 	result = KAUTH_RESULT_DEFER;
     75 	req = (enum kauth_system_req)arg0;
     76 
     77 	if ((action != KAUTH_SYSTEM_TIME) ||
     78 	    (req != KAUTH_REQ_SYSTEM_TIME_SYSTEM))
     79 		return result;
     80 
     81 	device_context = (bool)arg3;
     82 
     83 	/* Device is controlled by permissions, so allow. */
     84 	if (device_context)
     85 		result = KAUTH_RESULT_ALLOW;
     86 
     87 	return result;
     88 }
     89 
     90 /*ARGSUSED*/
     91 void
     92 clockctlattach(int num)
     93 {
     94 
     95 	clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
     96 	    clockctl_listener_cb, NULL);
     97 }
     98 
     99 int
    100 clockctlioctl(
    101     dev_t dev,
    102     u_long cmd,
    103     void *data,
    104     int flags,
    105     struct lwp *l)
    106 {
    107 	int error = 0;
    108 
    109 	switch (cmd) {
    110 	case CLOCKCTL_SETTIMEOFDAY: {
    111 		struct clockctl_settimeofday *args = data;
    112 
    113 		error = settimeofday1(args->tv, true, args->tzp, l, false);
    114 		break;
    115 	}
    116 	case CLOCKCTL_ADJTIME: {
    117 		struct timeval atv, oldatv;
    118 		struct clockctl_adjtime *args = data;
    119 
    120 		if (args->delta) {
    121 			error = copyin(args->delta, &atv, sizeof(atv));
    122 			if (error)
    123 				return (error);
    124 		}
    125 		adjtime1(args->delta ? &atv : NULL,
    126 		    args->olddelta ? &oldatv : NULL, l->l_proc);
    127 		if (args->olddelta)
    128 			error = copyout(&oldatv, args->olddelta,
    129 			    sizeof(oldatv));
    130 		break;
    131 	}
    132 	case CLOCKCTL_CLOCK_SETTIME: {
    133 		struct clockctl_clock_settime *args = data;
    134 		struct timespec ts;
    135 
    136 		error = copyin(args->tp, &ts, sizeof ts);
    137 		if (error)
    138 			return (error);
    139 		error = clock_settime1(l->l_proc, args->clock_id, &ts, false);
    140 		break;
    141 	}
    142 #ifdef NTP
    143 	case CLOCKCTL_NTP_ADJTIME: {
    144 		struct clockctl_ntp_adjtime *args = data;
    145 		struct timex ntv;
    146 
    147 		error = copyin(args->tp, &ntv, sizeof(ntv));
    148 		if (error)
    149 			return (error);
    150 
    151 		ntp_adjtime1(&ntv);
    152 
    153 		error = copyout(&ntv, args->tp, sizeof(ntv));
    154 		if (error == 0)
    155 			args->retval = ntp_timestatus();
    156 		break;
    157 	}
    158 #endif /* NTP */
    159 	default:
    160 #ifdef COMPAT_50
    161 		error = compat50_clockctlioctl(dev, cmd, data, flags, l);
    162 #else
    163 		error = EINVAL;
    164 #endif
    165 	}
    166 
    167 	return (error);
    168 }
    169 
    170 
    171