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/major.h>
89 #include <linux/signal.h>
90 #include <linux/sched.h>
91 #include <linux/errno.h>
92 #include <linux/string.h>
93 #include <linux/stat.h>
94 #include <linux/dcache.h>
95 #include <linux/namei.h>
96 #include <linux/socket.h>
98 #include <linux/fcntl.h>
99 #include <linux/termios.h>
100 #include <linux/sockios.h>
101 #include <linux/net.h>
102 #include <linux/in.h>
103 #include <linux/fs.h>
104 #include <linux/slab.h>
105 #include <asm/uaccess.h>
106 #include <linux/skbuff.h>
107 #include <linux/netdevice.h>
108 #include <net/sock.h>
109 #include <linux/tcp.h>
110 #include <net/af_unix.h>
111 #include <linux/proc_fs.h>
112 #include <linux/seq_file.h>
114 #include <linux/init.h>
115 #include <linux/poll.h>
116 #include <linux/smp_lock.h>
117 #include <linux/rtnetlink.h>
118 #include <linux/mount.h>
119 #include <net/checksum.h>
120 #include <linux/security.h>
122 int sysctl_unix_max_dgram_qlen
= 10;
124 struct hlist_head unix_socket_table
[UNIX_HASH_SIZE
+ 1];
125 DEFINE_RWLOCK(unix_table_lock
);
126 static atomic_t unix_nr_socks
= ATOMIC_INIT(0);
128 #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
130 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
133 * SMP locking strategy:
134 * hash table is protected with rwlock unix_table_lock
135 * each socket state is protected by separate rwlock.
138 static inline unsigned unix_hash_fold(unsigned hash
)
142 return hash
&(UNIX_HASH_SIZE
-1);
145 #define unix_peer(sk) (unix_sk(sk)->peer)
147 static inline int unix_our_peer(struct sock
*sk
, struct sock
*osk
)
149 return unix_peer(osk
) == sk
;
152 static inline int unix_may_send(struct sock
*sk
, struct sock
*osk
)
154 return (unix_peer(osk
) == NULL
|| unix_our_peer(sk
, osk
));
157 static struct sock
*unix_peer_get(struct sock
*s
)
165 unix_state_runlock(s
);
169 static inline void unix_release_addr(struct unix_address
*addr
)
171 if (atomic_dec_and_test(&addr
->refcnt
))
176 * Check unix socket name:
177 * - should be not zero length.
178 * - if started by not zero, should be NULL terminated (FS object)
179 * - if started by zero, it is abstract name.
182 static int unix_mkname(struct sockaddr_un
* sunaddr
, int len
, unsigned *hashp
)
184 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
186 if (!sunaddr
|| sunaddr
->sun_family
!= AF_UNIX
)
188 if (sunaddr
->sun_path
[0]) {
190 * This may look like an off by one error but it is a bit more
191 * subtle. 108 is the longest valid AF_UNIX path for a binding.
192 * sun_path[108] doesnt as such exist. However in kernel space
193 * we are guaranteed that it is a valid memory location in our
194 * kernel address buffer.
196 ((char *)sunaddr
)[len
]=0;
197 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
201 *hashp
= unix_hash_fold(csum_partial((char*)sunaddr
, len
, 0));
205 static void __unix_remove_socket(struct sock
*sk
)
207 sk_del_node_init(sk
);
210 static void __unix_insert_socket(struct hlist_head
*list
, struct sock
*sk
)
212 BUG_TRAP(sk_unhashed(sk
));
213 sk_add_node(sk
, list
);
216 static inline void unix_remove_socket(struct sock
*sk
)
218 write_lock(&unix_table_lock
);
219 __unix_remove_socket(sk
);
220 write_unlock(&unix_table_lock
);
223 static inline void unix_insert_socket(struct hlist_head
*list
, struct sock
*sk
)
225 write_lock(&unix_table_lock
);
226 __unix_insert_socket(list
, sk
);
227 write_unlock(&unix_table_lock
);
230 static struct sock
*__unix_find_socket_byname(struct sockaddr_un
*sunname
,
231 int len
, int type
, unsigned hash
)
234 struct hlist_node
*node
;
236 sk_for_each(s
, node
, &unix_socket_table
[hash
^ type
]) {
237 struct unix_sock
*u
= unix_sk(s
);
239 if (u
->addr
->len
== len
&&
240 !memcmp(u
->addr
->name
, sunname
, len
))
248 static inline struct sock
*unix_find_socket_byname(struct sockaddr_un
*sunname
,
254 read_lock(&unix_table_lock
);
255 s
= __unix_find_socket_byname(sunname
, len
, type
, hash
);
258 read_unlock(&unix_table_lock
);
262 static struct sock
*unix_find_socket_byinode(struct inode
*i
)
265 struct hlist_node
*node
;
267 read_lock(&unix_table_lock
);
269 &unix_socket_table
[i
->i_ino
& (UNIX_HASH_SIZE
- 1)]) {
270 struct dentry
*dentry
= unix_sk(s
)->dentry
;
272 if(dentry
&& dentry
->d_inode
== i
)
280 read_unlock(&unix_table_lock
);
284 static inline int unix_writable(struct sock
*sk
)
286 return (atomic_read(&sk
->sk_wmem_alloc
) << 2) <= sk
->sk_sndbuf
;
289 static void unix_write_space(struct sock
*sk
)
291 read_lock(&sk
->sk_callback_lock
);
292 if (unix_writable(sk
)) {
293 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
294 wake_up_interruptible(sk
->sk_sleep
);
295 sk_wake_async(sk
, 2, POLL_OUT
);
297 read_unlock(&sk
->sk_callback_lock
);
300 /* When dgram socket disconnects (or changes its peer), we clear its receive
301 * queue of packets arrived from previous peer. First, it allows to do
302 * flow control based only on wmem_alloc; second, sk connected to peer
303 * may receive messages only from that peer. */
304 static void unix_dgram_disconnected(struct sock
*sk
, struct sock
*other
)
306 if (skb_queue_len(&sk
->sk_receive_queue
)) {
307 skb_queue_purge(&sk
->sk_receive_queue
);
308 wake_up_interruptible_all(&unix_sk(sk
)->peer_wait
);
310 /* If one link of bidirectional dgram pipe is disconnected,
311 * we signal error. Messages are lost. Do not make this,
312 * when peer was not connected to us.
314 if (!sock_flag(other
, SOCK_DEAD
) && unix_peer(other
) == sk
) {
315 other
->sk_err
= ECONNRESET
;
316 other
->sk_error_report(other
);
321 static void unix_sock_destructor(struct sock
*sk
)
323 struct unix_sock
*u
= unix_sk(sk
);
325 skb_queue_purge(&sk
->sk_receive_queue
);
327 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
328 BUG_TRAP(sk_unhashed(sk
));
329 BUG_TRAP(!sk
->sk_socket
);
330 if (!sock_flag(sk
, SOCK_DEAD
)) {
331 printk("Attempt to release alive unix socket: %p\n", sk
);
336 unix_release_addr(u
->addr
);
338 atomic_dec(&unix_nr_socks
);
339 #ifdef UNIX_REFCNT_DEBUG
340 printk(KERN_DEBUG
"UNIX %p is destroyed, %d are still alive.\n", sk
, atomic_read(&unix_nr_socks
));
344 static int unix_release_sock (struct sock
*sk
, int embrion
)
346 struct unix_sock
*u
= unix_sk(sk
);
347 struct dentry
*dentry
;
348 struct vfsmount
*mnt
;
353 unix_remove_socket(sk
);
356 unix_state_wlock(sk
);
358 sk
->sk_shutdown
= SHUTDOWN_MASK
;
363 state
= sk
->sk_state
;
364 sk
->sk_state
= TCP_CLOSE
;
365 unix_state_wunlock(sk
);
367 wake_up_interruptible_all(&u
->peer_wait
);
369 skpair
=unix_peer(sk
);
372 if (sk
->sk_type
== SOCK_STREAM
|| sk
->sk_type
== SOCK_SEQPACKET
) {
373 unix_state_wlock(skpair
);
375 skpair
->sk_shutdown
= SHUTDOWN_MASK
;
376 if (!skb_queue_empty(&sk
->sk_receive_queue
) || embrion
)
377 skpair
->sk_err
= ECONNRESET
;
378 unix_state_wunlock(skpair
);
379 skpair
->sk_state_change(skpair
);
380 read_lock(&skpair
->sk_callback_lock
);
381 sk_wake_async(skpair
,1,POLL_HUP
);
382 read_unlock(&skpair
->sk_callback_lock
);
384 sock_put(skpair
); /* It may now die */
385 unix_peer(sk
) = NULL
;
388 /* Try to flush out this socket. Throw out buffers at least */
390 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
391 if (state
==TCP_LISTEN
)
392 unix_release_sock(skb
->sk
, 1);
393 /* passed fds are erased in the kfree_skb hook */
404 /* ---- Socket is dead now and most probably destroyed ---- */
407 * Fixme: BSD difference: In BSD all sockets connected to use get
408 * ECONNRESET and we die on the spot. In Linux we behave
409 * like files and pipes do and wait for the last
412 * Can't we simply set sock->err?
414 * What the above comment does talk about? --ANK(980817)
417 if (atomic_read(&unix_tot_inflight
))
418 unix_gc(); /* Garbage collect fds */
423 static int unix_listen(struct socket
*sock
, int backlog
)
426 struct sock
*sk
= sock
->sk
;
427 struct unix_sock
*u
= unix_sk(sk
);
430 if (sock
->type
!=SOCK_STREAM
&& sock
->type
!=SOCK_SEQPACKET
)
431 goto out
; /* Only stream/seqpacket sockets accept */
434 goto out
; /* No listens on an unbound socket */
435 unix_state_wlock(sk
);
436 if (sk
->sk_state
!= TCP_CLOSE
&& sk
->sk_state
!= TCP_LISTEN
)
438 if (backlog
> sk
->sk_max_ack_backlog
)
439 wake_up_interruptible_all(&u
->peer_wait
);
440 sk
->sk_max_ack_backlog
= backlog
;
441 sk
->sk_state
= TCP_LISTEN
;
442 /* set credentials so connect can copy them */
443 sk
->sk_peercred
.pid
= current
->tgid
;
444 sk
->sk_peercred
.uid
= current
->euid
;
445 sk
->sk_peercred
.gid
= current
->egid
;
449 unix_state_wunlock(sk
);
454 static int unix_release(struct socket
*);
455 static int unix_bind(struct socket
*, struct sockaddr
*, int);
456 static int unix_stream_connect(struct socket
*, struct sockaddr
*,
457 int addr_len
, int flags
);
458 static int unix_socketpair(struct socket
*, struct socket
*);
459 static int unix_accept(struct socket
*, struct socket
*, int);
460 static int unix_getname(struct socket
*, struct sockaddr
*, int *, int);
461 static unsigned int unix_poll(struct file
*, struct socket
*, poll_table
*);
462 static int unix_ioctl(struct socket
*, unsigned int, unsigned long);
463 static int unix_shutdown(struct socket
*, int);
464 static int unix_stream_sendmsg(struct kiocb
*, struct socket
*,
465 struct msghdr
*, size_t);
466 static int unix_stream_recvmsg(struct kiocb
*, struct socket
*,
467 struct msghdr
*, size_t, int);
468 static int unix_dgram_sendmsg(struct kiocb
*, struct socket
*,
469 struct msghdr
*, size_t);
470 static int unix_dgram_recvmsg(struct kiocb
*, struct socket
*,
471 struct msghdr
*, size_t, int);
472 static int unix_dgram_connect(struct socket
*, struct sockaddr
*,
474 static int unix_seqpacket_sendmsg(struct kiocb
*, struct socket
*,
475 struct msghdr
*, size_t);
477 static struct proto_ops unix_stream_ops
= {
479 .owner
= THIS_MODULE
,
480 .release
= unix_release
,
482 .connect
= unix_stream_connect
,
483 .socketpair
= unix_socketpair
,
484 .accept
= unix_accept
,
485 .getname
= unix_getname
,
488 .listen
= unix_listen
,
489 .shutdown
= unix_shutdown
,
490 .setsockopt
= sock_no_setsockopt
,
491 .getsockopt
= sock_no_getsockopt
,
492 .sendmsg
= unix_stream_sendmsg
,
493 .recvmsg
= unix_stream_recvmsg
,
494 .mmap
= sock_no_mmap
,
495 .sendpage
= sock_no_sendpage
,
498 static struct proto_ops unix_dgram_ops
= {
500 .owner
= THIS_MODULE
,
501 .release
= unix_release
,
503 .connect
= unix_dgram_connect
,
504 .socketpair
= unix_socketpair
,
505 .accept
= sock_no_accept
,
506 .getname
= unix_getname
,
507 .poll
= datagram_poll
,
509 .listen
= sock_no_listen
,
510 .shutdown
= unix_shutdown
,
511 .setsockopt
= sock_no_setsockopt
,
512 .getsockopt
= sock_no_getsockopt
,
513 .sendmsg
= unix_dgram_sendmsg
,
514 .recvmsg
= unix_dgram_recvmsg
,
515 .mmap
= sock_no_mmap
,
516 .sendpage
= sock_no_sendpage
,
519 static struct proto_ops unix_seqpacket_ops
= {
521 .owner
= THIS_MODULE
,
522 .release
= unix_release
,
524 .connect
= unix_stream_connect
,
525 .socketpair
= unix_socketpair
,
526 .accept
= unix_accept
,
527 .getname
= unix_getname
,
528 .poll
= datagram_poll
,
530 .listen
= unix_listen
,
531 .shutdown
= unix_shutdown
,
532 .setsockopt
= sock_no_setsockopt
,
533 .getsockopt
= sock_no_getsockopt
,
534 .sendmsg
= unix_seqpacket_sendmsg
,
535 .recvmsg
= unix_dgram_recvmsg
,
536 .mmap
= sock_no_mmap
,
537 .sendpage
= sock_no_sendpage
,
540 static struct proto unix_proto
= {
542 .owner
= THIS_MODULE
,
543 .obj_size
= sizeof(struct unix_sock
),
546 static struct sock
* unix_create1(struct socket
*sock
)
548 struct sock
*sk
= NULL
;
551 if (atomic_read(&unix_nr_socks
) >= 2*files_stat
.max_files
)
554 sk
= sk_alloc(PF_UNIX
, GFP_KERNEL
, &unix_proto
, 1);
558 atomic_inc(&unix_nr_socks
);
560 sock_init_data(sock
,sk
);
562 sk
->sk_write_space
= unix_write_space
;
563 sk
->sk_max_ack_backlog
= sysctl_unix_max_dgram_qlen
;
564 sk
->sk_destruct
= unix_sock_destructor
;
568 rwlock_init(&u
->lock
);
569 atomic_set(&u
->inflight
, sock
? 0 : -1);
570 init_MUTEX(&u
->readsem
); /* single task reading lock */
571 init_waitqueue_head(&u
->peer_wait
);
572 unix_insert_socket(unix_sockets_unbound
, sk
);
577 static int unix_create(struct socket
*sock
, int protocol
)
579 if (protocol
&& protocol
!= PF_UNIX
)
580 return -EPROTONOSUPPORT
;
582 sock
->state
= SS_UNCONNECTED
;
584 switch (sock
->type
) {
586 sock
->ops
= &unix_stream_ops
;
589 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
593 sock
->type
=SOCK_DGRAM
;
595 sock
->ops
= &unix_dgram_ops
;
598 sock
->ops
= &unix_seqpacket_ops
;
601 return -ESOCKTNOSUPPORT
;
604 return unix_create1(sock
) ? 0 : -ENOMEM
;
607 static int unix_release(struct socket
*sock
)
609 struct sock
*sk
= sock
->sk
;
616 return unix_release_sock (sk
, 0);
619 static int unix_autobind(struct socket
*sock
)
621 struct sock
*sk
= sock
->sk
;
622 struct unix_sock
*u
= unix_sk(sk
);
623 static u32 ordernum
= 1;
624 struct unix_address
* addr
;
634 addr
= kmalloc(sizeof(*addr
) + sizeof(short) + 16, GFP_KERNEL
);
638 memset(addr
, 0, sizeof(*addr
) + sizeof(short) + 16);
639 addr
->name
->sun_family
= AF_UNIX
;
640 atomic_set(&addr
->refcnt
, 1);
643 addr
->len
= sprintf(addr
->name
->sun_path
+1, "%05x", ordernum
) + 1 + sizeof(short);
644 addr
->hash
= unix_hash_fold(csum_partial((void*)addr
->name
, addr
->len
, 0));
646 write_lock(&unix_table_lock
);
647 ordernum
= (ordernum
+1)&0xFFFFF;
649 if (__unix_find_socket_byname(addr
->name
, addr
->len
, sock
->type
,
651 write_unlock(&unix_table_lock
);
652 /* Sanity yield. It is unusual case, but yet... */
653 if (!(ordernum
&0xFF))
657 addr
->hash
^= sk
->sk_type
;
659 __unix_remove_socket(sk
);
661 __unix_insert_socket(&unix_socket_table
[addr
->hash
], sk
);
662 write_unlock(&unix_table_lock
);
665 out
: up(&u
->readsem
);
669 static struct sock
*unix_find_other(struct sockaddr_un
*sunname
, int len
,
670 int type
, unsigned hash
, int *error
)
676 if (sunname
->sun_path
[0]) {
677 err
= path_lookup(sunname
->sun_path
, LOOKUP_FOLLOW
, &nd
);
680 err
= permission(nd
.dentry
->d_inode
,MAY_WRITE
, &nd
);
685 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
687 u
=unix_find_socket_byinode(nd
.dentry
->d_inode
);
691 if (u
->sk_type
== type
)
692 touch_atime(nd
.mnt
, nd
.dentry
);
697 if (u
->sk_type
!= type
) {
703 u
=unix_find_socket_byname(sunname
, len
, type
, hash
);
705 struct dentry
*dentry
;
706 dentry
= unix_sk(u
)->dentry
;
708 touch_atime(unix_sk(u
)->mnt
, dentry
);
722 static int unix_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
724 struct sock
*sk
= sock
->sk
;
725 struct unix_sock
*u
= unix_sk(sk
);
726 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
727 struct dentry
* dentry
= NULL
;
731 struct unix_address
*addr
;
732 struct hlist_head
*list
;
735 if (sunaddr
->sun_family
!= AF_UNIX
)
738 if (addr_len
==sizeof(short)) {
739 err
= unix_autobind(sock
);
743 err
= unix_mkname(sunaddr
, addr_len
, &hash
);
755 addr
= kmalloc(sizeof(*addr
)+addr_len
, GFP_KERNEL
);
759 memcpy(addr
->name
, sunaddr
, addr_len
);
760 addr
->len
= addr_len
;
761 addr
->hash
= hash
^ sk
->sk_type
;
762 atomic_set(&addr
->refcnt
, 1);
764 if (sunaddr
->sun_path
[0]) {
768 * Get the parent directory, calculate the hash for last
771 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
773 goto out_mknod_parent
;
775 * Yucky last component or no last component at all?
776 * (foo/., foo/.., /////)
779 if (nd
.last_type
!= LAST_NORM
)
782 * Lock the directory.
784 down(&nd
.dentry
->d_inode
->i_sem
);
786 * Do the final lookup.
788 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
789 err
= PTR_ERR(dentry
);
791 goto out_mknod_unlock
;
794 * Special case - lookup gave negative, but... we had foo/bar/
795 * From the vfs_mknod() POV we just have a negative dentry -
796 * all is fine. Let's be bastards - you had / on the end, you've
797 * been asking for (non-existent) directory. -ENOENT for you.
799 if (nd
.last
.name
[nd
.last
.len
] && !dentry
->d_inode
)
802 * All right, let's create it.
805 (SOCK_INODE(sock
)->i_mode
& ~current
->fs
->umask
);
806 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
809 up(&nd
.dentry
->d_inode
->i_sem
);
813 addr
->hash
= UNIX_HASH_SIZE
;
816 write_lock(&unix_table_lock
);
818 if (!sunaddr
->sun_path
[0]) {
820 if (__unix_find_socket_byname(sunaddr
, addr_len
,
821 sk
->sk_type
, hash
)) {
822 unix_release_addr(addr
);
826 list
= &unix_socket_table
[addr
->hash
];
828 list
= &unix_socket_table
[dentry
->d_inode
->i_ino
& (UNIX_HASH_SIZE
-1)];
829 u
->dentry
= nd
.dentry
;
834 __unix_remove_socket(sk
);
836 __unix_insert_socket(list
, sk
);
839 write_unlock(&unix_table_lock
);
848 up(&nd
.dentry
->d_inode
->i_sem
);
854 unix_release_addr(addr
);
858 static int unix_dgram_connect(struct socket
*sock
, struct sockaddr
*addr
,
861 struct sock
*sk
= sock
->sk
;
862 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
867 if (addr
->sa_family
!= AF_UNSPEC
) {
868 err
= unix_mkname(sunaddr
, alen
, &hash
);
873 if (test_bit(SOCK_PASSCRED
, &sock
->flags
) &&
874 !unix_sk(sk
)->addr
&& (err
= unix_autobind(sock
)) != 0)
877 other
=unix_find_other(sunaddr
, alen
, sock
->type
, hash
, &err
);
881 unix_state_wlock(sk
);
884 if (!unix_may_send(sk
, other
))
887 err
= security_unix_may_send(sk
->sk_socket
, other
->sk_socket
);
893 * 1003.1g breaking connected state with AF_UNSPEC
896 unix_state_wlock(sk
);
900 * If it was connected, reconnect.
903 struct sock
*old_peer
= unix_peer(sk
);
905 unix_state_wunlock(sk
);
907 if (other
!= old_peer
)
908 unix_dgram_disconnected(sk
, old_peer
);
912 unix_state_wunlock(sk
);
917 unix_state_wunlock(sk
);
923 static long unix_wait_for_peer(struct sock
*other
, long timeo
)
925 struct unix_sock
*u
= unix_sk(other
);
929 prepare_to_wait_exclusive(&u
->peer_wait
, &wait
, TASK_INTERRUPTIBLE
);
931 sched
= !sock_flag(other
, SOCK_DEAD
) &&
932 !(other
->sk_shutdown
& RCV_SHUTDOWN
) &&
933 (skb_queue_len(&other
->sk_receive_queue
) >
934 other
->sk_max_ack_backlog
);
936 unix_state_runlock(other
);
939 timeo
= schedule_timeout(timeo
);
941 finish_wait(&u
->peer_wait
, &wait
);
945 static int unix_stream_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
946 int addr_len
, int flags
)
948 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
949 struct sock
*sk
= sock
->sk
;
950 struct unix_sock
*u
= unix_sk(sk
), *newu
, *otheru
;
951 struct sock
*newsk
= NULL
;
952 struct sock
*other
= NULL
;
953 struct sk_buff
*skb
= NULL
;
959 err
= unix_mkname(sunaddr
, addr_len
, &hash
);
964 if (test_bit(SOCK_PASSCRED
, &sock
->flags
)
965 && !u
->addr
&& (err
= unix_autobind(sock
)) != 0)
968 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
970 /* First of all allocate resources.
971 If we will make it after state is locked,
972 we will have to recheck all again in any case.
977 /* create new sock for complete connection */
978 newsk
= unix_create1(NULL
);
982 /* Allocate skb for sending to listening sock */
983 skb
= sock_wmalloc(newsk
, 1, 0, GFP_KERNEL
);
988 /* Find listening sock. */
989 other
= unix_find_other(sunaddr
, addr_len
, sk
->sk_type
, hash
, &err
);
993 /* Latch state of peer */
994 unix_state_rlock(other
);
996 /* Apparently VFS overslept socket death. Retry. */
997 if (sock_flag(other
, SOCK_DEAD
)) {
998 unix_state_runlock(other
);
1003 err
= -ECONNREFUSED
;
1004 if (other
->sk_state
!= TCP_LISTEN
)
1007 if (skb_queue_len(&other
->sk_receive_queue
) >
1008 other
->sk_max_ack_backlog
) {
1013 timeo
= unix_wait_for_peer(other
, timeo
);
1015 err
= sock_intr_errno(timeo
);
1016 if (signal_pending(current
))
1024 It is tricky place. We need to grab write lock and cannot
1025 drop lock on peer. It is dangerous because deadlock is
1026 possible. Connect to self case and simultaneous
1027 attempt to connect are eliminated by checking socket
1028 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1029 check this before attempt to grab lock.
1031 Well, and we have to recheck the state after socket locked.
1037 /* This is ok... continue with connect */
1039 case TCP_ESTABLISHED
:
1040 /* Socket is already connected */
1048 unix_state_wlock(sk
);
1050 if (sk
->sk_state
!= st
) {
1051 unix_state_wunlock(sk
);
1052 unix_state_runlock(other
);
1057 err
= security_unix_stream_connect(sock
, other
->sk_socket
, newsk
);
1059 unix_state_wunlock(sk
);
1063 /* The way is open! Fastly set all the necessary fields... */
1066 unix_peer(newsk
) = sk
;
1067 newsk
->sk_state
= TCP_ESTABLISHED
;
1068 newsk
->sk_type
= sk
->sk_type
;
1069 newsk
->sk_peercred
.pid
= current
->tgid
;
1070 newsk
->sk_peercred
.uid
= current
->euid
;
1071 newsk
->sk_peercred
.gid
= current
->egid
;
1072 newu
= unix_sk(newsk
);
1073 newsk
->sk_sleep
= &newu
->peer_wait
;
1074 otheru
= unix_sk(other
);
1076 /* copy address information from listening to new sock*/
1078 atomic_inc(&otheru
->addr
->refcnt
);
1079 newu
->addr
= otheru
->addr
;
1081 if (otheru
->dentry
) {
1082 newu
->dentry
= dget(otheru
->dentry
);
1083 newu
->mnt
= mntget(otheru
->mnt
);
1086 /* Set credentials */
1087 sk
->sk_peercred
= other
->sk_peercred
;
1090 unix_peer(sk
) = newsk
;
1091 sock
->state
= SS_CONNECTED
;
1092 sk
->sk_state
= TCP_ESTABLISHED
;
1094 unix_state_wunlock(sk
);
1096 /* take ten and and send info to listening sock */
1097 spin_lock(&other
->sk_receive_queue
.lock
);
1098 __skb_queue_tail(&other
->sk_receive_queue
, skb
);
1099 /* Undo artificially decreased inflight after embrion
1100 * is installed to listening socket. */
1101 atomic_inc(&newu
->inflight
);
1102 spin_unlock(&other
->sk_receive_queue
.lock
);
1103 unix_state_runlock(other
);
1104 other
->sk_data_ready(other
, 0);
1110 unix_state_runlock(other
);
1116 unix_release_sock(newsk
, 0);
1122 static int unix_socketpair(struct socket
*socka
, struct socket
*sockb
)
1124 struct sock
*ska
=socka
->sk
, *skb
= sockb
->sk
;
1126 /* Join our sockets back to back */
1131 ska
->sk_peercred
.pid
= skb
->sk_peercred
.pid
= current
->tgid
;
1132 ska
->sk_peercred
.uid
= skb
->sk_peercred
.uid
= current
->euid
;
1133 ska
->sk_peercred
.gid
= skb
->sk_peercred
.gid
= current
->egid
;
1135 if (ska
->sk_type
!= SOCK_DGRAM
) {
1136 ska
->sk_state
= TCP_ESTABLISHED
;
1137 skb
->sk_state
= TCP_ESTABLISHED
;
1138 socka
->state
= SS_CONNECTED
;
1139 sockb
->state
= SS_CONNECTED
;
1144 static int unix_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1146 struct sock
*sk
= sock
->sk
;
1148 struct sk_buff
*skb
;
1152 if (sock
->type
!=SOCK_STREAM
&& sock
->type
!=SOCK_SEQPACKET
)
1156 if (sk
->sk_state
!= TCP_LISTEN
)
1159 /* If socket state is TCP_LISTEN it cannot change (for now...),
1160 * so that no locks are necessary.
1163 skb
= skb_recv_datagram(sk
, 0, flags
&O_NONBLOCK
, &err
);
1165 /* This means receive shutdown. */
1172 skb_free_datagram(sk
, skb
);
1173 wake_up_interruptible(&unix_sk(sk
)->peer_wait
);
1175 /* attach accepted sock to socket */
1176 unix_state_wlock(tsk
);
1177 newsock
->state
= SS_CONNECTED
;
1178 sock_graft(tsk
, newsock
);
1179 unix_state_wunlock(tsk
);
1187 static int unix_getname(struct socket
*sock
, struct sockaddr
*uaddr
, int *uaddr_len
, int peer
)
1189 struct sock
*sk
= sock
->sk
;
1190 struct unix_sock
*u
;
1191 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
1195 sk
= unix_peer_get(sk
);
1206 unix_state_rlock(sk
);
1208 sunaddr
->sun_family
= AF_UNIX
;
1209 sunaddr
->sun_path
[0] = 0;
1210 *uaddr_len
= sizeof(short);
1212 struct unix_address
*addr
= u
->addr
;
1214 *uaddr_len
= addr
->len
;
1215 memcpy(sunaddr
, addr
->name
, *uaddr_len
);
1217 unix_state_runlock(sk
);
1223 static void unix_detach_fds(struct scm_cookie
*scm
, struct sk_buff
*skb
)
1227 scm
->fp
= UNIXCB(skb
).fp
;
1228 skb
->destructor
= sock_wfree
;
1229 UNIXCB(skb
).fp
= NULL
;
1231 for (i
=scm
->fp
->count
-1; i
>=0; i
--)
1232 unix_notinflight(scm
->fp
->fp
[i
]);
1235 static void unix_destruct_fds(struct sk_buff
*skb
)
1237 struct scm_cookie scm
;
1238 memset(&scm
, 0, sizeof(scm
));
1239 unix_detach_fds(&scm
, skb
);
1241 /* Alas, it calls VFS */
1242 /* So fscking what? fput() had been SMP-safe since the last Summer */
1247 static void unix_attach_fds(struct scm_cookie
*scm
, struct sk_buff
*skb
)
1250 for (i
=scm
->fp
->count
-1; i
>=0; i
--)
1251 unix_inflight(scm
->fp
->fp
[i
]);
1252 UNIXCB(skb
).fp
= scm
->fp
;
1253 skb
->destructor
= unix_destruct_fds
;
1258 * Send AF_UNIX data.
1261 static int unix_dgram_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1262 struct msghdr
*msg
, size_t len
)
1264 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
1265 struct sock
*sk
= sock
->sk
;
1266 struct unix_sock
*u
= unix_sk(sk
);
1267 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1268 struct sock
*other
= NULL
;
1269 int namelen
= 0; /* fake GCC */
1272 struct sk_buff
*skb
;
1274 struct scm_cookie tmp_scm
;
1276 if (NULL
== siocb
->scm
)
1277 siocb
->scm
= &tmp_scm
;
1278 err
= scm_send(sock
, msg
, siocb
->scm
);
1283 if (msg
->msg_flags
&MSG_OOB
)
1286 if (msg
->msg_namelen
) {
1287 err
= unix_mkname(sunaddr
, msg
->msg_namelen
, &hash
);
1294 other
= unix_peer_get(sk
);
1299 if (test_bit(SOCK_PASSCRED
, &sock
->flags
)
1300 && !u
->addr
&& (err
= unix_autobind(sock
)) != 0)
1304 if (len
> sk
->sk_sndbuf
- 32)
1307 skb
= sock_alloc_send_skb(sk
, len
, msg
->msg_flags
&MSG_DONTWAIT
, &err
);
1311 memcpy(UNIXCREDS(skb
), &siocb
->scm
->creds
, sizeof(struct ucred
));
1313 unix_attach_fds(siocb
->scm
, skb
);
1315 skb
->h
.raw
= skb
->data
;
1316 err
= memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
);
1320 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1325 if (sunaddr
== NULL
)
1328 other
= unix_find_other(sunaddr
, namelen
, sk
->sk_type
,
1334 unix_state_rlock(other
);
1336 if (!unix_may_send(sk
, other
))
1339 if (sock_flag(other
, SOCK_DEAD
)) {
1341 * Check with 1003.1g - what should
1344 unix_state_runlock(other
);
1348 unix_state_wlock(sk
);
1349 if (unix_peer(sk
) == other
) {
1351 unix_state_wunlock(sk
);
1353 unix_dgram_disconnected(sk
, other
);
1355 err
= -ECONNREFUSED
;
1357 unix_state_wunlock(sk
);
1367 if (other
->sk_shutdown
& RCV_SHUTDOWN
)
1370 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
1371 err
= security_unix_may_send(sk
->sk_socket
, other
->sk_socket
);
1376 if (unix_peer(other
) != sk
&&
1377 (skb_queue_len(&other
->sk_receive_queue
) >
1378 other
->sk_max_ack_backlog
)) {
1384 timeo
= unix_wait_for_peer(other
, timeo
);
1386 err
= sock_intr_errno(timeo
);
1387 if (signal_pending(current
))
1393 skb_queue_tail(&other
->sk_receive_queue
, skb
);
1394 unix_state_runlock(other
);
1395 other
->sk_data_ready(other
, len
);
1397 scm_destroy(siocb
->scm
);
1401 unix_state_runlock(other
);
1407 scm_destroy(siocb
->scm
);
1412 static int unix_stream_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1413 struct msghdr
*msg
, size_t len
)
1415 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
1416 struct sock
*sk
= sock
->sk
;
1417 struct sock
*other
= NULL
;
1418 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1420 struct sk_buff
*skb
;
1422 struct scm_cookie tmp_scm
;
1424 if (NULL
== siocb
->scm
)
1425 siocb
->scm
= &tmp_scm
;
1426 err
= scm_send(sock
, msg
, siocb
->scm
);
1431 if (msg
->msg_flags
&MSG_OOB
)
1434 if (msg
->msg_namelen
) {
1435 err
= sk
->sk_state
== TCP_ESTABLISHED
? -EISCONN
: -EOPNOTSUPP
;
1440 other
= unix_peer_get(sk
);
1445 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
1451 * Optimisation for the fact that under 0.01% of X messages typically
1457 /* Keep two messages in the pipe so it schedules better */
1458 if (size
> sk
->sk_sndbuf
/ 2 - 64)
1459 size
= sk
->sk_sndbuf
/ 2 - 64;
1461 if (size
> SKB_MAX_ALLOC
)
1462 size
= SKB_MAX_ALLOC
;
1468 skb
=sock_alloc_send_skb(sk
,size
,msg
->msg_flags
&MSG_DONTWAIT
, &err
);
1474 * If you pass two values to the sock_alloc_send_skb
1475 * it tries to grab the large buffer with GFP_NOFS
1476 * (which can fail easily), and if it fails grab the
1477 * fallback size buffer which is under a page and will
1480 size
= min_t(int, size
, skb_tailroom(skb
));
1482 memcpy(UNIXCREDS(skb
), &siocb
->scm
->creds
, sizeof(struct ucred
));
1484 unix_attach_fds(siocb
->scm
, skb
);
1486 if ((err
= memcpy_fromiovec(skb_put(skb
,size
), msg
->msg_iov
, size
)) != 0) {
1491 unix_state_rlock(other
);
1493 if (sock_flag(other
, SOCK_DEAD
) ||
1494 (other
->sk_shutdown
& RCV_SHUTDOWN
))
1497 skb_queue_tail(&other
->sk_receive_queue
, skb
);
1498 unix_state_runlock(other
);
1499 other
->sk_data_ready(other
, size
);
1504 scm_destroy(siocb
->scm
);
1510 unix_state_runlock(other
);
1513 if (sent
==0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
1514 send_sig(SIGPIPE
,current
,0);
1519 scm_destroy(siocb
->scm
);
1521 return sent
? : err
;
1524 static int unix_seqpacket_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1525 struct msghdr
*msg
, size_t len
)
1528 struct sock
*sk
= sock
->sk
;
1530 err
= sock_error(sk
);
1534 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1537 if (msg
->msg_namelen
)
1538 msg
->msg_namelen
= 0;
1540 return unix_dgram_sendmsg(kiocb
, sock
, msg
, len
);
1543 static void unix_copy_addr(struct msghdr
*msg
, struct sock
*sk
)
1545 struct unix_sock
*u
= unix_sk(sk
);
1547 msg
->msg_namelen
= 0;
1549 msg
->msg_namelen
= u
->addr
->len
;
1550 memcpy(msg
->msg_name
, u
->addr
->name
, u
->addr
->len
);
1554 static int unix_dgram_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1555 struct msghdr
*msg
, size_t size
,
1558 struct sock_iocb
*siocb
= kiocb_to_siocb(iocb
);
1559 struct scm_cookie tmp_scm
;
1560 struct sock
*sk
= sock
->sk
;
1561 struct unix_sock
*u
= unix_sk(sk
);
1562 int noblock
= flags
& MSG_DONTWAIT
;
1563 struct sk_buff
*skb
;
1570 msg
->msg_namelen
= 0;
1574 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1578 wake_up_interruptible(&u
->peer_wait
);
1581 unix_copy_addr(msg
, skb
->sk
);
1583 if (size
> skb
->len
)
1585 else if (size
< skb
->len
)
1586 msg
->msg_flags
|= MSG_TRUNC
;
1588 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, size
);
1593 siocb
->scm
= &tmp_scm
;
1594 memset(&tmp_scm
, 0, sizeof(tmp_scm
));
1596 siocb
->scm
->creds
= *UNIXCREDS(skb
);
1598 if (!(flags
& MSG_PEEK
))
1601 unix_detach_fds(siocb
->scm
, skb
);
1605 /* It is questionable: on PEEK we could:
1606 - do not return fds - good, but too simple 8)
1607 - return fds, and do not return them on read (old strategy,
1609 - clone fds (I chose it for now, it is the most universal
1612 POSIX 1003.1g does not actually define this clearly
1613 at all. POSIX 1003.1g doesn't define a lot of things
1618 siocb
->scm
->fp
= scm_fp_dup(UNIXCB(skb
).fp
);
1622 scm_recv(sock
, msg
, siocb
->scm
, flags
);
1625 skb_free_datagram(sk
,skb
);
1633 * Sleep until data has arrive. But check for races..
1636 static long unix_stream_data_wait(struct sock
* sk
, long timeo
)
1640 unix_state_rlock(sk
);
1643 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
1645 if (skb_queue_len(&sk
->sk_receive_queue
) ||
1647 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
1648 signal_pending(current
) ||
1652 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
1653 unix_state_runlock(sk
);
1654 timeo
= schedule_timeout(timeo
);
1655 unix_state_rlock(sk
);
1656 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
1659 finish_wait(sk
->sk_sleep
, &wait
);
1660 unix_state_runlock(sk
);
1666 static int unix_stream_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1667 struct msghdr
*msg
, size_t size
,
1670 struct sock_iocb
*siocb
= kiocb_to_siocb(iocb
);
1671 struct scm_cookie tmp_scm
;
1672 struct sock
*sk
= sock
->sk
;
1673 struct unix_sock
*u
= unix_sk(sk
);
1674 struct sockaddr_un
*sunaddr
=msg
->msg_name
;
1676 int check_creds
= 0;
1682 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1689 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
1690 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
1692 msg
->msg_namelen
= 0;
1694 /* Lock the socket to prevent queue disordering
1695 * while sleeps in memcpy_tomsg
1699 siocb
->scm
= &tmp_scm
;
1700 memset(&tmp_scm
, 0, sizeof(tmp_scm
));
1708 struct sk_buff
*skb
;
1710 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1713 if (copied
>= target
)
1717 * POSIX 1003.1g mandates this order.
1720 if ((err
= sock_error(sk
)) != 0)
1722 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1729 timeo
= unix_stream_data_wait(sk
, timeo
);
1731 if (signal_pending(current
)) {
1732 err
= sock_intr_errno(timeo
);
1740 /* Never glue messages from different writers */
1741 if (memcmp(UNIXCREDS(skb
), &siocb
->scm
->creds
, sizeof(siocb
->scm
->creds
)) != 0) {
1742 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1746 /* Copy credentials */
1747 siocb
->scm
->creds
= *UNIXCREDS(skb
);
1751 /* Copy address just once */
1754 unix_copy_addr(msg
, skb
->sk
);
1758 chunk
= min_t(unsigned int, skb
->len
, size
);
1759 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
1760 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1768 /* Mark read part of skb as used */
1769 if (!(flags
& MSG_PEEK
))
1771 skb_pull(skb
, chunk
);
1774 unix_detach_fds(siocb
->scm
, skb
);
1776 /* put the skb back if we didn't use it up.. */
1779 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1790 /* It is questionable, see note in unix_dgram_recvmsg.
1793 siocb
->scm
->fp
= scm_fp_dup(UNIXCB(skb
).fp
);
1795 /* put message back and return */
1796 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1802 scm_recv(sock
, msg
, siocb
->scm
, flags
);
1804 return copied
? : err
;
1807 static int unix_shutdown(struct socket
*sock
, int mode
)
1809 struct sock
*sk
= sock
->sk
;
1812 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1815 unix_state_wlock(sk
);
1816 sk
->sk_shutdown
|= mode
;
1817 other
=unix_peer(sk
);
1820 unix_state_wunlock(sk
);
1821 sk
->sk_state_change(sk
);
1824 (sk
->sk_type
== SOCK_STREAM
|| sk
->sk_type
== SOCK_SEQPACKET
)) {
1828 if (mode
&RCV_SHUTDOWN
)
1829 peer_mode
|= SEND_SHUTDOWN
;
1830 if (mode
&SEND_SHUTDOWN
)
1831 peer_mode
|= RCV_SHUTDOWN
;
1832 unix_state_wlock(other
);
1833 other
->sk_shutdown
|= peer_mode
;
1834 unix_state_wunlock(other
);
1835 other
->sk_state_change(other
);
1836 read_lock(&other
->sk_callback_lock
);
1837 if (peer_mode
== SHUTDOWN_MASK
)
1838 sk_wake_async(other
,1,POLL_HUP
);
1839 else if (peer_mode
& RCV_SHUTDOWN
)
1840 sk_wake_async(other
,1,POLL_IN
);
1841 read_unlock(&other
->sk_callback_lock
);
1849 static int unix_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1851 struct sock
*sk
= sock
->sk
;
1858 amount
= atomic_read(&sk
->sk_wmem_alloc
);
1859 err
= put_user(amount
, (int __user
*)arg
);
1863 struct sk_buff
*skb
;
1865 if (sk
->sk_state
== TCP_LISTEN
) {
1870 spin_lock(&sk
->sk_receive_queue
.lock
);
1871 if (sk
->sk_type
== SOCK_STREAM
||
1872 sk
->sk_type
== SOCK_SEQPACKET
) {
1873 skb_queue_walk(&sk
->sk_receive_queue
, skb
)
1876 skb
= skb_peek(&sk
->sk_receive_queue
);
1880 spin_unlock(&sk
->sk_receive_queue
.lock
);
1881 err
= put_user(amount
, (int __user
*)arg
);
1886 err
= dev_ioctl(cmd
, (void __user
*)arg
);
1892 static unsigned int unix_poll(struct file
* file
, struct socket
*sock
, poll_table
*wait
)
1894 struct sock
*sk
= sock
->sk
;
1897 poll_wait(file
, sk
->sk_sleep
, wait
);
1900 /* exceptional events? */
1903 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
1907 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1908 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
1909 mask
|= POLLIN
| POLLRDNORM
;
1911 /* Connection-based need to check for termination and startup */
1912 if ((sk
->sk_type
== SOCK_STREAM
|| sk
->sk_type
== SOCK_SEQPACKET
) && sk
->sk_state
== TCP_CLOSE
)
1916 * we set writable also when the other side has shut down the
1917 * connection. This prevents stuck sockets.
1919 if (unix_writable(sk
))
1920 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1926 #ifdef CONFIG_PROC_FS
1927 static struct sock
*unix_seq_idx(int *iter
, loff_t pos
)
1932 for (s
= first_unix_socket(iter
); s
; s
= next_unix_socket(iter
, s
)) {
1941 static void *unix_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1943 read_lock(&unix_table_lock
);
1944 return *pos
? unix_seq_idx(seq
->private, *pos
- 1) : ((void *) 1);
1947 static void *unix_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1952 return first_unix_socket(seq
->private);
1953 return next_unix_socket(seq
->private, v
);
1956 static void unix_seq_stop(struct seq_file
*seq
, void *v
)
1958 read_unlock(&unix_table_lock
);
1961 static int unix_seq_show(struct seq_file
*seq
, void *v
)
1965 seq_puts(seq
, "Num RefCount Protocol Flags Type St "
1969 struct unix_sock
*u
= unix_sk(s
);
1970 unix_state_rlock(s
);
1972 seq_printf(seq
, "%p: %08X %08X %08X %04X %02X %5lu",
1974 atomic_read(&s
->sk_refcnt
),
1976 s
->sk_state
== TCP_LISTEN
? __SO_ACCEPTCON
: 0,
1979 (s
->sk_state
== TCP_ESTABLISHED
? SS_CONNECTED
: SS_UNCONNECTED
) :
1980 (s
->sk_state
== TCP_ESTABLISHED
? SS_CONNECTING
: SS_DISCONNECTING
),
1988 len
= u
->addr
->len
- sizeof(short);
1989 if (!UNIX_ABSTRACT(s
))
1995 for ( ; i
< len
; i
++)
1996 seq_putc(seq
, u
->addr
->name
->sun_path
[i
]);
1998 unix_state_runlock(s
);
1999 seq_putc(seq
, '\n');
2005 static struct seq_operations unix_seq_ops
= {
2006 .start
= unix_seq_start
,
2007 .next
= unix_seq_next
,
2008 .stop
= unix_seq_stop
,
2009 .show
= unix_seq_show
,
2013 static int unix_seq_open(struct inode
*inode
, struct file
*file
)
2015 struct seq_file
*seq
;
2017 int *iter
= kmalloc(sizeof(int), GFP_KERNEL
);
2022 rc
= seq_open(file
, &unix_seq_ops
);
2026 seq
= file
->private_data
;
2027 seq
->private = iter
;
2036 static struct file_operations unix_seq_fops
= {
2037 .owner
= THIS_MODULE
,
2038 .open
= unix_seq_open
,
2040 .llseek
= seq_lseek
,
2041 .release
= seq_release_private
,
2046 static struct net_proto_family unix_family_ops
= {
2048 .create
= unix_create
,
2049 .owner
= THIS_MODULE
,
2052 #ifdef CONFIG_SYSCTL
2053 extern void unix_sysctl_register(void);
2054 extern void unix_sysctl_unregister(void);
2056 static inline void unix_sysctl_register(void) {}
2057 static inline void unix_sysctl_unregister(void) {}
2060 static int __init
af_unix_init(void)
2063 struct sk_buff
*dummy_skb
;
2065 if (sizeof(struct unix_skb_parms
) > sizeof(dummy_skb
->cb
)) {
2066 printk(KERN_CRIT
"%s: panic\n", __FUNCTION__
);
2070 rc
= proto_register(&unix_proto
, 1);
2072 printk(KERN_CRIT
"%s: Cannot create unix_sock SLAB cache!\n",
2077 sock_register(&unix_family_ops
);
2078 #ifdef CONFIG_PROC_FS
2079 proc_net_fops_create("unix", 0, &unix_seq_fops
);
2081 unix_sysctl_register();
2086 static void __exit
af_unix_exit(void)
2088 sock_unregister(PF_UNIX
);
2089 unix_sysctl_unregister();
2090 proc_net_remove("unix");
2091 proto_unregister(&unix_proto
);
2094 module_init(af_unix_init
);
2095 module_exit(af_unix_exit
);
2097 MODULE_LICENSE("GPL");
2098 MODULE_ALIAS_NETPROTO(PF_UNIX
);