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