2 * NET4: Implementation of BSD Unix domain sockets.
4 * Authors: Alan Cox, <alan.cox@linux.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Version: $Id: af_unix.c,v 1.133 2002/02/08 03:57:19 davem Exp $
14 * Linus Torvalds : Assorted bug cures.
15 * Niibe Yutaka : async I/O support.
16 * Carsten Paeth : PF_UNIX check, address fixes.
17 * Alan Cox : Limit size of allocated blocks.
18 * Alan Cox : Fixed the stupid socketpair bug.
19 * Alan Cox : BSD compatibility fine tuning.
20 * Alan Cox : Fixed a bug in connect when interrupted.
21 * Alan Cox : Sorted out a proper draft version of
22 * file descriptor passing hacked up from
24 * Marty Leisner : Fixes to fd passing
25 * Nick Nevin : recvmsg bugfix.
26 * Alan Cox : Started proper garbage collector
27 * Heiko EiBfeldt : Missing verify_area check
28 * Alan Cox : Started POSIXisms
29 * Andreas Schwab : Replace inode by dentry for proper
31 * Kirk Petersen : Made this a module
32 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
34 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
35 * by above two patches.
36 * Andrea Arcangeli : If possible we block in connect(2)
37 * if the max backlog of the listen socket
38 * is been reached. This won't break
39 * old apps and it will avoid huge amount
40 * of socks hashed (this for unix_gc()
41 * performances reasons).
42 * Security fix that limits the max
43 * number of socks to 2*max_files and
44 * the number of skb queueable in the
46 * Artur Skawina : Hash function optimizations
47 * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
48 * Malcolm Beattie : Set peercred for socketpair
49 * Michal Ostrowski : Module initialization cleanup.
50 * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
51 * the core infrastructure is doing that
52 * for all net proto families now (2.5.69+)
55 * Known differences from reference BSD that was tested:
58 * ECONNREFUSED is not returned from one end of a connected() socket to the
59 * other the moment one end closes.
60 * fstat() doesn't return st_dev=0, and give the blksize as high water mark
61 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
63 * accept() returns a path name even if the connecting socket has closed
64 * in the meantime (BSD loses the path and gives up).
65 * accept() returns 0 length path for an unbound connector. BSD returns 16
66 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
67 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
68 * BSD af_unix apparently has connect forgetting to block properly.
69 * (need to check this with the POSIX spec in detail)
71 * Differences from 2.0.0-11-... (ANK)
72 * Bug fixes and improvements.
73 * - client shutdown killed server socket.
74 * - removed all useless cli/sti pairs.
76 * Semantic changes/extensions.
77 * - generic control message passing.
78 * - SCM_CREDENTIALS control message.
79 * - "Abstract" (not FS based) socket bindings.
80 * Abstract names are sequences of bytes (not zero terminated)
81 * started by 0, so that this name space does not intersect
85 #include <linux/module.h>
86 #include <linux/kernel.h>
87 #include <linux/signal.h>
88 #include <linux/sched.h>
89 #include <linux/errno.h>
90 #include <linux/string.h>
91 #include <linux/stat.h>
92 #include <linux/dcache.h>
93 #include <linux/namei.h>
94 #include <linux/socket.h>
96 #include <linux/fcntl.h>
97 #include <linux/termios.h>
98 #include <linux/sockios.h>
99 #include <linux/net.h>
100 #include <linux/in.h>
101 #include <linux/fs.h>
102 #include <linux/slab.h>
103 #include <asm/uaccess.h>
104 #include <linux/skbuff.h>
105 #include <linux/netdevice.h>
106 #include <net/sock.h>
107 #include <net/tcp_states.h>
108 #include <net/af_unix.h>
109 #include <linux/proc_fs.h>
110 #include <linux/seq_file.h>
112 #include <linux/init.h>
113 #include <linux/poll.h>
114 #include <linux/smp_lock.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 = 10;
122 struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
123 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 #ifdef CONFIG_SECURITY_NETWORK
131 static void unix_get_peersec_dgram(struct sk_buff *skb)
135 err = security_socket_getpeersec_dgram(skb, UNIXSECDATA(skb),
138 *(UNIXSECDATA(skb)) = NULL;
141 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
143 scm->secdata = *UNIXSECDATA(skb);
144 scm->seclen = *UNIXSECLEN(skb);
147 static inline void unix_get_peersec_dgram(struct sk_buff *skb)
150 static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
152 #endif /* CONFIG_SECURITY_NETWORK */
155 * SMP locking strategy:
156 * hash table is protected with spinlock unix_table_lock
157 * each socket state is protected by separate rwlock.
160 static inline unsigned unix_hash_fold(unsigned hash)
164 return hash&(UNIX_HASH_SIZE-1);
167 #define unix_peer(sk) (unix_sk(sk)->peer)
169 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
171 return unix_peer(osk) == sk;
174 static inline int unix_may_send(struct sock *sk, struct sock *osk)
176 return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
179 static struct sock *unix_peer_get(struct sock *s)
187 unix_state_runlock(s);
191 static inline void unix_release_addr(struct unix_address *addr)
193 if (atomic_dec_and_test(&addr->refcnt))
198 * Check unix socket name:
199 * - should be not zero length.
200 * - if started by not zero, should be NULL terminated (FS object)
201 * - if started by zero, it is abstract name.
204 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
206 if (len <= sizeof(short) || len > sizeof(*sunaddr))
208 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
210 if (sunaddr->sun_path[0]) {
212 * This may look like an off by one error but it is a bit more
213 * subtle. 108 is the longest valid AF_UNIX path for a binding.
214 * sun_path[108] doesnt as such exist. However in kernel space
215 * we are guaranteed that it is a valid memory location in our
216 * kernel address buffer.
218 ((char *)sunaddr)[len]=0;
219 len = strlen(sunaddr->sun_path)+1+sizeof(short);
223 *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
227 static void __unix_remove_socket(struct sock *sk)
229 sk_del_node_init(sk);
232 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
234 BUG_TRAP(sk_unhashed(sk));
235 sk_add_node(sk, list);
238 static inline void unix_remove_socket(struct sock *sk)
240 spin_lock(&unix_table_lock);
241 __unix_remove_socket(sk);
242 spin_unlock(&unix_table_lock);
245 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
247 spin_lock(&unix_table_lock);
248 __unix_insert_socket(list, sk);
249 spin_unlock(&unix_table_lock);
252 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
253 int len, int type, unsigned hash)
256 struct hlist_node *node;
258 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
259 struct unix_sock *u = unix_sk(s);
261 if (u->addr->len == len &&
262 !memcmp(u->addr->name, sunname, len))
270 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
276 spin_lock(&unix_table_lock);
277 s = __unix_find_socket_byname(sunname, len, type, hash);
280 spin_unlock(&unix_table_lock);
284 static struct sock *unix_find_socket_byinode(struct inode *i)
287 struct hlist_node *node;
289 spin_lock(&unix_table_lock);
291 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
292 struct dentry *dentry = unix_sk(s)->dentry;
294 if(dentry && dentry->d_inode == i)
302 spin_unlock(&unix_table_lock);
306 static inline int unix_writable(struct sock *sk)
308 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
311 static void unix_write_space(struct sock *sk)
313 read_lock(&sk->sk_callback_lock);
314 if (unix_writable(sk)) {
315 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
316 wake_up_interruptible(sk->sk_sleep);
317 sk_wake_async(sk, 2, POLL_OUT);
319 read_unlock(&sk->sk_callback_lock);
322 /* When dgram socket disconnects (or changes its peer), we clear its receive
323 * queue of packets arrived from previous peer. First, it allows to do
324 * flow control based only on wmem_alloc; second, sk connected to peer
325 * may receive messages only from that peer. */
326 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
328 if (!skb_queue_empty(&sk->sk_receive_queue)) {
329 skb_queue_purge(&sk->sk_receive_queue);
330 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
332 /* If one link of bidirectional dgram pipe is disconnected,
333 * we signal error. Messages are lost. Do not make this,
334 * when peer was not connected to us.
336 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
337 other->sk_err = ECONNRESET;
338 other->sk_error_report(other);
343 static void unix_sock_destructor(struct sock *sk)
345 struct unix_sock *u = unix_sk(sk);
347 skb_queue_purge(&sk->sk_receive_queue);
349 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
350 BUG_TRAP(sk_unhashed(sk));
351 BUG_TRAP(!sk->sk_socket);
352 if (!sock_flag(sk, SOCK_DEAD)) {
353 printk("Attempt to release alive unix socket: %p\n", sk);
358 unix_release_addr(u->addr);
360 atomic_dec(&unix_nr_socks);
361 #ifdef UNIX_REFCNT_DEBUG
362 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
366 static int unix_release_sock (struct sock *sk, int embrion)
368 struct unix_sock *u = unix_sk(sk);
369 struct dentry *dentry;
370 struct vfsmount *mnt;
375 unix_remove_socket(sk);
378 unix_state_wlock(sk);
380 sk->sk_shutdown = SHUTDOWN_MASK;
385 state = sk->sk_state;
386 sk->sk_state = TCP_CLOSE;
387 unix_state_wunlock(sk);
389 wake_up_interruptible_all(&u->peer_wait);
391 skpair=unix_peer(sk);
394 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
395 unix_state_wlock(skpair);
397 skpair->sk_shutdown = SHUTDOWN_MASK;
398 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
399 skpair->sk_err = ECONNRESET;
400 unix_state_wunlock(skpair);
401 skpair->sk_state_change(skpair);
402 read_lock(&skpair->sk_callback_lock);
403 sk_wake_async(skpair,1,POLL_HUP);
404 read_unlock(&skpair->sk_callback_lock);
406 sock_put(skpair); /* It may now die */
407 unix_peer(sk) = NULL;
410 /* Try to flush out this socket. Throw out buffers at least */
412 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
413 if (state==TCP_LISTEN)
414 unix_release_sock(skb->sk, 1);
415 /* passed fds are erased in the kfree_skb hook */
426 /* ---- Socket is dead now and most probably destroyed ---- */
429 * Fixme: BSD difference: In BSD all sockets connected to use get
430 * ECONNRESET and we die on the spot. In Linux we behave
431 * like files and pipes do and wait for the last
434 * Can't we simply set sock->err?
436 * What the above comment does talk about? --ANK(980817)
439 if (atomic_read(&unix_tot_inflight))
440 unix_gc(); /* Garbage collect fds */
445 static int unix_listen(struct socket *sock, int backlog)
448 struct sock *sk = sock->sk;
449 struct unix_sock *u = unix_sk(sk);
452 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
453 goto out; /* Only stream/seqpacket sockets accept */
456 goto out; /* No listens on an unbound socket */
457 unix_state_wlock(sk);
458 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
460 if (backlog > sk->sk_max_ack_backlog)
461 wake_up_interruptible_all(&u->peer_wait);
462 sk->sk_max_ack_backlog = backlog;
463 sk->sk_state = TCP_LISTEN;
464 /* set credentials so connect can copy them */
465 sk->sk_peercred.pid = current->tgid;
466 sk->sk_peercred.uid = current->euid;
467 sk->sk_peercred.gid = current->egid;
471 unix_state_wunlock(sk);
476 static int unix_release(struct socket *);
477 static int unix_bind(struct socket *, struct sockaddr *, int);
478 static int unix_stream_connect(struct socket *, struct sockaddr *,
479 int addr_len, int flags);
480 static int unix_socketpair(struct socket *, struct socket *);
481 static int unix_accept(struct socket *, struct socket *, int);
482 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
483 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
484 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
485 static int unix_shutdown(struct socket *, int);
486 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
487 struct msghdr *, size_t);
488 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
489 struct msghdr *, size_t, int);
490 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
491 struct msghdr *, size_t);
492 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
493 struct msghdr *, size_t, int);
494 static int unix_dgram_connect(struct socket *, struct sockaddr *,
496 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
497 struct msghdr *, size_t);
499 static const struct proto_ops unix_stream_ops = {
501 .owner = THIS_MODULE,
502 .release = unix_release,
504 .connect = unix_stream_connect,
505 .socketpair = unix_socketpair,
506 .accept = unix_accept,
507 .getname = unix_getname,
510 .listen = unix_listen,
511 .shutdown = unix_shutdown,
512 .setsockopt = sock_no_setsockopt,
513 .getsockopt = sock_no_getsockopt,
514 .sendmsg = unix_stream_sendmsg,
515 .recvmsg = unix_stream_recvmsg,
516 .mmap = sock_no_mmap,
517 .sendpage = sock_no_sendpage,
520 static const struct proto_ops unix_dgram_ops = {
522 .owner = THIS_MODULE,
523 .release = unix_release,
525 .connect = unix_dgram_connect,
526 .socketpair = unix_socketpair,
527 .accept = sock_no_accept,
528 .getname = unix_getname,
529 .poll = datagram_poll,
531 .listen = sock_no_listen,
532 .shutdown = unix_shutdown,
533 .setsockopt = sock_no_setsockopt,
534 .getsockopt = sock_no_getsockopt,
535 .sendmsg = unix_dgram_sendmsg,
536 .recvmsg = unix_dgram_recvmsg,
537 .mmap = sock_no_mmap,
538 .sendpage = sock_no_sendpage,
541 static const struct proto_ops unix_seqpacket_ops = {
543 .owner = THIS_MODULE,
544 .release = unix_release,
546 .connect = unix_stream_connect,
547 .socketpair = unix_socketpair,
548 .accept = unix_accept,
549 .getname = unix_getname,
550 .poll = datagram_poll,
552 .listen = unix_listen,
553 .shutdown = unix_shutdown,
554 .setsockopt = sock_no_setsockopt,
555 .getsockopt = sock_no_getsockopt,
556 .sendmsg = unix_seqpacket_sendmsg,
557 .recvmsg = unix_dgram_recvmsg,
558 .mmap = sock_no_mmap,
559 .sendpage = sock_no_sendpage,
562 static struct proto unix_proto = {
564 .owner = THIS_MODULE,
565 .obj_size = sizeof(struct unix_sock),
569 * AF_UNIX sockets do not interact with hardware, hence they
570 * dont trigger interrupts - so it's safe for them to have
571 * bh-unsafe locking for their sk_receive_queue.lock. Split off
572 * this special lock-class by reinitializing the spinlock key:
574 static struct lock_class_key af_unix_sk_receive_queue_lock_key;
576 static struct sock * unix_create1(struct socket *sock)
578 struct sock *sk = NULL;
581 if (atomic_read(&unix_nr_socks) >= 2*get_max_files())
584 sk = sk_alloc(PF_UNIX, GFP_KERNEL, &unix_proto, 1);
588 atomic_inc(&unix_nr_socks);
590 sock_init_data(sock,sk);
591 lockdep_set_class(&sk->sk_receive_queue.lock,
592 &af_unix_sk_receive_queue_lock_key);
594 sk->sk_write_space = unix_write_space;
595 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
596 sk->sk_destruct = unix_sock_destructor;
600 spin_lock_init(&u->lock);
601 atomic_set(&u->inflight, sock ? 0 : -1);
602 mutex_init(&u->readlock); /* single task reading lock */
603 init_waitqueue_head(&u->peer_wait);
604 unix_insert_socket(unix_sockets_unbound, sk);
609 static int unix_create(struct socket *sock, int protocol)
611 if (protocol && protocol != PF_UNIX)
612 return -EPROTONOSUPPORT;
614 sock->state = SS_UNCONNECTED;
616 switch (sock->type) {
618 sock->ops = &unix_stream_ops;
621 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
625 sock->type=SOCK_DGRAM;
627 sock->ops = &unix_dgram_ops;
630 sock->ops = &unix_seqpacket_ops;
633 return -ESOCKTNOSUPPORT;
636 return unix_create1(sock) ? 0 : -ENOMEM;
639 static int unix_release(struct socket *sock)
641 struct sock *sk = sock->sk;
648 return unix_release_sock (sk, 0);
651 static int unix_autobind(struct socket *sock)
653 struct sock *sk = sock->sk;
654 struct unix_sock *u = unix_sk(sk);
655 static u32 ordernum = 1;
656 struct unix_address * addr;
659 mutex_lock(&u->readlock);
666 addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
670 addr->name->sun_family = AF_UNIX;
671 atomic_set(&addr->refcnt, 1);
674 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
675 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
677 spin_lock(&unix_table_lock);
678 ordernum = (ordernum+1)&0xFFFFF;
680 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
682 spin_unlock(&unix_table_lock);
683 /* Sanity yield. It is unusual case, but yet... */
684 if (!(ordernum&0xFF))
688 addr->hash ^= sk->sk_type;
690 __unix_remove_socket(sk);
692 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
693 spin_unlock(&unix_table_lock);
696 out: mutex_unlock(&u->readlock);
700 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
701 int type, unsigned hash, int *error)
707 if (sunname->sun_path[0]) {
708 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
711 err = vfs_permission(&nd, MAY_WRITE);
716 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
718 u=unix_find_socket_byinode(nd.dentry->d_inode);
722 if (u->sk_type == type)
723 touch_atime(nd.mnt, nd.dentry);
728 if (u->sk_type != type) {
734 u=unix_find_socket_byname(sunname, len, type, hash);
736 struct dentry *dentry;
737 dentry = unix_sk(u)->dentry;
739 touch_atime(unix_sk(u)->mnt, dentry);
753 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
755 struct sock *sk = sock->sk;
756 struct unix_sock *u = unix_sk(sk);
757 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
758 struct dentry * dentry = NULL;
762 struct unix_address *addr;
763 struct hlist_head *list;
766 if (sunaddr->sun_family != AF_UNIX)
769 if (addr_len==sizeof(short)) {
770 err = unix_autobind(sock);
774 err = unix_mkname(sunaddr, addr_len, &hash);
779 mutex_lock(&u->readlock);
786 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
790 memcpy(addr->name, sunaddr, addr_len);
791 addr->len = addr_len;
792 addr->hash = hash ^ sk->sk_type;
793 atomic_set(&addr->refcnt, 1);
795 if (sunaddr->sun_path[0]) {
799 * Get the parent directory, calculate the hash for last
802 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
804 goto out_mknod_parent;
806 dentry = lookup_create(&nd, 0);
807 err = PTR_ERR(dentry);
809 goto out_mknod_unlock;
812 * All right, let's create it.
815 (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
816 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
819 mutex_unlock(&nd.dentry->d_inode->i_mutex);
823 addr->hash = UNIX_HASH_SIZE;
826 spin_lock(&unix_table_lock);
828 if (!sunaddr->sun_path[0]) {
830 if (__unix_find_socket_byname(sunaddr, addr_len,
831 sk->sk_type, hash)) {
832 unix_release_addr(addr);
836 list = &unix_socket_table[addr->hash];
838 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
839 u->dentry = nd.dentry;
844 __unix_remove_socket(sk);
846 __unix_insert_socket(list, sk);
849 spin_unlock(&unix_table_lock);
851 mutex_unlock(&u->readlock);
858 mutex_unlock(&nd.dentry->d_inode->i_mutex);
863 unix_release_addr(addr);
867 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
870 struct sock *sk = sock->sk;
871 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
876 if (addr->sa_family != AF_UNSPEC) {
877 err = unix_mkname(sunaddr, alen, &hash);
882 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
883 !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
886 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
890 unix_state_wlock(sk);
893 if (!unix_may_send(sk, other))
896 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
902 * 1003.1g breaking connected state with AF_UNSPEC
905 unix_state_wlock(sk);
909 * If it was connected, reconnect.
912 struct sock *old_peer = unix_peer(sk);
914 unix_state_wunlock(sk);
916 if (other != old_peer)
917 unix_dgram_disconnected(sk, old_peer);
921 unix_state_wunlock(sk);
926 unix_state_wunlock(sk);
932 static long unix_wait_for_peer(struct sock *other, long timeo)
934 struct unix_sock *u = unix_sk(other);
938 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
940 sched = !sock_flag(other, SOCK_DEAD) &&
941 !(other->sk_shutdown & RCV_SHUTDOWN) &&
942 (skb_queue_len(&other->sk_receive_queue) >
943 other->sk_max_ack_backlog);
945 unix_state_runlock(other);
948 timeo = schedule_timeout(timeo);
950 finish_wait(&u->peer_wait, &wait);
954 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
955 int addr_len, int flags)
957 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
958 struct sock *sk = sock->sk;
959 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
960 struct sock *newsk = NULL;
961 struct sock *other = NULL;
962 struct sk_buff *skb = NULL;
968 err = unix_mkname(sunaddr, addr_len, &hash);
973 if (test_bit(SOCK_PASSCRED, &sock->flags)
974 && !u->addr && (err = unix_autobind(sock)) != 0)
977 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
979 /* First of all allocate resources.
980 If we will make it after state is locked,
981 we will have to recheck all again in any case.
986 /* create new sock for complete connection */
987 newsk = unix_create1(NULL);
991 /* Allocate skb for sending to listening sock */
992 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
997 /* Find listening sock. */
998 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
1002 /* Latch state of peer */
1003 unix_state_rlock(other);
1005 /* Apparently VFS overslept socket death. Retry. */
1006 if (sock_flag(other, SOCK_DEAD)) {
1007 unix_state_runlock(other);
1012 err = -ECONNREFUSED;
1013 if (other->sk_state != TCP_LISTEN)
1016 if (skb_queue_len(&other->sk_receive_queue) >
1017 other->sk_max_ack_backlog) {
1022 timeo = unix_wait_for_peer(other, timeo);
1024 err = sock_intr_errno(timeo);
1025 if (signal_pending(current))
1033 It is tricky place. We need to grab write lock and cannot
1034 drop lock on peer. It is dangerous because deadlock is
1035 possible. Connect to self case and simultaneous
1036 attempt to connect are eliminated by checking socket
1037 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1038 check this before attempt to grab lock.
1040 Well, and we have to recheck the state after socket locked.
1046 /* This is ok... continue with connect */
1048 case TCP_ESTABLISHED:
1049 /* Socket is already connected */
1057 unix_state_wlock_nested(sk);
1059 if (sk->sk_state != st) {
1060 unix_state_wunlock(sk);
1061 unix_state_runlock(other);
1066 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1068 unix_state_wunlock(sk);
1072 /* The way is open! Fastly set all the necessary fields... */
1075 unix_peer(newsk) = sk;
1076 newsk->sk_state = TCP_ESTABLISHED;
1077 newsk->sk_type = sk->sk_type;
1078 newsk->sk_peercred.pid = current->tgid;
1079 newsk->sk_peercred.uid = current->euid;
1080 newsk->sk_peercred.gid = current->egid;
1081 newu = unix_sk(newsk);
1082 newsk->sk_sleep = &newu->peer_wait;
1083 otheru = unix_sk(other);
1085 /* copy address information from listening to new sock*/
1087 atomic_inc(&otheru->addr->refcnt);
1088 newu->addr = otheru->addr;
1090 if (otheru->dentry) {
1091 newu->dentry = dget(otheru->dentry);
1092 newu->mnt = mntget(otheru->mnt);
1095 /* Set credentials */
1096 sk->sk_peercred = other->sk_peercred;
1098 sock->state = SS_CONNECTED;
1099 sk->sk_state = TCP_ESTABLISHED;
1102 smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */
1103 unix_peer(sk) = newsk;
1105 unix_state_wunlock(sk);
1107 /* take ten and and send info to listening sock */
1108 spin_lock(&other->sk_receive_queue.lock);
1109 __skb_queue_tail(&other->sk_receive_queue, skb);
1110 /* Undo artificially decreased inflight after embrion
1111 * is installed to listening socket. */
1112 atomic_inc(&newu->inflight);
1113 spin_unlock(&other->sk_receive_queue.lock);
1114 unix_state_runlock(other);
1115 other->sk_data_ready(other, 0);
1121 unix_state_runlock(other);
1127 unix_release_sock(newsk, 0);
1133 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1135 struct sock *ska=socka->sk, *skb = sockb->sk;
1137 /* Join our sockets back to back */
1142 ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
1143 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1144 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1146 if (ska->sk_type != SOCK_DGRAM) {
1147 ska->sk_state = TCP_ESTABLISHED;
1148 skb->sk_state = TCP_ESTABLISHED;
1149 socka->state = SS_CONNECTED;
1150 sockb->state = SS_CONNECTED;
1155 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1157 struct sock *sk = sock->sk;
1159 struct sk_buff *skb;
1163 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1167 if (sk->sk_state != TCP_LISTEN)
1170 /* If socket state is TCP_LISTEN it cannot change (for now...),
1171 * so that no locks are necessary.
1174 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1176 /* This means receive shutdown. */
1183 skb_free_datagram(sk, skb);
1184 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1186 /* attach accepted sock to socket */
1187 unix_state_wlock(tsk);
1188 newsock->state = SS_CONNECTED;
1189 sock_graft(tsk, newsock);
1190 unix_state_wunlock(tsk);
1198 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1200 struct sock *sk = sock->sk;
1201 struct unix_sock *u;
1202 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1206 sk = unix_peer_get(sk);
1217 unix_state_rlock(sk);
1219 sunaddr->sun_family = AF_UNIX;
1220 sunaddr->sun_path[0] = 0;
1221 *uaddr_len = sizeof(short);
1223 struct unix_address *addr = u->addr;
1225 *uaddr_len = addr->len;
1226 memcpy(sunaddr, addr->name, *uaddr_len);
1228 unix_state_runlock(sk);
1234 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1238 scm->fp = UNIXCB(skb).fp;
1239 skb->destructor = sock_wfree;
1240 UNIXCB(skb).fp = NULL;
1242 for (i=scm->fp->count-1; i>=0; i--)
1243 unix_notinflight(scm->fp->fp[i]);
1246 static void unix_destruct_fds(struct sk_buff *skb)
1248 struct scm_cookie scm;
1249 memset(&scm, 0, sizeof(scm));
1250 unix_detach_fds(&scm, skb);
1252 /* Alas, it calls VFS */
1253 /* So fscking what? fput() had been SMP-safe since the last Summer */
1258 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1261 for (i=scm->fp->count-1; i>=0; i--)
1262 unix_inflight(scm->fp->fp[i]);
1263 UNIXCB(skb).fp = scm->fp;
1264 skb->destructor = unix_destruct_fds;
1269 * Send AF_UNIX data.
1272 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1273 struct msghdr *msg, size_t len)
1275 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1276 struct sock *sk = sock->sk;
1277 struct unix_sock *u = unix_sk(sk);
1278 struct sockaddr_un *sunaddr=msg->msg_name;
1279 struct sock *other = NULL;
1280 int namelen = 0; /* fake GCC */
1283 struct sk_buff *skb;
1285 struct scm_cookie tmp_scm;
1287 if (NULL == siocb->scm)
1288 siocb->scm = &tmp_scm;
1289 err = scm_send(sock, msg, siocb->scm);
1294 if (msg->msg_flags&MSG_OOB)
1297 if (msg->msg_namelen) {
1298 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1305 other = unix_peer_get(sk);
1310 if (test_bit(SOCK_PASSCRED, &sock->flags)
1311 && !u->addr && (err = unix_autobind(sock)) != 0)
1315 if (len > sk->sk_sndbuf - 32)
1318 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1322 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1324 unix_attach_fds(siocb->scm, skb);
1326 unix_get_peersec_dgram(skb);
1328 skb->h.raw = skb->data;
1329 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1333 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1338 if (sunaddr == NULL)
1341 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1347 unix_state_rlock(other);
1349 if (!unix_may_send(sk, other))
1352 if (sock_flag(other, SOCK_DEAD)) {
1354 * Check with 1003.1g - what should
1357 unix_state_runlock(other);
1361 unix_state_wlock(sk);
1362 if (unix_peer(sk) == other) {
1364 unix_state_wunlock(sk);
1366 unix_dgram_disconnected(sk, other);
1368 err = -ECONNREFUSED;
1370 unix_state_wunlock(sk);
1380 if (other->sk_shutdown & RCV_SHUTDOWN)
1383 if (sk->sk_type != SOCK_SEQPACKET) {
1384 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1389 if (unix_peer(other) != sk &&
1390 (skb_queue_len(&other->sk_receive_queue) >
1391 other->sk_max_ack_backlog)) {
1397 timeo = unix_wait_for_peer(other, timeo);
1399 err = sock_intr_errno(timeo);
1400 if (signal_pending(current))
1406 skb_queue_tail(&other->sk_receive_queue, skb);
1407 unix_state_runlock(other);
1408 other->sk_data_ready(other, len);
1410 scm_destroy(siocb->scm);
1414 unix_state_runlock(other);
1420 scm_destroy(siocb->scm);
1425 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1426 struct msghdr *msg, size_t len)
1428 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1429 struct sock *sk = sock->sk;
1430 struct sock *other = NULL;
1431 struct sockaddr_un *sunaddr=msg->msg_name;
1433 struct sk_buff *skb;
1435 struct scm_cookie tmp_scm;
1437 if (NULL == siocb->scm)
1438 siocb->scm = &tmp_scm;
1439 err = scm_send(sock, msg, siocb->scm);
1444 if (msg->msg_flags&MSG_OOB)
1447 if (msg->msg_namelen) {
1448 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1453 other = unix_peer(sk);
1458 if (sk->sk_shutdown & SEND_SHUTDOWN)
1464 * Optimisation for the fact that under 0.01% of X
1465 * messages typically need breaking up.
1470 /* Keep two messages in the pipe so it schedules better */
1471 if (size > ((sk->sk_sndbuf >> 1) - 64))
1472 size = (sk->sk_sndbuf >> 1) - 64;
1474 if (size > SKB_MAX_ALLOC)
1475 size = SKB_MAX_ALLOC;
1481 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1487 * If you pass two values to the sock_alloc_send_skb
1488 * it tries to grab the large buffer with GFP_NOFS
1489 * (which can fail easily), and if it fails grab the
1490 * fallback size buffer which is under a page and will
1493 size = min_t(int, size, skb_tailroom(skb));
1495 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1497 unix_attach_fds(siocb->scm, skb);
1499 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1504 unix_state_rlock(other);
1506 if (sock_flag(other, SOCK_DEAD) ||
1507 (other->sk_shutdown & RCV_SHUTDOWN))
1510 skb_queue_tail(&other->sk_receive_queue, skb);
1511 unix_state_runlock(other);
1512 other->sk_data_ready(other, size);
1516 scm_destroy(siocb->scm);
1522 unix_state_runlock(other);
1525 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1526 send_sig(SIGPIPE,current,0);
1529 scm_destroy(siocb->scm);
1531 return sent ? : err;
1534 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1535 struct msghdr *msg, size_t len)
1538 struct sock *sk = sock->sk;
1540 err = sock_error(sk);
1544 if (sk->sk_state != TCP_ESTABLISHED)
1547 if (msg->msg_namelen)
1548 msg->msg_namelen = 0;
1550 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1553 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1555 struct unix_sock *u = unix_sk(sk);
1557 msg->msg_namelen = 0;
1559 msg->msg_namelen = u->addr->len;
1560 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1564 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1565 struct msghdr *msg, size_t size,
1568 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1569 struct scm_cookie tmp_scm;
1570 struct sock *sk = sock->sk;
1571 struct unix_sock *u = unix_sk(sk);
1572 int noblock = flags & MSG_DONTWAIT;
1573 struct sk_buff *skb;
1580 msg->msg_namelen = 0;
1582 mutex_lock(&u->readlock);
1584 skb = skb_recv_datagram(sk, flags, noblock, &err);
1588 wake_up_interruptible(&u->peer_wait);
1591 unix_copy_addr(msg, skb->sk);
1593 if (size > skb->len)
1595 else if (size < skb->len)
1596 msg->msg_flags |= MSG_TRUNC;
1598 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1603 siocb->scm = &tmp_scm;
1604 memset(&tmp_scm, 0, sizeof(tmp_scm));
1606 siocb->scm->creds = *UNIXCREDS(skb);
1607 unix_set_secdata(siocb->scm, skb);
1609 if (!(flags & MSG_PEEK))
1612 unix_detach_fds(siocb->scm, skb);
1616 /* It is questionable: on PEEK we could:
1617 - do not return fds - good, but too simple 8)
1618 - return fds, and do not return them on read (old strategy,
1620 - clone fds (I chose it for now, it is the most universal
1623 POSIX 1003.1g does not actually define this clearly
1624 at all. POSIX 1003.1g doesn't define a lot of things
1629 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1633 scm_recv(sock, msg, siocb->scm, flags);
1636 skb_free_datagram(sk,skb);
1638 mutex_unlock(&u->readlock);
1644 * Sleep until data has arrive. But check for races..
1647 static long unix_stream_data_wait(struct sock * sk, long timeo)
1651 unix_state_rlock(sk);
1654 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1656 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1658 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1659 signal_pending(current) ||
1663 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1664 unix_state_runlock(sk);
1665 timeo = schedule_timeout(timeo);
1666 unix_state_rlock(sk);
1667 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1670 finish_wait(sk->sk_sleep, &wait);
1671 unix_state_runlock(sk);
1677 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1678 struct msghdr *msg, size_t size,
1681 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1682 struct scm_cookie tmp_scm;
1683 struct sock *sk = sock->sk;
1684 struct unix_sock *u = unix_sk(sk);
1685 struct sockaddr_un *sunaddr=msg->msg_name;
1687 int check_creds = 0;
1693 if (sk->sk_state != TCP_ESTABLISHED)
1700 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1701 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1703 msg->msg_namelen = 0;
1705 /* Lock the socket to prevent queue disordering
1706 * while sleeps in memcpy_tomsg
1710 siocb->scm = &tmp_scm;
1711 memset(&tmp_scm, 0, sizeof(tmp_scm));
1714 mutex_lock(&u->readlock);
1719 struct sk_buff *skb;
1721 skb = skb_dequeue(&sk->sk_receive_queue);
1724 if (copied >= target)
1728 * POSIX 1003.1g mandates this order.
1731 if ((err = sock_error(sk)) != 0)
1733 if (sk->sk_shutdown & RCV_SHUTDOWN)
1738 mutex_unlock(&u->readlock);
1740 timeo = unix_stream_data_wait(sk, timeo);
1742 if (signal_pending(current)) {
1743 err = sock_intr_errno(timeo);
1746 mutex_lock(&u->readlock);
1751 /* Never glue messages from different writers */
1752 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1753 skb_queue_head(&sk->sk_receive_queue, skb);
1757 /* Copy credentials */
1758 siocb->scm->creds = *UNIXCREDS(skb);
1762 /* Copy address just once */
1765 unix_copy_addr(msg, skb->sk);
1769 chunk = min_t(unsigned int, skb->len, size);
1770 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1771 skb_queue_head(&sk->sk_receive_queue, skb);
1779 /* Mark read part of skb as used */
1780 if (!(flags & MSG_PEEK))
1782 skb_pull(skb, chunk);
1785 unix_detach_fds(siocb->scm, skb);
1787 /* put the skb back if we didn't use it up.. */
1790 skb_queue_head(&sk->sk_receive_queue, skb);
1801 /* It is questionable, see note in unix_dgram_recvmsg.
1804 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1806 /* put message back and return */
1807 skb_queue_head(&sk->sk_receive_queue, skb);
1812 mutex_unlock(&u->readlock);
1813 scm_recv(sock, msg, siocb->scm, flags);
1815 return copied ? : err;
1818 static int unix_shutdown(struct socket *sock, int mode)
1820 struct sock *sk = sock->sk;
1823 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1826 unix_state_wlock(sk);
1827 sk->sk_shutdown |= mode;
1828 other=unix_peer(sk);
1831 unix_state_wunlock(sk);
1832 sk->sk_state_change(sk);
1835 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1839 if (mode&RCV_SHUTDOWN)
1840 peer_mode |= SEND_SHUTDOWN;
1841 if (mode&SEND_SHUTDOWN)
1842 peer_mode |= RCV_SHUTDOWN;
1843 unix_state_wlock(other);
1844 other->sk_shutdown |= peer_mode;
1845 unix_state_wunlock(other);
1846 other->sk_state_change(other);
1847 read_lock(&other->sk_callback_lock);
1848 if (peer_mode == SHUTDOWN_MASK)
1849 sk_wake_async(other,1,POLL_HUP);
1850 else if (peer_mode & RCV_SHUTDOWN)
1851 sk_wake_async(other,1,POLL_IN);
1852 read_unlock(&other->sk_callback_lock);
1860 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1862 struct sock *sk = sock->sk;
1869 amount = atomic_read(&sk->sk_wmem_alloc);
1870 err = put_user(amount, (int __user *)arg);
1874 struct sk_buff *skb;
1876 if (sk->sk_state == TCP_LISTEN) {
1881 spin_lock(&sk->sk_receive_queue.lock);
1882 if (sk->sk_type == SOCK_STREAM ||
1883 sk->sk_type == SOCK_SEQPACKET) {
1884 skb_queue_walk(&sk->sk_receive_queue, skb)
1887 skb = skb_peek(&sk->sk_receive_queue);
1891 spin_unlock(&sk->sk_receive_queue.lock);
1892 err = put_user(amount, (int __user *)arg);
1903 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1905 struct sock *sk = sock->sk;
1908 poll_wait(file, sk->sk_sleep, wait);
1911 /* exceptional events? */
1914 if (sk->sk_shutdown == SHUTDOWN_MASK)
1916 if (sk->sk_shutdown & RCV_SHUTDOWN)
1920 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1921 (sk->sk_shutdown & RCV_SHUTDOWN))
1922 mask |= POLLIN | POLLRDNORM;
1924 /* Connection-based need to check for termination and startup */
1925 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1929 * we set writable also when the other side has shut down the
1930 * connection. This prevents stuck sockets.
1932 if (unix_writable(sk))
1933 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1939 #ifdef CONFIG_PROC_FS
1940 static struct sock *unix_seq_idx(int *iter, loff_t pos)
1945 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
1954 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
1956 spin_lock(&unix_table_lock);
1957 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
1960 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1965 return first_unix_socket(seq->private);
1966 return next_unix_socket(seq->private, v);
1969 static void unix_seq_stop(struct seq_file *seq, void *v)
1971 spin_unlock(&unix_table_lock);
1974 static int unix_seq_show(struct seq_file *seq, void *v)
1978 seq_puts(seq, "Num RefCount Protocol Flags Type St "
1982 struct unix_sock *u = unix_sk(s);
1983 unix_state_rlock(s);
1985 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
1987 atomic_read(&s->sk_refcnt),
1989 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
1992 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
1993 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
2001 len = u->addr->len - sizeof(short);
2002 if (!UNIX_ABSTRACT(s))
2008 for ( ; i < len; i++)
2009 seq_putc(seq, u->addr->name->sun_path[i]);
2011 unix_state_runlock(s);
2012 seq_putc(seq, '\n');
2018 static struct seq_operations unix_seq_ops = {
2019 .start = unix_seq_start,
2020 .next = unix_seq_next,
2021 .stop = unix_seq_stop,
2022 .show = unix_seq_show,
2026 static int unix_seq_open(struct inode *inode, struct file *file)
2028 struct seq_file *seq;
2030 int *iter = kmalloc(sizeof(int), GFP_KERNEL);
2035 rc = seq_open(file, &unix_seq_ops);
2039 seq = file->private_data;
2040 seq->private = iter;
2049 static struct file_operations unix_seq_fops = {
2050 .owner = THIS_MODULE,
2051 .open = unix_seq_open,
2053 .llseek = seq_lseek,
2054 .release = seq_release_private,
2059 static struct net_proto_family unix_family_ops = {
2061 .create = unix_create,
2062 .owner = THIS_MODULE,
2065 static int __init af_unix_init(void)
2068 struct sk_buff *dummy_skb;
2070 if (sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb)) {
2071 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
2075 rc = proto_register(&unix_proto, 1);
2077 printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n",
2082 sock_register(&unix_family_ops);
2083 #ifdef CONFIG_PROC_FS
2084 proc_net_fops_create("unix", 0, &unix_seq_fops);
2086 unix_sysctl_register();
2091 static void __exit af_unix_exit(void)
2093 sock_unregister(PF_UNIX);
2094 unix_sysctl_unregister();
2095 proc_net_remove("unix");
2096 proto_unregister(&unix_proto);
2099 module_init(af_unix_init);
2100 module_exit(af_unix_exit);
2102 MODULE_LICENSE("GPL");
2103 MODULE_ALIAS_NETPROTO(PF_UNIX);