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