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