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