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 struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
122 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 #ifdef CONFIG_SECURITY_NETWORK
130 static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
132 memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
135 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
137 scm->secid = *UNIXSID(skb);
140 static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
143 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
145 #endif /* CONFIG_SECURITY_NETWORK */
148 * SMP locking strategy:
149 * hash table is protected with spinlock unix_table_lock
150 * each socket state is protected by separate rwlock.
153 static inline unsigned unix_hash_fold(__wsum n)
155 unsigned hash = (__force unsigned)n;
158 return hash&(UNIX_HASH_SIZE-1);
161 #define unix_peer(sk) (unix_sk(sk)->peer)
163 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
165 return unix_peer(osk) == sk;
168 static inline int unix_may_send(struct sock *sk, struct sock *osk)
170 return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
173 static struct sock *unix_peer_get(struct sock *s)
181 unix_state_unlock(s);
185 static inline void unix_release_addr(struct unix_address *addr)
187 if (atomic_dec_and_test(&addr->refcnt))
192 * Check unix socket name:
193 * - should be not zero length.
194 * - if started by not zero, should be NULL terminated (FS object)
195 * - if started by zero, it is abstract name.
198 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
200 if (len <= sizeof(short) || len > sizeof(*sunaddr))
202 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
204 if (sunaddr->sun_path[0]) {
206 * This may look like an off by one error but it is a bit more
207 * subtle. 108 is the longest valid AF_UNIX path for a binding.
208 * sun_path[108] doesnt as such exist. However in kernel space
209 * we are guaranteed that it is a valid memory location in our
210 * kernel address buffer.
212 ((char *)sunaddr)[len]=0;
213 len = strlen(sunaddr->sun_path)+1+sizeof(short);
217 *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
221 static void __unix_remove_socket(struct sock *sk)
223 sk_del_node_init(sk);
226 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
228 BUG_TRAP(sk_unhashed(sk));
229 sk_add_node(sk, list);
232 static inline void unix_remove_socket(struct sock *sk)
234 spin_lock(&unix_table_lock);
235 __unix_remove_socket(sk);
236 spin_unlock(&unix_table_lock);
239 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
241 spin_lock(&unix_table_lock);
242 __unix_insert_socket(list, sk);
243 spin_unlock(&unix_table_lock);
246 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
247 int len, int type, unsigned hash)
250 struct hlist_node *node;
252 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
253 struct unix_sock *u = unix_sk(s);
255 if (u->addr->len == len &&
256 !memcmp(u->addr->name, sunname, len))
264 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
270 spin_lock(&unix_table_lock);
271 s = __unix_find_socket_byname(sunname, len, type, hash);
274 spin_unlock(&unix_table_lock);
278 static struct sock *unix_find_socket_byinode(struct inode *i)
281 struct hlist_node *node;
283 spin_lock(&unix_table_lock);
285 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
286 struct dentry *dentry = unix_sk(s)->dentry;
288 if(dentry && dentry->d_inode == i)
296 spin_unlock(&unix_table_lock);
300 static inline int unix_writable(struct sock *sk)
302 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
305 static void unix_write_space(struct sock *sk)
307 read_lock(&sk->sk_callback_lock);
308 if (unix_writable(sk)) {
309 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
310 wake_up_interruptible(sk->sk_sleep);
311 sk_wake_async(sk, 2, POLL_OUT);
313 read_unlock(&sk->sk_callback_lock);
316 /* When dgram socket disconnects (or changes its peer), we clear its receive
317 * queue of packets arrived from previous peer. First, it allows to do
318 * flow control based only on wmem_alloc; second, sk connected to peer
319 * may receive messages only from that peer. */
320 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
322 if (!skb_queue_empty(&sk->sk_receive_queue)) {
323 skb_queue_purge(&sk->sk_receive_queue);
324 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
326 /* If one link of bidirectional dgram pipe is disconnected,
327 * we signal error. Messages are lost. Do not make this,
328 * when peer was not connected to us.
330 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
331 other->sk_err = ECONNRESET;
332 other->sk_error_report(other);
337 static void unix_sock_destructor(struct sock *sk)
339 struct unix_sock *u = unix_sk(sk);
341 skb_queue_purge(&sk->sk_receive_queue);
343 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
344 BUG_TRAP(sk_unhashed(sk));
345 BUG_TRAP(!sk->sk_socket);
346 if (!sock_flag(sk, SOCK_DEAD)) {
347 printk("Attempt to release alive unix socket: %p\n", sk);
352 unix_release_addr(u->addr);
354 atomic_dec(&unix_nr_socks);
355 #ifdef UNIX_REFCNT_DEBUG
356 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
360 static int unix_release_sock (struct sock *sk, int embrion)
362 struct unix_sock *u = unix_sk(sk);
363 struct dentry *dentry;
364 struct vfsmount *mnt;
369 unix_remove_socket(sk);
374 sk->sk_shutdown = SHUTDOWN_MASK;
379 state = sk->sk_state;
380 sk->sk_state = TCP_CLOSE;
381 unix_state_unlock(sk);
383 wake_up_interruptible_all(&u->peer_wait);
385 skpair=unix_peer(sk);
388 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
389 unix_state_lock(skpair);
391 skpair->sk_shutdown = SHUTDOWN_MASK;
392 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
393 skpair->sk_err = ECONNRESET;
394 unix_state_unlock(skpair);
395 skpair->sk_state_change(skpair);
396 read_lock(&skpair->sk_callback_lock);
397 sk_wake_async(skpair,1,POLL_HUP);
398 read_unlock(&skpair->sk_callback_lock);
400 sock_put(skpair); /* It may now die */
401 unix_peer(sk) = NULL;
404 /* Try to flush out this socket. Throw out buffers at least */
406 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
407 if (state==TCP_LISTEN)
408 unix_release_sock(skb->sk, 1);
409 /* passed fds are erased in the kfree_skb hook */
420 /* ---- Socket is dead now and most probably destroyed ---- */
423 * Fixme: BSD difference: In BSD all sockets connected to use get
424 * ECONNRESET and we die on the spot. In Linux we behave
425 * like files and pipes do and wait for the last
428 * Can't we simply set sock->err?
430 * What the above comment does talk about? --ANK(980817)
433 if (atomic_read(&unix_tot_inflight))
434 unix_gc(); /* Garbage collect fds */
439 static int unix_listen(struct socket *sock, int backlog)
442 struct sock *sk = sock->sk;
443 struct unix_sock *u = unix_sk(sk);
446 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
447 goto out; /* Only stream/seqpacket sockets accept */
450 goto out; /* No listens on an unbound socket */
452 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
454 if (backlog > sk->sk_max_ack_backlog)
455 wake_up_interruptible_all(&u->peer_wait);
456 sk->sk_max_ack_backlog = backlog;
457 sk->sk_state = TCP_LISTEN;
458 /* set credentials so connect can copy them */
459 sk->sk_peercred.pid = current->tgid;
460 sk->sk_peercred.uid = current->euid;
461 sk->sk_peercred.gid = current->egid;
465 unix_state_unlock(sk);
470 static int unix_release(struct socket *);
471 static int unix_bind(struct socket *, struct sockaddr *, int);
472 static int unix_stream_connect(struct socket *, struct sockaddr *,
473 int addr_len, int flags);
474 static int unix_socketpair(struct socket *, struct socket *);
475 static int unix_accept(struct socket *, struct socket *, int);
476 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
477 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
478 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
479 static int unix_shutdown(struct socket *, int);
480 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
481 struct msghdr *, size_t);
482 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
483 struct msghdr *, size_t, int);
484 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
485 struct msghdr *, size_t);
486 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
487 struct msghdr *, size_t, int);
488 static int unix_dgram_connect(struct socket *, struct sockaddr *,
490 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
491 struct msghdr *, size_t);
493 static const struct proto_ops unix_stream_ops = {
495 .owner = THIS_MODULE,
496 .release = unix_release,
498 .connect = unix_stream_connect,
499 .socketpair = unix_socketpair,
500 .accept = unix_accept,
501 .getname = unix_getname,
504 .listen = unix_listen,
505 .shutdown = unix_shutdown,
506 .setsockopt = sock_no_setsockopt,
507 .getsockopt = sock_no_getsockopt,
508 .sendmsg = unix_stream_sendmsg,
509 .recvmsg = unix_stream_recvmsg,
510 .mmap = sock_no_mmap,
511 .sendpage = sock_no_sendpage,
514 static const struct proto_ops unix_dgram_ops = {
516 .owner = THIS_MODULE,
517 .release = unix_release,
519 .connect = unix_dgram_connect,
520 .socketpair = unix_socketpair,
521 .accept = sock_no_accept,
522 .getname = unix_getname,
523 .poll = datagram_poll,
525 .listen = sock_no_listen,
526 .shutdown = unix_shutdown,
527 .setsockopt = sock_no_setsockopt,
528 .getsockopt = sock_no_getsockopt,
529 .sendmsg = unix_dgram_sendmsg,
530 .recvmsg = unix_dgram_recvmsg,
531 .mmap = sock_no_mmap,
532 .sendpage = sock_no_sendpage,
535 static const struct proto_ops unix_seqpacket_ops = {
537 .owner = THIS_MODULE,
538 .release = unix_release,
540 .connect = unix_stream_connect,
541 .socketpair = unix_socketpair,
542 .accept = unix_accept,
543 .getname = unix_getname,
544 .poll = datagram_poll,
546 .listen = unix_listen,
547 .shutdown = unix_shutdown,
548 .setsockopt = sock_no_setsockopt,
549 .getsockopt = sock_no_getsockopt,
550 .sendmsg = unix_seqpacket_sendmsg,
551 .recvmsg = unix_dgram_recvmsg,
552 .mmap = sock_no_mmap,
553 .sendpage = sock_no_sendpage,
556 static struct proto unix_proto = {
558 .owner = THIS_MODULE,
559 .obj_size = sizeof(struct unix_sock),
563 * AF_UNIX sockets do not interact with hardware, hence they
564 * dont trigger interrupts - so it's safe for them to have
565 * bh-unsafe locking for their sk_receive_queue.lock. Split off
566 * this special lock-class by reinitializing the spinlock key:
568 static struct lock_class_key af_unix_sk_receive_queue_lock_key;
570 static struct sock * unix_create1(struct socket *sock)
572 struct sock *sk = NULL;
575 if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
578 sk = sk_alloc(PF_UNIX, GFP_KERNEL, &unix_proto, 1);
582 atomic_inc(&unix_nr_socks);
584 sock_init_data(sock,sk);
585 lockdep_set_class(&sk->sk_receive_queue.lock,
586 &af_unix_sk_receive_queue_lock_key);
588 sk->sk_write_space = unix_write_space;
589 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
590 sk->sk_destruct = unix_sock_destructor;
594 spin_lock_init(&u->lock);
595 atomic_set(&u->inflight, sock ? 0 : -1);
596 mutex_init(&u->readlock); /* single task reading lock */
597 init_waitqueue_head(&u->peer_wait);
598 unix_insert_socket(unix_sockets_unbound, sk);
603 static int unix_create(struct socket *sock, int protocol)
605 if (protocol && protocol != PF_UNIX)
606 return -EPROTONOSUPPORT;
608 sock->state = SS_UNCONNECTED;
610 switch (sock->type) {
612 sock->ops = &unix_stream_ops;
615 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
619 sock->type=SOCK_DGRAM;
621 sock->ops = &unix_dgram_ops;
624 sock->ops = &unix_seqpacket_ops;
627 return -ESOCKTNOSUPPORT;
630 return unix_create1(sock) ? 0 : -ENOMEM;
633 static int unix_release(struct socket *sock)
635 struct sock *sk = sock->sk;
642 return unix_release_sock (sk, 0);
645 static int unix_autobind(struct socket *sock)
647 struct sock *sk = sock->sk;
648 struct unix_sock *u = unix_sk(sk);
649 static u32 ordernum = 1;
650 struct unix_address * addr;
653 mutex_lock(&u->readlock);
660 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
664 addr->name->sun_family = AF_UNIX;
665 atomic_set(&addr->refcnt, 1);
668 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
669 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
671 spin_lock(&unix_table_lock);
672 ordernum = (ordernum+1)&0xFFFFF;
674 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
676 spin_unlock(&unix_table_lock);
677 /* Sanity yield. It is unusual case, but yet... */
678 if (!(ordernum&0xFF))
682 addr->hash ^= sk->sk_type;
684 __unix_remove_socket(sk);
686 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
687 spin_unlock(&unix_table_lock);
690 out: mutex_unlock(&u->readlock);
694 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
695 int type, unsigned hash, int *error)
701 if (sunname->sun_path[0]) {
702 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
705 err = vfs_permission(&nd, MAY_WRITE);
710 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
712 u=unix_find_socket_byinode(nd.dentry->d_inode);
716 if (u->sk_type == type)
717 touch_atime(nd.mnt, nd.dentry);
722 if (u->sk_type != type) {
728 u=unix_find_socket_byname(sunname, len, type, hash);
730 struct dentry *dentry;
731 dentry = unix_sk(u)->dentry;
733 touch_atime(unix_sk(u)->mnt, dentry);
747 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
749 struct sock *sk = sock->sk;
750 struct unix_sock *u = unix_sk(sk);
751 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
752 struct dentry * dentry = NULL;
756 struct unix_address *addr;
757 struct hlist_head *list;
760 if (sunaddr->sun_family != AF_UNIX)
763 if (addr_len==sizeof(short)) {
764 err = unix_autobind(sock);
768 err = unix_mkname(sunaddr, addr_len, &hash);
773 mutex_lock(&u->readlock);
780 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
784 memcpy(addr->name, sunaddr, addr_len);
785 addr->len = addr_len;
786 addr->hash = hash ^ sk->sk_type;
787 atomic_set(&addr->refcnt, 1);
789 if (sunaddr->sun_path[0]) {
793 * Get the parent directory, calculate the hash for last
796 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
798 goto out_mknod_parent;
800 dentry = lookup_create(&nd, 0);
801 err = PTR_ERR(dentry);
803 goto out_mknod_unlock;
806 * All right, let's create it.
809 (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
810 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
813 mutex_unlock(&nd.dentry->d_inode->i_mutex);
817 addr->hash = UNIX_HASH_SIZE;
820 spin_lock(&unix_table_lock);
822 if (!sunaddr->sun_path[0]) {
824 if (__unix_find_socket_byname(sunaddr, addr_len,
825 sk->sk_type, hash)) {
826 unix_release_addr(addr);
830 list = &unix_socket_table[addr->hash];
832 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
833 u->dentry = nd.dentry;
838 __unix_remove_socket(sk);
840 __unix_insert_socket(list, sk);
843 spin_unlock(&unix_table_lock);
845 mutex_unlock(&u->readlock);
852 mutex_unlock(&nd.dentry->d_inode->i_mutex);
857 unix_release_addr(addr);
861 static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
863 if (unlikely(sk1 == sk2) || !sk2) {
864 unix_state_lock(sk1);
868 unix_state_lock(sk1);
869 unix_state_lock_nested(sk2);
871 unix_state_lock(sk2);
872 unix_state_lock_nested(sk1);
876 static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
878 if (unlikely(sk1 == sk2) || !sk2) {
879 unix_state_unlock(sk1);
882 unix_state_unlock(sk1);
883 unix_state_unlock(sk2);
886 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
889 struct sock *sk = sock->sk;
890 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
895 if (addr->sa_family != AF_UNSPEC) {
896 err = unix_mkname(sunaddr, alen, &hash);
901 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
902 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
906 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
910 unix_state_double_lock(sk, other);
912 /* Apparently VFS overslept socket death. Retry. */
913 if (sock_flag(other, SOCK_DEAD)) {
914 unix_state_double_unlock(sk, other);
920 if (!unix_may_send(sk, other))
923 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
929 * 1003.1g breaking connected state with AF_UNSPEC
932 unix_state_double_lock(sk, other);
936 * If it was connected, reconnect.
939 struct sock *old_peer = unix_peer(sk);
941 unix_state_double_unlock(sk, other);
943 if (other != old_peer)
944 unix_dgram_disconnected(sk, old_peer);
948 unix_state_double_unlock(sk, other);
953 unix_state_double_unlock(sk, other);
959 static long unix_wait_for_peer(struct sock *other, long timeo)
961 struct unix_sock *u = unix_sk(other);
965 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
967 sched = !sock_flag(other, SOCK_DEAD) &&
968 !(other->sk_shutdown & RCV_SHUTDOWN) &&
969 (skb_queue_len(&other->sk_receive_queue) >
970 other->sk_max_ack_backlog);
972 unix_state_unlock(other);
975 timeo = schedule_timeout(timeo);
977 finish_wait(&u->peer_wait, &wait);
981 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
982 int addr_len, int flags)
984 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
985 struct sock *sk = sock->sk;
986 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
987 struct sock *newsk = NULL;
988 struct sock *other = NULL;
989 struct sk_buff *skb = NULL;
995 err = unix_mkname(sunaddr, addr_len, &hash);
1000 if (test_bit(SOCK_PASSCRED, &sock->flags)
1001 && !u->addr && (err = unix_autobind(sock)) != 0)
1004 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1006 /* First of all allocate resources.
1007 If we will make it after state is locked,
1008 we will have to recheck all again in any case.
1013 /* create new sock for complete connection */
1014 newsk = unix_create1(NULL);
1018 /* Allocate skb for sending to listening sock */
1019 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1024 /* Find listening sock. */
1025 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
1029 /* Latch state of peer */
1030 unix_state_lock(other);
1032 /* Apparently VFS overslept socket death. Retry. */
1033 if (sock_flag(other, SOCK_DEAD)) {
1034 unix_state_unlock(other);
1039 err = -ECONNREFUSED;
1040 if (other->sk_state != TCP_LISTEN)
1043 if (skb_queue_len(&other->sk_receive_queue) >
1044 other->sk_max_ack_backlog) {
1049 timeo = unix_wait_for_peer(other, timeo);
1051 err = sock_intr_errno(timeo);
1052 if (signal_pending(current))
1060 It is tricky place. We need to grab write lock and cannot
1061 drop lock on peer. It is dangerous because deadlock is
1062 possible. Connect to self case and simultaneous
1063 attempt to connect are eliminated by checking socket
1064 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1065 check this before attempt to grab lock.
1067 Well, and we have to recheck the state after socket locked.
1073 /* This is ok... continue with connect */
1075 case TCP_ESTABLISHED:
1076 /* Socket is already connected */
1084 unix_state_lock_nested(sk);
1086 if (sk->sk_state != st) {
1087 unix_state_unlock(sk);
1088 unix_state_unlock(other);
1093 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1095 unix_state_unlock(sk);
1099 /* The way is open! Fastly set all the necessary fields... */
1102 unix_peer(newsk) = sk;
1103 newsk->sk_state = TCP_ESTABLISHED;
1104 newsk->sk_type = sk->sk_type;
1105 newsk->sk_peercred.pid = current->tgid;
1106 newsk->sk_peercred.uid = current->euid;
1107 newsk->sk_peercred.gid = current->egid;
1108 newu = unix_sk(newsk);
1109 newsk->sk_sleep = &newu->peer_wait;
1110 otheru = unix_sk(other);
1112 /* copy address information from listening to new sock*/
1114 atomic_inc(&otheru->addr->refcnt);
1115 newu->addr = otheru->addr;
1117 if (otheru->dentry) {
1118 newu->dentry = dget(otheru->dentry);
1119 newu->mnt = mntget(otheru->mnt);
1122 /* Set credentials */
1123 sk->sk_peercred = other->sk_peercred;
1125 sock->state = SS_CONNECTED;
1126 sk->sk_state = TCP_ESTABLISHED;
1129 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1130 unix_peer(sk) = newsk;
1132 unix_state_unlock(sk);
1134 /* take ten and and send info to listening sock */
1135 spin_lock(&other->sk_receive_queue.lock);
1136 __skb_queue_tail(&other->sk_receive_queue, skb);
1137 /* Undo artificially decreased inflight after embrion
1138 * is installed to listening socket. */
1139 atomic_inc(&newu->inflight);
1140 spin_unlock(&other->sk_receive_queue.lock);
1141 unix_state_unlock(other);
1142 other->sk_data_ready(other, 0);
1148 unix_state_unlock(other);
1154 unix_release_sock(newsk, 0);
1160 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1162 struct sock *ska=socka->sk, *skb = sockb->sk;
1164 /* Join our sockets back to back */
1169 ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
1170 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1171 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1173 if (ska->sk_type != SOCK_DGRAM) {
1174 ska->sk_state = TCP_ESTABLISHED;
1175 skb->sk_state = TCP_ESTABLISHED;
1176 socka->state = SS_CONNECTED;
1177 sockb->state = SS_CONNECTED;
1182 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1184 struct sock *sk = sock->sk;
1186 struct sk_buff *skb;
1190 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1194 if (sk->sk_state != TCP_LISTEN)
1197 /* If socket state is TCP_LISTEN it cannot change (for now...),
1198 * so that no locks are necessary.
1201 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1203 /* This means receive shutdown. */
1210 skb_free_datagram(sk, skb);
1211 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1213 /* attach accepted sock to socket */
1214 unix_state_lock(tsk);
1215 newsock->state = SS_CONNECTED;
1216 sock_graft(tsk, newsock);
1217 unix_state_unlock(tsk);
1225 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1227 struct sock *sk = sock->sk;
1228 struct unix_sock *u;
1229 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1233 sk = unix_peer_get(sk);
1244 unix_state_lock(sk);
1246 sunaddr->sun_family = AF_UNIX;
1247 sunaddr->sun_path[0] = 0;
1248 *uaddr_len = sizeof(short);
1250 struct unix_address *addr = u->addr;
1252 *uaddr_len = addr->len;
1253 memcpy(sunaddr, addr->name, *uaddr_len);
1255 unix_state_unlock(sk);
1261 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1265 scm->fp = UNIXCB(skb).fp;
1266 skb->destructor = sock_wfree;
1267 UNIXCB(skb).fp = NULL;
1269 for (i=scm->fp->count-1; i>=0; i--)
1270 unix_notinflight(scm->fp->fp[i]);
1273 static void unix_destruct_fds(struct sk_buff *skb)
1275 struct scm_cookie scm;
1276 memset(&scm, 0, sizeof(scm));
1277 unix_detach_fds(&scm, skb);
1279 /* Alas, it calls VFS */
1280 /* So fscking what? fput() had been SMP-safe since the last Summer */
1285 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1288 for (i=scm->fp->count-1; i>=0; i--)
1289 unix_inflight(scm->fp->fp[i]);
1290 UNIXCB(skb).fp = scm->fp;
1291 skb->destructor = unix_destruct_fds;
1296 * Send AF_UNIX data.
1299 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1300 struct msghdr *msg, size_t len)
1302 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1303 struct sock *sk = sock->sk;
1304 struct unix_sock *u = unix_sk(sk);
1305 struct sockaddr_un *sunaddr=msg->msg_name;
1306 struct sock *other = NULL;
1307 int namelen = 0; /* fake GCC */
1310 struct sk_buff *skb;
1312 struct scm_cookie tmp_scm;
1314 if (NULL == siocb->scm)
1315 siocb->scm = &tmp_scm;
1316 err = scm_send(sock, msg, siocb->scm);
1321 if (msg->msg_flags&MSG_OOB)
1324 if (msg->msg_namelen) {
1325 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1332 other = unix_peer_get(sk);
1337 if (test_bit(SOCK_PASSCRED, &sock->flags)
1338 && !u->addr && (err = unix_autobind(sock)) != 0)
1342 if (len > sk->sk_sndbuf - 32)
1345 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1349 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1351 unix_attach_fds(siocb->scm, skb);
1352 unix_get_secdata(siocb->scm, skb);
1354 skb_reset_transport_header(skb);
1355 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1359 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1364 if (sunaddr == NULL)
1367 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1373 unix_state_lock(other);
1375 if (!unix_may_send(sk, other))
1378 if (sock_flag(other, SOCK_DEAD)) {
1380 * Check with 1003.1g - what should
1383 unix_state_unlock(other);
1387 unix_state_lock(sk);
1388 if (unix_peer(sk) == other) {
1390 unix_state_unlock(sk);
1392 unix_dgram_disconnected(sk, other);
1394 err = -ECONNREFUSED;
1396 unix_state_unlock(sk);
1406 if (other->sk_shutdown & RCV_SHUTDOWN)
1409 if (sk->sk_type != SOCK_SEQPACKET) {
1410 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1415 if (unix_peer(other) != sk &&
1416 (skb_queue_len(&other->sk_receive_queue) >
1417 other->sk_max_ack_backlog)) {
1423 timeo = unix_wait_for_peer(other, timeo);
1425 err = sock_intr_errno(timeo);
1426 if (signal_pending(current))
1432 skb_queue_tail(&other->sk_receive_queue, skb);
1433 unix_state_unlock(other);
1434 other->sk_data_ready(other, len);
1436 scm_destroy(siocb->scm);
1440 unix_state_unlock(other);
1446 scm_destroy(siocb->scm);
1451 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1452 struct msghdr *msg, size_t len)
1454 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1455 struct sock *sk = sock->sk;
1456 struct sock *other = NULL;
1457 struct sockaddr_un *sunaddr=msg->msg_name;
1459 struct sk_buff *skb;
1461 struct scm_cookie tmp_scm;
1463 if (NULL == siocb->scm)
1464 siocb->scm = &tmp_scm;
1465 err = scm_send(sock, msg, siocb->scm);
1470 if (msg->msg_flags&MSG_OOB)
1473 if (msg->msg_namelen) {
1474 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1479 other = unix_peer(sk);
1484 if (sk->sk_shutdown & SEND_SHUTDOWN)
1490 * Optimisation for the fact that under 0.01% of X
1491 * messages typically need breaking up.
1496 /* Keep two messages in the pipe so it schedules better */
1497 if (size > ((sk->sk_sndbuf >> 1) - 64))
1498 size = (sk->sk_sndbuf >> 1) - 64;
1500 if (size > SKB_MAX_ALLOC)
1501 size = SKB_MAX_ALLOC;
1507 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1513 * If you pass two values to the sock_alloc_send_skb
1514 * it tries to grab the large buffer with GFP_NOFS
1515 * (which can fail easily), and if it fails grab the
1516 * fallback size buffer which is under a page and will
1519 size = min_t(int, size, skb_tailroom(skb));
1521 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1523 unix_attach_fds(siocb->scm, skb);
1525 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1530 unix_state_lock(other);
1532 if (sock_flag(other, SOCK_DEAD) ||
1533 (other->sk_shutdown & RCV_SHUTDOWN))
1536 skb_queue_tail(&other->sk_receive_queue, skb);
1537 unix_state_unlock(other);
1538 other->sk_data_ready(other, size);
1542 scm_destroy(siocb->scm);
1548 unix_state_unlock(other);
1551 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1552 send_sig(SIGPIPE,current,0);
1555 scm_destroy(siocb->scm);
1557 return sent ? : err;
1560 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1561 struct msghdr *msg, size_t len)
1564 struct sock *sk = sock->sk;
1566 err = sock_error(sk);
1570 if (sk->sk_state != TCP_ESTABLISHED)
1573 if (msg->msg_namelen)
1574 msg->msg_namelen = 0;
1576 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1579 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1581 struct unix_sock *u = unix_sk(sk);
1583 msg->msg_namelen = 0;
1585 msg->msg_namelen = u->addr->len;
1586 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1590 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1591 struct msghdr *msg, size_t size,
1594 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1595 struct scm_cookie tmp_scm;
1596 struct sock *sk = sock->sk;
1597 struct unix_sock *u = unix_sk(sk);
1598 int noblock = flags & MSG_DONTWAIT;
1599 struct sk_buff *skb;
1606 msg->msg_namelen = 0;
1608 mutex_lock(&u->readlock);
1610 skb = skb_recv_datagram(sk, flags, noblock, &err);
1614 wake_up_interruptible(&u->peer_wait);
1617 unix_copy_addr(msg, skb->sk);
1619 if (size > skb->len)
1621 else if (size < skb->len)
1622 msg->msg_flags |= MSG_TRUNC;
1624 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1629 siocb->scm = &tmp_scm;
1630 memset(&tmp_scm, 0, sizeof(tmp_scm));
1632 siocb->scm->creds = *UNIXCREDS(skb);
1633 unix_set_secdata(siocb->scm, skb);
1635 if (!(flags & MSG_PEEK))
1638 unix_detach_fds(siocb->scm, skb);
1642 /* It is questionable: on PEEK we could:
1643 - do not return fds - good, but too simple 8)
1644 - return fds, and do not return them on read (old strategy,
1646 - clone fds (I chose it for now, it is the most universal
1649 POSIX 1003.1g does not actually define this clearly
1650 at all. POSIX 1003.1g doesn't define a lot of things
1655 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1659 scm_recv(sock, msg, siocb->scm, flags);
1662 skb_free_datagram(sk,skb);
1664 mutex_unlock(&u->readlock);
1670 * Sleep until data has arrive. But check for races..
1673 static long unix_stream_data_wait(struct sock * sk, long timeo)
1677 unix_state_lock(sk);
1680 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1682 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1684 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1685 signal_pending(current) ||
1689 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1690 unix_state_unlock(sk);
1691 timeo = schedule_timeout(timeo);
1692 unix_state_lock(sk);
1693 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1696 finish_wait(sk->sk_sleep, &wait);
1697 unix_state_unlock(sk);
1703 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1704 struct msghdr *msg, size_t size,
1707 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1708 struct scm_cookie tmp_scm;
1709 struct sock *sk = sock->sk;
1710 struct unix_sock *u = unix_sk(sk);
1711 struct sockaddr_un *sunaddr=msg->msg_name;
1713 int check_creds = 0;
1719 if (sk->sk_state != TCP_ESTABLISHED)
1726 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1727 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1729 msg->msg_namelen = 0;
1731 /* Lock the socket to prevent queue disordering
1732 * while sleeps in memcpy_tomsg
1736 siocb->scm = &tmp_scm;
1737 memset(&tmp_scm, 0, sizeof(tmp_scm));
1740 mutex_lock(&u->readlock);
1745 struct sk_buff *skb;
1747 unix_state_lock(sk);
1748 skb = skb_dequeue(&sk->sk_receive_queue);
1751 if (copied >= target)
1755 * POSIX 1003.1g mandates this order.
1758 if ((err = sock_error(sk)) != 0)
1760 if (sk->sk_shutdown & RCV_SHUTDOWN)
1763 unix_state_unlock(sk);
1767 mutex_unlock(&u->readlock);
1769 timeo = unix_stream_data_wait(sk, timeo);
1771 if (signal_pending(current)) {
1772 err = sock_intr_errno(timeo);
1775 mutex_lock(&u->readlock);
1778 unix_state_unlock(sk);
1781 unix_state_unlock(sk);
1784 /* Never glue messages from different writers */
1785 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1786 skb_queue_head(&sk->sk_receive_queue, skb);
1790 /* Copy credentials */
1791 siocb->scm->creds = *UNIXCREDS(skb);
1795 /* Copy address just once */
1798 unix_copy_addr(msg, skb->sk);
1802 chunk = min_t(unsigned int, skb->len, size);
1803 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1804 skb_queue_head(&sk->sk_receive_queue, skb);
1812 /* Mark read part of skb as used */
1813 if (!(flags & MSG_PEEK))
1815 skb_pull(skb, chunk);
1818 unix_detach_fds(siocb->scm, skb);
1820 /* put the skb back if we didn't use it up.. */
1823 skb_queue_head(&sk->sk_receive_queue, skb);
1834 /* It is questionable, see note in unix_dgram_recvmsg.
1837 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1839 /* put message back and return */
1840 skb_queue_head(&sk->sk_receive_queue, skb);
1845 mutex_unlock(&u->readlock);
1846 scm_recv(sock, msg, siocb->scm, flags);
1848 return copied ? : err;
1851 static int unix_shutdown(struct socket *sock, int mode)
1853 struct sock *sk = sock->sk;
1856 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1859 unix_state_lock(sk);
1860 sk->sk_shutdown |= mode;
1861 other=unix_peer(sk);
1864 unix_state_unlock(sk);
1865 sk->sk_state_change(sk);
1868 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1872 if (mode&RCV_SHUTDOWN)
1873 peer_mode |= SEND_SHUTDOWN;
1874 if (mode&SEND_SHUTDOWN)
1875 peer_mode |= RCV_SHUTDOWN;
1876 unix_state_lock(other);
1877 other->sk_shutdown |= peer_mode;
1878 unix_state_unlock(other);
1879 other->sk_state_change(other);
1880 read_lock(&other->sk_callback_lock);
1881 if (peer_mode == SHUTDOWN_MASK)
1882 sk_wake_async(other,1,POLL_HUP);
1883 else if (peer_mode & RCV_SHUTDOWN)
1884 sk_wake_async(other,1,POLL_IN);
1885 read_unlock(&other->sk_callback_lock);
1893 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1895 struct sock *sk = sock->sk;
1902 amount = atomic_read(&sk->sk_wmem_alloc);
1903 err = put_user(amount, (int __user *)arg);
1907 struct sk_buff *skb;
1909 if (sk->sk_state == TCP_LISTEN) {
1914 spin_lock(&sk->sk_receive_queue.lock);
1915 if (sk->sk_type == SOCK_STREAM ||
1916 sk->sk_type == SOCK_SEQPACKET) {
1917 skb_queue_walk(&sk->sk_receive_queue, skb)
1920 skb = skb_peek(&sk->sk_receive_queue);
1924 spin_unlock(&sk->sk_receive_queue.lock);
1925 err = put_user(amount, (int __user *)arg);
1936 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1938 struct sock *sk = sock->sk;
1941 poll_wait(file, sk->sk_sleep, wait);
1944 /* exceptional events? */
1947 if (sk->sk_shutdown == SHUTDOWN_MASK)
1949 if (sk->sk_shutdown & RCV_SHUTDOWN)
1953 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1954 (sk->sk_shutdown & RCV_SHUTDOWN))
1955 mask |= POLLIN | POLLRDNORM;
1957 /* Connection-based need to check for termination and startup */
1958 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1962 * we set writable also when the other side has shut down the
1963 * connection. This prevents stuck sockets.
1965 if (unix_writable(sk))
1966 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1972 #ifdef CONFIG_PROC_FS
1973 static struct sock *unix_seq_idx(int *iter, loff_t pos)
1978 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
1987 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
1989 spin_lock(&unix_table_lock);
1990 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
1993 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1998 return first_unix_socket(seq->private);
1999 return next_unix_socket(seq->private, v);
2002 static void unix_seq_stop(struct seq_file *seq, void *v)
2004 spin_unlock(&unix_table_lock);
2007 static int unix_seq_show(struct seq_file *seq, void *v)
2011 seq_puts(seq, "Num RefCount Protocol Flags Type St "
2015 struct unix_sock *u = unix_sk(s);
2018 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
2020 atomic_read(&s->sk_refcnt),
2022 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
2025 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
2026 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2034 len = u->addr->len - sizeof(short);
2035 if (!UNIX_ABSTRACT(s))
2041 for ( ; i < len; i++)
2042 seq_putc(seq, u->addr->name->sun_path[i]);
2044 unix_state_unlock(s);
2045 seq_putc(seq, '\n');
2051 static struct seq_operations unix_seq_ops = {
2052 .start = unix_seq_start,
2053 .next = unix_seq_next,
2054 .stop = unix_seq_stop,
2055 .show = unix_seq_show,
2059 static int unix_seq_open(struct inode *inode, struct file *file)
2061 struct seq_file *seq;
2063 int *iter = kmalloc(sizeof(int), GFP_KERNEL);
2068 rc = seq_open(file, &unix_seq_ops);
2072 seq = file->private_data;
2073 seq->private = iter;
2082 static const struct file_operations unix_seq_fops = {
2083 .owner = THIS_MODULE,
2084 .open = unix_seq_open,
2086 .llseek = seq_lseek,
2087 .release = seq_release_private,
2092 static struct net_proto_family unix_family_ops = {
2094 .create = unix_create,
2095 .owner = THIS_MODULE,
2098 static int __init af_unix_init(void)
2101 struct sk_buff *dummy_skb;
2103 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
2105 rc = proto_register(&unix_proto, 1);
2107 printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
2112 sock_register(&unix_family_ops);
2113 #ifdef CONFIG_PROC_FS
2114 proc_net_fops_create("unix", 0, &unix_seq_fops);
2116 unix_sysctl_register();
2121 static void __exit af_unix_exit(void)
2123 sock_unregister(PF_UNIX);
2124 unix_sysctl_unregister();
2125 proc_net_remove("unix");
2126 proto_unregister(&unix_proto);
2129 module_init(af_unix_init);
2130 module_exit(af_unix_exit);
2132 MODULE_LICENSE("GPL");
2133 MODULE_ALIAS_NETPROTO(PF_UNIX);