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. 12 * Linus Torvalds : Assorted bug cures. 13 * Niibe Yutaka : async I/O support. 14 * Carsten Paeth : PF_UNIX check, address fixes. 15 * Alan Cox : Limit size of allocated blocks. 16 * Alan Cox : Fixed the stupid socketpair bug. 17 * Alan Cox : BSD compatibility fine tuning. 18 * Alan Cox : Fixed a bug in connect when interrupted. 19 * Alan Cox : Sorted out a proper draft version of 20 * file descriptor passing hacked up from 22 * Marty Leisner : Fixes to fd passing 23 * Nick Nevin : recvmsg bugfix. 24 * Alan Cox : Started proper garbage collector 25 * Heiko EiBfeldt : Missing verify_area check 26 * Alan Cox : Started POSIXisms 28 * Known differences from reference BSD that was tested: 31 * ECONNREFUSED is not returned from one end of a connected() socket to the 32 * other the moment one end closes. 33 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark 34 * and a fake inode identifier (nor the BSD first socket fstat twice bug). 36 * accept() returns a path name even if the connecting socket has closed 37 * in the meantime (BSD loses the path and gives up). 38 * accept() returns 0 length path for an unbound connector. BSD returns 16 39 * and a null first byte in the path (but not for gethost/peername - BSD bug ??) 40 * socketpair(...SOCK_RAW..) doesn't panic the kernel. 41 * BSD af_unix apparently has connect forgetting to block properly. 42 * (need to check this with the POSIX spec in detail) 44 * Differences from 2.0.0-11-... (ANK) 45 * Bug fixes and improvements. 46 * - client shutdown killed server socket. 47 * - removed all useless cli/sti pairs. 49 * Semantic changes/extensions. 50 * - generic control message passing. 51 * - SCM_CREDENTIALS control message. 52 * - "Abstract" (not FS based) socket bindings. 53 * Abstract names are sequences of bytes (not zero terminated) 54 * started by 0, so that this name space does not intersect 58 #include <linux/config.h> 59 #include <linux/kernel.h> 60 #include <linux/major.h> 61 #include <linux/signal.h> 62 #include <linux/sched.h> 63 #include <linux/errno.h> 64 #include <linux/string.h> 65 #include <linux/stat.h> 66 #include <linux/socket.h> 68 #include <linux/fcntl.h> 69 #include <linux/termios.h> 70 #include <linux/socket.h> 71 #include <linux/sockios.h> 72 #include <linux/net.h> 75 #include <linux/malloc.h> 76 #include <asm/uaccess.h> 77 #include <linux/skbuff.h> 78 #include <linux/netdevice.h> 81 #include <net/af_unix.h> 82 #include <linux/proc_fs.h> 84 #include <linux/init.h> 86 #include <asm/checksum.h> 88 #define min(a,b) (((a)<(b))?(a):(b)) 90 int sysctl_unix_delete_delay
= HZ
; 91 int sysctl_unix_destroy_delay
=10*HZ
; 93 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1]; 95 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE]) 97 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE) 99 extern __inline__
unsignedunix_hash_fold(unsigned hash
) 107 #define unix_peer(sk) ((sk)->pair) 109 extern __inline__
intunix_our_peer(unix_socket
*sk
, unix_socket
*osk
) 111 returnunix_peer(osk
) == sk
; 114 extern __inline__
intunix_may_send(unix_socket
*sk
, unix_socket
*osk
) 116 return(sk
->type
==osk
->type
); 119 extern __inline__
voidunix_lock(unix_socket
*sk
) 124 extern __inline__
intunix_unlock(unix_socket
*sk
) 126 return sk
->sock_readers
--; 129 extern __inline__
intunix_locked(unix_socket
*sk
) 131 return sk
->sock_readers
; 134 extern __inline__
voidunix_release_addr(struct unix_address
*addr
) 138 if(atomic_dec_and_test(&addr
->refcnt
)) 143 static voidunix_destruct_addr(struct sock
*sk
) 145 struct unix_address
*addr
= sk
->protinfo
.af_unix
.addr
; 147 unix_release_addr(addr
); 151 * Check unix socket name: 152 * - should be not zero length. 153 * - if started by not zero, should be NULL terminated (FS object) 154 * - if started by zero, it is abstract name. 157 static intunix_mkname(struct sockaddr_un
* sunaddr
,int len
,unsigned*hashp
) 159 if(len
<=sizeof(short) || len
>sizeof(*sunaddr
)) 161 if(!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
) 163 if(sunaddr
->sun_path
[0]) 165 if(len
>=sizeof(*sunaddr
)) 166 len
=sizeof(*sunaddr
)-1; 167 ((char*)sunaddr
)[len
]=0; 168 len
=strlen(sunaddr
->sun_path
)+1+sizeof(short); 172 *hashp
=unix_hash_fold(csum_partial((char*)sunaddr
, len
,0)); 176 static voidunix_remove_socket(unix_socket
*sk
) 178 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 180 sk
->next
->prev
= sk
->prev
; 182 sk
->prev
->next
= sk
->next
; 185 sk
->protinfo
.af_unix
.list
= NULL
; 190 static voidunix_insert_socket(unix_socket
*sk
) 192 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 200 static unix_socket
*unix_find_socket_byname(struct sockaddr_un
*sunname
, 201 int len
,int type
,unsigned hash
) 205 for(s
=unix_socket_table
[(hash
^type
)&0xF]; s
; s
=s
->next
) 207 if(s
->protinfo
.af_unix
.addr
->len
==len
&& 208 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) ==0&& 218 static unix_socket
*unix_find_socket_byinode(struct inode
*i
) 222 for(s
=unix_socket_table
[i
->i_ino
&0xF]; s
; s
=s
->next
) 224 if(s
->protinfo
.af_unix
.inode
==i
) 234 * Delete a unix socket. We have to allow for deferring this on a timer. 237 static voidunix_destroy_timer(unsigned long data
) 239 unix_socket
*sk
=(unix_socket
*)data
; 240 if(!unix_locked(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 250 sk
->timer
.expires
=jiffies
+sysctl_unix_destroy_delay
;/* No real hurry try it every 10 seconds or so */ 251 add_timer(&sk
->timer
); 255 static voidunix_delayed_delete(unix_socket
*sk
) 257 sk
->timer
.data
=(unsigned long)sk
; 258 sk
->timer
.expires
=jiffies
+sysctl_unix_delete_delay
;/* Normally 1 second after will clean up. After that we try every 10 */ 259 sk
->timer
.function
=unix_destroy_timer
; 260 add_timer(&sk
->timer
); 263 static voidunix_destroy_socket(unix_socket
*sk
) 267 unix_remove_socket(sk
); 269 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 271 if(sk
->state
==TCP_LISTEN
) 273 unix_socket
*osk
=skb
->sk
; 274 osk
->state
=TCP_CLOSE
; 275 kfree_skb(skb
, FREE_WRITE
);/* Now surplus - free the skb first before the socket */ 276 osk
->state_change(osk
);/* So the connect wakes and cleans up (if any) */ 277 /* osk will be destroyed when it gets to close or the timer fires */ 281 /* passed fds are erased in the kfree_skb hook */ 282 kfree_skb(skb
,FREE_WRITE
); 286 if(sk
->protinfo
.af_unix
.inode
!=NULL
) 288 iput(sk
->protinfo
.af_unix
.inode
); 289 sk
->protinfo
.af_unix
.inode
=NULL
; 292 if(!unix_unlock(sk
) &&atomic_read(&sk
->wmem_alloc
) ==0) 299 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 303 static intunix_listen(struct socket
*sock
,int backlog
) 305 struct sock
*sk
= sock
->sk
; 307 if(sock
->state
!= SS_UNCONNECTED
) 309 if(sock
->type
!=SOCK_STREAM
) 310 return-EOPNOTSUPP
;/* Only stream sockets accept */ 311 if(!sk
->protinfo
.af_unix
.addr
) 312 return-EINVAL
;/* No listens on an unbound socket */ 313 sk
->max_ack_backlog
=backlog
; 314 if(sk
->ack_backlog
< backlog
) 315 sk
->state_change(sk
); 316 sk
->state
=TCP_LISTEN
; 317 sock
->flags
|= SO_ACCEPTCON
; 321 externstruct proto_ops unix_stream_ops
; 322 externstruct proto_ops unix_dgram_ops
; 324 static intunix_create(struct socket
*sock
,int protocol
) 328 sock
->state
= SS_UNCONNECTED
; 330 if(protocol
&& protocol
!= PF_UNIX
) 331 return-EPROTONOSUPPORT
; 336 sock
->ops
= &unix_stream_ops
; 339 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 343 sock
->type
=SOCK_DGRAM
; 345 sock
->ops
= &unix_dgram_ops
; 348 return-ESOCKTNOSUPPORT
; 350 sk
=sk_alloc(GFP_KERNEL
); 354 sock_init_data(sock
,sk
); 356 sk
->destruct
= unix_destruct_addr
; 357 sk
->protinfo
.af_unix
.family
=AF_UNIX
; 358 sk
->protinfo
.af_unix
.inode
=NULL
; 359 sk
->sock_readers
=1;/* Us */ 360 sk
->protinfo
.af_unix
.readsem
=MUTEX
;/* single task reading lock */ 362 sk
->protinfo
.af_unix
.list
=&unix_sockets_unbound
; 363 unix_insert_socket(sk
); 367 static intunix_dup(struct socket
*newsock
,struct socket
*oldsock
) 369 returnunix_create(newsock
,0); 372 static intunix_release(struct socket
*sock
,struct socket
*peer
) 374 unix_socket
*sk
= sock
->sk
; 380 if(sock
->state
!= SS_UNCONNECTED
) 381 sock
->state
= SS_DISCONNECTING
; 383 sk
->state_change(sk
); 385 skpair
=unix_peer(sk
); 386 if(sock
->type
==SOCK_STREAM
&& skpair
) 388 if(unix_our_peer(sk
, skpair
)) 389 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes */ 390 if(skpair
->state
!=TCP_LISTEN
) 391 skpair
->state_change(skpair
);/* Wake any blocked writes */ 394 unix_unlock(skpair
);/* It may now die */ 395 unix_peer(sk
)=NULL
;/* No pair */ 396 unix_destroy_socket(sk
);/* Try to flush out this socket. Throw out buffers at least */ 397 unix_gc();/* Garbage collect fds */ 400 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In 401 * Linux we behave like files and pipes do and wait for the last dereference. 412 static intunix_autobind(struct socket
*sock
) 414 struct sock
*sk
= sock
->sk
; 415 static u32 ordernum
=1; 416 struct unix_address
* addr
; 419 addr
=kmalloc(sizeof(*addr
) +sizeof(short) +16, GFP_KERNEL
); 422 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.inode
) 427 memset(addr
,0,sizeof(*addr
) +sizeof(short) +16); 428 addr
->name
->sun_family
= AF_UNIX
; 429 atomic_set(&addr
->refcnt
,1); 432 addr
->len
=sprintf(addr
->name
->sun_path
+1,"%08x", ordernum
) +1+sizeof(short); 433 addr
->hash
=unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
,0)); 436 if((osk
=unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
, 437 addr
->hash
)) != NULL
) 443 sk
->protinfo
.af_unix
.addr
= addr
; 444 unix_remove_socket(sk
); 445 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(addr
->hash
^ sk
->type
)&0xF]; 446 unix_insert_socket(sk
); 450 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
,int len
, 451 int type
,unsigned hash
,int*error
) 455 if(sunname
->sun_path
[0]) 457 struct dentry
*dentry
; 458 dentry
=open_namei(sunname
->sun_path
,2, S_IFSOCK
); 460 *error
=PTR_ERR(dentry
); 463 u
=unix_find_socket_byinode(dentry
->d_inode
); 465 if(u
&& u
->type
!= type
) 473 u
=unix_find_socket_byname(sunname
, len
, type
, hash
); 477 *error
=-ECONNREFUSED
; 484 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 486 struct sock
*sk
= sock
->sk
; 487 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 488 struct dentry
* dentry
; 489 struct inode
* inode
= NULL
; 492 struct unix_address
*addr
; 494 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.inode
|| 495 sunaddr
->sun_family
!= AF_UNIX
) 498 if(addr_len
==sizeof(short)) 499 returnunix_autobind(sock
); 501 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 505 addr
=kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
); 509 /* We slept; recheck ... */ 511 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.inode
) 514 return-EINVAL
;/* Already bound */ 517 memcpy(addr
->name
, sunaddr
, addr_len
); 518 addr
->len
= addr_len
; 520 atomic_set(&addr
->refcnt
,1); 522 if(!sunaddr
->sun_path
[0]) 524 unix_socket
*osk
=unix_find_socket_byname(sunaddr
, addr_len
, 532 unix_remove_socket(sk
); 533 sk
->protinfo
.af_unix
.addr
= addr
; 534 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(hash
^sk
->type
)&0xF]; 535 unix_insert_socket(sk
); 539 addr
->hash
= UNIX_HASH_SIZE
; 540 sk
->protinfo
.af_unix
.addr
= addr
; 543 dentry
=do_mknod(sunaddr
->sun_path
, S_IFSOCK
|S_IRWXUGO
,0); 544 err
=PTR_ERR(dentry
); 545 if(!IS_ERR(dentry
)) { 546 inode
= dentry
->d_inode
; 547 inode
->i_count
++;/* HATEFUL - we should use the dentry */ 554 unix_release_addr(addr
); 555 sk
->protinfo
.af_unix
.addr
= NULL
; 561 unix_remove_socket(sk
); 562 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[inode
->i_ino
&0xF]; 563 sk
->protinfo
.af_unix
.inode
= inode
; 564 unix_insert_socket(sk
); 569 static intunix_dgram_connect(struct socket
*sock
,struct sockaddr
*addr
, 572 struct sock
*sk
= sock
->sk
; 573 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
; 579 * 1003.1g breaking connected state with AF_UNSPEC 582 if(addr
->sa_family
==AF_UNSPEC
) 586 unix_unlock(unix_peer(sk
)); 587 unix_peer(sk
) = NULL
; 588 sock
->state
=SS_UNCONNECTED
; 593 alen
=unix_mkname(sunaddr
, alen
, &hash
); 597 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
); 600 if(!unix_may_send(sk
, other
)) 607 * If it was connected, reconnect. 611 unix_unlock(unix_peer(sk
)); 615 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 620 static intunix_stream_connect1(struct socket
*sock
,struct msghdr
*msg
, 621 int len
,struct unix_skb_parms
*cmsg
,int nonblock
) 623 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)msg
->msg_name
; 624 struct sock
*sk
= sock
->sk
; 631 addr_len
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 638 /* This is ok... continue with connect */ 641 /* Socket is already connected */ 644 /* Not yet connected... we will check this. */ 653 if(sock
->state
==SS_CONNECTING
&& sk
->state
==TCP_ESTABLISHED
) 655 sock
->state
=SS_CONNECTED
; 656 if(!sk
->protinfo
.af_unix
.addr
) 660 if(sock
->state
==SS_CONNECTING
&& sk
->state
== TCP_CLOSE
) 662 sock
->state
=SS_UNCONNECTED
; 665 if(sock
->state
!=SS_CONNECTING
) 670 * Drop through the connect up logic to the wait. 674 if(sock
->state
==SS_UNCONNECTED
) 677 * Now ready to connect 680 skb
=sock_alloc_send_skb(sk
, len
,0, nonblock
, &err
);/* Marker object */ 683 memcpy(&UNIXCB(skb
), cmsg
,sizeof(*cmsg
)); 685 memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 687 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
); 690 kfree_skb(skb
, FREE_WRITE
); 693 other
->ack_backlog
++; 695 skb_queue_tail(&other
->receive_queue
,skb
); 696 sk
->state
=TCP_SYN_SENT
; 697 sock
->state
=SS_CONNECTING
; 698 other
->data_ready(other
,0);/* Wake up ! */ 702 /* Wait for an accept */ 704 while(sk
->state
==TCP_SYN_SENT
) 708 interruptible_sleep_on(sk
->sleep
); 709 if(current
->signal
& ~current
->blocked
) 714 * Has the other end closed on us ? 717 if(sk
->state
==TCP_CLOSE
) 719 unix_unlock(unix_peer(sk
)); 721 sock
->state
=SS_UNCONNECTED
; 726 * Amazingly it has worked 729 sock
->state
=SS_CONNECTED
; 730 if(!sk
->protinfo
.af_unix
.addr
) 736 static intunix_stream_connect(struct socket
*sock
,struct sockaddr
*uaddr
, 737 int addr_len
,int flags
) 740 struct unix_skb_parms cmsg
; 742 msg
.msg_name
= uaddr
; 743 msg
.msg_namelen
= addr_len
; 746 cmsg
.creds
.pid
= current
->pid
; 747 cmsg
.creds
.uid
= current
->euid
; 748 cmsg
.creds
.gid
= current
->egid
; 750 returnunix_stream_connect1(sock
, &msg
,0, &cmsg
, flags
&O_NONBLOCK
); 753 static intunix_socketpair(struct socket
*socka
,struct socket
*sockb
) 755 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
; 757 /* Join our sockets back to back */ 763 if(ska
->type
!= SOCK_DGRAM
) 765 ska
->state
=TCP_ESTABLISHED
; 766 skb
->state
=TCP_ESTABLISHED
; 767 socka
->state
=SS_CONNECTED
; 768 sockb
->state
=SS_CONNECTED
; 773 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 775 unix_socket
*sk
= sock
->sk
; 776 unix_socket
*newsk
= newsock
->sk
; 780 if(sock
->state
!= SS_UNCONNECTED
) 782 if(!(sock
->flags
& SO_ACCEPTCON
)) 785 if(sock
->type
!=SOCK_STREAM
) 787 if(sk
->state
!=TCP_LISTEN
) 790 if(sk
->protinfo
.af_unix
.addr
) 792 atomic_inc(&sk
->protinfo
.af_unix
.addr
->refcnt
); 793 newsk
->protinfo
.af_unix
.addr
=sk
->protinfo
.af_unix
.addr
; 795 if(sk
->protinfo
.af_unix
.inode
) 797 sk
->protinfo
.af_unix
.inode
->i_count
++;/* Should use dentry */ 798 newsk
->protinfo
.af_unix
.inode
=sk
->protinfo
.af_unix
.inode
; 803 skb
=skb_dequeue(&sk
->receive_queue
); 808 interruptible_sleep_on(sk
->sleep
); 809 if(current
->signal
& ~current
->blocked
) 813 if(!(UNIXCB(skb
).attr
& MSG_SYN
)) 816 tsk
->state_change(tsk
); 817 kfree_skb(skb
, FREE_WRITE
); 825 unix_peer(newsk
)=tsk
; 826 unix_peer(tsk
)=newsk
; 827 tsk
->state
=TCP_ESTABLISHED
; 828 newsk
->state
=TCP_ESTABLISHED
; 829 memcpy(&newsk
->peercred
,UNIXCREDS(skb
),sizeof(struct ucred
)); 830 tsk
->peercred
.pid
= current
->pid
; 831 tsk
->peercred
.uid
= current
->euid
; 832 tsk
->peercred
.gid
= current
->egid
; 833 unix_lock(newsk
);/* Swap lock over */ 834 unix_unlock(sk
);/* Locked to child socket not master */ 835 unix_lock(tsk
);/* Back lock */ 836 kfree_skb(skb
, FREE_WRITE
);/* The buffer is just used as a tag */ 837 tsk
->state_change(tsk
);/* Wake up any sleeping connect */ 838 sock_wake_async(tsk
->socket
,0); 843 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 845 struct sock
*sk
= sock
->sk
; 846 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 854 if(!sk
->protinfo
.af_unix
.addr
) 856 sunaddr
->sun_family
= AF_UNIX
; 857 sunaddr
->sun_path
[0] =0; 858 *uaddr_len
=sizeof(short); 859 return0;/* Not bound */ 861 *uaddr_len
= sk
->protinfo
.af_unix
.addr
->len
; 862 memcpy(sunaddr
, sk
->protinfo
.af_unix
.addr
->name
, *uaddr_len
); 866 static voidunix_detach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 870 scm
->fp
=UNIXCB(skb
).fp
; 871 skb
->destructor
= sock_wfree
; 872 UNIXCB(skb
).fp
= NULL
; 874 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 875 unix_notinflight(scm
->fp
->fp
[i
]); 878 static voidunix_destruct_fds(struct sk_buff
*skb
) 880 struct scm_cookie scm
; 881 memset(&scm
,0,sizeof(scm
)); 882 unix_detach_fds(&scm
, skb
); 887 static voidunix_attach_fds(struct scm_cookie
*scm
,struct sk_buff
*skb
) 890 for(i
=scm
->fp
->count
-1; i
>=0; i
--) 891 unix_inflight(scm
->fp
->fp
[i
]); 892 UNIXCB(skb
).fp
= scm
->fp
; 893 skb
->destructor
= unix_destruct_fds
; 902 static intunix_dgram_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 903 struct scm_cookie
*scm
) 905 struct sock
*sk
= sock
->sk
; 907 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 908 int namelen
=0;/* fake GCC */ 913 if(msg
->msg_flags
&MSG_OOB
) 916 if(msg
->msg_flags
&~MSG_DONTWAIT
) 919 if(msg
->msg_namelen
) { 920 namelen
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 929 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 932 skb
=sock_alloc_send_skb(sk
, len
,0, msg
->msg_flags
&MSG_DONTWAIT
, &err
); 937 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 938 UNIXCB(skb
).attr
= msg
->msg_flags
; 940 unix_attach_fds(scm
, skb
); 942 skb
->h
.raw
= skb
->data
; 943 memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 945 other
=unix_peer(sk
); 946 if(other
&& other
->dead
) 949 * Check with 1003.1g - what should 955 if(sunaddr
== NULL
) { 956 kfree_skb(skb
, FREE_WRITE
); 962 other
=unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
); 966 kfree_skb(skb
, FREE_WRITE
); 969 if(!unix_may_send(sk
, other
)) 972 kfree_skb(skb
, FREE_WRITE
); 977 skb_queue_tail(&other
->receive_queue
, skb
); 978 other
->data_ready(other
,len
); 986 static intunix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 987 struct scm_cookie
*scm
) 989 struct sock
*sk
= sock
->sk
; 991 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 997 if(sock
->flags
& SO_ACCEPTCON
) 1000 if(msg
->msg_flags
&MSG_OOB
) 1003 if(msg
->msg_flags
&~MSG_DONTWAIT
) 1006 if(msg
->msg_namelen
) { 1007 if(sk
->state
==TCP_ESTABLISHED
) 1017 if(sk
->shutdown
&SEND_SHUTDOWN
) { 1018 send_sig(SIGPIPE
,current
,0); 1025 * Optimisation for the fact that under 0.01% of X messages typically 1031 if(size
>(sk
->sndbuf
-sizeof(struct sk_buff
))/2)/* Keep two messages in the pipe so it schedules better */ 1032 size
=(sk
->sndbuf
-sizeof(struct sk_buff
))/2; 1035 * Keep to page sized kmalloc()'s as various people 1036 * have suggested. Big mallocs stress the vm too 1041 limit
=3500;/* Fall back to a page if we can't grab a big buffer this instant */ 1043 limit
=0;/* Otherwise just grab and wait */ 1049 skb
=sock_alloc_send_skb(sk
,size
,limit
,msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1059 * If you pass two values to the sock_alloc_send_skb 1060 * it tries to grab the large buffer with GFP_BUFFER 1061 * (which can fail easily), and if it fails grab the 1062 * fallback size buffer which is under a page and will 1065 size
=min(size
,skb_tailroom(skb
)); 1067 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1068 UNIXCB(skb
).attr
= msg
->msg_flags
; 1070 unix_attach_fds(scm
, skb
); 1072 memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
); 1074 other
=unix_peer(sk
); 1076 if(other
->dead
|| (sk
->shutdown
& SEND_SHUTDOWN
)) 1078 kfree_skb(skb
, FREE_WRITE
); 1081 send_sig(SIGPIPE
,current
,0); 1085 skb_queue_tail(&other
->receive_queue
, skb
); 1086 other
->data_ready(other
,size
); 1093 * Sleep until data has arrive. But check for races.. 1096 static voidunix_data_wait(unix_socket
* sk
) 1098 if(!skb_peek(&sk
->receive_queue
)) 1100 sk
->socket
->flags
|= SO_WAITDATA
; 1101 interruptible_sleep_on(sk
->sleep
); 1102 sk
->socket
->flags
&= ~SO_WAITDATA
; 1106 static intunix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1107 int flags
,struct scm_cookie
*scm
) 1109 struct sock
*sk
= sock
->sk
; 1110 int noblock
= flags
& MSG_DONTWAIT
; 1111 struct sk_buff
*skb
; 1117 msg
->msg_namelen
=0; 1119 skb
=skb_recv_datagram(sk
, flags
, noblock
, &err
); 1125 if(skb
->sk
->protinfo
.af_unix
.addr
) 1127 memcpy(msg
->msg_name
, skb
->sk
->protinfo
.af_unix
.addr
->name
, 1128 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1129 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1132 msg
->msg_namelen
=sizeof(short); 1137 else if(size
< skb
->len
) 1138 msg
->msg_flags
|= MSG_TRUNC
; 1140 if(skb_copy_datagram_iovec(skb
,0, msg
->msg_iov
, size
)) 1143 scm
->creds
= *UNIXCREDS(skb
); 1145 if(!(flags
& MSG_PEEK
)) 1148 unix_detach_fds(scm
, skb
); 1152 /* It is questionable: on PEEK we could: 1153 - do not return fds - good, but too simple 8) 1154 - return fds, and do not return them on read (old strategy, 1156 - clone fds (I choosed it for now, it is the most universal 1159 POSIX 1003.1g does not actually define this clearly 1160 at all. POSIX 1003.1g doesn't define a lot of things 1165 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1167 skb_free_datagram(sk
,skb
); 1172 static intunix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1173 int flags
,struct scm_cookie
*scm
) 1175 struct sock
*sk
= sock
->sk
; 1176 int noblock
= flags
& MSG_DONTWAIT
; 1177 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1182 if(sock
->flags
& SO_ACCEPTCON
) 1187 if(flags
&MSG_WAITALL
) 1191 msg
->msg_namelen
=0; 1193 /* Lock the socket to prevent queue disordering 1194 * while sleeps in memcpy_tomsg 1197 down(&sk
->protinfo
.af_unix
.readsem
); 1202 struct sk_buff
*skb
; 1204 skb
=skb_dequeue(&sk
->receive_queue
); 1207 if(copied
>= target
) 1211 returnsock_error(sk
); 1213 if(sk
->shutdown
& RCV_SHUTDOWN
) 1215 up(&sk
->protinfo
.af_unix
.readsem
); 1219 if(current
->signal
& ~current
->blocked
) 1221 down(&sk
->protinfo
.af_unix
.readsem
); 1225 /* Never glue messages from different writers */ 1227 memcmp(UNIXCREDS(skb
), &scm
->creds
,sizeof(scm
->creds
)) !=0) 1229 skb_queue_head(&sk
->receive_queue
, skb
); 1233 /* Copy address just once */ 1236 if(skb
->sk
->protinfo
.af_unix
.addr
) 1238 memcpy(sunaddr
, skb
->sk
->protinfo
.af_unix
.addr
->name
, 1239 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1240 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1243 msg
->msg_namelen
=sizeof(short); 1247 chunk
=min(skb
->len
, size
); 1248 memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
); 1252 /* Copy credentials */ 1253 scm
->creds
= *UNIXCREDS(skb
); 1256 /* Mark read part of skb as used */ 1257 if(!(flags
& MSG_PEEK
)) 1259 skb_pull(skb
, chunk
); 1262 unix_detach_fds(scm
, skb
); 1264 /* put the skb back if we didn't use it up.. */ 1267 skb_queue_head(&sk
->receive_queue
, skb
); 1271 kfree_skb(skb
, FREE_WRITE
); 1278 /* It is questionable, see note in unix_dgram_recvmsg. 1282 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1284 /* put message back and return */ 1285 skb_queue_head(&sk
->receive_queue
, skb
); 1290 up(&sk
->protinfo
.af_unix
.readsem
); 1294 static intunix_shutdown(struct socket
*sock
,int mode
) 1296 struct sock
*sk
= sock
->sk
; 1297 unix_socket
*other
=unix_peer(sk
); 1301 if(mode
&SEND_SHUTDOWN
) 1303 sk
->shutdown
|=SEND_SHUTDOWN
; 1304 sk
->state_change(sk
); 1305 if(other
&& sk
->type
== SOCK_STREAM
&& other
->state
!= TCP_LISTEN
) 1307 if(unix_our_peer(sk
, other
)) 1308 other
->shutdown
|=RCV_SHUTDOWN
; 1309 other
->state_change(other
); 1312 other
=unix_peer(sk
); 1313 if(mode
&RCV_SHUTDOWN
) 1315 sk
->shutdown
|=RCV_SHUTDOWN
; 1316 sk
->state_change(sk
); 1317 if(other
&& sk
->type
!= SOCK_DGRAM
&& other
->state
!= TCP_LISTEN
) 1319 if(unix_our_peer(sk
, other
)) 1320 other
->shutdown
|=SEND_SHUTDOWN
; 1321 other
->state_change(other
); 1328 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1330 struct sock
*sk
= sock
->sk
; 1337 amount
= sk
->sndbuf
-atomic_read(&sk
->wmem_alloc
); 1340 returnput_user(amount
, (int*)arg
); 1343 struct sk_buff
*skb
; 1344 if(sk
->state
==TCP_LISTEN
) 1347 * These two are safe on current systems as 1348 * only user tasks fiddle here 1350 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1352 returnput_user(amount
, (int*)arg
); 1362 #ifdef CONFIG_PROC_FS 1363 static intunix_read_proc(char*buffer
,char**start
, off_t offset
, 1364 int length
,int*eof
,void*data
) 1372 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1375 forall_unix_sockets(i
,s
) 1377 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X %5ld", 1381 s
->socket
? s
->socket
->flags
:0, 1383 s
->socket
? s
->socket
->state
:0, 1384 s
->socket
? s
->socket
->inode
->i_ino
:0); 1386 if(s
->protinfo
.af_unix
.addr
) 1389 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
, 1390 s
->protinfo
.af_unix
.addr
->len
-sizeof(short)); 1391 if(!UNIX_ABSTRACT(s
)) 1395 len
+= s
->protinfo
.af_unix
.addr
->len
-sizeof(short); 1405 if(pos
>offset
+length
) 1410 *start
=buffer
+(offset
-begin
); 1411 len
-=(offset
-begin
); 1418 struct proto_ops unix_stream_ops
= { 1424 unix_stream_connect
, 1435 unix_stream_sendmsg
, 1439 struct proto_ops unix_dgram_ops
= { 1460 struct net_proto_family unix_family_ops
= { 1465 __initfunc(voidunix_proto_init(struct net_proto
*pro
)) 1467 struct sk_buff
*dummy_skb
; 1468 struct proc_dir_entry
*ent
; 1470 printk(KERN_INFO
"NET3: Unix domain sockets 0.16 for Linux NET3.038.\n"); 1471 if(sizeof(struct unix_skb_parms
) >sizeof(dummy_skb
->cb
)) 1473 printk(KERN_CRIT
"unix_proto_init: panic\n"); 1476 sock_register(&unix_family_ops
); 1477 #ifdef CONFIG_PROC_FS 1478 ent
=create_proc_entry("net/unix",0,0); 1479 ent
->read_proc
= unix_read_proc
; 1484 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"