Import 2.1.45pre4
[davej-history.git] / net / unix / af_unix.c
blob481be1dc50b9483934cec4d8c359dbfd6ee52c3b
1 /*
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 * Fixes:
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
21 * Mike Shaver's work.
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:
30 * [TO FIX]
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).
35 * [NOT TO FIX]
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
55 * with BSD names.
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>
67 #include <linux/un.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>
73 #include <linux/in.h>
74 #include <linux/fs.h>
75 #include <linux/malloc.h>
76 #include <asm/uaccess.h>
77 #include <linux/skbuff.h>
78 #include <linux/netdevice.h>
79 #include <net/sock.h>
80 #include <net/tcp.h>
81 #include <net/af_unix.h>
82 #include <linux/proc_fs.h>
83 #include <net/scm.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)
101 hash ^= hash>>16;
102 hash ^= hash>>8;
103 hash ^= hash>>4;
104 return 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)
121 sk->sock_readers++;
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)
136 if(addr)
138 if(atomic_dec_and_test(&addr->refcnt))
139 kfree(addr);
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))
160 return-EINVAL;
161 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
162 return-EINVAL;
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);
169 return len;
172 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
173 return len;
176 static voidunix_remove_socket(unix_socket *sk)
178 unix_socket **list = sk->protinfo.af_unix.list;
179 if(sk->next)
180 sk->next->prev = sk->prev;
181 if(sk->prev)
182 sk->prev->next = sk->next;
183 if(*list == sk)
184 *list = sk->next;
185 sk->protinfo.af_unix.list = NULL;
186 sk->prev = NULL;
187 sk->next = NULL;
190 static voidunix_insert_socket(unix_socket *sk)
192 unix_socket **list = sk->protinfo.af_unix.list;
193 sk->prev = NULL;
194 sk->next = *list;
195 if(*list)
196 (*list)->prev = sk;
197 *list=sk;
200 static unix_socket *unix_find_socket_byname(struct sockaddr_un *sunname,
201 int len,int type,unsigned hash)
203 unix_socket *s;
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&&
209 s->type == type)
211 unix_lock(s);
212 return(s);
215 return(NULL);
218 static unix_socket *unix_find_socket_byinode(struct inode *i)
220 unix_socket *s;
222 for(s=unix_socket_table[i->i_ino &0xF]; s; s=s->next)
224 if(s->protinfo.af_unix.inode==i)
226 unix_lock(s);
227 return(s);
230 return(NULL);
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)
242 sk_free(sk);
243 return;
247 * Retry;
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)
265 struct sk_buff *skb;
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 */
279 else
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)
294 sk_free(sk);
296 else
298 sk->dead=1;
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)
308 return(-EINVAL);
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;
318 return0;
321 externstruct proto_ops unix_stream_ops;
322 externstruct proto_ops unix_dgram_ops;
324 static intunix_create(struct socket *sock,int protocol)
326 struct sock *sk;
328 sock->state = SS_UNCONNECTED;
330 if(protocol && protocol != PF_UNIX)
331 return-EPROTONOSUPPORT;
333 switch(sock->type)
335 case SOCK_STREAM:
336 sock->ops = &unix_stream_ops;
337 break;
339 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
340 * nothing uses it.
342 case SOCK_RAW:
343 sock->type=SOCK_DGRAM;
344 case SOCK_DGRAM:
345 sock->ops = &unix_dgram_ops;
346 break;
347 default:
348 return-ESOCKTNOSUPPORT;
350 sk =sk_alloc(GFP_KERNEL);
351 if(!sk)
352 return-ENOMEM;
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 */
361 sk->mtu=4096;
362 sk->protinfo.af_unix.list=&unix_sockets_unbound;
363 unix_insert_socket(sk);
364 return0;
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;
375 unix_socket *skpair;
377 if(!sk)
378 return0;
380 if(sock->state != SS_UNCONNECTED)
381 sock->state = SS_DISCONNECTING;
383 sk->state_change(sk);
384 sk->dead=1;
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 */
393 if(skpair!=NULL)
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.
403 if(sk->socket)
405 sk->socket = NULL;
406 sock->sk = NULL;
409 return0;
412 static intunix_autobind(struct socket *sock)
414 struct sock *sk = sock->sk;
415 static u32 ordernum =1;
416 struct unix_address * addr;
417 unix_socket *osk;
419 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
420 if(!addr)
421 return-ENOBUFS;
422 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
424 kfree(addr);
425 return-EINVAL;
427 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
428 addr->name->sun_family = AF_UNIX;
429 atomic_set(&addr->refcnt,1);
431 retry:
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));
434 ordernum++;
436 if((osk=unix_find_socket_byname(addr->name, addr->len, sock->type,
437 addr->hash)) != NULL)
439 unix_unlock(osk);
440 goto retry;
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);
447 return0;
450 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
451 int type,unsigned hash,int*error)
453 unix_socket *u;
455 if(sunname->sun_path[0])
457 struct dentry *dentry;
458 dentry =open_namei(sunname->sun_path,2, S_IFSOCK);
459 if(IS_ERR(dentry)) {
460 *error =PTR_ERR(dentry);
461 return NULL;
463 u=unix_find_socket_byinode(dentry->d_inode);
464 dput(dentry);
465 if(u && u->type != type)
467 *error=-EPROTOTYPE;
468 unix_unlock(u);
469 return NULL;
472 else
473 u=unix_find_socket_byname(sunname, len, type, hash);
475 if(u==NULL)
477 *error=-ECONNREFUSED;
478 return NULL;
480 return u;
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;
490 int err;
491 unsigned hash;
492 struct unix_address *addr;
494 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode ||
495 sunaddr->sun_family != AF_UNIX)
496 return-EINVAL;
498 if(addr_len==sizeof(short))
499 returnunix_autobind(sock);
501 addr_len =unix_mkname(sunaddr, addr_len, &hash);
502 if(addr_len <0)
503 return addr_len;
505 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
506 if(!addr)
507 return-ENOBUFS;
509 /* We slept; recheck ... */
511 if(sk->protinfo.af_unix.addr || sk->protinfo.af_unix.inode)
513 kfree(addr);
514 return-EINVAL;/* Already bound */
517 memcpy(addr->name, sunaddr, addr_len);
518 addr->len = addr_len;
519 addr->hash = hash;
520 atomic_set(&addr->refcnt,1);
522 if(!sunaddr->sun_path[0])
524 unix_socket *osk =unix_find_socket_byname(sunaddr, addr_len,
525 sk->type, hash);
526 if(osk)
528 unix_unlock(osk);
529 kfree(addr);
530 return-EADDRINUSE;
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);
536 return0;
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 atomic_inc(&inode->i_count);
548 dput(dentry);
549 err =0;
552 if(err<0)
554 unix_release_addr(addr);
555 sk->protinfo.af_unix.addr = NULL;
556 if(err==-EEXIST)
557 return-EADDRINUSE;
558 else
559 return err;
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);
566 return0;
569 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
570 int alen,int flags)
572 struct sock *sk = sock->sk;
573 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
574 struct sock *other;
575 unsigned hash;
576 int err;
579 * 1003.1g breaking connected state with AF_UNSPEC
582 if(addr->sa_family==AF_UNSPEC)
584 if(unix_peer(sk))
586 unix_unlock(unix_peer(sk));
587 unix_peer(sk) = NULL;
588 sock->state=SS_UNCONNECTED;
590 return0;
593 alen =unix_mkname(sunaddr, alen, &hash);
594 if(alen <0)
595 return alen;
597 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
598 if(!other)
599 return err;
600 if(!unix_may_send(sk, other))
602 unix_unlock(other);
603 return-EINVAL;
607 * If it was connected, reconnect.
609 if(unix_peer(sk))
611 unix_unlock(unix_peer(sk));
612 unix_peer(sk)=NULL;
614 unix_peer(sk)=other;
615 if(sock->passcred && !sk->protinfo.af_unix.addr)
616 unix_autobind(sock);
617 return0;
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;
625 unix_socket *other;
626 struct sk_buff *skb;
627 int err;
628 unsigned hash;
629 int addr_len;
631 addr_len =unix_mkname(sunaddr, msg->msg_namelen, &hash);
632 if(addr_len <0)
633 return addr_len;
635 switch(sock->state)
637 case SS_UNCONNECTED:
638 /* This is ok... continue with connect */
639 break;
640 case SS_CONNECTED:
641 /* Socket is already connected */
642 return-EISCONN;
643 case SS_CONNECTING:
644 /* Not yet connected... we will check this. */
645 break;
646 default:
647 return(-EINVAL);
651 if(unix_peer(sk))
653 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
655 sock->state=SS_CONNECTED;
656 if(!sk->protinfo.af_unix.addr)
657 unix_autobind(sock);
658 return0;
660 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
662 sock->state=SS_UNCONNECTED;
663 return-ECONNREFUSED;
665 if(sock->state!=SS_CONNECTING)
666 return-EISCONN;
667 if(nonblock)
668 return-EALREADY;
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 */
681 if(skb==NULL)
682 return err;
683 memcpy(&UNIXCB(skb), cmsg,sizeof(*cmsg));
684 if(len)
685 memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
686 sk->state=TCP_CLOSE;
687 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
688 if(other==NULL)
690 kfree_skb(skb, FREE_WRITE);
691 return err;
693 other->ack_backlog++;
694 unix_peer(sk)=other;
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)
706 if(nonblock)
707 return-EINPROGRESS;
708 interruptible_sleep_on(sk->sleep);
709 if(current->signal & ~current->blocked)
710 return-ERESTARTSYS;
714 * Has the other end closed on us ?
717 if(sk->state==TCP_CLOSE)
719 unix_unlock(unix_peer(sk));
720 unix_peer(sk)=NULL;
721 sock->state=SS_UNCONNECTED;
722 return-ECONNREFUSED;
726 * Amazingly it has worked
729 sock->state=SS_CONNECTED;
730 if(!sk->protinfo.af_unix.addr)
731 unix_autobind(sock);
732 return0;
736 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
737 int addr_len,int flags)
739 struct msghdr msg;
740 struct unix_skb_parms cmsg;
742 msg.msg_name = uaddr;
743 msg.msg_namelen = addr_len;
744 cmsg.fp = NULL;
745 cmsg.attr = MSG_SYN;
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 */
758 unix_lock(ska);
759 unix_lock(skb);
760 unix_peer(ska)=skb;
761 unix_peer(skb)=ska;
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;
770 return0;
773 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
775 unix_socket *sk = sock->sk;
776 unix_socket *newsk = newsock->sk;
777 unix_socket *tsk;
778 struct sk_buff *skb;
780 if(sock->state != SS_UNCONNECTED)
781 return(-EINVAL);
782 if(!(sock->flags & SO_ACCEPTCON))
783 return(-EINVAL);
785 if(sock->type!=SOCK_STREAM)
786 return-EOPNOTSUPP;
787 if(sk->state!=TCP_LISTEN)
788 return-EINVAL;
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 atomic_inc(&sk->protinfo.af_unix.inode->i_count);
798 newsk->protinfo.af_unix.inode=sk->protinfo.af_unix.inode;
801 for(;;)
803 skb=skb_dequeue(&sk->receive_queue);
804 if(skb==NULL)
806 if(flags&O_NONBLOCK)
807 return-EAGAIN;
808 interruptible_sleep_on(sk->sleep);
809 if(current->signal & ~current->blocked)
810 return-ERESTARTSYS;
811 continue;
813 if(!(UNIXCB(skb).attr & MSG_SYN))
815 tsk=skb->sk;
816 tsk->state_change(tsk);
817 kfree_skb(skb, FREE_WRITE);
818 continue;
820 break;
823 tsk=skb->sk;
824 sk->ack_backlog--;
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);
839 return0;
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;
848 if(peer)
850 if(!unix_peer(sk))
851 return-ENOTCONN;
852 sk=unix_peer(sk);
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);
863 return0;
866 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
868 int i;
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);
883 scm_destroy(&scm);
884 sock_wfree(skb);
887 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
889 int i;
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;
894 scm->fp = NULL;
899 * Send AF_UNIX data.
902 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
903 struct scm_cookie *scm)
905 struct sock *sk = sock->sk;
906 unix_socket *other;
907 struct sockaddr_un *sunaddr=msg->msg_name;
908 int namelen =0;/* fake GCC */
909 int err;
910 unsigned hash;
911 struct sk_buff *skb;
913 if(msg->msg_flags&MSG_OOB)
914 return-EOPNOTSUPP;
916 if(msg->msg_flags&~MSG_DONTWAIT)
917 return-EINVAL;
919 if(msg->msg_namelen) {
920 namelen =unix_mkname(sunaddr, msg->msg_namelen, &hash);
921 if(namelen <0)
922 return namelen;
923 }else{
924 sunaddr = NULL;
925 if(!unix_peer(sk))
926 return-ENOTCONN;
929 if(sock->passcred && !sk->protinfo.af_unix.addr)
930 unix_autobind(sock);
932 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
934 if(skb==NULL)
935 return err;
937 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
938 UNIXCB(skb).attr = msg->msg_flags;
939 if(scm->fp)
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
950 * datagram error
952 unix_unlock(other);
953 unix_peer(sk)=NULL;
954 other = NULL;
955 if(sunaddr == NULL) {
956 kfree_skb(skb, FREE_WRITE);
957 return-ECONNRESET;
960 if(!other)
962 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
964 if(other==NULL)
966 kfree_skb(skb, FREE_WRITE);
967 return err;
969 if(!unix_may_send(sk, other))
971 unix_unlock(other);
972 kfree_skb(skb, FREE_WRITE);
973 return-EINVAL;
977 skb_queue_tail(&other->receive_queue, skb);
978 other->data_ready(other,len);
980 if(!unix_peer(sk))
981 unix_unlock(other);
982 return 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;
990 unix_socket *other;
991 struct sockaddr_un *sunaddr=msg->msg_name;
992 int err,size;
993 struct sk_buff *skb;
994 int limit=0;
995 int sent=0;
997 if(sock->flags & SO_ACCEPTCON)
998 return(-EINVAL);
1000 if(msg->msg_flags&MSG_OOB)
1001 return-EOPNOTSUPP;
1003 if(msg->msg_flags&~MSG_DONTWAIT)
1004 return-EINVAL;
1006 if(msg->msg_namelen) {
1007 if(sk->state==TCP_ESTABLISHED)
1008 return-EISCONN;
1009 else
1010 return-EOPNOTSUPP;
1011 }else{
1012 sunaddr = NULL;
1013 if(!unix_peer(sk))
1014 return-ENOTCONN;
1017 if(sk->shutdown&SEND_SHUTDOWN) {
1018 send_sig(SIGPIPE,current,0);
1019 return-EPIPE;
1022 while(sent < len)
1025 * Optimisation for the fact that under 0.01% of X messages typically
1026 * need breaking up.
1029 size=len-sent;
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
1037 * much.
1040 if(size >3500)
1041 limit =3500;/* Fall back to a page if we can't grab a big buffer this instant */
1042 else
1043 limit =0;/* Otherwise just grab and wait */
1046 * Grab a buffer
1049 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1051 if(skb==NULL)
1053 if(sent)
1054 return sent;
1055 return 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
1063 * succeed. [Alan]
1065 size =min(size,skb_tailroom(skb));
1067 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1068 UNIXCB(skb).attr = msg->msg_flags;
1069 if(scm->fp)
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);
1079 if(sent)
1080 return sent;
1081 send_sig(SIGPIPE,current,0);
1082 return-EPIPE;
1085 skb_queue_tail(&other->receive_queue, skb);
1086 other->data_ready(other,size);
1087 sent+=size;
1089 return sent;
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;
1112 int err;
1114 if(flags&MSG_OOB)
1115 return-EOPNOTSUPP;
1117 msg->msg_namelen =0;
1119 skb=skb_recv_datagram(sk, flags, noblock, &err);
1120 if(skb==NULL)
1121 return err;
1123 if(msg->msg_name)
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;
1131 else
1132 msg->msg_namelen=sizeof(short);
1135 if(size > skb->len)
1136 size = skb->len;
1137 else if(size < skb->len)
1138 msg->msg_flags |= MSG_TRUNC;
1140 if(skb_copy_datagram_iovec(skb,0, msg->msg_iov, size))
1141 return-EFAULT;
1143 scm->creds = *UNIXCREDS(skb);
1145 if(!(flags & MSG_PEEK))
1147 if(UNIXCB(skb).fp)
1148 unix_detach_fds(scm, skb);
1150 else
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,
1155 apparently wrong)
1156 - clone fds (I choosed it for now, it is the most universal
1157 solution)
1159 POSIX 1003.1g does not actually define this clearly
1160 at all. POSIX 1003.1g doesn't define a lot of things
1161 clearly however!
1164 if(UNIXCB(skb).fp)
1165 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1167 skb_free_datagram(sk,skb);
1168 return size;
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;
1178 int copied =0;
1179 int check_creds =0;
1180 int target =1;
1182 if(sock->flags & SO_ACCEPTCON)
1183 return(-EINVAL);
1185 if(flags&MSG_OOB)
1186 return-EOPNOTSUPP;
1187 if(flags&MSG_WAITALL)
1188 target = size;
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);
1201 int chunk;
1202 struct sk_buff *skb;
1204 skb=skb_dequeue(&sk->receive_queue);
1205 if(skb==NULL)
1207 if(copied >= target)
1208 break;
1210 if(sk->err)
1211 returnsock_error(sk);
1213 if(sk->shutdown & RCV_SHUTDOWN)
1214 break;
1215 up(&sk->protinfo.af_unix.readsem);
1216 if(noblock)
1217 return-EAGAIN;
1218 unix_data_wait(sk);
1219 if(current->signal & ~current->blocked)
1220 return-ERESTARTSYS;
1221 down(&sk->protinfo.af_unix.readsem);
1222 continue;
1225 /* Never glue messages from different writers */
1226 if(check_creds &&
1227 memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0)
1229 skb_queue_head(&sk->receive_queue, skb);
1230 break;
1233 /* Copy address just once */
1234 if(sunaddr)
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;
1242 else
1243 msg->msg_namelen=sizeof(short);
1244 sunaddr = NULL;
1247 chunk =min(skb->len, size);
1248 memcpy_toiovec(msg->msg_iov, skb->data, chunk);
1249 copied += chunk;
1250 size -= chunk;
1252 /* Copy credentials */
1253 scm->creds = *UNIXCREDS(skb);
1254 check_creds =1;
1256 /* Mark read part of skb as used */
1257 if(!(flags & MSG_PEEK))
1259 skb_pull(skb, chunk);
1261 if(UNIXCB(skb).fp)
1262 unix_detach_fds(scm, skb);
1264 /* put the skb back if we didn't use it up.. */
1265 if(skb->len)
1267 skb_queue_head(&sk->receive_queue, skb);
1268 break;
1271 kfree_skb(skb, FREE_WRITE);
1273 if(scm->fp)
1274 break;
1276 else
1278 /* It is questionable, see note in unix_dgram_recvmsg.
1281 if(UNIXCB(skb).fp)
1282 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1284 /* put message back and return */
1285 skb_queue_head(&sk->receive_queue, skb);
1286 break;
1288 }while(size);
1290 up(&sk->protinfo.af_unix.readsem);
1291 return copied;
1294 static intunix_shutdown(struct socket *sock,int mode)
1296 struct sock *sk = sock->sk;
1297 unix_socket *other=unix_peer(sk);
1299 mode++;
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);
1324 return0;
1328 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1330 struct sock *sk = sock->sk;
1331 long amount=0;
1333 switch(cmd)
1336 case TIOCOUTQ:
1337 amount = sk->sndbuf -atomic_read(&sk->wmem_alloc);
1338 if(amount<0)
1339 amount=0;
1340 returnput_user(amount, (int*)arg);
1341 case TIOCINQ:
1343 struct sk_buff *skb;
1344 if(sk->state==TCP_LISTEN)
1345 return-EINVAL;
1347 * These two are safe on current systems as
1348 * only user tasks fiddle here
1350 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1351 amount=skb->len;
1352 returnput_user(amount, (int*)arg);
1355 default:
1356 return-EINVAL;
1358 /*NOTREACHED*/
1359 return(0);
1362 #ifdef CONFIG_PROC_FS
1363 static intunix_read_proc(char*buffer,char**start, off_t offset,
1364 int length,int*eof,void*data)
1366 off_t pos=0;
1367 off_t begin=0;
1368 int len=0;
1369 int i;
1370 unix_socket *s;
1372 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1373 "Inode Path\n");
1375 forall_unix_sockets(i,s)
1377 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X %5ld",
1379 s->sock_readers,
1381 s->socket ? s->socket->flags :0,
1382 s->type,
1383 s->socket ? s->socket->state :0,
1384 s->socket ? s->socket->inode->i_ino :0);
1386 if(s->protinfo.af_unix.addr)
1388 buffer[len++] =' ';
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))
1392 len--;
1393 else
1394 buffer[len] ='@';
1395 len += s->protinfo.af_unix.addr->len -sizeof(short);
1397 buffer[len++]='\n';
1399 pos=begin+len;
1400 if(pos<offset)
1402 len=0;
1403 begin=pos;
1405 if(pos>offset+length)
1406 goto done;
1408 *eof =1;
1409 done:
1410 *start=buffer+(offset-begin);
1411 len-=(offset-begin);
1412 if(len>length)
1413 len=length;
1414 return len;
1416 #endif
1418 struct proto_ops unix_stream_ops = {
1419 AF_UNIX,
1421 unix_dup,
1422 unix_release,
1423 unix_bind,
1424 unix_stream_connect,
1425 unix_socketpair,
1426 unix_accept,
1427 unix_getname,
1428 datagram_poll,
1429 unix_ioctl,
1430 unix_listen,
1431 unix_shutdown,
1432 sock_no_setsockopt,
1433 sock_no_getsockopt,
1434 sock_no_fcntl,
1435 unix_stream_sendmsg,
1436 unix_stream_recvmsg
1439 struct proto_ops unix_dgram_ops = {
1440 AF_UNIX,
1442 unix_dup,
1443 unix_release,
1444 unix_bind,
1445 unix_dgram_connect,
1446 unix_socketpair,
1447 NULL,
1448 unix_getname,
1449 datagram_poll,
1450 unix_ioctl,
1451 sock_no_listen,
1452 unix_shutdown,
1453 sock_no_setsockopt,
1454 sock_no_getsockopt,
1455 sock_no_fcntl,
1456 unix_dgram_sendmsg,
1457 unix_dgram_recvmsg
1460 struct net_proto_family unix_family_ops = {
1461 AF_UNIX,
1462 unix_create
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");
1474 return;
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;
1480 #endif
1483 * Local variables:
1484 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1485 * End:
close