| 1 | /* $NetBSD: clockctl.c,v 1.35 2016/11/21 03:53:59 rin 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 2016/11/21 03:53:59 rin 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(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; |
| 292 | |
| 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 | } |
| 331 | #ifdef NTP |
| 332 | case CLOCKCTL_ONTP_ADJTIME: { |
| 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; |
| 337 | } |
| 338 | #endif |
| 339 | default: |
| 340 | error = ENOTTY; |
| 341 | } |
| 342 | |
| 343 | return (error); |
| 344 | } |
| 345 | #endif |
| 346 | |