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