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