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.75 1999/03/22 05:02:45 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 47 * Known differences from reference BSD that was tested: 50 * ECONNREFUSED is not returned from one end of a connected() socket to the 51 * other the moment one end closes. 52 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark 53 * and a fake inode identifier (nor the BSD first socket fstat twice bug). 55 * accept() returns a path name even if the connecting socket has closed 56 * in the meantime (BSD loses the path and gives up). 57 * accept() returns 0 length path for an unbound connector. BSD returns 16 58 * and a null first byte in the path (but not for gethost/peername - BSD bug ??) 59 * socketpair(...SOCK_RAW..) doesn't panic the kernel. 60 * BSD af_unix apparently has connect forgetting to block properly. 61 * (need to check this with the POSIX spec in detail) 63 * Differences from 2.0.0-11-... (ANK) 64 * Bug fixes and improvements. 65 * - client shutdown killed server socket. 66 * - removed all useless cli/sti pairs. 68 * Semantic changes/extensions. 69 * - generic control message passing. 70 * - SCM_CREDENTIALS control message. 71 * - "Abstract" (not FS based) socket bindings. 72 * Abstract names are sequences of bytes (not zero terminated) 73 * started by 0, so that this name space does not intersect 77 #include <linux/module.h> 78 #include <linux/config.h> 79 #include <linux/kernel.h> 80 #include <linux/major.h> 81 #include <linux/signal.h> 82 #include <linux/sched.h> 83 #include <linux/errno.h> 84 #include <linux/string.h> 85 #include <linux/stat.h> 86 #include <linux/socket.h> 88 #include <linux/fcntl.h> 89 #include <linux/termios.h> 90 #include <linux/socket.h> 91 #include <linux/sockios.h> 92 #include <linux/net.h> 95 #include <linux/malloc.h> 96 #include <asm/uaccess.h> 97 #include <linux/skbuff.h> 98 #include <linux/netdevice.h> 101 #include <net/af_unix.h> 102 #include <linux/proc_fs.h> 104 #include <linux/init.h> 105 #include <linux/poll.h> 107 #include <asm/checksum.h> 109 #define min(a,b) (((a)<(b))?(a):(b)) 111 int sysctl_unix_delete_delay
= HZ
; 112 int sysctl_unix_destroy_delay
=10*HZ
; 113 int sysctl_unix_max_dgram_qlen
=10; 115 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1]; 116 static atomic_t unix_nr_socks
=ATOMIC_INIT(0); 117 static struct wait_queue
* unix_ack_wqueue
= NULL
; 118 static struct wait_queue
* unix_dgram_wqueue
= NULL
; 120 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE]) 122 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE) 124 static voidunix_destroy_socket(unix_socket
*sk
); 125 static voidunix_stream_write_space(struct sock
*sk
); 127 extern __inline__
unsignedunix_hash_fold(unsigned hash
) 135 #define unix_peer(sk) ((sk)->pair) 137 extern __inline__
intunix_our_peer(unix_socket
*sk
, unix_socket
*osk
) 139 returnunix_peer(osk
) == sk
; 142 extern __inline__
intunix_may_send(unix_socket
*sk
, unix_socket
*osk
) 144 return(unix_peer(osk
) == NULL
||unix_our_peer(sk
, osk
)); 147 extern __inline__
voidunix_lock(unix_socket
*sk
) 149 atomic_inc(&sk
->sock_readers
); 152 extern __inline__
voidunix_unlock(unix_socket
*sk
) 154 atomic_dec(&sk
->sock_readers
); 157 extern __inline__
intunix_locked(unix_socket
*sk
) 159 returnatomic_read(&sk
->sock_readers
); 162 extern __inline__
voidunix_release_addr(struct unix_address
*addr
) 166 if(atomic_dec_and_test(&addr
->refcnt
)) 171 static voidunix_destruct_addr(struct sock
*sk
) 173 struct unix_address
*addr
= sk
->protinfo
.af_unix
.addr
; 175 unix_release_addr(addr
); 179 * Check unix socket name: 180 * - should be not zero length. 181 * - if started by not zero, should be NULL terminated (FS object) 182 * - if started by zero, it is abstract name. 185 static intunix_mkname(struct sockaddr_un
* sunaddr
,int len
,unsigned*hashp
) 187 if(len
<=sizeof(short) || len
>sizeof(*sunaddr
)) 189 if(!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
) 191 if(sunaddr
->sun_path
[0]) 194 * This may look like an off by one error but it is 195 * a bit more subtle. 108 is the longest valid AF_UNIX 196 * path for a binding. sun_path[108] doesnt as such 197 * exist. However in kernel space we are guaranteed that 198 * it is a valid memory location in our kernel 201 if(len
>sizeof(*sunaddr
)) 202 len
=sizeof(*sunaddr
); 203 ((char*)sunaddr
)[len
]=0; 204 len
=strlen(sunaddr
->sun_path
)+1+sizeof(short); 208 *hashp
=unix_hash_fold(csum_partial((char*)sunaddr
, len
,0)); 212 static voidunix_remove_socket(unix_socket
*sk
) 214 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 216 sk
->next
->prev
= sk
->prev
; 218 sk
->prev
->next
= sk
->next
; 221 sk
->protinfo
.af_unix
.list
= NULL
; 226 static voidunix_insert_socket(unix_socket
*sk
) 228 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 236 static unix_socket
*unix_find_socket_byname(struct sockaddr_un
*sunname
, 237 int len
,int type
,unsigned hash
) 241 for(s
=unix_socket_table
[(hash
^type
)&0xF]; s
; s
=s
->next
) 243 if(s
->protinfo
.af_unix
.addr
->len
==len
&& 244 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) ==0&& 254 static unix_socket
*unix_find_socket_byinode(struct inode
*i
) 258 for(s
=unix_socket_table
[i
->i_ino
&0xF]; s
; s
=s
->next
) 260 struct dentry
*dentry
= s
->protinfo
.af_unix
.dentry
; 262 if(dentry
&& dentry
->d_inode
== i
) 272 * Delete a unix socket. We have to allow for deferring this on a timer. 275 static voidunix_destroy_timer(unsigned long data
) 277 unix_socket
*sk
=(unix_socket
*)data
; 278 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 280 atomic_dec(&unix_nr_socks
); 284 /* socket destroyed, decrement count */ 293 sk
->timer
.expires
=jiffies
+sysctl_unix_destroy_delay
;/* No real hurry try it every 10 seconds or so */ 294 add_timer(&sk
->timer
); 298 static voidunix_delayed_delete(unix_socket
*sk
) 300 sk
->timer
.data
=(unsigned long)sk
; 301 sk
->timer
.expires
=jiffies
+sysctl_unix_delete_delay
;/* Normally 1 second after will clean up. After that we try every 10 */ 302 sk
->timer
.function
=unix_destroy_timer
; 303 add_timer(&sk
->timer
); 306 static intunix_release_sock(unix_socket
*sk
) 310 sk
->state_change(sk
); 314 if(sk
->state
== TCP_LISTEN
) 315 wake_up_interruptible(&unix_ack_wqueue
); 316 if(sk
->type
== SOCK_DGRAM
) 317 wake_up_interruptible(&unix_dgram_wqueue
); 319 skpair
=unix_peer(sk
); 323 if(sk
->type
==SOCK_STREAM
&&unix_our_peer(sk
, skpair
)) 325 skpair
->state_change(skpair
); 326 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes*/ 328 unix_unlock(skpair
);/* It may now die */ 331 /* Try to flush out this socket. Throw out buffers at least */ 332 unix_destroy_socket(sk
); 335 * Fixme: BSD difference: In BSD all sockets connected to use get 336 * ECONNRESET and we die on the spot. In Linux we behave 337 * like files and pipes do and wait for the last 340 * Can't we simply set sock->err? 342 * What the above comment does talk about? --ANK(980817) 345 unix_gc();/* Garbage collect fds */ 349 static voidunix_destroy_socket(unix_socket
*sk
) 353 unix_remove_socket(sk
); 355 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 357 if(sk
->state
==TCP_LISTEN
) 358 unix_release_sock(skb
->sk
); 359 /* passed fds are erased in the kfree_skb hook */ 363 if(sk
->protinfo
.af_unix
.dentry
!=NULL
) 365 dput(sk
->protinfo
.af_unix
.dentry
); 366 sk
->protinfo
.af_unix
.dentry
=NULL
; 369 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 371 atomic_dec(&unix_nr_socks
); 375 /* socket destroyed, decrement count */ 382 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 387 static intunix_listen(struct socket
*sock
,int backlog
) 389 struct sock
*sk
= sock
->sk
; 391 if(sock
->state
!= SS_UNCONNECTED
) 393 if(sock
->type
!=SOCK_STREAM
) 394 return-EOPNOTSUPP
;/* Only stream sockets accept */ 395 if(!sk
->protinfo
.af_unix
.addr
) 396 return-EINVAL
;/* No listens on an unbound socket */ 397 if((unsigned) backlog
> SOMAXCONN
) 399 sk
->max_ack_backlog
=backlog
; 400 sk
->state
=TCP_LISTEN
; 401 sock
->flags
|= SO_ACCEPTCON
; 402 /* set credentials so connect can copy them */ 403 sk
->peercred
.pid
= current
->pid
; 404 sk
->peercred
.uid
= current
->euid
; 405 sk
->peercred
.gid
= current
->egid
; 409 externstruct proto_ops unix_stream_ops
; 410 externstruct proto_ops unix_dgram_ops
; 412 static struct sock
*unix_create1(struct socket
*sock
,int stream
) 416 if(atomic_read(&unix_nr_socks
) >=2*max_files
) 420 sk
=sk_alloc(PF_UNIX
, GFP_KERNEL
,1); 426 atomic_inc(&unix_nr_socks
); 428 sock_init_data(sock
,sk
); 431 sk
->write_space
= unix_stream_write_space
; 433 sk
->destruct
= unix_destruct_addr
; 434 sk
->protinfo
.af_unix
.family
=PF_UNIX
; 435 sk
->protinfo
.af_unix
.dentry
=NULL
; 436 sk
->protinfo
.af_unix
.readsem
=MUTEX
;/* single task reading lock */ 437 sk
->protinfo
.af_unix
.list
=&unix_sockets_unbound
; 438 unix_insert_socket(sk
); 443 static intunix_create(struct socket
*sock
,int protocol
) 447 if(protocol
&& protocol
!= PF_UNIX
) 448 return-EPROTONOSUPPORT
; 450 sock
->state
= SS_UNCONNECTED
; 454 sock
->ops
= &unix_stream_ops
; 458 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 462 sock
->type
=SOCK_DGRAM
; 464 sock
->ops
= &unix_dgram_ops
; 467 return-ESOCKTNOSUPPORT
; 470 returnunix_create1(sock
, stream
) ?0: -ENOMEM
; 473 static intunix_release(struct socket
*sock
,struct socket
*peer
) 475 unix_socket
*sk
= sock
->sk
; 481 if(sock
->state
!= SS_UNCONNECTED
) 482 sock
->state
= SS_DISCONNECTING
; 484 returnunix_release_sock(sk
); 487 static intunix_autobind(struct socket
*sock
) 489 struct sock
*sk
= sock
->sk
; 490 static u32 ordernum
=1; 491 struct unix_address
* addr
; 494 addr
=kmalloc(sizeof(*addr
) +sizeof(short) +16, GFP_KERNEL
); 497 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
) 502 memset(addr
,0,sizeof(*addr
) +sizeof(short) +16); 503 addr
->name
->sun_family
= AF_UNIX
; 504 atomic_set(&addr
->refcnt
,1); 507 addr
->len
=sprintf(addr
->name
->sun_path
+1,"%08x", ordernum
) +1+sizeof(short); 508 addr
->hash
=unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
,0)); 511 if((osk
=unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
, 512 addr
->hash
)) != NULL
) 518 sk
->protinfo
.af_unix
.addr
= addr
; 519 unix_remove_socket(sk
); 520 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(addr
->hash
^ sk
->type
)&0xF]; 521 unix_insert_socket(sk
); 525 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
,int len
, 526 int type
,unsigned hash
,int*error
) 530 if(sunname
->sun_path
[0]) 532 struct dentry
*dentry
; 533 dentry
=open_namei(sunname
->sun_path
,2, S_IFSOCK
); 535 *error
=PTR_ERR(dentry
); 538 u
=unix_find_socket_byinode(dentry
->d_inode
); 540 if(u
&& u
->type
!= type
) 548 u
=unix_find_socket_byname(sunname
, len
, type
, hash
); 552 *error
=-ECONNREFUSED
; 559 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 561 struct sock
*sk
= sock
->sk
; 562 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 563 struct dentry
* dentry
; 566 struct unix_address
*addr
; 568 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
|| 569 sunaddr
->sun_family
!= AF_UNIX
) 572 if(addr_len
==sizeof(short)) 573 returnunix_autobind(sock
); 575 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 579 addr
=kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
); 583 /* We slept; recheck ... */ 585 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
) 588 return-EINVAL
;/* Already bound */ 591 memcpy(addr
->name
, sunaddr
, addr_len
); 592 addr
->len
= addr_len
; 594 atomic_set(&addr
->refcnt
,1); 596 if(!sunaddr
->sun_path
[0]) 598 unix_socket
*osk
=unix_find_socket_byname(sunaddr
, addr_len
, 606 unix_remove_socket(sk
); 607 sk
->protinfo
.af_unix
.addr
= addr
; 608 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(hash
^sk
->type
)&0xF]; 609 unix_insert_socket(sk
); 613 addr
->hash
= UNIX_HASH_SIZE
; 614 sk
->protinfo
.af_unix
.addr
= addr
; 617 dentry
=do_mknod(sunaddr
->sun_path
, S_IFSOCK
|sock
->inode
->i_mode
,0); 620 err
=PTR_ERR(dentry
); 621 unix_release_addr(addr
); 622 sk
->protinfo
.af_unix
.addr
= NULL
; 628 unix_remove_socket(sk
); 629 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[dentry
->d_inode
->i_ino
&0xF]; 630 sk
->protinfo
.af_unix
.dentry
= dentry
; 631 unix_insert_socket(sk
); 636 static intunix_dgram_connect(struct socket
*sock
,struct sockaddr
*addr
, 639 struct sock
*sk
= sock
->sk
; 640 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
; 646 * 1003.1g breaking connected state with AF_UNSPEC 649 if(addr
->sa_family
==AF_UNSPEC
) 653 unix_unlock(unix_peer(sk
)); 654 unix_peer(sk
) = NULL
; 655 sock
->state
=SS_UNCONNECTED
; 660 alen
=unix_mkname(sunaddr
, alen
, &hash
); 664 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
); 667 if(!unix_may_send(sk
, other
)) 674 * If it was connected, reconnect. 678 unix_unlock(unix_peer(sk
)); 682 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 687 static intunix_stream_connect(struct socket
*sock
,struct sockaddr
*uaddr
, 688 int addr_len
,int flags
) 690 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 691 struct sock
*sk
= sock
->sk
, *newsk
; 697 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 701 /* First of all allocate resources. 702 If we will make it after state checks, 703 we will have to recheck all again in any case. 707 /* Find listening sock */ 708 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
); 713 while(other
->ack_backlog
>= other
->max_ack_backlog
) { 715 if(other
->dead
|| other
->state
!= TCP_LISTEN
) 717 if(flags
& O_NONBLOCK
) 719 interruptible_sleep_on(&unix_ack_wqueue
); 720 if(signal_pending(current
)) 725 /* create new sock for complete connection */ 726 newsk
=unix_create1(NULL
,1); 728 /* Allocate skb for sending to listening sock */ 731 skb
=sock_wmalloc(newsk
,1,0, GFP_KERNEL
); 736 /* This is ok... continue with connect */ 739 /* Socket is already connected */ 748 if(sk
->state
!= TCP_CLOSE
) 751 /* Check that listener is in valid state. */ 753 if(other
->dead
|| other
->state
!= TCP_LISTEN
) 757 if(newsk
== NULL
|| skb
== NULL
) 760 UNIXCB(skb
).attr
= MSG_SYN
; 762 /* set up connecting socket */ 763 sock
->state
=SS_CONNECTED
; 764 if(!sk
->protinfo
.af_unix
.addr
) 768 sk
->state
=TCP_ESTABLISHED
; 769 /* Set credentials */ 770 sk
->peercred
= other
->peercred
; 772 /* set up newly created sock */ 775 newsk
->state
=TCP_ESTABLISHED
; 776 newsk
->type
=SOCK_STREAM
; 777 newsk
->peercred
.pid
= current
->pid
; 778 newsk
->peercred
.uid
= current
->euid
; 779 newsk
->peercred
.gid
= current
->egid
; 781 /* copy address information from listening to new sock*/ 782 if(other
->protinfo
.af_unix
.addr
) 784 atomic_inc(&other
->protinfo
.af_unix
.addr
->refcnt
); 785 newsk
->protinfo
.af_unix
.addr
=other
->protinfo
.af_unix
.addr
; 787 if(other
->protinfo
.af_unix
.dentry
) 788 newsk
->protinfo
.af_unix
.dentry
=dget(other
->protinfo
.af_unix
.dentry
); 790 /* send info to listening sock */ 791 other
->ack_backlog
++; 792 skb_queue_tail(&other
->receive_queue
,skb
); 793 other
->data_ready(other
,0);/* Wake up ! */ 801 unix_destroy_socket(newsk
); 807 static intunix_socketpair(struct socket
*socka
,struct socket
*sockb
) 809 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
; 811 /* Join our sockets back to back */ 817 if(ska
->type
!= SOCK_DGRAM
) 819 ska
->state
=TCP_ESTABLISHED
; 820 skb
->state
=TCP_ESTABLISHED
; 821 socka
->state
=SS_CONNECTED
; 822 sockb
->state
=SS_CONNECTED
; 827 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 829 unix_socket
*sk
= sock
->sk
; 830 unix_socket
*newsk
= newsock
->sk
; 834 if(sock
->state
!= SS_UNCONNECTED
) 836 if(!(sock
->flags
& SO_ACCEPTCON
)) 839 if(sock
->type
!=SOCK_STREAM
) 841 if(sk
->state
!=TCP_LISTEN
) 846 skb
=skb_dequeue(&sk
->receive_queue
); 851 interruptible_sleep_on(sk
->sleep
); 852 if(signal_pending(current
)) 856 if(!(UNIXCB(skb
).attr
& MSG_SYN
)) 859 tsk
->state_change(tsk
); 864 if(sk
->max_ack_backlog
== sk
->ack_backlog
--) 865 wake_up_interruptible(&unix_ack_wqueue
); 871 /* attach accepted sock to socket */ 872 newsock
->state
=SS_CONNECTED
; 874 tsk
->sleep
=newsk
->sleep
; 877 /* destroy handed sock */ 878 newsk
->socket
= NULL
; 879 unix_destroy_socket(newsk
); 885 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 887 struct sock
*sk
= sock
->sk
; 888 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 896 if(!sk
->protinfo
.af_unix
.addr
) 898 sunaddr
->sun_family
= AF_UNIX
; 899 sunaddr
->sun_path
[0] =0; 900 *uaddr_len
=sizeof(short); 901 return0;/* Not bound */ 903 *uaddr_len
= sk
->protinfo
.af_unix
.addr
->len
; 904 memcpy(sunaddr
, sk
->protinfo
.af_unix
.addr
->name
, *uaddr_len
); 908 static voidunix_detach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 912 scm
->fp
=UNIXCB(skb
).fp
; 913 skb
->destructor
= sock_wfree
; 914 UNIXCB(skb
).fp
= NULL
; 916 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 917 unix_notinflight(scm
->fp
->fp
[i
]); 920 static voidunix_destruct_fds(struct sk_buff
*skb
) 922 struct scm_cookie scm
; 923 memset(&scm
,0,sizeof(scm
)); 924 unix_detach_fds(&scm
, skb
); 929 static voidunix_attach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 932 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 933 unix_inflight(scm
->fp
->fp
[i
]); 934 UNIXCB(skb
).fp
= scm
->fp
; 935 skb
->destructor
= unix_destruct_fds
; 944 static intunix_dgram_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 945 struct scm_cookie
*scm
) 947 struct sock
*sk
= sock
->sk
; 948 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 950 int namelen
=0;/* fake GCC */ 955 if(msg
->msg_flags
&MSG_OOB
) 958 if(msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
)) 961 if(msg
->msg_namelen
) { 962 namelen
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 971 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 974 skb
=sock_alloc_send_skb(sk
, len
,0, msg
->msg_flags
&MSG_DONTWAIT
, &err
); 978 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 979 UNIXCB(skb
).attr
= msg
->msg_flags
; 981 unix_attach_fds(scm
, skb
); 983 skb
->h
.raw
= skb
->data
; 984 err
=memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 988 other
=unix_peer(sk
); 989 if(other
&& other
->dead
) 992 * Check with 1003.1g - what should 1005 other
=unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
); 1009 if(!unix_may_send(sk
, other
)) 1013 while(skb_queue_len(&other
->receive_queue
) >= 1014 sysctl_unix_max_dgram_qlen
) 1016 if(sock
->file
->f_flags
& O_NONBLOCK
) 1021 interruptible_sleep_on(&unix_dgram_wqueue
); 1024 if(sk
->shutdown
& SEND_SHUTDOWN
) 1029 if(signal_pending(current
)) 1036 skb_queue_tail(&other
->receive_queue
, skb
); 1037 other
->data_ready(other
,len
); 1052 static intunix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 1053 struct scm_cookie
*scm
) 1055 struct sock
*sk
= sock
->sk
; 1057 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1059 struct sk_buff
*skb
; 1063 if(sock
->flags
& SO_ACCEPTCON
) 1066 if(msg
->msg_flags
&MSG_OOB
) 1069 if(msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
)) 1072 if(msg
->msg_namelen
) { 1073 if(sk
->state
==TCP_ESTABLISHED
) 1083 if(sk
->shutdown
&SEND_SHUTDOWN
) { 1084 if(!(msg
->msg_flags
&MSG_NOSIGNAL
)) 1085 send_sig(SIGPIPE
,current
,0); 1092 * Optimisation for the fact that under 0.01% of X messages typically 1098 /* Keep two messages in the pipe so it schedules better */ 1099 if(size
> sk
->sndbuf
/2-16) 1100 size
= sk
->sndbuf
/2-16; 1103 * Keep to page sized kmalloc()'s as various people 1104 * have suggested. Big mallocs stress the vm too 1109 limit
=4096-16;/* Fall back to a page if we can't grab a big buffer this instant */ 1111 limit
=0;/* Otherwise just grab and wait */ 1117 skb
=sock_alloc_send_skb(sk
,size
,limit
,msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1127 * If you pass two values to the sock_alloc_send_skb 1128 * it tries to grab the large buffer with GFP_BUFFER 1129 * (which can fail easily), and if it fails grab the 1130 * fallback size buffer which is under a page and will 1133 size
=min(size
,skb_tailroom(skb
)); 1135 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1136 UNIXCB(skb
).attr
= msg
->msg_flags
; 1138 unix_attach_fds(scm
, skb
); 1140 if(memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) { 1147 other
=unix_peer(sk
); 1149 if(other
->dead
|| (sk
->shutdown
& SEND_SHUTDOWN
)) 1154 if(!(msg
->msg_flags
&MSG_NOSIGNAL
)) 1155 send_sig(SIGPIPE
,current
,0); 1159 skb_queue_tail(&other
->receive_queue
, skb
); 1160 other
->data_ready(other
,size
); 1168 * Sleep until data has arrive. But check for races.. 1171 static voidunix_data_wait(unix_socket
* sk
) 1173 if(!skb_peek(&sk
->receive_queue
)) 1175 sk
->socket
->flags
|= SO_WAITDATA
; 1176 interruptible_sleep_on(sk
->sleep
); 1177 sk
->socket
->flags
&= ~SO_WAITDATA
; 1181 static intunix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1182 int flags
,struct scm_cookie
*scm
) 1184 struct sock
*sk
= sock
->sk
; 1185 int noblock
= flags
& MSG_DONTWAIT
; 1186 struct sk_buff
*skb
; 1192 msg
->msg_namelen
=0; 1194 skb
=skb_recv_datagram(sk
, flags
, noblock
, &err
); 1199 * sysctl_unix_max_dgram_qlen may change over the time we blocked 1200 * in the waitqueue so we must wakeup every time we shrink the 1201 * receiver queue. -arca 1203 wake_up_interruptible(&unix_dgram_wqueue
); 1207 msg
->msg_namelen
=sizeof(short); 1208 if(skb
->sk
->protinfo
.af_unix
.addr
) 1210 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1211 memcpy(msg
->msg_name
, 1212 skb
->sk
->protinfo
.af_unix
.addr
->name
, 1213 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1219 else if(size
< skb
->len
) 1220 msg
->msg_flags
|= MSG_TRUNC
; 1222 err
=skb_copy_datagram_iovec(skb
,0, msg
->msg_iov
, size
); 1226 scm
->creds
= *UNIXCREDS(skb
); 1228 if(!(flags
& MSG_PEEK
)) 1231 unix_detach_fds(scm
, skb
); 1235 /* It is questionable: on PEEK we could: 1236 - do not return fds - good, but too simple 8) 1237 - return fds, and do not return them on read (old strategy, 1239 - clone fds (I choosed it for now, it is the most universal 1242 POSIX 1003.1g does not actually define this clearly 1243 at all. POSIX 1003.1g doesn't define a lot of things 1248 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1253 skb_free_datagram(sk
,skb
); 1259 static intunix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1260 int flags
,struct scm_cookie
*scm
) 1262 struct sock
*sk
= sock
->sk
; 1263 int noblock
= flags
& MSG_DONTWAIT
; 1264 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1269 if(sock
->flags
& SO_ACCEPTCON
) 1274 if(flags
&MSG_WAITALL
) 1278 msg
->msg_namelen
=0; 1280 /* Lock the socket to prevent queue disordering 1281 * while sleeps in memcpy_tomsg 1284 down(&sk
->protinfo
.af_unix
.readsem
); 1289 struct sk_buff
*skb
; 1291 skb
=skb_dequeue(&sk
->receive_queue
); 1294 if(copied
>= target
) 1298 * POSIX 1003.1g mandates this order. 1303 up(&sk
->protinfo
.af_unix
.readsem
); 1304 returnsock_error(sk
); 1307 if(sk
->shutdown
& RCV_SHUTDOWN
) 1309 up(&sk
->protinfo
.af_unix
.readsem
); 1313 if(signal_pending(current
)) 1315 down(&sk
->protinfo
.af_unix
.readsem
); 1319 /* Never glue messages from different writers */ 1321 memcmp(UNIXCREDS(skb
), &scm
->creds
,sizeof(scm
->creds
)) !=0) 1323 skb_queue_head(&sk
->receive_queue
, skb
); 1327 /* Copy address just once */ 1330 msg
->msg_namelen
=sizeof(short); 1331 if(skb
->sk
->protinfo
.af_unix
.addr
) 1333 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1335 skb
->sk
->protinfo
.af_unix
.addr
->name
, 1336 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1341 chunk
=min(skb
->len
, size
); 1342 if(memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) { 1343 skb_queue_head(&sk
->receive_queue
, skb
); 1351 /* Copy credentials */ 1352 scm
->creds
= *UNIXCREDS(skb
); 1355 /* Mark read part of skb as used */ 1356 if(!(flags
& MSG_PEEK
)) 1358 skb_pull(skb
, chunk
); 1361 unix_detach_fds(scm
, skb
); 1363 /* put the skb back if we didn't use it up.. */ 1366 skb_queue_head(&sk
->receive_queue
, skb
); 1377 /* It is questionable, see note in unix_dgram_recvmsg. 1381 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1383 /* put message back and return */ 1384 skb_queue_head(&sk
->receive_queue
, skb
); 1389 up(&sk
->protinfo
.af_unix
.readsem
); 1393 static intunix_shutdown(struct socket
*sock
,int mode
) 1395 struct sock
*sk
= sock
->sk
; 1396 unix_socket
*other
=unix_peer(sk
); 1398 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
); 1401 sk
->shutdown
|= mode
; 1402 sk
->state_change(sk
); 1403 if(other
&& sk
->type
== SOCK_STREAM
&& 1404 unix_our_peer(sk
, other
)) { 1407 if(mode
&RCV_SHUTDOWN
) 1408 peer_mode
|= SEND_SHUTDOWN
; 1409 if(mode
&SEND_SHUTDOWN
) 1410 peer_mode
|= RCV_SHUTDOWN
; 1411 other
->shutdown
|= peer_mode
; 1412 other
->state_change(other
); 1419 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1421 struct sock
*sk
= sock
->sk
; 1428 amount
= sk
->sndbuf
-atomic_read(&sk
->wmem_alloc
); 1431 returnput_user(amount
, (int*)arg
); 1434 struct sk_buff
*skb
; 1435 if(sk
->state
==TCP_LISTEN
) 1438 * These two are safe on current systems as 1439 * only user tasks fiddle here 1441 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1443 returnput_user(amount
, (int*)arg
); 1453 static unsigned intunix_poll(struct file
* file
,struct socket
*sock
, poll_table
*wait
) 1455 struct sock
*sk
= sock
->sk
; 1458 poll_wait(file
, sk
->sleep
, wait
); 1461 /* exceptional events? */ 1464 if(sk
->shutdown
& RCV_SHUTDOWN
) 1468 if(!skb_queue_empty(&sk
->receive_queue
)) 1469 mask
|= POLLIN
| POLLRDNORM
; 1471 /* Connection-based need to check for termination and startup */ 1472 if(sk
->type
== SOCK_STREAM
&& sk
->state
==TCP_CLOSE
) 1476 * we set writable also when the other side has shut down the 1477 * connection. This prevents stuck sockets. 1479 if(sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
) 1480 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
; 1485 static voidunix_stream_write_space(struct sock
*sk
) 1489 wake_up_interruptible(sk
->sleep
); 1490 if(sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
) 1491 sock_wake_async(sk
->socket
,2); 1494 #ifdef CONFIG_PROC_FS 1495 static intunix_read_proc(char*buffer
,char**start
, off_t offset
, 1496 int length
,int*eof
,void*data
) 1504 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1507 forall_unix_sockets(i
,s
) 1509 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X %5ld", 1511 atomic_read(&s
->sock_readers
), 1513 s
->socket
? s
->socket
->flags
:0, 1515 s
->socket
? s
->socket
->state
: 1516 (s
->state
== TCP_ESTABLISHED
? 1517 SS_CONNECTING
: SS_DISCONNECTING
), 1518 s
->socket
? s
->socket
->inode
->i_ino
:0); 1520 if(s
->protinfo
.af_unix
.addr
) 1523 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
, 1524 s
->protinfo
.af_unix
.addr
->len
-sizeof(short)); 1525 if(!UNIX_ABSTRACT(s
)) 1529 len
+= s
->protinfo
.af_unix
.addr
->len
-sizeof(short); 1539 if(pos
>offset
+length
) 1544 *start
=buffer
+(offset
-begin
); 1545 len
-=(offset
-begin
); 1554 struct proto_ops unix_stream_ops
= { 1560 unix_stream_connect
, 1571 unix_stream_sendmsg
, 1575 struct proto_ops unix_dgram_ops
= { 1596 struct net_proto_family unix_family_ops
= { 1602 #ifdef CONFIG_SYSCTL 1603 externvoidunix_sysctl_register(void); 1604 externvoidunix_sysctl_unregister(void); 1607 intinit_module(void) 1609 __initfunc(voidunix_proto_init(struct net_proto
*pro
)) 1612 struct sk_buff
*dummy_skb
; 1613 struct proc_dir_entry
*ent
; 1615 printk(KERN_INFO
"NET4: Unix domain sockets 1.0 for Linux NET4.0.\n"); 1616 if(sizeof(struct unix_skb_parms
) >sizeof(dummy_skb
->cb
)) 1618 printk(KERN_CRIT
"unix_proto_init: panic\n"); 1625 sock_register(&unix_family_ops
); 1626 #ifdef CONFIG_PROC_FS 1627 ent
=create_proc_entry("net/unix",0,0); 1628 ent
->read_proc
= unix_read_proc
; 1632 #ifdef CONFIG_SYSCTL 1633 unix_sysctl_register(); 1641 voidcleanup_module(void) 1643 sock_unregister(PF_UNIX
); 1644 #ifdef CONFIG_SYSCTL 1645 unix_sysctl_unregister(); 1647 #ifdef CONFIG_PROC_FS 1648 remove_proc_entry("net/unix",0); 1655 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"