]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2011 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1990, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93 | |
61 | */ | |
2d21ac55 A |
62 | /* |
63 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
64 | * support for mandatory and extensible security protections. This notice | |
65 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
66 | * Version 2.0. | |
67 | */ | |
1c79356b A |
68 | |
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
91447636 | 71 | #include <sys/file_internal.h> |
55e303ae | 72 | #include <sys/event.h> |
1c79356b A |
73 | #include <sys/protosw.h> |
74 | #include <sys/socket.h> | |
75 | #include <sys/socketvar.h> | |
76 | #include <sys/filio.h> /* XXX */ | |
77 | #include <sys/sockio.h> | |
78 | #include <sys/stat.h> | |
79 | #include <sys/uio.h> | |
80 | #include <sys/filedesc.h> | |
91447636 A |
81 | #include <sys/kauth.h> |
82 | #include <sys/signalvar.h> | |
2d21ac55 | 83 | #include <sys/vnode.h> |
1c79356b A |
84 | |
85 | #include <net/if.h> | |
86 | #include <net/route.h> | |
87 | ||
2d21ac55 A |
88 | #if CONFIG_MACF |
89 | #include <security/mac_framework.h> | |
90 | #endif | |
91 | ||
91447636 A |
92 | /* |
93 | * File operations on sockets. | |
94 | */ | |
2d21ac55 A |
95 | static int soo_read(struct fileproc *, struct uio *, int, vfs_context_t ctx); |
96 | static int soo_write(struct fileproc *, struct uio *, int, vfs_context_t ctx); | |
97 | static int soo_close(struct fileglob *, vfs_context_t ctx); | |
98 | static int soo_drain(struct fileproc *, vfs_context_t ctx); | |
99 | ||
100 | /* TODO: these should be in header file */ | |
101 | extern int soo_ioctl(struct fileproc *, u_long, caddr_t, vfs_context_t ctx); | |
102 | extern int soo_stat(struct socket *, void *, int); | |
103 | extern int soo_select(struct fileproc *, int, void *, vfs_context_t ctx); | |
104 | extern int soo_kqfilter(struct fileproc *, struct knote *, vfs_context_t ctx); | |
105 | ||
106 | struct fileops socketops = { | |
107 | soo_read, soo_write, soo_ioctl, soo_select, soo_close, | |
108 | soo_kqfilter, soo_drain | |
109 | }; | |
1c79356b A |
110 | |
111 | /* ARGSUSED */ | |
2d21ac55 A |
112 | static int |
113 | soo_read(struct fileproc *fp, struct uio *uio, __unused int flags, | |
114 | #if !CONFIG_MACF_SOCKET | |
115 | __unused | |
116 | #endif | |
117 | vfs_context_t ctx) | |
1c79356b | 118 | { |
9bccf70c | 119 | struct socket *so; |
1c79356b | 120 | int stat; |
2d21ac55 A |
121 | #if CONFIG_MACF_SOCKET |
122 | int error; | |
123 | #endif | |
1c79356b | 124 | |
2d21ac55 A |
125 | int (*fsoreceive)(struct socket *so2, struct sockaddr **paddr, |
126 | struct uio *uio2, struct mbuf **mp0, struct mbuf **controlp, | |
127 | int *flagsp); | |
0b4e3aa0 | 128 | |
2d21ac55 A |
129 | if ((so = (struct socket *)fp->f_fglob->fg_data) == NULL) { |
130 | /* This is not a valid open file descriptor */ | |
131 | return (EBADF); | |
132 | } | |
133 | ||
134 | #if CONFIG_MACF_SOCKET | |
135 | error = mac_socket_check_receive(vfs_context_ucred(ctx), so); | |
136 | if (error) | |
137 | return (error); | |
138 | #endif /* CONFIG_MACF_SOCKET */ | |
0b4e3aa0 | 139 | |
91447636 | 140 | //###LD will have to change |
1c79356b | 141 | fsoreceive = so->so_proto->pr_usrreqs->pru_soreceive; |
2d21ac55 | 142 | |
1c79356b | 143 | stat = (*fsoreceive)(so, 0, uio, 0, 0, 0); |
2d21ac55 | 144 | return (stat); |
1c79356b A |
145 | } |
146 | ||
147 | /* ARGSUSED */ | |
2d21ac55 A |
148 | static int |
149 | soo_write(struct fileproc *fp, struct uio *uio, __unused int flags, | |
150 | vfs_context_t ctx) | |
1c79356b | 151 | { |
9bccf70c | 152 | struct socket *so; |
2d21ac55 A |
153 | int stat; |
154 | int (*fsosend)(struct socket *so2, struct sockaddr *addr, | |
155 | struct uio *uio2, struct mbuf *top, struct mbuf *control, | |
156 | int flags2); | |
157 | proc_t procp; | |
158 | ||
159 | #if CONFIG_MACF_SOCKET | |
160 | int error; | |
161 | #endif | |
1c79356b | 162 | |
91447636 A |
163 | if ((so = (struct socket *)fp->f_fglob->fg_data) == NULL) { |
164 | /* This is not a valid open file descriptor */ | |
165 | return (EBADF); | |
166 | } | |
9bccf70c | 167 | |
2d21ac55 A |
168 | #if CONFIG_MACF_SOCKET |
169 | /* JMM - have to fetch the socket's remote addr */ | |
170 | error = mac_socket_check_send(vfs_context_ucred(ctx), so, NULL); | |
171 | if (error) | |
172 | return (error); | |
173 | #endif /* CONFIG_MACF_SOCKET */ | |
174 | ||
1c79356b | 175 | fsosend = so->so_proto->pr_usrreqs->pru_sosend; |
1c79356b A |
176 | |
177 | stat = (*fsosend)(so, 0, uio, 0, 0, 0); | |
9bccf70c | 178 | |
91447636 | 179 | /* Generation of SIGPIPE can be controlled per socket */ |
2d21ac55 | 180 | procp = vfs_context_proc(ctx); |
b0d623f7 | 181 | if (stat == EPIPE && !(so->so_flags & SOF_NOSIGPIPE)) |
91447636 | 182 | psignal(procp, SIGPIPE); |
9bccf70c | 183 | |
2d21ac55 | 184 | return (stat); |
1c79356b A |
185 | } |
186 | ||
91447636 | 187 | __private_extern__ int |
2d21ac55 | 188 | soioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p) |
1c79356b | 189 | { |
2d21ac55 | 190 | int error = 0; |
91447636 | 191 | int dropsockref = -1; |
316670eb | 192 | int int_arg; |
9bccf70c | 193 | |
b0d623f7 | 194 | socket_lock(so, 1); |
1c79356b | 195 | |
2d21ac55 A |
196 | /* Call the socket filter's ioctl handler for most ioctls */ |
197 | if (IOCGROUP(cmd) != 'i' && IOCGROUP(cmd) != 'r') { | |
6d2010ae | 198 | error = sflt_ioctl(so, cmd, data); |
2d21ac55 A |
199 | if (error != 0) |
200 | goto out; | |
201 | } | |
202 | ||
1c79356b A |
203 | switch (cmd) { |
204 | ||
316670eb A |
205 | case FIONBIO: /* int */ |
206 | bcopy(data, &int_arg, sizeof (int_arg)); | |
207 | if (int_arg) | |
1c79356b A |
208 | so->so_state |= SS_NBIO; |
209 | else | |
210 | so->so_state &= ~SS_NBIO; | |
211 | ||
91447636 | 212 | goto out; |
1c79356b | 213 | |
316670eb A |
214 | case FIOASYNC: /* int */ |
215 | bcopy(data, &int_arg, sizeof (int_arg)); | |
216 | if (int_arg) { | |
1c79356b A |
217 | so->so_state |= SS_ASYNC; |
218 | so->so_rcv.sb_flags |= SB_ASYNC; | |
219 | so->so_snd.sb_flags |= SB_ASYNC; | |
220 | } else { | |
221 | so->so_state &= ~SS_ASYNC; | |
222 | so->so_rcv.sb_flags &= ~SB_ASYNC; | |
223 | so->so_snd.sb_flags &= ~SB_ASYNC; | |
224 | } | |
91447636 | 225 | goto out; |
1c79356b | 226 | |
316670eb A |
227 | case FIONREAD: /* int */ |
228 | bcopy(&so->so_rcv.sb_cc, data, sizeof (u_int32_t)); | |
91447636 | 229 | goto out; |
1c79356b | 230 | |
316670eb A |
231 | case SIOCSPGRP: /* int */ |
232 | bcopy(data, &so->so_pgid, sizeof (pid_t)); | |
91447636 | 233 | goto out; |
1c79356b | 234 | |
316670eb A |
235 | case SIOCGPGRP: /* int */ |
236 | bcopy(&so->so_pgid, data, sizeof (pid_t)); | |
91447636 | 237 | goto out; |
1c79356b | 238 | |
316670eb A |
239 | case SIOCATMARK: /* int */ |
240 | int_arg = (so->so_state & SS_RCVATMARK) != 0; | |
241 | bcopy(&int_arg, data, sizeof (int_arg)); | |
91447636 | 242 | goto out; |
1c79356b | 243 | |
316670eb | 244 | case SIOCSETOT: { /* int */ |
2d21ac55 A |
245 | /* |
246 | * Set socket level options here and then call protocol | |
247 | * specific routine. | |
248 | */ | |
249 | struct socket *cloned_so = NULL; | |
316670eb A |
250 | int cloned_fd; |
251 | ||
252 | bcopy(data, &cloned_fd, sizeof (cloned_fd)); | |
2d21ac55 A |
253 | |
254 | /* let's make sure it's either -1 or a valid file descriptor */ | |
255 | if (cloned_fd != -1) { | |
256 | error = file_socket(cloned_fd, &cloned_so); | |
257 | if (error) { | |
258 | goto out; | |
259 | } | |
260 | dropsockref = cloned_fd; | |
261 | } | |
262 | ||
263 | /* Always set socket non-blocking for OT */ | |
264 | so->so_state |= SS_NBIO; | |
265 | so->so_options |= SO_DONTTRUNC | SO_WANTMORE; | |
b0d623f7 | 266 | so->so_flags |= SOF_NOSIGPIPE | SOF_NPX_SETOPTSHUT; |
2d21ac55 A |
267 | |
268 | if (cloned_so && so != cloned_so) { | |
269 | /* Flags options */ | |
270 | so->so_options |= | |
271 | cloned_so->so_options & ~SO_ACCEPTCONN; | |
272 | ||
273 | /* SO_LINGER */ | |
274 | if (so->so_options & SO_LINGER) | |
275 | so->so_linger = cloned_so->so_linger; | |
276 | ||
277 | /* SO_SNDBUF, SO_RCVBUF */ | |
278 | if (cloned_so->so_snd.sb_hiwat > 0) { | |
279 | if (sbreserve(&so->so_snd, | |
280 | cloned_so->so_snd.sb_hiwat) == 0) { | |
281 | error = ENOBUFS; | |
282 | goto out; | |
283 | } | |
284 | } | |
285 | if (cloned_so->so_rcv.sb_hiwat > 0) { | |
286 | if (sbreserve(&so->so_rcv, | |
287 | cloned_so->so_rcv.sb_hiwat) == 0) { | |
288 | error = ENOBUFS; | |
289 | goto out; | |
290 | } | |
291 | } | |
292 | ||
293 | /* SO_SNDLOWAT, SO_RCVLOWAT */ | |
294 | so->so_snd.sb_lowat = | |
295 | (cloned_so->so_snd.sb_lowat > so->so_snd.sb_hiwat) ? | |
296 | so->so_snd.sb_hiwat : cloned_so->so_snd.sb_lowat; | |
297 | so->so_rcv.sb_lowat = | |
298 | (cloned_so->so_rcv.sb_lowat > so->so_rcv.sb_hiwat) ? | |
299 | so->so_rcv.sb_hiwat : cloned_so->so_rcv.sb_lowat; | |
300 | ||
301 | /* SO_SNDTIMEO, SO_RCVTIMEO */ | |
302 | so->so_snd.sb_timeo = cloned_so->so_snd.sb_timeo; | |
303 | so->so_rcv.sb_timeo = cloned_so->so_rcv.sb_timeo; | |
304 | } | |
305 | ||
306 | error = (*so->so_proto->pr_usrreqs->pru_control)(so, cmd, | |
307 | data, 0, p); | |
308 | /* Just ignore protocols that do not understand it */ | |
309 | if (error == EOPNOTSUPP) | |
310 | error = 0; | |
1c79356b | 311 | |
91447636 | 312 | goto out; |
2d21ac55 | 313 | } |
1c79356b A |
314 | } |
315 | /* | |
316 | * Interface/routing/protocol specific ioctls: | |
317 | * interface and routing ioctls should have a | |
318 | * different entry since a socket's unnecessary | |
319 | */ | |
2d21ac55 A |
320 | if (IOCGROUP(cmd) == 'i') { |
321 | error = ifioctllocked(so, cmd, data, p); | |
322 | } else { | |
323 | if (IOCGROUP(cmd) == 'r') | |
324 | error = rtioctl(cmd, data, p); | |
325 | else | |
326 | error = (*so->so_proto->pr_usrreqs->pru_control)(so, | |
327 | cmd, data, 0, p); | |
328 | } | |
1c79356b | 329 | |
91447636 A |
330 | out: |
331 | if (dropsockref != -1) | |
332 | file_drop(dropsockref); | |
333 | socket_unlock(so, 1); | |
334 | ||
2d21ac55 A |
335 | if (error == EJUSTRETURN) |
336 | error = 0; | |
337 | ||
338 | return (error); | |
91447636 A |
339 | } |
340 | ||
341 | int | |
2d21ac55 | 342 | soo_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, vfs_context_t ctx) |
91447636 | 343 | { |
2d21ac55 | 344 | struct socket *so; |
91447636 | 345 | int error; |
2d21ac55 | 346 | proc_t procp = vfs_context_proc(ctx); |
91447636 A |
347 | |
348 | if ((so = (struct socket *)fp->f_fglob->fg_data) == NULL) { | |
349 | /* This is not a valid open file descriptor */ | |
350 | return (EBADF); | |
351 | } | |
2d21ac55 A |
352 | |
353 | error = soioctl(so, cmd, data, procp); | |
354 | ||
91447636 A |
355 | if (error == 0 && cmd == SIOCSETOT) |
356 | fp->f_fglob->fg_flag |= FNONBLOCK; | |
357 | ||
2d21ac55 | 358 | return (error); |
1c79356b A |
359 | } |
360 | ||
361 | int | |
2d21ac55 | 362 | soo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx) |
1c79356b | 363 | { |
2d21ac55 A |
364 | struct socket *so = (struct socket *)fp->f_fglob->fg_data; |
365 | int retnum = 0; | |
366 | proc_t procp; | |
367 | ||
368 | if (so == NULL || so == (struct socket *)-1) | |
369 | return (0); | |
370 | ||
371 | procp = vfs_context_proc(ctx); | |
1c79356b | 372 | |
2d21ac55 A |
373 | #if CONFIG_MACF_SOCKET |
374 | if (mac_socket_check_select(vfs_context_ucred(ctx), so, which) != 0); | |
91447636 | 375 | return (0); |
2d21ac55 A |
376 | #endif /* CONFIG_MACF_SOCKET */ |
377 | ||
1c79356b | 378 | |
91447636 | 379 | socket_lock(so, 1); |
1c79356b A |
380 | switch (which) { |
381 | ||
382 | case FREAD: | |
0b4e3aa0 | 383 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b | 384 | if (soreadable(so)) { |
1c79356b | 385 | retnum = 1; |
0b4e3aa0 | 386 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
387 | goto done; |
388 | } | |
2d21ac55 | 389 | selrecord(procp, &so->so_rcv.sb_sel, wql); |
1c79356b A |
390 | break; |
391 | ||
392 | case FWRITE: | |
0b4e3aa0 | 393 | so->so_snd.sb_flags |= SB_SEL; |
1c79356b | 394 | if (sowriteable(so)) { |
1c79356b | 395 | retnum = 1; |
0b4e3aa0 | 396 | so->so_snd.sb_flags &= ~SB_SEL; |
1c79356b A |
397 | goto done; |
398 | } | |
2d21ac55 | 399 | selrecord(procp, &so->so_snd.sb_sel, wql); |
1c79356b A |
400 | break; |
401 | ||
402 | case 0: | |
0b4e3aa0 | 403 | so->so_rcv.sb_flags |= SB_SEL; |
1c79356b | 404 | if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) { |
1c79356b | 405 | retnum = 1; |
0b4e3aa0 | 406 | so->so_rcv.sb_flags &= ~SB_SEL; |
1c79356b A |
407 | goto done; |
408 | } | |
2d21ac55 | 409 | selrecord(procp, &so->so_rcv.sb_sel, wql); |
1c79356b A |
410 | break; |
411 | } | |
2d21ac55 | 412 | |
1c79356b | 413 | done: |
91447636 | 414 | socket_unlock(so, 1); |
1c79356b A |
415 | return (retnum); |
416 | } | |
417 | ||
1c79356b | 418 | int |
2d21ac55 | 419 | soo_stat(struct socket *so, void *ub, int isstat64) |
1c79356b | 420 | { |
2d21ac55 A |
421 | int ret; |
422 | /* warning avoidance ; protected by isstat64 */ | |
423 | struct stat *sb = (struct stat *)0; | |
424 | /* warning avoidance ; protected by isstat64 */ | |
425 | struct stat64 *sb64 = (struct stat64 *)0; | |
426 | ||
427 | #if CONFIG_MACF_SOCKET | |
428 | ret = mac_socket_check_stat(kauth_cred_get(), so); | |
429 | if (ret) | |
430 | return (ret); | |
431 | #endif | |
432 | ||
433 | if (isstat64 != 0) { | |
434 | sb64 = (struct stat64 *)ub; | |
435 | bzero((caddr_t)sb64, sizeof (*sb64)); | |
436 | } else { | |
437 | sb = (struct stat *)ub; | |
438 | bzero((caddr_t)sb, sizeof (*sb)); | |
439 | } | |
1c79356b | 440 | |
91447636 | 441 | socket_lock(so, 1); |
2d21ac55 A |
442 | if (isstat64 != 0) { |
443 | sb64->st_mode = S_IFSOCK; | |
444 | if ((so->so_state & SS_CANTRCVMORE) == 0 || | |
445 | so->so_rcv.sb_cc != 0) | |
446 | sb64->st_mode |= S_IRUSR | S_IRGRP | S_IROTH; | |
447 | if ((so->so_state & SS_CANTSENDMORE) == 0) | |
448 | sb64->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; | |
449 | sb64->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; | |
316670eb A |
450 | sb64->st_uid = kauth_cred_getuid(so->so_cred); |
451 | sb64->st_gid = kauth_cred_getgid(so->so_cred); | |
2d21ac55 A |
452 | } else { |
453 | sb->st_mode = S_IFSOCK; | |
454 | if ((so->so_state & SS_CANTRCVMORE) == 0 || | |
455 | so->so_rcv.sb_cc != 0) | |
456 | sb->st_mode |= S_IRUSR | S_IRGRP | S_IROTH; | |
457 | if ((so->so_state & SS_CANTSENDMORE) == 0) | |
458 | sb->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; | |
459 | sb->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; | |
316670eb A |
460 | sb->st_uid = kauth_cred_getuid(so->so_cred); |
461 | sb->st_gid = kauth_cred_getgid(so->so_cred); | |
2d21ac55 A |
462 | } |
463 | ||
464 | ret = (*so->so_proto->pr_usrreqs->pru_sense)(so, ub, isstat64); | |
91447636 | 465 | socket_unlock(so, 1); |
2d21ac55 | 466 | return (ret); |
1c79356b A |
467 | } |
468 | ||
469 | /* ARGSUSED */ | |
2d21ac55 A |
470 | static int |
471 | soo_close(struct fileglob *fg, __unused vfs_context_t ctx) | |
1c79356b A |
472 | { |
473 | int error = 0; | |
55e303ae A |
474 | struct socket *sp; |
475 | ||
91447636 A |
476 | sp = (struct socket *)fg->fg_data; |
477 | fg->fg_data = NULL; | |
1c79356b | 478 | |
55e303ae | 479 | if (sp) |
2d21ac55 | 480 | error = soclose(sp); |
1c79356b | 481 | |
1c79356b A |
482 | return (error); |
483 | } | |
91447636 | 484 | |
2d21ac55 A |
485 | static int |
486 | soo_drain(struct fileproc *fp, __unused vfs_context_t ctx) | |
91447636 A |
487 | { |
488 | int error = 0; | |
489 | struct socket *so = (struct socket *)fp->f_fglob->fg_data; | |
490 | ||
491 | if (so) { | |
492 | socket_lock(so, 1); | |
493 | so->so_state |= SS_DRAINING; | |
494 | ||
495 | wakeup((caddr_t)&so->so_timeo); | |
496 | sorwakeup(so); | |
497 | sowwakeup(so); | |
316670eb | 498 | soevent(so, SO_FILT_HINT_LOCKED); |
2d21ac55 | 499 | |
91447636 A |
500 | socket_unlock(so, 1); |
501 | } | |
502 | ||
2d21ac55 | 503 | return (error); |
91447636 | 504 | } |