xref: /dragonfly/sys/kern/sys_socket.c (revision c3762235)
1 /*
2  * Copyright (c) 1982, 1986, 1990, 1993
3  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD: src/sys/kern/sys_socket.c,v 1.28.2.2 2001/02/26 04:23:16 jlemon Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/file.h>
36 #include <sys/lock.h>
37 #include <sys/protosw.h>
38 #include <sys/signalvar.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/socketops.h>
42 #include <sys/filio.h>			/* XXX */
43 #include <sys/sockio.h>
44 #include <sys/vnode.h>
45 #include <sys/stat.h>
46 #include <sys/uio.h>
47 #include <sys/filedesc.h>
48 #include <sys/ucred.h>
49 
50 #include <sys/socketvar2.h>
51 
52 #include <net/if.h>
53 #include <net/route.h>
54 
55 struct	fileops socketops = {
56 	.fo_read = soo_read,
57 	.fo_write = soo_write,
58 	.fo_ioctl = soo_ioctl,
59 	.fo_kqfilter = sokqfilter,
60 	.fo_stat = soo_stat,
61 	.fo_close = soo_close,
62 	.fo_shutdown = soo_shutdown
63 };
64 
65 /*
66  * MPSAFE
67  */
68 int
69 soo_read(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
70 {
71 	struct socket *so;
72 	int error;
73 	int msgflags;
74 
75 	atomic_set_int(&curthread->td_mpflags, TDF_MP_BATCH_DEMARC);
76 
77 	so = (struct socket *)fp->f_data;
78 
79 	if (fflags & O_FBLOCKING)
80 		msgflags = 0;
81 	else if (fflags & O_FNONBLOCKING)
82 		msgflags = MSG_FNONBLOCKING;
83 	else if (fp->f_flag & FNONBLOCK)
84 		msgflags = MSG_FNONBLOCKING;
85 	else
86 		msgflags = 0;
87 
88 	error = so_pru_soreceive(so, NULL, uio, NULL, NULL, &msgflags);
89 	return (error);
90 }
91 
92 /*
93  * MPSAFE
94  */
95 int
96 soo_write(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
97 {
98 	struct socket *so;
99 	int error;
100 	int msgflags;
101 
102 	so = (struct socket *)fp->f_data;
103 
104 	if (fflags & O_FBLOCKING)
105 		msgflags = 0;
106 	else if (fflags & O_FNONBLOCKING)
107 		msgflags = MSG_FNONBLOCKING;
108 	else if (fp->f_flag & FNONBLOCK)
109 		msgflags = MSG_FNONBLOCKING;
110 	else
111 		msgflags = 0;
112 
113 	error = so_pru_sosend(so, NULL, uio, NULL, NULL, msgflags, uio->uio_td);
114 	if (error == EPIPE && !(fflags & MSG_NOSIGNAL) &&
115 	    !(so->so_options & SO_NOSIGPIPE) &&
116 	    uio->uio_td->td_lwp) {
117 		lwpsignal(uio->uio_td->td_proc, uio->uio_td->td_lwp, SIGPIPE);
118 	}
119 	return (error);
120 }
121 
122 /*
123  * MPSAFE
124  */
125 int
126 soo_ioctl(struct file *fp, u_long cmd, caddr_t data,
127 	  struct ucred *cred, struct sysmsg *msg)
128 {
129 	struct socket *so;
130 	int error;
131 
132 	so = (struct socket *)fp->f_data;
133 
134 	switch (cmd) {
135 	case FIOASYNC:
136 		if (*(int *)data) {
137 			sosetstate(so, SS_ASYNC);
138 			atomic_set_int(&so->so_rcv.ssb_flags,  SSB_ASYNC);
139 			atomic_set_int(&so->so_snd.ssb_flags, SSB_ASYNC);
140 		} else {
141 			soclrstate(so, SS_ASYNC);
142 			atomic_clear_int(&so->so_rcv.ssb_flags, SSB_ASYNC);
143 			atomic_clear_int(&so->so_snd.ssb_flags, SSB_ASYNC);
144 		}
145 		error = 0;
146 		break;
147 	case FIONREAD:
148 		*(int *)data = so->so_rcv.ssb_cc;
149 		error = 0;
150 		break;
151 	case FIOSETOWN:
152 		error = fsetown(*(int *)data, &so->so_sigio);
153 		break;
154 	case FIOGETOWN:
155 		*(int *)data = fgetown(&so->so_sigio);
156 		error = 0;
157 		break;
158 	case SIOCSPGRP:
159 		error = fsetown(-(*(int *)data), &so->so_sigio);
160 		break;
161 	case SIOCGPGRP:
162 		*(int *)data = -fgetown(&so->so_sigio);
163 		error = 0;
164 		break;
165 	case SIOCATMARK:
166 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
167 		error = 0;
168 		break;
169 	default:
170 		/*
171 		 * Interface/routing/protocol specific ioctls:
172 		 * interface and routing ioctls should have a
173 		 * different entry since a socket's unnecessary
174 		 */
175 		if (IOCGROUP(cmd) == 'i') {
176 			error = ifioctl(so, cmd, data, cred);
177 		} else if (IOCGROUP(cmd) == 'r') {
178 			error = rtioctl(cmd, data, cred);
179 		} else {
180 			error = so_pru_control_direct(so, cmd, data, NULL);
181 		}
182 		break;
183 	}
184 	return (error);
185 }
186 
187 /*
188  * MPSAFE
189  */
190 int
191 soo_stat(struct file *fp, struct stat *ub, struct ucred *cred)
192 {
193 	struct socket *so;
194 	int error;
195 
196 	bzero((caddr_t)ub, sizeof (*ub));
197 	ub->st_mode = S_IFSOCK;
198 	so = (struct socket *)fp->f_data;
199 
200 	/*
201 	 * If SS_CANTRCVMORE is set, but there's still data left in the
202 	 * receive buffer, the socket is still readable.
203 	 */
204 	if ((so->so_state & SS_CANTRCVMORE) == 0 ||
205 	    so->so_rcv.ssb_cc != 0)
206 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
207 	if ((so->so_state & SS_CANTSENDMORE) == 0)
208 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
209 	ub->st_size = so->so_rcv.ssb_cc;
210 	ub->st_uid = so->so_cred->cr_uid;
211 	ub->st_gid = so->so_cred->cr_gid;
212 	error = so_pru_sense(so, ub);
213 	return (error);
214 }
215 
216 /*
217  * MPSAFE
218  */
219 int
220 soo_close(struct file *fp)
221 {
222 	int error;
223 
224 	fp->f_ops = &badfileops;
225 	if (fp->f_data)
226 		error = soclose((struct socket *)fp->f_data, fp->f_flag);
227 	else
228 		error = 0;
229 	fp->f_data = NULL;
230 	return (error);
231 }
232 
233 /*
234  * MPSAFE
235  */
236 int
237 soo_shutdown(struct file *fp, int how)
238 {
239 	int error;
240 
241 	if (fp->f_data)
242 		error = soshutdown((struct socket *)fp->f_data, how);
243 	else
244 		error = 0;
245 	return (error);
246 }
247 
248