Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_43.c revision 1.5
      1 /*	$NetBSD: uipc_syscalls_43.c,v 1.5 1996/03/14 19:31:50 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/filedesc.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/file.h>
     44 #include <sys/socket.h>
     45 #include <sys/socketvar.h>
     46 #include <sys/stat.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/fcntl.h>
     49 #include <sys/malloc.h>
     50 #include <sys/syslog.h>
     51 #include <sys/unistd.h>
     52 #include <sys/resourcevar.h>
     53 
     54 #include <sys/mount.h>
     55 #include <sys/syscallargs.h>
     56 
     57 int
     58 compat_43_sys_accept(p, v, retval)
     59 	struct proc *p;
     60 	void *v;
     61 	register_t *retval;
     62 {
     63 	struct sys_accept_args /* {
     64 		syscallarg(int) s;
     65 		syscallarg(caddr_t) name;
     66 		syscallarg(int *) anamelen;
     67 	} */ *uap = v;
     68 	int error;
     69 
     70 	if ((error = sys_accept(p, uap, retval)) != 0)
     71 		return error;
     72 
     73 	if (SCARG(uap, name)) {
     74 		struct sockaddr sa;
     75 
     76 		if ((error = copyin(SCARG(uap, name), &sa, sizeof(sa))) != 0)
     77 			return error;
     78 
     79 		((struct osockaddr*) &sa)->sa_family = sa.sa_family;
     80 
     81 		if ((error = copyout(&sa, SCARG(uap, name), sizeof(sa))) != 0)
     82 			return error;
     83 	}
     84 	return 0;
     85 }
     86 
     87 
     88 int
     89 compat_43_sys_getpeername(p, v, retval)
     90 	struct proc *p;
     91 	void *v;
     92 	register_t *retval;
     93 {
     94 	struct sys_getpeername_args /* {
     95 		syscallarg(int) fdes;
     96 		syscallarg(caddr_t) asa;
     97 		syscallarg(int *) alen;
     98 	} */ *uap = v;
     99 	struct sockaddr sa;
    100 
    101 	int error;
    102 
    103 	if ((error = sys_getpeername(p, uap, retval)) != 0)
    104 		return error;
    105 
    106 	if ((error = copyin(SCARG(uap, asa), &sa, sizeof(sa))) != 0)
    107 		return error;
    108 
    109 	((struct osockaddr*) &sa)->sa_family = sa.sa_family;
    110 
    111 	if ((error = copyout(&sa, SCARG(uap, asa), sizeof(sa))) != 0)
    112 		return error;
    113 
    114 	return 0;
    115 }
    116 
    117 
    118 int
    119 compat_43_sys_getsockname(p, v, retval)
    120 	struct proc *p;
    121 	void *v;
    122 	register_t *retval;
    123 {
    124 	struct sys_getsockname_args /* {
    125 		syscallarg(int) fdes;
    126 		syscallarg(caddr_t) asa;
    127 		syscallarg(int *) alen;
    128 	} */ *uap = v;
    129 	struct sockaddr sa;
    130 	int error;
    131 
    132 	if ((error = sys_getsockname(p, uap, retval)) != 0)
    133 		return error;
    134 
    135 	if ((error = copyin(SCARG(uap, asa), &sa, sizeof(sa))) != 0)
    136 		return error;
    137 
    138 	((struct osockaddr*) &sa)->sa_family = sa.sa_family;
    139 
    140 	if ((error = copyout(&sa, SCARG(uap, asa), sizeof(sa))) != 0)
    141 		return error;
    142 
    143 	return 0;
    144 }
    145 
    146 
    147 int
    148 compat_43_sys_recv(p, v, retval)
    149 	struct proc *p;
    150 	void *v;
    151 	register_t *retval;
    152 {
    153 	register struct compat_43_sys_recv_args /* {
    154 		syscallarg(int) s;
    155 		syscallarg(caddr_t) buf;
    156 		syscallarg(int) len;
    157 		syscallarg(int) flags;
    158 	} */ *uap = v;
    159 	struct msghdr msg;
    160 	struct iovec aiov;
    161 
    162 	msg.msg_name = 0;
    163 	msg.msg_namelen = 0;
    164 	msg.msg_iov = &aiov;
    165 	msg.msg_iovlen = 1;
    166 	aiov.iov_base = SCARG(uap, buf);
    167 	aiov.iov_len = SCARG(uap, len);
    168 	msg.msg_control = 0;
    169 	msg.msg_flags = SCARG(uap, flags);
    170 	return (recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval));
    171 }
    172 
    173 
    174 #ifdef MSG_COMPAT
    175 int
    176 compat_43_sys_recvfrom(p, v, retval)
    177 	struct proc *p;
    178 	void *v;
    179 	register_t *retval;
    180 {
    181 	struct sys_recvfrom_args /* {
    182 		syscallarg(int) s;
    183 		syscallarg(caddr_t) buf;
    184 		syscallarg(size_t) len;
    185 		syscallarg(int) flags;
    186 		syscallarg(caddr_t) from;
    187 		syscallarg(int *) fromlenaddr;
    188 	} */ *uap = v;
    189 
    190 	SCARG(uap, flags) |= MSG_COMPAT;
    191 	return (sys_recvfrom(p, uap, retval));
    192 }
    193 #endif
    194 
    195 
    196 #ifdef MSG_COMPAT
    197 /*
    198  * Old recvmsg.  This code takes advantage of the fact that the old msghdr
    199  * overlays the new one, missing only the flags, and with the (old) access
    200  * rights where the control fields are now.
    201  */
    202 int
    203 compat_43_sys_recvmsg(p, v, retval)
    204 	struct proc *p;
    205 	void *v;
    206 	register_t *retval;
    207 {
    208 	register struct compat_43_sys_recvmsg_args /* {
    209 		syscallarg(int) s;
    210 		syscallarg(struct omsghdr *) msg;
    211 		syscallarg(int) flags;
    212 	} */ *uap = v;
    213 	struct msghdr msg;
    214 	struct iovec aiov[UIO_SMALLIOV], *iov;
    215 	int error;
    216 
    217 	error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
    218 	    sizeof (struct omsghdr));
    219 	if (error)
    220 		return (error);
    221 	if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
    222 		if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
    223 			return (EMSGSIZE);
    224 		MALLOC(iov, struct iovec *,
    225 		      sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
    226 		      M_WAITOK);
    227 	} else
    228 		iov = aiov;
    229 	msg.msg_flags = SCARG(uap, flags) | MSG_COMPAT;
    230 	error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    231 	    (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
    232 	if (error)
    233 		goto done;
    234 	msg.msg_iov = iov;
    235 	error = recvit(p, SCARG(uap, s), &msg,
    236 	    (caddr_t)&SCARG(uap, msg)->msg_namelen, retval);
    237 
    238 	if (msg.msg_controllen && error == 0)
    239 		error = copyout((caddr_t)&msg.msg_controllen,
    240 		    (caddr_t)&SCARG(uap, msg)->msg_accrightslen, sizeof (int));
    241 done:
    242 	if (iov != aiov)
    243 		FREE(iov, M_IOV);
    244 	return (error);
    245 }
    246 #endif
    247 
    248 int
    249 compat_43_sys_send(p, v, retval)
    250 	struct proc *p;
    251 	void *v;
    252 	register_t *retval;
    253 {
    254 	register struct compat_43_sys_send_args /* {
    255 		syscallarg(int) s;
    256 		syscallarg(caddr_t) buf;
    257 		syscallarg(int) len;
    258 		syscallarg(int) flags;
    259 	} */ *uap = v;
    260 	struct msghdr msg;
    261 	struct iovec aiov;
    262 
    263 	msg.msg_name = 0;
    264 	msg.msg_namelen = 0;
    265 	msg.msg_iov = &aiov;
    266 	msg.msg_iovlen = 1;
    267 	aiov.iov_base = SCARG(uap, buf);
    268 	aiov.iov_len = SCARG(uap, len);
    269 	msg.msg_control = 0;
    270 	msg.msg_flags = 0;
    271 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
    272 }
    273 
    274 #ifdef MSG_COMPAT
    275 int
    276 compat_43_sys_sendmsg(p, v, retval)
    277 	struct proc *p;
    278 	void *v;
    279 	register_t *retval;
    280 {
    281 	register struct compat_43_sys_sendmsg_args /* {
    282 		syscallarg(int) s;
    283 		syscallarg(caddr_t) msg;
    284 		syscallarg(int) flags;
    285 	} */ *uap = v;
    286 	struct msghdr msg;
    287 	struct iovec aiov[UIO_SMALLIOV], *iov;
    288 	int error;
    289 
    290 	error = copyin(SCARG(uap, msg), (caddr_t)&msg,
    291 	    sizeof (struct omsghdr));
    292 	if (error)
    293 		return (error);
    294 	if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
    295 		if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
    296 			return (EMSGSIZE);
    297 		MALLOC(iov, struct iovec *,
    298 		      sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
    299 		      M_WAITOK);
    300 	} else
    301 		iov = aiov;
    302 	error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    303 	    (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
    304 	if (error)
    305 		goto done;
    306 	msg.msg_flags = MSG_COMPAT;
    307 	msg.msg_iov = iov;
    308 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    309 done:
    310 	if (iov != aiov)
    311 		FREE(iov, M_IOV);
    312 	return (error);
    313 }
    314 #endif
    315