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