clockctl.c revision 1.32.4.1 1 1.32.4.1 skrll /* $NetBSD: clockctl.c,v 1.32.4.1 2015/12/27 12:09:48 skrll 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.32.4.1 skrll __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.32.4.1 2015/12/27 12:09:48 skrll Exp $");
35 1.5 manu
36 1.32.4.1 skrll #ifdef _KERNEL_OPT
37 1.5 manu #include "opt_ntp.h"
38 1.24 christos #include "opt_compat_netbsd.h"
39 1.32.4.1 skrll #endif
40 1.1 manu
41 1.1 manu #include <sys/param.h>
42 1.1 manu #include <sys/systm.h>
43 1.1 manu #include <sys/proc.h>
44 1.1 manu #include <sys/errno.h>
45 1.1 manu #include <sys/ioctl.h>
46 1.1 manu #include <sys/device.h>
47 1.1 manu #include <sys/time.h>
48 1.7 gehenna #include <sys/conf.h>
49 1.1 manu #ifdef NTP
50 1.1 manu #include <sys/timex.h>
51 1.1 manu #endif /* NTP */
52 1.28 elad #include <sys/kauth.h>
53 1.32.4.1 skrll #include <sys/module.h>
54 1.32.4.1 skrll #include <sys/mutex.h>
55 1.32.4.1 skrll #include <sys/once.h>
56 1.1 manu
57 1.1 manu #include <sys/clockctl.h>
58 1.24 christos #ifdef COMPAT_50
59 1.24 christos #include <compat/sys/clockctl.h>
60 1.32.4.1 skrll #include <compat/sys/time_types.h>
61 1.24 christos #endif
62 1.1 manu
63 1.32.4.1 skrll kmutex_t clockctl_mtx;
64 1.32.4.1 skrll int clockctl_refcnt;
65 1.32.4.1 skrll
66 1.32.4.1 skrll ONCE_DECL(clockctl_once);
67 1.32.4.1 skrll
68 1.32.4.1 skrll #include "ioconf.h"
69 1.32.4.1 skrll
70 1.7 gehenna dev_type_ioctl(clockctlioctl);
71 1.7 gehenna
72 1.7 gehenna const struct cdevsw clockctl_cdevsw = {
73 1.32.4.1 skrll .d_open = clockctlopen,
74 1.32.4.1 skrll .d_close = clockctlclose,
75 1.31 dholland .d_read = noread,
76 1.31 dholland .d_write = nowrite,
77 1.31 dholland .d_ioctl = clockctlioctl,
78 1.31 dholland .d_stop = nostop,
79 1.31 dholland .d_tty = notty,
80 1.31 dholland .d_poll = nopoll,
81 1.31 dholland .d_mmap = nommap,
82 1.31 dholland .d_kqfilter = nokqfilter,
83 1.32 dholland .d_discard = nodiscard,
84 1.31 dholland .d_flag = D_OTHER,
85 1.7 gehenna };
86 1.1 manu
87 1.28 elad static kauth_listener_t clockctl_listener;
88 1.28 elad
89 1.28 elad static int
90 1.28 elad clockctl_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
91 1.28 elad void *arg0, void *arg1, void *arg2, void *arg3)
92 1.28 elad {
93 1.28 elad int result;
94 1.28 elad enum kauth_system_req req;
95 1.28 elad bool device_context;
96 1.28 elad
97 1.28 elad result = KAUTH_RESULT_DEFER;
98 1.28 elad req = (enum kauth_system_req)arg0;
99 1.28 elad
100 1.28 elad if ((action != KAUTH_SYSTEM_TIME) ||
101 1.28 elad (req != KAUTH_REQ_SYSTEM_TIME_SYSTEM))
102 1.28 elad return result;
103 1.28 elad
104 1.28 elad device_context = (bool)arg3;
105 1.28 elad
106 1.28 elad /* Device is controlled by permissions, so allow. */
107 1.28 elad if (device_context)
108 1.28 elad result = KAUTH_RESULT_ALLOW;
109 1.28 elad
110 1.28 elad return result;
111 1.28 elad }
112 1.28 elad
113 1.10 perry /*ARGSUSED*/
114 1.1 manu void
115 1.20 christos clockctlattach(int num)
116 1.1 manu {
117 1.28 elad
118 1.32.4.1 skrll /*
119 1.32.4.1 skrll * Don't initialize the listener here - it will get handled as part
120 1.32.4.1 skrll * of module initialization.
121 1.32.4.1 skrll */
122 1.32.4.1 skrll #if 0
123 1.28 elad clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
124 1.28 elad clockctl_listener_cb, NULL);
125 1.32.4.1 skrll #endif
126 1.32.4.1 skrll }
127 1.32.4.1 skrll
128 1.32.4.1 skrll /*
129 1.32.4.1 skrll * Maintain a refcount for each open/close, so we know when it is
130 1.32.4.1 skrll * safe to call devsw_detach()
131 1.32.4.1 skrll */
132 1.32.4.1 skrll int
133 1.32.4.1 skrll clockctlopen(dev_t dev, int flag, int mode, struct lwp *l)
134 1.32.4.1 skrll {
135 1.32.4.1 skrll
136 1.32.4.1 skrll mutex_enter(&clockctl_mtx);
137 1.32.4.1 skrll clockctl_refcnt++;
138 1.32.4.1 skrll mutex_exit(&clockctl_mtx);
139 1.32.4.1 skrll
140 1.32.4.1 skrll return 0;
141 1.32.4.1 skrll }
142 1.32.4.1 skrll
143 1.32.4.1 skrll int
144 1.32.4.1 skrll clockctlclose(dev_t dev, int flag, int mode, struct lwp *l)
145 1.32.4.1 skrll {
146 1.32.4.1 skrll
147 1.32.4.1 skrll mutex_enter(&clockctl_mtx);
148 1.32.4.1 skrll clockctl_refcnt--;
149 1.32.4.1 skrll mutex_exit(&clockctl_mtx);
150 1.32.4.1 skrll
151 1.32.4.1 skrll return 0;
152 1.32.4.1 skrll }
153 1.32.4.1 skrll
154 1.32.4.1 skrll int
155 1.32.4.1 skrll clockctl_init(void)
156 1.32.4.1 skrll {
157 1.32.4.1 skrll
158 1.32.4.1 skrll mutex_init(&clockctl_mtx, MUTEX_DEFAULT, IPL_NONE);
159 1.32.4.1 skrll clockctl_refcnt = 0;
160 1.32.4.1 skrll return 0;
161 1.32.4.1 skrll }
162 1.32.4.1 skrll
163 1.32.4.1 skrll MODULE(MODULE_CLASS_DRIVER, clockctl, NULL);
164 1.32.4.1 skrll
165 1.32.4.1 skrll int
166 1.32.4.1 skrll clockctl_modcmd(modcmd_t cmd, void *data)
167 1.32.4.1 skrll {
168 1.32.4.1 skrll int error;
169 1.32.4.1 skrll #ifdef _MODULE
170 1.32.4.1 skrll int bmajor, cmajor;
171 1.32.4.1 skrll #endif
172 1.32.4.1 skrll
173 1.32.4.1 skrll error = 0;
174 1.32.4.1 skrll
175 1.32.4.1 skrll switch (cmd) {
176 1.32.4.1 skrll case MODULE_CMD_INIT:
177 1.32.4.1 skrll RUN_ONCE(&clockctl_once, clockctl_init);
178 1.32.4.1 skrll
179 1.32.4.1 skrll clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
180 1.32.4.1 skrll clockctl_listener_cb, NULL);
181 1.32.4.1 skrll
182 1.32.4.1 skrll #ifdef _MODULE
183 1.32.4.1 skrll bmajor = cmajor = -1;
184 1.32.4.1 skrll error = devsw_attach("clockctl", NULL, &bmajor,
185 1.32.4.1 skrll &clockctl_cdevsw, &cmajor);
186 1.32.4.1 skrll if (error != 0)
187 1.32.4.1 skrll kauth_unlisten_scope(clockctl_listener);
188 1.32.4.1 skrll #endif
189 1.32.4.1 skrll
190 1.32.4.1 skrll break;
191 1.32.4.1 skrll
192 1.32.4.1 skrll case MODULE_CMD_FINI:
193 1.32.4.1 skrll mutex_enter(&clockctl_mtx);
194 1.32.4.1 skrll if (clockctl_refcnt != 0) {
195 1.32.4.1 skrll mutex_exit(&clockctl_mtx);
196 1.32.4.1 skrll return EBUSY;
197 1.32.4.1 skrll }
198 1.32.4.1 skrll #ifdef _MODULE
199 1.32.4.1 skrll error = devsw_detach(NULL, &clockctl_cdevsw);
200 1.32.4.1 skrll #endif
201 1.32.4.1 skrll mutex_exit(&clockctl_mtx);
202 1.32.4.1 skrll
203 1.32.4.1 skrll if (error == 0)
204 1.32.4.1 skrll kauth_unlisten_scope(clockctl_listener);
205 1.32.4.1 skrll
206 1.32.4.1 skrll break;
207 1.32.4.1 skrll
208 1.32.4.1 skrll default:
209 1.32.4.1 skrll error = ENOTTY;
210 1.32.4.1 skrll break;
211 1.32.4.1 skrll }
212 1.32.4.1 skrll
213 1.32.4.1 skrll return error;
214 1.1 manu }
215 1.1 manu
216 1.1 manu int
217 1.19 christos clockctlioctl(
218 1.20 christos dev_t dev,
219 1.19 christos u_long cmd,
220 1.21 christos void *data,
221 1.20 christos int flags,
222 1.19 christos struct lwp *l)
223 1.1 manu {
224 1.1 manu int error = 0;
225 1.13 perry
226 1.1 manu switch (cmd) {
227 1.24 christos case CLOCKCTL_SETTIMEOFDAY: {
228 1.24 christos struct clockctl_settimeofday *args = data;
229 1.1 manu
230 1.24 christos error = settimeofday1(args->tv, true, args->tzp, l, false);
231 1.24 christos break;
232 1.24 christos }
233 1.24 christos case CLOCKCTL_ADJTIME: {
234 1.24 christos struct timeval atv, oldatv;
235 1.24 christos struct clockctl_adjtime *args = data;
236 1.1 manu
237 1.24 christos if (args->delta) {
238 1.27 nakayama error = copyin(args->delta, &atv, sizeof(atv));
239 1.1 manu if (error)
240 1.13 perry return (error);
241 1.1 manu }
242 1.24 christos adjtime1(args->delta ? &atv : NULL,
243 1.24 christos args->olddelta ? &oldatv : NULL, l->l_proc);
244 1.24 christos if (args->olddelta)
245 1.24 christos error = copyout(&oldatv, args->olddelta,
246 1.27 nakayama sizeof(oldatv));
247 1.24 christos break;
248 1.24 christos }
249 1.24 christos case CLOCKCTL_CLOCK_SETTIME: {
250 1.24 christos struct clockctl_clock_settime *args = data;
251 1.25 mrg struct timespec ts;
252 1.1 manu
253 1.26 mrg error = copyin(args->tp, &ts, sizeof ts);
254 1.26 mrg if (error)
255 1.26 mrg return (error);
256 1.26 mrg error = clock_settime1(l->l_proc, args->clock_id, &ts, false);
257 1.24 christos break;
258 1.24 christos }
259 1.1 manu #ifdef NTP
260 1.24 christos case CLOCKCTL_NTP_ADJTIME: {
261 1.24 christos struct clockctl_ntp_adjtime *args = data;
262 1.24 christos struct timex ntv;
263 1.1 manu
264 1.24 christos error = copyin(args->tp, &ntv, sizeof(ntv));
265 1.24 christos if (error)
266 1.24 christos return (error);
267 1.6 manu
268 1.24 christos ntp_adjtime1(&ntv);
269 1.17 kardel
270 1.24 christos error = copyout(&ntv, args->tp, sizeof(ntv));
271 1.24 christos if (error == 0)
272 1.29 apb args->retval = ntp_timestatus();
273 1.27 nakayama break;
274 1.24 christos }
275 1.1 manu #endif /* NTP */
276 1.24 christos default:
277 1.24 christos #ifdef COMPAT_50
278 1.24 christos error = compat50_clockctlioctl(dev, cmd, data, flags, l);
279 1.24 christos #else
280 1.24 christos error = EINVAL;
281 1.24 christos #endif
282 1.1 manu }
283 1.1 manu
284 1.1 manu return (error);
285 1.1 manu }
286 1.1 manu
287 1.32.4.1 skrll #ifdef COMPAT_50
288 1.32.4.1 skrll int
289 1.32.4.1 skrll compat50_clockctlioctl(dev_t dev, u_long cmd, void *data, int flags,
290 1.32.4.1 skrll struct lwp *l)
291 1.32.4.1 skrll {
292 1.32.4.1 skrll int error = 0;
293 1.32.4.1 skrll const struct cdevsw *cd = cdevsw_lookup(dev);
294 1.32.4.1 skrll
295 1.32.4.1 skrll if (cd == NULL || cd->d_ioctl == NULL)
296 1.32.4.1 skrll return ENXIO;
297 1.32.4.1 skrll
298 1.32.4.1 skrll switch (cmd) {
299 1.32.4.1 skrll case CLOCKCTL_OSETTIMEOFDAY: {
300 1.32.4.1 skrll struct timeval50 tv50;
301 1.32.4.1 skrll struct timeval tv;
302 1.32.4.1 skrll struct clockctl50_settimeofday *args = data;
303 1.1 manu
304 1.32.4.1 skrll error = copyin(args->tv, &tv50, sizeof(tv50));
305 1.32.4.1 skrll if (error)
306 1.32.4.1 skrll return (error);
307 1.32.4.1 skrll timeval50_to_timeval(&tv50, &tv);
308 1.32.4.1 skrll error = settimeofday1(&tv, false, args->tzp, l, false);
309 1.32.4.1 skrll break;
310 1.32.4.1 skrll }
311 1.32.4.1 skrll case CLOCKCTL_OADJTIME: {
312 1.32.4.1 skrll struct timeval atv, oldatv;
313 1.32.4.1 skrll struct timeval50 atv50;
314 1.32.4.1 skrll struct clockctl50_adjtime *args = data;
315 1.32.4.1 skrll
316 1.32.4.1 skrll if (args->delta) {
317 1.32.4.1 skrll error = copyin(args->delta, &atv50, sizeof(atv50));
318 1.32.4.1 skrll if (error)
319 1.32.4.1 skrll return (error);
320 1.32.4.1 skrll timeval50_to_timeval(&atv50, &atv);
321 1.32.4.1 skrll }
322 1.32.4.1 skrll adjtime1(args->delta ? &atv : NULL,
323 1.32.4.1 skrll args->olddelta ? &oldatv : NULL, l->l_proc);
324 1.32.4.1 skrll if (args->olddelta) {
325 1.32.4.1 skrll timeval_to_timeval50(&oldatv, &atv50);
326 1.32.4.1 skrll error = copyout(&atv50, args->olddelta, sizeof(atv50));
327 1.32.4.1 skrll }
328 1.32.4.1 skrll break;
329 1.32.4.1 skrll }
330 1.32.4.1 skrll case CLOCKCTL_OCLOCK_SETTIME: {
331 1.32.4.1 skrll struct timespec50 tp50;
332 1.32.4.1 skrll struct timespec tp;
333 1.32.4.1 skrll struct clockctl50_clock_settime *args = data;
334 1.32.4.1 skrll
335 1.32.4.1 skrll error = copyin(args->tp, &tp50, sizeof(tp50));
336 1.32.4.1 skrll if (error)
337 1.32.4.1 skrll return (error);
338 1.32.4.1 skrll timespec50_to_timespec(&tp50, &tp);
339 1.32.4.1 skrll error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
340 1.32.4.1 skrll break;
341 1.32.4.1 skrll }
342 1.32.4.1 skrll case CLOCKCTL_ONTP_ADJTIME:
343 1.32.4.1 skrll /* The ioctl number changed but the data did not change. */
344 1.32.4.1 skrll error = (cd->d_ioctl)(dev, CLOCKCTL_NTP_ADJTIME,
345 1.32.4.1 skrll data, flags, l);
346 1.32.4.1 skrll break;
347 1.32.4.1 skrll default:
348 1.32.4.1 skrll error = EINVAL;
349 1.32.4.1 skrll }
350 1.32.4.1 skrll
351 1.32.4.1 skrll return (error);
352 1.32.4.1 skrll }
353 1.32.4.1 skrll #endif
354