Import 2.4.0-test3pre5
[davej-history.git] / net / unix / af_unix.c
bloba04b85cfafecd5f5e89362719ff01789bbc112f5
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 * Version: $Id: af_unix.c,v 1.101 2000/07/06 01:41:46 davem Exp $
13 * Fixes:
14 * Linus Torvalds : Assorted bug cures.
15 * Niibe Yutaka : async I/O support.
16 * Carsten Paeth : PF_UNIX check, address fixes.
17 * Alan Cox : Limit size of allocated blocks.
18 * Alan Cox : Fixed the stupid socketpair bug.
19 * Alan Cox : BSD compatibility fine tuning.
20 * Alan Cox : Fixed a bug in connect when interrupted.
21 * Alan Cox : Sorted out a proper draft version of
22 * file descriptor passing hacked up from
23 * Mike Shaver's work.
24 * Marty Leisner : Fixes to fd passing
25 * Nick Nevin : recvmsg bugfix.
26 * Alan Cox : Started proper garbage collector
27 * Heiko EiBfeldt : Missing verify_area check
28 * Alan Cox : Started POSIXisms
29 * Andreas Schwab : Replace inode by dentry for proper
30 * reference counting
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
33 * Lots of bug fixes.
34 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
35 * by above two patches.
36 * Andrea Arcangeli : If possible we block in connect(2)
37 * if the max backlog of the listen socket
38 * is been reached. This won't break
39 * old apps and it will avoid huge amount
40 * of socks hashed (this for unix_gc()
41 * performances reasons).
42 * Security fix that limits the max
43 * number of socks to 2*max_files and
44 * the number of skb queueable in the
45 * dgram receiver.
46 * Artur Skawina : Hash function optimizations
47 * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
48 * Malcolm Beattie : Set peercred for socketpair
51 * Known differences from reference BSD that was tested:
53 * [TO FIX]
54 * ECONNREFUSED is not returned from one end of a connected() socket to the
55 * other the moment one end closes.
56 * fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
57 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
58 * [NOT TO FIX]
59 * accept() returns a path name even if the connecting socket has closed
60 * in the meantime (BSD loses the path and gives up).
61 * accept() returns 0 length path for an unbound connector. BSD returns 16
62 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
63 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
64 * BSD af_unix apparently has connect forgetting to block properly.
65 * (need to check this with the POSIX spec in detail)
67 * Differences from 2.0.0-11-... (ANK)
68 * Bug fixes and improvements.
69 * - client shutdown killed server socket.
70 * - removed all useless cli/sti pairs.
72 * Semantic changes/extensions.
73 * - generic control message passing.
74 * - SCM_CREDENTIALS control message.
75 * - "Abstract" (not FS based) socket bindings.
76 * Abstract names are sequences of bytes (not zero terminated)
77 * started by 0, so that this name space does not intersect
78 * with BSD names.
81 #include <linux/module.h>
82 #include <linux/config.h>
83 #include <linux/kernel.h>
84 #include <linux/major.h>
85 #include <linux/signal.h>
86 #include <linux/sched.h>
87 #include <linux/errno.h>
88 #include <linux/string.h>
89 #include <linux/stat.h>
90 #include <linux/socket.h>
91 #include <linux/un.h>
92 #include <linux/fcntl.h>
93 #include <linux/termios.h>
94 #include <linux/sockios.h>
95 #include <linux/net.h>
96 #include <linux/in.h>
97 #include <linux/fs.h>
98 #include <linux/malloc.h>
99 #include <asm/uaccess.h>
100 #include <linux/skbuff.h>
101 #include <linux/netdevice.h>
102 #include <net/sock.h>
103 #include <net/tcp.h>
104 #include <net/af_unix.h>
105 #include <linux/proc_fs.h>
106 #include <net/scm.h>
107 #include <linux/init.h>
108 #include <linux/poll.h>
109 #include <linux/smp_lock.h>
111 #include <asm/checksum.h>
113 #define min(a,b) (((a)<(b))?(a):(b))
115 int sysctl_unix_max_dgram_qlen =10;
117 unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
118 rwlock_t unix_table_lock = RW_LOCK_UNLOCKED;
119 static atomic_t unix_nr_socks =ATOMIC_INIT(0);
121 #define unix_sockets_unbound (unix_socket_table[UNIX_HASH_SIZE])
123 #define UNIX_ABSTRACT(sk) ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
126 SMP locking strategy.
127 * hash table is protceted with rwlock unix_table_lock
128 * each socket state is protected by separate rwlock.
132 extern __inline__ unsignedunix_hash_fold(unsigned hash)
134 hash ^= hash>>16;
135 hash ^= hash>>8;
136 return hash&(UNIX_HASH_SIZE-1);
139 #define unix_peer(sk) ((sk)->pair)
141 extern __inline__ intunix_our_peer(unix_socket *sk, unix_socket *osk)
143 returnunix_peer(osk) == sk;
146 extern __inline__ intunix_may_send(unix_socket *sk, unix_socket *osk)
148 return(unix_peer(osk) == NULL ||unix_our_peer(sk, osk));
151 static __inline__ unix_socket *unix_peer_get(unix_socket *s)
153 unix_socket *peer;
155 unix_state_rlock(s);
156 peer =unix_peer(s);
157 if(peer)
158 sock_hold(peer);
159 unix_state_runlock(s);
160 return peer;
163 extern __inline__ voidunix_release_addr(struct unix_address *addr)
165 if(atomic_dec_and_test(&addr->refcnt))
166 kfree(addr);
170 * Check unix socket name:
171 * - should be not zero length.
172 * - if started by not zero, should be NULL terminated (FS object)
173 * - if started by zero, it is abstract name.
176 static intunix_mkname(struct sockaddr_un * sunaddr,int len,unsigned*hashp)
178 if(len <=sizeof(short) || len >sizeof(*sunaddr))
179 return-EINVAL;
180 if(!sunaddr || sunaddr->sun_family != AF_UNIX)
181 return-EINVAL;
182 if(sunaddr->sun_path[0])
185 * This may look like an off by one error but it is
186 * a bit more subtle. 108 is the longest valid AF_UNIX
187 * path for a binding. sun_path[108] doesnt as such
188 * exist. However in kernel space we are guaranteed that
189 * it is a valid memory location in our kernel
190 * address buffer.
192 if(len >sizeof(*sunaddr))
193 len =sizeof(*sunaddr);
194 ((char*)sunaddr)[len]=0;
195 len =strlen(sunaddr->sun_path)+1+sizeof(short);
196 return len;
199 *hashp =unix_hash_fold(csum_partial((char*)sunaddr, len,0));
200 return len;
203 static void__unix_remove_socket(unix_socket *sk)
205 unix_socket **list = sk->protinfo.af_unix.list;
206 if(list) {
207 if(sk->next)
208 sk->next->prev = sk->prev;
209 if(sk->prev)
210 sk->prev->next = sk->next;
211 if(*list == sk)
212 *list = sk->next;
213 sk->protinfo.af_unix.list = NULL;
214 sk->prev = NULL;
215 sk->next = NULL;
216 __sock_put(sk);
220 static void__unix_insert_socket(unix_socket **list, unix_socket *sk)
222 BUG_TRAP(sk->protinfo.af_unix.list==NULL);
224 sk->protinfo.af_unix.list = list;
225 sk->prev = NULL;
226 sk->next = *list;
227 if(*list)
228 (*list)->prev = sk;
229 *list=sk;
230 sock_hold(sk);
233 static __inline__ voidunix_remove_socket(unix_socket *sk)
235 write_lock(&unix_table_lock);
236 __unix_remove_socket(sk);
237 write_unlock(&unix_table_lock);
240 static __inline__ voidunix_insert_socket(unix_socket **list, unix_socket *sk)
242 write_lock(&unix_table_lock);
243 __unix_insert_socket(list, sk);
244 write_unlock(&unix_table_lock);
247 static unix_socket *__unix_find_socket_byname(struct sockaddr_un *sunname,
248 int len,int type,unsigned hash)
250 unix_socket *s;
252 for(s=unix_socket_table[hash^type]; s; s=s->next) {
253 if(s->protinfo.af_unix.addr->len==len &&
254 memcmp(s->protinfo.af_unix.addr->name, sunname, len) ==0)
255 return s;
257 return NULL;
260 static __inline__ unix_socket *
261 unix_find_socket_byname(struct sockaddr_un *sunname,
262 int len,int type,unsigned hash)
264 unix_socket *s;
266 read_lock(&unix_table_lock);
267 s =__unix_find_socket_byname(sunname, len, type, hash);
268 if(s)
269 sock_hold(s);
270 read_unlock(&unix_table_lock);
271 return s;
274 static unix_socket *unix_find_socket_byinode(struct inode *i)
276 unix_socket *s;
278 read_lock(&unix_table_lock);
279 for(s=unix_socket_table[i->i_ino & (UNIX_HASH_SIZE-1)]; s; s=s->next)
281 struct dentry *dentry = s->protinfo.af_unix.dentry;
283 if(dentry && dentry->d_inode == i)
285 sock_hold(s);
286 break;
289 read_unlock(&unix_table_lock);
290 return s;
293 static __inline__ intunix_writable(struct sock *sk)
295 return((atomic_read(&sk->wmem_alloc)<<2) <= sk->sndbuf);
298 static voidunix_write_space(struct sock *sk)
300 read_lock(&sk->callback_lock);
301 if(unix_writable(sk)) {
302 if(sk->sleep &&waitqueue_active(sk->sleep))
303 wake_up_interruptible(sk->sleep);
304 sk_wake_async(sk,2, POLL_OUT);
306 read_unlock(&sk->callback_lock);
309 static voidunix_sock_destructor(struct sock *sk)
311 skb_queue_purge(&sk->receive_queue);
313 BUG_TRAP(atomic_read(&sk->wmem_alloc) ==0);
314 BUG_TRAP(sk->protinfo.af_unix.list==NULL);
315 BUG_TRAP(sk->socket==NULL);
316 if(sk->dead==0) {
317 printk("Attempt to release alive unix socket: %p\n", sk);
318 return;
321 if(sk->protinfo.af_unix.addr)
322 unix_release_addr(sk->protinfo.af_unix.addr);
324 atomic_dec(&unix_nr_socks);
325 #ifdef UNIX_REFCNT_DEBUG
326 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk,atomic_read(&unix_nr_socks));
327 #endif
328 MOD_DEC_USE_COUNT;
331 static intunix_release_sock(unix_socket *sk,int embrion)
333 struct dentry *dentry;
334 struct vfsmount *mnt;
335 unix_socket *skpair;
336 struct sk_buff *skb;
337 int state;
339 unix_remove_socket(sk);
341 /* Clear state */
342 unix_state_wlock(sk);
343 sock_orphan(sk);
344 sk->shutdown = SHUTDOWN_MASK;
345 dentry = sk->protinfo.af_unix.dentry;
346 sk->protinfo.af_unix.dentry=NULL;
347 mnt = sk->protinfo.af_unix.mnt;
348 sk->protinfo.af_unix.mnt=NULL;
349 state = sk->state;
350 sk->state = TCP_CLOSE;
351 unix_state_wunlock(sk);
353 wake_up_interruptible_all(&sk->protinfo.af_unix.peer_wait);
355 skpair=unix_peer(sk);
357 if(skpair!=NULL) {
358 if(sk->type==SOCK_STREAM) {
359 unix_state_wlock(skpair);
360 skpair->shutdown=SHUTDOWN_MASK;/* No more writes*/
361 if(!skb_queue_empty(&sk->receive_queue) || embrion)
362 skpair->err = ECONNRESET;
363 unix_state_wunlock(skpair);
364 skpair->state_change(skpair);
365 read_lock(&skpair->callback_lock);
366 sk_wake_async(skpair,1,POLL_HUP);
367 read_unlock(&skpair->callback_lock);
369 sock_put(skpair);/* It may now die */
370 unix_peer(sk) = NULL;
373 /* Try to flush out this socket. Throw out buffers at least */
375 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
377 if(state==TCP_LISTEN)
378 unix_release_sock(skb->sk,1);
379 /* passed fds are erased in the kfree_skb hook */
380 kfree_skb(skb);
383 if(dentry) {
384 dput(dentry);
385 mntput(mnt);
388 sock_put(sk);
390 /* ---- Socket is dead now and most probably destroyed ---- */
393 * Fixme: BSD difference: In BSD all sockets connected to use get
394 * ECONNRESET and we die on the spot. In Linux we behave
395 * like files and pipes do and wait for the last
396 * dereference.
398 * Can't we simply set sock->err?
400 * What the above comment does talk about? --ANK(980817)
403 if(atomic_read(&unix_tot_inflight))
404 unix_gc();/* Garbage collect fds */
406 return0;
409 static intunix_listen(struct socket *sock,int backlog)
411 int err;
412 struct sock *sk = sock->sk;
414 err = -EOPNOTSUPP;
415 if(sock->type!=SOCK_STREAM)
416 goto out;/* Only stream sockets accept */
417 err = -EINVAL;
418 if(!sk->protinfo.af_unix.addr)
419 goto out;/* No listens on an unbound socket */
420 unix_state_wlock(sk);
421 if(sk->state != TCP_CLOSE && sk->state != TCP_LISTEN)
422 goto out_unlock;
423 if(backlog > sk->max_ack_backlog)
424 wake_up_interruptible_all(&sk->protinfo.af_unix.peer_wait);
425 sk->max_ack_backlog=backlog;
426 sk->state=TCP_LISTEN;
427 /* set credentials so connect can copy them */
428 sk->peercred.pid = current->pid;
429 sk->peercred.uid = current->euid;
430 sk->peercred.gid = current->egid;
431 err =0;
433 out_unlock:
434 unix_state_wunlock(sk);
435 out:
436 return err;
439 externstruct proto_ops unix_stream_ops;
440 externstruct proto_ops unix_dgram_ops;
442 static struct sock *unix_create1(struct socket *sock)
444 struct sock *sk;
446 if(atomic_read(&unix_nr_socks) >=2*files_stat.max_files)
447 return NULL;
449 MOD_INC_USE_COUNT;
450 sk =sk_alloc(PF_UNIX, GFP_KERNEL,1);
451 if(!sk) {
452 MOD_DEC_USE_COUNT;
453 return NULL;
456 atomic_inc(&unix_nr_socks);
458 sock_init_data(sock,sk);
460 sk->write_space = unix_write_space;
462 sk->max_ack_backlog = sysctl_unix_max_dgram_qlen;
463 sk->destruct = unix_sock_destructor;
464 sk->protinfo.af_unix.dentry=NULL;
465 sk->protinfo.af_unix.mnt=NULL;
466 sk->protinfo.af_unix.lock = RW_LOCK_UNLOCKED;
467 atomic_set(&sk->protinfo.af_unix.inflight,0);
468 init_MUTEX(&sk->protinfo.af_unix.readsem);/* single task reading lock */
469 init_waitqueue_head(&sk->protinfo.af_unix.peer_wait);
470 sk->protinfo.af_unix.list=NULL;
471 unix_insert_socket(&unix_sockets_unbound, sk);
473 return sk;
476 static intunix_create(struct socket *sock,int protocol)
478 if(protocol && protocol != PF_UNIX)
479 return-EPROTONOSUPPORT;
481 sock->state = SS_UNCONNECTED;
483 switch(sock->type) {
484 case SOCK_STREAM:
485 sock->ops = &unix_stream_ops;
486 break;
488 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
489 * nothing uses it.
491 case SOCK_RAW:
492 sock->type=SOCK_DGRAM;
493 case SOCK_DGRAM:
494 sock->ops = &unix_dgram_ops;
495 break;
496 default:
497 return-ESOCKTNOSUPPORT;
500 returnunix_create1(sock) ?0: -ENOMEM;
503 static intunix_release(struct socket *sock)
505 unix_socket *sk = sock->sk;
507 if(!sk)
508 return0;
510 sock->sk = NULL;
512 returnunix_release_sock(sk,0);
515 static intunix_autobind(struct socket *sock)
517 struct sock *sk = sock->sk;
518 static u32 ordernum =1;
519 struct unix_address * addr;
520 int err;
522 down(&sk->protinfo.af_unix.readsem);
524 err =0;
525 if(sk->protinfo.af_unix.addr)
526 goto out;
528 err = -ENOMEM;
529 addr =kmalloc(sizeof(*addr) +sizeof(short) +16, GFP_KERNEL);
530 if(!addr)
531 goto out;
533 memset(addr,0,sizeof(*addr) +sizeof(short) +16);
534 addr->name->sun_family = AF_UNIX;
535 atomic_set(&addr->refcnt,1);
537 retry:
538 addr->len =sprintf(addr->name->sun_path+1,"%05x", ordernum) +1+sizeof(short);
539 addr->hash =unix_hash_fold(csum_partial((void*)addr->name, addr->len,0));
541 write_lock(&unix_table_lock);
542 ordernum = (ordernum+1)&0xFFFFF;
544 if(__unix_find_socket_byname(addr->name, addr->len, sock->type,
545 addr->hash)) {
546 write_unlock(&unix_table_lock);
547 /* Sanity yield. It is unusual case, but yet... */
548 if(!(ordernum&0xFF)) {
549 current->policy |= SCHED_YIELD;
550 schedule();
552 goto retry;
554 addr->hash ^= sk->type;
556 __unix_remove_socket(sk);
557 sk->protinfo.af_unix.addr = addr;
558 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
559 write_unlock(&unix_table_lock);
560 err =0;
562 out:
563 up(&sk->protinfo.af_unix.readsem);
564 return err;
567 static unix_socket *unix_find_other(struct sockaddr_un *sunname,int len,
568 int type,unsigned hash,int*error)
570 unix_socket *u;
571 struct nameidata nd;
572 int err =0;
574 if(sunname->sun_path[0]) {
575 if(path_init(sunname->sun_path, LOOKUP_POSITIVE, &nd))
576 err =path_walk(sunname->sun_path, &nd);
577 if(err)
578 goto fail;
579 err =permission(nd.dentry->d_inode,MAY_WRITE);
580 if(err)
581 goto put_fail;
583 err = -ECONNREFUSED;
584 if(!S_ISSOCK(nd.dentry->d_inode->i_mode))
585 goto put_fail;
586 u=unix_find_socket_byinode(nd.dentry->d_inode);
587 if(!u)
588 goto put_fail;
590 path_release(&nd);
592 err=-EPROTOTYPE;
593 if(u->type != type) {
594 sock_put(u);
595 goto fail;
597 }else{
598 err = -ECONNREFUSED;
599 u=unix_find_socket_byname(sunname, len, type, hash);
600 if(!u)
601 goto fail;
603 return u;
605 put_fail:
606 path_release(&nd);
607 fail:
608 *error=err;
609 return NULL;
613 static intunix_bind(struct socket *sock,struct sockaddr *uaddr,int addr_len)
615 struct sock *sk = sock->sk;
616 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
617 struct dentry * dentry = NULL;
618 struct nameidata nd;
619 int err;
620 unsigned hash;
621 struct unix_address *addr;
622 unix_socket **list;
624 err = -EINVAL;
625 if(sunaddr->sun_family != AF_UNIX)
626 goto out;
628 if(addr_len==sizeof(short)) {
629 err =unix_autobind(sock);
630 goto out;
633 err =unix_mkname(sunaddr, addr_len, &hash);
634 if(err <0)
635 goto out;
636 addr_len = err;
638 down(&sk->protinfo.af_unix.readsem);
640 err = -EINVAL;
641 if(sk->protinfo.af_unix.addr)
642 goto out_up;
644 err = -ENOMEM;
645 addr =kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
646 if(!addr)
647 goto out_up;
649 memcpy(addr->name, sunaddr, addr_len);
650 addr->len = addr_len;
651 addr->hash = hash^sk->type;
652 atomic_set(&addr->refcnt,1);
654 if(sunaddr->sun_path[0]) {
655 err =0;
657 * Get the parent directory, calculate the hash for last
658 * component.
660 if(path_init(sunaddr->sun_path, LOOKUP_PARENT, &nd))
661 err =path_walk(sunaddr->sun_path, &nd);
662 if(err)
663 goto out_mknod_parent;
665 * Yucky last component or no last component at all?
666 * (foo/., foo/.., /////)
668 err = -EEXIST;
669 if(nd.last_type != LAST_NORM)
670 goto out_mknod;
672 * Lock the directory.
674 down(&nd.dentry->d_inode->i_sem);
676 * Do the final lookup.
678 dentry =lookup_hash(&nd.last, nd.dentry);
679 err =PTR_ERR(dentry);
680 if(IS_ERR(dentry))
681 goto out_mknod_unlock;
682 err = -ENOENT;
684 * Special case - lookup gave negative, but... we had foo/bar/
685 * From the vfs_mknod() POV we just have a negative dentry -
686 * all is fine. Let's be bastards - you had / on the end, you've
687 * been asking for (non-existent) directory. -ENOENT for you.
689 if(nd.last.name[nd.last.len] && !dentry->d_inode)
690 goto out_mknod_dput;
692 * All right, let's create it.
694 err =vfs_mknod(nd.dentry->d_inode, dentry,
695 S_IFSOCK|sock->inode->i_mode,0);
696 if(err)
697 goto out_mknod_dput;
698 up(&nd.dentry->d_inode->i_sem);
699 dput(nd.dentry);
700 nd.dentry = dentry;
702 addr->hash = UNIX_HASH_SIZE;
705 write_lock(&unix_table_lock);
707 if(!sunaddr->sun_path[0]) {
708 err = -EADDRINUSE;
709 if(__unix_find_socket_byname(sunaddr, addr_len,
710 sk->type, hash)) {
711 unix_release_addr(addr);
712 goto out_unlock;
715 list = &unix_socket_table[addr->hash];
716 }else{
717 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
718 sk->protinfo.af_unix.dentry = nd.dentry;
719 sk->protinfo.af_unix.mnt = nd.mnt;
722 err =0;
723 __unix_remove_socket(sk);
724 sk->protinfo.af_unix.addr = addr;
725 __unix_insert_socket(list, sk);
727 out_unlock:
728 write_unlock(&unix_table_lock);
729 out_up:
730 up(&sk->protinfo.af_unix.readsem);
731 out:
732 return err;
734 out_mknod_dput:
735 dput(dentry);
736 out_mknod_unlock:
737 up(&nd.dentry->d_inode->i_sem);
738 out_mknod:
739 path_release(&nd);
740 out_mknod_parent:
741 if(err==-EEXIST)
742 err=-EADDRINUSE;
743 unix_release_addr(addr);
744 goto out_up;
747 static intunix_dgram_connect(struct socket *sock,struct sockaddr *addr,
748 int alen,int flags)
750 struct sock *sk = sock->sk;
751 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
752 struct sock *other;
753 unsigned hash;
754 int err;
756 if(addr->sa_family != AF_UNSPEC) {
757 err =unix_mkname(sunaddr, alen, &hash);
758 if(err <0)
759 goto out;
760 alen = err;
762 if(sock->passcred && !sk->protinfo.af_unix.addr &&
763 (err =unix_autobind(sock)) !=0)
764 goto out;
766 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
767 if(!other)
768 goto out;
770 unix_state_wlock(sk);
772 err = -EPERM;
773 if(!unix_may_send(sk, other))
774 goto out_unlock;
775 }else{
777 * 1003.1g breaking connected state with AF_UNSPEC
779 other = NULL;
780 unix_state_wlock(sk);
784 * If it was connected, reconnect.
786 if(unix_peer(sk)) {
787 struct sock *old_peer =unix_peer(sk);
788 unix_peer(sk)=other;
789 unix_state_wunlock(sk);
791 sock_put(old_peer);
792 }else{
793 unix_peer(sk)=other;
794 unix_state_wunlock(sk);
796 return0;
798 out_unlock:
799 unix_state_wunlock(sk);
800 sock_put(other);
801 out:
802 return err;
805 static longunix_wait_for_peer(unix_socket *other,long timeo)
807 int sched;
808 DECLARE_WAITQUEUE(wait, current);
810 __set_current_state(TASK_INTERRUPTIBLE|TASK_EXCLUSIVE);
811 add_wait_queue_exclusive(&other->protinfo.af_unix.peer_wait, &wait);
813 sched = (!other->dead &&
814 !(other->shutdown&RCV_SHUTDOWN) &&
815 skb_queue_len(&other->receive_queue) > other->max_ack_backlog);
817 unix_state_runlock(other);
819 if(sched)
820 timeo =schedule_timeout(timeo);
822 __set_current_state(TASK_RUNNING);
823 remove_wait_queue(&other->protinfo.af_unix.peer_wait, &wait);
824 return timeo;
827 static intunix_stream_connect(struct socket *sock,struct sockaddr *uaddr,
828 int addr_len,int flags)
830 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
831 struct sock *sk = sock->sk;
832 struct sock *newsk = NULL;
833 unix_socket *other = NULL;
834 struct sk_buff *skb = NULL;
835 unsigned hash;
836 int st;
837 int err;
838 long timeo;
840 err =unix_mkname(sunaddr, addr_len, &hash);
841 if(err <0)
842 goto out;
843 addr_len = err;
845 if(sock->passcred && !sk->protinfo.af_unix.addr &&
846 (err =unix_autobind(sock)) !=0)
847 goto out;
849 timeo =sock_sndtimeo(sk, flags & O_NONBLOCK);
851 /* First of all allocate resources.
852 If we will make it after state is locked,
853 we will have to recheck all again in any case.
856 err = -ENOMEM;
858 /* create new sock for complete connection */
859 newsk =unix_create1(NULL);
860 if(newsk == NULL)
861 goto out;
863 /* Allocate skb for sending to listening sock */
864 skb =sock_wmalloc(newsk,1,0, GFP_KERNEL);
865 if(skb == NULL)
866 goto out;
868 restart:
869 /* Find listening sock. */
870 other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
871 if(!other)
872 goto out;
874 /* Latch state of peer */
875 unix_state_rlock(other);
877 /* Apparently VFS overslept socket death. Retry. */
878 if(other->dead) {
879 unix_state_runlock(other);
880 sock_put(other);
881 goto restart;
884 err = -ECONNREFUSED;
885 if(other->state != TCP_LISTEN)
886 goto out_unlock;
888 if(skb_queue_len(&other->receive_queue) > other->max_ack_backlog) {
889 err = -EAGAIN;
890 if(!timeo)
891 goto out_unlock;
893 timeo =unix_wait_for_peer(other, timeo);
895 err =sock_intr_errno(timeo);
896 if(signal_pending(current))
897 goto out;
898 sock_put(other);
899 goto restart;
902 /* Latch our state.
904 It is tricky place. We need to grab write lock and cannot
905 drop lock on peer. It is dangerous because deadlock is
906 possible. Connect to self case and simultaneous
907 attempt to connect are eliminated by checking socket
908 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
909 check this before attempt to grab lock.
911 Well, and we have to recheck the state after socket locked.
913 st = sk->state;
915 switch(st) {
916 case TCP_CLOSE:
917 /* This is ok... continue with connect */
918 break;
919 case TCP_ESTABLISHED:
920 /* Socket is already connected */
921 err = -EISCONN;
922 goto out_unlock;
923 default:
924 err = -EINVAL;
925 goto out_unlock;
928 unix_state_wlock(sk);
930 if(sk->state != st) {
931 unix_state_wunlock(sk);
932 unix_state_runlock(other);
933 sock_put(other);
934 goto restart;
937 /* The way is open! Fastly set all the necessary fields... */
939 sock_hold(sk);
940 unix_peer(newsk)=sk;
941 newsk->state=TCP_ESTABLISHED;
942 newsk->type=SOCK_STREAM;
943 newsk->peercred.pid = current->pid;
944 newsk->peercred.uid = current->euid;
945 newsk->peercred.gid = current->egid;
946 newsk->sleep = &newsk->protinfo.af_unix.peer_wait;
948 /* copy address information from listening to new sock*/
949 if(other->protinfo.af_unix.addr)
951 atomic_inc(&other->protinfo.af_unix.addr->refcnt);
952 newsk->protinfo.af_unix.addr=other->protinfo.af_unix.addr;
954 if(other->protinfo.af_unix.dentry) {
955 newsk->protinfo.af_unix.dentry=dget(other->protinfo.af_unix.dentry);
956 newsk->protinfo.af_unix.mnt=mntget(other->protinfo.af_unix.mnt);
959 /* Set credentials */
960 sk->peercred = other->peercred;
962 sock_hold(newsk);
963 unix_peer(sk)=newsk;
964 sock->state=SS_CONNECTED;
965 sk->state=TCP_ESTABLISHED;
967 unix_state_wunlock(sk);
969 /* take ten and and send info to listening sock */
970 skb_queue_tail(&other->receive_queue,skb);
971 unix_state_runlock(other);
972 other->data_ready(other,0);
973 sock_put(other);
974 return0;
976 out_unlock:
977 if(other)
978 unix_state_runlock(other);
980 out:
981 if(skb)
982 kfree_skb(skb);
983 if(newsk)
984 unix_release_sock(newsk,0);
985 if(other)
986 sock_put(other);
987 return err;
990 static intunix_socketpair(struct socket *socka,struct socket *sockb)
992 struct sock *ska=socka->sk, *skb = sockb->sk;
994 /* Join our sockets back to back */
995 sock_hold(ska);
996 sock_hold(skb);
997 unix_peer(ska)=skb;
998 unix_peer(skb)=ska;
999 ska->peercred.pid = skb->peercred.pid = current->pid;
1000 ska->peercred.uid = skb->peercred.uid = current->euid;
1001 ska->peercred.gid = skb->peercred.gid = current->egid;
1003 if(ska->type != SOCK_DGRAM)
1005 ska->state=TCP_ESTABLISHED;
1006 skb->state=TCP_ESTABLISHED;
1007 socka->state=SS_CONNECTED;
1008 sockb->state=SS_CONNECTED;
1010 return0;
1013 static intunix_accept(struct socket *sock,struct socket *newsock,int flags)
1015 unix_socket *sk = sock->sk;
1016 unix_socket *tsk;
1017 struct sk_buff *skb;
1018 int err;
1020 err = -EOPNOTSUPP;
1021 if(sock->type!=SOCK_STREAM)
1022 goto out;
1024 err = -EINVAL;
1025 if(sk->state!=TCP_LISTEN)
1026 goto out;
1028 /* If socket state is TCP_LISTEN it cannot change (for now...),
1029 * so that no locks are necessary.
1032 skb =skb_recv_datagram(sk,0, flags&O_NONBLOCK, &err);
1033 if(!skb)
1034 goto out;
1036 tsk = skb->sk;
1037 skb_free_datagram(sk, skb);
1038 wake_up_interruptible(&sk->protinfo.af_unix.peer_wait);
1040 /* attach accepted sock to socket */
1041 unix_state_wlock(tsk);
1042 newsock->state = SS_CONNECTED;
1043 sock_graft(tsk, newsock);
1044 unix_state_wunlock(tsk);
1045 return0;
1047 out:
1048 return err;
1052 static intunix_getname(struct socket *sock,struct sockaddr *uaddr,int*uaddr_len,int peer)
1054 struct sock *sk = sock->sk;
1055 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1056 int err =0;
1058 if(peer) {
1059 sk =unix_peer_get(sk);
1061 err = -ENOTCONN;
1062 if(!sk)
1063 goto out;
1064 err =0;
1065 }else{
1066 sock_hold(sk);
1069 unix_state_rlock(sk);
1070 if(!sk->protinfo.af_unix.addr) {
1071 sunaddr->sun_family = AF_UNIX;
1072 sunaddr->sun_path[0] =0;
1073 *uaddr_len =sizeof(short);
1074 }else{
1075 struct unix_address *addr = sk->protinfo.af_unix.addr;
1077 *uaddr_len = addr->len;
1078 memcpy(sunaddr, addr->name, *uaddr_len);
1080 unix_state_runlock(sk);
1081 sock_put(sk);
1082 out:
1083 return err;
1086 static voidunix_detach_fds(struct scm_cookie *scm,struct sk_buff *skb)
1088 int i;
1090 scm->fp =UNIXCB(skb).fp;
1091 skb->destructor = sock_wfree;
1092 UNIXCB(skb).fp = NULL;
1094 for(i=scm->fp->count-1; i>=0; i--)
1095 unix_notinflight(scm->fp->fp[i]);
1098 static voidunix_destruct_fds(struct sk_buff *skb)
1100 struct scm_cookie scm;
1101 memset(&scm,0,sizeof(scm));
1102 unix_detach_fds(&scm, skb);
1104 /* Alas, it calls VFS */
1105 /* So fscking what? fput() had been SMP-safe since the last Summer */
1106 scm_destroy(&scm);
1107 sock_wfree(skb);
1110 static voidunix_attach_fds(struct scm_cookie *scm,struct sk_buff *skb)
1112 int i;
1113 for(i=scm->fp->count-1; i>=0; i--)
1114 unix_inflight(scm->fp->fp[i]);
1115 UNIXCB(skb).fp = scm->fp;
1116 skb->destructor = unix_destruct_fds;
1117 scm->fp = NULL;
1121 * Send AF_UNIX data.
1124 static intunix_dgram_sendmsg(struct socket *sock,struct msghdr *msg,int len,
1125 struct scm_cookie *scm)
1127 struct sock *sk = sock->sk;
1128 struct sockaddr_un *sunaddr=msg->msg_name;
1129 unix_socket *other = NULL;
1130 int namelen =0;/* fake GCC */
1131 int err;
1132 unsigned hash;
1133 struct sk_buff *skb;
1134 long timeo;
1136 err = -EOPNOTSUPP;
1137 if(msg->msg_flags&MSG_OOB)
1138 goto out;
1140 if(msg->msg_namelen) {
1141 err =unix_mkname(sunaddr, msg->msg_namelen, &hash);
1142 if(err <0)
1143 goto out;
1144 namelen = err;
1145 }else{
1146 sunaddr = NULL;
1147 err = -ENOTCONN;
1148 other =unix_peer_get(sk);
1149 if(!other)
1150 goto out;
1153 if(sock->passcred && !sk->protinfo.af_unix.addr &&
1154 (err =unix_autobind(sock)) !=0)
1155 goto out;
1157 err = -EMSGSIZE;
1158 if((unsigned)len > sk->sndbuf -32)
1159 goto out;
1161 skb =sock_alloc_send_skb(sk, len,0, msg->msg_flags&MSG_DONTWAIT, &err);
1162 if(skb==NULL)
1163 goto out;
1165 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1166 if(scm->fp)
1167 unix_attach_fds(scm, skb);
1169 skb->h.raw = skb->data;
1170 err =memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1171 if(err)
1172 goto out_free;
1174 timeo =sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1176 restart:
1177 if(!other) {
1178 err = -ECONNRESET;
1179 if(sunaddr == NULL)
1180 goto out_free;
1182 other =unix_find_other(sunaddr, namelen, sk->type, hash, &err);
1183 if(other==NULL)
1184 goto out_free;
1187 unix_state_rlock(other);
1188 err = -EPERM;
1189 if(!unix_may_send(sk, other))
1190 goto out_unlock;
1192 if(other->dead) {
1194 * Check with 1003.1g - what should
1195 * datagram error
1197 unix_state_runlock(other);
1198 sock_put(other);
1200 err =0;
1201 unix_state_wlock(sk);
1202 if(unix_peer(sk) == other) {
1203 unix_peer(sk)=NULL;
1204 unix_state_wunlock(sk);
1206 sock_put(other);
1207 err = -ECONNREFUSED;
1208 }else{
1209 unix_state_wunlock(sk);
1212 other = NULL;
1213 if(err)
1214 goto out_free;
1215 goto restart;
1218 err = -EPIPE;
1219 if(other->shutdown&RCV_SHUTDOWN)
1220 goto out_unlock;
1222 if(skb_queue_len(&other->receive_queue) > other->max_ack_backlog) {
1223 if(!timeo) {
1224 err = -EAGAIN;
1225 goto out_unlock;
1228 timeo =unix_wait_for_peer(other, timeo);
1230 err =sock_intr_errno(timeo);
1231 if(signal_pending(current))
1232 goto out_free;
1234 goto restart;
1237 skb_queue_tail(&other->receive_queue, skb);
1238 unix_state_runlock(other);
1239 other->data_ready(other, len);
1240 sock_put(other);
1241 return len;
1243 out_unlock:
1244 unix_state_runlock(other);
1245 out_free:
1246 kfree_skb(skb);
1247 out:
1248 if(other)
1249 sock_put(other);
1250 return err;
1254 static intunix_stream_sendmsg(struct socket *sock,struct msghdr *msg,int len,
1255 struct scm_cookie *scm)
1257 struct sock *sk = sock->sk;
1258 unix_socket *other = NULL;
1259 struct sockaddr_un *sunaddr=msg->msg_name;
1260 int err,size;
1261 struct sk_buff *skb;
1262 int limit=0;
1263 int sent=0;
1265 err = -EOPNOTSUPP;
1266 if(msg->msg_flags&MSG_OOB)
1267 goto out_err;
1269 if(msg->msg_namelen) {
1270 err = (sk->state==TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP);
1271 goto out_err;
1272 }else{
1273 sunaddr = NULL;
1274 err = -ENOTCONN;
1275 other =unix_peer_get(sk);
1276 if(!other)
1277 goto out_err;
1280 if(sk->shutdown&SEND_SHUTDOWN)
1281 goto pipe_err;
1283 while(sent < len)
1286 * Optimisation for the fact that under 0.01% of X messages typically
1287 * need breaking up.
1290 size=len-sent;
1292 /* Keep two messages in the pipe so it schedules better */
1293 if(size > sk->sndbuf/2-16)
1294 size = sk->sndbuf/2-16;
1297 * Keep to page sized kmalloc()'s as various people
1298 * have suggested. Big mallocs stress the vm too
1299 * much.
1302 if(size > PAGE_SIZE-16)
1303 limit = PAGE_SIZE-16;/* Fall back to a page if we can't grab a big buffer this instant */
1304 else
1305 limit =0;/* Otherwise just grab and wait */
1308 * Grab a buffer
1311 skb=sock_alloc_send_skb(sk,size,limit,msg->msg_flags&MSG_DONTWAIT, &err);
1313 if(skb==NULL)
1314 goto out_err;
1317 * If you pass two values to the sock_alloc_send_skb
1318 * it tries to grab the large buffer with GFP_BUFFER
1319 * (which can fail easily), and if it fails grab the
1320 * fallback size buffer which is under a page and will
1321 * succeed. [Alan]
1323 size =min(size,skb_tailroom(skb));
1325 memcpy(UNIXCREDS(skb), &scm->creds,sizeof(struct ucred));
1326 if(scm->fp)
1327 unix_attach_fds(scm, skb);
1329 if((err =memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) !=0) {
1330 kfree_skb(skb);
1331 goto out_err;
1334 unix_state_rlock(other);
1336 if(other->dead || (other->shutdown & RCV_SHUTDOWN))
1337 goto pipe_err_free;
1339 skb_queue_tail(&other->receive_queue, skb);
1340 unix_state_runlock(other);
1341 other->data_ready(other, size);
1342 sent+=size;
1344 sock_put(other);
1345 return sent;
1347 pipe_err_free:
1348 unix_state_runlock(other);
1349 kfree_skb(skb);
1350 pipe_err:
1351 if(sent==0&& !(msg->msg_flags&MSG_NOSIGNAL))
1352 send_sig(SIGPIPE,current,0);
1353 err = -EPIPE;
1354 out_err:
1355 if(other)
1356 sock_put(other);
1357 return sent ? : err;
1360 static voidunix_copy_addr(struct msghdr *msg,struct sock *sk)
1362 msg->msg_namelen =sizeof(short);
1363 if(sk->protinfo.af_unix.addr) {
1364 msg->msg_namelen=sk->protinfo.af_unix.addr->len;
1365 memcpy(msg->msg_name,
1366 sk->protinfo.af_unix.addr->name,
1367 sk->protinfo.af_unix.addr->len);
1371 static intunix_dgram_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1372 int flags,struct scm_cookie *scm)
1374 struct sock *sk = sock->sk;
1375 int noblock = flags & MSG_DONTWAIT;
1376 struct sk_buff *skb;
1377 int err;
1379 err = -EOPNOTSUPP;
1380 if(flags&MSG_OOB)
1381 goto out;
1383 msg->msg_namelen =0;
1385 skb =skb_recv_datagram(sk, flags, noblock, &err);
1386 if(!skb)
1387 goto out;
1389 wake_up_interruptible(&sk->protinfo.af_unix.peer_wait);
1391 if(msg->msg_name)
1392 unix_copy_addr(msg, skb->sk);
1394 if(size > skb->len)
1395 size = skb->len;
1396 else if(size < skb->len)
1397 msg->msg_flags |= MSG_TRUNC;
1399 err =skb_copy_datagram_iovec(skb,0, msg->msg_iov, size);
1400 if(err)
1401 goto out_free;
1403 scm->creds = *UNIXCREDS(skb);
1405 if(!(flags & MSG_PEEK))
1407 if(UNIXCB(skb).fp)
1408 unix_detach_fds(scm, skb);
1410 else
1412 /* It is questionable: on PEEK we could:
1413 - do not return fds - good, but too simple 8)
1414 - return fds, and do not return them on read (old strategy,
1415 apparently wrong)
1416 - clone fds (I choosed it for now, it is the most universal
1417 solution)
1419 POSIX 1003.1g does not actually define this clearly
1420 at all. POSIX 1003.1g doesn't define a lot of things
1421 clearly however!
1424 if(UNIXCB(skb).fp)
1425 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1427 err = size;
1429 out_free:
1430 skb_free_datagram(sk,skb);
1431 out:
1432 return err;
1436 * Sleep until data has arrive. But check for races..
1439 static longunix_stream_data_wait(unix_socket * sk,long timeo)
1441 DECLARE_WAITQUEUE(wait, current);
1443 unix_state_rlock(sk);
1445 add_wait_queue(sk->sleep, &wait);
1447 for(;;) {
1448 set_current_state(TASK_INTERRUPTIBLE);
1450 if(skb_queue_len(&sk->receive_queue) ||
1451 sk->err ||
1452 (sk->shutdown & RCV_SHUTDOWN) ||
1453 signal_pending(current) ||
1454 !timeo)
1455 break;
1457 set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1458 unix_state_runlock(sk);
1459 timeo =schedule_timeout(timeo);
1460 unix_state_rlock(sk);
1461 clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1464 __set_current_state(TASK_RUNNING);
1465 remove_wait_queue(sk->sleep, &wait);
1466 unix_state_runlock(sk);
1467 return timeo;
1472 static intunix_stream_recvmsg(struct socket *sock,struct msghdr *msg,int size,
1473 int flags,struct scm_cookie *scm)
1475 struct sock *sk = sock->sk;
1476 struct sockaddr_un *sunaddr=msg->msg_name;
1477 int copied =0;
1478 int check_creds =0;
1479 int target;
1480 int err =0;
1481 long timeo;
1483 err = -EINVAL;
1484 if(sk->state != TCP_ESTABLISHED)
1485 goto out;
1487 err = -EOPNOTSUPP;
1488 if(flags&MSG_OOB)
1489 goto out;
1491 target =sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1492 timeo =sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1494 msg->msg_namelen =0;
1496 /* Lock the socket to prevent queue disordering
1497 * while sleeps in memcpy_tomsg
1500 down(&sk->protinfo.af_unix.readsem);
1504 int chunk;
1505 struct sk_buff *skb;
1507 skb=skb_dequeue(&sk->receive_queue);
1508 if(skb==NULL)
1510 if(copied >= target)
1511 break;
1514 * POSIX 1003.1g mandates this order.
1517 if((err =sock_error(sk)) !=0)
1518 break;
1519 if(sk->shutdown & RCV_SHUTDOWN)
1520 break;
1521 err = -EAGAIN;
1522 if(!timeo)
1523 break;
1524 up(&sk->protinfo.af_unix.readsem);
1526 timeo =unix_stream_data_wait(sk, timeo);
1528 if(signal_pending(current)) {
1529 err =sock_intr_errno(timeo);
1530 goto out;
1532 down(&sk->protinfo.af_unix.readsem);
1533 continue;
1536 if(check_creds) {
1537 /* Never glue messages from different writers */
1538 if(memcmp(UNIXCREDS(skb), &scm->creds,sizeof(scm->creds)) !=0) {
1539 skb_queue_head(&sk->receive_queue, skb);
1540 break;
1542 }else{
1543 /* Copy credentials */
1544 scm->creds = *UNIXCREDS(skb);
1545 check_creds =1;
1548 /* Copy address just once */
1549 if(sunaddr)
1551 unix_copy_addr(msg, skb->sk);
1552 sunaddr = NULL;
1555 chunk =min(skb->len, size);
1556 if(memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1557 skb_queue_head(&sk->receive_queue, skb);
1558 if(copied ==0)
1559 copied = -EFAULT;
1560 break;
1562 copied += chunk;
1563 size -= chunk;
1565 /* Mark read part of skb as used */
1566 if(!(flags & MSG_PEEK))
1568 skb_pull(skb, chunk);
1570 if(UNIXCB(skb).fp)
1571 unix_detach_fds(scm, skb);
1573 /* put the skb back if we didn't use it up.. */
1574 if(skb->len)
1576 skb_queue_head(&sk->receive_queue, skb);
1577 break;
1580 kfree_skb(skb);
1582 if(scm->fp)
1583 break;
1585 else
1587 /* It is questionable, see note in unix_dgram_recvmsg.
1589 if(UNIXCB(skb).fp)
1590 scm->fp =scm_fp_dup(UNIXCB(skb).fp);
1592 /* put message back and return */
1593 skb_queue_head(&sk->receive_queue, skb);
1594 break;
1596 }while(size);
1598 up(&sk->protinfo.af_unix.readsem);
1599 out:
1600 return copied ? : err;
1603 static intunix_shutdown(struct socket *sock,int mode)
1605 struct sock *sk = sock->sk;
1606 unix_socket *other;
1608 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1610 if(mode) {
1611 unix_state_wlock(sk);
1612 sk->shutdown |= mode;
1613 other=unix_peer(sk);
1614 if(other)
1615 sock_hold(other);
1616 unix_state_wunlock(sk);
1617 sk->state_change(sk);
1619 if(other && sk->type == SOCK_STREAM) {
1620 int peer_mode =0;
1622 if(mode&RCV_SHUTDOWN)
1623 peer_mode |= SEND_SHUTDOWN;
1624 if(mode&SEND_SHUTDOWN)
1625 peer_mode |= RCV_SHUTDOWN;
1626 unix_state_wlock(other);
1627 other->shutdown |= peer_mode;
1628 unix_state_wunlock(other);
1629 other->state_change(other);
1630 read_lock(&other->callback_lock);
1631 if(peer_mode == SHUTDOWN_MASK)
1632 sk_wake_async(other,1,POLL_HUP);
1633 else if(peer_mode & RCV_SHUTDOWN)
1634 sk_wake_async(other,1,POLL_IN);
1635 read_unlock(&other->callback_lock);
1637 if(other)
1638 sock_put(other);
1640 return0;
1644 static intunix_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
1646 struct sock *sk = sock->sk;
1647 long amount=0;
1648 int err;
1650 switch(cmd)
1652 case SIOCOUTQ:
1653 amount =atomic_read(&sk->wmem_alloc);
1654 err =put_user(amount, (int*)arg);
1655 break;
1656 case SIOCINQ:
1658 struct sk_buff *skb;
1659 if(sk->state==TCP_LISTEN) {
1660 err = -EINVAL;
1661 break;
1664 spin_lock(&sk->receive_queue.lock);
1665 if((skb=skb_peek(&sk->receive_queue))!=NULL)
1666 amount=skb->len;
1667 spin_unlock(&sk->receive_queue.lock);
1668 err =put_user(amount, (int*)arg);
1669 break;
1672 default:
1673 err = -EINVAL;
1674 break;
1676 return err;
1679 static unsigned intunix_poll(struct file * file,struct socket *sock, poll_table *wait)
1681 struct sock *sk = sock->sk;
1682 unsigned int mask;
1684 poll_wait(file, sk->sleep, wait);
1685 mask =0;
1687 /* exceptional events? */
1688 if(sk->err)
1689 mask |= POLLERR;
1690 if(sk->shutdown == SHUTDOWN_MASK)
1691 mask |= POLLHUP;
1693 /* readable? */
1694 if(!skb_queue_empty(&sk->receive_queue) || (sk->shutdown&RCV_SHUTDOWN))
1695 mask |= POLLIN | POLLRDNORM;
1697 /* Connection-based need to check for termination and startup */
1698 if(sk->type == SOCK_STREAM && sk->state==TCP_CLOSE)
1699 mask |= POLLHUP;
1702 * we set writable also when the other side has shut down the
1703 * connection. This prevents stuck sockets.
1705 if(unix_writable(sk))
1706 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1708 return mask;
1712 #ifdef CONFIG_PROC_FS
1713 static intunix_read_proc(char*buffer,char**start, off_t offset,
1714 int length,int*eof,void*data)
1716 off_t pos=0;
1717 off_t begin=0;
1718 int len=0;
1719 int i;
1720 unix_socket *s;
1722 len+=sprintf(buffer,"Num RefCount Protocol Flags Type St "
1723 "Inode Path\n");
1725 read_lock(&unix_table_lock);
1726 forall_unix_sockets(i,s)
1728 unix_state_rlock(s);
1730 len+=sprintf(buffer+len,"%p: %08X %08X %08X %04X %02X %5ld",
1732 atomic_read(&s->refcnt),
1734 s->state == TCP_LISTEN ? __SO_ACCEPTCON :0,
1735 s->type,
1736 s->socket ?
1737 (s->state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
1738 (s->state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
1739 s->socket ? s->socket->inode->i_ino :0);
1741 if(s->protinfo.af_unix.addr)
1743 buffer[len++] =' ';
1744 memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1745 s->protinfo.af_unix.addr->len-sizeof(short));
1746 if(!UNIX_ABSTRACT(s))
1747 len--;
1748 else
1749 buffer[len] ='@';
1750 len += s->protinfo.af_unix.addr->len -sizeof(short);
1752 unix_state_runlock(s);
1754 buffer[len++]='\n';
1756 pos = begin + len;
1757 if(pos<offset)
1759 len=0;
1760 begin=pos;
1762 if(pos>offset+length)
1763 goto done;
1765 *eof =1;
1766 done:
1767 read_unlock(&unix_table_lock);
1768 *start=buffer+(offset-begin);
1769 len-=(offset-begin);
1770 if(len>length)
1771 len=length;
1772 if(len <0)
1773 len =0;
1774 return len;
1776 #endif
1778 struct proto_ops unix_stream_ops = {
1779 family: PF_UNIX,
1781 release: unix_release,
1782 bind: unix_bind,
1783 connect: unix_stream_connect,
1784 socketpair: unix_socketpair,
1785 accept: unix_accept,
1786 getname: unix_getname,
1787 poll: unix_poll,
1788 ioctl: unix_ioctl,
1789 listen: unix_listen,
1790 shutdown: unix_shutdown,
1791 setsockopt: sock_no_setsockopt,
1792 getsockopt: sock_no_getsockopt,
1793 sendmsg: unix_stream_sendmsg,
1794 recvmsg: unix_stream_recvmsg,
1795 mmap: sock_no_mmap,
1798 struct proto_ops unix_dgram_ops = {
1799 family: PF_UNIX,
1801 release: unix_release,
1802 bind: unix_bind,
1803 connect: unix_dgram_connect,
1804 socketpair: unix_socketpair,
1805 accept: sock_no_accept,
1806 getname: unix_getname,
1807 poll: datagram_poll,
1808 ioctl: unix_ioctl,
1809 listen: sock_no_listen,
1810 shutdown: unix_shutdown,
1811 setsockopt: sock_no_setsockopt,
1812 getsockopt: sock_no_getsockopt,
1813 sendmsg: unix_dgram_sendmsg,
1814 recvmsg: unix_dgram_recvmsg,
1815 mmap: sock_no_mmap,
1818 struct net_proto_family unix_family_ops = {
1819 PF_UNIX,
1820 unix_create
1823 #ifdef MODULE
1824 #ifdef CONFIG_SYSCTL
1825 externvoidunix_sysctl_register(void);
1826 externvoidunix_sysctl_unregister(void);
1827 #endif
1829 intinit_module(void)
1830 #else
1831 void __init unix_proto_init(struct net_proto *pro)
1832 #endif
1834 struct sk_buff *dummy_skb;
1836 printk(KERN_INFO "NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.\n");
1837 if(sizeof(struct unix_skb_parms) >sizeof(dummy_skb->cb))
1839 printk(KERN_CRIT "unix_proto_init: panic\n");
1840 #ifdef MODULE
1841 return-1;
1842 #else
1843 return;
1844 #endif
1846 sock_register(&unix_family_ops);
1847 #ifdef CONFIG_PROC_FS
1848 create_proc_read_entry("net/unix",0,0, unix_read_proc, NULL);
1849 #endif
1851 #ifdef MODULE
1852 #ifdef CONFIG_SYSCTL
1853 unix_sysctl_register();
1854 #endif
1856 return0;
1857 #endif
1860 #ifdef MODULE
1861 voidcleanup_module(void)
1863 sock_unregister(PF_UNIX);
1864 #ifdef CONFIG_SYSCTL
1865 unix_sysctl_unregister();
1866 #endif
1867 #ifdef CONFIG_PROC_FS
1868 remove_proc_entry("net/unix",0);
1869 #endif
1871 #endif
1874 * Local variables:
1875 * compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1876 * End:
close