2 * NET3: Implementation of BSD Unix domain sockets. 4 * Authors: Alan Cox, <alan.cox@linux.org> 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 9 * 2 of the License, or (at your option) any later version. 11 * Version: $Id: af_unix.c,v 1.101 2000/07/06 01:41:46 davem Exp $ 14 * Linus Torvalds : Assorted bug cures. 15 * Niibe Yutaka : async I/O support. 16 * Carsten Paeth : PF_UNIX check, address fixes. 17 * Alan Cox : Limit size of allocated blocks. 18 * Alan Cox : Fixed the stupid socketpair bug. 19 * Alan Cox : BSD compatibility fine tuning. 20 * Alan Cox : Fixed a bug in connect when interrupted. 21 * Alan Cox : Sorted out a proper draft version of 22 * file descriptor passing hacked up from 24 * Marty Leisner : Fixes to fd passing 25 * Nick Nevin : recvmsg bugfix. 26 * Alan Cox : Started proper garbage collector 27 * Heiko EiBfeldt : Missing verify_area check 28 * Alan Cox : Started POSIXisms 29 * Andreas Schwab : Replace inode by dentry for proper 31 * Kirk Petersen : Made this a module 32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm. 34 * Alexey Kuznetosv : Repaired (I hope) bugs introduces 35 * by above two patches. 36 * Andrea Arcangeli : If possible we block in connect(2) 37 * if the max backlog of the listen socket 38 * is been reached. This won't break 39 * old apps and it will avoid huge amount 40 * of socks hashed (this for unix_gc() 41 * performances reasons). 42 * Security fix that limits the max 43 * number of socks to 2*max_files and 44 * the number of skb queueable in the 46 * Artur Skawina : Hash function optimizations 47 * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8) 48 * Malcolm Beattie : Set peercred for socketpair 51 * Known differences from reference BSD that was tested: 54 * ECONNREFUSED is not returned from one end of a connected() socket to the 55 * other the moment one end closes. 56 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark 57 * and a fake inode identifier (nor the BSD first socket fstat twice bug). 59 * accept() returns a path name even if the connecting socket has closed 60 * in the meantime (BSD loses the path and gives up). 61 * accept() returns 0 length path for an unbound connector. BSD returns 16 62 * and a null first byte in the path (but not for gethost/peername - BSD bug ??) 63 * socketpair(...SOCK_RAW..) doesn't panic the kernel. 64 * BSD af_unix apparently has connect forgetting to block properly. 65 * (need to check this with the POSIX spec in detail) 67 * Differences from 2.0.0-11-... (ANK) 68 * Bug fixes and improvements. 69 * - client shutdown killed server socket. 70 * - removed all useless cli/sti pairs. 72 * Semantic changes/extensions. 73 * - generic control message passing. 74 * - SCM_CREDENTIALS control message. 75 * - "Abstract" (not FS based) socket bindings. 76 * Abstract names are sequences of bytes (not zero terminated) 77 * started by 0, so that this name space does not intersect 81 #include <linux/module.h> 82 #include <linux/config.h> 83 #include <linux/kernel.h> 84 #include <linux/major.h> 85 #include <linux/signal.h> 86 #include <linux/sched.h> 87 #include <linux/errno.h> 88 #include <linux/string.h> 89 #include <linux/stat.h> 90 #include <linux/socket.h> 92 #include <linux/fcntl.h> 93 #include <linux/termios.h> 94 #include <linux/sockios.h> 95 #include <linux/net.h> 98 #include <linux/malloc.h> 99 #include <asm/uaccess.h> 100 #include <linux/skbuff.h> 101 #include <linux/netdevice.h> 102 #include <net/sock.h> 104 #include <net/af_unix.h> 105 #include <linux/proc_fs.h> 107 #include <linux/init.h> 108 #include <linux/poll.h> 109 #include <linux/smp_lock.h> 111 #include <asm/checksum.h> 113 #define min(a,b) (((a)<(b))?(a):(b)) 115 int sysctl_unix_max_dgram_qlen
=10; 117 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1]; 118 rwlock_t unix_table_lock
= RW_LOCK_UNLOCKED
; 119 static atomic_t unix_nr_socks
=ATOMIC_INIT(0); 121 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE]) 123 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE) 126 SMP locking strategy. 127 * hash table is protceted with rwlock unix_table_lock 128 * each socket state is protected by separate rwlock. 132 extern __inline__
unsignedunix_hash_fold(unsigned hash
) 136 return hash
&(UNIX_HASH_SIZE
-1); 139 #define unix_peer(sk) ((sk)->pair) 141 extern __inline__
intunix_our_peer(unix_socket
*sk
, unix_socket
*osk
) 143 returnunix_peer(osk
) == sk
; 146 extern __inline__
intunix_may_send(unix_socket
*sk
, unix_socket
*osk
) 148 return(unix_peer(osk
) == NULL
||unix_our_peer(sk
, osk
)); 151 static __inline__ unix_socket
*unix_peer_get(unix_socket
*s
) 159 unix_state_runlock(s
); 163 extern __inline__
voidunix_release_addr(struct unix_address
*addr
) 165 if(atomic_dec_and_test(&addr
->refcnt
)) 170 * Check unix socket name: 171 * - should be not zero length. 172 * - if started by not zero, should be NULL terminated (FS object) 173 * - if started by zero, it is abstract name. 176 static intunix_mkname(struct sockaddr_un
* sunaddr
,int len
,unsigned*hashp
) 178 if(len
<=sizeof(short) || len
>sizeof(*sunaddr
)) 180 if(!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
) 182 if(sunaddr
->sun_path
[0]) 185 * This may look like an off by one error but it is 186 * a bit more subtle. 108 is the longest valid AF_UNIX 187 * path for a binding. sun_path[108] doesnt as such 188 * exist. However in kernel space we are guaranteed that 189 * it is a valid memory location in our kernel 192 if(len
>sizeof(*sunaddr
)) 193 len
=sizeof(*sunaddr
); 194 ((char*)sunaddr
)[len
]=0; 195 len
=strlen(sunaddr
->sun_path
)+1+sizeof(short); 199 *hashp
=unix_hash_fold(csum_partial((char*)sunaddr
, len
,0)); 203 static void__unix_remove_socket(unix_socket
*sk
) 205 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 208 sk
->next
->prev
= sk
->prev
; 210 sk
->prev
->next
= sk
->next
; 213 sk
->protinfo
.af_unix
.list
= NULL
; 220 static void__unix_insert_socket(unix_socket
**list
, unix_socket
*sk
) 222 BUG_TRAP(sk
->protinfo
.af_unix
.list
==NULL
); 224 sk
->protinfo
.af_unix
.list
= list
; 233 static __inline__
voidunix_remove_socket(unix_socket
*sk
) 235 write_lock(&unix_table_lock
); 236 __unix_remove_socket(sk
); 237 write_unlock(&unix_table_lock
); 240 static __inline__
voidunix_insert_socket(unix_socket
**list
, unix_socket
*sk
) 242 write_lock(&unix_table_lock
); 243 __unix_insert_socket(list
, sk
); 244 write_unlock(&unix_table_lock
); 247 static unix_socket
*__unix_find_socket_byname(struct sockaddr_un
*sunname
, 248 int len
,int type
,unsigned hash
) 252 for(s
=unix_socket_table
[hash
^type
]; s
; s
=s
->next
) { 253 if(s
->protinfo
.af_unix
.addr
->len
==len
&& 254 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) ==0) 260 static __inline__ unix_socket
* 261 unix_find_socket_byname(struct sockaddr_un
*sunname
, 262 int len
,int type
,unsigned hash
) 266 read_lock(&unix_table_lock
); 267 s
=__unix_find_socket_byname(sunname
, len
, type
, hash
); 270 read_unlock(&unix_table_lock
); 274 static unix_socket
*unix_find_socket_byinode(struct inode
*i
) 278 read_lock(&unix_table_lock
); 279 for(s
=unix_socket_table
[i
->i_ino
& (UNIX_HASH_SIZE
-1)]; s
; s
=s
->next
) 281 struct dentry
*dentry
= s
->protinfo
.af_unix
.dentry
; 283 if(dentry
&& dentry
->d_inode
== i
) 289 read_unlock(&unix_table_lock
); 293 static __inline__
intunix_writable(struct sock
*sk
) 295 return((atomic_read(&sk
->wmem_alloc
)<<2) <= sk
->sndbuf
); 298 static voidunix_write_space(struct sock
*sk
) 300 read_lock(&sk
->callback_lock
); 301 if(unix_writable(sk
)) { 302 if(sk
->sleep
&&waitqueue_active(sk
->sleep
)) 303 wake_up_interruptible(sk
->sleep
); 304 sk_wake_async(sk
,2, POLL_OUT
); 306 read_unlock(&sk
->callback_lock
); 309 static voidunix_sock_destructor(struct sock
*sk
) 311 skb_queue_purge(&sk
->receive_queue
); 313 BUG_TRAP(atomic_read(&sk
->wmem_alloc
) ==0); 314 BUG_TRAP(sk
->protinfo
.af_unix
.list
==NULL
); 315 BUG_TRAP(sk
->socket
==NULL
); 317 printk("Attempt to release alive unix socket: %p\n", sk
); 321 if(sk
->protinfo
.af_unix
.addr
) 322 unix_release_addr(sk
->protinfo
.af_unix
.addr
); 324 atomic_dec(&unix_nr_socks
); 325 #ifdef UNIX_REFCNT_DEBUG 326 printk(KERN_DEBUG
"UNIX %p is destroyed, %d are still alive.\n", sk
,atomic_read(&unix_nr_socks
)); 331 static intunix_release_sock(unix_socket
*sk
,int embrion
) 333 struct dentry
*dentry
; 334 struct vfsmount
*mnt
; 339 unix_remove_socket(sk
); 342 unix_state_wlock(sk
); 344 sk
->shutdown
= SHUTDOWN_MASK
; 345 dentry
= sk
->protinfo
.af_unix
.dentry
; 346 sk
->protinfo
.af_unix
.dentry
=NULL
; 347 mnt
= sk
->protinfo
.af_unix
.mnt
; 348 sk
->protinfo
.af_unix
.mnt
=NULL
; 350 sk
->state
= TCP_CLOSE
; 351 unix_state_wunlock(sk
); 353 wake_up_interruptible_all(&sk
->protinfo
.af_unix
.peer_wait
); 355 skpair
=unix_peer(sk
); 358 if(sk
->type
==SOCK_STREAM
) { 359 unix_state_wlock(skpair
); 360 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes*/ 361 if(!skb_queue_empty(&sk
->receive_queue
) || embrion
) 362 skpair
->err
= ECONNRESET
; 363 unix_state_wunlock(skpair
); 364 skpair
->state_change(skpair
); 365 read_lock(&skpair
->callback_lock
); 366 sk_wake_async(skpair
,1,POLL_HUP
); 367 read_unlock(&skpair
->callback_lock
); 369 sock_put(skpair
);/* It may now die */ 370 unix_peer(sk
) = NULL
; 373 /* Try to flush out this socket. Throw out buffers at least */ 375 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 377 if(state
==TCP_LISTEN
) 378 unix_release_sock(skb
->sk
,1); 379 /* passed fds are erased in the kfree_skb hook */ 390 /* ---- Socket is dead now and most probably destroyed ---- */ 393 * Fixme: BSD difference: In BSD all sockets connected to use get 394 * ECONNRESET and we die on the spot. In Linux we behave 395 * like files and pipes do and wait for the last 398 * Can't we simply set sock->err? 400 * What the above comment does talk about? --ANK(980817) 403 if(atomic_read(&unix_tot_inflight
)) 404 unix_gc();/* Garbage collect fds */ 409 static intunix_listen(struct socket
*sock
,int backlog
) 412 struct sock
*sk
= sock
->sk
; 415 if(sock
->type
!=SOCK_STREAM
) 416 goto out
;/* Only stream sockets accept */ 418 if(!sk
->protinfo
.af_unix
.addr
) 419 goto out
;/* No listens on an unbound socket */ 420 unix_state_wlock(sk
); 421 if(sk
->state
!= TCP_CLOSE
&& sk
->state
!= TCP_LISTEN
) 423 if(backlog
> sk
->max_ack_backlog
) 424 wake_up_interruptible_all(&sk
->protinfo
.af_unix
.peer_wait
); 425 sk
->max_ack_backlog
=backlog
; 426 sk
->state
=TCP_LISTEN
; 427 /* set credentials so connect can copy them */ 428 sk
->peercred
.pid
= current
->pid
; 429 sk
->peercred
.uid
= current
->euid
; 430 sk
->peercred
.gid
= current
->egid
; 434 unix_state_wunlock(sk
); 439 externstruct proto_ops unix_stream_ops
; 440 externstruct proto_ops unix_dgram_ops
; 442 static struct sock
*unix_create1(struct socket
*sock
) 446 if(atomic_read(&unix_nr_socks
) >=2*files_stat
.max_files
) 450 sk
=sk_alloc(PF_UNIX
, GFP_KERNEL
,1); 456 atomic_inc(&unix_nr_socks
); 458 sock_init_data(sock
,sk
); 460 sk
->write_space
= unix_write_space
; 462 sk
->max_ack_backlog
= sysctl_unix_max_dgram_qlen
; 463 sk
->destruct
= unix_sock_destructor
; 464 sk
->protinfo
.af_unix
.dentry
=NULL
; 465 sk
->protinfo
.af_unix
.mnt
=NULL
; 466 sk
->protinfo
.af_unix
.lock
= RW_LOCK_UNLOCKED
; 467 atomic_set(&sk
->protinfo
.af_unix
.inflight
,0); 468 init_MUTEX(&sk
->protinfo
.af_unix
.readsem
);/* single task reading lock */ 469 init_waitqueue_head(&sk
->protinfo
.af_unix
.peer_wait
); 470 sk
->protinfo
.af_unix
.list
=NULL
; 471 unix_insert_socket(&unix_sockets_unbound
, sk
); 476 static intunix_create(struct socket
*sock
,int protocol
) 478 if(protocol
&& protocol
!= PF_UNIX
) 479 return-EPROTONOSUPPORT
; 481 sock
->state
= SS_UNCONNECTED
; 485 sock
->ops
= &unix_stream_ops
; 488 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 492 sock
->type
=SOCK_DGRAM
; 494 sock
->ops
= &unix_dgram_ops
; 497 return-ESOCKTNOSUPPORT
; 500 returnunix_create1(sock
) ?0: -ENOMEM
; 503 static intunix_release(struct socket
*sock
) 505 unix_socket
*sk
= sock
->sk
; 512 returnunix_release_sock(sk
,0); 515 static intunix_autobind(struct socket
*sock
) 517 struct sock
*sk
= sock
->sk
; 518 static u32 ordernum
=1; 519 struct unix_address
* addr
; 522 down(&sk
->protinfo
.af_unix
.readsem
); 525 if(sk
->protinfo
.af_unix
.addr
) 529 addr
=kmalloc(sizeof(*addr
) +sizeof(short) +16, GFP_KERNEL
); 533 memset(addr
,0,sizeof(*addr
) +sizeof(short) +16); 534 addr
->name
->sun_family
= AF_UNIX
; 535 atomic_set(&addr
->refcnt
,1); 538 addr
->len
=sprintf(addr
->name
->sun_path
+1,"%05x", ordernum
) +1+sizeof(short); 539 addr
->hash
=unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
,0)); 541 write_lock(&unix_table_lock
); 542 ordernum
= (ordernum
+1)&0xFFFFF; 544 if(__unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
, 546 write_unlock(&unix_table_lock
); 547 /* Sanity yield. It is unusual case, but yet... */ 548 if(!(ordernum
&0xFF)) { 549 current
->policy
|= SCHED_YIELD
; 554 addr
->hash
^= sk
->type
; 556 __unix_remove_socket(sk
); 557 sk
->protinfo
.af_unix
.addr
= addr
; 558 __unix_insert_socket(&unix_socket_table
[addr
->hash
], sk
); 559 write_unlock(&unix_table_lock
); 563 up(&sk
->protinfo
.af_unix
.readsem
); 567 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
,int len
, 568 int type
,unsigned hash
,int*error
) 574 if(sunname
->sun_path
[0]) { 575 if(path_init(sunname
->sun_path
, LOOKUP_POSITIVE
, &nd
)) 576 err
=path_walk(sunname
->sun_path
, &nd
); 579 err
=permission(nd
.dentry
->d_inode
,MAY_WRITE
); 584 if(!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) 586 u
=unix_find_socket_byinode(nd
.dentry
->d_inode
); 593 if(u
->type
!= type
) { 599 u
=unix_find_socket_byname(sunname
, len
, type
, hash
); 613 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 615 struct sock
*sk
= sock
->sk
; 616 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 617 struct dentry
* dentry
= NULL
; 621 struct unix_address
*addr
; 625 if(sunaddr
->sun_family
!= AF_UNIX
) 628 if(addr_len
==sizeof(short)) { 629 err
=unix_autobind(sock
); 633 err
=unix_mkname(sunaddr
, addr_len
, &hash
); 638 down(&sk
->protinfo
.af_unix
.readsem
); 641 if(sk
->protinfo
.af_unix
.addr
) 645 addr
=kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
); 649 memcpy(addr
->name
, sunaddr
, addr_len
); 650 addr
->len
= addr_len
; 651 addr
->hash
= hash
^sk
->type
; 652 atomic_set(&addr
->refcnt
,1); 654 if(sunaddr
->sun_path
[0]) { 657 * Get the parent directory, calculate the hash for last 660 if(path_init(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
)) 661 err
=path_walk(sunaddr
->sun_path
, &nd
); 663 goto out_mknod_parent
; 665 * Yucky last component or no last component at all? 666 * (foo/., foo/.., /////) 669 if(nd
.last_type
!= LAST_NORM
) 672 * Lock the directory. 674 down(&nd
.dentry
->d_inode
->i_sem
); 676 * Do the final lookup. 678 dentry
=lookup_hash(&nd
.last
, nd
.dentry
); 679 err
=PTR_ERR(dentry
); 681 goto out_mknod_unlock
; 684 * Special case - lookup gave negative, but... we had foo/bar/ 685 * From the vfs_mknod() POV we just have a negative dentry - 686 * all is fine. Let's be bastards - you had / on the end, you've 687 * been asking for (non-existent) directory. -ENOENT for you. 689 if(nd
.last
.name
[nd
.last
.len
] && !dentry
->d_inode
) 692 * All right, let's create it. 694 err
=vfs_mknod(nd
.dentry
->d_inode
, dentry
, 695 S_IFSOCK
|sock
->inode
->i_mode
,0); 698 up(&nd
.dentry
->d_inode
->i_sem
); 702 addr
->hash
= UNIX_HASH_SIZE
; 705 write_lock(&unix_table_lock
); 707 if(!sunaddr
->sun_path
[0]) { 709 if(__unix_find_socket_byname(sunaddr
, addr_len
, 711 unix_release_addr(addr
); 715 list
= &unix_socket_table
[addr
->hash
]; 717 list
= &unix_socket_table
[dentry
->d_inode
->i_ino
& (UNIX_HASH_SIZE
-1)]; 718 sk
->protinfo
.af_unix
.dentry
= nd
.dentry
; 719 sk
->protinfo
.af_unix
.mnt
= nd
.mnt
; 723 __unix_remove_socket(sk
); 724 sk
->protinfo
.af_unix
.addr
= addr
; 725 __unix_insert_socket(list
, sk
); 728 write_unlock(&unix_table_lock
); 730 up(&sk
->protinfo
.af_unix
.readsem
); 737 up(&nd
.dentry
->d_inode
->i_sem
); 743 unix_release_addr(addr
); 747 static intunix_dgram_connect(struct socket
*sock
,struct sockaddr
*addr
, 750 struct sock
*sk
= sock
->sk
; 751 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
; 756 if(addr
->sa_family
!= AF_UNSPEC
) { 757 err
=unix_mkname(sunaddr
, alen
, &hash
); 762 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
&& 763 (err
=unix_autobind(sock
)) !=0) 766 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
); 770 unix_state_wlock(sk
); 773 if(!unix_may_send(sk
, other
)) 777 * 1003.1g breaking connected state with AF_UNSPEC 780 unix_state_wlock(sk
); 784 * If it was connected, reconnect. 787 struct sock
*old_peer
=unix_peer(sk
); 789 unix_state_wunlock(sk
); 794 unix_state_wunlock(sk
); 799 unix_state_wunlock(sk
); 805 static longunix_wait_for_peer(unix_socket
*other
,long timeo
) 808 DECLARE_WAITQUEUE(wait
, current
); 810 __set_current_state(TASK_INTERRUPTIBLE
|TASK_EXCLUSIVE
); 811 add_wait_queue_exclusive(&other
->protinfo
.af_unix
.peer_wait
, &wait
); 813 sched
= (!other
->dead
&& 814 !(other
->shutdown
&RCV_SHUTDOWN
) && 815 skb_queue_len(&other
->receive_queue
) > other
->max_ack_backlog
); 817 unix_state_runlock(other
); 820 timeo
=schedule_timeout(timeo
); 822 __set_current_state(TASK_RUNNING
); 823 remove_wait_queue(&other
->protinfo
.af_unix
.peer_wait
, &wait
); 827 static intunix_stream_connect(struct socket
*sock
,struct sockaddr
*uaddr
, 828 int addr_len
,int flags
) 830 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 831 struct sock
*sk
= sock
->sk
; 832 struct sock
*newsk
= NULL
; 833 unix_socket
*other
= NULL
; 834 struct sk_buff
*skb
= NULL
; 840 err
=unix_mkname(sunaddr
, addr_len
, &hash
); 845 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
&& 846 (err
=unix_autobind(sock
)) !=0) 849 timeo
=sock_sndtimeo(sk
, flags
& O_NONBLOCK
); 851 /* First of all allocate resources. 852 If we will make it after state is locked, 853 we will have to recheck all again in any case. 858 /* create new sock for complete connection */ 859 newsk
=unix_create1(NULL
); 863 /* Allocate skb for sending to listening sock */ 864 skb
=sock_wmalloc(newsk
,1,0, GFP_KERNEL
); 869 /* Find listening sock. */ 870 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
); 874 /* Latch state of peer */ 875 unix_state_rlock(other
); 877 /* Apparently VFS overslept socket death. Retry. */ 879 unix_state_runlock(other
); 885 if(other
->state
!= TCP_LISTEN
) 888 if(skb_queue_len(&other
->receive_queue
) > other
->max_ack_backlog
) { 893 timeo
=unix_wait_for_peer(other
, timeo
); 895 err
=sock_intr_errno(timeo
); 896 if(signal_pending(current
)) 904 It is tricky place. We need to grab write lock and cannot 905 drop lock on peer. It is dangerous because deadlock is 906 possible. Connect to self case and simultaneous 907 attempt to connect are eliminated by checking socket 908 state. other is TCP_LISTEN, if sk is TCP_LISTEN we 909 check this before attempt to grab lock. 911 Well, and we have to recheck the state after socket locked. 917 /* This is ok... continue with connect */ 919 case TCP_ESTABLISHED
: 920 /* Socket is already connected */ 928 unix_state_wlock(sk
); 930 if(sk
->state
!= st
) { 931 unix_state_wunlock(sk
); 932 unix_state_runlock(other
); 937 /* The way is open! Fastly set all the necessary fields... */ 941 newsk
->state
=TCP_ESTABLISHED
; 942 newsk
->type
=SOCK_STREAM
; 943 newsk
->peercred
.pid
= current
->pid
; 944 newsk
->peercred
.uid
= current
->euid
; 945 newsk
->peercred
.gid
= current
->egid
; 946 newsk
->sleep
= &newsk
->protinfo
.af_unix
.peer_wait
; 948 /* copy address information from listening to new sock*/ 949 if(other
->protinfo
.af_unix
.addr
) 951 atomic_inc(&other
->protinfo
.af_unix
.addr
->refcnt
); 952 newsk
->protinfo
.af_unix
.addr
=other
->protinfo
.af_unix
.addr
; 954 if(other
->protinfo
.af_unix
.dentry
) { 955 newsk
->protinfo
.af_unix
.dentry
=dget(other
->protinfo
.af_unix
.dentry
); 956 newsk
->protinfo
.af_unix
.mnt
=mntget(other
->protinfo
.af_unix
.mnt
); 959 /* Set credentials */ 960 sk
->peercred
= other
->peercred
; 964 sock
->state
=SS_CONNECTED
; 965 sk
->state
=TCP_ESTABLISHED
; 967 unix_state_wunlock(sk
); 969 /* take ten and and send info to listening sock */ 970 skb_queue_tail(&other
->receive_queue
,skb
); 971 unix_state_runlock(other
); 972 other
->data_ready(other
,0); 978 unix_state_runlock(other
); 984 unix_release_sock(newsk
,0); 990 static intunix_socketpair(struct socket
*socka
,struct socket
*sockb
) 992 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
; 994 /* Join our sockets back to back */ 999 ska
->peercred
.pid
= skb
->peercred
.pid
= current
->pid
; 1000 ska
->peercred
.uid
= skb
->peercred
.uid
= current
->euid
; 1001 ska
->peercred
.gid
= skb
->peercred
.gid
= current
->egid
; 1003 if(ska
->type
!= SOCK_DGRAM
) 1005 ska
->state
=TCP_ESTABLISHED
; 1006 skb
->state
=TCP_ESTABLISHED
; 1007 socka
->state
=SS_CONNECTED
; 1008 sockb
->state
=SS_CONNECTED
; 1013 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 1015 unix_socket
*sk
= sock
->sk
; 1017 struct sk_buff
*skb
; 1021 if(sock
->type
!=SOCK_STREAM
) 1025 if(sk
->state
!=TCP_LISTEN
) 1028 /* If socket state is TCP_LISTEN it cannot change (for now...), 1029 * so that no locks are necessary. 1032 skb
=skb_recv_datagram(sk
,0, flags
&O_NONBLOCK
, &err
); 1037 skb_free_datagram(sk
, skb
); 1038 wake_up_interruptible(&sk
->protinfo
.af_unix
.peer_wait
); 1040 /* attach accepted sock to socket */ 1041 unix_state_wlock(tsk
); 1042 newsock
->state
= SS_CONNECTED
; 1043 sock_graft(tsk
, newsock
); 1044 unix_state_wunlock(tsk
); 1052 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 1054 struct sock
*sk
= sock
->sk
; 1055 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 1059 sk
=unix_peer_get(sk
); 1069 unix_state_rlock(sk
); 1070 if(!sk
->protinfo
.af_unix
.addr
) { 1071 sunaddr
->sun_family
= AF_UNIX
; 1072 sunaddr
->sun_path
[0] =0; 1073 *uaddr_len
=sizeof(short); 1075 struct unix_address
*addr
= sk
->protinfo
.af_unix
.addr
; 1077 *uaddr_len
= addr
->len
; 1078 memcpy(sunaddr
, addr
->name
, *uaddr_len
); 1080 unix_state_runlock(sk
); 1086 static voidunix_detach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 1090 scm
->fp
=UNIXCB(skb
).fp
; 1091 skb
->destructor
= sock_wfree
; 1092 UNIXCB(skb
).fp
= NULL
; 1094 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 1095 unix_notinflight(scm
->fp
->fp
[i
]); 1098 static voidunix_destruct_fds(struct sk_buff
*skb
) 1100 struct scm_cookie scm
; 1101 memset(&scm
,0,sizeof(scm
)); 1102 unix_detach_fds(&scm
, skb
); 1104 /* Alas, it calls VFS */ 1105 /* So fscking what? fput() had been SMP-safe since the last Summer */ 1110 static voidunix_attach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 1113 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 1114 unix_inflight(scm
->fp
->fp
[i
]); 1115 UNIXCB(skb
).fp
= scm
->fp
; 1116 skb
->destructor
= unix_destruct_fds
; 1121 * Send AF_UNIX data. 1124 static intunix_dgram_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 1125 struct scm_cookie
*scm
) 1127 struct sock
*sk
= sock
->sk
; 1128 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1129 unix_socket
*other
= NULL
; 1130 int namelen
=0;/* fake GCC */ 1133 struct sk_buff
*skb
; 1137 if(msg
->msg_flags
&MSG_OOB
) 1140 if(msg
->msg_namelen
) { 1141 err
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 1148 other
=unix_peer_get(sk
); 1153 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
&& 1154 (err
=unix_autobind(sock
)) !=0) 1158 if((unsigned)len
> sk
->sndbuf
-32) 1161 skb
=sock_alloc_send_skb(sk
, len
,0, msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1165 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1167 unix_attach_fds(scm
, skb
); 1169 skb
->h
.raw
= skb
->data
; 1170 err
=memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 1174 timeo
=sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
); 1182 other
=unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
); 1187 unix_state_rlock(other
); 1189 if(!unix_may_send(sk
, other
)) 1194 * Check with 1003.1g - what should 1197 unix_state_runlock(other
); 1201 unix_state_wlock(sk
); 1202 if(unix_peer(sk
) == other
) { 1204 unix_state_wunlock(sk
); 1207 err
= -ECONNREFUSED
; 1209 unix_state_wunlock(sk
); 1219 if(other
->shutdown
&RCV_SHUTDOWN
) 1222 if(skb_queue_len(&other
->receive_queue
) > other
->max_ack_backlog
) { 1228 timeo
=unix_wait_for_peer(other
, timeo
); 1230 err
=sock_intr_errno(timeo
); 1231 if(signal_pending(current
)) 1237 skb_queue_tail(&other
->receive_queue
, skb
); 1238 unix_state_runlock(other
); 1239 other
->data_ready(other
, len
); 1244 unix_state_runlock(other
); 1254 static intunix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 1255 struct scm_cookie
*scm
) 1257 struct sock
*sk
= sock
->sk
; 1258 unix_socket
*other
= NULL
; 1259 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1261 struct sk_buff
*skb
; 1266 if(msg
->msg_flags
&MSG_OOB
) 1269 if(msg
->msg_namelen
) { 1270 err
= (sk
->state
==TCP_ESTABLISHED
? -EISCONN
: -EOPNOTSUPP
); 1275 other
=unix_peer_get(sk
); 1280 if(sk
->shutdown
&SEND_SHUTDOWN
) 1286 * Optimisation for the fact that under 0.01% of X messages typically 1292 /* Keep two messages in the pipe so it schedules better */ 1293 if(size
> sk
->sndbuf
/2-16) 1294 size
= sk
->sndbuf
/2-16; 1297 * Keep to page sized kmalloc()'s as various people 1298 * have suggested. Big mallocs stress the vm too 1302 if(size
> PAGE_SIZE
-16) 1303 limit
= PAGE_SIZE
-16;/* Fall back to a page if we can't grab a big buffer this instant */ 1305 limit
=0;/* Otherwise just grab and wait */ 1311 skb
=sock_alloc_send_skb(sk
,size
,limit
,msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1317 * If you pass two values to the sock_alloc_send_skb 1318 * it tries to grab the large buffer with GFP_BUFFER 1319 * (which can fail easily), and if it fails grab the 1320 * fallback size buffer which is under a page and will 1323 size
=min(size
,skb_tailroom(skb
)); 1325 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1327 unix_attach_fds(scm
, skb
); 1329 if((err
=memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) !=0) { 1334 unix_state_rlock(other
); 1336 if(other
->dead
|| (other
->shutdown
& RCV_SHUTDOWN
)) 1339 skb_queue_tail(&other
->receive_queue
, skb
); 1340 unix_state_runlock(other
); 1341 other
->data_ready(other
, size
); 1348 unix_state_runlock(other
); 1351 if(sent
==0&& !(msg
->msg_flags
&MSG_NOSIGNAL
)) 1352 send_sig(SIGPIPE
,current
,0); 1357 return sent
? : err
; 1360 static voidunix_copy_addr(struct msghdr
*msg
,struct sock
*sk
) 1362 msg
->msg_namelen
=sizeof(short); 1363 if(sk
->protinfo
.af_unix
.addr
) { 1364 msg
->msg_namelen
=sk
->protinfo
.af_unix
.addr
->len
; 1365 memcpy(msg
->msg_name
, 1366 sk
->protinfo
.af_unix
.addr
->name
, 1367 sk
->protinfo
.af_unix
.addr
->len
); 1371 static intunix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1372 int flags
,struct scm_cookie
*scm
) 1374 struct sock
*sk
= sock
->sk
; 1375 int noblock
= flags
& MSG_DONTWAIT
; 1376 struct sk_buff
*skb
; 1383 msg
->msg_namelen
=0; 1385 skb
=skb_recv_datagram(sk
, flags
, noblock
, &err
); 1389 wake_up_interruptible(&sk
->protinfo
.af_unix
.peer_wait
); 1392 unix_copy_addr(msg
, skb
->sk
); 1396 else if(size
< skb
->len
) 1397 msg
->msg_flags
|= MSG_TRUNC
; 1399 err
=skb_copy_datagram_iovec(skb
,0, msg
->msg_iov
, size
); 1403 scm
->creds
= *UNIXCREDS(skb
); 1405 if(!(flags
& MSG_PEEK
)) 1408 unix_detach_fds(scm
, skb
); 1412 /* It is questionable: on PEEK we could: 1413 - do not return fds - good, but too simple 8) 1414 - return fds, and do not return them on read (old strategy, 1416 - clone fds (I choosed it for now, it is the most universal 1419 POSIX 1003.1g does not actually define this clearly 1420 at all. POSIX 1003.1g doesn't define a lot of things 1425 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1430 skb_free_datagram(sk
,skb
); 1436 * Sleep until data has arrive. But check for races.. 1439 static longunix_stream_data_wait(unix_socket
* sk
,long timeo
) 1441 DECLARE_WAITQUEUE(wait
, current
); 1443 unix_state_rlock(sk
); 1445 add_wait_queue(sk
->sleep
, &wait
); 1448 set_current_state(TASK_INTERRUPTIBLE
); 1450 if(skb_queue_len(&sk
->receive_queue
) || 1452 (sk
->shutdown
& RCV_SHUTDOWN
) || 1453 signal_pending(current
) || 1457 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->socket
->flags
); 1458 unix_state_runlock(sk
); 1459 timeo
=schedule_timeout(timeo
); 1460 unix_state_rlock(sk
); 1461 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->socket
->flags
); 1464 __set_current_state(TASK_RUNNING
); 1465 remove_wait_queue(sk
->sleep
, &wait
); 1466 unix_state_runlock(sk
); 1472 static intunix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1473 int flags
,struct scm_cookie
*scm
) 1475 struct sock
*sk
= sock
->sk
; 1476 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1484 if(sk
->state
!= TCP_ESTABLISHED
) 1491 target
=sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
); 1492 timeo
=sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
); 1494 msg
->msg_namelen
=0; 1496 /* Lock the socket to prevent queue disordering 1497 * while sleeps in memcpy_tomsg 1500 down(&sk
->protinfo
.af_unix
.readsem
); 1505 struct sk_buff
*skb
; 1507 skb
=skb_dequeue(&sk
->receive_queue
); 1510 if(copied
>= target
) 1514 * POSIX 1003.1g mandates this order. 1517 if((err
=sock_error(sk
)) !=0) 1519 if(sk
->shutdown
& RCV_SHUTDOWN
) 1524 up(&sk
->protinfo
.af_unix
.readsem
); 1526 timeo
=unix_stream_data_wait(sk
, timeo
); 1528 if(signal_pending(current
)) { 1529 err
=sock_intr_errno(timeo
); 1532 down(&sk
->protinfo
.af_unix
.readsem
); 1537 /* Never glue messages from different writers */ 1538 if(memcmp(UNIXCREDS(skb
), &scm
->creds
,sizeof(scm
->creds
)) !=0) { 1539 skb_queue_head(&sk
->receive_queue
, skb
); 1543 /* Copy credentials */ 1544 scm
->creds
= *UNIXCREDS(skb
); 1548 /* Copy address just once */ 1551 unix_copy_addr(msg
, skb
->sk
); 1555 chunk
=min(skb
->len
, size
); 1556 if(memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) { 1557 skb_queue_head(&sk
->receive_queue
, skb
); 1565 /* Mark read part of skb as used */ 1566 if(!(flags
& MSG_PEEK
)) 1568 skb_pull(skb
, chunk
); 1571 unix_detach_fds(scm
, skb
); 1573 /* put the skb back if we didn't use it up.. */ 1576 skb_queue_head(&sk
->receive_queue
, skb
); 1587 /* It is questionable, see note in unix_dgram_recvmsg. 1590 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1592 /* put message back and return */ 1593 skb_queue_head(&sk
->receive_queue
, skb
); 1598 up(&sk
->protinfo
.af_unix
.readsem
); 1600 return copied
? : err
; 1603 static intunix_shutdown(struct socket
*sock
,int mode
) 1605 struct sock
*sk
= sock
->sk
; 1608 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
); 1611 unix_state_wlock(sk
); 1612 sk
->shutdown
|= mode
; 1613 other
=unix_peer(sk
); 1616 unix_state_wunlock(sk
); 1617 sk
->state_change(sk
); 1619 if(other
&& sk
->type
== SOCK_STREAM
) { 1622 if(mode
&RCV_SHUTDOWN
) 1623 peer_mode
|= SEND_SHUTDOWN
; 1624 if(mode
&SEND_SHUTDOWN
) 1625 peer_mode
|= RCV_SHUTDOWN
; 1626 unix_state_wlock(other
); 1627 other
->shutdown
|= peer_mode
; 1628 unix_state_wunlock(other
); 1629 other
->state_change(other
); 1630 read_lock(&other
->callback_lock
); 1631 if(peer_mode
== SHUTDOWN_MASK
) 1632 sk_wake_async(other
,1,POLL_HUP
); 1633 else if(peer_mode
& RCV_SHUTDOWN
) 1634 sk_wake_async(other
,1,POLL_IN
); 1635 read_unlock(&other
->callback_lock
); 1644 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1646 struct sock
*sk
= sock
->sk
; 1653 amount
=atomic_read(&sk
->wmem_alloc
); 1654 err
=put_user(amount
, (int*)arg
); 1658 struct sk_buff
*skb
; 1659 if(sk
->state
==TCP_LISTEN
) { 1664 spin_lock(&sk
->receive_queue
.lock
); 1665 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1667 spin_unlock(&sk
->receive_queue
.lock
); 1668 err
=put_user(amount
, (int*)arg
); 1679 static unsigned intunix_poll(struct file
* file
,struct socket
*sock
, poll_table
*wait
) 1681 struct sock
*sk
= sock
->sk
; 1684 poll_wait(file
, sk
->sleep
, wait
); 1687 /* exceptional events? */ 1690 if(sk
->shutdown
== SHUTDOWN_MASK
) 1694 if(!skb_queue_empty(&sk
->receive_queue
) || (sk
->shutdown
&RCV_SHUTDOWN
)) 1695 mask
|= POLLIN
| POLLRDNORM
; 1697 /* Connection-based need to check for termination and startup */ 1698 if(sk
->type
== SOCK_STREAM
&& sk
->state
==TCP_CLOSE
) 1702 * we set writable also when the other side has shut down the 1703 * connection. This prevents stuck sockets. 1705 if(unix_writable(sk
)) 1706 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
; 1712 #ifdef CONFIG_PROC_FS 1713 static intunix_read_proc(char*buffer
,char**start
, off_t offset
, 1714 int length
,int*eof
,void*data
) 1722 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1725 read_lock(&unix_table_lock
); 1726 forall_unix_sockets(i
,s
) 1728 unix_state_rlock(s
); 1730 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08X %04X %02X %5ld", 1732 atomic_read(&s
->refcnt
), 1734 s
->state
== TCP_LISTEN
? __SO_ACCEPTCON
:0, 1737 (s
->state
== TCP_ESTABLISHED
? SS_CONNECTED
: SS_UNCONNECTED
) : 1738 (s
->state
== TCP_ESTABLISHED
? SS_CONNECTING
: SS_DISCONNECTING
), 1739 s
->socket
? s
->socket
->inode
->i_ino
:0); 1741 if(s
->protinfo
.af_unix
.addr
) 1744 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
, 1745 s
->protinfo
.af_unix
.addr
->len
-sizeof(short)); 1746 if(!UNIX_ABSTRACT(s
)) 1750 len
+= s
->protinfo
.af_unix
.addr
->len
-sizeof(short); 1752 unix_state_runlock(s
); 1762 if(pos
>offset
+length
) 1767 read_unlock(&unix_table_lock
); 1768 *start
=buffer
+(offset
-begin
); 1769 len
-=(offset
-begin
); 1778 struct proto_ops unix_stream_ops
= { 1781 release
: unix_release
, 1783 connect
: unix_stream_connect
, 1784 socketpair
: unix_socketpair
, 1785 accept
: unix_accept
, 1786 getname
: unix_getname
, 1789 listen
: unix_listen
, 1790 shutdown
: unix_shutdown
, 1791 setsockopt
: sock_no_setsockopt
, 1792 getsockopt
: sock_no_getsockopt
, 1793 sendmsg
: unix_stream_sendmsg
, 1794 recvmsg
: unix_stream_recvmsg
, 1798 struct proto_ops unix_dgram_ops
= { 1801 release
: unix_release
, 1803 connect
: unix_dgram_connect
, 1804 socketpair
: unix_socketpair
, 1805 accept
: sock_no_accept
, 1806 getname
: unix_getname
, 1807 poll
: datagram_poll
, 1809 listen
: sock_no_listen
, 1810 shutdown
: unix_shutdown
, 1811 setsockopt
: sock_no_setsockopt
, 1812 getsockopt
: sock_no_getsockopt
, 1813 sendmsg
: unix_dgram_sendmsg
, 1814 recvmsg
: unix_dgram_recvmsg
, 1818 struct net_proto_family unix_family_ops
= { 1824 #ifdef CONFIG_SYSCTL 1825 externvoidunix_sysctl_register(void); 1826 externvoidunix_sysctl_unregister(void); 1829 intinit_module(void) 1831 void __init
unix_proto_init(struct net_proto
*pro
) 1834 struct sk_buff
*dummy_skb
; 1836 printk(KERN_INFO
"NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.\n"); 1837 if(sizeof(struct unix_skb_parms
) >sizeof(dummy_skb
->cb
)) 1839 printk(KERN_CRIT
"unix_proto_init: panic\n"); 1846 sock_register(&unix_family_ops
); 1847 #ifdef CONFIG_PROC_FS 1848 create_proc_read_entry("net/unix",0,0, unix_read_proc
, NULL
); 1852 #ifdef CONFIG_SYSCTL 1853 unix_sysctl_register(); 1861 voidcleanup_module(void) 1863 sock_unregister(PF_UNIX
); 1864 #ifdef CONFIG_SYSCTL 1865 unix_sysctl_unregister(); 1867 #ifdef CONFIG_PROC_FS 1868 remove_proc_entry("net/unix",0); 1875 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"