Annotation of src/sys/dev/clockctl.c, revision 1.35
1.35 ! rin 1: /* $NetBSD: clockctl.c,v 1.34 2016/01/06 18:06:38 christos Exp $ */
1.1 manu 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: */
1.2 lukem 32:
33: #include <sys/cdefs.h>
1.35 ! rin 34: __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.34 2016/01/06 18:06:38 christos Exp $");
1.5 manu 35:
1.33 pgoyette 36: #ifdef _KERNEL_OPT
1.5 manu 37: #include "opt_ntp.h"
1.24 christos 38: #include "opt_compat_netbsd.h"
1.33 pgoyette 39: #endif
1.1 manu 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>
1.7 gehenna 48: #include <sys/conf.h>
1.1 manu 49: #ifdef NTP
50: #include <sys/timex.h>
51: #endif /* NTP */
1.28 elad 52: #include <sys/kauth.h>
1.33 pgoyette 53: #include <sys/module.h>
54: #include <sys/mutex.h>
1.1 manu 55:
56: #include <sys/clockctl.h>
1.24 christos 57: #ifdef COMPAT_50
58: #include <compat/sys/clockctl.h>
1.33 pgoyette 59: #include <compat/sys/time_types.h>
1.24 christos 60: #endif
1.1 manu 61:
1.33 pgoyette 62: kmutex_t clockctl_mtx;
63: int clockctl_refcnt;
64:
65: #include "ioconf.h"
66:
1.7 gehenna 67: dev_type_ioctl(clockctlioctl);
68:
69: const struct cdevsw clockctl_cdevsw = {
1.33 pgoyette 70: .d_open = clockctlopen,
71: .d_close = clockctlclose,
1.31 dholland 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,
1.32 dholland 80: .d_discard = nodiscard,
1.31 dholland 81: .d_flag = D_OTHER,
1.7 gehenna 82: };
1.1 manu 83:
1.28 elad 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:
1.10 perry 110: /*ARGSUSED*/
1.1 manu 111: void
1.20 christos 112: clockctlattach(int num)
1.1 manu 113: {
1.28 elad 114:
1.33 pgoyette 115: /*
116: * Don't initialize the listener here - it will get handled as part
117: * of module initialization.
118: */
119: #if 0
1.28 elad 120: clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
121: clockctl_listener_cb, NULL);
1.33 pgoyette 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:
1.34 christos 165: mutex_init(&clockctl_mtx, MUTEX_DEFAULT, IPL_NONE);
1.33 pgoyette 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:
1.34 christos 191: if (error == 0) {
1.33 pgoyette 192: kauth_unlisten_scope(clockctl_listener);
1.34 christos 193: mutex_destroy(&clockctl_mtx);
194: }
1.33 pgoyette 195: break;
196:
197: default:
198: error = ENOTTY;
199: break;
200: }
201:
202: return error;
1.1 manu 203: }
204:
205: int
1.19 christos 206: clockctlioctl(
1.20 christos 207: dev_t dev,
1.19 christos 208: u_long cmd,
1.21 christos 209: void *data,
1.20 christos 210: int flags,
1.19 christos 211: struct lwp *l)
1.1 manu 212: {
213: int error = 0;
1.13 perry 214:
1.1 manu 215: switch (cmd) {
1.24 christos 216: case CLOCKCTL_SETTIMEOFDAY: {
217: struct clockctl_settimeofday *args = data;
1.1 manu 218:
1.24 christos 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;
1.1 manu 225:
1.24 christos 226: if (args->delta) {
1.27 nakayama 227: error = copyin(args->delta, &atv, sizeof(atv));
1.1 manu 228: if (error)
1.13 perry 229: return (error);
1.1 manu 230: }
1.24 christos 231: adjtime1(args->delta ? &atv : NULL,
232: args->olddelta ? &oldatv : NULL, l->l_proc);
233: if (args->olddelta)
234: error = copyout(&oldatv, args->olddelta,
1.27 nakayama 235: sizeof(oldatv));
1.24 christos 236: break;
237: }
238: case CLOCKCTL_CLOCK_SETTIME: {
239: struct clockctl_clock_settime *args = data;
1.25 mrg 240: struct timespec ts;
1.1 manu 241:
1.26 mrg 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);
1.24 christos 246: break;
247: }
1.1 manu 248: #ifdef NTP
1.24 christos 249: case CLOCKCTL_NTP_ADJTIME: {
250: struct clockctl_ntp_adjtime *args = data;
251: struct timex ntv;
1.1 manu 252:
1.24 christos 253: error = copyin(args->tp, &ntv, sizeof(ntv));
254: if (error)
255: return (error);
1.6 manu 256:
1.24 christos 257: ntp_adjtime1(&ntv);
1.17 kardel 258:
1.24 christos 259: error = copyout(&ntv, args->tp, sizeof(ntv));
260: if (error == 0)
1.29 apb 261: args->retval = ntp_timestatus();
1.27 nakayama 262: break;
1.24 christos 263: }
1.1 manu 264: #endif /* NTP */
1.24 christos 265: default:
266: #ifdef COMPAT_50
267: error = compat50_clockctlioctl(dev, cmd, data, flags, l);
268: #else
1.35 ! rin 269: error = ENOTTY;
1.24 christos 270: #endif
1.1 manu 271: }
272:
273: return (error);
274: }
275:
1.33 pgoyette 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(dev);
283:
284: if (cd == NULL || cd->d_ioctl == NULL)
285: return ENXIO;
286:
287: switch (cmd) {
288: case CLOCKCTL_OSETTIMEOFDAY: {
289: struct timeval50 tv50;
290: struct timeval tv;
291: struct clockctl50_settimeofday *args = data;
1.1 manu 292:
1.33 pgoyette 293: error = copyin(args->tv, &tv50, sizeof(tv50));
294: if (error)
295: return (error);
296: timeval50_to_timeval(&tv50, &tv);
297: error = settimeofday1(&tv, false, args->tzp, l, false);
298: break;
299: }
300: case CLOCKCTL_OADJTIME: {
301: struct timeval atv, oldatv;
302: struct timeval50 atv50;
303: struct clockctl50_adjtime *args = data;
304:
305: if (args->delta) {
306: error = copyin(args->delta, &atv50, sizeof(atv50));
307: if (error)
308: return (error);
309: timeval50_to_timeval(&atv50, &atv);
310: }
311: adjtime1(args->delta ? &atv : NULL,
312: args->olddelta ? &oldatv : NULL, l->l_proc);
313: if (args->olddelta) {
314: timeval_to_timeval50(&oldatv, &atv50);
315: error = copyout(&atv50, args->olddelta, sizeof(atv50));
316: }
317: break;
318: }
319: case CLOCKCTL_OCLOCK_SETTIME: {
320: struct timespec50 tp50;
321: struct timespec tp;
322: struct clockctl50_clock_settime *args = data;
323:
324: error = copyin(args->tp, &tp50, sizeof(tp50));
325: if (error)
326: return (error);
327: timespec50_to_timespec(&tp50, &tp);
328: error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
329: break;
330: }
1.35 ! rin 331: #ifdef NTP
! 332: case CLOCKCTL_ONTP_ADJTIME: {
1.33 pgoyette 333: /* The ioctl number changed but the data did not change. */
334: error = (cd->d_ioctl)(dev, CLOCKCTL_NTP_ADJTIME,
335: data, flags, l);
336: break;
1.35 ! rin 337: }
! 338: #endif
1.33 pgoyette 339: default:
1.35 ! rin 340: error = ENOTTY;
1.33 pgoyette 341: }
342:
343: return (error);
344: }
345: #endif
CVSweb <webmaster@jp.NetBSD.org>