xref: /openbsd-src/sys/kern/kern_subr.c (revision 1304992892c84c24180772c36a7a82fdc83bdc11)
1 /*	$OpenBSD: kern_subr.c,v 1.7 1999/02/26 03:16:47 millert 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 (uio->uio_rw == UIO_READ)
89 				bcopy((caddr_t)cp, iov->iov_base, cnt);
90 			else
91 				bcopy(iov->iov_base, (caddr_t)cp, cnt);
92 			break;
93 		}
94 		iov->iov_base += cnt;
95 		iov->iov_len -= cnt;
96 		uio->uio_resid -= cnt;
97 		uio->uio_offset += cnt;
98 		cp += cnt;
99 		n -= cnt;
100 	}
101 	return (error);
102 }
103 
104 /*
105  * Give next character to user as result of read.
106  */
107 int
108 ureadc(c, uio)
109 	register int c;
110 	register struct uio *uio;
111 {
112 	register struct iovec *iov;
113 
114 	if (uio->uio_resid == 0)
115 #ifdef DIAGNOSTIC
116 		panic("ureadc: zero resid");
117 #else
118 		return (EINVAL);
119 #endif
120 again:
121 	if (uio->uio_iovcnt <= 0)
122 #ifdef DIAGNOSTIC
123 		panic("ureadc: non-positive iovcnt");
124 #else
125 		return (EINVAL);
126 #endif
127 	iov = uio->uio_iov;
128 	if (iov->iov_len <= 0) {
129 		uio->uio_iovcnt--;
130 		uio->uio_iov++;
131 		goto again;
132 	}
133 	switch (uio->uio_segflg) {
134 
135 	case UIO_USERSPACE:
136 		if (subyte(iov->iov_base, c) < 0)
137 			return (EFAULT);
138 		break;
139 
140 	case UIO_SYSSPACE:
141 		*(char *)iov->iov_base = c;
142 		break;
143 	}
144 	iov->iov_base++;
145 	iov->iov_len--;
146 	uio->uio_resid--;
147 	uio->uio_offset++;
148 	return (0);
149 }
150 
151 /*
152  * General routine to allocate a hash table.
153  */
154 void *
155 hashinit(elements, type, hashmask)
156 	int elements, type;
157 	u_long *hashmask;
158 {
159 	return newhashinit(elements, type, M_WAITOK, hashmask);
160 }
161 
162 void *
163 newhashinit(elements, type, flags, hashmask)
164 	int elements, type, flags;
165 	u_long *hashmask;
166 {
167 	long hashsize;
168 	LIST_HEAD(generic, generic) *hashtbl;
169 	int i;
170 
171 	if (elements <= 0)
172 		panic("hashinit: bad cnt");
173 	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
174 		continue;
175 	hashsize >>= 1;
176 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
177 	for (i = 0; i < hashsize; i++)
178 		LIST_INIT(&hashtbl[i]);
179 	*hashmask = hashsize - 1;
180 	return (hashtbl);
181 }
182 
183 /*
184  * "Shutdown hook" types, functions, and variables.
185  */
186 
187 struct shutdownhook_desc {
188 	LIST_ENTRY(shutdownhook_desc) sfd_list;
189 	void	(*sfd_fn) __P((void *));
190 	void	*sfd_arg;
191 };
192 
193 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
194 
195 int shutdownhooks_done;
196 
197 void *
198 shutdownhook_establish(fn, arg)
199 	void (*fn) __P((void *));
200 	void *arg;
201 {
202 	struct shutdownhook_desc *ndp;
203 
204 	ndp = (struct shutdownhook_desc *)
205 	    malloc(sizeof (*ndp), M_DEVBUF, M_NOWAIT);
206 	if (ndp == NULL)
207 		return NULL;
208 
209 	ndp->sfd_fn = fn;
210 	ndp->sfd_arg = arg;
211 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
212 
213 	return (ndp);
214 }
215 
216 void
217 shutdownhook_disestablish(vhook)
218 	void *vhook;
219 {
220 #ifdef DIAGNOSTIC
221 	struct shutdownhook_desc *dp;
222 
223 	for (dp = shutdownhook_list.lh_first; dp != NULL;
224 	    dp = dp->sfd_list.le_next)
225                 if (dp == vhook)
226 			break;
227 	if (dp == NULL)
228 		panic("shutdownhook_disestablish: hook not established");
229 #endif
230 
231 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
232 }
233 
234 /*
235  * Run shutdown hooks.  Should be invoked immediately before the
236  * system is halted or rebooted, i.e. after file systems unmounted,
237  * after crash dump done, etc.
238  */
239 void
240 doshutdownhooks()
241 {
242 	struct shutdownhook_desc *dp;
243 
244 	if (shutdownhooks_done)
245 		return;
246 
247 	for (dp = shutdownhook_list.lh_first; dp != NULL; dp =
248 	    dp->sfd_list.le_next)
249 		(*dp->sfd_fn)(dp->sfd_arg);
250 }
251