2 * NET3: Implementation of BSD Unix domain sockets. 4 * Authors: Alan Cox, <alan@cymru.net> 6 * Currently this contains all but the file descriptor passing code. 7 * Before that goes in the odd bugs in the iovec handlers need 8 * fixing, and this bit testing. BSD fd passing is not a trivial part 9 * of the exercise it turns out. Anyone like writing garbage collectors. 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation; either version 14 * 2 of the License, or (at your option) any later version. 17 * Linus Torvalds : Assorted bug cures. 18 * Niibe Yutaka : async I/O support. 19 * Carsten Paeth : PF_UNIX check, address fixes. 20 * Alan Cox : Limit size of allocated blocks. 21 * Alan Cox : Fixed the stupid socketpair bug. 22 * Alan Cox : BSD compatibility fine tuning. 23 * Alan Cox : Fixed a bug in connect when interrupted. 24 * Alan Cox : Sorted out a proper draft version of 25 * file descriptor passing hacked up from 27 * Marty Leisner : Fixes to fd passing 28 * Nick Nevin : recvmsg bugfix. 29 * Alan Cox : Started proper garbage collector 31 * Known differences from reference BSD that was tested: 34 * ECONNREFUSED is not returned from one end of a connected() socket to the 35 * other the moment one end closes. 36 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark 37 * and a fake inode identifier (nor the BSD first socket fstat twice bug). 39 * accept() returns a path name even if the connecting socket has closed 40 * in the meantime (BSD loses the path and gives up). 41 * accept() returns 0 length path for an unbound connector. BSD returns 16 42 * and a null first byte in the path (but not for gethost/peername - BSD bug ??) 43 * socketpair(...SOCK_RAW..) doesnt panic the kernel. 44 * BSD af_unix apprently has connect forgetting to block properly. 47 #include <linux/config.h> 48 #include <linux/kernel.h> 49 #include <linux/major.h> 50 #include <linux/signal.h> 51 #include <linux/sched.h> 52 #include <linux/errno.h> 53 #include <linux/string.h> 54 #include <linux/stat.h> 55 #include <linux/socket.h> 57 #include <linux/fcntl.h> 58 #include <linux/termios.h> 59 #include <linux/socket.h> 60 #include <linux/sockios.h> 61 #include <linux/net.h> 64 #include <linux/malloc.h> 65 #include <asm/segment.h> 66 #include <linux/skbuff.h> 67 #include <linux/netdevice.h> 70 #include <net/af_unix.h> 71 #include <linux/proc_fs.h> 73 unix_socket
*unix_socket_list
=NULL
; 75 #define min(a,b) (((a)<(b))?(a):(b)) 78 * Make sure the unix name is null-terminated. 81 staticinlinevoidunix_mkname(struct sockaddr_un
* sunaddr
,unsigned long len
) 83 if(len
>=sizeof(*sunaddr
)) 84 len
=sizeof(*sunaddr
)-1; 85 ((char*)sunaddr
)[len
]=0; 89 * Note: Sockets may not be removed _during_ an interrupt or net_bh 90 * handler using this technique. They can be added although we do not 94 static voidunix_remove_socket(unix_socket
*sk
) 114 static voidunix_insert_socket(unix_socket
*sk
) 117 sk
->next
=unix_socket_list
; 122 static unix_socket
*unix_find_socket(struct inode
*i
) 129 if(s
->protinfo
.af_unix
.inode
==i
) 141 * Delete a unix socket. We have to allow for deferring this on a timer. 144 static voidunix_destroy_timer(unsigned long data
) 146 unix_socket
*sk
=(unix_socket
*)data
; 147 if(sk
->protinfo
.af_unix
.locks
==0&& sk
->wmem_alloc
==0) 149 if(sk
->protinfo
.af_unix
.name
) 150 kfree(sk
->protinfo
.af_unix
.name
); 159 sk
->timer
.expires
=jiffies
+10*HZ
;/* No real hurry try it every 10 seconds or so */ 160 add_timer(&sk
->timer
); 164 static voidunix_delayed_delete(unix_socket
*sk
) 166 sk
->timer
.data
=(unsigned long)sk
; 167 sk
->timer
.expires
=jiffies
+HZ
;/* Normally 1 second after will clean up. After that we try every 10 */ 168 sk
->timer
.function
=unix_destroy_timer
; 169 add_timer(&sk
->timer
); 172 static voidunix_destroy_socket(unix_socket
*sk
) 176 unix_remove_socket(sk
); 178 while((skb
=skb_dequeue(&sk
->receive_queue
))!=NULL
) 180 if(sk
->state
==TCP_LISTEN
) 182 unix_socket
*osk
=skb
->sk
; 183 osk
->state
=TCP_CLOSE
; 184 kfree_skb(skb
, FREE_WRITE
);/* Now surplus - free the skb first before the socket */ 185 osk
->state_change(osk
);/* So the connect wakes and cleans up (if any) */ 186 /* osk will be destroyed when it gets to close or the timer fires */ 190 /* passed fds are erased where?? */ 191 kfree_skb(skb
,FREE_WRITE
); 195 if(sk
->protinfo
.af_unix
.inode
!=NULL
) 197 iput(sk
->protinfo
.af_unix
.inode
); 198 sk
->protinfo
.af_unix
.inode
=NULL
; 201 if(--sk
->protinfo
.af_unix
.locks
==0&& sk
->wmem_alloc
==0) 203 if(sk
->protinfo
.af_unix
.name
) 204 kfree(sk
->protinfo
.af_unix
.name
); 210 unix_delayed_delete(sk
);/* Try every so often until buffers are all freed */ 215 * Fixme: We need async I/O on AF_UNIX doing next. 218 static intunix_fcntl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 224 * Yes socket options work with the new unix domain socketry!!!!!!! 227 static intunix_setsockopt(struct socket
*sock
,int level
,int optname
,char*optval
,int optlen
) 229 unix_socket
*sk
=sock
->data
; 230 if(level
!=SOL_SOCKET
) 232 returnsock_setsockopt(sk
,level
,optname
,optval
,optlen
); 235 static intunix_getsockopt(struct socket
*sock
,int level
,int optname
,char*optval
,int*optlen
) 237 unix_socket
*sk
=sock
->data
; 238 if(level
!=SOL_SOCKET
) 240 returnsock_getsockopt(sk
,level
,optname
,optval
,optlen
); 243 static intunix_listen(struct socket
*sock
,int backlog
) 245 unix_socket
*sk
=sock
->data
; 246 if(sk
->type
!=SOCK_STREAM
) 247 return-EOPNOTSUPP
;/* Only stream sockets accept */ 248 if(sk
->protinfo
.af_unix
.name
==NULL
) 249 return-EINVAL
;/* No listens on an unbound socket */ 250 sk
->max_ack_backlog
=backlog
; 251 sk
->state
=TCP_LISTEN
; 255 static voiddef_callback1(struct sock
*sk
) 258 wake_up_interruptible(sk
->sleep
); 261 static voiddef_callback2(struct sock
*sk
,int len
) 265 wake_up_interruptible(sk
->sleep
); 266 sock_wake_async(sk
->socket
,1); 270 static voiddef_callback3(struct sock
*sk
) 274 wake_up_interruptible(sk
->sleep
); 275 sock_wake_async(sk
->socket
,2); 279 static intunix_create(struct socket
*sock
,int protocol
) 282 if(protocol
&& protocol
!= PF_UNIX
) 283 return-EPROTONOSUPPORT
; 284 sk
=(unix_socket
*)sk_alloc(GFP_KERNEL
); 292 * Believe it or not BSD has AF_UNIX, SOCK_RAW though 296 sock
->type
=SOCK_DGRAM
; 301 return-ESOCKTNOSUPPORT
; 304 init_timer(&sk
->timer
); 305 skb_queue_head_init(&sk
->write_queue
); 306 skb_queue_head_init(&sk
->receive_queue
); 307 skb_queue_head_init(&sk
->back_log
); 308 sk
->protinfo
.af_unix
.family
=AF_UNIX
; 309 sk
->protinfo
.af_unix
.inode
=NULL
; 310 sk
->protinfo
.af_unix
.locks
=1;/* Us */ 311 sk
->protinfo
.af_unix
.readsem
=MUTEX
;/* single task reading lock */ 312 sk
->protinfo
.af_unix
.name
=NULL
; 313 sk
->protinfo
.af_unix
.other
=NULL
; 320 sk
->rcvbuf
=SK_RMEM_MAX
; 321 sk
->sndbuf
=SK_WMEM_MAX
; 322 sk
->allocation
=GFP_KERNEL
; 331 sk
->priority
=SOPRI_NORMAL
; 334 sk
->state_change
=def_callback1
; 335 sk
->data_ready
=def_callback2
; 336 sk
->write_space
=def_callback3
; 337 sk
->error_report
=def_callback1
; 340 sock
->data
=(void*)sk
; 341 sk
->sleep
=sock
->wait
; 343 unix_insert_socket(sk
); 347 static intunix_dup(struct socket
*newsock
,struct socket
*oldsock
) 349 returnunix_create(newsock
,0); 352 static intunix_release(struct socket
*sock
,struct socket
*peer
) 354 unix_socket
*sk
=sock
->data
; 357 /* May not have data attached */ 362 sk
->state_change(sk
); 364 skpair
=(unix_socket
*)sk
->protinfo
.af_unix
.other
;/* Person we send to (default) */ 365 if(sk
->type
==SOCK_STREAM
&& skpair
!=NULL
&& skpair
->state
!=TCP_LISTEN
) 367 skpair
->shutdown
=SHUTDOWN_MASK
;/* No more writes */ 368 skpair
->state_change(skpair
);/* Wake any blocked writes */ 371 skpair
->protinfo
.af_unix
.locks
--;/* It may now die */ 372 sk
->protinfo
.af_unix
.other
=NULL
;/* No pair */ 373 unix_destroy_socket(sk
);/* Try and flush out this socket. Throw our buffers at least */ 376 * FIXME: BSD difference: In BSD all sockets connected to use get ECONNRESET and we die on the spot. In 377 * Linux we behave like files and pipes do and wait for the last dereference. 384 static unix_socket
*unix_find_other(char*path
,int*error
) 393 err
=open_namei(path
,2, S_IFSOCK
, &inode
, NULL
); 400 u
=unix_find_socket(inode
); 404 *error
=-ECONNREFUSED
; 411 static intunix_bind(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
) 413 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 414 unix_socket
*sk
=sock
->data
; 418 if(sk
->protinfo
.af_unix
.name
) 419 return-EINVAL
;/* Already bound */ 421 if(addr_len
>sizeof(struct sockaddr_un
) || addr_len
<3|| sunaddr
->sun_family
!=AF_UNIX
) 423 unix_mkname(sunaddr
, addr_len
); 425 * Put ourselves in the filesystem 427 if(sk
->protinfo
.af_unix
.inode
!=NULL
) 430 sk
->protinfo
.af_unix
.name
=kmalloc(addr_len
+1, GFP_KERNEL
); 431 if(sk
->protinfo
.af_unix
.name
==NULL
) 433 memcpy(sk
->protinfo
.af_unix
.name
, sunaddr
->sun_path
, addr_len
+1); 438 err
=do_mknod(sk
->protinfo
.af_unix
.name
,S_IFSOCK
|S_IRWXUGO
,0); 440 err
=open_namei(sk
->protinfo
.af_unix
.name
,2, S_IFSOCK
, &sk
->protinfo
.af_unix
.inode
, NULL
); 446 kfree_s(sk
->protinfo
.af_unix
.name
,addr_len
+1); 447 sk
->protinfo
.af_unix
.name
=NULL
; 458 static intunix_connect(struct socket
*sock
,struct sockaddr
*uaddr
,int addr_len
,int flags
) 460 unix_socket
*sk
=sock
->data
; 461 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 466 if(sk
->type
==SOCK_STREAM
&& sk
->protinfo
.af_unix
.other
) 468 if(sock
->state
==SS_CONNECTING
&& sk
->state
==TCP_ESTABLISHED
) 470 sock
->state
=SS_CONNECTED
; 473 if(sock
->state
==SS_CONNECTING
&& sk
->state
== TCP_CLOSE
) 475 sock
->state
=SS_UNCONNECTED
; 478 if(sock
->state
!=SS_CONNECTING
) 483 * Drop through the connect up logic to the wait. 487 if(addr_len
<sizeof(sunaddr
->sun_family
)+1|| sunaddr
->sun_family
!=AF_UNIX
) 490 unix_mkname(sunaddr
, addr_len
); 492 if(sk
->type
==SOCK_DGRAM
) 494 if(sk
->protinfo
.af_unix
.other
) 496 sk
->protinfo
.af_unix
.other
->protinfo
.af_unix
.locks
--; 497 sk
->protinfo
.af_unix
.other
=NULL
; 498 sock
->state
=SS_UNCONNECTED
; 500 other
=unix_find_other(sunaddr
->sun_path
, &err
); 503 if(other
->type
!=sk
->type
) 505 other
->protinfo
.af_unix
.locks
++; 506 sk
->protinfo
.af_unix
.other
=other
; 507 sock
->state
=SS_CONNECTED
; 508 sk
->state
=TCP_ESTABLISHED
; 513 if(sock
->state
==SS_UNCONNECTED
) 516 * Now ready to connect 519 skb
=sock_alloc_send_skb(sk
,0,0,0, &err
);/* Marker object */ 522 skb
->sk
=sk
;/* So they know it is us */ 525 unix_mkname(sunaddr
, addr_len
); 526 other
=unix_find_other(sunaddr
->sun_path
, &err
); 529 kfree_skb(skb
, FREE_WRITE
); 532 if(other
->type
!=sk
->type
) 534 kfree_skb(skb
, FREE_WRITE
); 537 other
->protinfo
.af_unix
.locks
++;/* Lock the other socket so it doesn't run off for a moment */ 538 other
->ack_backlog
++; 539 sk
->protinfo
.af_unix
.other
=other
; 540 skb_queue_tail(&other
->receive_queue
,skb
); 541 sk
->state
=TCP_SYN_SENT
; 542 sock
->state
=SS_CONNECTING
; 544 other
->data_ready(other
,0);/* Wake up ! */ 548 /* Wait for an accept */ 551 while(sk
->state
==TCP_SYN_SENT
) 558 interruptible_sleep_on(sk
->sleep
); 559 if(current
->signal
& ~current
->blocked
) 567 * Has the other end closed on us ? 570 if(sk
->state
==TCP_CLOSE
) 572 sk
->protinfo
.af_unix
.other
->protinfo
.af_unix
.locks
--; 573 sk
->protinfo
.af_unix
.other
=NULL
; 574 sock
->state
=SS_UNCONNECTED
; 580 * Amazingly it has worked 583 sock
->state
=SS_CONNECTED
; 589 static intunix_socketpair(struct socket
*a
,struct socket
*b
) 591 unix_socket
*ska
,*skb
; 596 /* Join our sockets back to back */ 597 ska
->protinfo
.af_unix
.locks
++; 598 skb
->protinfo
.af_unix
.locks
++; 599 ska
->protinfo
.af_unix
.other
=skb
; 600 skb
->protinfo
.af_unix
.other
=ska
; 601 ska
->state
=TCP_ESTABLISHED
; 602 skb
->state
=TCP_ESTABLISHED
; 606 static intunix_accept(struct socket
*sock
,struct socket
*newsock
,int flags
) 608 unix_socket
*sk
=sock
->data
; 609 unix_socket
*newsk
, *tsk
; 612 if(sk
->type
!=SOCK_STREAM
) 616 if(sk
->state
!=TCP_LISTEN
) 622 if(sk
->protinfo
.af_unix
.name
!=NULL
) 624 newsk
->protinfo
.af_unix
.name
=kmalloc(strlen(sk
->protinfo
.af_unix
.name
)+1, GFP_KERNEL
); 625 if(newsk
->protinfo
.af_unix
.name
==NULL
) 627 strcpy(newsk
->protinfo
.af_unix
.name
, sk
->protinfo
.af_unix
.name
); 633 skb
=skb_dequeue(&sk
->receive_queue
); 641 interruptible_sleep_on(sk
->sleep
); 642 if(current
->signal
& ~current
->blocked
) 652 kfree_skb(skb
, FREE_WRITE
);/* The buffer is just used as a tag */ 654 newsk
->protinfo
.af_unix
.other
=tsk
; 655 tsk
->protinfo
.af_unix
.other
=newsk
; 656 tsk
->state
=TCP_ESTABLISHED
; 657 newsk
->state
=TCP_ESTABLISHED
; 658 newsk
->protinfo
.af_unix
.locks
++;/* Swap lock over */ 659 sk
->protinfo
.af_unix
.locks
--;/* Locked to child socket not master */ 660 tsk
->protinfo
.af_unix
.locks
++;/* Back lock */ 662 tsk
->state_change(tsk
);/* Wake up any sleeping connect */ 663 sock_wake_async(tsk
->socket
,0); 667 static intunix_getname(struct socket
*sock
,struct sockaddr
*uaddr
,int*uaddr_len
,int peer
) 669 unix_socket
*sk
=sock
->data
; 670 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
; 674 if(sk
->protinfo
.af_unix
.other
==NULL
) 676 sk
=sk
->protinfo
.af_unix
.other
; 678 sunaddr
->sun_family
=AF_UNIX
; 679 if(sk
->protinfo
.af_unix
.name
==NULL
) 681 *sunaddr
->sun_path
=0; 682 *uaddr_len
=sizeof(sunaddr
->sun_family
)+1; 683 return0;/* Not bound */ 685 *uaddr_len
=sizeof(sunaddr
->sun_family
)+strlen(sk
->protinfo
.af_unix
.name
)+1; 686 strcpy(sunaddr
->sun_path
,sk
->protinfo
.af_unix
.name
);/* 108 byte limited */ 691 * Support routines for struct cmsghdr handling 694 static struct cmsghdr
*unix_copyrights(void*userp
,int len
) 698 if(len
>256|| len
<=0) 700 cm
=kmalloc(len
, GFP_KERNEL
); 701 memcpy_fromfs(cm
, userp
, len
); 706 * Return a header block 709 static voidunix_returnrights(void*userp
,int len
,struct cmsghdr
*cm
) 711 memcpy_tofs(userp
, cm
, len
); 716 * Copy file descriptors into system space. 717 * Return number copied or negative error code 720 static intunix_fd_copy(struct sock
*sk
,struct cmsghdr
*cmsg
,struct file
**fp
) 722 int num
=cmsg
->cmsg_len
-sizeof(struct cmsghdr
); 724 int*fdp
=(int*)cmsg
->cmsg_data
; 725 num
/=4;/* Odd bytes are forgotten in BSD not errored */ 732 * Verify the descriptors. 735 for(i
=0; i
< num
; i
++) 741 printk("testing fd %d\n", fd
); 743 if(fd
<0|| fd
>=NR_OPEN
) 745 if(current
->files
->fd
[fd
]==NULL
) 749 /* add another reference to these files */ 750 for(i
=0; i
< num
; i
++) 752 fp
[i
]=current
->files
->fd
[fdp
[i
]]; 754 unix_inflight(fp
[i
]); 761 * Free the descriptors in the array 764 static voidunix_fd_free(struct sock
*sk
,struct file
**fp
,int num
) 770 unix_notinflight(fp
[i
]); 775 * Count the free descriptors available to a process. 776 * Interpretation issue: Is the limit the highest descriptor (buggy 777 * allowing passed fd's higher up to cause a limit to be exceeded) - 778 * but how the old code did it - or like this... 781 static intunix_files_free(void) 785 for(i
=0;i
<NR_OPEN
;i
++) 787 if(current
->files
->fd
[i
]) 792 if(i
>current
->rlim
[RLIMIT_NOFILE
].rlim_cur
) 793 i
=current
->rlim
[RLIMIT_NOFILE
].rlim_cur
; 800 * Perform the AF_UNIX file descriptor pass out functionality. This 801 * is nasty and messy as is the whole design of BSD file passing. 804 static voidunix_detach_fds(struct sk_buff
*skb
,struct cmsghdr
*cmsg
) 807 /* count of space in parent for fds */ 811 int*cmfptr
=NULL
;/* =NULL To keep gcc happy */ 812 /* number of fds actually passed */ 821 cmnum
=cmsg
->cmsg_len
-sizeof(struct cmsghdr
); 823 cmfptr
=(int*)&cmsg
->cmsg_data
; 826 memcpy(&fdnum
,skb
->h
.filp
,sizeof(int)); 827 fp
=(struct file
**)(skb
->h
.filp
+sizeof(int)); 830 ffree
=unix_files_free(); 833 ufp
=¤t
->files
->fd
[0]; 836 * Copy those that fit 843 while(ufp
[ufn
]!=NULL
) 847 FD_CLR(ufn
,¤t
->files
->close_on_exec
); 848 unix_notinflight(fp
[i
]); 851 * Dump those that don't 856 unix_notinflight(fp
[i
]); 861 /* no need to use destructor */ 862 skb
->destructor
= NULL
; 865 static voidunix_destruct_fds(struct sk_buff
*skb
) 867 unix_detach_fds(skb
,NULL
); 871 * Attach the file descriptor array to an sk_buff 873 static voidunix_attach_fds(int fpnum
,struct file
**fp
,struct sk_buff
*skb
) 876 skb
->h
.filp
=kmalloc(sizeof(int)+fpnum
*sizeof(struct file
*), 878 /* number of descriptors starts block */ 879 memcpy(skb
->h
.filp
,&fpnum
,sizeof(int)); 880 /* actual descriptors */ 881 memcpy(skb
->h
.filp
+sizeof(int),fp
,fpnum
*sizeof(struct file
*)); 882 skb
->destructor
= unix_destruct_fds
; 889 static intunix_sendmsg(struct socket
*sock
,struct msghdr
*msg
,int len
,int nonblock
,int flags
) 891 unix_socket
*sk
=sock
->data
; 893 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 898 struct file
*fp
[UNIX_MAX_FD
]; 899 /* number of fds waiting to be passed, 0 means either 900 * no fds to pass or they've already been passed 905 returnsock_error(sk
); 910 if(flags
)/* For now */{ 916 if(sock
->type
==SOCK_STREAM
) 918 if(sk
->state
==TCP_ESTABLISHED
) 927 if(sk
->protinfo
.af_unix
.other
==NULL
) 932 * A control message has been attached. 934 if(msg
->msg_accrights
) 936 struct cmsghdr
*cm
=unix_copyrights(msg
->msg_accrights
, 937 msg
->msg_accrightslen
); 938 if(cm
==NULL
|| msg
->msg_accrightslen
<sizeof(struct cmsghdr
) || 939 cm
->cmsg_type
!=SCM_RIGHTS
|| 940 cm
->cmsg_level
!=SOL_SOCKET
|| 941 msg
->msg_accrightslen
!=cm
->cmsg_len
) 946 fpnum
=unix_fd_copy(sk
,cm
,fp
); 956 * Optimisation for the fact that under 0.01% of X messages typically 962 if(size
>(sk
->sndbuf
-sizeof(struct sk_buff
))/2)/* Keep two messages in the pipe so it schedules better */ 964 if(sock
->type
==SOCK_DGRAM
) 966 unix_fd_free(sk
,fp
,fpnum
); 969 size
=(sk
->sndbuf
-sizeof(struct sk_buff
))/2; 972 * Keep to page sized kmalloc()'s as various people 973 * have suggested. Big mallocs stress the vm too 977 if(size
>4000&& sock
->type
!=SOCK_DGRAM
) 978 limit
=4000;/* Fall back to 4K if we can't grab a big buffer this instant */ 980 limit
=0;/* Otherwise just grab and wait */ 986 skb
=sock_alloc_send_skb(sk
,size
,limit
,nonblock
, &err
); 990 unix_fd_free(sk
,fp
,fpnum
); 998 size
=skb_tailroom(skb
);/* If we dropped back on a limit then our skb is smaller */ 1005 unix_attach_fds(fpnum
,fp
,skb
); 1011 memcpy_fromiovec(skb_put(skb
,size
),msg
->msg_iov
, size
); 1016 other
=sk
->protinfo
.af_unix
.other
; 1017 if(sock
->type
==SOCK_DGRAM
&& other
->dead
) 1019 other
->protinfo
.af_unix
.locks
--; 1020 sk
->protinfo
.af_unix
.other
=NULL
; 1021 sock
->state
=SS_UNCONNECTED
; 1023 kfree_skb(skb
, FREE_WRITE
); 1032 unix_mkname(sunaddr
, msg
->msg_namelen
); 1033 other
=unix_find_other(sunaddr
->sun_path
, &err
); 1037 kfree_skb(skb
, FREE_WRITE
); 1044 skb_queue_tail(&other
->receive_queue
, skb
); 1046 /* if we sent an fd, only do it once */ 1047 other
->data_ready(other
,size
); 1054 * Sleep until data has arrive. But check for races.. 1057 static voidunix_data_wait(unix_socket
* sk
) 1060 if(!skb_peek(&sk
->receive_queue
)) { 1061 sk
->socket
->flags
|= SO_WAITDATA
; 1062 interruptible_sleep_on(sk
->sleep
); 1063 sk
->socket
->flags
&= ~SO_WAITDATA
; 1068 static intunix_recvmsg(struct socket
*sock
,struct msghdr
*msg
,int size
,int noblock
,int flags
,int*addr_len
) 1070 unix_socket
*sk
=sock
->data
; 1071 struct sockaddr_un
*sunaddr
=msg
->msg_name
; 1072 struct sk_buff
*skb
; 1077 struct iovec
*iov
=msg
->msg_iov
; 1078 struct cmsghdr
*cm
=NULL
; 1079 int ct
=msg
->msg_iovlen
; 1088 returnsock_error(sk
); 1090 if(msg
->msg_accrights
) 1092 cm
=unix_copyrights(msg
->msg_accrights
, 1093 msg
->msg_accrightslen
); 1094 if(msg
->msg_accrightslen
<sizeof(struct cmsghdr
) 1096 /* investigate this furthur -- Stevens example doen't seem to care */ 1098 cm
->cmsg_type
!=SCM_RIGHTS
|| 1099 cm
->cmsg_level
!=SOL_SOCKET
|| 1100 msg
->msg_accrightslen
!=cm
->cmsg_len
1105 printk("recvmsg: Bad msg_accrights\n"); 1110 down(&sk
->protinfo
.af_unix
.readsem
);/* Lock the socket */ 1120 if(copied
&& (flags
& MSG_PEEK
)) 1124 skb
=skb_dequeue(&sk
->receive_queue
); 1127 up(&sk
->protinfo
.af_unix
.readsem
); 1128 if(sk
->shutdown
& RCV_SHUTDOWN
) 1134 if(current
->signal
& ~current
->blocked
) 1137 down(&sk
->protinfo
.af_unix
.readsem
); 1140 if(msg
->msg_name
!=NULL
) 1142 sunaddr
->sun_family
=AF_UNIX
; 1143 if(skb
->sk
->protinfo
.af_unix
.name
) 1145 memcpy(sunaddr
->sun_path
, skb
->sk
->protinfo
.af_unix
.name
,108); 1147 *addr_len
=strlen(sunaddr
->sun_path
)+sizeof(short); 1151 *addr_len
=sizeof(short); 1154 num
=min(skb
->len
,len
-done
); 1155 memcpy_tofs(sp
, skb
->data
, num
); 1157 if(skb
->h
.filp
!=NULL
) 1158 unix_detach_fds(skb
,cm
); 1163 if(!(flags
& MSG_PEEK
)) 1165 /* put the skb back if we didn't use it up.. */ 1167 skb_queue_head(&sk
->receive_queue
, skb
); 1170 kfree_skb(skb
, FREE_WRITE
); 1171 if(sock
->type
==SOCK_DGRAM
|| cm
) 1176 up(&sk
->protinfo
.af_unix
.readsem
); 1178 unix_returnrights(msg
->msg_accrights
,msg
->msg_accrightslen
,cm
); 1182 static intunix_shutdown(struct socket
*sock
,int mode
) 1184 unix_socket
*sk
=(unix_socket
*)sock
->data
; 1185 unix_socket
*other
=sk
->protinfo
.af_unix
.other
; 1186 if(mode
&SEND_SHUTDOWN
) 1188 sk
->shutdown
|=SEND_SHUTDOWN
; 1189 sk
->state_change(sk
); 1192 other
->shutdown
|=RCV_SHUTDOWN
; 1193 other
->state_change(other
); 1196 other
=sk
->protinfo
.af_unix
.other
; 1197 if(mode
&RCV_SHUTDOWN
) 1199 sk
->shutdown
|=RCV_SHUTDOWN
; 1200 sk
->state_change(sk
); 1203 other
->shutdown
|=SEND_SHUTDOWN
; 1204 other
->state_change(other
); 1211 static intunix_select(struct socket
*sock
,int sel_type
, select_table
*wait
) 1213 returndatagram_select(sock
->data
,sel_type
,wait
); 1216 static intunix_ioctl(struct socket
*sock
,unsigned int cmd
,unsigned long arg
) 1218 unix_socket
*sk
=sock
->data
; 1226 err
=verify_area(VERIFY_WRITE
,(void*)arg
,sizeof(unsigned long)); 1229 amount
=sk
->sndbuf
-sk
->wmem_alloc
; 1232 put_fs_long(amount
,(unsigned long*)arg
); 1236 struct sk_buff
*skb
; 1237 if(sk
->state
==TCP_LISTEN
) 1239 /* These two are safe on a single CPU system as only user tasks fiddle here */ 1240 if((skb
=skb_peek(&sk
->receive_queue
))!=NULL
) 1242 err
=verify_area(VERIFY_WRITE
,(void*)arg
,sizeof(unsigned long)); 1243 put_fs_long(amount
,(unsigned long*)arg
); 1254 #ifdef CONFIG_PROC_FS 1255 static intunix_get_info(char*buffer
,char**start
, off_t offset
,int length
,int dummy
) 1260 unix_socket
*s
=unix_socket_list
; 1262 len
+=sprintf(buffer
,"Num RefCount Protocol Flags Type St " 1267 len
+=sprintf(buffer
+len
,"%p: %08X %08X %08lX %04X %02X %5ld", 1269 s
->protinfo
.af_unix
.locks
, 1274 s
->socket
->inode
? s
->socket
->inode
->i_ino
:0); 1275 if(s
->protinfo
.af_unix
.name
!=NULL
) 1276 len
+=sprintf(buffer
+len
," %s\n", s
->protinfo
.af_unix
.name
); 1286 if(pos
>offset
+length
) 1290 *start
=buffer
+(offset
-begin
); 1291 len
-=(offset
-begin
); 1298 struct proto_ops unix_proto_ops
= { 1321 voidunix_proto_init(struct net_proto
*pro
) 1323 printk("NET3: Unix domain sockets 0.12 for Linux NET3.033.\n"); 1324 sock_register(unix_proto_ops
.family
, &unix_proto_ops
); 1325 #ifdef CONFIG_PROC_FS 1326 proc_net_register(&(struct proc_dir_entry
) { 1327 PROC_NET_UNIX
,4,"unix", 1328 S_IFREG
| S_IRUGO
,1,0,0, 1329 0, &proc_net_inode_operations
, 1336 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"