2014-07-25 12:10:31 +04:00
|
|
|
/* $NetBSD: clockctl.c,v 1.32 2014/07/25 08:10:35 dholland Exp $ */
|
2001-09-16 10:53:54 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Emmanuel Dreyfus.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 08:32:49 +03:00
|
|
|
#include <sys/cdefs.h>
|
2014-07-25 12:10:31 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.32 2014/07/25 08:10:35 dholland Exp $");
|
2002-02-26 00:16:36 +03:00
|
|
|
|
|
|
|
#include "opt_ntp.h"
|
2009-01-11 05:45:45 +03:00
|
|
|
#include "opt_compat_netbsd.h"
|
2001-11-13 08:32:49 +03:00
|
|
|
|
2001-09-16 10:53:54 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/time.h>
|
2002-09-06 17:18:43 +04:00
|
|
|
#include <sys/conf.h>
|
2001-09-16 10:53:54 +04:00
|
|
|
#ifdef NTP
|
|
|
|
#include <sys/timex.h>
|
|
|
|
#endif /* NTP */
|
2009-10-03 06:01:12 +04:00
|
|
|
#include <sys/kauth.h>
|
2001-09-16 10:53:54 +04:00
|
|
|
|
|
|
|
#include <sys/clockctl.h>
|
2009-01-11 05:45:45 +03:00
|
|
|
#ifdef COMPAT_50
|
|
|
|
#include <compat/sys/clockctl.h>
|
|
|
|
#endif
|
2001-09-16 10:53:54 +04:00
|
|
|
|
2002-09-06 17:18:43 +04:00
|
|
|
dev_type_ioctl(clockctlioctl);
|
|
|
|
|
|
|
|
const struct cdevsw clockctl_cdevsw = {
|
2014-03-16 09:20:22 +04:00
|
|
|
.d_open = nullopen,
|
|
|
|
.d_close = nullclose,
|
|
|
|
.d_read = noread,
|
|
|
|
.d_write = nowrite,
|
|
|
|
.d_ioctl = clockctlioctl,
|
|
|
|
.d_stop = nostop,
|
|
|
|
.d_tty = notty,
|
|
|
|
.d_poll = nopoll,
|
|
|
|
.d_mmap = nommap,
|
|
|
|
.d_kqfilter = nokqfilter,
|
2014-07-25 12:10:31 +04:00
|
|
|
.d_discard = nodiscard,
|
2014-03-16 09:20:22 +04:00
|
|
|
.d_flag = D_OTHER,
|
2002-09-06 17:18:43 +04:00
|
|
|
};
|
2001-09-16 10:53:54 +04:00
|
|
|
|
2009-10-03 06:01:12 +04:00
|
|
|
static kauth_listener_t clockctl_listener;
|
|
|
|
|
|
|
|
static int
|
|
|
|
clockctl_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
|
|
|
|
void *arg0, void *arg1, void *arg2, void *arg3)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
enum kauth_system_req req;
|
|
|
|
bool device_context;
|
|
|
|
|
|
|
|
result = KAUTH_RESULT_DEFER;
|
|
|
|
req = (enum kauth_system_req)arg0;
|
|
|
|
|
|
|
|
if ((action != KAUTH_SYSTEM_TIME) ||
|
|
|
|
(req != KAUTH_REQ_SYSTEM_TIME_SYSTEM))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
device_context = (bool)arg3;
|
|
|
|
|
|
|
|
/* Device is controlled by permissions, so allow. */
|
|
|
|
if (device_context)
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-02-11 02:20:15 +03:00
|
|
|
/*ARGSUSED*/
|
2001-09-16 10:53:54 +04:00
|
|
|
void
|
2006-11-16 04:32:37 +03:00
|
|
|
clockctlattach(int num)
|
2001-09-16 10:53:54 +04:00
|
|
|
{
|
2009-10-03 06:01:12 +04:00
|
|
|
|
|
|
|
clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
|
|
|
|
clockctl_listener_cb, NULL);
|
2001-09-16 10:53:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-10-12 05:30:41 +04:00
|
|
|
clockctlioctl(
|
2006-11-16 04:32:37 +03:00
|
|
|
dev_t dev,
|
2006-10-12 05:30:41 +04:00
|
|
|
u_long cmd,
|
2007-03-04 08:59:00 +03:00
|
|
|
void *data,
|
2006-11-16 04:32:37 +03:00
|
|
|
int flags,
|
2006-10-12 05:30:41 +04:00
|
|
|
struct lwp *l)
|
2001-09-16 10:53:54 +04:00
|
|
|
{
|
|
|
|
int error = 0;
|
2005-02-27 03:26:58 +03:00
|
|
|
|
2001-09-16 10:53:54 +04:00
|
|
|
switch (cmd) {
|
2009-01-11 05:45:45 +03:00
|
|
|
case CLOCKCTL_SETTIMEOFDAY: {
|
|
|
|
struct clockctl_settimeofday *args = data;
|
2001-09-16 10:53:54 +04:00
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
error = settimeofday1(args->tv, true, args->tzp, l, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOCKCTL_ADJTIME: {
|
|
|
|
struct timeval atv, oldatv;
|
|
|
|
struct clockctl_adjtime *args = data;
|
2001-09-16 10:53:54 +04:00
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
if (args->delta) {
|
2009-02-22 16:06:58 +03:00
|
|
|
error = copyin(args->delta, &atv, sizeof(atv));
|
2001-09-16 10:53:54 +04:00
|
|
|
if (error)
|
2005-02-27 03:26:58 +03:00
|
|
|
return (error);
|
2001-09-16 10:53:54 +04:00
|
|
|
}
|
2009-01-11 05:45:45 +03:00
|
|
|
adjtime1(args->delta ? &atv : NULL,
|
|
|
|
args->olddelta ? &oldatv : NULL, l->l_proc);
|
|
|
|
if (args->olddelta)
|
|
|
|
error = copyout(&oldatv, args->olddelta,
|
2009-02-22 16:06:58 +03:00
|
|
|
sizeof(oldatv));
|
2009-01-11 05:45:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOCKCTL_CLOCK_SETTIME: {
|
|
|
|
struct clockctl_clock_settime *args = data;
|
2009-02-18 08:17:56 +03:00
|
|
|
struct timespec ts;
|
2001-09-16 10:53:54 +04:00
|
|
|
|
2009-02-18 20:57:11 +03:00
|
|
|
error = copyin(args->tp, &ts, sizeof ts);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
error = clock_settime1(l->l_proc, args->clock_id, &ts, false);
|
2009-01-11 05:45:45 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-09-16 10:53:54 +04:00
|
|
|
#ifdef NTP
|
2009-01-11 05:45:45 +03:00
|
|
|
case CLOCKCTL_NTP_ADJTIME: {
|
|
|
|
struct clockctl_ntp_adjtime *args = data;
|
|
|
|
struct timex ntv;
|
2002-03-02 01:58:33 +03:00
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
error = copyin(args->tp, &ntv, sizeof(ntv));
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2006-06-08 02:33:33 +04:00
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
ntp_adjtime1(&ntv);
|
2006-06-08 02:33:33 +04:00
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
error = copyout(&ntv, args->tp, sizeof(ntv));
|
|
|
|
if (error == 0)
|
2012-01-04 17:40:53 +04:00
|
|
|
args->retval = ntp_timestatus();
|
2009-02-22 16:06:58 +03:00
|
|
|
break;
|
2009-01-11 05:45:45 +03:00
|
|
|
}
|
2001-09-16 10:53:54 +04:00
|
|
|
#endif /* NTP */
|
2009-01-11 05:45:45 +03:00
|
|
|
default:
|
|
|
|
#ifdef COMPAT_50
|
|
|
|
error = compat50_clockctlioctl(dev, cmd, data, flags, l);
|
|
|
|
#else
|
|
|
|
error = EINVAL;
|
|
|
|
#endif
|
2001-09-16 10:53:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|