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> 85 #include <asm/checksum.h> 87 #define min(a,b) (((a)<(b))?(a):(b)) 90 unix_socket
*unix_socket_table
[UNIX_HASH_SIZE
+1]; 92 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE]) 94 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE) 96 extern __inline__
unsignedunix_hash_fold(unsigned hash
) 104 #define unix_peer(sk) ((sk)->pair) 106 extern __inline__
intunix_our_peer(unix_socket
*sk
, unix_socket
*osk
) 108 returnunix_peer(osk
) == sk
; 111 extern __inline__
intunix_may_send(unix_socket
*sk
, unix_socket
*osk
) 113 return(sk
->type
==osk
->type
); 116 extern __inline__
voidunix_lock(unix_socket
*sk
) 121 extern __inline__
intunix_unlock(unix_socket
*sk
) 126 extern __inline__
intunix_locked(unix_socket
*sk
) 131 extern __inline__
voidunix_release_addr(struct unix_address
*addr
) 135 if(atomic_dec_and_test(&addr
->refcnt
)) 142 * Check unix socket name: 143 * - should be not zero length. 144 * - if started by not zero, should be NULL terminated (FS object) 145 * - if started by zero, it is abstract name. 148 static intunix_mkname(struct sockaddr_un
* sunaddr
,int len
,unsigned*hashp
) 150 if(len
<=sizeof(short) || len
>sizeof(*sunaddr
)) 152 if(!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
) 154 if(sunaddr
->sun_path
[0]) 156 if(len
>=sizeof(*sunaddr
)) 157 len
=sizeof(*sunaddr
)-1; 158 ((char*)sunaddr
)[len
]=0; 159 len
=strlen(sunaddr
->sun_path
)+1+sizeof(short); 163 *hashp
=unix_hash_fold(csum_partial((char*)sunaddr
, len
,0)); 167 static voidunix_remove_socket(unix_socket
*sk
) 169 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 171 sk
->next
->prev
= sk
->prev
; 173 sk
->prev
->next
= sk
->next
; 176 sk
->protinfo
.af_unix
.list
= NULL
; 181 static voidunix_insert_socket(unix_socket
*sk
) 183 unix_socket
**list
= sk
->protinfo
.af_unix
.list
; 191 static unix_socket
*unix_find_socket_byname(struct sockaddr_un
*sunname
, 192 int len
,int type
,unsigned hash
) 196 for(s
=unix_socket_table
[(hash
^type
)&0xF]; s
; s
=s
->next
) 198 if(s
->protinfo
.af_unix
.addr
->len
==len
&& 199 memcmp(s
->protinfo
.af_unix
.addr
->name
, sunname
, len
) ==0&& 209 static unix_socket
*unix_find_socket_byinode(struct inode
*i
) 213 for(s
=unix_socket_table
[i
->i_ino
&0xF]; s
; s
=s
->next
) 215 if(s
->protinfo
.af_unix
.inode
==i
) 225 * Delete a unix socket. We have to allow for deferring this on a timer. 228 static voidunix_destroy_timer(unsigned long data
) 230 unix_socket
*sk
=(unix_socket
*)data
; 231 if(!unix_locked(sk
) && sk
->wmem_alloc
==0) 233 unix_release_addr(sk
->protinfo
.af_unix
.addr
); 242 sk
->timer
.expires
=jiffies
+10*HZ
;/* No real hurry try it every 10 seconds or so */ 243 add_timer(&sk
->timer
); 247 static voidunix_delayed_delete(unix_socket
*sk
) 249 sk
->timer
.data
=(unsigned long)sk
; 250 sk
->timer
.expires
=jiffies
+HZ
;/* Normally 1 second after will clean up. After that we try every 10 */ 251 sk
->timer
.function
=unix_destroy_timer
; 252 add_timer(&sk
->timer
); 255 static voidunix_destroy_socket(unix_socket
*sk
) 259 unix_remove_socket(sk
); 261 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 263 if(sk
->state
==TCP_LISTEN
) 265 unix_socket
*osk
=skb
->sk
; 266 osk
->state
=TCP_CLOSE
; 267 kfree_skb(skb
, FREE_WRITE
);/* Now surplus - free the skb first before the socket */ 268 osk
->state_change(osk
);/* So the connect wakes and cleans up (if any) */ 269 /* osk will be destroyed when it gets to close or the timer fires */ 273 /* passed fds are erased in the kfree_skb hook */ 274 kfree_skb(skb
,FREE_WRITE
); 278 if(sk
->protinfo
.af_unix
.inode
!=NULL
) 280 iput(sk
->protinfo
.af_unix
.inode
); 281 sk
->protinfo
.af_unix
.inode
=NULL
; 284 if(!unix_unlock(sk
) && sk
->wmem_alloc
==0) 286 unix_release_addr(sk
->protinfo
.af_unix
.addr
); 292 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 296 static intunix_listen(struct socket
*sock
,int backlog
) 298 struct sock
*sk
= sock
->sk
; 300 if(sock
->state
!= SS_UNCONNECTED
) 302 if(sock
->type
!=SOCK_STREAM
) 303 return-EOPNOTSUPP
;/* Only stream sockets accept */ 304 if(!sk
->protinfo
.af_unix
.addr
) 305 return-EINVAL
;/* No listens on an unbound socket */ 306 sk
->max_ack_backlog
=backlog
; 307 if(sk
->ack_backlog
< backlog
) 308 sk
->state_change(sk
); 309 sk
->state
=TCP_LISTEN
; 310 sock
->flags
|= SO_ACCEPTCON
; 314 externstruct proto_ops unix_stream_ops
; 315 externstruct proto_ops unix_dgram_ops
; 317 static intunix_create(struct socket
*sock
,int protocol
) 321 sock
->state
= SS_UNCONNECTED
; 323 if(protocol
&& protocol
!= PF_UNIX
) 324 return-EPROTONOSUPPORT
; 329 sock
->ops
= &unix_stream_ops
; 332 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 336 sock
->type
=SOCK_DGRAM
; 338 sock
->ops
= &unix_dgram_ops
; 341 return-ESOCKTNOSUPPORT
; 343 sk
=sk_alloc(GFP_KERNEL
); 347 sock_init_data(sock
,sk
); 349 sk
->protinfo
.af_unix
.family
=AF_UNIX
; 350 sk
->protinfo
.af_unix
.inode
=NULL
; 352 sk
->protinfo
.af_unix
.readsem
=MUTEX
;/* single task reading lock */ 354 sk
->protinfo
.af_unix
.list
=&unix_sockets_unbound
; 355 unix_insert_socket(sk
); 359 static intunix_dup(struct socket
*newsock
,struct socket
*oldsock
) 361 returnunix_create(newsock
,0); 364 static intunix_release(struct socket
*sock
,struct socket
*peer
) 366 unix_socket
*sk
= sock
->sk
; 372 if(sock
->state
!= SS_UNCONNECTED
) 373 sock
->state
= SS_DISCONNECTING
; 375 sk
->state_change(sk
); 377 skpair
=unix_peer(sk
); 378 if(sock
->type
==SOCK_STREAM
&& skpair
) 380 if(unix_our_peer(sk
, skpair
)) 381 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes */ 382 if(skpair
->state
!=TCP_LISTEN
) 383 skpair
->state_change(skpair
);/* Wake any blocked writes */ 386 unix_unlock(skpair
);/* It may now die */ 387 unix_peer(sk
)=NULL
;/* No pair */ 388 unix_destroy_socket(sk
);/* Try to flush out this socket. Throw out buffers at least */ 389 unix_gc();/* Garbage collect fds */ 392 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In 393 * Linux we behave like files and pipes do and wait for the last dereference. 404 static intunix_autobind(struct socket
*sock
) 406 struct sock
*sk
= sock
->sk
; 407 static u32 ordernum
=1; 408 struct unix_address
* addr
; 411 addr
=kmalloc(sizeof(*addr
) +sizeof(short) +16, GFP_KERNEL
); 414 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.inode
) 419 memset(addr
,0,sizeof(*addr
) +sizeof(short) +16); 420 addr
->name
->sun_family
= AF_UNIX
; 424 addr
->len
=sprintf(addr
->name
->sun_path
+1,"%08x", ordernum
) +1+sizeof(short); 425 addr
->hash
=unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
,0)); 428 if((osk
=unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
, 429 addr
->hash
)) != NULL
) 435 sk
->protinfo
.af_unix
.addr
= addr
; 436 unix_remove_socket(sk
); 437 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(addr
->hash
^ sk
->type
)&0xF]; 438 unix_insert_socket(sk
); 442 static unix_socket
*unix_find_other(struct sockaddr_un
*sunname
,int len
, 443 int type
,unsigned hash
,int*error
) 450 if(sunname
->sun_path
[0]) 454 err
=open_namei(sunname
->sun_path
,2, S_IFSOCK
, &inode
, NULL
); 461 u
=unix_find_socket_byinode(inode
); 463 if(u
&& u
->type
!= type
) 471 u
=unix_find_socket_byname(sunname
, len
, type
, hash
); 475 *error
=-ECONNREFUSED
; 482 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 484 struct sock
*sk
= sock
->sk
; 485 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 486 struct inode
* inode
; 490 struct unix_address
*addr
; 492 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.inode
|| 493 sunaddr
->sun_family
!= AF_UNIX
) 496 if(addr_len
==sizeof(short)) 497 returnunix_autobind(sock
); 499 addr_len
=unix_mkname(sunaddr
, addr_len
, &hash
); 503 addr
=kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
); 507 /* We sleeped; recheck ... */ 509 if(sk
->protinfo
.af_unix
.addr
|| sk
->protinfo
.af_unix
.inode
) 512 return-EINVAL
;/* Already bound */ 515 memcpy(addr
->name
, sunaddr
, addr_len
); 516 addr
->len
= addr_len
; 520 if(!sunaddr
->sun_path
[0]) 522 unix_socket
*osk
=unix_find_socket_byname(sunaddr
, addr_len
, 530 unix_remove_socket(sk
); 531 sk
->protinfo
.af_unix
.addr
= addr
; 532 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[(hash
^sk
->type
)&0xF]; 533 unix_insert_socket(sk
); 537 addr
->hash
= UNIX_HASH_SIZE
; 538 sk
->protinfo
.af_unix
.addr
= addr
; 543 err
=do_mknod(sunaddr
->sun_path
, S_IFSOCK
|S_IRWXUGO
,0); 545 err
=open_namei(sunaddr
->sun_path
,2, S_IFSOCK
, &inode
, NULL
); 551 unix_release_addr(addr
); 552 sk
->protinfo
.af_unix
.addr
= NULL
; 558 unix_remove_socket(sk
); 559 sk
->protinfo
.af_unix
.list
= &unix_socket_table
[inode
->i_ino
&0xF]; 560 sk
->protinfo
.af_unix
.inode
= inode
; 561 unix_insert_socket(sk
); 566 static intunix_dgram_connect(struct socket
*sock
,struct sockaddr
*addr
, 569 struct sock
*sk
= sock
->sk
; 570 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
; 576 * 1003.1g breaking connected state with AF_UNSPEC 579 if(addr
->sa_family
==AF_UNSPEC
) 583 unix_unlock(unix_peer(sk
)); 584 unix_peer(sk
) = NULL
; 585 sock
->state
=SS_UNCONNECTED
; 590 alen
=unix_mkname(sunaddr
, alen
, &hash
); 594 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
); 597 if(!unix_may_send(sk
, other
)) 604 * If it was connected, reconnect. 608 unix_unlock(unix_peer(sk
)); 612 if(sock
->passcred
&& !sk
->protinfo
.af_unix
.addr
) 617 static intunix_stream_connect1(struct socket
*sock
,struct msghdr
*msg
, 618 int len
,struct unix_skb_parms
*cmsg
,int nonblock
) 620 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)msg
->msg_name
; 621 struct sock
*sk
= sock
->sk
; 628 addr_len
=unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
); 635 /* This is ok... continue with connect */ 638 /* Socket is already connected */ 641 /* Not yet connected... we will check this. */ 650 if(sock
->state
==SS_CONNECTING
&& sk
->state
==TCP_ESTABLISHED
) 652 sock
->state
=SS_CONNECTED
; 653 if(!sk
->protinfo
.af_unix
.addr
) 657 if(sock
->state
==SS_CONNECTING
&& sk
->state
== TCP_CLOSE
) 659 sock
->state
=SS_UNCONNECTED
; 662 if(sock
->state
!=SS_CONNECTING
) 667 * Drop through the connect up logic to the wait. 671 if(sock
->state
==SS_UNCONNECTED
) 674 * Now ready to connect 677 skb
=sock_alloc_send_skb(sk
, len
,0, nonblock
, &err
);/* Marker object */ 680 memcpy(&UNIXCB(skb
), cmsg
,sizeof(*cmsg
)); 682 memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 684 other
=unix_find_other(sunaddr
, addr_len
, sk
->type
, hash
, &err
); 687 kfree_skb(skb
, FREE_WRITE
); 690 other
->ack_backlog
++; 692 skb_queue_tail(&other
->receive_queue
,skb
); 693 sk
->state
=TCP_SYN_SENT
; 694 sock
->state
=SS_CONNECTING
; 695 other
->data_ready(other
,0);/* Wake up ! */ 699 /* Wait for an accept */ 701 while(sk
->state
==TCP_SYN_SENT
) 705 interruptible_sleep_on(sk
->sleep
); 706 if(current
->signal
& ~current
->blocked
) 711 * Has the other end closed on us ? 714 if(sk
->state
==TCP_CLOSE
) 716 unix_unlock(unix_peer(sk
)); 718 sock
->state
=SS_UNCONNECTED
; 723 * Amazingly it has worked 726 sock
->state
=SS_CONNECTED
; 727 if(!sk
->protinfo
.af_unix
.addr
) 733 static intunix_stream_connect(struct socket
*sock
,struct sockaddr
*uaddr
, 734 int addr_len
,int flags
) 737 struct unix_skb_parms cmsg
; 739 msg
.msg_name
= uaddr
; 740 msg
.msg_namelen
= addr_len
; 743 cmsg
.creds
.pid
= current
->pid
; 744 cmsg
.creds
.uid
= current
->euid
; 745 cmsg
.creds
.gid
= current
->egid
; 747 returnunix_stream_connect1(sock
, &msg
,0, &cmsg
, flags
&O_NONBLOCK
); 750 static intunix_socketpair(struct socket
*socka
,struct socket
*sockb
) 752 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
; 754 /* Join our sockets back to back */ 760 if(ska
->type
!= SOCK_DGRAM
) 762 ska
->state
=TCP_ESTABLISHED
; 763 skb
->state
=TCP_ESTABLISHED
; 764 socka
->state
=SS_CONNECTED
; 765 sockb
->state
=SS_CONNECTED
; 770 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 772 unix_socket
*sk
= sock
->sk
; 773 unix_socket
*newsk
= newsock
->sk
; 777 if(sock
->state
!= SS_UNCONNECTED
) 779 if(!(sock
->flags
& SO_ACCEPTCON
)) 782 if(sock
->type
!=SOCK_STREAM
) 784 if(sk
->state
!=TCP_LISTEN
) 787 if(sk
->protinfo
.af_unix
.addr
) 789 atomic_inc(&sk
->protinfo
.af_unix
.addr
->refcnt
); 790 newsk
->protinfo
.af_unix
.addr
=sk
->protinfo
.af_unix
.addr
; 792 if(sk
->protinfo
.af_unix
.inode
) 794 sk
->protinfo
.af_unix
.inode
->i_count
++; 795 newsk
->protinfo
.af_unix
.inode
=sk
->protinfo
.af_unix
.inode
; 800 skb
=skb_dequeue(&sk
->receive_queue
); 805 interruptible_sleep_on(sk
->sleep
); 806 if(current
->signal
& ~current
->blocked
) 810 if(!(UNIXCB(skb
).attr
& MSG_SYN
)) 813 tsk
->state_change(tsk
); 814 kfree_skb(skb
, FREE_WRITE
); 822 unix_peer(newsk
)=tsk
; 823 unix_peer(tsk
)=newsk
; 824 tsk
->state
=TCP_ESTABLISHED
; 825 newsk
->state
=TCP_ESTABLISHED
; 826 memcpy(&newsk
->peercred
,UNIXCREDS(skb
),sizeof(struct ucred
)); 827 tsk
->peercred
.pid
= current
->pid
; 828 tsk
->peercred
.uid
= current
->euid
; 829 tsk
->peercred
.gid
= current
->egid
; 830 unix_lock(newsk
);/* Swap lock over */ 831 unix_unlock(sk
);/* Locked to child socket not master */ 832 unix_lock(tsk
);/* Back lock */ 833 kfree_skb(skb
, FREE_WRITE
);/* The buffer is just used as a tag */ 834 tsk
->state_change(tsk
);/* Wake up any sleeping connect */ 835 sock_wake_async(tsk
->socket
,0); 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
; 904 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
) 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
); 934 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 935 UNIXCB(skb
).attr
= msg
->msg_flags
; 937 unix_attach_fds(scm
, skb
); 939 memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
); 941 other
=unix_peer(sk
); 942 if(other
&& other
->dead
) 945 * Check with 1003.1g - what should 951 if(sunaddr
== NULL
) { 952 kfree_skb(skb
, FREE_WRITE
); 958 other
=unix_find_other(sunaddr
, namelen
, sk
->type
, hash
, &err
); 962 kfree_skb(skb
, FREE_WRITE
); 965 if(!unix_may_send(sk
, other
)) 968 kfree_skb(skb
, FREE_WRITE
); 973 skb_queue_tail(&other
->receive_queue
, skb
); 974 other
->data_ready(other
,len
); 982 static intunix_stream_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
, 983 struct scm_cookie
*scm
) 985 struct sock
*sk
= sock
->sk
; 987 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 993 if(sock
->flags
& SO_ACCEPTCON
) 996 if(msg
->msg_flags
&MSG_OOB
) 999 if(msg
->msg_flags
&~MSG_DONTWAIT
) 1002 if(msg
->msg_namelen
) { 1003 if(sk
->state
==TCP_ESTABLISHED
) 1013 if(sk
->shutdown
&SEND_SHUTDOWN
) { 1014 send_sig(SIGPIPE
,current
,0); 1021 * Optimisation for the fact that under 0.01% of X messages typically 1027 if(size
>(sk
->sndbuf
-sizeof(struct sk_buff
))/2)/* Keep two messages in the pipe so it schedules better */ 1028 size
=(sk
->sndbuf
-sizeof(struct sk_buff
))/2; 1031 * Keep to page sized kmalloc()'s as various people 1032 * have suggested. Big mallocs stress the vm too 1037 limit
=3500;/* Fall back to a page if we can't grab a big buffer this instant */ 1039 limit
=0;/* Otherwise just grab and wait */ 1045 skb
=sock_alloc_send_skb(sk
,size
,limit
,msg
->msg_flags
&MSG_DONTWAIT
, &err
); 1055 * If you pass two values to the sock_alloc_send_skb 1056 * it tries to grab the large buffer with GFP_BUFFER 1057 * (which can fail easily), and if it fails grab the 1058 * fallback size buffer which is under a page and will 1061 size
=min(size
,skb_tailroom(skb
)); 1063 memcpy(UNIXCREDS(skb
), &scm
->creds
,sizeof(struct ucred
)); 1064 UNIXCB(skb
).attr
= msg
->msg_flags
; 1066 unix_attach_fds(scm
, skb
); 1068 memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
); 1070 other
=unix_peer(sk
); 1072 if(other
->dead
|| (sk
->shutdown
& SEND_SHUTDOWN
)) 1074 kfree_skb(skb
, FREE_WRITE
); 1077 send_sig(SIGPIPE
,current
,0); 1081 skb_queue_tail(&other
->receive_queue
, skb
); 1082 other
->data_ready(other
,size
); 1089 * Sleep until data has arrive. But check for races.. 1092 static voidunix_data_wait(unix_socket
* sk
) 1094 if(!skb_peek(&sk
->receive_queue
)) 1096 sk
->socket
->flags
|= SO_WAITDATA
; 1097 interruptible_sleep_on(sk
->sleep
); 1098 sk
->socket
->flags
&= ~SO_WAITDATA
; 1102 static intunix_dgram_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1103 int flags
,struct scm_cookie
*scm
) 1105 struct sock
*sk
= sock
->sk
; 1106 int noblock
= flags
& MSG_DONTWAIT
; 1107 struct sk_buff
*skb
; 1112 msg
->msg_namelen
=0; 1115 skb
=skb_dequeue(&sk
->receive_queue
); 1119 if(sk
->shutdown
& RCV_SHUTDOWN
) 1124 if(current
->signal
& ~current
->blocked
) 1131 if(skb
->sk
->protinfo
.af_unix
.addr
) 1133 memcpy(msg
->msg_name
, skb
->sk
->protinfo
.af_unix
.addr
->name
, 1134 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1135 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1138 msg
->msg_namelen
=sizeof(short); 1143 else if(size
< skb
->len
) 1144 msg
->msg_flags
|= MSG_TRUNC
; 1146 if(memcpy_toiovec(msg
->msg_iov
, skb
->data
, size
)) { 1147 skb_queue_head(&sk
->receive_queue
, skb
); 1151 scm
->creds
= *UNIXCREDS(skb
); 1153 if(!(flags
& MSG_PEEK
)) 1156 unix_detach_fds(scm
, skb
); 1157 kfree_skb(skb
, FREE_WRITE
); 1160 /* It is questionable: on PEEK we could: 1161 - do not return fds - good, but too simple 8) 1162 - return fds, and do not return them on read (old strategy, 1164 - clone fds (I choosed it for now, it is the most universal 1167 POSIX 1003.1g does not actually define this clearly 1168 at all. POSIX 1003.1g doesn't define a lot of things 1173 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1175 skb_queue_head(&sk
->receive_queue
, skb
); 1180 static intunix_stream_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
, 1181 int flags
,struct scm_cookie
*scm
) 1183 struct sock
*sk
= sock
->sk
; 1184 int noblock
= flags
& MSG_DONTWAIT
; 1185 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1190 if(sock
->flags
& SO_ACCEPTCON
) 1195 if(flags
&MSG_WAITALL
) 1199 msg
->msg_namelen
=0; 1201 /* Lock the socket to prevent queue disordering 1202 * while sleeps in memcpy_tomsg 1205 down(&sk
->protinfo
.af_unix
.readsem
); 1210 struct sk_buff
*skb
; 1212 skb
=skb_dequeue(&sk
->receive_queue
); 1215 if(copied
>= target
) 1219 returnsock_error(sk
); 1221 if(sk
->shutdown
& RCV_SHUTDOWN
) 1223 up(&sk
->protinfo
.af_unix
.readsem
); 1227 if(current
->signal
& ~current
->blocked
) 1229 down(&sk
->protinfo
.af_unix
.readsem
); 1233 /* Never glue messages from different writers */ 1235 memcmp(UNIXCREDS(skb
), &scm
->creds
,sizeof(scm
->creds
)) !=0) 1237 skb_queue_head(&sk
->receive_queue
, skb
); 1241 /* Copy address just once */ 1244 if(skb
->sk
->protinfo
.af_unix
.addr
) 1246 memcpy(sunaddr
, skb
->sk
->protinfo
.af_unix
.addr
->name
, 1247 skb
->sk
->protinfo
.af_unix
.addr
->len
); 1248 msg
->msg_namelen
=skb
->sk
->protinfo
.af_unix
.addr
->len
; 1251 msg
->msg_namelen
=sizeof(short); 1255 chunk
=min(skb
->len
, size
); 1256 memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
); 1260 /* Copy credentials */ 1261 scm
->creds
= *UNIXCREDS(skb
); 1264 /* Mark read part of skb as used */ 1265 if(!(flags
& MSG_PEEK
)) 1267 skb_pull(skb
, chunk
); 1270 unix_detach_fds(scm
, skb
); 1272 /* put the skb back if we didn't use it up.. */ 1275 skb_queue_head(&sk
->receive_queue
, skb
); 1279 kfree_skb(skb
, FREE_WRITE
); 1286 /* It is questionable, see note in unix_dgram_recvmsg. 1290 scm
->fp
=scm_fp_dup(UNIXCB(skb
).fp
); 1292 /* put message back and return */ 1293 skb_queue_head(&sk
->receive_queue
, skb
); 1298 up(&sk
->protinfo
.af_unix
.readsem
); 1302 static intunix_shutdown(struct socket
*sock
,int mode
) 1304 struct sock
*sk
= sock
->sk
; 1305 unix_socket
*other
=unix_peer(sk
); 1307 if(mode
&SEND_SHUTDOWN
) 1309 sk
->shutdown
|=SEND_SHUTDOWN
; 1310 sk
->state_change(sk
); 1311 if(other
&& sk
->type
== SOCK_STREAM
&& other
->state
!= TCP_LISTEN
) 1313 if(unix_our_peer(sk
, other
)) 1314 other
->shutdown
|=RCV_SHUTDOWN
; 1315 other
->state_change(other
); 1318 other
=unix_peer(sk
); 1319 if(mode
&RCV_SHUTDOWN
) 1321 sk
->shutdown
|=RCV_SHUTDOWN
; 1322 sk
->state_change(sk
); 1323 if(other
&& sk
->type
!= SOCK_DGRAM
&& other
->state
!= TCP_LISTEN
) 1325 if(unix_our_peer(sk
, other
)) 1326 other
->shutdown
|=SEND_SHUTDOWN
; 1327 other
->state_change(other
); 1334 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1336 struct sock
*sk
= sock
->sk
; 1343 amount
=sk
->sndbuf
-sk
->wmem_alloc
; 1346 returnput_user(amount
, (int*)arg
); 1349 struct sk_buff
*skb
; 1350 if(sk
->state
==TCP_LISTEN
) 1353 * These two are safe on current systems as 1354 * only user tasks fiddle here 1356 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1358 returnput_user(amount
, (int*)arg
); 1368 #ifdef CONFIG_PROC_FS 1369 static intunix_read_proc(char*buffer
,char**start
, off_t offset
, 1370 int length
,int*eof
,void*data
) 1378 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1381 forall_unix_sockets(i
,s
) 1383 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X %5ld", 1387 s
->socket
? s
->socket
->flags
:0, 1389 s
->socket
? s
->socket
->state
:0, 1390 s
->socket
? s
->socket
->inode
->i_ino
:0); 1392 if(s
->protinfo
.af_unix
.addr
) 1395 memcpy(buffer
+len
, s
->protinfo
.af_unix
.addr
->name
->sun_path
, 1396 s
->protinfo
.af_unix
.addr
->len
-sizeof(short)); 1397 if(!UNIX_ABSTRACT(s
)) 1401 len
+= s
->protinfo
.af_unix
.addr
->len
-sizeof(short); 1411 if(pos
>offset
+length
) 1416 *start
=buffer
+(offset
-begin
); 1417 len
-=(offset
-begin
); 1424 struct proto_ops unix_stream_ops
= { 1430 unix_stream_connect
, 1441 unix_stream_sendmsg
, 1445 struct proto_ops unix_dgram_ops
= { 1466 struct net_proto_family unix_family_ops
= { 1471 voidunix_proto_init(struct net_proto
*pro
) 1473 struct sk_buff
*dummy_skb
; 1474 struct proc_dir_entry
*ent
; 1476 printk(KERN_INFO
"NET3: Unix domain sockets 0.15 for Linux NET3.038.\n"); 1477 if(sizeof(struct unix_skb_parms
) >sizeof(dummy_skb
->cb
)) 1479 printk(KERN_CRIT
"unix_proto_init: panic\n"); 1482 sock_register(&unix_family_ops
); 1483 #ifdef CONFIG_PROC_FS 1484 ent
=create_proc_entry("net/unix",0,0); 1485 ent
->read_proc
= unix_read_proc
; 1490 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"