Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.9
      1 /*
      2  * Copyright (c) 1982, 1986, 1991 Regents of the University of California.
      3  * All rights reserved.
      4  * (c) UNIX System Laboratories, Inc.
      5  * All or some portions of this file are derived from material licensed
      6  * to the University of California by American Telephone and Telegraph
      7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  * the permission of UNIX System Laboratories, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	from: @(#)kern_subr.c	7.7 (Berkeley) 4/15/91
     39  *	$Id: kern_subr.c,v 1.9 1994/05/17 04:22:00 cgd Exp $
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 
     46 int
     47 uiomove(cp, n, uio)
     48 	register caddr_t cp;
     49 	register int n;
     50 	register struct uio *uio;
     51 {
     52 	register struct iovec *iov;
     53 	u_int cnt;
     54 	int error = 0;
     55 
     56 
     57 #ifdef DIAGNOSTIC
     58 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
     59 		panic("uiomove: mode");
     60 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
     61 		panic("uiomove proc");
     62 #endif
     63 	while (n > 0 && uio->uio_resid) {
     64 		iov = uio->uio_iov;
     65 		cnt = iov->iov_len;
     66 		if (cnt == 0) {
     67 			uio->uio_iov++;
     68 			uio->uio_iovcnt--;
     69 			continue;
     70 		}
     71 		if (cnt > n)
     72 			cnt = n;
     73 		switch (uio->uio_segflg) {
     74 
     75 		case UIO_USERSPACE:
     76 		case UIO_USERISPACE:
     77 			if (uio->uio_rw == UIO_READ)
     78 				error = copyout(cp, iov->iov_base, cnt);
     79 			else
     80 				error = copyin(iov->iov_base, cp, cnt);
     81 			if (error)
     82 				return (error);
     83 			break;
     84 
     85 		case UIO_SYSSPACE:
     86 			if (uio->uio_rw == UIO_READ)
     87 				bcopy((caddr_t)cp, iov->iov_base, cnt);
     88 			else
     89 				bcopy(iov->iov_base, (caddr_t)cp, cnt);
     90 			break;
     91 		}
     92 		iov->iov_base += cnt;
     93 		iov->iov_len -= cnt;
     94 		uio->uio_resid -= cnt;
     95 		uio->uio_offset += cnt;
     96 		cp += cnt;
     97 		n -= cnt;
     98 	}
     99 	return (error);
    100 }
    101 
    102 int
    103 uioapply(func, arg1, arg2, uio)
    104 	int (*func)();
    105 	int arg1, arg2;
    106 	register struct uio *uio;
    107 {
    108 	register struct iovec *iov;
    109 	u_int cnt, cnt1;
    110 	int error = 0;
    111 
    112 
    113 /*#ifdef DIAGNOSTIC*/
    114 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    115 		panic("uioapply: mode");
    116 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    117 		panic("uioapply proc");
    118 /*#endif*/
    119 	while (uio->uio_resid) {
    120 		iov = uio->uio_iov;
    121 		cnt = iov->iov_len;
    122 		if (cnt == 0) {
    123 			uio->uio_iov++;
    124 			uio->uio_iovcnt--;
    125 			continue;
    126 		}
    127 		cnt1 = cnt;
    128 		error = (*func)(arg1, arg2, uio->uio_offset, uio->uio_rw,
    129 			iov->iov_base, &cnt1, uio->uio_procp);
    130 		cnt -= cnt1;
    131 		iov->iov_base += cnt;
    132 		iov->iov_len -= cnt;
    133 		uio->uio_resid -= cnt;
    134 		uio->uio_offset += cnt;
    135 		if (error || cnt1)
    136 			return (error);
    137 	}
    138 	return (0);
    139 }
    140 
    141 /*
    142  * Give next character to user as result of read.
    143  */
    144 int
    145 ureadc(c, uio)
    146 	register int c;
    147 	register struct uio *uio;
    148 {
    149 	register struct iovec *iov;
    150 
    151 	if (uio->uio_resid <= 0)
    152 		panic("ureadc: uio_resid == 0");
    153 
    154 again:
    155 	if (uio->uio_iovcnt == 0)
    156 		panic("ureadc: uio_iovcnt == 0");
    157 
    158 	iov = uio->uio_iov;
    159 	if (iov->iov_len <= 0) {
    160 		uio->uio_iovcnt--;
    161 		uio->uio_iov++;
    162 		goto again;
    163 	}
    164 	switch (uio->uio_segflg) {
    165 
    166 	case UIO_USERSPACE:
    167 		if (subyte(iov->iov_base, c) < 0)
    168 			return (EFAULT);
    169 		break;
    170 
    171 	case UIO_SYSSPACE:
    172 		*iov->iov_base = c;
    173 		break;
    174 
    175 	case UIO_USERISPACE:
    176 		if (suibyte(iov->iov_base, c) < 0)
    177 			return (EFAULT);
    178 		break;
    179 	}
    180 	iov->iov_base++;
    181 	iov->iov_len--;
    182 	uio->uio_resid--;
    183 	uio->uio_offset++;
    184 	return (0);
    185 }
    186 
    187 #ifndef lint	/* unused except by ct.c, other oddities XXX */
    188 /*
    189  * Get next character written in by user from uio.
    190  */
    191 int
    192 uwritec(uio)
    193 	struct uio *uio;
    194 {
    195 	register struct iovec *iov;
    196 	register int c;
    197 
    198 	if (uio->uio_resid <= 0)
    199 		return (-1);
    200 again:
    201 	if (uio->uio_iovcnt <= 0)
    202 		panic("uwritec");
    203 	iov = uio->uio_iov;
    204 	if (iov->iov_len == 0) {
    205 		uio->uio_iov++;
    206 		if (--uio->uio_iovcnt == 0)
    207 			return (-1);
    208 		goto again;
    209 	}
    210 	switch (uio->uio_segflg) {
    211 
    212 	case UIO_USERSPACE:
    213 		c = fubyte(iov->iov_base);
    214 		break;
    215 
    216 	case UIO_SYSSPACE:
    217 		c = *(u_char *) iov->iov_base;
    218 		break;
    219 
    220 	case UIO_USERISPACE:
    221 		c = fuibyte(iov->iov_base);
    222 		break;
    223 	}
    224 	if (c < 0)
    225 		return (-1);
    226 	iov->iov_base++;
    227 	iov->iov_len--;
    228 	uio->uio_resid--;
    229 	uio->uio_offset++;
    230 	return (c);
    231 }
    232 #endif /* notdef */
    233