xref: /openbsd-src/sys/kern/kern_subr.c (revision 52890ed5eb51a06eaa600e1bc69dd2af52c223c6)
1 /*	$OpenBSD: kern_subr.c,v 1.10 1999/11/07 17:39:14 provos Exp $	*/
2 /*	$NetBSD: kern_subr.c,v 1.15 1996/04/09 17:21:56 ragge Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 #include <sys/queue.h>
49 
50 int
51 uiomove(cp, n, uio)
52 	register caddr_t cp;
53 	register int n;
54 	register struct uio *uio;
55 {
56 	register struct iovec *iov;
57 	u_int cnt;
58 	int error = 0;
59 
60 #ifdef DIAGNOSTIC
61 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
62 		panic("uiomove: mode");
63 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
64 		panic("uiomove proc");
65 #endif
66 	while (n > 0 && uio->uio_resid) {
67 		iov = uio->uio_iov;
68 		cnt = iov->iov_len;
69 		if (cnt == 0) {
70 			uio->uio_iov++;
71 			uio->uio_iovcnt--;
72 			continue;
73 		}
74 		if (cnt > n)
75 			cnt = n;
76 		switch (uio->uio_segflg) {
77 
78 		case UIO_USERSPACE:
79 			if (uio->uio_rw == UIO_READ)
80 				error = copyout(cp, iov->iov_base, cnt);
81 			else
82 				error = copyin(iov->iov_base, cp, cnt);
83 			if (error)
84 				return (error);
85 			break;
86 
87 		case UIO_SYSSPACE:
88 #if defined(UVM)
89 			if (uio->uio_rw == UIO_READ)
90 				error = kcopy(cp, iov->iov_base, cnt);
91 			else
92 				error = kcopy(iov->iov_base, cp, cnt);
93 			if (error)
94 				return(error);
95 #else
96 			if (uio->uio_rw == UIO_READ)
97 				bcopy((caddr_t)cp, iov->iov_base, cnt);
98 			else
99 				bcopy(iov->iov_base, (caddr_t)cp, cnt);
100 			break;
101 #endif
102 		}
103 		iov->iov_base += cnt;
104 		iov->iov_len -= cnt;
105 		uio->uio_resid -= cnt;
106 		uio->uio_offset += cnt;
107 		cp += cnt;
108 		n -= cnt;
109 	}
110 	return (error);
111 }
112 
113 /*
114  * Give next character to user as result of read.
115  */
116 int
117 ureadc(c, uio)
118 	register int c;
119 	register struct uio *uio;
120 {
121 	register struct iovec *iov;
122 
123 	if (uio->uio_resid == 0)
124 #ifdef DIAGNOSTIC
125 		panic("ureadc: zero resid");
126 #else
127 		return (EINVAL);
128 #endif
129 again:
130 	if (uio->uio_iovcnt <= 0)
131 #ifdef DIAGNOSTIC
132 		panic("ureadc: non-positive iovcnt");
133 #else
134 		return (EINVAL);
135 #endif
136 	iov = uio->uio_iov;
137 	if (iov->iov_len <= 0) {
138 		uio->uio_iovcnt--;
139 		uio->uio_iov++;
140 		goto again;
141 	}
142 	switch (uio->uio_segflg) {
143 
144 	case UIO_USERSPACE:
145 		if (subyte(iov->iov_base, c) < 0)
146 			return (EFAULT);
147 		break;
148 
149 	case UIO_SYSSPACE:
150 		*(char *)iov->iov_base = c;
151 		break;
152 	}
153 	iov->iov_base++;
154 	iov->iov_len--;
155 	uio->uio_resid--;
156 	uio->uio_offset++;
157 	return (0);
158 }
159 
160 /*
161  * General routine to allocate a hash table.
162  */
163 void *
164 hashinit(elements, type, flags, hashmask)
165 	int elements, type, flags;
166 	u_long *hashmask;
167 {
168 	long hashsize;
169 	LIST_HEAD(generic, generic) *hashtbl;
170 	int i;
171 
172 	if (elements <= 0)
173 		panic("hashinit: bad cnt");
174 	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
175 		continue;
176 	hashsize >>= 1;
177 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
178 	for (i = 0; i < hashsize; i++)
179 		LIST_INIT(&hashtbl[i]);
180 	*hashmask = hashsize - 1;
181 	return (hashtbl);
182 }
183 
184 /*
185  * "Shutdown hook" types, functions, and variables.
186  */
187 
188 struct shutdownhook_desc {
189 	LIST_ENTRY(shutdownhook_desc) sfd_list;
190 	void	(*sfd_fn) __P((void *));
191 	void	*sfd_arg;
192 };
193 
194 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
195 
196 int shutdownhooks_done;
197 
198 void *
199 shutdownhook_establish(fn, arg)
200 	void (*fn) __P((void *));
201 	void *arg;
202 {
203 	struct shutdownhook_desc *ndp;
204 
205 	ndp = (struct shutdownhook_desc *)
206 	    malloc(sizeof (*ndp), M_DEVBUF, M_NOWAIT);
207 	if (ndp == NULL)
208 		return NULL;
209 
210 	ndp->sfd_fn = fn;
211 	ndp->sfd_arg = arg;
212 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
213 
214 	return (ndp);
215 }
216 
217 void
218 shutdownhook_disestablish(vhook)
219 	void *vhook;
220 {
221 #ifdef DIAGNOSTIC
222 	struct shutdownhook_desc *dp;
223 
224 	for (dp = shutdownhook_list.lh_first; dp != NULL;
225 	    dp = dp->sfd_list.le_next)
226                 if (dp == vhook)
227 			break;
228 	if (dp == NULL)
229 		panic("shutdownhook_disestablish: hook not established");
230 #endif
231 
232 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
233 }
234 
235 /*
236  * Run shutdown hooks.  Should be invoked immediately before the
237  * system is halted or rebooted, i.e. after file systems unmounted,
238  * after crash dump done, etc.
239  */
240 void
241 doshutdownhooks()
242 {
243 	struct shutdownhook_desc *dp;
244 
245 	if (shutdownhooks_done)
246 		return;
247 
248 	for (dp = shutdownhook_list.lh_first; dp != NULL; dp =
249 	    dp->sfd_list.le_next)
250 		(*dp->sfd_fn)(dp->sfd_arg);
251 }
252 
253 /*
254  * "Power hook" types, functions, and variables.
255  */
256 
257 struct powerhook_desc {
258 	LIST_ENTRY(powerhook_desc) sfd_list;
259 	void	(*sfd_fn) __P((int, void *));
260 	void	*sfd_arg;
261 };
262 
263 LIST_HEAD(, powerhook_desc) powerhook_list;
264 
265 void *
266 powerhook_establish(fn, arg)
267 	void (*fn) __P((int, void *));
268 	void *arg;
269 {
270 	struct powerhook_desc *ndp;
271 
272 	ndp = (struct powerhook_desc *)
273 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
274 	if (ndp == NULL)
275 		return NULL;
276 
277 	ndp->sfd_fn = fn;
278 	ndp->sfd_arg = arg;
279 	LIST_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
280 
281 	return (ndp);
282 }
283 
284 void
285 powerhook_disestablish(vhook)
286 	void *vhook;
287 {
288 #ifdef DIAGNOSTIC
289 	struct powerhook_desc *dp;
290 
291 	for (dp = powerhook_list.lh_first; dp != NULL;
292 	    dp = dp->sfd_list.le_next)
293                 if (dp == vhook)
294 			break;
295 	if (dp == NULL)
296 		panic("powerhook_disestablish: hook not established");
297 #endif
298 
299 	LIST_REMOVE((struct powerhook_desc *)vhook, sfd_list);
300 	free(vhook, M_DEVBUF);
301 }
302 
303 /*
304  * Run power hooks.
305  */
306 void
307 dopowerhooks(why)
308 	int why;
309 {
310 	struct powerhook_desc *dp;
311 
312 	for (dp = LIST_FIRST(&powerhook_list);
313 	     dp != NULL;
314 	     dp = LIST_NEXT(dp, sfd_list)) {
315 		(*dp->sfd_fn)(why, dp->sfd_arg);
316 	}
317 }
318