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/net_namespace.h>
107 #include <net/sock.h>
108 #include <net/tcp_states.h>
109 #include <net/af_unix.h>
110 #include <linux/proc_fs.h>
111 #include <linux/seq_file.h>
113 #include <linux/init.h>
114 #include <linux/poll.h>
115 #include <linux/rtnetlink.h>
116 #include <linux/mount.h>
117 #include <net/checksum.h>
118 #include <linux/security.h>
120 int sysctl_unix_max_dgram_qlen __read_mostly = 10;
122 static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
123 static DEFINE_SPINLOCK(unix_table_lock);
124 static atomic_t unix_nr_socks = ATOMIC_INIT(0);
126 #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
128 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
130 static struct sock *first_unix_socket(int *i)
132 for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) {
133 if (!hlist_empty(&unix_socket_table[*i]))
134 return __sk_head(&unix_socket_table[*i]);
139 static struct sock *next_unix_socket(int *i, struct sock *s)
141 struct sock *next = sk_next(s);
142 /* More in this chain? */
145 /* Look for next non-empty chain. */
146 for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) {
147 if (!hlist_empty(&unix_socket_table[*i]))
148 return __sk_head(&unix_socket_table[*i]);
153 #define forall_unix_sockets(i, s) \
154 for (s = first_unix_socket(&(i)); s; s = next_unix_socket(&(i),(s)))
156 #ifdef CONFIG_SECURITY_NETWORK
157 static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
159 memcpy(UNIXSID(skb), &scm->secid, sizeof(u32));
162 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
164 scm->secid = *UNIXSID(skb);
167 static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
170 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
172 #endif /* CONFIG_SECURITY_NETWORK */
175 * SMP locking strategy:
176 * hash table is protected with spinlock unix_table_lock
177 * each socket state is protected by separate rwlock.
180 static inline unsigned unix_hash_fold(__wsum n)
182 unsigned hash = (__force unsigned)n;
185 return hash&(UNIX_HASH_SIZE-1);
188 #define unix_peer(sk) (unix_sk(sk)->peer)
190 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
192 return unix_peer(osk) == sk;
195 static inline int unix_may_send(struct sock *sk, struct sock *osk)
197 return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
200 static struct sock *unix_peer_get(struct sock *s)
208 unix_state_unlock(s);
212 static inline void unix_release_addr(struct unix_address *addr)
214 if (atomic_dec_and_test(&addr->refcnt))
219 * Check unix socket name:
220 * - should be not zero length.
221 * - if started by not zero, should be NULL terminated (FS object)
222 * - if started by zero, it is abstract name.
225 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
227 if (len <= sizeof(short) || len > sizeof(*sunaddr))
229 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
231 if (sunaddr->sun_path[0]) {
233 * This may look like an off by one error but it is a bit more
234 * subtle. 108 is the longest valid AF_UNIX path for a binding.
235 * sun_path[108] doesnt as such exist. However in kernel space
236 * we are guaranteed that it is a valid memory location in our
237 * kernel address buffer.
239 ((char *)sunaddr)[len]=0;
240 len = strlen(sunaddr->sun_path)+1+sizeof(short);
244 *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
248 static void __unix_remove_socket(struct sock *sk)
250 sk_del_node_init(sk);
253 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
255 BUG_TRAP(sk_unhashed(sk));
256 sk_add_node(sk, list);
259 static inline void unix_remove_socket(struct sock *sk)
261 spin_lock(&unix_table_lock);
262 __unix_remove_socket(sk);
263 spin_unlock(&unix_table_lock);
266 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
268 spin_lock(&unix_table_lock);
269 __unix_insert_socket(list, sk);
270 spin_unlock(&unix_table_lock);
273 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
274 int len, int type, unsigned hash)
277 struct hlist_node *node;
279 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
280 struct unix_sock *u = unix_sk(s);
282 if (u->addr->len == len &&
283 !memcmp(u->addr->name, sunname, len))
291 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
297 spin_lock(&unix_table_lock);
298 s = __unix_find_socket_byname(sunname, len, type, hash);
301 spin_unlock(&unix_table_lock);
305 static struct sock *unix_find_socket_byinode(struct inode *i)
308 struct hlist_node *node;
310 spin_lock(&unix_table_lock);
312 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
313 struct dentry *dentry = unix_sk(s)->dentry;
315 if(dentry && dentry->d_inode == i)
323 spin_unlock(&unix_table_lock);
327 static inline int unix_writable(struct sock *sk)
329 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
332 static void unix_write_space(struct sock *sk)
334 read_lock(&sk->sk_callback_lock);
335 if (unix_writable(sk)) {
336 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
337 wake_up_interruptible_sync(sk->sk_sleep);
338 sk_wake_async(sk, 2, POLL_OUT);
340 read_unlock(&sk->sk_callback_lock);
343 /* When dgram socket disconnects (or changes its peer), we clear its receive
344 * queue of packets arrived from previous peer. First, it allows to do
345 * flow control based only on wmem_alloc; second, sk connected to peer
346 * may receive messages only from that peer. */
347 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
349 if (!skb_queue_empty(&sk->sk_receive_queue)) {
350 skb_queue_purge(&sk->sk_receive_queue);
351 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
353 /* If one link of bidirectional dgram pipe is disconnected,
354 * we signal error. Messages are lost. Do not make this,
355 * when peer was not connected to us.
357 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
358 other->sk_err = ECONNRESET;
359 other->sk_error_report(other);
364 static void unix_sock_destructor(struct sock *sk)
366 struct unix_sock *u = unix_sk(sk);
368 skb_queue_purge(&sk->sk_receive_queue);
370 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
371 BUG_TRAP(sk_unhashed(sk));
372 BUG_TRAP(!sk->sk_socket);
373 if (!sock_flag(sk, SOCK_DEAD)) {
374 printk("Attempt to release alive unix socket: %p\n", sk);
379 unix_release_addr(u->addr);
381 atomic_dec(&unix_nr_socks);
382 #ifdef UNIX_REFCNT_DEBUG
383 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
387 static int unix_release_sock (struct sock *sk, int embrion)
389 struct unix_sock *u = unix_sk(sk);
390 struct dentry *dentry;
391 struct vfsmount *mnt;
396 unix_remove_socket(sk);
401 sk->sk_shutdown = SHUTDOWN_MASK;
406 state = sk->sk_state;
407 sk->sk_state = TCP_CLOSE;
408 unix_state_unlock(sk);
410 wake_up_interruptible_all(&u->peer_wait);
412 skpair=unix_peer(sk);
415 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
416 unix_state_lock(skpair);
418 skpair->sk_shutdown = SHUTDOWN_MASK;
419 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
420 skpair->sk_err = ECONNRESET;
421 unix_state_unlock(skpair);
422 skpair->sk_state_change(skpair);
423 read_lock(&skpair->sk_callback_lock);
424 sk_wake_async(skpair,1,POLL_HUP);
425 read_unlock(&skpair->sk_callback_lock);
427 sock_put(skpair); /* It may now die */
428 unix_peer(sk) = NULL;
431 /* Try to flush out this socket. Throw out buffers at least */
433 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
434 if (state==TCP_LISTEN)
435 unix_release_sock(skb->sk, 1);
436 /* passed fds are erased in the kfree_skb hook */
447 /* ---- Socket is dead now and most probably destroyed ---- */
450 * Fixme: BSD difference: In BSD all sockets connected to use get
451 * ECONNRESET and we die on the spot. In Linux we behave
452 * like files and pipes do and wait for the last
455 * Can't we simply set sock->err?
457 * What the above comment does talk about? --ANK(980817)
460 if (atomic_read(&unix_tot_inflight))
461 unix_gc(); /* Garbage collect fds */
466 static int unix_listen(struct socket *sock, int backlog)
469 struct sock *sk = sock->sk;
470 struct unix_sock *u = unix_sk(sk);
473 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
474 goto out; /* Only stream/seqpacket sockets accept */
477 goto out; /* No listens on an unbound socket */
479 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
481 if (backlog > sk->sk_max_ack_backlog)
482 wake_up_interruptible_all(&u->peer_wait);
483 sk->sk_max_ack_backlog = backlog;
484 sk->sk_state = TCP_LISTEN;
485 /* set credentials so connect can copy them */
486 sk->sk_peercred.pid = task_tgid_vnr(current);
487 sk->sk_peercred.uid = current->euid;
488 sk->sk_peercred.gid = current->egid;
492 unix_state_unlock(sk);
497 static int unix_release(struct socket *);
498 static int unix_bind(struct socket *, struct sockaddr *, int);
499 static int unix_stream_connect(struct socket *, struct sockaddr *,
500 int addr_len, int flags);
501 static int unix_socketpair(struct socket *, struct socket *);
502 static int unix_accept(struct socket *, struct socket *, int);
503 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
504 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
505 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
506 static int unix_shutdown(struct socket *, int);
507 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
508 struct msghdr *, size_t);
509 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
510 struct msghdr *, size_t, int);
511 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
512 struct msghdr *, size_t);
513 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
514 struct msghdr *, size_t, int);
515 static int unix_dgram_connect(struct socket *, struct sockaddr *,
517 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
518 struct msghdr *, size_t);
520 static const struct proto_ops unix_stream_ops = {
522 .owner = THIS_MODULE,
523 .release = unix_release,
525 .connect = unix_stream_connect,
526 .socketpair = unix_socketpair,
527 .accept = unix_accept,
528 .getname = unix_getname,
531 .listen = unix_listen,
532 .shutdown = unix_shutdown,
533 .setsockopt = sock_no_setsockopt,
534 .getsockopt = sock_no_getsockopt,
535 .sendmsg = unix_stream_sendmsg,
536 .recvmsg = unix_stream_recvmsg,
537 .mmap = sock_no_mmap,
538 .sendpage = sock_no_sendpage,
541 static const struct proto_ops unix_dgram_ops = {
543 .owner = THIS_MODULE,
544 .release = unix_release,
546 .connect = unix_dgram_connect,
547 .socketpair = unix_socketpair,
548 .accept = sock_no_accept,
549 .getname = unix_getname,
550 .poll = datagram_poll,
552 .listen = sock_no_listen,
553 .shutdown = unix_shutdown,
554 .setsockopt = sock_no_setsockopt,
555 .getsockopt = sock_no_getsockopt,
556 .sendmsg = unix_dgram_sendmsg,
557 .recvmsg = unix_dgram_recvmsg,
558 .mmap = sock_no_mmap,
559 .sendpage = sock_no_sendpage,
562 static const struct proto_ops unix_seqpacket_ops = {
564 .owner = THIS_MODULE,
565 .release = unix_release,
567 .connect = unix_stream_connect,
568 .socketpair = unix_socketpair,
569 .accept = unix_accept,
570 .getname = unix_getname,
571 .poll = datagram_poll,
573 .listen = unix_listen,
574 .shutdown = unix_shutdown,
575 .setsockopt = sock_no_setsockopt,
576 .getsockopt = sock_no_getsockopt,
577 .sendmsg = unix_seqpacket_sendmsg,
578 .recvmsg = unix_dgram_recvmsg,
579 .mmap = sock_no_mmap,
580 .sendpage = sock_no_sendpage,
583 static struct proto unix_proto = {
585 .owner = THIS_MODULE,
586 .obj_size = sizeof(struct unix_sock),
590 * AF_UNIX sockets do not interact with hardware, hence they
591 * dont trigger interrupts - so it's safe for them to have
592 * bh-unsafe locking for their sk_receive_queue.lock. Split off
593 * this special lock-class by reinitializing the spinlock key:
595 static struct lock_class_key af_unix_sk_receive_queue_lock_key;
597 static struct sock * unix_create1(struct net *net, struct socket *sock)
599 struct sock *sk = NULL;
602 if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
605 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto);
609 atomic_inc(&unix_nr_socks);
611 sock_init_data(sock,sk);
612 lockdep_set_class(&sk->sk_receive_queue.lock,
613 &af_unix_sk_receive_queue_lock_key);
615 sk->sk_write_space = unix_write_space;
616 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
617 sk->sk_destruct = unix_sock_destructor;
621 spin_lock_init(&u->lock);
622 atomic_set(&u->inflight, 0);
623 INIT_LIST_HEAD(&u->link);
624 mutex_init(&u->readlock); /* single task reading lock */
625 init_waitqueue_head(&u->peer_wait);
626 unix_insert_socket(unix_sockets_unbound, sk);
631 static int unix_create(struct net *net, struct socket *sock, int protocol)
633 if (net != &init_net)
634 return -EAFNOSUPPORT;
636 if (protocol && protocol != PF_UNIX)
637 return -EPROTONOSUPPORT;
639 sock->state = SS_UNCONNECTED;
641 switch (sock->type) {
643 sock->ops = &unix_stream_ops;
646 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
650 sock->type=SOCK_DGRAM;
652 sock->ops = &unix_dgram_ops;
655 sock->ops = &unix_seqpacket_ops;
658 return -ESOCKTNOSUPPORT;
661 return unix_create1(net, sock) ? 0 : -ENOMEM;
664 static int unix_release(struct socket *sock)
666 struct sock *sk = sock->sk;
673 return unix_release_sock (sk, 0);
676 static int unix_autobind(struct socket *sock)
678 struct sock *sk = sock->sk;
679 struct unix_sock *u = unix_sk(sk);
680 static u32 ordernum = 1;
681 struct unix_address * addr;
684 mutex_lock(&u->readlock);
691 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
695 addr->name->sun_family = AF_UNIX;
696 atomic_set(&addr->refcnt, 1);
699 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
700 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
702 spin_lock(&unix_table_lock);
703 ordernum = (ordernum+1)&0xFFFFF;
705 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
707 spin_unlock(&unix_table_lock);
708 /* Sanity yield. It is unusual case, but yet... */
709 if (!(ordernum&0xFF))
713 addr->hash ^= sk->sk_type;
715 __unix_remove_socket(sk);
717 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
718 spin_unlock(&unix_table_lock);
721 out: mutex_unlock(&u->readlock);
725 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
726 int type, unsigned hash, int *error)
732 if (sunname->sun_path[0]) {
733 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
736 err = vfs_permission(&nd, MAY_WRITE);
741 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
743 u=unix_find_socket_byinode(nd.dentry->d_inode);
747 if (u->sk_type == type)
748 touch_atime(nd.mnt, nd.dentry);
753 if (u->sk_type != type) {
759 u=unix_find_socket_byname(sunname, len, type, hash);
761 struct dentry *dentry;
762 dentry = unix_sk(u)->dentry;
764 touch_atime(unix_sk(u)->mnt, dentry);
778 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
780 struct sock *sk = sock->sk;
781 struct unix_sock *u = unix_sk(sk);
782 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
783 struct dentry * dentry = NULL;
787 struct unix_address *addr;
788 struct hlist_head *list;
791 if (sunaddr->sun_family != AF_UNIX)
794 if (addr_len==sizeof(short)) {
795 err = unix_autobind(sock);
799 err = unix_mkname(sunaddr, addr_len, &hash);
804 mutex_lock(&u->readlock);
811 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
815 memcpy(addr->name, sunaddr, addr_len);
816 addr->len = addr_len;
817 addr->hash = hash ^ sk->sk_type;
818 atomic_set(&addr->refcnt, 1);
820 if (sunaddr->sun_path[0]) {
824 * Get the parent directory, calculate the hash for last
827 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
829 goto out_mknod_parent;
831 dentry = lookup_create(&nd, 0);
832 err = PTR_ERR(dentry);
834 goto out_mknod_unlock;
837 * All right, let's create it.
840 (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
841 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
844 mutex_unlock(&nd.dentry->d_inode->i_mutex);
848 addr->hash = UNIX_HASH_SIZE;
851 spin_lock(&unix_table_lock);
853 if (!sunaddr->sun_path[0]) {
855 if (__unix_find_socket_byname(sunaddr, addr_len,
856 sk->sk_type, hash)) {
857 unix_release_addr(addr);
861 list = &unix_socket_table[addr->hash];
863 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
864 u->dentry = nd.dentry;
869 __unix_remove_socket(sk);
871 __unix_insert_socket(list, sk);
874 spin_unlock(&unix_table_lock);
876 mutex_unlock(&u->readlock);
883 mutex_unlock(&nd.dentry->d_inode->i_mutex);
888 unix_release_addr(addr);
892 static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
894 if (unlikely(sk1 == sk2) || !sk2) {
895 unix_state_lock(sk1);
899 unix_state_lock(sk1);
900 unix_state_lock_nested(sk2);
902 unix_state_lock(sk2);
903 unix_state_lock_nested(sk1);
907 static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
909 if (unlikely(sk1 == sk2) || !sk2) {
910 unix_state_unlock(sk1);
913 unix_state_unlock(sk1);
914 unix_state_unlock(sk2);
917 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
920 struct sock *sk = sock->sk;
921 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
926 if (addr->sa_family != AF_UNSPEC) {
927 err = unix_mkname(sunaddr, alen, &hash);
932 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
933 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
937 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
941 unix_state_double_lock(sk, other);
943 /* Apparently VFS overslept socket death. Retry. */
944 if (sock_flag(other, SOCK_DEAD)) {
945 unix_state_double_unlock(sk, other);
951 if (!unix_may_send(sk, other))
954 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
960 * 1003.1g breaking connected state with AF_UNSPEC
963 unix_state_double_lock(sk, other);
967 * If it was connected, reconnect.
970 struct sock *old_peer = unix_peer(sk);
972 unix_state_double_unlock(sk, other);
974 if (other != old_peer)
975 unix_dgram_disconnected(sk, old_peer);
979 unix_state_double_unlock(sk, other);
984 unix_state_double_unlock(sk, other);
990 static long unix_wait_for_peer(struct sock *other, long timeo)
992 struct unix_sock *u = unix_sk(other);
996 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
998 sched = !sock_flag(other, SOCK_DEAD) &&
999 !(other->sk_shutdown & RCV_SHUTDOWN) &&
1000 (skb_queue_len(&other->sk_receive_queue) >
1001 other->sk_max_ack_backlog);
1003 unix_state_unlock(other);
1006 timeo = schedule_timeout(timeo);
1008 finish_wait(&u->peer_wait, &wait);
1012 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1013 int addr_len, int flags)
1015 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1016 struct sock *sk = sock->sk;
1017 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1018 struct sock *newsk = NULL;
1019 struct sock *other = NULL;
1020 struct sk_buff *skb = NULL;
1026 err = unix_mkname(sunaddr, addr_len, &hash);
1031 if (test_bit(SOCK_PASSCRED, &sock->flags)
1032 && !u->addr && (err = unix_autobind(sock)) != 0)
1035 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1037 /* First of all allocate resources.
1038 If we will make it after state is locked,
1039 we will have to recheck all again in any case.
1044 /* create new sock for complete connection */
1045 newsk = unix_create1(sk->sk_net, NULL);
1049 /* Allocate skb for sending to listening sock */
1050 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1055 /* Find listening sock. */
1056 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
1060 /* Latch state of peer */
1061 unix_state_lock(other);
1063 /* Apparently VFS overslept socket death. Retry. */
1064 if (sock_flag(other, SOCK_DEAD)) {
1065 unix_state_unlock(other);
1070 err = -ECONNREFUSED;
1071 if (other->sk_state != TCP_LISTEN)
1074 if (skb_queue_len(&other->sk_receive_queue) >
1075 other->sk_max_ack_backlog) {
1080 timeo = unix_wait_for_peer(other, timeo);
1082 err = sock_intr_errno(timeo);
1083 if (signal_pending(current))
1091 It is tricky place. We need to grab write lock and cannot
1092 drop lock on peer. It is dangerous because deadlock is
1093 possible. Connect to self case and simultaneous
1094 attempt to connect are eliminated by checking socket
1095 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1096 check this before attempt to grab lock.
1098 Well, and we have to recheck the state after socket locked.
1104 /* This is ok... continue with connect */
1106 case TCP_ESTABLISHED:
1107 /* Socket is already connected */
1115 unix_state_lock_nested(sk);
1117 if (sk->sk_state != st) {
1118 unix_state_unlock(sk);
1119 unix_state_unlock(other);
1124 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1126 unix_state_unlock(sk);
1130 /* The way is open! Fastly set all the necessary fields... */
1133 unix_peer(newsk) = sk;
1134 newsk->sk_state = TCP_ESTABLISHED;
1135 newsk->sk_type = sk->sk_type;
1136 newsk->sk_peercred.pid = task_tgid_vnr(current);
1137 newsk->sk_peercred.uid = current->euid;
1138 newsk->sk_peercred.gid = current->egid;
1139 newu = unix_sk(newsk);
1140 newsk->sk_sleep = &newu->peer_wait;
1141 otheru = unix_sk(other);
1143 /* copy address information from listening to new sock*/
1145 atomic_inc(&otheru->addr->refcnt);
1146 newu->addr = otheru->addr;
1148 if (otheru->dentry) {
1149 newu->dentry = dget(otheru->dentry);
1150 newu->mnt = mntget(otheru->mnt);
1153 /* Set credentials */
1154 sk->sk_peercred = other->sk_peercred;
1156 sock->state = SS_CONNECTED;
1157 sk->sk_state = TCP_ESTABLISHED;
1160 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1161 unix_peer(sk) = newsk;
1163 unix_state_unlock(sk);
1165 /* take ten and and send info to listening sock */
1166 spin_lock(&other->sk_receive_queue.lock);
1167 __skb_queue_tail(&other->sk_receive_queue, skb);
1168 spin_unlock(&other->sk_receive_queue.lock);
1169 unix_state_unlock(other);
1170 other->sk_data_ready(other, 0);
1176 unix_state_unlock(other);
1182 unix_release_sock(newsk, 0);
1188 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1190 struct sock *ska=socka->sk, *skb = sockb->sk;
1192 /* Join our sockets back to back */
1197 ska->sk_peercred.pid = skb->sk_peercred.pid = task_tgid_vnr(current);
1198 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1199 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1201 if (ska->sk_type != SOCK_DGRAM) {
1202 ska->sk_state = TCP_ESTABLISHED;
1203 skb->sk_state = TCP_ESTABLISHED;
1204 socka->state = SS_CONNECTED;
1205 sockb->state = SS_CONNECTED;
1210 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1212 struct sock *sk = sock->sk;
1214 struct sk_buff *skb;
1218 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1222 if (sk->sk_state != TCP_LISTEN)
1225 /* If socket state is TCP_LISTEN it cannot change (for now...),
1226 * so that no locks are necessary.
1229 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1231 /* This means receive shutdown. */
1238 skb_free_datagram(sk, skb);
1239 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1241 /* attach accepted sock to socket */
1242 unix_state_lock(tsk);
1243 newsock->state = SS_CONNECTED;
1244 sock_graft(tsk, newsock);
1245 unix_state_unlock(tsk);
1253 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1255 struct sock *sk = sock->sk;
1256 struct unix_sock *u;
1257 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1261 sk = unix_peer_get(sk);
1272 unix_state_lock(sk);
1274 sunaddr->sun_family = AF_UNIX;
1275 sunaddr->sun_path[0] = 0;
1276 *uaddr_len = sizeof(short);
1278 struct unix_address *addr = u->addr;
1280 *uaddr_len = addr->len;
1281 memcpy(sunaddr, addr->name, *uaddr_len);
1283 unix_state_unlock(sk);
1289 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1293 scm->fp = UNIXCB(skb).fp;
1294 skb->destructor = sock_wfree;
1295 UNIXCB(skb).fp = NULL;
1297 for (i=scm->fp->count-1; i>=0; i--)
1298 unix_notinflight(scm->fp->fp[i]);
1301 static void unix_destruct_fds(struct sk_buff *skb)
1303 struct scm_cookie scm;
1304 memset(&scm, 0, sizeof(scm));
1305 unix_detach_fds(&scm, skb);
1307 /* Alas, it calls VFS */
1308 /* So fscking what? fput() had been SMP-safe since the last Summer */
1313 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1316 for (i=scm->fp->count-1; i>=0; i--)
1317 unix_inflight(scm->fp->fp[i]);
1318 UNIXCB(skb).fp = scm->fp;
1319 skb->destructor = unix_destruct_fds;
1324 * Send AF_UNIX data.
1327 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1328 struct msghdr *msg, size_t len)
1330 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1331 struct sock *sk = sock->sk;
1332 struct unix_sock *u = unix_sk(sk);
1333 struct sockaddr_un *sunaddr=msg->msg_name;
1334 struct sock *other = NULL;
1335 int namelen = 0; /* fake GCC */
1338 struct sk_buff *skb;
1340 struct scm_cookie tmp_scm;
1342 if (NULL == siocb->scm)
1343 siocb->scm = &tmp_scm;
1344 err = scm_send(sock, msg, siocb->scm);
1349 if (msg->msg_flags&MSG_OOB)
1352 if (msg->msg_namelen) {
1353 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1360 other = unix_peer_get(sk);
1365 if (test_bit(SOCK_PASSCRED, &sock->flags)
1366 && !u->addr && (err = unix_autobind(sock)) != 0)
1370 if (len > sk->sk_sndbuf - 32)
1373 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1377 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1379 unix_attach_fds(siocb->scm, skb);
1380 unix_get_secdata(siocb->scm, skb);
1382 skb_reset_transport_header(skb);
1383 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1387 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1392 if (sunaddr == NULL)
1395 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1401 unix_state_lock(other);
1403 if (!unix_may_send(sk, other))
1406 if (sock_flag(other, SOCK_DEAD)) {
1408 * Check with 1003.1g - what should
1411 unix_state_unlock(other);
1415 unix_state_lock(sk);
1416 if (unix_peer(sk) == other) {
1418 unix_state_unlock(sk);
1420 unix_dgram_disconnected(sk, other);
1422 err = -ECONNREFUSED;
1424 unix_state_unlock(sk);
1434 if (other->sk_shutdown & RCV_SHUTDOWN)
1437 if (sk->sk_type != SOCK_SEQPACKET) {
1438 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1443 if (unix_peer(other) != sk &&
1444 (skb_queue_len(&other->sk_receive_queue) >
1445 other->sk_max_ack_backlog)) {
1451 timeo = unix_wait_for_peer(other, timeo);
1453 err = sock_intr_errno(timeo);
1454 if (signal_pending(current))
1460 skb_queue_tail(&other->sk_receive_queue, skb);
1461 unix_state_unlock(other);
1462 other->sk_data_ready(other, len);
1464 scm_destroy(siocb->scm);
1468 unix_state_unlock(other);
1474 scm_destroy(siocb->scm);
1479 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1480 struct msghdr *msg, size_t len)
1482 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1483 struct sock *sk = sock->sk;
1484 struct sock *other = NULL;
1485 struct sockaddr_un *sunaddr=msg->msg_name;
1487 struct sk_buff *skb;
1489 struct scm_cookie tmp_scm;
1491 if (NULL == siocb->scm)
1492 siocb->scm = &tmp_scm;
1493 err = scm_send(sock, msg, siocb->scm);
1498 if (msg->msg_flags&MSG_OOB)
1501 if (msg->msg_namelen) {
1502 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1507 other = unix_peer(sk);
1512 if (sk->sk_shutdown & SEND_SHUTDOWN)
1518 * Optimisation for the fact that under 0.01% of X
1519 * messages typically need breaking up.
1524 /* Keep two messages in the pipe so it schedules better */
1525 if (size > ((sk->sk_sndbuf >> 1) - 64))
1526 size = (sk->sk_sndbuf >> 1) - 64;
1528 if (size > SKB_MAX_ALLOC)
1529 size = SKB_MAX_ALLOC;
1535 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1541 * If you pass two values to the sock_alloc_send_skb
1542 * it tries to grab the large buffer with GFP_NOFS
1543 * (which can fail easily), and if it fails grab the
1544 * fallback size buffer which is under a page and will
1547 size = min_t(int, size, skb_tailroom(skb));
1549 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1551 unix_attach_fds(siocb->scm, skb);
1553 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1558 unix_state_lock(other);
1560 if (sock_flag(other, SOCK_DEAD) ||
1561 (other->sk_shutdown & RCV_SHUTDOWN))
1564 skb_queue_tail(&other->sk_receive_queue, skb);
1565 unix_state_unlock(other);
1566 other->sk_data_ready(other, size);
1570 scm_destroy(siocb->scm);
1576 unix_state_unlock(other);
1579 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1580 send_sig(SIGPIPE,current,0);
1583 scm_destroy(siocb->scm);
1585 return sent ? : err;
1588 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1589 struct msghdr *msg, size_t len)
1592 struct sock *sk = sock->sk;
1594 err = sock_error(sk);
1598 if (sk->sk_state != TCP_ESTABLISHED)
1601 if (msg->msg_namelen)
1602 msg->msg_namelen = 0;
1604 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1607 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1609 struct unix_sock *u = unix_sk(sk);
1611 msg->msg_namelen = 0;
1613 msg->msg_namelen = u->addr->len;
1614 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1618 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1619 struct msghdr *msg, size_t size,
1622 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1623 struct scm_cookie tmp_scm;
1624 struct sock *sk = sock->sk;
1625 struct unix_sock *u = unix_sk(sk);
1626 int noblock = flags & MSG_DONTWAIT;
1627 struct sk_buff *skb;
1634 msg->msg_namelen = 0;
1636 mutex_lock(&u->readlock);
1638 skb = skb_recv_datagram(sk, flags, noblock, &err);
1642 wake_up_interruptible_sync(&u->peer_wait);
1645 unix_copy_addr(msg, skb->sk);
1647 if (size > skb->len)
1649 else if (size < skb->len)
1650 msg->msg_flags |= MSG_TRUNC;
1652 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1657 siocb->scm = &tmp_scm;
1658 memset(&tmp_scm, 0, sizeof(tmp_scm));
1660 siocb->scm->creds = *UNIXCREDS(skb);
1661 unix_set_secdata(siocb->scm, skb);
1663 if (!(flags & MSG_PEEK))
1666 unix_detach_fds(siocb->scm, skb);
1670 /* It is questionable: on PEEK we could:
1671 - do not return fds - good, but too simple 8)
1672 - return fds, and do not return them on read (old strategy,
1674 - clone fds (I chose it for now, it is the most universal
1677 POSIX 1003.1g does not actually define this clearly
1678 at all. POSIX 1003.1g doesn't define a lot of things
1683 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1687 scm_recv(sock, msg, siocb->scm, flags);
1690 skb_free_datagram(sk,skb);
1692 mutex_unlock(&u->readlock);
1698 * Sleep until data has arrive. But check for races..
1701 static long unix_stream_data_wait(struct sock * sk, long timeo)
1705 unix_state_lock(sk);
1708 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1710 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1712 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1713 signal_pending(current) ||
1717 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1718 unix_state_unlock(sk);
1719 timeo = schedule_timeout(timeo);
1720 unix_state_lock(sk);
1721 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1724 finish_wait(sk->sk_sleep, &wait);
1725 unix_state_unlock(sk);
1731 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1732 struct msghdr *msg, size_t size,
1735 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1736 struct scm_cookie tmp_scm;
1737 struct sock *sk = sock->sk;
1738 struct unix_sock *u = unix_sk(sk);
1739 struct sockaddr_un *sunaddr=msg->msg_name;
1741 int check_creds = 0;
1747 if (sk->sk_state != TCP_ESTABLISHED)
1754 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1755 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1757 msg->msg_namelen = 0;
1759 /* Lock the socket to prevent queue disordering
1760 * while sleeps in memcpy_tomsg
1764 siocb->scm = &tmp_scm;
1765 memset(&tmp_scm, 0, sizeof(tmp_scm));
1768 mutex_lock(&u->readlock);
1773 struct sk_buff *skb;
1775 unix_state_lock(sk);
1776 skb = skb_dequeue(&sk->sk_receive_queue);
1779 if (copied >= target)
1783 * POSIX 1003.1g mandates this order.
1786 if ((err = sock_error(sk)) != 0)
1788 if (sk->sk_shutdown & RCV_SHUTDOWN)
1791 unix_state_unlock(sk);
1795 mutex_unlock(&u->readlock);
1797 timeo = unix_stream_data_wait(sk, timeo);
1799 if (signal_pending(current)) {
1800 err = sock_intr_errno(timeo);
1803 mutex_lock(&u->readlock);
1806 unix_state_unlock(sk);
1809 unix_state_unlock(sk);
1812 /* Never glue messages from different writers */
1813 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1814 skb_queue_head(&sk->sk_receive_queue, skb);
1818 /* Copy credentials */
1819 siocb->scm->creds = *UNIXCREDS(skb);
1823 /* Copy address just once */
1826 unix_copy_addr(msg, skb->sk);
1830 chunk = min_t(unsigned int, skb->len, size);
1831 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1832 skb_queue_head(&sk->sk_receive_queue, skb);
1840 /* Mark read part of skb as used */
1841 if (!(flags & MSG_PEEK))
1843 skb_pull(skb, chunk);
1846 unix_detach_fds(siocb->scm, skb);
1848 /* put the skb back if we didn't use it up.. */
1851 skb_queue_head(&sk->sk_receive_queue, skb);
1862 /* It is questionable, see note in unix_dgram_recvmsg.
1865 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1867 /* put message back and return */
1868 skb_queue_head(&sk->sk_receive_queue, skb);
1873 mutex_unlock(&u->readlock);
1874 scm_recv(sock, msg, siocb->scm, flags);
1876 return copied ? : err;
1879 static int unix_shutdown(struct socket *sock, int mode)
1881 struct sock *sk = sock->sk;
1884 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1887 unix_state_lock(sk);
1888 sk->sk_shutdown |= mode;
1889 other=unix_peer(sk);
1892 unix_state_unlock(sk);
1893 sk->sk_state_change(sk);
1896 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1900 if (mode&RCV_SHUTDOWN)
1901 peer_mode |= SEND_SHUTDOWN;
1902 if (mode&SEND_SHUTDOWN)
1903 peer_mode |= RCV_SHUTDOWN;
1904 unix_state_lock(other);
1905 other->sk_shutdown |= peer_mode;
1906 unix_state_unlock(other);
1907 other->sk_state_change(other);
1908 read_lock(&other->sk_callback_lock);
1909 if (peer_mode == SHUTDOWN_MASK)
1910 sk_wake_async(other,1,POLL_HUP);
1911 else if (peer_mode & RCV_SHUTDOWN)
1912 sk_wake_async(other,1,POLL_IN);
1913 read_unlock(&other->sk_callback_lock);
1921 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1923 struct sock *sk = sock->sk;
1930 amount = atomic_read(&sk->sk_wmem_alloc);
1931 err = put_user(amount, (int __user *)arg);
1935 struct sk_buff *skb;
1937 if (sk->sk_state == TCP_LISTEN) {
1942 spin_lock(&sk->sk_receive_queue.lock);
1943 if (sk->sk_type == SOCK_STREAM ||
1944 sk->sk_type == SOCK_SEQPACKET) {
1945 skb_queue_walk(&sk->sk_receive_queue, skb)
1948 skb = skb_peek(&sk->sk_receive_queue);
1952 spin_unlock(&sk->sk_receive_queue.lock);
1953 err = put_user(amount, (int __user *)arg);
1964 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1966 struct sock *sk = sock->sk;
1969 poll_wait(file, sk->sk_sleep, wait);
1972 /* exceptional events? */
1975 if (sk->sk_shutdown == SHUTDOWN_MASK)
1977 if (sk->sk_shutdown & RCV_SHUTDOWN)
1981 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1982 (sk->sk_shutdown & RCV_SHUTDOWN))
1983 mask |= POLLIN | POLLRDNORM;
1985 /* Connection-based need to check for termination and startup */
1986 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1990 * we set writable also when the other side has shut down the
1991 * connection. This prevents stuck sockets.
1993 if (unix_writable(sk))
1994 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2000 #ifdef CONFIG_PROC_FS
2001 static struct sock *unix_seq_idx(int *iter, loff_t pos)
2006 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
2015 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
2017 spin_lock(&unix_table_lock);
2018 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
2021 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2026 return first_unix_socket(seq->private);
2027 return next_unix_socket(seq->private, v);
2030 static void unix_seq_stop(struct seq_file *seq, void *v)
2032 spin_unlock(&unix_table_lock);
2035 static int unix_seq_show(struct seq_file *seq, void *v)
2039 seq_puts(seq, "Num RefCount Protocol Flags Type St "
2043 struct unix_sock *u = unix_sk(s);
2046 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
2048 atomic_read(&s->sk_refcnt),
2050 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
2053 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
2054 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2062 len = u->addr->len - sizeof(short);
2063 if (!UNIX_ABSTRACT(s))
2069 for ( ; i < len; i++)
2070 seq_putc(seq, u->addr->name->sun_path[i]);
2072 unix_state_unlock(s);
2073 seq_putc(seq, '\n');
2079 static const struct seq_operations unix_seq_ops = {
2080 .start = unix_seq_start,
2081 .next = unix_seq_next,
2082 .stop = unix_seq_stop,
2083 .show = unix_seq_show,
2087 static int unix_seq_open(struct inode *inode, struct file *file)
2089 return seq_open_private(file, &unix_seq_ops, sizeof(int));
2092 static const struct file_operations unix_seq_fops = {
2093 .owner = THIS_MODULE,
2094 .open = unix_seq_open,
2096 .llseek = seq_lseek,
2097 .release = seq_release_private,
2102 static struct net_proto_family unix_family_ops = {
2104 .create = unix_create,
2105 .owner = THIS_MODULE,
2108 static int __init af_unix_init(void)
2111 struct sk_buff *dummy_skb;
2113 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb));
2115 rc = proto_register(&unix_proto, 1);
2117 printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
2122 sock_register(&unix_family_ops);
2123 #ifdef CONFIG_PROC_FS
2124 proc_net_fops_create(&init_net, "unix", 0, &unix_seq_fops);
2126 unix_sysctl_register();
2131 static void __exit af_unix_exit(void)
2133 sock_unregister(PF_UNIX);
2134 unix_sysctl_unregister();
2135 proc_net_remove(&init_net, "unix");
2136 proto_unregister(&unix_proto);
2139 module_init(af_unix_init);
2140 module_exit(af_unix_exit);
2142 MODULE_LICENSE("GPL");
2143 MODULE_ALIAS_NETPROTO(PF_UNIX);