xref: /netbsd-src/sys/compat/common/clockctl_50.c (revision 8a031a1d1e0aac23d43015c71ae6350b917c8347)
1 /*      $NetBSD: clockctl_50.c,v 1.4 2019/12/12 02:15:42 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_50.c,v 1.4 2019/12/12 02:15:42 pgoyette Exp $");
35 
36 #if defined(_KERNEL_OPT)
37 #include "opt_compat_netbsd.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/errno.h>
44 #include <sys/ioctl.h>
45 #include <sys/device.h>
46 #include <sys/time.h>
47 #include <sys/conf.h>
48 #include <sys/timex.h>
49 #include <sys/kauth.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/compat_stub.h>
53 
54 #include <sys/clockctl.h>
55 #include <compat/sys/clockctl.h>
56 #include <compat/sys/time_types.h>
57 
58 int
compat50_clockctlioctl(dev_t dev,u_long cmd,void * data,int flags,struct lwp * l)59 compat50_clockctlioctl(dev_t dev, u_long cmd, void *data, int flags,
60     struct lwp *l)
61 {
62 	int error = 0;
63 	const struct cdevsw *cd = cdevsw_lookup(dev);
64 
65 	if (cd == NULL || cd->d_ioctl == NULL)
66 		return ENXIO;
67 
68 	switch (cmd) {
69 	case CLOCKCTL_OSETTIMEOFDAY: {
70 		struct timeval50 tv50;
71 		struct timeval tv;
72 		struct clockctl50_settimeofday *args = data;
73 
74 		error = copyin(args->tv, &tv50, sizeof(tv50));
75 		if (error)
76 			return (error);
77 		timeval50_to_timeval(&tv50, &tv);
78 		error = settimeofday1(&tv, false, args->tzp, l, false);
79 		break;
80 	}
81 	case CLOCKCTL_OADJTIME: {
82 		struct timeval atv, oldatv;
83 		struct timeval50 atv50;
84 		struct clockctl50_adjtime *args = data;
85 
86 		if (args->delta) {
87 			error = copyin(args->delta, &atv50, sizeof(atv50));
88 			if (error)
89 				return (error);
90 			timeval50_to_timeval(&atv50, &atv);
91 		}
92 		adjtime1(args->delta ? &atv : NULL,
93 		    args->olddelta ? &oldatv : NULL, l->l_proc);
94 		if (args->olddelta) {
95 			timeval_to_timeval50(&oldatv, &atv50);
96 			error = copyout(&atv50, args->olddelta, sizeof(atv50));
97 		}
98 		break;
99 	}
100 	case CLOCKCTL_OCLOCK_SETTIME: {
101 		struct timespec50 tp50;
102 		struct timespec tp;
103 		struct clockctl50_clock_settime *args = data;
104 
105 		error = copyin(args->tp, &tp50, sizeof(tp50));
106 		if (error)
107 			return (error);
108 		timespec50_to_timespec(&tp50, &tp);
109 		error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
110 		break;
111 	}
112 	case CLOCKCTL_ONTP_ADJTIME: {
113 		if (vec_ntp_timestatus == NULL) {
114 			error = ENOTTY;
115 			break;
116 		}
117 		/* The ioctl number changed but the data did not change. */
118 		error = (cd->d_ioctl)(dev, CLOCKCTL_NTP_ADJTIME,
119 		    data, flags, l);
120 		break;
121 	}
122 	default:
123 		error = ENOTTY;
124 	}
125 
126 	return (error);
127 }
128 
129 void
clockctl_50_init(void)130 clockctl_50_init(void)
131 {
132 
133 	MODULE_HOOK_SET(clockctl_ioctl_50_hook, compat50_clockctlioctl);
134 }
135 
136 void
clockctl_50_fini(void)137 clockctl_50_fini(void)
138 {
139 
140 	MODULE_HOOK_UNSET(clockctl_ioctl_50_hook);
141 }
142