clockctl.c revision 1.31 1 /* $NetBSD: clockctl.c,v 1.31 2014/03/16 05:20:26 dholland 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.31 2014/03/16 05:20:26 dholland Exp $");
35
36 #include "opt_ntp.h"
37 #include "opt_compat_netbsd.h"
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>
46 #include <sys/conf.h>
47 #ifdef NTP
48 #include <sys/timex.h>
49 #endif /* NTP */
50 #include <sys/kauth.h>
51
52 #include <sys/clockctl.h>
53 #ifdef COMPAT_50
54 #include <compat/sys/clockctl.h>
55 #endif
56
57 dev_type_ioctl(clockctlioctl);
58
59 const struct cdevsw clockctl_cdevsw = {
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,
70 .d_flag = D_OTHER,
71 };
72
73 static kauth_listener_t clockctl_listener;
74
75 static int
76 clockctl_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
77 void *arg0, void *arg1, void *arg2, void *arg3)
78 {
79 int result;
80 enum kauth_system_req req;
81 bool device_context;
82
83 result = KAUTH_RESULT_DEFER;
84 req = (enum kauth_system_req)arg0;
85
86 if ((action != KAUTH_SYSTEM_TIME) ||
87 (req != KAUTH_REQ_SYSTEM_TIME_SYSTEM))
88 return result;
89
90 device_context = (bool)arg3;
91
92 /* Device is controlled by permissions, so allow. */
93 if (device_context)
94 result = KAUTH_RESULT_ALLOW;
95
96 return result;
97 }
98
99 /*ARGSUSED*/
100 void
101 clockctlattach(int num)
102 {
103
104 clockctl_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
105 clockctl_listener_cb, NULL);
106 }
107
108 int
109 clockctlioctl(
110 dev_t dev,
111 u_long cmd,
112 void *data,
113 int flags,
114 struct lwp *l)
115 {
116 int error = 0;
117
118 switch (cmd) {
119 case CLOCKCTL_SETTIMEOFDAY: {
120 struct clockctl_settimeofday *args = data;
121
122 error = settimeofday1(args->tv, true, args->tzp, l, false);
123 break;
124 }
125 case CLOCKCTL_ADJTIME: {
126 struct timeval atv, oldatv;
127 struct clockctl_adjtime *args = data;
128
129 if (args->delta) {
130 error = copyin(args->delta, &atv, sizeof(atv));
131 if (error)
132 return (error);
133 }
134 adjtime1(args->delta ? &atv : NULL,
135 args->olddelta ? &oldatv : NULL, l->l_proc);
136 if (args->olddelta)
137 error = copyout(&oldatv, args->olddelta,
138 sizeof(oldatv));
139 break;
140 }
141 case CLOCKCTL_CLOCK_SETTIME: {
142 struct clockctl_clock_settime *args = data;
143 struct timespec ts;
144
145 error = copyin(args->tp, &ts, sizeof ts);
146 if (error)
147 return (error);
148 error = clock_settime1(l->l_proc, args->clock_id, &ts, false);
149 break;
150 }
151 #ifdef NTP
152 case CLOCKCTL_NTP_ADJTIME: {
153 struct clockctl_ntp_adjtime *args = data;
154 struct timex ntv;
155
156 error = copyin(args->tp, &ntv, sizeof(ntv));
157 if (error)
158 return (error);
159
160 ntp_adjtime1(&ntv);
161
162 error = copyout(&ntv, args->tp, sizeof(ntv));
163 if (error == 0)
164 args->retval = ntp_timestatus();
165 break;
166 }
167 #endif /* NTP */
168 default:
169 #ifdef COMPAT_50
170 error = compat50_clockctlioctl(dev, cmd, data, flags, l);
171 #else
172 error = EINVAL;
173 #endif
174 }
175
176 return (error);
177 }
178
179
180