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.69 1998/08/28 01:15: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. 37 * Known differences from reference BSD that was tested: 40 * ECONNREFUSED is not returned from one end of a connected() socket to the 41 * other the moment one end closes. 42 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark 43 * and a fake inode identifier (nor the BSD first socket fstat twice bug). 45 * accept() returns a path name even if the connecting socket has closed 46 * in the meantime (BSD loses the path and gives up). 47 * accept() returns 0 length path for an unbound connector. BSD returns 16 48 * and a null first byte in the path (but not for gethost/peername - BSD bug ??) 49 * socketpair(...SOCK_RAW..) doesn't panic the kernel. 50 * BSD af_unix apparently has connect forgetting to block properly. 51 * (need to check this with the POSIX spec in detail) 53 * Differences from 2.0.0-11-... (ANK) 54 * Bug fixes and improvements. 55 * - client shutdown killed server socket. 56 * - removed all useless cli/sti pairs. 58 * Semantic changes/extensions. 59 * - generic control message passing. 60 * - SCM_CREDENTIALS control message. 61 * - "Abstract" (not FS based) socket bindings. 62 * Abstract names are sequences of bytes (not zero terminated) 63 * started by 0, so that this name space does not intersect 67 #include <linux/module.h> 68 #include <linux/config.h> 69 #include <linux/kernel.h> 70 #include <linux/major.h> 71 #include <linux/signal.h> 72 #include <linux/sched.h> 73 #include <linux/errno.h> 74 #include <linux/string.h> 75 #include <linux/stat.h> 76 #include <linux/socket.h> 78 #include <linux/fcntl.h> 79 #include <linux/termios.h> 80 #include <linux/socket.h> 81 #include <linux/sockios.h> 82 #include <linux/net.h> 85 #include <linux/malloc.h> 86 #include <asm/uaccess.h> 87 #include <linux/skbuff.h> 88 #include <linux/netdevice.h> 91 #include <net/af_unix.h> 92 #include <linux/proc_fs.h> 94 #include <linux/init.h> 95 #include <linux/poll.h> 97 #include <asm/checksum.h> 99 #define min(a,b) (((a)<(b))?(a):(b)) 101 int sysctl_unix_delete_delay
= HZ
; 102 int sysctl_unix_destroy_delay
=10*HZ
; 104 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1]; 106 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE]) 108 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE) 110 static voidunix_destroy_socket(unix_socket
*sk
); 111 static voidunix_stream_write_space(struct sock
*sk
); 113 extern __inline__
unsignedunix_hash_fold(unsigned hash
) 121 #define unix_peer(sk) ((sk)->pair) 123 extern __inline__
intunix_our_peer(unix_socket
*sk
, unix_socket
*osk
) 125 returnunix_peer(osk
) == sk
; 128 extern __inline__
intunix_may_send(unix_socket
*sk
, unix_socket
*osk
) 130 return(unix_peer(osk
) == NULL
||unix_our_peer(sk
, osk
)); 133 extern __inline__
voidunix_lock(unix_socket
*sk
) 135 atomic_inc(&sk
->sock_readers
); 138 extern __inline__
voidunix_unlock(unix_socket
*sk
) 140 atomic_dec(&sk
->sock_readers
); 143 extern __inline__
intunix_locked(unix_socket
*sk
) 145 returnatomic_read(&sk
->sock_readers
); 148 extern __inline__
voidunix_release_addr(struct unix_address
*addr
) 152 if(atomic_dec_and_test(&addr
->refcnt
)) 157 static voidunix_destruct_addr(struct sock
*sk
) 159 struct unix_address
*addr
= sk
->protinfo
.af_unix
.addr
; 161 unix_release_addr(addr
); 165 * Check unix socket name: 166 * - should be not zero length. 167 * - if started by not zero, should be NULL terminated (FS object) 168 * - if started by zero, it is abstract name. 171 static intunix_mkname(struct sockaddr_un
* sunaddr
,int len
,unsigned*hashp
) 173 if(len
<=sizeof(short) || len
>sizeof(*sunaddr
)) 175 if(!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
) 177 if(sunaddr
->sun_path
[0]) 180 * This may look like an off by one error but it is 181 * a bit more subtle. 108 is the longest valid AF_UNIX 182 * path for a binding. sun_path[108] doesnt as such 183 * exist. However in kernel space we are guaranteed that 184 * it is a valid memory location in our kernel 187 if(len
>sizeof(*sunaddr
)) 188 len
=sizeof(*sunaddr
); 189 ((char*)sunaddr
)[len
]=0; 190 len
=strlen(sunaddr
->sun_path
)+1+sizeof(short); 194 *hashp
=unix_hash_fold(csum_partial((char*)sunaddr
, len
,0)); 198 static voidunix_remove_socket(unix_socket
*sk
) 200 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 202 sk
->next
->prev
= sk
->prev
; 204 sk
->prev
->next
= sk
->next
; 207 sk
->protinfo
.af_unix
.list
= NULL
; 212 static voidunix_insert_socket(unix_socket
*sk
) 214 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 222 static unix_socket
*unix_find_socket_byname(struct sockaddr_un
*sunname
, 223 int len
,int type
,unsigned hash
) 227 for(s
=unix_socket_table
[(hash
^type
)&0xF]; s
; s
=s
->next
) 229 if(s
->protinfo
.af_unix
.addr
->len
==len
&& 230 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) ==0&& 240 static unix_socket
*unix_find_socket_byinode(struct inode
*i
) 244 for(s
=unix_socket_table
[i
->i_ino
&0xF]; s
; s
=s
->next
) 246 struct dentry
*dentry
= s
->protinfo
.af_unix
.dentry
; 248 if(dentry
&& dentry
->d_inode
== i
) 258 * Delete a unix socket. We have to allow for deferring this on a timer. 261 static voidunix_destroy_timer(unsigned long data
) 263 unix_socket
*sk
=(unix_socket
*)data
; 264 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 268 /* socket destroyed, decrement count */ 277 sk
->timer
.expires
=jiffies
+sysctl_unix_destroy_delay
;/* No real hurry try it every 10 seconds or so */ 278 add_timer(&sk
->timer
); 282 static voidunix_delayed_delete(unix_socket
*sk
) 284 sk
->timer
.data
=(unsigned long)sk
; 285 sk
->timer
.expires
=jiffies
+sysctl_unix_delete_delay
;/* Normally 1 second after will clean up. After that we try every 10 */ 286 sk
->timer
.function
=unix_destroy_timer
; 287 add_timer(&sk
->timer
); 290 static intunix_release_sock(unix_socket
*sk
) 294 sk
->state_change(sk
); 298 skpair
=unix_peer(sk
); 302 if(sk
->type
==SOCK_STREAM
&&unix_our_peer(sk
, skpair
)) 304 skpair
->state_change(skpair
); 305 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes*/ 307 unix_unlock(skpair
);/* It may now die */ 310 /* Try to flush out this socket. Throw out buffers at least */ 311 unix_destroy_socket(sk
); 314 * Fixme: BSD difference: In BSD all sockets connected to use get 315 * ECONNRESET and we die on the spot. In Linux we behave 316 * like files and pipes do and wait for the last 319 * Can't we simply set sock->err? 321 * What the above comment does talk about? --ANK(980817) 324 unix_gc();/* Garbage collect fds */ 328 static voidunix_destroy_socket(unix_socket
*sk
) 332 unix_remove_socket(sk
); 334 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 336 if(sk
->state
==TCP_LISTEN
) 337 unix_release_sock(skb
->sk
); 338 /* passed fds are erased in the kfree_skb hook */ 342 if(sk
->protinfo
.af_unix
.dentry
!=NULL
) 344 dput(sk
->protinfo
.af_unix
.dentry
); 345 sk
->protinfo
.af_unix
.dentry
=NULL
; 348 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 352 /* socket destroyed, decrement count */ 359 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 364 static intunix_listen(struct socket
*sock
,int backlog
) 366 struct sock
*sk
= sock
->sk
; 368 if(sock
->state
!= SS_UNCONNECTED
) 370 if(sock
->type
!=SOCK_STREAM
) 371 return-EOPNOTSUPP
;/* Only stream sockets accept */ 372 if(!sk
->protinfo
.af_unix
.addr
) 373 return-EINVAL
;/* No listens on an unbound socket */ 374 sk
->max_ack_backlog
=backlog
; 375 sk
->state
=TCP_LISTEN
; 376 sock
->flags
|= SO_ACCEPTCON
; 377 /* set credentials so connect can copy them */ 378 sk
->peercred
.pid
= current
->pid
; 379 sk
->peercred
.uid
= current
->euid
; 380 sk
->peercred
.gid
= current
->egid
; 384 externstruct proto_ops unix_stream_ops
; 385 externstruct proto_ops unix_dgram_ops
; 387 static struct sock
*unix_create1(struct socket
*sock
,int stream
) 392 sk
=sk_alloc(PF_UNIX
, GFP_KERNEL
,1); 398 sock_init_data(sock
,sk
); 401 sk
->write_space
= unix_stream_write_space
; 403 sk
->destruct
= unix_destruct_addr
; 404 sk
->protinfo
.af_unix
.family
=PF_UNIX
; 405 sk
->protinfo
.af_unix
.dentry
=NULL
; 406 sk
->protinfo
.af_unix
.readsem
=MUTEX
;/* single task reading lock */ 407 sk
->protinfo
.af_unix
.list
=&unix_sockets_unbound
; 408 unix_insert_socket(sk
); 413 static intunix_create(struct socket
*sock
,int protocol
) 417 if(protocol
&& protocol
!= PF_UNIX
) 418 return-EPROTONOSUPPORT
; 420 sock
->state
= SS_UNCONNECTED
; 424 sock
->ops
= &unix_stream_ops
; 428 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 432 sock
->type
=SOCK_DGRAM
; 434 sock
->ops
= &unix_dgram_ops
; 437 return-ESOCKTNOSUPPORT
; 440 returnunix_create1(sock
, stream
) ?0: -ENOMEM
; 443 static intunix_release(struct socket
*sock
,struct socket
*peer
) 445 unix_socket
*sk
= sock
->sk
; 451 if(sock
->state
!= SS_UNCONNECTED
) 452 sock
->state
= SS_DISCONNECTING
; 454 returnunix_release_sock(sk
); 457 static intunix_autobind(struct socket
*sock
) 459 struct sock
*sk
= sock
->sk
; 460 static u32 ordernum
=1; 461 struct unix_address
* addr
; 464 addr
=kmalloc(sizeof(*addr
) +sizeof(short) +16, GFP_KERNEL
); 467 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
) 472 memset(addr
,0,sizeof(*addr
) +sizeof(short) +16); 473 addr
->name
->sun_family
= AF_UNIX
; 474 atomic_set(&addr
->refcnt
,1); 477 addr
->len
=sprintf(addr
->name
->sun_path
+1,"%08x", ordernum
) +1+sizeof(short); 478 addr
->hash
=unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
,0)); 481 if((osk
=unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
, 482 addr
->hash
)) != NULL
) 488 sk
->protinfo
.af_unix
.addr
= addr
; 489 unix_remove_socket(sk
); 490 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(addr
->hash
^ sk
->type
)&0xF]; 491 unix_insert_socket(sk
); 495 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
,int len
, 496 int type
,unsigned hash
,int*error
) 500 if(sunname
->sun_path
[0]) 502 struct dentry
*dentry
; 503 dentry
=open_namei(sunname
->sun_path
,2, S_IFSOCK
); 505 *error
=PTR_ERR(dentry
); 508 u
=unix_find_socket_byinode(dentry
->d_inode
); 510 if(u
&& u
->type
!= type
) 518 u
=unix_find_socket_byname(sunname
, len
, type
, hash
); 522 *error
=-ECONNREFUSED
; 529 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 531 struct sock
*sk
= sock
->sk
; 532 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 533 struct dentry
* dentry
; 536 struct unix_address
*addr
; 538 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
|| 539 sunaddr
->sun_family
!= AF_UNIX
) 542 if(addr_len
==sizeof(short)) 543 returnunix_autobind(sock
); 545 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 549 addr
=kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
); 553 /* We slept; recheck ... */ 555 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.dentry
) 558 return-EINVAL
;/* Already bound */ 561 memcpy(addr
->name
, sunaddr
, addr_len
); 562 addr
->len
= addr_len
; 564 atomic_set(&addr
->refcnt
,1); 566 if(!sunaddr
->sun_path
[0]) 568 unix_socket
*osk
=unix_find_socket_byname(sunaddr
, addr_len
, 576 unix_remove_socket(sk
); 577 sk
->protinfo
.af_unix
.addr
= addr
; 578 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(hash
^sk
->type
)&0xF]; 579 unix_insert_socket(sk
); 583 addr
->hash
= UNIX_HASH_SIZE
; 584 sk
->protinfo
.af_unix
.addr
= addr
; 587 dentry
=do_mknod(sunaddr
->sun_path
, S_IFSOCK
|S_IRWXUGO
,0); 590 err
=PTR_ERR(dentry
); 591 unix_release_addr(addr
); 592 sk
->protinfo
.af_unix
.addr
= NULL
; 598 unix_remove_socket(sk
); 599 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[dentry
->d_inode
->i_ino
&0xF]; 600 sk
->protinfo
.af_unix
.dentry
= dentry
; 601 unix_insert_socket(sk
); 606 static intunix_dgram_connect(struct socket
*sock
,struct sockaddr
*addr
, 609 struct sock
*sk
= sock
->sk
; 610 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
; 616 * 1003.1g breaking connected state with AF_UNSPEC 619 if(addr
->sa_family
==AF_UNSPEC
) 623 unix_unlock(unix_peer(sk
)); 624 unix_peer(sk
) = NULL
; 625 sock
->state
=SS_UNCONNECTED
; 630 alen
=unix_mkname(sunaddr
, alen
, &hash
); 634 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
); 637 if(!unix_may_send(sk
, other
)) 644 * If it was connected, reconnect. 648 unix_unlock(unix_peer(sk
)); 652 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 657 static intunix_stream_connect(struct socket
*sock
,struct sockaddr
*uaddr
, 658 int addr_len
,int flags
) 660 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 661 struct sock
*sk
= sock
->sk
, *newsk
; 667 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 671 /* First of all allocate resources. 672 If we will make it after state checks, 673 we will have to recheck all again in any case. 676 /* Find listening sock */ 677 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
); 679 /* create new sock for complete connection */ 680 newsk
=unix_create1(NULL
,1); 682 /* Allocate skb for sending to listening sock */ 685 skb
=sock_wmalloc(newsk
,1,0, GFP_KERNEL
); 690 /* This is ok... continue with connect */ 693 /* Socket is already connected */ 702 if(sk
->state
!= TCP_CLOSE
) 705 /* Check that listener is in valid state. */ 707 if(other
== NULL
|| other
->dead
|| other
->state
!= TCP_LISTEN
) 711 if(newsk
== NULL
|| skb
== NULL
) 714 UNIXCB(skb
).attr
= MSG_SYN
; 716 /* set up connecting socket */ 717 sock
->state
=SS_CONNECTED
; 718 if(!sk
->protinfo
.af_unix
.addr
) 722 sk
->state
=TCP_ESTABLISHED
; 723 /* Set credentials */ 724 sk
->peercred
= other
->peercred
; 726 /* set up newly created sock */ 729 newsk
->state
=TCP_ESTABLISHED
; 730 newsk
->type
=SOCK_STREAM
; 731 newsk
->peercred
.pid
= current
->pid
; 732 newsk
->peercred
.uid
= current
->euid
; 733 newsk
->peercred
.gid
= current
->egid
; 735 /* copy address information from listening to new sock*/ 736 if(other
->protinfo
.af_unix
.addr
) 738 atomic_inc(&other
->protinfo
.af_unix
.addr
->refcnt
); 739 newsk
->protinfo
.af_unix
.addr
=other
->protinfo
.af_unix
.addr
; 741 if(other
->protinfo
.af_unix
.dentry
) 742 newsk
->protinfo
.af_unix
.dentry
=dget(other
->protinfo
.af_unix
.dentry
); 744 /* send info to listening sock */ 745 other
->ack_backlog
++; 746 skb_queue_tail(&other
->receive_queue
,skb
); 747 other
->data_ready(other
,0);/* Wake up ! */ 755 unix_destroy_socket(newsk
); 761 static intunix_socketpair(struct socket
*socka
,struct socket
*sockb
) 763 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
; 765 /* Join our sockets back to back */ 771 if(ska
->type
!= SOCK_DGRAM
) 773 ska
->state
=TCP_ESTABLISHED
; 774 skb
->state
=TCP_ESTABLISHED
; 775 socka
->state
=SS_CONNECTED
; 776 sockb
->state
=SS_CONNECTED
; 781 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 783 unix_socket
*sk
= sock
->sk
; 784 unix_socket
*newsk
= newsock
->sk
; 788 if(sock
->state
!= SS_UNCONNECTED
) 790 if(!(sock
->flags
& SO_ACCEPTCON
)) 793 if(sock
->type
!=SOCK_STREAM
) 795 if(sk
->state
!=TCP_LISTEN
) 800 skb
=skb_dequeue(&sk
->receive_queue
); 805 interruptible_sleep_on(sk
->sleep
); 806 if(signal_pending(current
)) 810 if(!(UNIXCB(skb
).attr
& MSG_SYN
)) 813 tsk
->state_change(tsk
); 822 unix_release_sock(tsk
); 826 /* attach accepted sock to socket */ 827 newsock
->state
=SS_CONNECTED
; 829 tsk
->sleep
=newsk
->sleep
; 832 /* destroy handed sock */ 833 newsk
->socket
= NULL
; 834 unix_destroy_socket(newsk
); 840 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 842 struct sock
*sk
= sock
->sk
; 843 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 851 if(!sk
->protinfo
.af_unix
.addr
) 853 sunaddr
->sun_family
= AF_UNIX
; 854 sunaddr
->sun_path
[0] =0; 855 *uaddr_len
=sizeof(short); 856 return0;/* Not bound */ 858 *uaddr_len
= sk
->protinfo
.af_unix
.addr
->len
; 859 memcpy(sunaddr
, sk
->protinfo
.af_unix
.addr
->name
, *uaddr_len
); 863 static voidunix_detach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 867 scm
->fp
=UNIXCB(skb
).fp
; 868 skb
->destructor
= sock_wfree
; 869 UNIXCB(skb
).fp
= NULL
; 871 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 872 unix_notinflight(scm
->fp
->fp
[i
]); 875 static voidunix_destruct_fds(struct sk_buff
*skb
) 877 struct scm_cookie scm
; 878 memset(&scm
,0,sizeof(scm
)); 879 unix_detach_fds(&scm
, skb
); 884 static voidunix_attach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 887 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 888 unix_inflight(scm
->fp
->fp
[i
]); 889 UNIXCB(skb
).fp
= scm
->fp
; 890 skb
->destructor
= unix_destruct_fds
; 899 static intunix_dgram_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 900 struct scm_cookie
*scm
) 902 struct sock
*sk
= sock
->sk
; 903 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 905 int namelen
=0;/* fake GCC */ 910 if(msg
->msg_flags
&MSG_OOB
) 913 if(msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
)) 916 if(msg
->msg_namelen
) { 917 namelen
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 926 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 929 skb
=sock_alloc_send_skb(sk
, len
,0, msg
->msg_flags
&MSG_DONTWAIT
, &err
); 933 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 934 UNIXCB(skb
).attr
= msg
->msg_flags
; 936 unix_attach_fds(scm
, skb
); 938 skb
->h
.raw
= skb
->data
; 939 err
=memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 943 other
=unix_peer(sk
); 944 if(other
&& other
->dead
) 947 * Check with 1003.1g - what should 959 other
=unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
); 963 if(!unix_may_send(sk
, other
)) 967 skb_queue_tail(&other
->receive_queue
, skb
); 968 other
->data_ready(other
,len
); 983 static intunix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 984 struct scm_cookie
*scm
) 986 struct sock
*sk
= sock
->sk
; 988 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 994 if(sock
->flags
& SO_ACCEPTCON
) 997 if(msg
->msg_flags
&MSG_OOB
) 1000 if(msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
)) 1003 if(msg
->msg_namelen
) { 1004 if(sk
->state
==TCP_ESTABLISHED
) 1014 if(sk
->shutdown
&SEND_SHUTDOWN
) { 1015 if(!(msg
->msg_flags
&MSG_NOSIGNAL
)) 1016 send_sig(SIGPIPE
,current
,0); 1023 * Optimisation for the fact that under 0.01% of X messages typically 1029 /* Keep two messages in the pipe so it schedules better */ 1030 if(size
> sk
->sndbuf
/2-16) 1031 size
= sk
->sndbuf
/2-16; 1034 * Keep to page sized kmalloc()'s as various people 1035 * have suggested. Big mallocs stress the vm too 1040 limit
=4096-16;/* Fall back to a page if we can't grab a big buffer this instant */ 1042 limit
=0;/* Otherwise just grab and wait */ 1048 skb
=sock_alloc_send_skb(sk
,size
,limit
,msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1058 * If you pass two values to the sock_alloc_send_skb 1059 * it tries to grab the large buffer with GFP_BUFFER 1060 * (which can fail easily), and if it fails grab the 1061 * fallback size buffer which is under a page and will 1064 size
=min(size
,skb_tailroom(skb
)); 1066 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1067 UNIXCB(skb
).attr
= msg
->msg_flags
; 1069 unix_attach_fds(scm
, skb
); 1071 if(memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) { 1078 other
=unix_peer(sk
); 1080 if(other
->dead
|| (sk
->shutdown
& SEND_SHUTDOWN
)) 1085 if(!(msg
->msg_flags
&MSG_NOSIGNAL
)) 1086 send_sig(SIGPIPE
,current
,0); 1090 skb_queue_tail(&other
->receive_queue
, skb
); 1091 other
->data_ready(other
,size
); 1099 * Sleep until data has arrive. But check for races.. 1102 static voidunix_data_wait(unix_socket
* sk
) 1104 if(!skb_peek(&sk
->receive_queue
)) 1106 sk
->socket
->flags
|= SO_WAITDATA
; 1107 interruptible_sleep_on(sk
->sleep
); 1108 sk
->socket
->flags
&= ~SO_WAITDATA
; 1112 static intunix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1113 int flags
,struct scm_cookie
*scm
) 1115 struct sock
*sk
= sock
->sk
; 1116 int noblock
= flags
& MSG_DONTWAIT
; 1117 struct sk_buff
*skb
; 1123 msg
->msg_namelen
=0; 1125 skb
=skb_recv_datagram(sk
, flags
, noblock
, &err
); 1131 msg
->msg_namelen
=sizeof(short); 1132 if(skb
->sk
->protinfo
.af_unix
.addr
) 1134 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1135 memcpy(msg
->msg_name
, 1136 skb
->sk
->protinfo
.af_unix
.addr
->name
, 1137 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1143 else if(size
< skb
->len
) 1144 msg
->msg_flags
|= MSG_TRUNC
; 1146 err
=skb_copy_datagram_iovec(skb
,0, msg
->msg_iov
, size
); 1150 scm
->creds
= *UNIXCREDS(skb
); 1152 if(!(flags
& MSG_PEEK
)) 1155 unix_detach_fds(scm
, skb
); 1159 /* It is questionable: on PEEK we could: 1160 - do not return fds - good, but too simple 8) 1161 - return fds, and do not return them on read (old strategy, 1163 - clone fds (I choosed it for now, it is the most universal 1166 POSIX 1003.1g does not actually define this clearly 1167 at all. POSIX 1003.1g doesn't define a lot of things 1172 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1177 skb_free_datagram(sk
,skb
); 1183 static intunix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1184 int flags
,struct scm_cookie
*scm
) 1186 struct sock
*sk
= sock
->sk
; 1187 int noblock
= flags
& MSG_DONTWAIT
; 1188 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1193 if(sock
->flags
& SO_ACCEPTCON
) 1198 if(flags
&MSG_WAITALL
) 1202 msg
->msg_namelen
=0; 1204 /* Lock the socket to prevent queue disordering 1205 * while sleeps in memcpy_tomsg 1208 down(&sk
->protinfo
.af_unix
.readsem
); 1213 struct sk_buff
*skb
; 1215 skb
=skb_dequeue(&sk
->receive_queue
); 1218 if(copied
>= target
) 1222 * POSIX 1003.1g mandates this order. 1227 up(&sk
->protinfo
.af_unix
.readsem
); 1228 returnsock_error(sk
); 1231 if(sk
->shutdown
& RCV_SHUTDOWN
) 1233 up(&sk
->protinfo
.af_unix
.readsem
); 1237 if(signal_pending(current
)) 1239 down(&sk
->protinfo
.af_unix
.readsem
); 1243 /* Never glue messages from different writers */ 1245 memcmp(UNIXCREDS(skb
), &scm
->creds
,sizeof(scm
->creds
)) !=0) 1247 skb_queue_head(&sk
->receive_queue
, skb
); 1251 /* Copy address just once */ 1254 msg
->msg_namelen
=sizeof(short); 1255 if(skb
->sk
->protinfo
.af_unix
.addr
) 1257 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1259 skb
->sk
->protinfo
.af_unix
.addr
->name
, 1260 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1265 chunk
=min(skb
->len
, size
); 1266 if(memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) { 1267 skb_queue_head(&sk
->receive_queue
, skb
); 1275 /* Copy credentials */ 1276 scm
->creds
= *UNIXCREDS(skb
); 1279 /* Mark read part of skb as used */ 1280 if(!(flags
& MSG_PEEK
)) 1282 skb_pull(skb
, chunk
); 1285 unix_detach_fds(scm
, skb
); 1287 /* put the skb back if we didn't use it up.. */ 1290 skb_queue_head(&sk
->receive_queue
, skb
); 1301 /* It is questionable, see note in unix_dgram_recvmsg. 1305 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1307 /* put message back and return */ 1308 skb_queue_head(&sk
->receive_queue
, skb
); 1313 up(&sk
->protinfo
.af_unix
.readsem
); 1317 static intunix_shutdown(struct socket
*sock
,int mode
) 1319 struct sock
*sk
= sock
->sk
; 1320 unix_socket
*other
=unix_peer(sk
); 1322 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
); 1325 sk
->shutdown
|= mode
; 1326 sk
->state_change(sk
); 1327 if(other
&& sk
->type
== SOCK_STREAM
&& 1328 unix_our_peer(sk
, other
)) { 1331 if(mode
&RCV_SHUTDOWN
) 1332 peer_mode
|= SEND_SHUTDOWN
; 1333 if(mode
&SEND_SHUTDOWN
) 1334 peer_mode
|= RCV_SHUTDOWN
; 1335 other
->shutdown
|= mode
; 1336 other
->state_change(other
); 1343 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1345 struct sock
*sk
= sock
->sk
; 1352 amount
= sk
->sndbuf
-atomic_read(&sk
->wmem_alloc
); 1355 returnput_user(amount
, (int*)arg
); 1358 struct sk_buff
*skb
; 1359 if(sk
->state
==TCP_LISTEN
) 1362 * These two are safe on current systems as 1363 * only user tasks fiddle here 1365 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1367 returnput_user(amount
, (int*)arg
); 1377 static unsigned intunix_poll(struct file
* file
,struct socket
*sock
, poll_table
*wait
) 1379 struct sock
*sk
= sock
->sk
; 1382 poll_wait(file
, sk
->sleep
, wait
); 1385 /* exceptional events? */ 1388 if(sk
->shutdown
& RCV_SHUTDOWN
) 1392 if(!skb_queue_empty(&sk
->receive_queue
)) 1393 mask
|= POLLIN
| POLLRDNORM
; 1395 /* Connection-based need to check for termination and startup */ 1396 if(sk
->type
== SOCK_STREAM
&& sk
->state
==TCP_CLOSE
) 1400 * we set writable also when the other side has shut down the 1401 * connection. This prevents stuck sockets. 1403 if(sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
) 1404 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
; 1409 static voidunix_stream_write_space(struct sock
*sk
) 1413 wake_up_interruptible(sk
->sleep
); 1414 if(sk
->sndbuf
- (int)atomic_read(&sk
->wmem_alloc
) >= MIN_WRITE_SPACE
) 1415 sock_wake_async(sk
->socket
,2); 1418 #ifdef CONFIG_PROC_FS 1419 static intunix_read_proc(char*buffer
,char**start
, off_t offset
, 1420 int length
,int*eof
,void*data
) 1428 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1431 forall_unix_sockets(i
,s
) 1433 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X %5ld", 1435 atomic_read(&s
->sock_readers
), 1437 s
->socket
? s
->socket
->flags
:0, 1439 s
->socket
? s
->socket
->state
: 1440 (s
->state
== TCP_ESTABLISHED
? 1441 SS_CONNECTING
: SS_DISCONNECTING
), 1442 s
->socket
? s
->socket
->inode
->i_ino
:0); 1444 if(s
->protinfo
.af_unix
.addr
) 1447 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
, 1448 s
->protinfo
.af_unix
.addr
->len
-sizeof(short)); 1449 if(!UNIX_ABSTRACT(s
)) 1453 len
+= s
->protinfo
.af_unix
.addr
->len
-sizeof(short); 1463 if(pos
>offset
+length
) 1468 *start
=buffer
+(offset
-begin
); 1469 len
-=(offset
-begin
); 1478 struct proto_ops unix_stream_ops
= { 1484 unix_stream_connect
, 1495 unix_stream_sendmsg
, 1499 struct proto_ops unix_dgram_ops
= { 1520 struct net_proto_family unix_family_ops
= { 1526 #ifdef CONFIG_SYSCTL 1527 externvoidunix_sysctl_register(void); 1528 externvoidunix_sysctl_unregister(void); 1531 intinit_module(void) 1533 __initfunc(voidunix_proto_init(struct net_proto
*pro
)) 1536 struct sk_buff
*dummy_skb
; 1537 struct proc_dir_entry
*ent
; 1539 printk(KERN_INFO
"NET3: Unix domain sockets 0.16 for Linux NET3.038.\n"); 1540 if(sizeof(struct unix_skb_parms
) >sizeof(dummy_skb
->cb
)) 1542 printk(KERN_CRIT
"unix_proto_init: panic\n"); 1549 sock_register(&unix_family_ops
); 1550 #ifdef CONFIG_PROC_FS 1551 ent
=create_proc_entry("net/unix",0,0); 1552 ent
->read_proc
= unix_read_proc
; 1556 #ifdef CONFIG_SYSCTL 1557 unix_sysctl_register(); 1565 voidcleanup_module(void) 1567 sock_unregister(PF_UNIX
); 1568 #ifdef CONFIG_SYSCTL 1569 unix_sysctl_unregister(); 1571 #ifdef CONFIG_PROC_FS 1572 remove_proc_entry("net/unix",0); 1579 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"