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