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/config.h>
87 #include <linux/kernel.h>
88 #include <linux/signal.h>
89 #include <linux/sched.h>
90 #include <linux/errno.h>
91 #include <linux/string.h>
92 #include <linux/stat.h>
93 #include <linux/dcache.h>
94 #include <linux/namei.h>
95 #include <linux/socket.h>
97 #include <linux/fcntl.h>
98 #include <linux/termios.h>
99 #include <linux/sockios.h>
100 #include <linux/net.h>
101 #include <linux/in.h>
102 #include <linux/fs.h>
103 #include <linux/slab.h>
104 #include <asm/uaccess.h>
105 #include <linux/skbuff.h>
106 #include <linux/netdevice.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/smp_lock.h>
116 #include <linux/rtnetlink.h>
117 #include <linux/mount.h>
118 #include <net/checksum.h>
119 #include <linux/security.h>
121 int sysctl_unix_max_dgram_qlen
= 10;
123 struct hlist_head unix_socket_table
[UNIX_HASH_SIZE
+ 1];
124 DEFINE_SPINLOCK(unix_table_lock
);
125 static atomic_t unix_nr_socks
= ATOMIC_INIT(0);
127 #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
129 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
131 #ifdef CONFIG_SECURITY_NETWORK
132 static void unix_get_peersec_dgram(struct sk_buff
*skb
)
136 err
= security_socket_getpeersec_dgram(skb
, UNIXSECDATA(skb
),
139 *(UNIXSECDATA(skb
)) = NULL
;
142 static inline void unix_set_secdata(struct scm_cookie
*scm
, struct sk_buff
*skb
)
144 scm
->secdata
= *UNIXSECDATA(skb
);
145 scm
->seclen
= *UNIXSECLEN(skb
);
148 static void unix_get_peersec_dgram(struct sk_buff
*skb
)
151 static inline void unix_set_secdata(struct scm_cookie
*scm
, struct sk_buff
*skb
)
153 #endif /* CONFIG_SECURITY_NETWORK */
156 * SMP locking strategy:
157 * hash table is protected with spinlock unix_table_lock
158 * each socket state is protected by separate rwlock.
161 static inline unsigned unix_hash_fold(unsigned hash
)
165 return hash
&(UNIX_HASH_SIZE
-1);
168 #define unix_peer(sk) (unix_sk(sk)->peer)
170 static inline int unix_our_peer(struct sock
*sk
, struct sock
*osk
)
172 return unix_peer(osk
) == sk
;
175 static inline int unix_may_send(struct sock
*sk
, struct sock
*osk
)
177 return (unix_peer(osk
) == NULL
|| unix_our_peer(sk
, osk
));
180 static struct sock
*unix_peer_get(struct sock
*s
)
188 unix_state_runlock(s
);
192 static inline void unix_release_addr(struct unix_address
*addr
)
194 if (atomic_dec_and_test(&addr
->refcnt
))
199 * Check unix socket name:
200 * - should be not zero length.
201 * - if started by not zero, should be NULL terminated (FS object)
202 * - if started by zero, it is abstract name.
205 static int unix_mkname(struct sockaddr_un
* sunaddr
, int len
, unsigned *hashp
)
207 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
209 if (!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
)
211 if (sunaddr
->sun_path
[0]) {
213 * This may look like an off by one error but it is a bit more
214 * subtle. 108 is the longest valid AF_UNIX path for a binding.
215 * sun_path[108] doesnt as such exist. However in kernel space
216 * we are guaranteed that it is a valid memory location in our
217 * kernel address buffer.
219 ((char *)sunaddr
)[len
]=0;
220 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
224 *hashp
= unix_hash_fold(csum_partial((char*)sunaddr
, len
, 0));
228 static void __unix_remove_socket(struct sock
*sk
)
230 sk_del_node_init(sk
);
233 static void __unix_insert_socket(struct hlist_head
*list
, struct sock
*sk
)
235 BUG_TRAP(sk_unhashed(sk
));
236 sk_add_node(sk
, list
);
239 static inline void unix_remove_socket(struct sock
*sk
)
241 spin_lock(&unix_table_lock
);
242 __unix_remove_socket(sk
);
243 spin_unlock(&unix_table_lock
);
246 static inline void unix_insert_socket(struct hlist_head
*list
, struct sock
*sk
)
248 spin_lock(&unix_table_lock
);
249 __unix_insert_socket(list
, sk
);
250 spin_unlock(&unix_table_lock
);
253 static struct sock
*__unix_find_socket_byname(struct sockaddr_un
*sunname
,
254 int len
, int type
, unsigned hash
)
257 struct hlist_node
*node
;
259 sk_for_each(s
, node
, &unix_socket_table
[hash
^ type
]) {
260 struct unix_sock
*u
= unix_sk(s
);
262 if (u
->addr
->len
== len
&&
263 !memcmp(u
->addr
->name
, sunname
, len
))
271 static inline struct sock
*unix_find_socket_byname(struct sockaddr_un
*sunname
,
277 spin_lock(&unix_table_lock
);
278 s
= __unix_find_socket_byname(sunname
, len
, type
, hash
);
281 spin_unlock(&unix_table_lock
);
285 static struct sock
*unix_find_socket_byinode(struct inode
*i
)
288 struct hlist_node
*node
;
290 spin_lock(&unix_table_lock
);
292 &unix_socket_table
[i
->i_ino
& (UNIX_HASH_SIZE
- 1)]) {
293 struct dentry
*dentry
= unix_sk(s
)->dentry
;
295 if(dentry
&& dentry
->d_inode
== i
)
303 spin_unlock(&unix_table_lock
);
307 static inline int unix_writable(struct sock
*sk
)
309 return (atomic_read(&sk
->sk_wmem_alloc
) << 2) <= sk
->sk_sndbuf
;
312 static void unix_write_space(struct sock
*sk
)
314 read_lock(&sk
->sk_callback_lock
);
315 if (unix_writable(sk
)) {
316 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
317 wake_up_interruptible(sk
->sk_sleep
);
318 sk_wake_async(sk
, 2, POLL_OUT
);
320 read_unlock(&sk
->sk_callback_lock
);
323 /* When dgram socket disconnects (or changes its peer), we clear its receive
324 * queue of packets arrived from previous peer. First, it allows to do
325 * flow control based only on wmem_alloc; second, sk connected to peer
326 * may receive messages only from that peer. */
327 static void unix_dgram_disconnected(struct sock
*sk
, struct sock
*other
)
329 if (!skb_queue_empty(&sk
->sk_receive_queue
)) {
330 skb_queue_purge(&sk
->sk_receive_queue
);
331 wake_up_interruptible_all(&unix_sk(sk
)->peer_wait
);
333 /* If one link of bidirectional dgram pipe is disconnected,
334 * we signal error. Messages are lost. Do not make this,
335 * when peer was not connected to us.
337 if (!sock_flag(other
, SOCK_DEAD
) && unix_peer(other
) == sk
) {
338 other
->sk_err
= ECONNRESET
;
339 other
->sk_error_report(other
);
344 static void unix_sock_destructor(struct sock
*sk
)
346 struct unix_sock
*u
= unix_sk(sk
);
348 skb_queue_purge(&sk
->sk_receive_queue
);
350 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
351 BUG_TRAP(sk_unhashed(sk
));
352 BUG_TRAP(!sk
->sk_socket
);
353 if (!sock_flag(sk
, SOCK_DEAD
)) {
354 printk("Attempt to release alive unix socket: %p\n", sk
);
359 unix_release_addr(u
->addr
);
361 atomic_dec(&unix_nr_socks
);
362 #ifdef UNIX_REFCNT_DEBUG
363 printk(KERN_DEBUG
"UNIX %p is destroyed, %d are still alive.\n", sk
, atomic_read(&unix_nr_socks
));
367 static int unix_release_sock (struct sock
*sk
, int embrion
)
369 struct unix_sock
*u
= unix_sk(sk
);
370 struct dentry
*dentry
;
371 struct vfsmount
*mnt
;
376 unix_remove_socket(sk
);
379 unix_state_wlock(sk
);
381 sk
->sk_shutdown
= SHUTDOWN_MASK
;
386 state
= sk
->sk_state
;
387 sk
->sk_state
= TCP_CLOSE
;
388 unix_state_wunlock(sk
);
390 wake_up_interruptible_all(&u
->peer_wait
);
392 skpair
=unix_peer(sk
);
395 if (sk
->sk_type
== SOCK_STREAM
|| sk
->sk_type
== SOCK_SEQPACKET
) {
396 unix_state_wlock(skpair
);
398 skpair
->sk_shutdown
= SHUTDOWN_MASK
;
399 if (!skb_queue_empty(&sk
->sk_receive_queue
) || embrion
)
400 skpair
->sk_err
= ECONNRESET
;
401 unix_state_wunlock(skpair
);
402 skpair
->sk_state_change(skpair
);
403 read_lock(&skpair
->sk_callback_lock
);
404 sk_wake_async(skpair
,1,POLL_HUP
);
405 read_unlock(&skpair
->sk_callback_lock
);
407 sock_put(skpair
); /* It may now die */
408 unix_peer(sk
) = NULL
;
411 /* Try to flush out this socket. Throw out buffers at least */
413 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
414 if (state
==TCP_LISTEN
)
415 unix_release_sock(skb
->sk
, 1);
416 /* passed fds are erased in the kfree_skb hook */
427 /* ---- Socket is dead now and most probably destroyed ---- */
430 * Fixme: BSD difference: In BSD all sockets connected to use get
431 * ECONNRESET and we die on the spot. In Linux we behave
432 * like files and pipes do and wait for the last
435 * Can't we simply set sock->err?
437 * What the above comment does talk about? --ANK(980817)
440 if (atomic_read(&unix_tot_inflight
))
441 unix_gc(); /* Garbage collect fds */
446 static int unix_listen(struct socket
*sock
, int backlog
)
449 struct sock
*sk
= sock
->sk
;
450 struct unix_sock
*u
= unix_sk(sk
);
453 if (sock
->type
!=SOCK_STREAM
&& sock
->type
!=SOCK_SEQPACKET
)
454 goto out
; /* Only stream/seqpacket sockets accept */
457 goto out
; /* No listens on an unbound socket */
458 unix_state_wlock(sk
);
459 if (sk
->sk_state
!= TCP_CLOSE
&& sk
->sk_state
!= TCP_LISTEN
)
461 if (backlog
> sk
->sk_max_ack_backlog
)
462 wake_up_interruptible_all(&u
->peer_wait
);
463 sk
->sk_max_ack_backlog
= backlog
;
464 sk
->sk_state
= TCP_LISTEN
;
465 /* set credentials so connect can copy them */
466 sk
->sk_peercred
.pid
= current
->tgid
;
467 sk
->sk_peercred
.uid
= current
->euid
;
468 sk
->sk_peercred
.gid
= current
->egid
;
472 unix_state_wunlock(sk
);
477 static int unix_release(struct socket
*);
478 static int unix_bind(struct socket
*, struct sockaddr
*, int);
479 static int unix_stream_connect(struct socket
*, struct sockaddr
*,
480 int addr_len
, int flags
);
481 static int unix_socketpair(struct socket
*, struct socket
*);
482 static int unix_accept(struct socket
*, struct socket
*, int);
483 static int unix_getname(struct socket
*, struct sockaddr
*, int *, int);
484 static unsigned int unix_poll(struct file
*, struct socket
*, poll_table
*);
485 static int unix_ioctl(struct socket
*, unsigned int, unsigned long);
486 static int unix_shutdown(struct socket
*, int);
487 static int unix_stream_sendmsg(struct kiocb
*, struct socket
*,
488 struct msghdr
*, size_t);
489 static int unix_stream_recvmsg(struct kiocb
*, struct socket
*,
490 struct msghdr
*, size_t, int);
491 static int unix_dgram_sendmsg(struct kiocb
*, struct socket
*,
492 struct msghdr
*, size_t);
493 static int unix_dgram_recvmsg(struct kiocb
*, struct socket
*,
494 struct msghdr
*, size_t, int);
495 static int unix_dgram_connect(struct socket
*, struct sockaddr
*,
497 static int unix_seqpacket_sendmsg(struct kiocb
*, struct socket
*,
498 struct msghdr
*, size_t);
500 static const struct proto_ops unix_stream_ops
= {
502 .owner
= THIS_MODULE
,
503 .release
= unix_release
,
505 .connect
= unix_stream_connect
,
506 .socketpair
= unix_socketpair
,
507 .accept
= unix_accept
,
508 .getname
= unix_getname
,
511 .listen
= unix_listen
,
512 .shutdown
= unix_shutdown
,
513 .setsockopt
= sock_no_setsockopt
,
514 .getsockopt
= sock_no_getsockopt
,
515 .sendmsg
= unix_stream_sendmsg
,
516 .recvmsg
= unix_stream_recvmsg
,
517 .mmap
= sock_no_mmap
,
518 .sendpage
= sock_no_sendpage
,
521 static const struct proto_ops unix_dgram_ops
= {
523 .owner
= THIS_MODULE
,
524 .release
= unix_release
,
526 .connect
= unix_dgram_connect
,
527 .socketpair
= unix_socketpair
,
528 .accept
= sock_no_accept
,
529 .getname
= unix_getname
,
530 .poll
= datagram_poll
,
532 .listen
= sock_no_listen
,
533 .shutdown
= unix_shutdown
,
534 .setsockopt
= sock_no_setsockopt
,
535 .getsockopt
= sock_no_getsockopt
,
536 .sendmsg
= unix_dgram_sendmsg
,
537 .recvmsg
= unix_dgram_recvmsg
,
538 .mmap
= sock_no_mmap
,
539 .sendpage
= sock_no_sendpage
,
542 static const struct proto_ops unix_seqpacket_ops
= {
544 .owner
= THIS_MODULE
,
545 .release
= unix_release
,
547 .connect
= unix_stream_connect
,
548 .socketpair
= unix_socketpair
,
549 .accept
= unix_accept
,
550 .getname
= unix_getname
,
551 .poll
= datagram_poll
,
553 .listen
= unix_listen
,
554 .shutdown
= unix_shutdown
,
555 .setsockopt
= sock_no_setsockopt
,
556 .getsockopt
= sock_no_getsockopt
,
557 .sendmsg
= unix_seqpacket_sendmsg
,
558 .recvmsg
= unix_dgram_recvmsg
,
559 .mmap
= sock_no_mmap
,
560 .sendpage
= sock_no_sendpage
,
563 static struct proto unix_proto
= {
565 .owner
= THIS_MODULE
,
566 .obj_size
= sizeof(struct unix_sock
),
569 static struct sock
* unix_create1(struct socket
*sock
)
571 struct sock
*sk
= NULL
;
574 if (atomic_read(&unix_nr_socks
) >= 2*get_max_files())
577 sk
= sk_alloc(PF_UNIX
, GFP_KERNEL
, &unix_proto
, 1);
581 atomic_inc(&unix_nr_socks
);
583 sock_init_data(sock
,sk
);
585 sk
->sk_write_space
= unix_write_space
;
586 sk
->sk_max_ack_backlog
= sysctl_unix_max_dgram_qlen
;
587 sk
->sk_destruct
= unix_sock_destructor
;
591 spin_lock_init(&u
->lock
);
592 atomic_set(&u
->inflight
, sock
? 0 : -1);
593 mutex_init(&u
->readlock
); /* single task reading lock */
594 init_waitqueue_head(&u
->peer_wait
);
595 unix_insert_socket(unix_sockets_unbound
, sk
);
600 static int unix_create(struct socket
*sock
, int protocol
)
602 if (protocol
&& protocol
!= PF_UNIX
)
603 return -EPROTONOSUPPORT
;
605 sock
->state
= SS_UNCONNECTED
;
607 switch (sock
->type
) {
609 sock
->ops
= &unix_stream_ops
;
612 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
616 sock
->type
=SOCK_DGRAM
;
618 sock
->ops
= &unix_dgram_ops
;
621 sock
->ops
= &unix_seqpacket_ops
;
624 return -ESOCKTNOSUPPORT
;
627 return unix_create1(sock
) ? 0 : -ENOMEM
;
630 static int unix_release(struct socket
*sock
)
632 struct sock
*sk
= sock
->sk
;
639 return unix_release_sock (sk
, 0);
642 static int unix_autobind(struct socket
*sock
)
644 struct sock
*sk
= sock
->sk
;
645 struct unix_sock
*u
= unix_sk(sk
);
646 static u32 ordernum
= 1;
647 struct unix_address
* addr
;
650 mutex_lock(&u
->readlock
);
657 addr
= kmalloc(sizeof(*addr
) + sizeof(short) + 16, GFP_KERNEL
);
661 memset(addr
, 0, sizeof(*addr
) + sizeof(short) + 16);
662 addr
->name
->sun_family
= AF_UNIX
;
663 atomic_set(&addr
->refcnt
, 1);
666 addr
->len
= sprintf(addr
->name
->sun_path
+1, "%05x", ordernum
) + 1 + sizeof(short);
667 addr
->hash
= unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
, 0));
669 spin_lock(&unix_table_lock
);
670 ordernum
= (ordernum
+1)&0xFFFFF;
672 if (__unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
,
674 spin_unlock(&unix_table_lock
);
675 /* Sanity yield. It is unusual case, but yet... */
676 if (!(ordernum
&0xFF))
680 addr
->hash
^= sk
->sk_type
;
682 __unix_remove_socket(sk
);
684 __unix_insert_socket(&unix_socket_table
[addr
->hash
], sk
);
685 spin_unlock(&unix_table_lock
);
688 out
: mutex_unlock(&u
->readlock
);
692 static struct sock
*unix_find_other(struct sockaddr_un
*sunname
, int len
,
693 int type
, unsigned hash
, int *error
)
699 if (sunname
->sun_path
[0]) {
700 err
= path_lookup(sunname
->sun_path
, LOOKUP_FOLLOW
, &nd
);
703 err
= vfs_permission(&nd
, MAY_WRITE
);
708 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
710 u
=unix_find_socket_byinode(nd
.dentry
->d_inode
);
714 if (u
->sk_type
== type
)
715 touch_atime(nd
.mnt
, nd
.dentry
);
720 if (u
->sk_type
!= type
) {
726 u
=unix_find_socket_byname(sunname
, len
, type
, hash
);
728 struct dentry
*dentry
;
729 dentry
= unix_sk(u
)->dentry
;
731 touch_atime(unix_sk(u
)->mnt
, dentry
);
745 static int unix_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
747 struct sock
*sk
= sock
->sk
;
748 struct unix_sock
*u
= unix_sk(sk
);
749 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
750 struct dentry
* dentry
= NULL
;
754 struct unix_address
*addr
;
755 struct hlist_head
*list
;
758 if (sunaddr
->sun_family
!= AF_UNIX
)
761 if (addr_len
==sizeof(short)) {
762 err
= unix_autobind(sock
);
766 err
= unix_mkname(sunaddr
, addr_len
, &hash
);
771 mutex_lock(&u
->readlock
);
778 addr
= kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
);
782 memcpy(addr
->name
, sunaddr
, addr_len
);
783 addr
->len
= addr_len
;
784 addr
->hash
= hash
^ sk
->sk_type
;
785 atomic_set(&addr
->refcnt
, 1);
787 if (sunaddr
->sun_path
[0]) {
791 * Get the parent directory, calculate the hash for last
794 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
796 goto out_mknod_parent
;
798 dentry
= lookup_create(&nd
, 0);
799 err
= PTR_ERR(dentry
);
801 goto out_mknod_unlock
;
804 * All right, let's create it.
807 (SOCK_INODE(sock
)->i_mode
& ~current
->fs
->umask
);
808 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
811 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
815 addr
->hash
= UNIX_HASH_SIZE
;
818 spin_lock(&unix_table_lock
);
820 if (!sunaddr
->sun_path
[0]) {
822 if (__unix_find_socket_byname(sunaddr
, addr_len
,
823 sk
->sk_type
, hash
)) {
824 unix_release_addr(addr
);
828 list
= &unix_socket_table
[addr
->hash
];
830 list
= &unix_socket_table
[dentry
->d_inode
->i_ino
& (UNIX_HASH_SIZE
-1)];
831 u
->dentry
= nd
.dentry
;
836 __unix_remove_socket(sk
);
838 __unix_insert_socket(list
, sk
);
841 spin_unlock(&unix_table_lock
);
843 mutex_unlock(&u
->readlock
);
850 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
855 unix_release_addr(addr
);
859 static int unix_dgram_connect(struct socket
*sock
, struct sockaddr
*addr
,
862 struct sock
*sk
= sock
->sk
;
863 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
868 if (addr
->sa_family
!= AF_UNSPEC
) {
869 err
= unix_mkname(sunaddr
, alen
, &hash
);
874 if (test_bit(SOCK_PASSCRED
, &sock
->flags
) &&
875 !unix_sk(sk
)->addr
&& (err
= unix_autobind(sock
)) != 0)
878 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
);
882 unix_state_wlock(sk
);
885 if (!unix_may_send(sk
, other
))
888 err
= security_unix_may_send(sk
->sk_socket
, other
->sk_socket
);
894 * 1003.1g breaking connected state with AF_UNSPEC
897 unix_state_wlock(sk
);
901 * If it was connected, reconnect.
904 struct sock
*old_peer
= unix_peer(sk
);
906 unix_state_wunlock(sk
);
908 if (other
!= old_peer
)
909 unix_dgram_disconnected(sk
, old_peer
);
913 unix_state_wunlock(sk
);
918 unix_state_wunlock(sk
);
924 static long unix_wait_for_peer(struct sock
*other
, long timeo
)
926 struct unix_sock
*u
= unix_sk(other
);
930 prepare_to_wait_exclusive(&u
->peer_wait
, &wait
, TASK_INTERRUPTIBLE
);
932 sched
= !sock_flag(other
, SOCK_DEAD
) &&
933 !(other
->sk_shutdown
& RCV_SHUTDOWN
) &&
934 (skb_queue_len(&other
->sk_receive_queue
) >
935 other
->sk_max_ack_backlog
);
937 unix_state_runlock(other
);
940 timeo
= schedule_timeout(timeo
);
942 finish_wait(&u
->peer_wait
, &wait
);
946 static int unix_stream_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
947 int addr_len
, int flags
)
949 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
950 struct sock
*sk
= sock
->sk
;
951 struct unix_sock
*u
= unix_sk(sk
), *newu
, *otheru
;
952 struct sock
*newsk
= NULL
;
953 struct sock
*other
= NULL
;
954 struct sk_buff
*skb
= NULL
;
960 err
= unix_mkname(sunaddr
, addr_len
, &hash
);
965 if (test_bit(SOCK_PASSCRED
, &sock
->flags
)
966 && !u
->addr
&& (err
= unix_autobind(sock
)) != 0)
969 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
971 /* First of all allocate resources.
972 If we will make it after state is locked,
973 we will have to recheck all again in any case.
978 /* create new sock for complete connection */
979 newsk
= unix_create1(NULL
);
983 /* Allocate skb for sending to listening sock */
984 skb
= sock_wmalloc(newsk
, 1, 0, GFP_KERNEL
);
989 /* Find listening sock. */
990 other
= unix_find_other(sunaddr
, addr_len
, sk
->sk_type
, hash
, &err
);
994 /* Latch state of peer */
995 unix_state_rlock(other
);
997 /* Apparently VFS overslept socket death. Retry. */
998 if (sock_flag(other
, SOCK_DEAD
)) {
999 unix_state_runlock(other
);
1004 err
= -ECONNREFUSED
;
1005 if (other
->sk_state
!= TCP_LISTEN
)
1008 if (skb_queue_len(&other
->sk_receive_queue
) >
1009 other
->sk_max_ack_backlog
) {
1014 timeo
= unix_wait_for_peer(other
, timeo
);
1016 err
= sock_intr_errno(timeo
);
1017 if (signal_pending(current
))
1025 It is tricky place. We need to grab write lock and cannot
1026 drop lock on peer. It is dangerous because deadlock is
1027 possible. Connect to self case and simultaneous
1028 attempt to connect are eliminated by checking socket
1029 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1030 check this before attempt to grab lock.
1032 Well, and we have to recheck the state after socket locked.
1038 /* This is ok... continue with connect */
1040 case TCP_ESTABLISHED
:
1041 /* Socket is already connected */
1049 unix_state_wlock(sk
);
1051 if (sk
->sk_state
!= st
) {
1052 unix_state_wunlock(sk
);
1053 unix_state_runlock(other
);
1058 err
= security_unix_stream_connect(sock
, other
->sk_socket
, newsk
);
1060 unix_state_wunlock(sk
);
1064 /* The way is open! Fastly set all the necessary fields... */
1067 unix_peer(newsk
) = sk
;
1068 newsk
->sk_state
= TCP_ESTABLISHED
;
1069 newsk
->sk_type
= sk
->sk_type
;
1070 newsk
->sk_peercred
.pid
= current
->tgid
;
1071 newsk
->sk_peercred
.uid
= current
->euid
;
1072 newsk
->sk_peercred
.gid
= current
->egid
;
1073 newu
= unix_sk(newsk
);
1074 newsk
->sk_sleep
= &newu
->peer_wait
;
1075 otheru
= unix_sk(other
);
1077 /* copy address information from listening to new sock*/
1079 atomic_inc(&otheru
->addr
->refcnt
);
1080 newu
->addr
= otheru
->addr
;
1082 if (otheru
->dentry
) {
1083 newu
->dentry
= dget(otheru
->dentry
);
1084 newu
->mnt
= mntget(otheru
->mnt
);
1087 /* Set credentials */
1088 sk
->sk_peercred
= other
->sk_peercred
;
1090 sock
->state
= SS_CONNECTED
;
1091 sk
->sk_state
= TCP_ESTABLISHED
;
1094 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1095 unix_peer(sk
) = newsk
;
1097 unix_state_wunlock(sk
);
1099 /* take ten and and send info to listening sock */
1100 spin_lock(&other
->sk_receive_queue
.lock
);
1101 __skb_queue_tail(&other
->sk_receive_queue
, skb
);
1102 /* Undo artificially decreased inflight after embrion
1103 * is installed to listening socket. */
1104 atomic_inc(&newu
->inflight
);
1105 spin_unlock(&other
->sk_receive_queue
.lock
);
1106 unix_state_runlock(other
);
1107 other
->sk_data_ready(other
, 0);
1113 unix_state_runlock(other
);
1119 unix_release_sock(newsk
, 0);
1125 static int unix_socketpair(struct socket
*socka
, struct socket
*sockb
)
1127 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
;
1129 /* Join our sockets back to back */
1134 ska
->sk_peercred
.pid
= skb
->sk_peercred
.pid
= current
->tgid
;
1135 ska
->sk_peercred
.uid
= skb
->sk_peercred
.uid
= current
->euid
;
1136 ska
->sk_peercred
.gid
= skb
->sk_peercred
.gid
= current
->egid
;
1138 if (ska
->sk_type
!= SOCK_DGRAM
) {
1139 ska
->sk_state
= TCP_ESTABLISHED
;
1140 skb
->sk_state
= TCP_ESTABLISHED
;
1141 socka
->state
= SS_CONNECTED
;
1142 sockb
->state
= SS_CONNECTED
;
1147 static int unix_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1149 struct sock
*sk
= sock
->sk
;
1151 struct sk_buff
*skb
;
1155 if (sock
->type
!=SOCK_STREAM
&& sock
->type
!=SOCK_SEQPACKET
)
1159 if (sk
->sk_state
!= TCP_LISTEN
)
1162 /* If socket state is TCP_LISTEN it cannot change (for now...),
1163 * so that no locks are necessary.
1166 skb
= skb_recv_datagram(sk
, 0, flags
&O_NONBLOCK
, &err
);
1168 /* This means receive shutdown. */
1175 skb_free_datagram(sk
, skb
);
1176 wake_up_interruptible(&unix_sk(sk
)->peer_wait
);
1178 /* attach accepted sock to socket */
1179 unix_state_wlock(tsk
);
1180 newsock
->state
= SS_CONNECTED
;
1181 sock_graft(tsk
, newsock
);
1182 unix_state_wunlock(tsk
);
1190 static int unix_getname(struct socket
*sock
, struct sockaddr
*uaddr
, int *uaddr_len
, int peer
)
1192 struct sock
*sk
= sock
->sk
;
1193 struct unix_sock
*u
;
1194 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
1198 sk
= unix_peer_get(sk
);
1209 unix_state_rlock(sk
);
1211 sunaddr
->sun_family
= AF_UNIX
;
1212 sunaddr
->sun_path
[0] = 0;
1213 *uaddr_len
= sizeof(short);
1215 struct unix_address
*addr
= u
->addr
;
1217 *uaddr_len
= addr
->len
;
1218 memcpy(sunaddr
, addr
->name
, *uaddr_len
);
1220 unix_state_runlock(sk
);
1226 static void unix_detach_fds(struct scm_cookie
*scm
, struct sk_buff
*skb
)
1230 scm
->fp
= UNIXCB(skb
).fp
;
1231 skb
->destructor
= sock_wfree
;
1232 UNIXCB(skb
).fp
= NULL
;
1234 for (i
=scm
->fp
->count
-1; i
>=0; i
--)
1235 unix_notinflight(scm
->fp
->fp
[i
]);
1238 static void unix_destruct_fds(struct sk_buff
*skb
)
1240 struct scm_cookie scm
;
1241 memset(&scm
, 0, sizeof(scm
));
1242 unix_detach_fds(&scm
, skb
);
1244 /* Alas, it calls VFS */
1245 /* So fscking what? fput() had been SMP-safe since the last Summer */
1250 static void unix_attach_fds(struct scm_cookie
*scm
, struct sk_buff
*skb
)
1253 for (i
=scm
->fp
->count
-1; i
>=0; i
--)
1254 unix_inflight(scm
->fp
->fp
[i
]);
1255 UNIXCB(skb
).fp
= scm
->fp
;
1256 skb
->destructor
= unix_destruct_fds
;
1261 * Send AF_UNIX data.
1264 static int unix_dgram_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1265 struct msghdr
*msg
, size_t len
)
1267 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
1268 struct sock
*sk
= sock
->sk
;
1269 struct unix_sock
*u
= unix_sk(sk
);
1270 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1271 struct sock
*other
= NULL
;
1272 int namelen
= 0; /* fake GCC */
1275 struct sk_buff
*skb
;
1277 struct scm_cookie tmp_scm
;
1279 if (NULL
== siocb
->scm
)
1280 siocb
->scm
= &tmp_scm
;
1281 err
= scm_send(sock
, msg
, siocb
->scm
);
1286 if (msg
->msg_flags
&MSG_OOB
)
1289 if (msg
->msg_namelen
) {
1290 err
= unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
);
1297 other
= unix_peer_get(sk
);
1302 if (test_bit(SOCK_PASSCRED
, &sock
->flags
)
1303 && !u
->addr
&& (err
= unix_autobind(sock
)) != 0)
1307 if (len
> sk
->sk_sndbuf
- 32)
1310 skb
= sock_alloc_send_skb(sk
, len
, msg
->msg_flags
&MSG_DONTWAIT
, &err
);
1314 memcpy(UNIXCREDS(skb
), &siocb
->scm
->creds
, sizeof(struct ucred
));
1316 unix_attach_fds(siocb
->scm
, skb
);
1318 unix_get_peersec_dgram(skb
);
1320 skb
->h
.raw
= skb
->data
;
1321 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
1325 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1330 if (sunaddr
== NULL
)
1333 other
= unix_find_other(sunaddr
, namelen
, sk
->sk_type
,
1339 unix_state_rlock(other
);
1341 if (!unix_may_send(sk
, other
))
1344 if (sock_flag(other
, SOCK_DEAD
)) {
1346 * Check with 1003.1g - what should
1349 unix_state_runlock(other
);
1353 unix_state_wlock(sk
);
1354 if (unix_peer(sk
) == other
) {
1356 unix_state_wunlock(sk
);
1358 unix_dgram_disconnected(sk
, other
);
1360 err
= -ECONNREFUSED
;
1362 unix_state_wunlock(sk
);
1372 if (other
->sk_shutdown
& RCV_SHUTDOWN
)
1375 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
1376 err
= security_unix_may_send(sk
->sk_socket
, other
->sk_socket
);
1381 if (unix_peer(other
) != sk
&&
1382 (skb_queue_len(&other
->sk_receive_queue
) >
1383 other
->sk_max_ack_backlog
)) {
1389 timeo
= unix_wait_for_peer(other
, timeo
);
1391 err
= sock_intr_errno(timeo
);
1392 if (signal_pending(current
))
1398 skb_queue_tail(&other
->sk_receive_queue
, skb
);
1399 unix_state_runlock(other
);
1400 other
->sk_data_ready(other
, len
);
1402 scm_destroy(siocb
->scm
);
1406 unix_state_runlock(other
);
1412 scm_destroy(siocb
->scm
);
1417 static int unix_stream_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1418 struct msghdr
*msg
, size_t len
)
1420 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
1421 struct sock
*sk
= sock
->sk
;
1422 struct sock
*other
= NULL
;
1423 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1425 struct sk_buff
*skb
;
1427 struct scm_cookie tmp_scm
;
1429 if (NULL
== siocb
->scm
)
1430 siocb
->scm
= &tmp_scm
;
1431 err
= scm_send(sock
, msg
, siocb
->scm
);
1436 if (msg
->msg_flags
&MSG_OOB
)
1439 if (msg
->msg_namelen
) {
1440 err
= sk
->sk_state
== TCP_ESTABLISHED
? -EISCONN
: -EOPNOTSUPP
;
1445 other
= unix_peer(sk
);
1450 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
1456 * Optimisation for the fact that under 0.01% of X
1457 * messages typically need breaking up.
1462 /* Keep two messages in the pipe so it schedules better */
1463 if (size
> ((sk
->sk_sndbuf
>> 1) - 64))
1464 size
= (sk
->sk_sndbuf
>> 1) - 64;
1466 if (size
> SKB_MAX_ALLOC
)
1467 size
= SKB_MAX_ALLOC
;
1473 skb
=sock_alloc_send_skb(sk
,size
,msg
->msg_flags
&MSG_DONTWAIT
, &err
);
1479 * If you pass two values to the sock_alloc_send_skb
1480 * it tries to grab the large buffer with GFP_NOFS
1481 * (which can fail easily), and if it fails grab the
1482 * fallback size buffer which is under a page and will
1485 size
= min_t(int, size
, skb_tailroom(skb
));
1487 memcpy(UNIXCREDS(skb
), &siocb
->scm
->creds
, sizeof(struct ucred
));
1489 unix_attach_fds(siocb
->scm
, skb
);
1491 if ((err
= memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) != 0) {
1496 unix_state_rlock(other
);
1498 if (sock_flag(other
, SOCK_DEAD
) ||
1499 (other
->sk_shutdown
& RCV_SHUTDOWN
))
1502 skb_queue_tail(&other
->sk_receive_queue
, skb
);
1503 unix_state_runlock(other
);
1504 other
->sk_data_ready(other
, size
);
1508 scm_destroy(siocb
->scm
);
1514 unix_state_runlock(other
);
1517 if (sent
==0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
1518 send_sig(SIGPIPE
,current
,0);
1521 scm_destroy(siocb
->scm
);
1523 return sent
? : err
;
1526 static int unix_seqpacket_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1527 struct msghdr
*msg
, size_t len
)
1530 struct sock
*sk
= sock
->sk
;
1532 err
= sock_error(sk
);
1536 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1539 if (msg
->msg_namelen
)
1540 msg
->msg_namelen
= 0;
1542 return unix_dgram_sendmsg(kiocb
, sock
, msg
, len
);
1545 static void unix_copy_addr(struct msghdr
*msg
, struct sock
*sk
)
1547 struct unix_sock
*u
= unix_sk(sk
);
1549 msg
->msg_namelen
= 0;
1551 msg
->msg_namelen
= u
->addr
->len
;
1552 memcpy(msg
->msg_name
, u
->addr
->name
, u
->addr
->len
);
1556 static int unix_dgram_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1557 struct msghdr
*msg
, size_t size
,
1560 struct sock_iocb
*siocb
= kiocb_to_siocb(iocb
);
1561 struct scm_cookie tmp_scm
;
1562 struct sock
*sk
= sock
->sk
;
1563 struct unix_sock
*u
= unix_sk(sk
);
1564 int noblock
= flags
& MSG_DONTWAIT
;
1565 struct sk_buff
*skb
;
1572 msg
->msg_namelen
= 0;
1574 mutex_lock(&u
->readlock
);
1576 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1580 wake_up_interruptible(&u
->peer_wait
);
1583 unix_copy_addr(msg
, skb
->sk
);
1585 if (size
> skb
->len
)
1587 else if (size
< skb
->len
)
1588 msg
->msg_flags
|= MSG_TRUNC
;
1590 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, size
);
1595 siocb
->scm
= &tmp_scm
;
1596 memset(&tmp_scm
, 0, sizeof(tmp_scm
));
1598 siocb
->scm
->creds
= *UNIXCREDS(skb
);
1599 unix_set_secdata(siocb
->scm
, skb
);
1601 if (!(flags
& MSG_PEEK
))
1604 unix_detach_fds(siocb
->scm
, skb
);
1608 /* It is questionable: on PEEK we could:
1609 - do not return fds - good, but too simple 8)
1610 - return fds, and do not return them on read (old strategy,
1612 - clone fds (I chose it for now, it is the most universal
1615 POSIX 1003.1g does not actually define this clearly
1616 at all. POSIX 1003.1g doesn't define a lot of things
1621 siocb
->scm
->fp
= scm_fp_dup(UNIXCB(skb
).fp
);
1625 scm_recv(sock
, msg
, siocb
->scm
, flags
);
1628 skb_free_datagram(sk
,skb
);
1630 mutex_unlock(&u
->readlock
);
1636 * Sleep until data has arrive. But check for races..
1639 static long unix_stream_data_wait(struct sock
* sk
, long timeo
)
1643 unix_state_rlock(sk
);
1646 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
1648 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1650 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
1651 signal_pending(current
) ||
1655 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
1656 unix_state_runlock(sk
);
1657 timeo
= schedule_timeout(timeo
);
1658 unix_state_rlock(sk
);
1659 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
1662 finish_wait(sk
->sk_sleep
, &wait
);
1663 unix_state_runlock(sk
);
1669 static int unix_stream_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1670 struct msghdr
*msg
, size_t size
,
1673 struct sock_iocb
*siocb
= kiocb_to_siocb(iocb
);
1674 struct scm_cookie tmp_scm
;
1675 struct sock
*sk
= sock
->sk
;
1676 struct unix_sock
*u
= unix_sk(sk
);
1677 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1679 int check_creds
= 0;
1685 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1692 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
1693 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
1695 msg
->msg_namelen
= 0;
1697 /* Lock the socket to prevent queue disordering
1698 * while sleeps in memcpy_tomsg
1702 siocb
->scm
= &tmp_scm
;
1703 memset(&tmp_scm
, 0, sizeof(tmp_scm
));
1706 mutex_lock(&u
->readlock
);
1711 struct sk_buff
*skb
;
1713 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1716 if (copied
>= target
)
1720 * POSIX 1003.1g mandates this order.
1723 if ((err
= sock_error(sk
)) != 0)
1725 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1730 mutex_unlock(&u
->readlock
);
1732 timeo
= unix_stream_data_wait(sk
, timeo
);
1734 if (signal_pending(current
)) {
1735 err
= sock_intr_errno(timeo
);
1738 mutex_lock(&u
->readlock
);
1743 /* Never glue messages from different writers */
1744 if (memcmp(UNIXCREDS(skb
), &siocb
->scm
->creds
, sizeof(siocb
->scm
->creds
)) != 0) {
1745 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1749 /* Copy credentials */
1750 siocb
->scm
->creds
= *UNIXCREDS(skb
);
1754 /* Copy address just once */
1757 unix_copy_addr(msg
, skb
->sk
);
1761 chunk
= min_t(unsigned int, skb
->len
, size
);
1762 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
1763 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1771 /* Mark read part of skb as used */
1772 if (!(flags
& MSG_PEEK
))
1774 skb_pull(skb
, chunk
);
1777 unix_detach_fds(siocb
->scm
, skb
);
1779 /* put the skb back if we didn't use it up.. */
1782 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1793 /* It is questionable, see note in unix_dgram_recvmsg.
1796 siocb
->scm
->fp
= scm_fp_dup(UNIXCB(skb
).fp
);
1798 /* put message back and return */
1799 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1804 mutex_unlock(&u
->readlock
);
1805 scm_recv(sock
, msg
, siocb
->scm
, flags
);
1807 return copied
? : err
;
1810 static int unix_shutdown(struct socket
*sock
, int mode
)
1812 struct sock
*sk
= sock
->sk
;
1815 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1818 unix_state_wlock(sk
);
1819 sk
->sk_shutdown
|= mode
;
1820 other
=unix_peer(sk
);
1823 unix_state_wunlock(sk
);
1824 sk
->sk_state_change(sk
);
1827 (sk
->sk_type
== SOCK_STREAM
|| sk
->sk_type
== SOCK_SEQPACKET
)) {
1831 if (mode
&RCV_SHUTDOWN
)
1832 peer_mode
|= SEND_SHUTDOWN
;
1833 if (mode
&SEND_SHUTDOWN
)
1834 peer_mode
|= RCV_SHUTDOWN
;
1835 unix_state_wlock(other
);
1836 other
->sk_shutdown
|= peer_mode
;
1837 unix_state_wunlock(other
);
1838 other
->sk_state_change(other
);
1839 read_lock(&other
->sk_callback_lock
);
1840 if (peer_mode
== SHUTDOWN_MASK
)
1841 sk_wake_async(other
,1,POLL_HUP
);
1842 else if (peer_mode
& RCV_SHUTDOWN
)
1843 sk_wake_async(other
,1,POLL_IN
);
1844 read_unlock(&other
->sk_callback_lock
);
1852 static int unix_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1854 struct sock
*sk
= sock
->sk
;
1861 amount
= atomic_read(&sk
->sk_wmem_alloc
);
1862 err
= put_user(amount
, (int __user
*)arg
);
1866 struct sk_buff
*skb
;
1868 if (sk
->sk_state
== TCP_LISTEN
) {
1873 spin_lock(&sk
->sk_receive_queue
.lock
);
1874 if (sk
->sk_type
== SOCK_STREAM
||
1875 sk
->sk_type
== SOCK_SEQPACKET
) {
1876 skb_queue_walk(&sk
->sk_receive_queue
, skb
)
1879 skb
= skb_peek(&sk
->sk_receive_queue
);
1883 spin_unlock(&sk
->sk_receive_queue
.lock
);
1884 err
= put_user(amount
, (int __user
*)arg
);
1895 static unsigned int unix_poll(struct file
* file
, struct socket
*sock
, poll_table
*wait
)
1897 struct sock
*sk
= sock
->sk
;
1900 poll_wait(file
, sk
->sk_sleep
, wait
);
1903 /* exceptional events? */
1906 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
1908 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1912 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1913 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
1914 mask
|= POLLIN
| POLLRDNORM
;
1916 /* Connection-based need to check for termination and startup */
1917 if ((sk
->sk_type
== SOCK_STREAM
|| sk
->sk_type
== SOCK_SEQPACKET
) && sk
->sk_state
== TCP_CLOSE
)
1921 * we set writable also when the other side has shut down the
1922 * connection. This prevents stuck sockets.
1924 if (unix_writable(sk
))
1925 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1931 #ifdef CONFIG_PROC_FS
1932 static struct sock
*unix_seq_idx(int *iter
, loff_t pos
)
1937 for (s
= first_unix_socket(iter
); s
; s
= next_unix_socket(iter
, s
)) {
1946 static void *unix_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1948 spin_lock(&unix_table_lock
);
1949 return *pos
? unix_seq_idx(seq
->private, *pos
- 1) : ((void *) 1);
1952 static void *unix_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1957 return first_unix_socket(seq
->private);
1958 return next_unix_socket(seq
->private, v
);
1961 static void unix_seq_stop(struct seq_file
*seq
, void *v
)
1963 spin_unlock(&unix_table_lock
);
1966 static int unix_seq_show(struct seq_file
*seq
, void *v
)
1970 seq_puts(seq
, "Num RefCount Protocol Flags Type St "
1974 struct unix_sock
*u
= unix_sk(s
);
1975 unix_state_rlock(s
);
1977 seq_printf(seq
, "%p: %08X %08X %08X %04X %02X %5lu",
1979 atomic_read(&s
->sk_refcnt
),
1981 s
->sk_state
== TCP_LISTEN
? __SO_ACCEPTCON
: 0,
1984 (s
->sk_state
== TCP_ESTABLISHED
? SS_CONNECTED
: SS_UNCONNECTED
) :
1985 (s
->sk_state
== TCP_ESTABLISHED
? SS_CONNECTING
: SS_DISCONNECTING
),
1993 len
= u
->addr
->len
- sizeof(short);
1994 if (!UNIX_ABSTRACT(s
))
2000 for ( ; i
< len
; i
++)
2001 seq_putc(seq
, u
->addr
->name
->sun_path
[i
]);
2003 unix_state_runlock(s
);
2004 seq_putc(seq
, '\n');
2010 static struct seq_operations unix_seq_ops
= {
2011 .start
= unix_seq_start
,
2012 .next
= unix_seq_next
,
2013 .stop
= unix_seq_stop
,
2014 .show
= unix_seq_show
,
2018 static int unix_seq_open(struct inode
*inode
, struct file
*file
)
2020 struct seq_file
*seq
;
2022 int *iter
= kmalloc(sizeof(int), GFP_KERNEL
);
2027 rc
= seq_open(file
, &unix_seq_ops
);
2031 seq
= file
->private_data
;
2032 seq
->private = iter
;
2041 static struct file_operations unix_seq_fops
= {
2042 .owner
= THIS_MODULE
,
2043 .open
= unix_seq_open
,
2045 .llseek
= seq_lseek
,
2046 .release
= seq_release_private
,
2051 static struct net_proto_family unix_family_ops
= {
2053 .create
= unix_create
,
2054 .owner
= THIS_MODULE
,
2057 static int __init
af_unix_init(void)
2060 struct sk_buff
*dummy_skb
;
2062 if (sizeof(struct unix_skb_parms
) > sizeof(dummy_skb
->cb
)) {
2063 printk(KERN_CRIT
"%s: panic\n", __FUNCTION__
);
2067 rc
= proto_register(&unix_proto
, 1);
2069 printk(KERN_CRIT
"%s: Cannot create unix_sock SLAB cache!\n",
2074 sock_register(&unix_family_ops
);
2075 #ifdef CONFIG_PROC_FS
2076 proc_net_fops_create("unix", 0, &unix_seq_fops
);
2078 unix_sysctl_register();
2083 static void __exit
af_unix_exit(void)
2085 sock_unregister(PF_UNIX
);
2086 unix_sysctl_unregister();
2087 proc_net_remove("unix");
2088 proto_unregister(&unix_proto
);
2091 module_init(af_unix_init
);
2092 module_exit(af_unix_exit
);
2094 MODULE_LICENSE("GPL");
2095 MODULE_ALIAS_NETPROTO(PF_UNIX
);