2 * NET4: 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.133 2002/02/08 03:57:19 davem Exp $
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
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
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
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
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
49 * Michal Ostrowski : Module initialization cleanup.
50 * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
51 * the core infrastructure is doing that
52 * for all net proto families now (2.5.69+)
55 * Known differences from reference BSD that was tested:
58 * ECONNREFUSED is not returned from one end of a connected() socket to the
59 * other the moment one end closes.
60 * fstat() doesn't return st_dev=0, and give the blksize as high water mark
61 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
63 * accept() returns a path name even if the connecting socket has closed
64 * in the meantime (BSD loses the path and gives up).
65 * accept() returns 0 length path for an unbound connector. BSD returns 16
66 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
67 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
68 * BSD af_unix apparently has connect forgetting to block properly.
69 * (need to check this with the POSIX spec in detail)
71 * Differences from 2.0.0-11-... (ANK)
72 * Bug fixes and improvements.
73 * - client shutdown killed server socket.
74 * - removed all useless cli/sti pairs.
76 * Semantic changes/extensions.
77 * - generic control message passing.
78 * - SCM_CREDENTIALS control message.
79 * - "Abstract" (not FS based) socket bindings.
80 * Abstract names are sequences of bytes (not zero terminated)
81 * started by 0, so that this name space does not intersect
85 #include <linux/module.h>
86 #include <linux/kernel.h>
87 #include <linux/signal.h>
88 #include <linux/sched.h>
89 #include <linux/errno.h>
90 #include <linux/string.h>
91 #include <linux/stat.h>
92 #include <linux/dcache.h>
93 #include <linux/namei.h>
94 #include <linux/socket.h>
96 #include <linux/fcntl.h>
97 #include <linux/termios.h>
98 #include <linux/sockios.h>
99 #include <linux/net.h>
100 #include <linux/in.h>
101 #include <linux/fs.h>
102 #include <linux/slab.h>
103 #include <asm/uaccess.h>
104 #include <linux/skbuff.h>
105 #include <linux/netdevice.h>
106 #include <net/sock.h>
107 #include <net/tcp_states.h>
108 #include <net/af_unix.h>
109 #include <linux/proc_fs.h>
110 #include <linux/seq_file.h>
112 #include <linux/init.h>
113 #include <linux/poll.h>
114 #include <linux/rtnetlink.h>
115 #include <linux/mount.h>
116 #include <net/checksum.h>
117 #include <linux/security.h>
119 int sysctl_unix_max_dgram_qlen __read_mostly = 10;
121 static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
122 static DEFINE_SPINLOCK(unix_table_lock);
123 static atomic_t unix_nr_socks = ATOMIC_INIT(0);
125 #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
127 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
129 static struct sock *first_unix_socket(int *i)
131 for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) {
132 if (!hlist_empty(&unix_socket_table[*i]))
133 return __sk_head(&unix_socket_table[*i]);
138 static struct sock *next_unix_socket(int *i, struct sock *s)
140 struct sock *next = sk_next(s);
141 /* More in this chain? */
144 /* Look for next non-empty chain. */
145 for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) {
146 if (!hlist_empty(&unix_socket_table[*i]))
147 return __sk_head(&unix_socket_table[*i]);
152 #define forall_unix_sockets(i, s) \
153 for (s = first_unix_socket(&(i)); s; s = next_unix_socket(&(i),(s)))
155 #ifdef CONFIG_SECURITY_NETWORK
156 static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
158 memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
161 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
163 scm->secid = *UNIXSID(skb);
166 static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
169 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
171 #endif /* CONFIG_SECURITY_NETWORK */
174 * SMP locking strategy:
175 * hash table is protected with spinlock unix_table_lock
176 * each socket state is protected by separate rwlock.
179 static inline unsigned unix_hash_fold(__wsum n)
181 unsigned hash = (__force unsigned)n;
184 return hash&(UNIX_HASH_SIZE-1);
187 #define unix_peer(sk) (unix_sk(sk)->peer)
189 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
191 return unix_peer(osk) == sk;
194 static inline int unix_may_send(struct sock *sk, struct sock *osk)
196 return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
199 static struct sock *unix_peer_get(struct sock *s)
207 unix_state_unlock(s);
211 static inline void unix_release_addr(struct unix_address *addr)
213 if (atomic_dec_and_test(&addr->refcnt))
218 * Check unix socket name:
219 * - should be not zero length.
220 * - if started by not zero, should be NULL terminated (FS object)
221 * - if started by zero, it is abstract name.
224 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
226 if (len <= sizeof(short) || len > sizeof(*sunaddr))
228 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
230 if (sunaddr->sun_path[0]) {
232 * This may look like an off by one error but it is a bit more
233 * subtle. 108 is the longest valid AF_UNIX path for a binding.
234 * sun_path[108] doesnt as such exist. However in kernel space
235 * we are guaranteed that it is a valid memory location in our
236 * kernel address buffer.
238 ((char *)sunaddr)[len]=0;
239 len = strlen(sunaddr->sun_path)+1+sizeof(short);
243 *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
247 static void __unix_remove_socket(struct sock *sk)
249 sk_del_node_init(sk);
252 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
254 BUG_TRAP(sk_unhashed(sk));
255 sk_add_node(sk, list);
258 static inline void unix_remove_socket(struct sock *sk)
260 spin_lock(&unix_table_lock);
261 __unix_remove_socket(sk);
262 spin_unlock(&unix_table_lock);
265 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
267 spin_lock(&unix_table_lock);
268 __unix_insert_socket(list, sk);
269 spin_unlock(&unix_table_lock);
272 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
273 int len, int type, unsigned hash)
276 struct hlist_node *node;
278 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
279 struct unix_sock *u = unix_sk(s);
281 if (u->addr->len == len &&
282 !memcmp(u->addr->name, sunname, len))
290 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
296 spin_lock(&unix_table_lock);
297 s = __unix_find_socket_byname(sunname, len, type, hash);
300 spin_unlock(&unix_table_lock);
304 static struct sock *unix_find_socket_byinode(struct inode *i)
307 struct hlist_node *node;
309 spin_lock(&unix_table_lock);
311 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
312 struct dentry *dentry = unix_sk(s)->dentry;
314 if(dentry && dentry->d_inode == i)
322 spin_unlock(&unix_table_lock);
326 static inline int unix_writable(struct sock *sk)
328 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
331 static void unix_write_space(struct sock *sk)
333 read_lock(&sk->sk_callback_lock);
334 if (unix_writable(sk)) {
335 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
336 wake_up_interruptible(sk->sk_sleep);
337 sk_wake_async(sk, 2, POLL_OUT);
339 read_unlock(&sk->sk_callback_lock);
342 /* When dgram socket disconnects (or changes its peer), we clear its receive
343 * queue of packets arrived from previous peer. First, it allows to do
344 * flow control based only on wmem_alloc; second, sk connected to peer
345 * may receive messages only from that peer. */
346 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
348 if (!skb_queue_empty(&sk->sk_receive_queue)) {
349 skb_queue_purge(&sk->sk_receive_queue);
350 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
352 /* If one link of bidirectional dgram pipe is disconnected,
353 * we signal error. Messages are lost. Do not make this,
354 * when peer was not connected to us.
356 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
357 other->sk_err = ECONNRESET;
358 other->sk_error_report(other);
363 static void unix_sock_destructor(struct sock *sk)
365 struct unix_sock *u = unix_sk(sk);
367 skb_queue_purge(&sk->sk_receive_queue);
369 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
370 BUG_TRAP(sk_unhashed(sk));
371 BUG_TRAP(!sk->sk_socket);
372 if (!sock_flag(sk, SOCK_DEAD)) {
373 printk("Attempt to release alive unix socket: %p\n", sk);
378 unix_release_addr(u->addr);
380 atomic_dec(&unix_nr_socks);
381 #ifdef UNIX_REFCNT_DEBUG
382 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
386 static int unix_release_sock (struct sock *sk, int embrion)
388 struct unix_sock *u = unix_sk(sk);
389 struct dentry *dentry;
390 struct vfsmount *mnt;
395 unix_remove_socket(sk);
400 sk->sk_shutdown = SHUTDOWN_MASK;
405 state = sk->sk_state;
406 sk->sk_state = TCP_CLOSE;
407 unix_state_unlock(sk);
409 wake_up_interruptible_all(&u->peer_wait);
411 skpair=unix_peer(sk);
414 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
415 unix_state_lock(skpair);
417 skpair->sk_shutdown = SHUTDOWN_MASK;
418 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
419 skpair->sk_err = ECONNRESET;
420 unix_state_unlock(skpair);
421 skpair->sk_state_change(skpair);
422 read_lock(&skpair->sk_callback_lock);
423 sk_wake_async(skpair,1,POLL_HUP);
424 read_unlock(&skpair->sk_callback_lock);
426 sock_put(skpair); /* It may now die */
427 unix_peer(sk) = NULL;
430 /* Try to flush out this socket. Throw out buffers at least */
432 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
433 if (state==TCP_LISTEN)
434 unix_release_sock(skb->sk, 1);
435 /* passed fds are erased in the kfree_skb hook */
446 /* ---- Socket is dead now and most probably destroyed ---- */
449 * Fixme: BSD difference: In BSD all sockets connected to use get
450 * ECONNRESET and we die on the spot. In Linux we behave
451 * like files and pipes do and wait for the last
454 * Can't we simply set sock->err?
456 * What the above comment does talk about? --ANK(980817)
459 if (atomic_read(&unix_tot_inflight))
460 unix_gc(); /* Garbage collect fds */
465 static int unix_listen(struct socket *sock, int backlog)
468 struct sock *sk = sock->sk;
469 struct unix_sock *u = unix_sk(sk);
472 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
473 goto out; /* Only stream/seqpacket sockets accept */
476 goto out; /* No listens on an unbound socket */
478 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
480 if (backlog > sk->sk_max_ack_backlog)
481 wake_up_interruptible_all(&u->peer_wait);
482 sk->sk_max_ack_backlog = backlog;
483 sk->sk_state = TCP_LISTEN;
484 /* set credentials so connect can copy them */
485 sk->sk_peercred.pid = current->tgid;
486 sk->sk_peercred.uid = current->euid;
487 sk->sk_peercred.gid = current->egid;
491 unix_state_unlock(sk);
496 static int unix_release(struct socket *);
497 static int unix_bind(struct socket *, struct sockaddr *, int);
498 static int unix_stream_connect(struct socket *, struct sockaddr *,
499 int addr_len, int flags);
500 static int unix_socketpair(struct socket *, struct socket *);
501 static int unix_accept(struct socket *, struct socket *, int);
502 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
503 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
504 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
505 static int unix_shutdown(struct socket *, int);
506 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
507 struct msghdr *, size_t);
508 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
509 struct msghdr *, size_t, int);
510 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
511 struct msghdr *, size_t);
512 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
513 struct msghdr *, size_t, int);
514 static int unix_dgram_connect(struct socket *, struct sockaddr *,
516 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
517 struct msghdr *, size_t);
519 static const struct proto_ops unix_stream_ops = {
521 .owner = THIS_MODULE,
522 .release = unix_release,
524 .connect = unix_stream_connect,
525 .socketpair = unix_socketpair,
526 .accept = unix_accept,
527 .getname = unix_getname,
530 .listen = unix_listen,
531 .shutdown = unix_shutdown,
532 .setsockopt = sock_no_setsockopt,
533 .getsockopt = sock_no_getsockopt,
534 .sendmsg = unix_stream_sendmsg,
535 .recvmsg = unix_stream_recvmsg,
536 .mmap = sock_no_mmap,
537 .sendpage = sock_no_sendpage,
540 static const struct proto_ops unix_dgram_ops = {
542 .owner = THIS_MODULE,
543 .release = unix_release,
545 .connect = unix_dgram_connect,
546 .socketpair = unix_socketpair,
547 .accept = sock_no_accept,
548 .getname = unix_getname,
549 .poll = datagram_poll,
551 .listen = sock_no_listen,
552 .shutdown = unix_shutdown,
553 .setsockopt = sock_no_setsockopt,
554 .getsockopt = sock_no_getsockopt,
555 .sendmsg = unix_dgram_sendmsg,
556 .recvmsg = unix_dgram_recvmsg,
557 .mmap = sock_no_mmap,
558 .sendpage = sock_no_sendpage,
561 static const struct proto_ops unix_seqpacket_ops = {
563 .owner = THIS_MODULE,
564 .release = unix_release,
566 .connect = unix_stream_connect,
567 .socketpair = unix_socketpair,
568 .accept = unix_accept,
569 .getname = unix_getname,
570 .poll = datagram_poll,
572 .listen = unix_listen,
573 .shutdown = unix_shutdown,
574 .setsockopt = sock_no_setsockopt,
575 .getsockopt = sock_no_getsockopt,
576 .sendmsg = unix_seqpacket_sendmsg,
577 .recvmsg = unix_dgram_recvmsg,
578 .mmap = sock_no_mmap,
579 .sendpage = sock_no_sendpage,
582 static struct proto unix_proto = {
584 .owner = THIS_MODULE,
585 .obj_size = sizeof(struct unix_sock),
589 * AF_UNIX sockets do not interact with hardware, hence they
590 * dont trigger interrupts - so it's safe for them to have
591 * bh-unsafe locking for their sk_receive_queue.lock. Split off
592 * this special lock-class by reinitializing the spinlock key:
594 static struct lock_class_key af_unix_sk_receive_queue_lock_key;
596 static struct sock * unix_create1(struct socket *sock)
598 struct sock *sk = NULL;
601 if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
604 sk = sk_alloc(PF_UNIX, GFP_KERNEL, &unix_proto, 1);
608 atomic_inc(&unix_nr_socks);
610 sock_init_data(sock,sk);
611 lockdep_set_class(&sk->sk_receive_queue.lock,
612 &af_unix_sk_receive_queue_lock_key);
614 sk->sk_write_space = unix_write_space;
615 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
616 sk->sk_destruct = unix_sock_destructor;
620 spin_lock_init(&u->lock);
621 atomic_set(&u->inflight, 0);
622 INIT_LIST_HEAD(&u->link);
623 mutex_init(&u->readlock); /* single task reading lock */
624 init_waitqueue_head(&u->peer_wait);
625 unix_insert_socket(unix_sockets_unbound, sk);
630 static int unix_create(struct socket *sock, int protocol)
632 if (protocol && protocol != PF_UNIX)
633 return -EPROTONOSUPPORT;
635 sock->state = SS_UNCONNECTED;
637 switch (sock->type) {
639 sock->ops = &unix_stream_ops;
642 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
646 sock->type=SOCK_DGRAM;
648 sock->ops = &unix_dgram_ops;
651 sock->ops = &unix_seqpacket_ops;
654 return -ESOCKTNOSUPPORT;
657 return unix_create1(sock) ? 0 : -ENOMEM;
660 static int unix_release(struct socket *sock)
662 struct sock *sk = sock->sk;
669 return unix_release_sock (sk, 0);
672 static int unix_autobind(struct socket *sock)
674 struct sock *sk = sock->sk;
675 struct unix_sock *u = unix_sk(sk);
676 static u32 ordernum = 1;
677 struct unix_address * addr;
680 mutex_lock(&u->readlock);
687 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
691 addr->name->sun_family = AF_UNIX;
692 atomic_set(&addr->refcnt, 1);
695 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
696 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
698 spin_lock(&unix_table_lock);
699 ordernum = (ordernum+1)&0xFFFFF;
701 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
703 spin_unlock(&unix_table_lock);
704 /* Sanity yield. It is unusual case, but yet... */
705 if (!(ordernum&0xFF))
709 addr->hash ^= sk->sk_type;
711 __unix_remove_socket(sk);
713 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
714 spin_unlock(&unix_table_lock);
717 out: mutex_unlock(&u->readlock);
721 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
722 int type, unsigned hash, int *error)
728 if (sunname->sun_path[0]) {
729 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
732 err = vfs_permission(&nd, MAY_WRITE);
737 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
739 u=unix_find_socket_byinode(nd.dentry->d_inode);
743 if (u->sk_type == type)
744 touch_atime(nd.mnt, nd.dentry);
749 if (u->sk_type != type) {
755 u=unix_find_socket_byname(sunname, len, type, hash);
757 struct dentry *dentry;
758 dentry = unix_sk(u)->dentry;
760 touch_atime(unix_sk(u)->mnt, dentry);
774 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
776 struct sock *sk = sock->sk;
777 struct unix_sock *u = unix_sk(sk);
778 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
779 struct dentry * dentry = NULL;
783 struct unix_address *addr;
784 struct hlist_head *list;
787 if (sunaddr->sun_family != AF_UNIX)
790 if (addr_len==sizeof(short)) {
791 err = unix_autobind(sock);
795 err = unix_mkname(sunaddr, addr_len, &hash);
800 mutex_lock(&u->readlock);
807 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
811 memcpy(addr->name, sunaddr, addr_len);
812 addr->len = addr_len;
813 addr->hash = hash ^ sk->sk_type;
814 atomic_set(&addr->refcnt, 1);
816 if (sunaddr->sun_path[0]) {
820 * Get the parent directory, calculate the hash for last
823 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
825 goto out_mknod_parent;
827 dentry = lookup_create(&nd, 0);
828 err = PTR_ERR(dentry);
830 goto out_mknod_unlock;
833 * All right, let's create it.
836 (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
837 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
840 mutex_unlock(&nd.dentry->d_inode->i_mutex);
844 addr->hash = UNIX_HASH_SIZE;
847 spin_lock(&unix_table_lock);
849 if (!sunaddr->sun_path[0]) {
851 if (__unix_find_socket_byname(sunaddr, addr_len,
852 sk->sk_type, hash)) {
853 unix_release_addr(addr);
857 list = &unix_socket_table[addr->hash];
859 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
860 u->dentry = nd.dentry;
865 __unix_remove_socket(sk);
867 __unix_insert_socket(list, sk);
870 spin_unlock(&unix_table_lock);
872 mutex_unlock(&u->readlock);
879 mutex_unlock(&nd.dentry->d_inode->i_mutex);
884 unix_release_addr(addr);
888 static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
890 if (unlikely(sk1 == sk2) || !sk2) {
891 unix_state_lock(sk1);
895 unix_state_lock(sk1);
896 unix_state_lock_nested(sk2);
898 unix_state_lock(sk2);
899 unix_state_lock_nested(sk1);
903 static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
905 if (unlikely(sk1 == sk2) || !sk2) {
906 unix_state_unlock(sk1);
909 unix_state_unlock(sk1);
910 unix_state_unlock(sk2);
913 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
916 struct sock *sk = sock->sk;
917 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
922 if (addr->sa_family != AF_UNSPEC) {
923 err = unix_mkname(sunaddr, alen, &hash);
928 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
929 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
933 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
937 unix_state_double_lock(sk, other);
939 /* Apparently VFS overslept socket death. Retry. */
940 if (sock_flag(other, SOCK_DEAD)) {
941 unix_state_double_unlock(sk, other);
947 if (!unix_may_send(sk, other))
950 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
956 * 1003.1g breaking connected state with AF_UNSPEC
959 unix_state_double_lock(sk, other);
963 * If it was connected, reconnect.
966 struct sock *old_peer = unix_peer(sk);
968 unix_state_double_unlock(sk, other);
970 if (other != old_peer)
971 unix_dgram_disconnected(sk, old_peer);
975 unix_state_double_unlock(sk, other);
980 unix_state_double_unlock(sk, other);
986 static long unix_wait_for_peer(struct sock *other, long timeo)
988 struct unix_sock *u = unix_sk(other);
992 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
994 sched = !sock_flag(other, SOCK_DEAD) &&
995 !(other->sk_shutdown & RCV_SHUTDOWN) &&
996 (skb_queue_len(&other->sk_receive_queue) >
997 other->sk_max_ack_backlog);
999 unix_state_unlock(other);
1002 timeo = schedule_timeout(timeo);
1004 finish_wait(&u->peer_wait, &wait);
1008 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1009 int addr_len, int flags)
1011 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1012 struct sock *sk = sock->sk;
1013 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1014 struct sock *newsk = NULL;
1015 struct sock *other = NULL;
1016 struct sk_buff *skb = NULL;
1022 err = unix_mkname(sunaddr, addr_len, &hash);
1027 if (test_bit(SOCK_PASSCRED, &sock->flags)
1028 && !u->addr && (err = unix_autobind(sock)) != 0)
1031 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1033 /* First of all allocate resources.
1034 If we will make it after state is locked,
1035 we will have to recheck all again in any case.
1040 /* create new sock for complete connection */
1041 newsk = unix_create1(NULL);
1045 /* Allocate skb for sending to listening sock */
1046 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1051 /* Find listening sock. */
1052 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
1056 /* Latch state of peer */
1057 unix_state_lock(other);
1059 /* Apparently VFS overslept socket death. Retry. */
1060 if (sock_flag(other, SOCK_DEAD)) {
1061 unix_state_unlock(other);
1066 err = -ECONNREFUSED;
1067 if (other->sk_state != TCP_LISTEN)
1070 if (skb_queue_len(&other->sk_receive_queue) >
1071 other->sk_max_ack_backlog) {
1076 timeo = unix_wait_for_peer(other, timeo);
1078 err = sock_intr_errno(timeo);
1079 if (signal_pending(current))
1087 It is tricky place. We need to grab write lock and cannot
1088 drop lock on peer. It is dangerous because deadlock is
1089 possible. Connect to self case and simultaneous
1090 attempt to connect are eliminated by checking socket
1091 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1092 check this before attempt to grab lock.
1094 Well, and we have to recheck the state after socket locked.
1100 /* This is ok... continue with connect */
1102 case TCP_ESTABLISHED:
1103 /* Socket is already connected */
1111 unix_state_lock_nested(sk);
1113 if (sk->sk_state != st) {
1114 unix_state_unlock(sk);
1115 unix_state_unlock(other);
1120 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1122 unix_state_unlock(sk);
1126 /* The way is open! Fastly set all the necessary fields... */
1129 unix_peer(newsk) = sk;
1130 newsk->sk_state = TCP_ESTABLISHED;
1131 newsk->sk_type = sk->sk_type;
1132 newsk->sk_peercred.pid = current->tgid;
1133 newsk->sk_peercred.uid = current->euid;
1134 newsk->sk_peercred.gid = current->egid;
1135 newu = unix_sk(newsk);
1136 newsk->sk_sleep = &newu->peer_wait;
1137 otheru = unix_sk(other);
1139 /* copy address information from listening to new sock*/
1141 atomic_inc(&otheru->addr->refcnt);
1142 newu->addr = otheru->addr;
1144 if (otheru->dentry) {
1145 newu->dentry = dget(otheru->dentry);
1146 newu->mnt = mntget(otheru->mnt);
1149 /* Set credentials */
1150 sk->sk_peercred = other->sk_peercred;
1152 sock->state = SS_CONNECTED;
1153 sk->sk_state = TCP_ESTABLISHED;
1156 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1157 unix_peer(sk) = newsk;
1159 unix_state_unlock(sk);
1161 /* take ten and and send info to listening sock */
1162 spin_lock(&other->sk_receive_queue.lock);
1163 __skb_queue_tail(&other->sk_receive_queue, skb);
1164 spin_unlock(&other->sk_receive_queue.lock);
1165 unix_state_unlock(other);
1166 other->sk_data_ready(other, 0);
1172 unix_state_unlock(other);
1178 unix_release_sock(newsk, 0);
1184 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1186 struct sock *ska=socka->sk, *skb = sockb->sk;
1188 /* Join our sockets back to back */
1193 ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
1194 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1195 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1197 if (ska->sk_type != SOCK_DGRAM) {
1198 ska->sk_state = TCP_ESTABLISHED;
1199 skb->sk_state = TCP_ESTABLISHED;
1200 socka->state = SS_CONNECTED;
1201 sockb->state = SS_CONNECTED;
1206 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1208 struct sock *sk = sock->sk;
1210 struct sk_buff *skb;
1214 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1218 if (sk->sk_state != TCP_LISTEN)
1221 /* If socket state is TCP_LISTEN it cannot change (for now...),
1222 * so that no locks are necessary.
1225 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1227 /* This means receive shutdown. */
1234 skb_free_datagram(sk, skb);
1235 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1237 /* attach accepted sock to socket */
1238 unix_state_lock(tsk);
1239 newsock->state = SS_CONNECTED;
1240 sock_graft(tsk, newsock);
1241 unix_state_unlock(tsk);
1249 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1251 struct sock *sk = sock->sk;
1252 struct unix_sock *u;
1253 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1257 sk = unix_peer_get(sk);
1268 unix_state_lock(sk);
1270 sunaddr->sun_family = AF_UNIX;
1271 sunaddr->sun_path[0] = 0;
1272 *uaddr_len = sizeof(short);
1274 struct unix_address *addr = u->addr;
1276 *uaddr_len = addr->len;
1277 memcpy(sunaddr, addr->name, *uaddr_len);
1279 unix_state_unlock(sk);
1285 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1289 scm->fp = UNIXCB(skb).fp;
1290 skb->destructor = sock_wfree;
1291 UNIXCB(skb).fp = NULL;
1293 for (i=scm->fp->count-1; i>=0; i--)
1294 unix_notinflight(scm->fp->fp[i]);
1297 static void unix_destruct_fds(struct sk_buff *skb)
1299 struct scm_cookie scm;
1300 memset(&scm, 0, sizeof(scm));
1301 unix_detach_fds(&scm, skb);
1303 /* Alas, it calls VFS */
1304 /* So fscking what? fput() had been SMP-safe since the last Summer */
1309 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1312 for (i=scm->fp->count-1; i>=0; i--)
1313 unix_inflight(scm->fp->fp[i]);
1314 UNIXCB(skb).fp = scm->fp;
1315 skb->destructor = unix_destruct_fds;
1320 * Send AF_UNIX data.
1323 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1324 struct msghdr *msg, size_t len)
1326 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1327 struct sock *sk = sock->sk;
1328 struct unix_sock *u = unix_sk(sk);
1329 struct sockaddr_un *sunaddr=msg->msg_name;
1330 struct sock *other = NULL;
1331 int namelen = 0; /* fake GCC */
1334 struct sk_buff *skb;
1336 struct scm_cookie tmp_scm;
1338 if (NULL == siocb->scm)
1339 siocb->scm = &tmp_scm;
1340 err = scm_send(sock, msg, siocb->scm);
1345 if (msg->msg_flags&MSG_OOB)
1348 if (msg->msg_namelen) {
1349 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1356 other = unix_peer_get(sk);
1361 if (test_bit(SOCK_PASSCRED, &sock->flags)
1362 && !u->addr && (err = unix_autobind(sock)) != 0)
1366 if (len > sk->sk_sndbuf - 32)
1369 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1373 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1375 unix_attach_fds(siocb->scm, skb);
1376 unix_get_secdata(siocb->scm, skb);
1378 skb_reset_transport_header(skb);
1379 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1383 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1388 if (sunaddr == NULL)
1391 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1397 unix_state_lock(other);
1399 if (!unix_may_send(sk, other))
1402 if (sock_flag(other, SOCK_DEAD)) {
1404 * Check with 1003.1g - what should
1407 unix_state_unlock(other);
1411 unix_state_lock(sk);
1412 if (unix_peer(sk) == other) {
1414 unix_state_unlock(sk);
1416 unix_dgram_disconnected(sk, other);
1418 err = -ECONNREFUSED;
1420 unix_state_unlock(sk);
1430 if (other->sk_shutdown & RCV_SHUTDOWN)
1433 if (sk->sk_type != SOCK_SEQPACKET) {
1434 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1439 if (unix_peer(other) != sk &&
1440 (skb_queue_len(&other->sk_receive_queue) >
1441 other->sk_max_ack_backlog)) {
1447 timeo = unix_wait_for_peer(other, timeo);
1449 err = sock_intr_errno(timeo);
1450 if (signal_pending(current))
1456 skb_queue_tail(&other->sk_receive_queue, skb);
1457 unix_state_unlock(other);
1458 other->sk_data_ready(other, len);
1460 scm_destroy(siocb->scm);
1464 unix_state_unlock(other);
1470 scm_destroy(siocb->scm);
1475 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1476 struct msghdr *msg, size_t len)
1478 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1479 struct sock *sk = sock->sk;
1480 struct sock *other = NULL;
1481 struct sockaddr_un *sunaddr=msg->msg_name;
1483 struct sk_buff *skb;
1485 struct scm_cookie tmp_scm;
1487 if (NULL == siocb->scm)
1488 siocb->scm = &tmp_scm;
1489 err = scm_send(sock, msg, siocb->scm);
1494 if (msg->msg_flags&MSG_OOB)
1497 if (msg->msg_namelen) {
1498 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1503 other = unix_peer(sk);
1508 if (sk->sk_shutdown & SEND_SHUTDOWN)
1514 * Optimisation for the fact that under 0.01% of X
1515 * messages typically need breaking up.
1520 /* Keep two messages in the pipe so it schedules better */
1521 if (size > ((sk->sk_sndbuf >> 1) - 64))
1522 size = (sk->sk_sndbuf >> 1) - 64;
1524 if (size > SKB_MAX_ALLOC)
1525 size = SKB_MAX_ALLOC;
1531 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1537 * If you pass two values to the sock_alloc_send_skb
1538 * it tries to grab the large buffer with GFP_NOFS
1539 * (which can fail easily), and if it fails grab the
1540 * fallback size buffer which is under a page and will
1543 size = min_t(int, size, skb_tailroom(skb));
1545 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1547 unix_attach_fds(siocb->scm, skb);
1549 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1554 unix_state_lock(other);
1556 if (sock_flag(other, SOCK_DEAD) ||
1557 (other->sk_shutdown & RCV_SHUTDOWN))
1560 skb_queue_tail(&other->sk_receive_queue, skb);
1561 unix_state_unlock(other);
1562 other->sk_data_ready(other, size);
1566 scm_destroy(siocb->scm);
1572 unix_state_unlock(other);
1575 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1576 send_sig(SIGPIPE,current,0);
1579 scm_destroy(siocb->scm);
1581 return sent ? : err;
1584 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1585 struct msghdr *msg, size_t len)
1588 struct sock *sk = sock->sk;
1590 err = sock_error(sk);
1594 if (sk->sk_state != TCP_ESTABLISHED)
1597 if (msg->msg_namelen)
1598 msg->msg_namelen = 0;
1600 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1603 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1605 struct unix_sock *u = unix_sk(sk);
1607 msg->msg_namelen = 0;
1609 msg->msg_namelen = u->addr->len;
1610 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1614 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1615 struct msghdr *msg, size_t size,
1618 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1619 struct scm_cookie tmp_scm;
1620 struct sock *sk = sock->sk;
1621 struct unix_sock *u = unix_sk(sk);
1622 int noblock = flags & MSG_DONTWAIT;
1623 struct sk_buff *skb;
1630 msg->msg_namelen = 0;
1632 mutex_lock(&u->readlock);
1634 skb = skb_recv_datagram(sk, flags, noblock, &err);
1638 wake_up_interruptible(&u->peer_wait);
1641 unix_copy_addr(msg, skb->sk);
1643 if (size > skb->len)
1645 else if (size < skb->len)
1646 msg->msg_flags |= MSG_TRUNC;
1648 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1653 siocb->scm = &tmp_scm;
1654 memset(&tmp_scm, 0, sizeof(tmp_scm));
1656 siocb->scm->creds = *UNIXCREDS(skb);
1657 unix_set_secdata(siocb->scm, skb);
1659 if (!(flags & MSG_PEEK))
1662 unix_detach_fds(siocb->scm, skb);
1666 /* It is questionable: on PEEK we could:
1667 - do not return fds - good, but too simple 8)
1668 - return fds, and do not return them on read (old strategy,
1670 - clone fds (I chose it for now, it is the most universal
1673 POSIX 1003.1g does not actually define this clearly
1674 at all. POSIX 1003.1g doesn't define a lot of things
1679 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1683 scm_recv(sock, msg, siocb->scm, flags);
1686 skb_free_datagram(sk,skb);
1688 mutex_unlock(&u->readlock);
1694 * Sleep until data has arrive. But check for races..
1697 static long unix_stream_data_wait(struct sock * sk, long timeo)
1701 unix_state_lock(sk);
1704 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1706 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1708 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1709 signal_pending(current) ||
1713 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1714 unix_state_unlock(sk);
1715 timeo = schedule_timeout(timeo);
1716 unix_state_lock(sk);
1717 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1720 finish_wait(sk->sk_sleep, &wait);
1721 unix_state_unlock(sk);
1727 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1728 struct msghdr *msg, size_t size,
1731 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1732 struct scm_cookie tmp_scm;
1733 struct sock *sk = sock->sk;
1734 struct unix_sock *u = unix_sk(sk);
1735 struct sockaddr_un *sunaddr=msg->msg_name;
1737 int check_creds = 0;
1743 if (sk->sk_state != TCP_ESTABLISHED)
1750 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1751 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1753 msg->msg_namelen = 0;
1755 /* Lock the socket to prevent queue disordering
1756 * while sleeps in memcpy_tomsg
1760 siocb->scm = &tmp_scm;
1761 memset(&tmp_scm, 0, sizeof(tmp_scm));
1764 mutex_lock(&u->readlock);
1769 struct sk_buff *skb;
1771 unix_state_lock(sk);
1772 skb = skb_dequeue(&sk->sk_receive_queue);
1775 if (copied >= target)
1779 * POSIX 1003.1g mandates this order.
1782 if ((err = sock_error(sk)) != 0)
1784 if (sk->sk_shutdown & RCV_SHUTDOWN)
1787 unix_state_unlock(sk);
1791 mutex_unlock(&u->readlock);
1793 timeo = unix_stream_data_wait(sk, timeo);
1795 if (signal_pending(current)) {
1796 err = sock_intr_errno(timeo);
1799 mutex_lock(&u->readlock);
1802 unix_state_unlock(sk);
1805 unix_state_unlock(sk);
1808 /* Never glue messages from different writers */
1809 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1810 skb_queue_head(&sk->sk_receive_queue, skb);
1814 /* Copy credentials */
1815 siocb->scm->creds = *UNIXCREDS(skb);
1819 /* Copy address just once */
1822 unix_copy_addr(msg, skb->sk);
1826 chunk = min_t(unsigned int, skb->len, size);
1827 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1828 skb_queue_head(&sk->sk_receive_queue, skb);
1836 /* Mark read part of skb as used */
1837 if (!(flags & MSG_PEEK))
1839 skb_pull(skb, chunk);
1842 unix_detach_fds(siocb->scm, skb);
1844 /* put the skb back if we didn't use it up.. */
1847 skb_queue_head(&sk->sk_receive_queue, skb);
1858 /* It is questionable, see note in unix_dgram_recvmsg.
1861 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1863 /* put message back and return */
1864 skb_queue_head(&sk->sk_receive_queue, skb);
1869 mutex_unlock(&u->readlock);
1870 scm_recv(sock, msg, siocb->scm, flags);
1872 return copied ? : err;
1875 static int unix_shutdown(struct socket *sock, int mode)
1877 struct sock *sk = sock->sk;
1880 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1883 unix_state_lock(sk);
1884 sk->sk_shutdown |= mode;
1885 other=unix_peer(sk);
1888 unix_state_unlock(sk);
1889 sk->sk_state_change(sk);
1892 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1896 if (mode&RCV_SHUTDOWN)
1897 peer_mode |= SEND_SHUTDOWN;
1898 if (mode&SEND_SHUTDOWN)
1899 peer_mode |= RCV_SHUTDOWN;
1900 unix_state_lock(other);
1901 other->sk_shutdown |= peer_mode;
1902 unix_state_unlock(other);
1903 other->sk_state_change(other);
1904 read_lock(&other->sk_callback_lock);
1905 if (peer_mode == SHUTDOWN_MASK)
1906 sk_wake_async(other,1,POLL_HUP);
1907 else if (peer_mode & RCV_SHUTDOWN)
1908 sk_wake_async(other,1,POLL_IN);
1909 read_unlock(&other->sk_callback_lock);
1917 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1919 struct sock *sk = sock->sk;
1926 amount = atomic_read(&sk->sk_wmem_alloc);
1927 err = put_user(amount, (int __user *)arg);
1931 struct sk_buff *skb;
1933 if (sk->sk_state == TCP_LISTEN) {
1938 spin_lock(&sk->sk_receive_queue.lock);
1939 if (sk->sk_type == SOCK_STREAM ||
1940 sk->sk_type == SOCK_SEQPACKET) {
1941 skb_queue_walk(&sk->sk_receive_queue, skb)
1944 skb = skb_peek(&sk->sk_receive_queue);
1948 spin_unlock(&sk->sk_receive_queue.lock);
1949 err = put_user(amount, (int __user *)arg);
1960 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1962 struct sock *sk = sock->sk;
1965 poll_wait(file, sk->sk_sleep, wait);
1968 /* exceptional events? */
1971 if (sk->sk_shutdown == SHUTDOWN_MASK)
1973 if (sk->sk_shutdown & RCV_SHUTDOWN)
1977 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1978 (sk->sk_shutdown & RCV_SHUTDOWN))
1979 mask |= POLLIN | POLLRDNORM;
1981 /* Connection-based need to check for termination and startup */
1982 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1986 * we set writable also when the other side has shut down the
1987 * connection. This prevents stuck sockets.
1989 if (unix_writable(sk))
1990 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1996 #ifdef CONFIG_PROC_FS
1997 static struct sock *unix_seq_idx(int *iter, loff_t pos)
2002 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
2011 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
2013 spin_lock(&unix_table_lock);
2014 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
2017 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2022 return first_unix_socket(seq->private);
2023 return next_unix_socket(seq->private, v);
2026 static void unix_seq_stop(struct seq_file *seq, void *v)
2028 spin_unlock(&unix_table_lock);
2031 static int unix_seq_show(struct seq_file *seq, void *v)
2035 seq_puts(seq, "Num RefCount Protocol Flags Type St "
2039 struct unix_sock *u = unix_sk(s);
2042 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
2044 atomic_read(&s->sk_refcnt),
2046 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
2049 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
2050 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2058 len = u->addr->len - sizeof(short);
2059 if (!UNIX_ABSTRACT(s))
2065 for ( ; i < len; i++)
2066 seq_putc(seq, u->addr->name->sun_path[i]);
2068 unix_state_unlock(s);
2069 seq_putc(seq, '\n');
2075 static const struct seq_operations unix_seq_ops = {
2076 .start = unix_seq_start,
2077 .next = unix_seq_next,
2078 .stop = unix_seq_stop,
2079 .show = unix_seq_show,
2083 static int unix_seq_open(struct inode *inode, struct file *file)
2085 struct seq_file *seq;
2087 int *iter = kmalloc(sizeof(int), GFP_KERNEL);
2092 rc = seq_open(file, &unix_seq_ops);
2096 seq = file->private_data;
2097 seq->private = iter;
2106 static const struct file_operations unix_seq_fops = {
2107 .owner = THIS_MODULE,
2108 .open = unix_seq_open,
2110 .llseek = seq_lseek,
2111 .release = seq_release_private,
2116 static struct net_proto_family unix_family_ops = {
2118 .create = unix_create,
2119 .owner = THIS_MODULE,
2122 static int __init af_unix_init(void)
2125 struct sk_buff *dummy_skb;
2127 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
2129 rc = proto_register(&unix_proto, 1);
2131 printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
2136 sock_register(&unix_family_ops);
2137 #ifdef CONFIG_PROC_FS
2138 proc_net_fops_create("unix", 0, &unix_seq_fops);
2140 unix_sysctl_register();
2145 static void __exit af_unix_exit(void)
2147 sock_unregister(PF_UNIX);
2148 unix_sysctl_unregister();
2149 proc_net_remove("unix");
2150 proto_unregister(&unix_proto);
2153 module_init(af_unix_init);
2154 module_exit(af_unix_exit);
2156 MODULE_LICENSE("GPL");
2157 MODULE_ALIAS_NETPROTO(PF_UNIX);