]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_usrreq.c
639ede8557ee23c62cd60528842282ea043a23c8
[apple/xnu.git] / bsd / kern / uipc_usrreq.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1989, 1991, 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 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/domain.h>
67 #include <sys/fcntl.h>
68 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
69 #include <sys/file_internal.h>
70 #include <sys/filedesc.h>
71 #include <sys/lock.h>
72 #include <sys/mbuf.h>
73 #include <sys/namei.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/stat.h>
80 #include <sys/sysctl.h>
81 #include <sys/un.h>
82 #include <sys/unpcb.h>
83 #include <sys/vnode_internal.h>
84 #include <sys/kdebug.h>
85
86 #include <kern/zalloc.h>
87 #include <kern/locks.h>
88
89 #define f_msgcount f_fglob->fg_msgcount
90 #define f_cred f_fglob->fg_cred
91 #define f_ops f_fglob->fg_ops
92 #define f_offset f_fglob->fg_offset
93 #define f_data f_fglob->fg_data
94 struct zone *unp_zone;
95 static unp_gen_t unp_gencnt;
96 static u_int unp_count;
97
98 static lck_attr_t *unp_mtx_attr;
99 static lck_grp_t *unp_mtx_grp;
100 static lck_grp_attr_t *unp_mtx_grp_attr;
101 static lck_rw_t *unp_list_mtx;
102
103 extern lck_mtx_t * uipc_lock;
104 static struct unp_head unp_shead, unp_dhead;
105
106 /*
107 * Unix communications domain.
108 *
109 * TODO:
110 * SEQPACKET, RDM
111 * rethink name space problems
112 * need a proper out-of-band
113 * lock pushdown
114 */
115 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL, { 0 } };
116 static ino_t unp_ino; /* prototype for fake inode numbers */
117
118 static int unp_attach(struct socket *);
119 static void unp_detach(struct unpcb *);
120 static int unp_bind(struct unpcb *,struct sockaddr *, struct proc *);
121 static int unp_connect(struct socket *,struct sockaddr *, struct proc *);
122 static void unp_disconnect(struct unpcb *);
123 static void unp_shutdown(struct unpcb *);
124 static void unp_drop(struct unpcb *, int);
125 static void unp_gc(void);
126 static void unp_scan(struct mbuf *, void (*)(struct fileglob *));
127 static void unp_mark(struct fileglob *);
128 static void unp_discard(struct fileglob *);
129 static void unp_discard_fdlocked(struct fileglob *, struct proc *);
130 static int unp_internalize(struct mbuf *, struct proc *);
131 static int unp_listen(struct unpcb *, struct proc *);
132
133
134 static int
135 uipc_abort(struct socket *so)
136 {
137 struct unpcb *unp = sotounpcb(so);
138
139 if (unp == 0)
140 return EINVAL;
141 unp_drop(unp, ECONNABORTED);
142 unp_detach(unp);
143 sofree(so);
144 return 0;
145 }
146
147 static int
148 uipc_accept(struct socket *so, struct sockaddr **nam)
149 {
150 struct unpcb *unp = sotounpcb(so);
151
152 if (unp == 0)
153 return EINVAL;
154
155 /*
156 * Pass back name of connected socket,
157 * if it was bound and we are still connected
158 * (our peer may have closed already!).
159 */
160 if (unp->unp_conn && unp->unp_conn->unp_addr) {
161 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
162 1);
163 } else {
164 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
165 }
166 return 0;
167 }
168
169 static int
170 uipc_attach(struct socket *so, __unused int proto, __unused struct proc *p)
171 {
172 struct unpcb *unp = sotounpcb(so);
173
174 if (unp != 0)
175 return EISCONN;
176 return unp_attach(so);
177 }
178
179 static int
180 uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
181 {
182 struct unpcb *unp = sotounpcb(so);
183
184 if (unp == 0)
185 return EINVAL;
186
187 return unp_bind(unp, nam, p);
188 }
189
190 static int
191 uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
192 {
193 struct unpcb *unp = sotounpcb(so);
194
195 if (unp == 0)
196 return EINVAL;
197 return unp_connect(so, nam, p);
198 }
199
200 static int
201 uipc_connect2(struct socket *so1, struct socket *so2)
202 {
203 struct unpcb *unp = sotounpcb(so1);
204
205 if (unp == 0)
206 return EINVAL;
207
208 return unp_connect2(so1, so2);
209 }
210
211 /* control is EOPNOTSUPP */
212
213 static int
214 uipc_detach(struct socket *so)
215 {
216 struct unpcb *unp = sotounpcb(so);
217
218 if (unp == 0)
219 return EINVAL;
220
221 unp_detach(unp);
222 return 0;
223 }
224
225 static int
226 uipc_disconnect(struct socket *so)
227 {
228 struct unpcb *unp = sotounpcb(so);
229
230 if (unp == 0)
231 return EINVAL;
232 unp_disconnect(unp);
233 return 0;
234 }
235
236 static int
237 uipc_listen(struct socket *so, __unused struct proc *p)
238 {
239 struct unpcb *unp = sotounpcb(so);
240
241 if (unp == 0 || unp->unp_vnode == 0)
242 return EINVAL;
243 return unp_listen(unp, p);
244 }
245
246 static int
247 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
248 {
249 struct unpcb *unp = sotounpcb(so);
250
251 if (unp == 0)
252 return EINVAL;
253 if (unp->unp_conn && unp->unp_conn->unp_addr)
254 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
255 1);
256 return 0;
257 }
258
259 static int
260 uipc_rcvd(struct socket *so, __unused int flags)
261 {
262 struct unpcb *unp = sotounpcb(so);
263 struct socket *so2;
264
265 if (unp == 0)
266 return EINVAL;
267 switch (so->so_type) {
268 case SOCK_DGRAM:
269 panic("uipc_rcvd DGRAM?");
270 /*NOTREACHED*/
271
272 case SOCK_STREAM:
273 #define rcv (&so->so_rcv)
274 #define snd (&so2->so_snd)
275 if (unp->unp_conn == 0)
276 break;
277 so2 = unp->unp_conn->unp_socket;
278 /*
279 * Adjust backpressure on sender
280 * and wakeup any waiting to write.
281 */
282 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
283 unp->unp_mbcnt = rcv->sb_mbcnt;
284 snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
285 unp->unp_cc = rcv->sb_cc;
286 sowwakeup(so2);
287 #undef snd
288 #undef rcv
289 break;
290
291 default:
292 panic("uipc_rcvd unknown socktype");
293 }
294 return 0;
295 }
296
297 /* pru_rcvoob is EOPNOTSUPP */
298
299 static int
300 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
301 struct mbuf *control, struct proc *p)
302 {
303 int error = 0;
304 struct unpcb *unp = sotounpcb(so);
305 struct socket *so2;
306
307 if (unp == 0) {
308 error = EINVAL;
309 goto release;
310 }
311 if (flags & PRUS_OOB) {
312 error = EOPNOTSUPP;
313 goto release;
314 }
315
316 if (control) {
317 socket_unlock(so, 0); /* release global lock to avoid deadlock (4436174) */
318 error = unp_internalize(control, p);
319 socket_lock(so, 0);
320 if (error)
321 goto release;
322 }
323
324 switch (so->so_type) {
325 case SOCK_DGRAM:
326 {
327 struct sockaddr *from;
328
329 if (nam) {
330 if (unp->unp_conn) {
331 error = EISCONN;
332 break;
333 }
334 error = unp_connect(so, nam, p);
335 if (error)
336 break;
337 } else {
338 if (unp->unp_conn == 0) {
339 error = ENOTCONN;
340 break;
341 }
342 }
343 so2 = unp->unp_conn->unp_socket;
344 if (unp->unp_addr)
345 from = (struct sockaddr *)unp->unp_addr;
346 else
347 from = &sun_noname;
348 if (sbappendaddr(&so2->so_rcv, from, m, control, &error)) {
349 sorwakeup(so2);
350 }
351 m = 0;
352 control = 0;
353 if (nam)
354 unp_disconnect(unp);
355 break;
356 }
357
358 case SOCK_STREAM: {
359 int didreceive = 0;
360 #define rcv (&so2->so_rcv)
361 #define snd (&so->so_snd)
362 /* Connect if not connected yet. */
363 /*
364 * Note: A better implementation would complain
365 * if not equal to the peer's address.
366 */
367 if ((so->so_state & SS_ISCONNECTED) == 0) {
368 if (nam) {
369 error = unp_connect(so, nam, p);
370 if (error)
371 break; /* XXX */
372 } else {
373 error = ENOTCONN;
374 break;
375 }
376 }
377
378 if (so->so_state & SS_CANTSENDMORE) {
379 error = EPIPE;
380 break;
381 }
382 if (unp->unp_conn == 0)
383 panic("uipc_send connected but no connection?");
384 so2 = unp->unp_conn->unp_socket;
385 /*
386 * Send to paired receive port, and then reduce
387 * send buffer hiwater marks to maintain backpressure.
388 * Wake up readers.
389 */
390 if ((control && sbappendcontrol(rcv, m, control, NULL)) ||
391 sbappend(rcv, m)) {
392 didreceive = 1;
393 }
394 snd->sb_mbmax -=
395 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
396 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
397 snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
398 unp->unp_conn->unp_cc = rcv->sb_cc;
399 if (didreceive)
400 sorwakeup(so2);
401 m = 0;
402 control = 0;
403 #undef snd
404 #undef rcv
405 }
406 break;
407
408 default:
409 panic("uipc_send unknown socktype");
410 }
411
412 /*
413 * SEND_EOF is equivalent to a SEND followed by
414 * a SHUTDOWN.
415 */
416 if (flags & PRUS_EOF) {
417 socantsendmore(so);
418 unp_shutdown(unp);
419 }
420
421 if (control && error != 0)
422 unp_dispose(control);
423
424 release:
425 if (control)
426 m_freem(control);
427 if (m)
428 m_freem(m);
429 return error;
430 }
431
432 static int
433 uipc_sense(struct socket *so, struct stat *sb)
434 {
435 struct unpcb *unp = sotounpcb(so);
436 struct socket *so2;
437
438 if (unp == 0)
439 return EINVAL;
440 sb->st_blksize = so->so_snd.sb_hiwat;
441 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
442 so2 = unp->unp_conn->unp_socket;
443 sb->st_blksize += so2->so_rcv.sb_cc;
444 }
445 sb->st_dev = NODEV;
446 if (unp->unp_ino == 0)
447 unp->unp_ino = unp_ino++;
448 sb->st_ino = unp->unp_ino;
449 return (0);
450 }
451
452 static int
453 uipc_shutdown(struct socket *so)
454 {
455 struct unpcb *unp = sotounpcb(so);
456
457 if (unp == 0)
458 return EINVAL;
459 socantsendmore(so);
460 unp_shutdown(unp);
461 return 0;
462 }
463
464 static int
465 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
466 {
467 struct unpcb *unp = sotounpcb(so);
468
469 if (unp == 0)
470 return EINVAL;
471 if (unp->unp_addr)
472 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
473 return 0;
474 }
475
476 struct pr_usrreqs uipc_usrreqs = {
477 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
478 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
479 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
480 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
481 sosend, soreceive, pru_sopoll_notsupp
482 };
483
484 int
485 uipc_ctloutput(
486 struct socket *so,
487 struct sockopt *sopt)
488 {
489 struct unpcb *unp = sotounpcb(so);
490 int error;
491
492 switch (sopt->sopt_dir) {
493 case SOPT_GET:
494 switch (sopt->sopt_name) {
495 case LOCAL_PEERCRED:
496 if (unp->unp_flags & UNP_HAVEPC)
497 error = sooptcopyout(sopt, &unp->unp_peercred,
498 sizeof(unp->unp_peercred));
499 else {
500 if (so->so_type == SOCK_STREAM)
501 error = ENOTCONN;
502 else
503 error = EINVAL;
504 }
505 break;
506 default:
507 error = EOPNOTSUPP;
508 break;
509 }
510 break;
511 case SOPT_SET:
512 default:
513 error = EOPNOTSUPP;
514 break;
515 }
516 return (error);
517 }
518
519 /*
520 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
521 * for stream sockets, although the total for sender and receiver is
522 * actually only PIPSIZ.
523 * Datagram sockets really use the sendspace as the maximum datagram size,
524 * and don't really want to reserve the sendspace. Their recvspace should
525 * be large enough for at least one max-size datagram plus address.
526 */
527 #ifndef PIPSIZ
528 #define PIPSIZ 8192
529 #endif
530 static u_long unpst_sendspace = PIPSIZ;
531 static u_long unpst_recvspace = PIPSIZ;
532 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
533 static u_long unpdg_recvspace = 4*1024;
534
535 static int unp_rights; /* file descriptors in flight */
536
537 SYSCTL_DECL(_net_local_stream);
538 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
539 &unpst_sendspace, 0, "");
540 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
541 &unpst_recvspace, 0, "");
542 SYSCTL_DECL(_net_local_dgram);
543 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
544 &unpdg_sendspace, 0, "");
545 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
546 &unpdg_recvspace, 0, "");
547 SYSCTL_DECL(_net_local);
548 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
549
550 static int
551 unp_attach(struct socket *so)
552 {
553 struct unpcb *unp;
554 int error = 0;
555
556 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
557 switch (so->so_type) {
558
559 case SOCK_STREAM:
560 error = soreserve(so, unpst_sendspace, unpst_recvspace);
561 break;
562
563 case SOCK_DGRAM:
564 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
565 break;
566
567 default:
568 panic("unp_attach");
569 }
570 if (error)
571 return (error);
572 }
573 unp = (struct unpcb*)zalloc(unp_zone);
574 if (unp == NULL)
575 return (ENOBUFS);
576 bzero(unp, sizeof *unp);
577 lck_rw_lock_exclusive(unp_list_mtx);
578 LIST_INIT(&unp->unp_refs);
579 unp->unp_socket = so;
580 unp->unp_gencnt = ++unp_gencnt;
581 unp_count++;
582 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
583 : &unp_shead, unp, unp_link);
584 so->so_pcb = (caddr_t)unp;
585 lck_rw_done(unp_list_mtx);
586 return (0);
587 }
588
589 static void
590 unp_detach(struct unpcb *unp)
591 {
592 lck_rw_lock_exclusive(unp_list_mtx);
593 LIST_REMOVE(unp, unp_link);
594 unp->unp_gencnt = ++unp_gencnt;
595 lck_rw_done(unp_list_mtx);
596 --unp_count;
597 if (unp->unp_vnode) {
598 struct vnode *tvp = unp->unp_vnode;
599 unp->unp_vnode->v_socket = 0;
600 unp->unp_vnode = 0;
601 vnode_rele(tvp); /* drop the usecount */
602 }
603 if (unp->unp_conn)
604 unp_disconnect(unp);
605 while (unp->unp_refs.lh_first)
606 unp_drop(unp->unp_refs.lh_first, ECONNRESET);
607 soisdisconnected(unp->unp_socket);
608 unp->unp_socket->so_flags |= SOF_PCBCLEARING; /* makes sure we're getting dealloced */
609 unp->unp_socket->so_pcb = 0;
610 if (unp_rights) {
611 /*
612 * Normally the receive buffer is flushed later,
613 * in sofree, but if our receive buffer holds references
614 * to descriptors that are now garbage, we will dispose
615 * of those descriptor references after the garbage collector
616 * gets them (resulting in a "panic: closef: count < 0").
617 */
618 sorflush(unp->unp_socket);
619 unp_gc();
620 }
621 if (unp->unp_addr)
622 FREE(unp->unp_addr, M_SONAME);
623 zfree(unp_zone, unp);
624 }
625
626 static int
627 unp_bind(
628 struct unpcb *unp,
629 struct sockaddr *nam,
630 struct proc *p)
631 {
632 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
633 struct vnode *vp, *dvp;
634 struct vnode_attr va;
635 struct vfs_context context;
636 int error, namelen;
637 struct nameidata nd;
638 char buf[SOCK_MAXADDRLEN];
639
640 context.vc_proc = p;
641 context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? proxy */
642
643 if (unp->unp_vnode != NULL)
644 return (EINVAL);
645 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
646 if (namelen <= 0)
647 return EINVAL;
648 strncpy(buf, soun->sun_path, namelen);
649 buf[namelen] = 0; /* null-terminate the string */
650 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE32,
651 CAST_USER_ADDR_T(buf), &context);
652 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
653 error = namei(&nd);
654 if (error) {
655 return (error);
656 }
657 dvp = nd.ni_dvp;
658 vp = nd.ni_vp;
659
660 if (vp != NULL) {
661 /*
662 * need to do this before the vnode_put of dvp
663 * since we may have to release an fs_nodelock
664 */
665 nameidone(&nd);
666
667 vnode_put(dvp);
668 vnode_put(vp);
669
670 return (EADDRINUSE);
671 }
672
673 /* authorize before creating */
674 error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, &context);
675
676 if (!error) {
677 VATTR_INIT(&va);
678 VATTR_SET(&va, va_type, VSOCK);
679 VATTR_SET(&va, va_mode, (ACCESSPERMS & ~p->p_fd->fd_cmask));
680
681 /* create the socket */
682 error = vn_create(dvp, &vp, &nd.ni_cnd, &va, 0, &context);
683 }
684
685 nameidone(&nd);
686 vnode_put(dvp);
687
688 if (error) {
689 return (error);
690 }
691 vnode_ref(vp); /* gain a longterm reference */
692 vp->v_socket = unp->unp_socket;
693 unp->unp_vnode = vp;
694 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
695 vnode_put(vp); /* drop the iocount */
696
697 return (0);
698 }
699
700 static int
701 unp_connect(
702 struct socket *so,
703 struct sockaddr *nam,
704 struct proc *p)
705 {
706 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
707 struct vnode *vp;
708 struct socket *so2, *so3;
709 struct unpcb *unp, *unp2, *unp3;
710 struct vfs_context context;
711 int error, len;
712 struct nameidata nd;
713 char buf[SOCK_MAXADDRLEN];
714
715 context.vc_proc = p;
716 context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? proxy */
717 so2 = so3 = NULL;
718
719 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
720 if (len <= 0)
721 return EINVAL;
722 strncpy(buf, soun->sun_path, len);
723 buf[len] = 0;
724
725 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32, CAST_USER_ADDR_T(buf), &context);
726 error = namei(&nd);
727 if (error) {
728 return (error);
729 }
730 nameidone(&nd);
731 vp = nd.ni_vp;
732 if (vp->v_type != VSOCK) {
733 error = ENOTSOCK;
734 goto bad;
735 }
736
737 error = vnode_authorize(vp, NULL, KAUTH_VNODE_WRITE_DATA, &context);
738 if (error)
739 goto bad;
740 so2 = vp->v_socket;
741 if (so2 == 0 || so2->so_pcb == NULL ) {
742 error = ECONNREFUSED;
743 goto bad;
744 }
745
746 /* make sure the socket can't go away while we're connecting */
747 so2->so_usecount++;
748
749 if (so->so_type != so2->so_type) {
750 error = EPROTOTYPE;
751 goto bad;
752 }
753
754 /*
755 * Check if socket was connected while we were trying to
756 * acquire the funnel.
757 * XXX - probably shouldn't return an error for SOCK_DGRAM
758 */
759 if ((so->so_state & SS_ISCONNECTED) != 0) {
760 error = EISCONN;
761 goto bad;
762 }
763
764 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
765 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
766 (so3 = sonewconn(so2, 0, nam)) == 0) {
767 error = ECONNREFUSED;
768 goto bad;
769 }
770 unp = sotounpcb(so);
771 unp2 = sotounpcb(so2);
772 unp3 = sotounpcb(so3);
773 if (unp2->unp_addr)
774 unp3->unp_addr = (struct sockaddr_un *)
775 dup_sockaddr((struct sockaddr *)
776 unp2->unp_addr, 1);
777
778 /*
779 * unp_peercred management:
780 *
781 * The connecter's (client's) credentials are copied
782 * from its process structure at the time of connect()
783 * (which is now).
784 */
785 cru2x(p->p_ucred, &unp3->unp_peercred);
786 unp3->unp_flags |= UNP_HAVEPC;
787 /*
788 * The receiver's (server's) credentials are copied
789 * from the unp_peercred member of socket on which the
790 * former called listen(); unp_listen() cached that
791 * process's credentials at that time so we can use
792 * them now.
793 */
794 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
795 ("unp_connect: listener without cached peercred"));
796 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
797 sizeof(unp->unp_peercred));
798 unp->unp_flags |= UNP_HAVEPC;
799
800 so2->so_usecount--; /* drop reference taken on so2 */
801 so2 = so3;
802 so3->so_usecount++; /* make sure we keep it around */
803 }
804 error = unp_connect2(so, so2);
805 bad:
806
807 if (so2 != NULL)
808 so2->so_usecount--; /* release count on socket */
809
810 vnode_put(vp);
811 return (error);
812 }
813
814 int
815 unp_connect2(
816 struct socket *so,
817 struct socket *so2)
818 {
819 struct unpcb *unp = sotounpcb(so);
820 struct unpcb *unp2;
821
822 if (so2->so_type != so->so_type)
823 return (EPROTOTYPE);
824 unp2 = sotounpcb(so2);
825
826 /* Verify both sockets are still opened */
827 if (unp == 0 || unp2 == 0)
828 return (EINVAL);
829
830 unp->unp_conn = unp2;
831 switch (so->so_type) {
832
833 case SOCK_DGRAM:
834 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
835 soisconnected(so);
836 break;
837
838 case SOCK_STREAM:
839 /* This takes care of socketpair */
840 if (!(unp->unp_flags & UNP_HAVEPC) && !(unp2->unp_flags & UNP_HAVEPC)) {
841 cru2x(kauth_cred_get(), &unp->unp_peercred);
842 unp->unp_flags |= UNP_HAVEPC;
843
844 cru2x(kauth_cred_get(), &unp2->unp_peercred);
845 unp2->unp_flags |= UNP_HAVEPC;
846 }
847 unp2->unp_conn = unp;
848 soisconnected(so);
849 soisconnected(so2);
850 break;
851
852 default:
853 panic("unp_connect2");
854 }
855 return (0);
856 }
857
858 static void
859 unp_disconnect(struct unpcb *unp)
860 {
861 struct unpcb *unp2 = unp->unp_conn;
862
863 if (unp2 == 0)
864 return;
865 unp->unp_conn = 0;
866 switch (unp->unp_socket->so_type) {
867
868 case SOCK_DGRAM:
869 lck_rw_lock_exclusive(unp_list_mtx);
870 LIST_REMOVE(unp, unp_reflink);
871 lck_rw_done(unp_list_mtx);
872 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
873 break;
874
875 case SOCK_STREAM:
876 soisdisconnected(unp->unp_socket);
877 unp2->unp_conn = 0;
878 soisdisconnected(unp2->unp_socket);
879 break;
880 }
881 }
882
883 #ifdef notdef
884 void
885 unp_abort(struct unpcb *unp)
886 {
887
888 unp_detach(unp);
889 }
890 #endif
891
892 static int
893 unp_pcblist SYSCTL_HANDLER_ARGS
894 {
895 int error, i, n;
896 struct unpcb *unp, **unp_list;
897 unp_gen_t gencnt;
898 struct xunpgen xug;
899 struct unp_head *head;
900
901 lck_rw_lock_shared(unp_list_mtx);
902 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
903
904 /*
905 * The process of preparing the PCB list is too time-consuming and
906 * resource-intensive to repeat twice on every request.
907 */
908 if (req->oldptr == USER_ADDR_NULL) {
909 n = unp_count;
910 req->oldidx = 2 * (sizeof xug)
911 + (n + n/8) * sizeof(struct xunpcb);
912 lck_rw_done(unp_list_mtx);
913 return 0;
914 }
915
916 if (req->newptr != USER_ADDR_NULL) {
917 lck_rw_done(unp_list_mtx);
918 return EPERM;
919 }
920
921 /*
922 * OK, now we're committed to doing something.
923 */
924 gencnt = unp_gencnt;
925 n = unp_count;
926
927 bzero(&xug, sizeof(xug));
928 xug.xug_len = sizeof xug;
929 xug.xug_count = n;
930 xug.xug_gen = gencnt;
931 xug.xug_sogen = so_gencnt;
932 error = SYSCTL_OUT(req, &xug, sizeof xug);
933 if (error) {
934 lck_rw_done(unp_list_mtx);
935 return error;
936 }
937
938 /*
939 * We are done if there is no pcb
940 */
941 if (n == 0) {
942 lck_rw_done(unp_list_mtx);
943 return 0;
944 }
945
946 MALLOC(unp_list, struct unpcb **, n * sizeof *unp_list, M_TEMP, M_WAITOK);
947 if (unp_list == 0) {
948 lck_rw_done(unp_list_mtx);
949 return ENOMEM;
950 }
951
952 for (unp = head->lh_first, i = 0; unp && i < n;
953 unp = unp->unp_link.le_next) {
954 if (unp->unp_gencnt <= gencnt)
955 unp_list[i++] = unp;
956 }
957 n = i; /* in case we lost some during malloc */
958
959 error = 0;
960 for (i = 0; i < n; i++) {
961 unp = unp_list[i];
962 if (unp->unp_gencnt <= gencnt) {
963 struct xunpcb xu;
964
965 bzero(&xu, sizeof(xu));
966 xu.xu_len = sizeof xu;
967 xu.xu_unpp = (struct unpcb_compat *)unp;
968 /*
969 * XXX - need more locking here to protect against
970 * connect/disconnect races for SMP.
971 */
972 if (unp->unp_addr)
973 bcopy(unp->unp_addr, &xu.xu_addr,
974 unp->unp_addr->sun_len);
975 if (unp->unp_conn && unp->unp_conn->unp_addr)
976 bcopy(unp->unp_conn->unp_addr,
977 &xu.xu_caddr,
978 unp->unp_conn->unp_addr->sun_len);
979 bcopy(unp, &xu.xu_unp, sizeof(xu.xu_unp));
980 sotoxsocket(unp->unp_socket, &xu.xu_socket);
981 error = SYSCTL_OUT(req, &xu, sizeof xu);
982 }
983 }
984 if (!error) {
985 /*
986 * Give the user an updated idea of our state.
987 * If the generation differs from what we told
988 * her before, she knows that something happened
989 * while we were processing this request, and it
990 * might be necessary to retry.
991 */
992 bzero(&xug, sizeof(xug));
993 xug.xug_len = sizeof xug;
994 xug.xug_gen = unp_gencnt;
995 xug.xug_sogen = so_gencnt;
996 xug.xug_count = unp_count;
997 error = SYSCTL_OUT(req, &xug, sizeof xug);
998 }
999 FREE(unp_list, M_TEMP);
1000 lck_rw_done(unp_list_mtx);
1001 return error;
1002 }
1003
1004 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
1005 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1006 "List of active local datagram sockets");
1007 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
1008 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1009 "List of active local stream sockets");
1010
1011 static void
1012 unp_shutdown(struct unpcb *unp)
1013 {
1014 struct socket *so;
1015
1016 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
1017 (so = unp->unp_conn->unp_socket))
1018 socantrcvmore(so);
1019 }
1020
1021 static void
1022 unp_drop(
1023 struct unpcb *unp,
1024 int errno)
1025 {
1026 struct socket *so = unp->unp_socket;
1027
1028 so->so_error = errno;
1029 unp_disconnect(unp);
1030 }
1031
1032 #ifdef notdef
1033 void
1034 unp_drain()
1035 {
1036
1037 }
1038 #endif
1039
1040 int
1041 unp_externalize(struct mbuf *rights)
1042 {
1043 struct proc *p = current_proc(); /* XXX */
1044 int i;
1045 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1046 struct fileglob **rp = (struct fileglob **)(cm + 1);
1047 struct fileproc *fp;
1048 struct fileglob *fg;
1049 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
1050 int f;
1051
1052 proc_fdlock(p);
1053
1054 /*
1055 * if the new FD's will not fit, then we free them all
1056 */
1057 if (!fdavail(p, newfds)) {
1058 for (i = 0; i < newfds; i++) {
1059 fg = *rp;
1060 unp_discard_fdlocked(fg, p);
1061 *rp++ = 0;
1062 }
1063 proc_fdunlock(p);
1064
1065 return (EMSGSIZE);
1066 }
1067 /*
1068 * now change each pointer to an fd in the global table to
1069 * an integer that is the index to the local fd table entry
1070 * that we set up to point to the global one we are transferring.
1071 * XXX this assumes a pointer and int are the same size...!
1072 */
1073 for (i = 0; i < newfds; i++) {
1074 if (fdalloc(p, 0, &f))
1075 panic("unp_externalize");
1076 fg = *rp;
1077 MALLOC_ZONE(fp, struct fileproc *, sizeof(struct fileproc), M_FILEPROC, M_WAITOK);
1078 bzero(fp, sizeof(struct fileproc));
1079 fp->f_iocount = 0;
1080 fp->f_fglob = fg;
1081 p->p_fd->fd_ofiles[f] = fp;
1082 fg_removeuipc(fg);
1083 *fdflags(p, f) &= ~UF_RESERVED;
1084 unp_rights--;
1085 *(int *)rp++ = f;
1086 }
1087 proc_fdunlock(p);
1088
1089 return (0);
1090 }
1091
1092 void
1093 unp_init(void)
1094 {
1095 unp_zone = zinit(sizeof(struct unpcb),
1096 (nmbclusters * sizeof(struct unpcb)),
1097 4096, "unpzone");
1098 if (unp_zone == 0)
1099 panic("unp_init");
1100 LIST_INIT(&unp_dhead);
1101 LIST_INIT(&unp_shead);
1102
1103 /*
1104 * allocate lock group attribute and group for udp pcb mutexes
1105 */
1106 unp_mtx_grp_attr = lck_grp_attr_alloc_init();
1107
1108 unp_mtx_grp = lck_grp_alloc_init("unp_list", unp_mtx_grp_attr);
1109
1110 unp_mtx_attr = lck_attr_alloc_init();
1111
1112 if ((unp_list_mtx = lck_rw_alloc_init(unp_mtx_grp, unp_mtx_attr)) == NULL)
1113 return; /* pretty much dead if this fails... */
1114
1115 }
1116
1117 #ifndef MIN
1118 #define MIN(a,b) (((a)<(b))?(a):(b))
1119 #endif
1120
1121 static int
1122 unp_internalize(
1123 struct mbuf *control,
1124 struct proc *p)
1125 {
1126 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1127 struct fileglob **rp;
1128 struct fileproc *fp;
1129 register int i, error;
1130 int oldfds;
1131 int fdgetf_noref(proc_t, struct fileglob **, struct fileproc **);
1132
1133 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
1134 cm->cmsg_len != control->m_len) {
1135 return (EINVAL);
1136 }
1137 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
1138
1139 proc_fdlock(p);
1140 rp = (struct fileglob **)(cm + 1);
1141
1142 for (i = 0; i < oldfds; i++) {
1143 if (error = fdgetf_noref(p, *(int *)rp++, (struct fileglob **)0)) {
1144 proc_fdunlock(p);
1145 return (error);
1146 }
1147 }
1148 rp = (struct fileglob **)(cm + 1);
1149
1150 for (i = 0; i < oldfds; i++) {
1151 (void) fdgetf_noref(p, *(int *)rp, &fp);
1152 fg_insertuipc(fp->f_fglob);
1153 *rp++ = fp->f_fglob;
1154 unp_rights++;
1155 }
1156 proc_fdunlock(p);
1157
1158 return (0);
1159 }
1160
1161 static int unp_defer, unp_gcing;
1162
1163 static void
1164 unp_gc()
1165 {
1166 register struct fileglob *fg, *nextfg;
1167 register struct socket *so;
1168 struct fileglob **extra_ref, **fpp;
1169 int nunref, i;
1170
1171 lck_mtx_lock(uipc_lock);
1172 if (unp_gcing) {
1173 lck_mtx_unlock(uipc_lock);
1174 return;
1175 }
1176 unp_gcing = 1;
1177 unp_defer = 0;
1178 lck_mtx_unlock(uipc_lock);
1179 /*
1180 * before going through all this, set all FDs to
1181 * be NOT defered and NOT externally accessible
1182 */
1183 for (fg = fmsghead.lh_first; fg != 0; fg = fg->f_msglist.le_next) {
1184 lck_mtx_lock(&fg->fg_lock);
1185 fg->fg_flag &= ~(FMARK|FDEFER);
1186 lck_mtx_unlock(&fg->fg_lock);
1187 }
1188 do {
1189 for (fg = fmsghead.lh_first; fg != 0; fg = fg->f_msglist.le_next) {
1190 lck_mtx_lock(&fg->fg_lock);
1191 /*
1192 * If the file is not open, skip it
1193 */
1194 if (fg->fg_count == 0) {
1195 lck_mtx_unlock(&fg->fg_lock);
1196 continue;
1197 }
1198 /*
1199 * If we already marked it as 'defer' in a
1200 * previous pass, then try process it this time
1201 * and un-mark it
1202 */
1203 if (fg->fg_flag & FDEFER) {
1204 fg->fg_flag &= ~FDEFER;
1205 unp_defer--;
1206 } else {
1207 /*
1208 * if it's not defered, then check if it's
1209 * already marked.. if so skip it
1210 */
1211 if (fg->fg_flag & FMARK){
1212 lck_mtx_unlock(&fg->fg_lock);
1213 continue;
1214 }
1215 /*
1216 * If all references are from messages
1217 * in transit, then skip it. it's not
1218 * externally accessible.
1219 */
1220 if (fg->fg_count == fg->fg_msgcount) {
1221 lck_mtx_unlock(&fg->fg_lock);
1222 continue;
1223 }
1224 /*
1225 * If it got this far then it must be
1226 * externally accessible.
1227 */
1228 fg->fg_flag |= FMARK;
1229 }
1230 /*
1231 * either it was defered, or it is externally
1232 * accessible and not already marked so.
1233 * Now check if it is possibly one of OUR sockets.
1234 */
1235 if (fg->fg_type != DTYPE_SOCKET ||
1236 (so = (struct socket *)fg->fg_data) == 0) {
1237 lck_mtx_unlock(&fg->fg_lock);
1238 continue;
1239 }
1240 if (so->so_proto->pr_domain != &localdomain ||
1241 (so->so_proto->pr_flags&PR_RIGHTS) == 0) {
1242 lck_mtx_unlock(&fg->fg_lock);
1243 continue;
1244 }
1245 #ifdef notdef
1246 /* if this code is enabled need to run under network funnel */
1247 if (so->so_rcv.sb_flags & SB_LOCK) {
1248 /*
1249 * This is problematical; it's not clear
1250 * we need to wait for the sockbuf to be
1251 * unlocked (on a uniprocessor, at least),
1252 * and it's also not clear what to do
1253 * if sbwait returns an error due to receipt
1254 * of a signal. If sbwait does return
1255 * an error, we'll go into an infinite
1256 * loop. Delete all of this for now.
1257 */
1258 (void) sbwait(&so->so_rcv);
1259 goto restart;
1260 }
1261 #endif
1262 /*
1263 * So, Ok, it's one of our sockets and it IS externally
1264 * accessible (or was defered). Now we look
1265 * to see if we hold any file descriptors in its
1266 * message buffers. Follow those links and mark them
1267 * as accessible too.
1268 */
1269 unp_scan(so->so_rcv.sb_mb, unp_mark);
1270 lck_mtx_unlock(&fg->fg_lock);
1271 }
1272 } while (unp_defer);
1273 /*
1274 * We grab an extra reference to each of the file table entries
1275 * that are not otherwise accessible and then free the rights
1276 * that are stored in messages on them.
1277 *
1278 * The bug in the orginal code is a little tricky, so I'll describe
1279 * what's wrong with it here.
1280 *
1281 * It is incorrect to simply unp_discard each entry for f_msgcount
1282 * times -- consider the case of sockets A and B that contain
1283 * references to each other. On a last close of some other socket,
1284 * we trigger a gc since the number of outstanding rights (unp_rights)
1285 * is non-zero. If during the sweep phase the gc code un_discards,
1286 * we end up doing a (full) closef on the descriptor. A closef on A
1287 * results in the following chain. Closef calls soo_close, which
1288 * calls soclose. Soclose calls first (through the switch
1289 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1290 * returns because the previous instance had set unp_gcing, and
1291 * we return all the way back to soclose, which marks the socket
1292 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1293 * to free up the rights that are queued in messages on the socket A,
1294 * i.e., the reference on B. The sorflush calls via the dom_dispose
1295 * switch unp_dispose, which unp_scans with unp_discard. This second
1296 * instance of unp_discard just calls closef on B.
1297 *
1298 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1299 * which results in another closef on A. Unfortunately, A is already
1300 * being closed, and the descriptor has already been marked with
1301 * SS_NOFDREF, and soclose panics at this point.
1302 *
1303 * Here, we first take an extra reference to each inaccessible
1304 * descriptor. Then, we call sorflush ourself, since we know
1305 * it is a Unix domain socket anyhow. After we destroy all the
1306 * rights carried in messages, we do a last closef to get rid
1307 * of our extra reference. This is the last close, and the
1308 * unp_detach etc will shut down the socket.
1309 *
1310 * 91/09/19, bsy@cs.cmu.edu
1311 */
1312 extra_ref = _MALLOC(nfiles * sizeof(struct fileglob *), M_FILEGLOB, M_WAITOK);
1313 for (nunref = 0, fg = fmsghead.lh_first, fpp = extra_ref; fg != 0;
1314 fg = nextfg) {
1315 lck_mtx_lock(&fg->fg_lock);
1316
1317 nextfg = fg->f_msglist.le_next;
1318 /*
1319 * If it's not open, skip it
1320 */
1321 if (fg->fg_count == 0) {
1322 lck_mtx_unlock(&fg->fg_lock);
1323 continue;
1324 }
1325 /*
1326 * If all refs are from msgs, and it's not marked accessible
1327 * then it must be referenced from some unreachable cycle
1328 * of (shut-down) FDs, so include it in our
1329 * list of FDs to remove
1330 */
1331 if (fg->fg_count == fg->fg_msgcount && !(fg->fg_flag & FMARK)) {
1332 fg->fg_count++;
1333 *fpp++ = fg;
1334 nunref++;
1335 }
1336 lck_mtx_unlock(&fg->fg_lock);
1337 }
1338 /*
1339 * for each FD on our hit list, do the following two things
1340 */
1341 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1342 struct fileglob *tfg;
1343
1344 tfg = *fpp;
1345
1346 if (tfg->fg_type == DTYPE_SOCKET && tfg->fg_data != NULL) {
1347 sorflush((struct socket *)(tfg->fg_data));
1348 }
1349 }
1350 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1351 closef_locked((struct fileproc *)0, *fpp, (struct proc *) NULL);
1352 unp_gcing = 0;
1353 FREE((caddr_t)extra_ref, M_FILEGLOB);
1354
1355 }
1356
1357 void
1358 unp_dispose(struct mbuf *m)
1359 {
1360
1361 if (m) {
1362 unp_scan(m, unp_discard);
1363 }
1364 }
1365
1366 static int
1367 unp_listen(
1368 struct unpcb *unp,
1369 struct proc *p)
1370 {
1371
1372 cru2x(p->p_ucred, &unp->unp_peercred);
1373 unp->unp_flags |= UNP_HAVEPCCACHED;
1374 return (0);
1375 }
1376
1377 /* should run under kernel funnel */
1378 static void
1379 unp_scan(
1380 struct mbuf *m0,
1381 void (*op)(struct fileglob *))
1382 {
1383 struct mbuf *m;
1384 struct fileglob **rp;
1385 struct cmsghdr *cm;
1386 int i;
1387 int qfds;
1388
1389 while (m0) {
1390 for (m = m0; m; m = m->m_next)
1391 if (m->m_type == MT_CONTROL &&
1392 (size_t) m->m_len >= sizeof(*cm)) {
1393 cm = mtod(m, struct cmsghdr *);
1394 if (cm->cmsg_level != SOL_SOCKET ||
1395 cm->cmsg_type != SCM_RIGHTS)
1396 continue;
1397 qfds = (cm->cmsg_len - sizeof *cm)
1398 / sizeof (struct fileglob *);
1399 rp = (struct fileglob **)(cm + 1);
1400 for (i = 0; i < qfds; i++)
1401 (*op)(*rp++);
1402 break; /* XXX, but saves time */
1403 }
1404 m0 = m0->m_act;
1405 }
1406 }
1407
1408 /* should run under kernel funnel */
1409 static void
1410 unp_mark(struct fileglob *fg)
1411 {
1412 lck_mtx_lock(&fg->fg_lock);
1413
1414 if (fg->fg_flag & FMARK) {
1415 lck_mtx_unlock(&fg->fg_lock);
1416 return;
1417 }
1418 fg->fg_flag |= (FMARK|FDEFER);
1419
1420 lck_mtx_unlock(&fg->fg_lock);
1421
1422 unp_defer++;
1423 }
1424
1425 /* should run under kernel funnel */
1426 static void
1427 unp_discard(fg)
1428 struct fileglob *fg;
1429 {
1430 struct proc *p = current_proc(); /* XXX */
1431
1432 proc_fdlock(p);
1433 unp_discard_fdlocked(fg, p);
1434 proc_fdunlock(p);
1435 }
1436 static void
1437 unp_discard_fdlocked(fg, p)
1438 struct fileglob *fg;
1439 struct proc *p;
1440 {
1441
1442 fg_removeuipc(fg);
1443
1444 unp_rights--;
1445 (void) closef_locked((struct fileproc *)0, fg, p);
1446 }