1 /* $OpenBSD: sys_socket.c,v 1.14 2009/02/22 07:47:22 otto Exp $ */ 2 /* $NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/file.h> 38 #include <sys/proc.h> 39 #include <sys/mbuf.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/ioctl.h> 44 #include <sys/poll.h> 45 #include <sys/stat.h> 46 47 #include <net/if.h> 48 #include <net/route.h> 49 50 struct fileops socketops = { 51 soo_read, soo_write, soo_ioctl, soo_poll, soo_kqfilter, 52 soo_stat, soo_close 53 }; 54 55 /* ARGSUSED */ 56 int 57 soo_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred) 58 { 59 60 return (soreceive((struct socket *)fp->f_data, (struct mbuf **)0, 61 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0, 62 (socklen_t)0)); 63 } 64 65 /* ARGSUSED */ 66 int 67 soo_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred) 68 { 69 70 return (sosend((struct socket *)fp->f_data, (struct mbuf *)0, 71 uio, (struct mbuf *)0, (struct mbuf *)0, 0)); 72 } 73 74 int 75 soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p) 76 { 77 struct socket *so = (struct socket *)fp->f_data; 78 79 switch (cmd) { 80 81 case FIONBIO: 82 if (*(int *)data) 83 so->so_state |= SS_NBIO; 84 else 85 so->so_state &= ~SS_NBIO; 86 return (0); 87 88 case FIOASYNC: 89 if (*(int *)data) { 90 so->so_state |= SS_ASYNC; 91 so->so_rcv.sb_flags |= SB_ASYNC; 92 so->so_snd.sb_flags |= SB_ASYNC; 93 } else { 94 so->so_state &= ~SS_ASYNC; 95 so->so_rcv.sb_flags &= ~SB_ASYNC; 96 so->so_snd.sb_flags &= ~SB_ASYNC; 97 } 98 return (0); 99 100 case FIONREAD: 101 *(int *)data = so->so_rcv.sb_datacc; 102 return (0); 103 104 case SIOCSPGRP: 105 so->so_pgid = *(int *)data; 106 so->so_siguid = p->p_cred->p_ruid; 107 so->so_sigeuid = p->p_ucred->cr_uid; 108 return (0); 109 110 case SIOCGPGRP: 111 *(int *)data = so->so_pgid; 112 return (0); 113 114 case SIOCATMARK: 115 *(int *)data = (so->so_state&SS_RCVATMARK) != 0; 116 return (0); 117 } 118 /* 119 * Interface/routing/protocol specific ioctls: 120 * interface and routing ioctls should have a 121 * different entry since a socket's unnecessary 122 */ 123 if (IOCGROUP(cmd) == 'i') 124 return (ifioctl(so, cmd, data, p)); 125 if (IOCGROUP(cmd) == 'r') 126 return (rtioctl(cmd, data, p)); 127 return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 128 (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0, p)); 129 } 130 131 int 132 soo_poll(struct file *fp, int events, struct proc *p) 133 { 134 struct socket *so = (struct socket *)fp->f_data; 135 int revents = 0; 136 int s = splsoftnet(); 137 138 if (events & (POLLIN | POLLRDNORM)) { 139 if (soreadable(so)) 140 revents |= events & (POLLIN | POLLRDNORM); 141 } 142 if (events & (POLLOUT | POLLWRNORM)) { 143 if (sowriteable(so)) 144 revents |= events & (POLLOUT | POLLWRNORM); 145 } 146 if (events & (POLLPRI | POLLRDBAND)) { 147 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 148 revents |= events & (POLLPRI | POLLRDBAND); 149 } 150 if (revents == 0) { 151 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 152 selrecord(p, &so->so_rcv.sb_sel); 153 so->so_rcv.sb_flags |= SB_SEL; 154 } 155 if (events & (POLLOUT | POLLWRNORM)) { 156 selrecord(p, &so->so_snd.sb_sel); 157 so->so_snd.sb_flags |= SB_SEL; 158 } 159 } 160 splx(s); 161 return (revents); 162 } 163 164 int 165 soo_stat(struct file *fp, struct stat *ub, struct proc *p) 166 { 167 struct socket *so = (struct socket *)fp->f_data; 168 169 bzero((caddr_t)ub, sizeof (*ub)); 170 ub->st_mode = S_IFSOCK; 171 if ((so->so_state & SS_CANTRCVMORE) == 0 || 172 so->so_rcv.sb_cc != 0) 173 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH; 174 if ((so->so_state & SS_CANTSENDMORE) == 0) 175 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; 176 ub->st_uid = so->so_euid; 177 ub->st_gid = so->so_egid; 178 (void) ((*so->so_proto->pr_usrreq)(so, PRU_SENSE, 179 (struct mbuf *)ub, (struct mbuf *)0, 180 (struct mbuf *)0, p)); 181 return (0); 182 } 183 184 /* ARGSUSED */ 185 int 186 soo_close(struct file *fp, struct proc *p) 187 { 188 int error = 0; 189 190 if (fp->f_data) 191 error = soclose((struct socket *)fp->f_data); 192 fp->f_data = 0; 193 return (error); 194 } 195