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.78 1999/05/27 00:38:41 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> 106 #include <linux/smp_lock.h> 108 #include <asm/checksum.h> 110 #define min(a,b) (((a)<(b))?(a):(b)) 112 int sysctl_unix_delete_delay
= HZ
; 113 int sysctl_unix_destroy_delay
=10*HZ
; 114 int sysctl_unix_max_dgram_qlen
=10; 116 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1]; 117 static atomic_t unix_nr_socks
=ATOMIC_INIT(0); 118 staticDECLARE_WAIT_QUEUE_HEAD(unix_ack_wqueue
); 119 staticDECLARE_WAIT_QUEUE_HEAD(unix_dgram_wqueue
); 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) 125 static voidunix_destroy_socket(unix_socket
*sk
); 126 static voidunix_stream_write_space(struct sock
*sk
); 128 extern __inline__
unsignedunix_hash_fold(unsigned hash
) 136 #define unix_peer(sk) ((sk)->pair) 138 extern __inline__
intunix_our_peer(unix_socket
*sk
, unix_socket
*osk
) 140 returnunix_peer(osk
) == sk
; 143 extern __inline__
intunix_may_send(unix_socket
*sk
, unix_socket
*osk
) 145 return(unix_peer(osk
) == NULL
||unix_our_peer(sk
, osk
)); 148 #define ulock(sk) (&(sk->protinfo.af_unix.user_count)) 150 extern __inline__
voidunix_lock(unix_socket
*sk
) 152 atomic_inc(ulock(sk
)); 155 extern __inline__
voidunix_unlock(unix_socket
*sk
) 157 atomic_dec(ulock(sk
)); 160 extern __inline__
intunix_locked(unix_socket
*sk
) 162 return(atomic_read(ulock(sk
)) !=0); 165 extern __inline__
voidunix_release_addr(struct unix_address
*addr
) 169 if(atomic_dec_and_test(&addr
->refcnt
)) 174 static voidunix_destruct_addr(struct sock
*sk
) 176 struct unix_address
*addr
= sk
->protinfo
.af_unix
.addr
; 178 unix_release_addr(addr
); 182 * Check unix socket name: 183 * - should be not zero length. 184 * - if started by not zero, should be NULL terminated (FS object) 185 * - if started by zero, it is abstract name. 188 static intunix_mkname(struct sockaddr_un
* sunaddr
,int len
,unsigned*hashp
) 190 if(len
<=sizeof(short) || len
>sizeof(*sunaddr
)) 192 if(!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
) 194 if(sunaddr
->sun_path
[0]) 197 * This may look like an off by one error but it is 198 * a bit more subtle. 108 is the longest valid AF_UNIX 199 * path for a binding. sun_path[108] doesnt as such 200 * exist. However in kernel space we are guaranteed that 201 * it is a valid memory location in our kernel 204 if(len
>sizeof(*sunaddr
)) 205 len
=sizeof(*sunaddr
); 206 ((char*)sunaddr
)[len
]=0; 207 len
=strlen(sunaddr
->sun_path
)+1+sizeof(short); 211 *hashp
=unix_hash_fold(csum_partial((char*)sunaddr
, len
,0)); 215 static voidunix_remove_socket(unix_socket
*sk
) 217 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 219 sk
->next
->prev
= sk
->prev
; 221 sk
->prev
->next
= sk
->next
; 224 sk
->protinfo
.af_unix
.list
= NULL
; 229 static voidunix_insert_socket(unix_socket
*sk
) 231 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 239 static unix_socket
*unix_find_socket_byname(struct sockaddr_un
*sunname
, 240 int len
,int type
,unsigned hash
) 244 for(s
=unix_socket_table
[(hash
^type
)&0xF]; s
; s
=s
->next
) 246 if(s
->protinfo
.af_unix
.addr
->len
==len
&& 247 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) ==0&& 257 static unix_socket
*unix_find_socket_byinode(struct inode
*i
) 261 for(s
=unix_socket_table
[i
->i_ino
&0xF]; s
; s
=s
->next
) 263 struct dentry
*dentry
= s
->protinfo
.af_unix
.dentry
; 265 if(dentry
&& dentry
->d_inode
== i
) 275 * Delete a unix socket. We have to allow for deferring this on a timer. 278 static voidunix_destroy_timer(unsigned long data
) 280 unix_socket
*sk
=(unix_socket
*)data
; 281 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 283 atomic_dec(&unix_nr_socks
); 287 /* socket destroyed, decrement count */ 296 sk
->timer
.expires
=jiffies
+sysctl_unix_destroy_delay
;/* No real hurry try it every 10 seconds or so */ 297 add_timer(&sk
->timer
); 301 static voidunix_delayed_delete(unix_socket
*sk
) 303 sk
->timer
.data
=(unsigned long)sk
; 304 sk
->timer
.expires
=jiffies
+sysctl_unix_delete_delay
;/* Normally 1 second after will clean up. After that we try every 10 */ 305 sk
->timer
.function
=unix_destroy_timer
; 306 add_timer(&sk
->timer
); 309 static intunix_release_sock(unix_socket
*sk
) 313 sk
->state_change(sk
); 317 if(sk
->state
== TCP_LISTEN
) 318 wake_up_interruptible(&unix_ack_wqueue
); 319 if(sk
->type
== SOCK_DGRAM
) 320 wake_up_interruptible(&unix_dgram_wqueue
); 322 skpair
=unix_peer(sk
); 326 if(sk
->type
==SOCK_STREAM
&&unix_our_peer(sk
, skpair
)) 328 skpair
->data_ready(skpair
,0); 329 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes*/ 331 unix_unlock(skpair
);/* It may now die */ 334 /* Try to flush out this socket. Throw out buffers at least */ 335 unix_destroy_socket(sk
); 338 * Fixme: BSD difference: In BSD all sockets connected to use get 339 * ECONNRESET and we die on the spot. In Linux we behave 340 * like files and pipes do and wait for the last 343 * Can't we simply set sock->err? 345 * What the above comment does talk about? --ANK(980817) 348 unix_gc();/* Garbage collect fds */ 352 static voidunix_destroy_socket(unix_socket
*sk
) 356 unix_remove_socket(sk
); 358 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 360 if(sk
->state
==TCP_LISTEN
) 361 unix_release_sock(skb
->sk
); 362 /* passed fds are erased in the kfree_skb hook */ 366 if(sk
->protinfo
.af_unix
.dentry
!=NULL
) 368 dput(sk
->protinfo
.af_unix
.dentry
); 369 sk
->protinfo
.af_unix
.dentry
=NULL
; 372 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 374 atomic_dec(&unix_nr_socks
); 378 /* socket destroyed, decrement count */ 385 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 390 static intunix_listen(struct socket
*sock
,int backlog
) 392 struct sock
*sk
= sock
->sk
; 394 if(sock
->state
!= SS_UNCONNECTED
) 396 if(sock
->type
!=SOCK_STREAM
) 397 return-EOPNOTSUPP
;/* Only stream sockets accept */ 398 if(!sk
->protinfo
.af_unix
.addr
) 399 return-EINVAL
;/* No listens on an unbound socket */ 400 if((unsigned) backlog
> SOMAXCONN
) 402 sk
->max_ack_backlog
=backlog
; 403 sk
->state
=TCP_LISTEN
; 404 sock
->flags
|= SO_ACCEPTCON
; 405 /* set credentials so connect can copy them */ 406 sk
->peercred
.pid
= current
->pid
; 407 sk
->peercred
.uid
= current
->euid
; 408 sk
->peercred
.gid
= current
->egid
; 412 externstruct proto_ops unix_stream_ops
; 413 externstruct proto_ops unix_dgram_ops
; 415 static struct sock
*unix_create1(struct socket
*sock
,int stream
) 419 if(atomic_read(&unix_nr_socks
) >=2*max_files
) 423 sk
=sk_alloc(PF_UNIX
, GFP_KERNEL
,1); 429 atomic_inc(&unix_nr_socks
); 431 sock_init_data(sock
,sk
); 434 sk
->write_space
= unix_stream_write_space
; 436 sk
->destruct
= unix_destruct_addr
; 437 sk
->protinfo
.af_unix
.family
=PF_UNIX
; 438 sk
->protinfo
.af_unix
.dentry
=NULL
; 439 init_MUTEX(&sk
->protinfo
.af_unix
.readsem
);/* single task reading lock */ 440 sk
->protinfo
.af_unix
.list
=&unix_sockets_unbound
; 441 unix_insert_socket(sk
); 446 static intunix_create(struct socket
*sock
,int protocol
) 450 if(protocol
&& protocol
!= PF_UNIX
) 451 return-EPROTONOSUPPORT
; 453 sock
->state
= SS_UNCONNECTED
; 457 sock
->ops
= &unix_stream_ops
; 461 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 465 sock
->type
=SOCK_DGRAM
; 467 sock
->ops
= &unix_dgram_ops
; 470 return-ESOCKTNOSUPPORT
; 473 returnunix_create1(sock
, stream
) ?0: -ENOMEM
; 476 static intunix_release(struct socket
*sock
,struct socket
*peer
) 478 unix_socket
*sk
= sock
->sk
; 484 if(sock
->state
!= SS_UNCONNECTED
) 485 sock
->state
= SS_DISCONNECTING
; 487 returnunix_release_sock(sk
); 490 static intunix_autobind(struct socket
*sock
) 492 struct sock
*sk
= sock
->sk
; 493 static u32 ordernum
=1; 494 struct unix_address
* addr
; 497 addr
=kmalloc(sizeof(*addr
) +sizeof(short) +16, GFP_KERNEL
); 500 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
) 505 memset(addr
,0,sizeof(*addr
) +sizeof(short) +16); 506 addr
->name
->sun_family
= AF_UNIX
; 507 atomic_set(&addr
->refcnt
,1); 510 addr
->len
=sprintf(addr
->name
->sun_path
+1,"%08x", ordernum
) +1+sizeof(short); 511 addr
->hash
=unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
,0)); 514 if((osk
=unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
, 515 addr
->hash
)) != NULL
) 521 sk
->protinfo
.af_unix
.addr
= addr
; 522 unix_remove_socket(sk
); 523 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(addr
->hash
^ sk
->type
)&0xF]; 524 unix_insert_socket(sk
); 528 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
,int len
, 529 int type
,unsigned hash
,int*error
) 533 if(sunname
->sun_path
[0]) 535 struct dentry
*dentry
; 536 dentry
=open_namei(sunname
->sun_path
,2, S_IFSOCK
); 538 *error
=PTR_ERR(dentry
); 541 u
=unix_find_socket_byinode(dentry
->d_inode
); 543 if(u
&& u
->type
!= type
) 551 u
=unix_find_socket_byname(sunname
, len
, type
, hash
); 555 *error
=-ECONNREFUSED
; 562 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 564 struct sock
*sk
= sock
->sk
; 565 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 566 struct dentry
* dentry
; 569 struct unix_address
*addr
; 571 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
|| 572 sunaddr
->sun_family
!= AF_UNIX
) 575 if(addr_len
==sizeof(short)) 576 returnunix_autobind(sock
); 578 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 582 addr
=kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
); 586 /* We slept; recheck ... */ 588 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
) 591 return-EINVAL
;/* Already bound */ 594 memcpy(addr
->name
, sunaddr
, addr_len
); 595 addr
->len
= addr_len
; 597 atomic_set(&addr
->refcnt
,1); 599 if(!sunaddr
->sun_path
[0]) 601 unix_socket
*osk
=unix_find_socket_byname(sunaddr
, addr_len
, 609 unix_remove_socket(sk
); 610 sk
->protinfo
.af_unix
.addr
= addr
; 611 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(hash
^sk
->type
)&0xF]; 612 unix_insert_socket(sk
); 616 addr
->hash
= UNIX_HASH_SIZE
; 617 sk
->protinfo
.af_unix
.addr
= addr
; 620 dentry
=do_mknod(sunaddr
->sun_path
, S_IFSOCK
|sock
->inode
->i_mode
,0); 623 err
=PTR_ERR(dentry
); 624 unix_release_addr(addr
); 625 sk
->protinfo
.af_unix
.addr
= NULL
; 631 unix_remove_socket(sk
); 632 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[dentry
->d_inode
->i_ino
&0xF]; 633 sk
->protinfo
.af_unix
.dentry
= dentry
; 634 unix_insert_socket(sk
); 639 static intunix_dgram_connect(struct socket
*sock
,struct sockaddr
*addr
, 642 struct sock
*sk
= sock
->sk
; 643 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
; 649 * 1003.1g breaking connected state with AF_UNSPEC 652 if(addr
->sa_family
==AF_UNSPEC
) 656 unix_unlock(unix_peer(sk
)); 657 unix_peer(sk
) = NULL
; 658 sock
->state
=SS_UNCONNECTED
; 663 alen
=unix_mkname(sunaddr
, alen
, &hash
); 667 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
); 670 if(!unix_may_send(sk
, other
)) 677 * If it was connected, reconnect. 681 unix_unlock(unix_peer(sk
)); 685 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 690 static intunix_stream_connect(struct socket
*sock
,struct sockaddr
*uaddr
, 691 int addr_len
,int flags
) 693 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 694 struct sock
*sk
= sock
->sk
, *newsk
; 700 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 704 /* First of all allocate resources. 705 If we will make it after state checks, 706 we will have to recheck all again in any case. 710 /* Find listening sock */ 711 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
); 716 while(other
->ack_backlog
>= other
->max_ack_backlog
) { 718 if(other
->dead
|| other
->state
!= TCP_LISTEN
) 720 if(flags
& O_NONBLOCK
) 722 interruptible_sleep_on(&unix_ack_wqueue
); 723 if(signal_pending(current
)) 728 /* create new sock for complete connection */ 729 newsk
=unix_create1(NULL
,1); 731 /* Allocate skb for sending to listening sock */ 734 skb
=sock_wmalloc(newsk
,1,0, GFP_KERNEL
); 739 /* This is ok... continue with connect */ 742 /* Socket is already connected */ 751 if(sk
->state
!= TCP_CLOSE
) 754 /* Check that listener is in valid state. */ 756 if(other
->dead
|| other
->state
!= TCP_LISTEN
) 760 if(newsk
== NULL
|| skb
== NULL
) 763 UNIXCB(skb
).attr
= MSG_SYN
; 765 /* set up connecting socket */ 766 sock
->state
=SS_CONNECTED
; 767 if(!sk
->protinfo
.af_unix
.addr
) 771 sk
->state
=TCP_ESTABLISHED
; 772 /* Set credentials */ 773 sk
->peercred
= other
->peercred
; 775 /* set up newly created sock */ 778 newsk
->state
=TCP_ESTABLISHED
; 779 newsk
->type
=SOCK_STREAM
; 780 newsk
->peercred
.pid
= current
->pid
; 781 newsk
->peercred
.uid
= current
->euid
; 782 newsk
->peercred
.gid
= current
->egid
; 784 /* copy address information from listening to new sock*/ 785 if(other
->protinfo
.af_unix
.addr
) 787 atomic_inc(&other
->protinfo
.af_unix
.addr
->refcnt
); 788 newsk
->protinfo
.af_unix
.addr
=other
->protinfo
.af_unix
.addr
; 790 if(other
->protinfo
.af_unix
.dentry
) 791 newsk
->protinfo
.af_unix
.dentry
=dget(other
->protinfo
.af_unix
.dentry
); 793 /* send info to listening sock */ 794 other
->ack_backlog
++; 795 skb_queue_tail(&other
->receive_queue
,skb
); 796 other
->data_ready(other
,0);/* Wake up ! */ 804 unix_destroy_socket(newsk
); 810 static intunix_socketpair(struct socket
*socka
,struct socket
*sockb
) 812 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
; 814 /* Join our sockets back to back */ 820 if(ska
->type
!= SOCK_DGRAM
) 822 ska
->state
=TCP_ESTABLISHED
; 823 skb
->state
=TCP_ESTABLISHED
; 824 socka
->state
=SS_CONNECTED
; 825 sockb
->state
=SS_CONNECTED
; 830 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 832 unix_socket
*sk
= sock
->sk
; 833 unix_socket
*newsk
= newsock
->sk
; 837 if(sock
->state
!= SS_UNCONNECTED
) 839 if(!(sock
->flags
& SO_ACCEPTCON
)) 842 if(sock
->type
!=SOCK_STREAM
) 844 if(sk
->state
!=TCP_LISTEN
) 849 skb
=skb_dequeue(&sk
->receive_queue
); 854 interruptible_sleep_on(sk
->sleep
); 855 if(signal_pending(current
)) 859 if(!(UNIXCB(skb
).attr
& MSG_SYN
)) 862 tsk
->state_change(tsk
); 867 if(sk
->max_ack_backlog
== sk
->ack_backlog
--) 868 wake_up_interruptible(&unix_ack_wqueue
); 874 /* attach accepted sock to socket */ 875 newsock
->state
=SS_CONNECTED
; 877 tsk
->sleep
=newsk
->sleep
; 880 /* destroy handed sock */ 881 newsk
->socket
= NULL
; 882 unix_destroy_socket(newsk
); 888 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 890 struct sock
*sk
= sock
->sk
; 891 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 899 if(!sk
->protinfo
.af_unix
.addr
) 901 sunaddr
->sun_family
= AF_UNIX
; 902 sunaddr
->sun_path
[0] =0; 903 *uaddr_len
=sizeof(short); 904 return0;/* Not bound */ 906 *uaddr_len
= sk
->protinfo
.af_unix
.addr
->len
; 907 memcpy(sunaddr
, sk
->protinfo
.af_unix
.addr
->name
, *uaddr_len
); 911 static voidunix_detach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 915 scm
->fp
=UNIXCB(skb
).fp
; 916 skb
->destructor
= sock_wfree
; 917 UNIXCB(skb
).fp
= NULL
; 919 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 920 unix_notinflight(scm
->fp
->fp
[i
]); 923 static voidunix_destruct_fds(struct sk_buff
*skb
) 925 struct scm_cookie scm
; 926 memset(&scm
,0,sizeof(scm
)); 927 unix_detach_fds(&scm
, skb
); 932 static voidunix_attach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 935 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 936 unix_inflight(scm
->fp
->fp
[i
]); 937 UNIXCB(skb
).fp
= scm
->fp
; 938 skb
->destructor
= unix_destruct_fds
; 947 static intdo_unix_dgram_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 948 struct scm_cookie
*scm
) 950 struct sock
*sk
= sock
->sk
; 951 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 953 int namelen
=0;/* fake GCC */ 958 if(msg
->msg_flags
&MSG_OOB
) 961 if(msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
)) 964 if(msg
->msg_namelen
) { 965 namelen
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 974 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 977 skb
=sock_alloc_send_skb(sk
, len
,0, msg
->msg_flags
&MSG_DONTWAIT
, &err
); 981 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 982 UNIXCB(skb
).attr
= msg
->msg_flags
; 984 unix_attach_fds(scm
, skb
); 986 skb
->h
.raw
= skb
->data
; 987 err
=memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 991 other
=unix_peer(sk
); 992 if(other
&& other
->dead
) 995 * Check with 1003.1g - what should 1008 other
=unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
); 1012 if(!unix_may_send(sk
, other
)) 1016 while(skb_queue_len(&other
->receive_queue
) >= 1017 sysctl_unix_max_dgram_qlen
) 1019 if(sock
->file
->f_flags
& O_NONBLOCK
) 1024 interruptible_sleep_on(&unix_dgram_wqueue
); 1027 if(sk
->shutdown
& SEND_SHUTDOWN
) 1032 if(signal_pending(current
)) 1039 skb_queue_tail(&other
->receive_queue
, skb
); 1040 other
->data_ready(other
,len
); 1055 static intunix_dgram_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 1056 struct scm_cookie
*scm
) 1061 retval
=do_unix_dgram_sendmsg(sock
, msg
, len
, scm
); 1066 static intdo_unix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 1067 struct scm_cookie
*scm
) 1069 struct sock
*sk
= sock
->sk
; 1071 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1073 struct sk_buff
*skb
; 1077 if(sock
->flags
& SO_ACCEPTCON
) 1080 if(msg
->msg_flags
&MSG_OOB
) 1083 if(msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
)) 1086 if(msg
->msg_namelen
) { 1087 if(sk
->state
==TCP_ESTABLISHED
) 1097 if(sk
->shutdown
&SEND_SHUTDOWN
) { 1098 if(!(msg
->msg_flags
&MSG_NOSIGNAL
)) 1099 send_sig(SIGPIPE
,current
,0); 1106 * Optimisation for the fact that under 0.01% of X messages typically 1112 /* Keep two messages in the pipe so it schedules better */ 1113 if(size
> sk
->sndbuf
/2-16) 1114 size
= sk
->sndbuf
/2-16; 1117 * Keep to page sized kmalloc()'s as various people 1118 * have suggested. Big mallocs stress the vm too 1123 limit
=4096-16;/* Fall back to a page if we can't grab a big buffer this instant */ 1125 limit
=0;/* Otherwise just grab and wait */ 1131 skb
=sock_alloc_send_skb(sk
,size
,limit
,msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1141 * If you pass two values to the sock_alloc_send_skb 1142 * it tries to grab the large buffer with GFP_BUFFER 1143 * (which can fail easily), and if it fails grab the 1144 * fallback size buffer which is under a page and will 1147 size
=min(size
,skb_tailroom(skb
)); 1149 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1150 UNIXCB(skb
).attr
= msg
->msg_flags
; 1152 unix_attach_fds(scm
, skb
); 1154 if(memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) { 1161 other
=unix_peer(sk
); 1163 if(other
->dead
|| (sk
->shutdown
& SEND_SHUTDOWN
)) 1168 if(!(msg
->msg_flags
&MSG_NOSIGNAL
)) 1169 send_sig(SIGPIPE
,current
,0); 1174 skb_queue_tail(&other
->receive_queue
, skb
); 1175 other
->data_ready(other
,size
); 1182 static intunix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 1183 struct scm_cookie
*scm
) 1188 retval
=do_unix_stream_sendmsg(sock
, msg
, len
, scm
); 1194 * Sleep until data has arrive. But check for races.. 1197 static voidunix_data_wait(unix_socket
* sk
) 1199 if(!skb_peek(&sk
->receive_queue
)) 1201 sk
->socket
->flags
|= SO_WAITDATA
; 1202 interruptible_sleep_on(sk
->sleep
); 1203 sk
->socket
->flags
&= ~SO_WAITDATA
; 1207 static intdo_unix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1208 int flags
,struct scm_cookie
*scm
) 1210 struct sock
*sk
= sock
->sk
; 1211 int noblock
= flags
& MSG_DONTWAIT
; 1212 struct sk_buff
*skb
; 1218 msg
->msg_namelen
=0; 1220 skb
=skb_recv_datagram(sk
, flags
, noblock
, &err
); 1225 * sysctl_unix_max_dgram_qlen may change over the time we blocked 1226 * in the waitqueue so we must wakeup every time we shrink the 1227 * receiver queue. -arca 1229 wake_up_interruptible(&unix_dgram_wqueue
); 1233 msg
->msg_namelen
=sizeof(short); 1234 if(skb
->sk
->protinfo
.af_unix
.addr
) 1236 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1237 memcpy(msg
->msg_name
, 1238 skb
->sk
->protinfo
.af_unix
.addr
->name
, 1239 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1245 else if(size
< skb
->len
) 1246 msg
->msg_flags
|= MSG_TRUNC
; 1248 err
=skb_copy_datagram_iovec(skb
,0, msg
->msg_iov
, size
); 1252 scm
->creds
= *UNIXCREDS(skb
); 1254 if(!(flags
& MSG_PEEK
)) 1257 unix_detach_fds(scm
, skb
); 1261 /* It is questionable: on PEEK we could: 1262 - do not return fds - good, but too simple 8) 1263 - return fds, and do not return them on read (old strategy, 1265 - clone fds (I choosed it for now, it is the most universal 1268 POSIX 1003.1g does not actually define this clearly 1269 at all. POSIX 1003.1g doesn't define a lot of things 1274 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1279 skb_free_datagram(sk
,skb
); 1284 static intunix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1285 int flags
,struct scm_cookie
*scm
) 1290 retval
=do_unix_dgram_recvmsg(sock
, msg
, size
, flags
, scm
); 1295 static intdo_unix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1296 int flags
,struct scm_cookie
*scm
) 1298 struct sock
*sk
= sock
->sk
; 1299 int noblock
= flags
& MSG_DONTWAIT
; 1300 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1305 if(sock
->flags
& SO_ACCEPTCON
) 1310 if(flags
&MSG_WAITALL
) 1313 msg
->msg_namelen
=0; 1315 /* Lock the socket to prevent queue disordering 1316 * while sleeps in memcpy_tomsg 1319 down(&sk
->protinfo
.af_unix
.readsem
); 1324 struct sk_buff
*skb
; 1326 skb
=skb_dequeue(&sk
->receive_queue
); 1329 if(copied
>= target
) 1333 * POSIX 1003.1g mandates this order. 1338 up(&sk
->protinfo
.af_unix
.readsem
); 1339 returnsock_error(sk
); 1342 if(sk
->shutdown
& RCV_SHUTDOWN
) 1344 up(&sk
->protinfo
.af_unix
.readsem
); 1348 if(signal_pending(current
)) 1350 down(&sk
->protinfo
.af_unix
.readsem
); 1354 /* Never glue messages from different writers */ 1356 memcmp(UNIXCREDS(skb
), &scm
->creds
,sizeof(scm
->creds
)) !=0) 1358 skb_queue_head(&sk
->receive_queue
, skb
); 1362 /* Copy address just once */ 1365 msg
->msg_namelen
=sizeof(short); 1366 if(skb
->sk
->protinfo
.af_unix
.addr
) 1368 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1370 skb
->sk
->protinfo
.af_unix
.addr
->name
, 1371 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1376 chunk
=min(skb
->len
, size
); 1377 if(memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) { 1378 skb_queue_head(&sk
->receive_queue
, skb
); 1386 /* Copy credentials */ 1387 scm
->creds
= *UNIXCREDS(skb
); 1390 /* Mark read part of skb as used */ 1391 if(!(flags
& MSG_PEEK
)) 1393 skb_pull(skb
, chunk
); 1396 unix_detach_fds(scm
, skb
); 1398 /* put the skb back if we didn't use it up.. */ 1401 skb_queue_head(&sk
->receive_queue
, skb
); 1412 /* It is questionable, see note in unix_dgram_recvmsg. 1416 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1418 /* put message back and return */ 1419 skb_queue_head(&sk
->receive_queue
, skb
); 1424 up(&sk
->protinfo
.af_unix
.readsem
); 1428 static intunix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1429 int flags
,struct scm_cookie
*scm
) 1434 retval
=do_unix_stream_recvmsg(sock
, msg
, size
, flags
, scm
); 1439 static intunix_shutdown(struct socket
*sock
,int mode
) 1441 struct sock
*sk
= sock
->sk
; 1442 unix_socket
*other
=unix_peer(sk
); 1444 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
); 1447 sk
->shutdown
|= mode
; 1448 sk
->state_change(sk
); 1449 if(other
&& sk
->type
== SOCK_STREAM
&& 1450 unix_our_peer(sk
, other
)) { 1453 if(mode
&RCV_SHUTDOWN
) 1454 peer_mode
|= SEND_SHUTDOWN
; 1455 if(mode
&SEND_SHUTDOWN
) 1456 peer_mode
|= RCV_SHUTDOWN
; 1457 other
->shutdown
|= peer_mode
; 1458 if(peer_mode
&RCV_SHUTDOWN
) 1459 other
->data_ready(other
,0); 1461 other
->state_change(other
); 1468 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1470 struct sock
*sk
= sock
->sk
; 1477 amount
= sk
->sndbuf
-atomic_read(&sk
->wmem_alloc
); 1480 returnput_user(amount
, (int*)arg
); 1483 struct sk_buff
*skb
; 1484 if(sk
->state
==TCP_LISTEN
) 1487 * These two are safe on current systems as 1488 * only user tasks fiddle here 1490 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1492 returnput_user(amount
, (int*)arg
); 1502 static unsigned intunix_poll(struct file
* file
,struct socket
*sock
, poll_table
*wait
) 1504 struct sock
*sk
= sock
->sk
; 1507 poll_wait(file
, sk
->sleep
, wait
); 1510 /* exceptional events? */ 1513 if(sk
->shutdown
& RCV_SHUTDOWN
) 1517 if(!skb_queue_empty(&sk
->receive_queue
)) 1518 mask
|= POLLIN
| POLLRDNORM
; 1520 /* Connection-based need to check for termination and startup */ 1521 if(sk
->type
== SOCK_STREAM
&& sk
->state
==TCP_CLOSE
) 1525 * we set writable also when the other side has shut down the 1526 * connection. This prevents stuck sockets. 1528 if(sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
) 1529 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
; 1534 static voidunix_stream_write_space(struct sock
*sk
) 1538 wake_up_interruptible(sk
->sleep
); 1539 if(sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
) 1540 sock_wake_async(sk
->socket
,2); 1543 #ifdef CONFIG_PROC_FS 1544 static intunix_read_proc(char*buffer
,char**start
, off_t offset
, 1545 int length
,int*eof
,void*data
) 1553 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1556 forall_unix_sockets(i
,s
) 1558 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X %5ld", 1560 atomic_read(ulock(s
)), 1562 s
->socket
? s
->socket
->flags
:0, 1564 s
->socket
? s
->socket
->state
: 1565 (s
->state
== TCP_ESTABLISHED
? 1566 SS_CONNECTING
: SS_DISCONNECTING
), 1567 s
->socket
? s
->socket
->inode
->i_ino
:0); 1569 if(s
->protinfo
.af_unix
.addr
) 1572 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
, 1573 s
->protinfo
.af_unix
.addr
->len
-sizeof(short)); 1574 if(!UNIX_ABSTRACT(s
)) 1578 len
+= s
->protinfo
.af_unix
.addr
->len
-sizeof(short); 1588 if(pos
>offset
+length
) 1593 *start
=buffer
+(offset
-begin
); 1594 len
-=(offset
-begin
); 1603 struct proto_ops unix_stream_ops
= { 1609 unix_stream_connect
, 1620 unix_stream_sendmsg
, 1624 struct proto_ops unix_dgram_ops
= { 1645 struct net_proto_family unix_family_ops
= { 1651 #ifdef CONFIG_SYSCTL 1652 externvoidunix_sysctl_register(void); 1653 externvoidunix_sysctl_unregister(void); 1656 intinit_module(void) 1658 __initfunc(voidunix_proto_init(struct net_proto
*pro
)) 1661 struct sk_buff
*dummy_skb
; 1662 struct proc_dir_entry
*ent
; 1664 printk(KERN_INFO
"NET4: Unix domain sockets 1.0 for Linux NET4.0.\n"); 1665 if(sizeof(struct unix_skb_parms
) >sizeof(dummy_skb
->cb
)) 1667 printk(KERN_CRIT
"unix_proto_init: panic\n"); 1674 sock_register(&unix_family_ops
); 1675 #ifdef CONFIG_PROC_FS 1676 ent
=create_proc_entry("net/unix",0,0); 1677 ent
->read_proc
= unix_read_proc
; 1681 #ifdef CONFIG_SYSCTL 1682 unix_sysctl_register(); 1690 voidcleanup_module(void) 1692 sock_unregister(PF_UNIX
); 1693 #ifdef CONFIG_SYSCTL 1694 unix_sysctl_unregister(); 1696 #ifdef CONFIG_PROC_FS 1697 remove_proc_entry("net/unix",0); 1704 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"