Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / clockctl.c
blob4b05069e362a3ba4bcaaca70f8e7f5243ed3d830
1 /* $NetBSD: clockctl.c,v 1.27 2009/02/22 13:06:59 nakayama Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.27 2009/02/22 13:06:59 nakayama Exp $");
36 #include "opt_ntp.h"
37 #include "opt_compat_netbsd.h"
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>
52 #include <sys/clockctl.h>
53 #ifdef COMPAT_50
54 #include <compat/sys/clockctl.h>
55 #endif
57 struct clockctl_softc {
58 struct device clockctl_dev;
61 dev_type_ioctl(clockctlioctl);
63 const struct cdevsw clockctl_cdevsw = {
64 nullopen, nullclose, noread, nowrite, clockctlioctl,
65 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
68 static kauth_listener_t clockctl_listener;
70 static int
71 clockctl_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
72 void *arg0, void *arg1, void *arg2, void *arg3)
74 int result;
75 enum kauth_system_req req;
76 bool device_context;
78 result = KAUTH_RESULT_DEFER;
79 req = (enum kauth_system_req)arg0;
81 if ((action != KAUTH_SYSTEM_TIME) ||
82 (req != KAUTH_REQ_SYSTEM_TIME_SYSTEM))
83 return result;
85 device_context = (bool)arg3;
87 /* Device is controlled by permissions, so allow. */
88 if (device_context)
89 result = KAUTH_RESULT_ALLOW;
91 return result;
94 /*ARGSUSED*/
95 void
96 clockctlattach(int num)
99 clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
100 clockctl_listener_cb, NULL);
104 clockctlioctl(
105 dev_t dev,
106 u_long cmd,
107 void *data,
108 int flags,
109 struct lwp *l)
111 int error = 0;
113 switch (cmd) {
114 case CLOCKCTL_SETTIMEOFDAY: {
115 struct clockctl_settimeofday *args = data;
117 error = settimeofday1(args->tv, true, args->tzp, l, false);
118 break;
120 case CLOCKCTL_ADJTIME: {
121 struct timeval atv, oldatv;
122 struct clockctl_adjtime *args = data;
124 if (args->delta) {
125 error = copyin(args->delta, &atv, sizeof(atv));
126 if (error)
127 return (error);
129 adjtime1(args->delta ? &atv : NULL,
130 args->olddelta ? &oldatv : NULL, l->l_proc);
131 if (args->olddelta)
132 error = copyout(&oldatv, args->olddelta,
133 sizeof(oldatv));
134 break;
136 case CLOCKCTL_CLOCK_SETTIME: {
137 struct clockctl_clock_settime *args = data;
138 struct timespec ts;
140 error = copyin(args->tp, &ts, sizeof ts);
141 if (error)
142 return (error);
143 error = clock_settime1(l->l_proc, args->clock_id, &ts, false);
144 break;
146 #ifdef NTP
147 case CLOCKCTL_NTP_ADJTIME: {
148 struct clockctl_ntp_adjtime *args = data;
149 struct timex ntv;
150 register_t retval;
152 error = copyin(args->tp, &ntv, sizeof(ntv));
153 if (error)
154 return (error);
156 ntp_adjtime1(&ntv);
158 error = copyout(&ntv, args->tp, sizeof(ntv));
159 if (error == 0)
160 error = copyout(&retval, &args->retval, sizeof(retval));
161 break;
163 #endif /* NTP */
164 default:
165 #ifdef COMPAT_50
166 error = compat50_clockctlioctl(dev, cmd, data, flags, l);
167 #else
168 error = EINVAL;
169 #endif
172 return (error);