Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.17
      1 /*	$NetBSD: kern_subr.c,v 1.17 1996/11/24 00:42:31 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(buf, n, uio)
     51 	register void *buf;
     52 	register int n;
     53 	register struct uio *uio;
     54 {
     55 	register struct iovec *iov;
     56 	u_int cnt;
     57 	int error = 0;
     58 	char *cp = buf;
     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(cp, iov->iov_base, cnt);
     90 			else
     91 				bcopy(iov->iov_base, 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 		panic("ureadc: non-positive resid");
    116 again:
    117 	if (uio->uio_iovcnt <= 0)
    118 		panic("ureadc: non-positive iovcnt");
    119 	iov = uio->uio_iov;
    120 	if (iov->iov_len <= 0) {
    121 		uio->uio_iovcnt--;
    122 		uio->uio_iov++;
    123 		goto again;
    124 	}
    125 	switch (uio->uio_segflg) {
    126 
    127 	case UIO_USERSPACE:
    128 		if (subyte(iov->iov_base, c) < 0)
    129 			return (EFAULT);
    130 		break;
    131 
    132 	case UIO_SYSSPACE:
    133 		*iov->iov_base = c;
    134 		break;
    135 	}
    136 	iov->iov_base++;
    137 	iov->iov_len--;
    138 	uio->uio_resid--;
    139 	uio->uio_offset++;
    140 	return (0);
    141 }
    142 
    143 /*
    144  * General routine to allocate a hash table.
    145  */
    146 void *
    147 hashinit(elements, type, hashmask)
    148 	int elements, type;
    149 	u_long *hashmask;
    150 {
    151 	long hashsize;
    152 	LIST_HEAD(generic, generic) *hashtbl;
    153 	int i;
    154 
    155 	if (elements <= 0)
    156 		panic("hashinit: bad cnt");
    157 	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
    158 		continue;
    159 	hashsize >>= 1;
    160 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
    161 	for (i = 0; i < hashsize; i++)
    162 		LIST_INIT(&hashtbl[i]);
    163 	*hashmask = hashsize - 1;
    164 	return (hashtbl);
    165 }
    166 
    167 /*
    168  * "Shutdown hook" types, functions, and variables.
    169  */
    170 
    171 struct shutdownhook_desc {
    172 	LIST_ENTRY(shutdownhook_desc) sfd_list;
    173 	void	(*sfd_fn) __P((void *));
    174 	void	*sfd_arg;
    175 };
    176 
    177 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
    178 
    179 void *
    180 shutdownhook_establish(fn, arg)
    181 	void (*fn) __P((void *));
    182 	void *arg;
    183 {
    184 	struct shutdownhook_desc *ndp;
    185 
    186 	ndp = (struct shutdownhook_desc *)
    187 	    malloc(sizeof (*ndp), M_DEVBUF, M_NOWAIT);
    188 	if (ndp == NULL)
    189 		return NULL;
    190 
    191 	ndp->sfd_fn = fn;
    192 	ndp->sfd_arg = arg;
    193 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
    194 
    195 	return (ndp);
    196 }
    197 
    198 void
    199 shutdownhook_disestablish(vhook)
    200 	void *vhook;
    201 {
    202 #ifdef DIAGNOSTIC
    203 	struct shutdownhook_desc *dp;
    204 
    205 	for (dp = shutdownhook_list.lh_first; dp != NULL;
    206 	    dp = dp->sfd_list.le_next)
    207                 if (dp == vhook)
    208 			break;
    209 	if (dp == NULL)
    210 		panic("shutdownhook_disestablish: hook not established");
    211 #endif
    212 
    213 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
    214 	free(vhook, M_DEVBUF);
    215 }
    216 
    217 /*
    218  * Run shutdown hooks.  Should be invoked immediately before the
    219  * system is halted or rebooted, i.e. after file systems unmounted,
    220  * after crash dump done, etc.
    221  *
    222  * Each shutdown hook is removed from the list before it's run, so that
    223  * it won't be run again.
    224  */
    225 void
    226 doshutdownhooks()
    227 {
    228 	struct shutdownhook_desc *dp;
    229 
    230 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    231 		LIST_REMOVE(dp, sfd_list);
    232 		(*dp->sfd_fn)(dp->sfd_arg);
    233 #if 0
    234 		/*
    235 		 * Don't bother freeing the hook structure,, since we may
    236 		 * be rebooting because of a memory corruption problem,
    237 		 * and this might only make things worse.  It doesn't
    238 		 * matter, anyway, since the system is just about to
    239 		 * reboot.
    240 		 */
    241 		free(dp, M_DEVBUF);
    242 #endif
    243 	}
    244 }
    245