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