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