1 /*
2 * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Socket file descriptor operations
34 *
35 * $Id: sock_fops.c,v 1.1 94/10/19 23:49:52 bill Exp Locker: root $
36 */
37
38 #include "sys/param.h"
39 #include "sys/ioctl.h"
40 #include "sys/stat.h"
41 #include "sys/file.h"
42 #include "sys/errno.h"
43 #include "mbuf.h"
44 #include "socketvar.h"
45 #include "protosw.h"
46 #include "prototypes.h"
47
48 #include "if.h"
49 #include "route.h"
50
51
52 static int
53 soo_read(struct file *fp, struct uio *uio, struct ucred *cred),
54 soo_write(struct file *fp, struct uio *uio, struct ucred *cred),
55 soo_ioctl(struct file *fp, int com, caddr_t data, struct proc *p),
56 soo_select(struct file *fp, int which, struct proc *p),
57 soo_close(struct file *fp, struct proc *p),
58 soo_stat(struct file *fp, struct stat *ub, struct proc *);
59
60 struct fileops socketops =
61 { soo_read, soo_write, soo_ioctl, soo_select, soo_close, soo_stat };
62
63 /* perform a read() on a socket file descriptor */
64 static int
soo_read(struct file * fp,struct uio * uio,struct ucred * cred)65 soo_read(struct file *fp, struct uio *uio, struct ucred *cred)
66 {
67
68 return (soreceive((struct socket *)fp->f_data, (struct mbuf **)0,
69 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
70 }
71
72 /* perform a write() on a socket file descriptor */
73 static int
soo_write(struct file * fp,struct uio * uio,struct ucred * cred)74 soo_write(struct file *fp, struct uio *uio, struct ucred *cred)
75 {
76
77 return (sosend((struct socket *)fp->f_data, (struct mbuf *)0,
78 uio, (struct mbuf *)0, (struct mbuf *)0, 0));
79 }
80
81 /* perform a ioctl() on a socket file descriptor */
82 static int
soo_ioctl(struct file * fp,int cmd,caddr_t data,struct proc * p)83 soo_ioctl(struct file *fp, int cmd, caddr_t data, struct proc *p)
84 {
85 struct socket *so = (struct socket *)fp->f_data;
86
87 switch (cmd) {
88
89 /* set/clear nonblocking i/o flag */
90 case FIONBIO:
91 if (*(int *)data)
92 so->so_state |= SS_NBIO;
93 else
94 so->so_state &= ~SS_NBIO;
95 return (0);
96
97 /* set/clear asynchronous i/o flag */
98 case FIOASYNC:
99 if (*(int *)data) {
100 so->so_state |= SS_ASYNC;
101 so->so_rcv.sb_flags |= SB_ASYNC;
102 so->so_snd.sb_flags |= SB_ASYNC;
103 } else {
104 so->so_state &= ~SS_ASYNC;
105 so->so_rcv.sb_flags &= ~SB_ASYNC;
106 so->so_snd.sb_flags &= ~SB_ASYNC;
107 }
108 return (0);
109
110 /* get outstanding received characters */
111 case FIONREAD:
112 *(int *)data = so->so_rcv.sb_cc;
113 return (0);
114
115 /* set/clear nonblocking i/o flag */
116 case SIOCSPGRP:
117 so->so_pgid = *(int *)data;
118 return (0);
119
120 /* set/clear nonblocking i/o flag */
121 case SIOCGPGRP:
122 *(int *)data = so->so_pgid;
123 return (0);
124
125 /* set/clear nonblocking i/o flag */
126 case SIOCATMARK:
127 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
128 return (0);
129 }
130
131 /*
132 * Interface/routing/protocol specific ioctls:
133 * interface and routing ioctls should have a
134 * different entry since a socket's unnecessary
135 */
136 if (IOCGROUP(cmd) == 'i')
137 return (ifioctl(so, cmd, data, p));
138 #ifdef COMPAT_43
139 if (IOCGROUP(cmd) == 'r') {
140 return rtioctl(cmd, data, p);
141 /* if (_router_.rt_ioctl)
142 return RTIOCTL(cmd, data, p);
143 else
144 return(EOPNOTSUPP);*/
145 }
146 #endif
147
148 return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
149 (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0));
150 }
151
152 /* examine socket of the file descriptor to see if it is select()ed */
153 static int
soo_select(struct file * fp,int which,struct proc * p)154 soo_select(struct file *fp, int which, struct proc *p)
155 {
156 struct socket *so = (struct socket *)fp->f_data;
157 int s = splnet();
158
159 switch (which) {
160
161 /* is the socket ready for a read()? */
162 case FREAD:
163 if (soreadable(so)) {
164 splx(s);
165 return(1);
166 }
167 sbselqueue(&so->so_rcv, p);
168 break;
169
170 /* is the socket ready for a write()? */
171 case FWRITE:
172 if (sowriteable(so)) {
173 splx(s);
174 return(1);
175 }
176 sbselqueue(&so->so_snd, p);
177 break;
178
179 /* does the socket have an outstanding exception? */
180 case 0:
181 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
182 splx(s);
183 return (1);
184 }
185 sbselqueue(&so->so_rcv, p);
186 break;
187 }
188 splx(s);
189 return (0);
190 }
191
192 /* return the results for a stat() on a socket file descriptor */
193 static int
soo_stat(struct file * fp,struct stat * ub,struct proc * p)194 soo_stat(struct file *fp, struct stat *ub, struct proc *p)
195 {
196 struct socket *so = (struct socket *)fp->f_data;
197
198 /* clear the status buffer first */
199 (void) memset((caddr_t)ub, 0, sizeof (*ub));
200
201 /* ask socket's protocol for contents of status buffer to return */
202 return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
203 (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0));
204 }
205
206 /* perform a close() on a socket file descriptor */
207 static int
soo_close(struct file * fp,struct proc * p)208 soo_close(struct file *fp, struct proc *p)
209 {
210 int error = 0;
211
212 /* if socket is not closed, close it with a socket op */
213 if (fp->f_data)
214 error = soclose((struct socket *)fp->f_data);
215
216 fp->f_data = 0;
217 return (error);
218 }
219