Annotation of src/sys/dev/clockctl.c, revision 1.32

1.32    ! dholland    1: /*      $NetBSD: clockctl.c,v 1.31 2014/03/16 05:20:26 dholland 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.32    ! dholland   34: __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.31 2014/03/16 05:20:26 dholland Exp $");
1.5       manu       35: 
                     36: #include "opt_ntp.h"
1.24      christos   37: #include "opt_compat_netbsd.h"
1.1       manu       38: 
                     39: #include <sys/param.h>
                     40: #include <sys/systm.h>
                     41: #include <sys/proc.h>
                     42: #include <sys/errno.h>
                     43: #include <sys/ioctl.h>
                     44: #include <sys/device.h>
                     45: #include <sys/time.h>
1.7       gehenna    46: #include <sys/conf.h>
1.1       manu       47: #ifdef NTP
                     48: #include <sys/timex.h>
                     49: #endif /* NTP */
1.28      elad       50: #include <sys/kauth.h>
1.1       manu       51: 
                     52: #include <sys/clockctl.h>
1.24      christos   53: #ifdef COMPAT_50
                     54: #include <compat/sys/clockctl.h>
                     55: #endif
1.1       manu       56: 
1.7       gehenna    57: dev_type_ioctl(clockctlioctl);
                     58: 
                     59: const struct cdevsw clockctl_cdevsw = {
1.31      dholland   60:        .d_open = nullopen,
                     61:        .d_close = nullclose,
                     62:        .d_read = noread,
                     63:        .d_write = nowrite,
                     64:        .d_ioctl = clockctlioctl,
                     65:        .d_stop = nostop,
                     66:        .d_tty = notty,
                     67:        .d_poll = nopoll,
                     68:        .d_mmap = nommap,
                     69:        .d_kqfilter = nokqfilter,
1.32    ! dholland   70:        .d_discard = nodiscard,
1.31      dholland   71:        .d_flag = D_OTHER,
1.7       gehenna    72: };
1.1       manu       73: 
1.28      elad       74: static kauth_listener_t clockctl_listener;
                     75: 
                     76: static int
                     77: clockctl_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
                     78:     void *arg0, void *arg1, void *arg2, void *arg3)
                     79: {
                     80:        int result;
                     81:        enum kauth_system_req req;
                     82:        bool device_context;
                     83: 
                     84:        result = KAUTH_RESULT_DEFER;
                     85:        req = (enum kauth_system_req)arg0;
                     86: 
                     87:        if ((action != KAUTH_SYSTEM_TIME) ||
                     88:            (req != KAUTH_REQ_SYSTEM_TIME_SYSTEM))
                     89:                return result;
                     90: 
                     91:        device_context = (bool)arg3;
                     92: 
                     93:        /* Device is controlled by permissions, so allow. */
                     94:        if (device_context)
                     95:                result = KAUTH_RESULT_ALLOW;
                     96: 
                     97:        return result;
                     98: }
                     99: 
1.10      perry     100: /*ARGSUSED*/
1.1       manu      101: void
1.20      christos  102: clockctlattach(int num)
1.1       manu      103: {
1.28      elad      104: 
                    105:        clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
                    106:            clockctl_listener_cb, NULL);
1.1       manu      107: }
                    108: 
                    109: int
1.19      christos  110: clockctlioctl(
1.20      christos  111:     dev_t dev,
1.19      christos  112:     u_long cmd,
1.21      christos  113:     void *data,
1.20      christos  114:     int flags,
1.19      christos  115:     struct lwp *l)
1.1       manu      116: {
                    117:        int error = 0;
1.13      perry     118: 
1.1       manu      119:        switch (cmd) {
1.24      christos  120:        case CLOCKCTL_SETTIMEOFDAY: {
                    121:                struct clockctl_settimeofday *args = data;
1.1       manu      122: 
1.24      christos  123:                error = settimeofday1(args->tv, true, args->tzp, l, false);
                    124:                break;
                    125:        }
                    126:        case CLOCKCTL_ADJTIME: {
                    127:                struct timeval atv, oldatv;
                    128:                struct clockctl_adjtime *args = data;
1.1       manu      129: 
1.24      christos  130:                if (args->delta) {
1.27      nakayama  131:                        error = copyin(args->delta, &atv, sizeof(atv));
1.1       manu      132:                        if (error)
1.13      perry     133:                                return (error);
1.1       manu      134:                }
1.24      christos  135:                adjtime1(args->delta ? &atv : NULL,
                    136:                    args->olddelta ? &oldatv : NULL, l->l_proc);
                    137:                if (args->olddelta)
                    138:                        error = copyout(&oldatv, args->olddelta,
1.27      nakayama  139:                            sizeof(oldatv));
1.24      christos  140:                break;
                    141:        }
                    142:        case CLOCKCTL_CLOCK_SETTIME: {
                    143:                struct clockctl_clock_settime *args = data;
1.25      mrg       144:                struct timespec ts;
1.1       manu      145: 
1.26      mrg       146:                error = copyin(args->tp, &ts, sizeof ts);
                    147:                if (error)
                    148:                        return (error);
                    149:                error = clock_settime1(l->l_proc, args->clock_id, &ts, false);
1.24      christos  150:                break;
                    151:        }
1.1       manu      152: #ifdef NTP
1.24      christos  153:        case CLOCKCTL_NTP_ADJTIME: {
                    154:                struct clockctl_ntp_adjtime *args = data;
                    155:                struct timex ntv;
1.1       manu      156: 
1.24      christos  157:                error = copyin(args->tp, &ntv, sizeof(ntv));
                    158:                if (error)
                    159:                        return (error);
1.6       manu      160: 
1.24      christos  161:                ntp_adjtime1(&ntv);
1.17      kardel    162: 
1.24      christos  163:                error = copyout(&ntv, args->tp, sizeof(ntv));
                    164:                if (error == 0)
1.29      apb       165:                        args->retval = ntp_timestatus();
1.27      nakayama  166:                break;
1.24      christos  167:        }
1.1       manu      168: #endif /* NTP */
1.24      christos  169:        default:
                    170: #ifdef COMPAT_50
                    171:                error = compat50_clockctlioctl(dev, cmd, data, flags, l);
                    172: #else
                    173:                error = EINVAL;
                    174: #endif
1.1       manu      175:        }
                    176: 
                    177:        return (error);
                    178: }
                    179: 
                    180: 

CVSweb <webmaster@jp.NetBSD.org>