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>
121 #include <linux/vs_context.h>
122 #include <linux/vs_network.h>
123 #include <linux/vs_limit.h>
125 int sysctl_unix_max_dgram_qlen = 10;
127 kmem_cache_t *unix_sk_cachep;
129 struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
130 rwlock_t unix_table_lock = RW_LOCK_UNLOCKED;
131 static atomic_t unix_nr_socks = ATOMIC_INIT(0);
133 #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE])
135 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE)
138 * SMP locking strategy:
139 * hash table is protected with rwlock unix_table_lock
140 * each socket state is protected by separate rwlock.
143 static inline unsigned unix_hash_fold(unsigned hash)
147 return hash&(UNIX_HASH_SIZE-1);
150 #define unix_peer(sk) (unix_sk(sk)->peer)
152 static inline int unix_our_peer(struct sock *sk, struct sock *osk)
154 return unix_peer(osk) == sk;
157 static inline int unix_may_send(struct sock *sk, struct sock *osk)
159 return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
162 static struct sock *unix_peer_get(struct sock *s)
170 unix_state_runlock(s);
174 static inline void unix_release_addr(struct unix_address *addr)
176 if (atomic_dec_and_test(&addr->refcnt))
181 * Check unix socket name:
182 * - should be not zero length.
183 * - if started by not zero, should be NULL terminated (FS object)
184 * - if started by zero, it is abstract name.
187 static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
189 if (len <= sizeof(short) || len > sizeof(*sunaddr))
191 if (!sunaddr || sunaddr->sun_family != AF_UNIX)
193 if (sunaddr->sun_path[0]) {
194 ((char *)sunaddr)[len]=0;
195 len = strlen(sunaddr->sun_path)+1+sizeof(short);
199 *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
203 static void __unix_remove_socket(struct sock *sk)
205 sk_del_node_init(sk);
208 static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
210 BUG_TRAP(sk_unhashed(sk));
211 sk_add_node(sk, list);
214 static inline void unix_remove_socket(struct sock *sk)
216 write_lock(&unix_table_lock);
217 __unix_remove_socket(sk);
218 write_unlock(&unix_table_lock);
221 static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
223 write_lock(&unix_table_lock);
224 __unix_insert_socket(list, sk);
225 write_unlock(&unix_table_lock);
228 static struct sock *__unix_find_socket_byname(struct sockaddr_un *sunname,
229 int len, int type, unsigned hash)
232 struct hlist_node *node;
234 sk_for_each(s, node, &unix_socket_table[hash ^ type]) {
235 struct unix_sock *u = unix_sk(s);
237 if (u->addr->len == len &&
238 !memcmp(u->addr->name, sunname, len))
246 static inline struct sock *unix_find_socket_byname(struct sockaddr_un *sunname,
252 read_lock(&unix_table_lock);
253 s = __unix_find_socket_byname(sunname, len, type, hash);
256 read_unlock(&unix_table_lock);
260 static struct sock *unix_find_socket_byinode(struct inode *i)
263 struct hlist_node *node;
265 read_lock(&unix_table_lock);
267 &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) {
268 struct dentry *dentry = unix_sk(s)->dentry;
270 if(dentry && dentry->d_inode == i)
278 read_unlock(&unix_table_lock);
282 static inline int unix_writable(struct sock *sk)
284 return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
287 static void unix_write_space(struct sock *sk)
289 read_lock(&sk->sk_callback_lock);
290 if (unix_writable(sk)) {
291 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
292 wake_up_interruptible(sk->sk_sleep);
293 sk_wake_async(sk, 2, POLL_OUT);
295 read_unlock(&sk->sk_callback_lock);
298 /* When dgram socket disconnects (or changes its peer), we clear its receive
299 * queue of packets arrived from previous peer. First, it allows to do
300 * flow control based only on wmem_alloc; second, sk connected to peer
301 * may receive messages only from that peer. */
302 static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
304 if (skb_queue_len(&sk->sk_receive_queue)) {
305 skb_queue_purge(&sk->sk_receive_queue);
306 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
308 /* If one link of bidirectional dgram pipe is disconnected,
309 * we signal error. Messages are lost. Do not make this,
310 * when peer was not connected to us.
312 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
313 other->sk_err = ECONNRESET;
314 other->sk_error_report(other);
319 static void unix_sock_destructor(struct sock *sk)
321 struct unix_sock *u = unix_sk(sk);
323 skb_queue_purge(&sk->sk_receive_queue);
325 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
326 BUG_TRAP(sk_unhashed(sk));
327 BUG_TRAP(!sk->sk_socket);
328 if (!sock_flag(sk, SOCK_DEAD)) {
329 printk("Attempt to release alive unix socket: %p\n", sk);
334 unix_release_addr(u->addr);
336 atomic_dec(&unix_nr_socks);
337 #ifdef UNIX_REFCNT_DEBUG
338 printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
342 static int unix_release_sock (struct sock *sk, int embrion)
344 struct unix_sock *u = unix_sk(sk);
345 struct dentry *dentry;
346 struct vfsmount *mnt;
351 unix_remove_socket(sk);
354 unix_state_wlock(sk);
356 sk->sk_shutdown = SHUTDOWN_MASK;
361 state = sk->sk_state;
362 sk->sk_state = TCP_CLOSE;
363 unix_state_wunlock(sk);
365 wake_up_interruptible_all(&u->peer_wait);
367 skpair=unix_peer(sk);
370 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
371 unix_state_wlock(skpair);
373 skpair->sk_shutdown = SHUTDOWN_MASK;
374 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
375 skpair->sk_err = ECONNRESET;
376 unix_state_wunlock(skpair);
377 skpair->sk_state_change(skpair);
378 read_lock(&skpair->sk_callback_lock);
379 sk_wake_async(skpair,1,POLL_HUP);
380 read_unlock(&skpair->sk_callback_lock);
382 sock_put(skpair); /* It may now die */
383 unix_peer(sk) = NULL;
386 /* Try to flush out this socket. Throw out buffers at least */
388 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
389 if (state==TCP_LISTEN)
390 unix_release_sock(skb->sk, 1);
391 /* passed fds are erased in the kfree_skb hook */
401 clr_vx_info(&sk->sk_vx_info);
402 clr_nx_info(&sk->sk_nx_info);
405 /* ---- Socket is dead now and most probably destroyed ---- */
408 * Fixme: BSD difference: In BSD all sockets connected to use get
409 * ECONNRESET and we die on the spot. In Linux we behave
410 * like files and pipes do and wait for the last
413 * Can't we simply set sock->err?
415 * What the above comment does talk about? --ANK(980817)
418 if (atomic_read(&unix_tot_inflight))
419 unix_gc(); /* Garbage collect fds */
424 static int unix_listen(struct socket *sock, int backlog)
427 struct sock *sk = sock->sk;
428 struct unix_sock *u = unix_sk(sk);
431 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
432 goto out; /* Only stream/seqpacket sockets accept */
435 goto out; /* No listens on an unbound socket */
436 unix_state_wlock(sk);
437 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
439 if (backlog > sk->sk_max_ack_backlog)
440 wake_up_interruptible_all(&u->peer_wait);
441 sk->sk_max_ack_backlog = backlog;
442 sk->sk_state = TCP_LISTEN;
443 /* set credentials so connect can copy them */
444 sk->sk_peercred.pid = current->tgid;
445 sk->sk_peercred.uid = current->euid;
446 sk->sk_peercred.gid = current->egid;
450 unix_state_wunlock(sk);
455 static int unix_release(struct socket *);
456 static int unix_bind(struct socket *, struct sockaddr *, int);
457 static int unix_stream_connect(struct socket *, struct sockaddr *,
458 int addr_len, int flags);
459 static int unix_socketpair(struct socket *, struct socket *);
460 static int unix_accept(struct socket *, struct socket *, int);
461 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
462 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
463 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
464 static int unix_shutdown(struct socket *, int);
465 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
466 struct msghdr *, size_t);
467 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
468 struct msghdr *, size_t, int);
469 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
470 struct msghdr *, size_t);
471 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
472 struct msghdr *, size_t, int);
473 static int unix_dgram_connect(struct socket *, struct sockaddr *,
475 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
476 struct msghdr *, size_t);
478 static struct proto_ops unix_stream_ops = {
480 .owner = THIS_MODULE,
481 .release = unix_release,
483 .connect = unix_stream_connect,
484 .socketpair = unix_socketpair,
485 .accept = unix_accept,
486 .getname = unix_getname,
489 .listen = unix_listen,
490 .shutdown = unix_shutdown,
491 .setsockopt = sock_no_setsockopt,
492 .getsockopt = sock_no_getsockopt,
493 .sendmsg = unix_stream_sendmsg,
494 .recvmsg = unix_stream_recvmsg,
495 .mmap = sock_no_mmap,
496 .sendpage = sock_no_sendpage,
499 static struct proto_ops unix_dgram_ops = {
501 .owner = THIS_MODULE,
502 .release = unix_release,
504 .connect = unix_dgram_connect,
505 .socketpair = unix_socketpair,
506 .accept = sock_no_accept,
507 .getname = unix_getname,
508 .poll = datagram_poll,
510 .listen = sock_no_listen,
511 .shutdown = unix_shutdown,
512 .setsockopt = sock_no_setsockopt,
513 .getsockopt = sock_no_getsockopt,
514 .sendmsg = unix_dgram_sendmsg,
515 .recvmsg = unix_dgram_recvmsg,
516 .mmap = sock_no_mmap,
517 .sendpage = sock_no_sendpage,
520 static struct proto_ops unix_seqpacket_ops = {
522 .owner = THIS_MODULE,
523 .release = unix_release,
525 .connect = unix_stream_connect,
526 .socketpair = unix_socketpair,
527 .accept = unix_accept,
528 .getname = unix_getname,
529 .poll = datagram_poll,
531 .listen = unix_listen,
532 .shutdown = unix_shutdown,
533 .setsockopt = sock_no_setsockopt,
534 .getsockopt = sock_no_getsockopt,
535 .sendmsg = unix_seqpacket_sendmsg,
536 .recvmsg = unix_dgram_recvmsg,
537 .mmap = sock_no_mmap,
538 .sendpage = sock_no_sendpage,
541 static struct sock * unix_create1(struct socket *sock)
543 struct sock *sk = NULL;
546 if (atomic_read(&unix_nr_socks) >= 2*files_stat.max_files)
549 sk = sk_alloc(PF_UNIX, GFP_KERNEL, sizeof(struct unix_sock),
554 atomic_inc(&unix_nr_socks);
556 sock_init_data(sock,sk);
557 sk_set_owner(sk, THIS_MODULE);
559 set_vx_info(&sk->sk_vx_info, current->vx_info);
560 sk->sk_xid = vx_current_xid();
562 set_nx_info(&sk->sk_nx_info, current->nx_info);
564 sk->sk_write_space = unix_write_space;
565 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
566 sk->sk_destruct = unix_sock_destructor;
570 rwlock_init(&u->lock);
571 atomic_set(&u->inflight, sock ? 0 : -1);
572 init_MUTEX(&u->readsem); /* single task reading lock */
573 init_waitqueue_head(&u->peer_wait);
574 unix_insert_socket(unix_sockets_unbound, sk);
579 static int unix_create(struct socket *sock, int protocol)
581 if (protocol && protocol != PF_UNIX)
582 return -EPROTONOSUPPORT;
584 sock->state = SS_UNCONNECTED;
586 switch (sock->type) {
588 sock->ops = &unix_stream_ops;
591 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
595 sock->type=SOCK_DGRAM;
597 sock->ops = &unix_dgram_ops;
600 sock->ops = &unix_seqpacket_ops;
603 return -ESOCKTNOSUPPORT;
606 return unix_create1(sock) ? 0 : -ENOMEM;
609 static int unix_release(struct socket *sock)
611 struct sock *sk = sock->sk;
618 return unix_release_sock (sk, 0);
621 static int unix_autobind(struct socket *sock)
623 struct sock *sk = sock->sk;
624 struct unix_sock *u = unix_sk(sk);
625 static u32 ordernum = 1;
626 struct unix_address * addr;
636 addr = kmalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
640 memset(addr, 0, sizeof(*addr) + sizeof(short) + 16);
641 addr->name->sun_family = AF_UNIX;
642 atomic_set(&addr->refcnt, 1);
645 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
646 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
648 write_lock(&unix_table_lock);
649 ordernum = (ordernum+1)&0xFFFFF;
651 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
653 write_unlock(&unix_table_lock);
654 /* Sanity yield. It is unusual case, but yet... */
655 if (!(ordernum&0xFF))
659 addr->hash ^= sk->sk_type;
661 __unix_remove_socket(sk);
663 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
664 write_unlock(&unix_table_lock);
667 out: up(&u->readsem);
671 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
672 int type, unsigned hash, int *error)
678 if (sunname->sun_path[0]) {
679 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
682 err = permission(nd.dentry->d_inode,MAY_WRITE, &nd);
687 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
689 u=unix_find_socket_byinode(nd.dentry->d_inode);
693 if (u->sk_type == type)
694 touch_atime(nd.mnt, nd.dentry);
699 if (u->sk_type != type) {
705 u=unix_find_socket_byname(sunname, len, type, hash);
707 struct dentry *dentry;
708 dentry = unix_sk(u)->dentry;
710 touch_atime(unix_sk(u)->mnt, dentry);
724 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
726 struct sock *sk = sock->sk;
727 struct unix_sock *u = unix_sk(sk);
728 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
729 struct dentry * dentry = NULL;
733 struct unix_address *addr;
734 struct hlist_head *list;
737 if (sunaddr->sun_family != AF_UNIX)
740 if (addr_len==sizeof(short)) {
741 err = unix_autobind(sock);
745 err = unix_mkname(sunaddr, addr_len, &hash);
757 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
761 memcpy(addr->name, sunaddr, addr_len);
762 addr->len = addr_len;
763 addr->hash = hash ^ sk->sk_type;
764 atomic_set(&addr->refcnt, 1);
766 if (sunaddr->sun_path[0]) {
770 * Get the parent directory, calculate the hash for last
773 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
775 goto out_mknod_parent;
777 * Yucky last component or no last component at all?
778 * (foo/., foo/.., /////)
781 if (nd.last_type != LAST_NORM)
784 * Lock the directory.
786 down(&nd.dentry->d_inode->i_sem);
788 * Do the final lookup.
790 dentry = lookup_hash(&nd.last, nd.dentry);
791 err = PTR_ERR(dentry);
793 goto out_mknod_unlock;
796 * Special case - lookup gave negative, but... we had foo/bar/
797 * From the vfs_mknod() POV we just have a negative dentry -
798 * all is fine. Let's be bastards - you had / on the end, you've
799 * been asking for (non-existent) directory. -ENOENT for you.
801 if (nd.last.name[nd.last.len] && !dentry->d_inode)
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 up(&nd.dentry->d_inode->i_sem);
815 addr->hash = UNIX_HASH_SIZE;
818 write_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 write_unlock(&unix_table_lock);
850 up(&nd.dentry->d_inode->i_sem);
856 unix_release_addr(addr);
860 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
863 struct sock *sk = sock->sk;
864 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
869 if (addr->sa_family != AF_UNSPEC) {
870 err = unix_mkname(sunaddr, alen, &hash);
875 if (test_bit(SOCK_PASS_CRED, &sock->flags) && !unix_sk(sk)->addr &&
876 (err = unix_autobind(sock)) != 0)
879 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
883 unix_state_wlock(sk);
886 if (!unix_may_send(sk, other))
889 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
895 * 1003.1g breaking connected state with AF_UNSPEC
898 unix_state_wlock(sk);
902 * If it was connected, reconnect.
905 struct sock *old_peer = unix_peer(sk);
907 unix_state_wunlock(sk);
909 if (other != old_peer)
910 unix_dgram_disconnected(sk, old_peer);
914 unix_state_wunlock(sk);
919 unix_state_wunlock(sk);
925 static long unix_wait_for_peer(struct sock *other, long timeo)
927 struct unix_sock *u = unix_sk(other);
931 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
933 sched = !sock_flag(other, SOCK_DEAD) &&
934 !(other->sk_shutdown & RCV_SHUTDOWN) &&
935 (skb_queue_len(&other->sk_receive_queue) >
936 other->sk_max_ack_backlog);
938 unix_state_runlock(other);
941 timeo = schedule_timeout(timeo);
943 finish_wait(&u->peer_wait, &wait);
947 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
948 int addr_len, int flags)
950 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
951 struct sock *sk = sock->sk;
952 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
953 struct sock *newsk = NULL;
954 struct sock *other = NULL;
955 struct sk_buff *skb = NULL;
961 err = unix_mkname(sunaddr, addr_len, &hash);
966 if (test_bit(SOCK_PASS_CRED, &sock->flags)
967 && !u->addr && (err = unix_autobind(sock)) != 0)
970 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
972 /* First of all allocate resources.
973 If we will make it after state is locked,
974 we will have to recheck all again in any case.
979 /* create new sock for complete connection */
980 newsk = unix_create1(NULL);
984 /* Allocate skb for sending to listening sock */
985 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
990 /* Find listening sock. */
991 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
995 /* Latch state of peer */
996 unix_state_rlock(other);
998 /* Apparently VFS overslept socket death. Retry. */
999 if (sock_flag(other, SOCK_DEAD)) {
1000 unix_state_runlock(other);
1005 err = -ECONNREFUSED;
1006 if (other->sk_state != TCP_LISTEN)
1009 if (skb_queue_len(&other->sk_receive_queue) >
1010 other->sk_max_ack_backlog) {
1015 timeo = unix_wait_for_peer(other, timeo);
1017 err = sock_intr_errno(timeo);
1018 if (signal_pending(current))
1026 It is tricky place. We need to grab write lock and cannot
1027 drop lock on peer. It is dangerous because deadlock is
1028 possible. Connect to self case and simultaneous
1029 attempt to connect are eliminated by checking socket
1030 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1031 check this before attempt to grab lock.
1033 Well, and we have to recheck the state after socket locked.
1039 /* This is ok... continue with connect */
1041 case TCP_ESTABLISHED:
1042 /* Socket is already connected */
1050 unix_state_wlock(sk);
1052 if (sk->sk_state != st) {
1053 unix_state_wunlock(sk);
1054 unix_state_runlock(other);
1059 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1061 unix_state_wunlock(sk);
1065 /* The way is open! Fastly set all the necessary fields... */
1068 unix_peer(newsk) = sk;
1069 newsk->sk_state = TCP_ESTABLISHED;
1070 newsk->sk_type = sk->sk_type;
1071 newsk->sk_peercred.pid = current->tgid;
1072 newsk->sk_peercred.uid = current->euid;
1073 newsk->sk_peercred.gid = current->egid;
1074 newu = unix_sk(newsk);
1075 newsk->sk_sleep = &newu->peer_wait;
1076 otheru = unix_sk(other);
1078 /* copy address information from listening to new sock*/
1080 atomic_inc(&otheru->addr->refcnt);
1081 newu->addr = otheru->addr;
1083 if (otheru->dentry) {
1084 newu->dentry = dget(otheru->dentry);
1085 newu->mnt = mntget(otheru->mnt);
1088 /* Set credentials */
1089 sk->sk_peercred = other->sk_peercred;
1092 unix_peer(sk) = newsk;
1093 sock->state = SS_CONNECTED;
1094 sk->sk_state = TCP_ESTABLISHED;
1096 unix_state_wunlock(sk);
1098 /* take ten and and send info to listening sock */
1099 spin_lock(&other->sk_receive_queue.lock);
1100 __skb_queue_tail(&other->sk_receive_queue, skb);
1101 /* Undo artificially decreased inflight after embrion
1102 * is installed to listening socket. */
1103 atomic_inc(&newu->inflight);
1104 spin_unlock(&other->sk_receive_queue.lock);
1105 unix_state_runlock(other);
1106 other->sk_data_ready(other, 0);
1112 unix_state_runlock(other);
1118 unix_release_sock(newsk, 0);
1124 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1126 struct sock *ska=socka->sk, *skb = sockb->sk;
1128 /* Join our sockets back to back */
1133 ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
1134 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1135 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1137 if (ska->sk_type != SOCK_DGRAM) {
1138 ska->sk_state = TCP_ESTABLISHED;
1139 skb->sk_state = TCP_ESTABLISHED;
1140 socka->state = SS_CONNECTED;
1141 sockb->state = SS_CONNECTED;
1146 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1148 struct sock *sk = sock->sk;
1150 struct sk_buff *skb;
1154 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1158 if (sk->sk_state != TCP_LISTEN)
1161 /* If socket state is TCP_LISTEN it cannot change (for now...),
1162 * so that no locks are necessary.
1165 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1167 /* This means receive shutdown. */
1174 skb_free_datagram(sk, skb);
1175 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1177 /* attach accepted sock to socket */
1178 unix_state_wlock(tsk);
1179 newsock->state = SS_CONNECTED;
1180 sock_graft(tsk, newsock);
1181 unix_state_wunlock(tsk);
1189 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1191 struct sock *sk = sock->sk;
1192 struct unix_sock *u;
1193 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1197 sk = unix_peer_get(sk);
1208 unix_state_rlock(sk);
1210 sunaddr->sun_family = AF_UNIX;
1211 sunaddr->sun_path[0] = 0;
1212 *uaddr_len = sizeof(short);
1214 struct unix_address *addr = u->addr;
1216 *uaddr_len = addr->len;
1217 memcpy(sunaddr, addr->name, *uaddr_len);
1219 unix_state_runlock(sk);
1225 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1229 scm->fp = UNIXCB(skb).fp;
1230 skb->destructor = sock_wfree;
1231 UNIXCB(skb).fp = NULL;
1233 for (i=scm->fp->count-1; i>=0; i--)
1234 unix_notinflight(scm->fp->fp[i]);
1237 static void unix_destruct_fds(struct sk_buff *skb)
1239 struct scm_cookie scm;
1240 memset(&scm, 0, sizeof(scm));
1241 unix_detach_fds(&scm, skb);
1243 /* Alas, it calls VFS */
1244 /* So fscking what? fput() had been SMP-safe since the last Summer */
1249 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1252 for (i=scm->fp->count-1; i>=0; i--)
1253 unix_inflight(scm->fp->fp[i]);
1254 UNIXCB(skb).fp = scm->fp;
1255 skb->destructor = unix_destruct_fds;
1260 * Send AF_UNIX data.
1263 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1264 struct msghdr *msg, size_t len)
1266 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1267 struct sock *sk = sock->sk;
1268 struct unix_sock *u = unix_sk(sk);
1269 struct sockaddr_un *sunaddr=msg->msg_name;
1270 struct sock *other = NULL;
1271 int namelen = 0; /* fake GCC */
1274 struct sk_buff *skb;
1276 struct scm_cookie tmp_scm;
1278 if (NULL == siocb->scm)
1279 siocb->scm = &tmp_scm;
1280 err = scm_send(sock, msg, siocb->scm);
1285 if (msg->msg_flags&MSG_OOB)
1288 if (msg->msg_namelen) {
1289 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1296 other = unix_peer_get(sk);
1301 if (test_bit(SOCK_PASS_CRED, &sock->flags)
1302 && !u->addr && (err = unix_autobind(sock)) != 0)
1306 if (len > sk->sk_sndbuf - 32)
1309 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1313 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1315 unix_attach_fds(siocb->scm, skb);
1317 skb->h.raw = skb->data;
1318 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1322 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1327 if (sunaddr == NULL)
1330 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1336 unix_state_rlock(other);
1338 if (!unix_may_send(sk, other))
1341 if (sock_flag(other, SOCK_DEAD)) {
1343 * Check with 1003.1g - what should
1346 unix_state_runlock(other);
1350 unix_state_wlock(sk);
1351 if (unix_peer(sk) == other) {
1353 unix_state_wunlock(sk);
1355 unix_dgram_disconnected(sk, other);
1357 err = -ECONNREFUSED;
1359 unix_state_wunlock(sk);
1369 if (other->sk_shutdown & RCV_SHUTDOWN)
1372 if (sk->sk_type != SOCK_SEQPACKET) {
1373 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1378 if (unix_peer(other) != sk &&
1379 (skb_queue_len(&other->sk_receive_queue) >
1380 other->sk_max_ack_backlog)) {
1386 timeo = unix_wait_for_peer(other, timeo);
1388 err = sock_intr_errno(timeo);
1389 if (signal_pending(current))
1395 skb_queue_tail(&other->sk_receive_queue, skb);
1396 unix_state_runlock(other);
1397 other->sk_data_ready(other, len);
1399 scm_destroy(siocb->scm);
1403 unix_state_runlock(other);
1409 scm_destroy(siocb->scm);
1414 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1415 struct msghdr *msg, size_t len)
1417 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1418 struct sock *sk = sock->sk;
1419 struct sock *other = NULL;
1420 struct sockaddr_un *sunaddr=msg->msg_name;
1422 struct sk_buff *skb;
1424 struct scm_cookie tmp_scm;
1426 if (NULL == siocb->scm)
1427 siocb->scm = &tmp_scm;
1428 err = scm_send(sock, msg, siocb->scm);
1433 if (msg->msg_flags&MSG_OOB)
1436 if (msg->msg_namelen) {
1437 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1442 other = unix_peer_get(sk);
1447 if (sk->sk_shutdown & SEND_SHUTDOWN)
1453 * Optimisation for the fact that under 0.01% of X messages typically
1459 /* Keep two messages in the pipe so it schedules better */
1460 if (size > sk->sk_sndbuf / 2 - 64)
1461 size = sk->sk_sndbuf / 2 - 64;
1463 if (size > SKB_MAX_ALLOC)
1464 size = SKB_MAX_ALLOC;
1470 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1476 * If you pass two values to the sock_alloc_send_skb
1477 * it tries to grab the large buffer with GFP_NOFS
1478 * (which can fail easily), and if it fails grab the
1479 * fallback size buffer which is under a page and will
1482 size = min_t(int, size, skb_tailroom(skb));
1484 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1486 unix_attach_fds(siocb->scm, skb);
1488 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1493 unix_state_rlock(other);
1495 if (sock_flag(other, SOCK_DEAD) ||
1496 (other->sk_shutdown & RCV_SHUTDOWN))
1499 skb_queue_tail(&other->sk_receive_queue, skb);
1500 unix_state_runlock(other);
1501 other->sk_data_ready(other, size);
1506 scm_destroy(siocb->scm);
1512 unix_state_runlock(other);
1515 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1516 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;
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);
1600 if (!(flags & MSG_PEEK))
1603 unix_detach_fds(siocb->scm, skb);
1607 /* It is questionable: on PEEK we could:
1608 - do not return fds - good, but too simple 8)
1609 - return fds, and do not return them on read (old strategy,
1611 - clone fds (I chose it for now, it is the most universal
1614 POSIX 1003.1g does not actually define this clearly
1615 at all. POSIX 1003.1g doesn't define a lot of things
1620 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1624 scm_recv(sock, msg, siocb->scm, flags);
1627 skb_free_datagram(sk,skb);
1635 * Sleep until data has arrive. But check for races..
1638 static long unix_stream_data_wait(struct sock * sk, long timeo)
1642 unix_state_rlock(sk);
1645 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1647 if (skb_queue_len(&sk->sk_receive_queue) ||
1649 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1650 signal_pending(current) ||
1654 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1655 unix_state_runlock(sk);
1656 timeo = schedule_timeout(timeo);
1657 unix_state_rlock(sk);
1658 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1661 finish_wait(sk->sk_sleep, &wait);
1662 unix_state_runlock(sk);
1668 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1669 struct msghdr *msg, size_t size,
1672 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1673 struct scm_cookie tmp_scm;
1674 struct sock *sk = sock->sk;
1675 struct unix_sock *u = unix_sk(sk);
1676 struct sockaddr_un *sunaddr=msg->msg_name;
1678 int check_creds = 0;
1684 if (sk->sk_state != TCP_ESTABLISHED)
1691 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1692 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1694 msg->msg_namelen = 0;
1696 /* Lock the socket to prevent queue disordering
1697 * while sleeps in memcpy_tomsg
1701 siocb->scm = &tmp_scm;
1702 memset(&tmp_scm, 0, sizeof(tmp_scm));
1710 struct sk_buff *skb;
1712 skb = skb_dequeue(&sk->sk_receive_queue);
1715 if (copied >= target)
1719 * POSIX 1003.1g mandates this order.
1722 if ((err = sock_error(sk)) != 0)
1724 if (sk->sk_shutdown & RCV_SHUTDOWN)
1731 timeo = unix_stream_data_wait(sk, timeo);
1733 if (signal_pending(current)) {
1734 err = sock_intr_errno(timeo);
1742 /* Never glue messages from different writers */
1743 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1744 skb_queue_head(&sk->sk_receive_queue, skb);
1748 /* Copy credentials */
1749 siocb->scm->creds = *UNIXCREDS(skb);
1753 /* Copy address just once */
1756 unix_copy_addr(msg, skb->sk);
1760 chunk = min_t(unsigned int, skb->len, size);
1761 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1762 skb_queue_head(&sk->sk_receive_queue, skb);
1770 /* Mark read part of skb as used */
1771 if (!(flags & MSG_PEEK))
1773 skb_pull(skb, chunk);
1776 unix_detach_fds(siocb->scm, skb);
1778 /* put the skb back if we didn't use it up.. */
1781 skb_queue_head(&sk->sk_receive_queue, skb);
1792 /* It is questionable, see note in unix_dgram_recvmsg.
1795 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1797 /* put message back and return */
1798 skb_queue_head(&sk->sk_receive_queue, skb);
1804 scm_recv(sock, msg, siocb->scm, flags);
1806 return copied ? : err;
1809 static int unix_shutdown(struct socket *sock, int mode)
1811 struct sock *sk = sock->sk;
1814 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1817 unix_state_wlock(sk);
1818 sk->sk_shutdown |= mode;
1819 other=unix_peer(sk);
1822 unix_state_wunlock(sk);
1823 sk->sk_state_change(sk);
1826 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1830 if (mode&RCV_SHUTDOWN)
1831 peer_mode |= SEND_SHUTDOWN;
1832 if (mode&SEND_SHUTDOWN)
1833 peer_mode |= RCV_SHUTDOWN;
1834 unix_state_wlock(other);
1835 other->sk_shutdown |= peer_mode;
1836 unix_state_wunlock(other);
1837 other->sk_state_change(other);
1838 read_lock(&other->sk_callback_lock);
1839 if (peer_mode == SHUTDOWN_MASK)
1840 sk_wake_async(other,1,POLL_HUP);
1841 else if (peer_mode & RCV_SHUTDOWN)
1842 sk_wake_async(other,1,POLL_IN);
1843 read_unlock(&other->sk_callback_lock);
1851 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1853 struct sock *sk = sock->sk;
1860 amount = atomic_read(&sk->sk_wmem_alloc);
1861 err = put_user(amount, (int __user *)arg);
1865 struct sk_buff *skb;
1866 if (sk->sk_state == TCP_LISTEN) {
1871 spin_lock(&sk->sk_receive_queue.lock);
1872 skb = skb_peek(&sk->sk_receive_queue);
1875 spin_unlock(&sk->sk_receive_queue.lock);
1876 err = put_user(amount, (int __user *)arg);
1881 err = dev_ioctl(cmd, (void __user *)arg);
1887 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1889 struct sock *sk = sock->sk;
1892 poll_wait(file, sk->sk_sleep, wait);
1895 /* exceptional events? */
1898 if (sk->sk_shutdown == SHUTDOWN_MASK)
1902 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1903 (sk->sk_shutdown & RCV_SHUTDOWN))
1904 mask |= POLLIN | POLLRDNORM;
1906 /* Connection-based need to check for termination and startup */
1907 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1911 * we set writable also when the other side has shut down the
1912 * connection. This prevents stuck sockets.
1914 if (unix_writable(sk))
1915 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1921 #ifdef CONFIG_PROC_FS
1922 static struct sock *unix_seq_idx(int *iter, loff_t pos)
1927 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
1936 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
1938 read_lock(&unix_table_lock);
1939 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
1942 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1947 return first_unix_socket(seq->private);
1948 return next_unix_socket(seq->private, v);
1951 static void unix_seq_stop(struct seq_file *seq, void *v)
1953 read_unlock(&unix_table_lock);
1956 static int unix_seq_show(struct seq_file *seq, void *v)
1960 seq_puts(seq, "Num RefCount Protocol Flags Type St "
1964 struct unix_sock *u = unix_sk(s);
1965 unix_state_rlock(s);
1967 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
1969 atomic_read(&s->sk_refcnt),
1971 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
1974 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
1975 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
1983 len = u->addr->len - sizeof(short);
1984 if (!UNIX_ABSTRACT(s))
1990 for ( ; i < len; i++)
1991 seq_putc(seq, u->addr->name->sun_path[i]);
1993 unix_state_runlock(s);
1994 seq_putc(seq, '\n');
2000 static struct seq_operations unix_seq_ops = {
2001 .start = unix_seq_start,
2002 .next = unix_seq_next,
2003 .stop = unix_seq_stop,
2004 .show = unix_seq_show,
2008 static int unix_seq_open(struct inode *inode, struct file *file)
2010 struct seq_file *seq;
2012 int *iter = kmalloc(sizeof(int), GFP_KERNEL);
2017 rc = seq_open(file, &unix_seq_ops);
2021 seq = file->private_data;
2022 seq->private = iter;
2031 static struct file_operations unix_seq_fops = {
2032 .owner = THIS_MODULE,
2033 .open = unix_seq_open,
2035 .llseek = seq_lseek,
2036 .release = seq_release_private,
2041 static struct net_proto_family unix_family_ops = {
2043 .create = unix_create,
2044 .owner = THIS_MODULE,
2047 #ifdef CONFIG_SYSCTL
2048 extern void unix_sysctl_register(void);
2049 extern void unix_sysctl_unregister(void);
2051 static inline void unix_sysctl_register(void) {}
2052 static inline void unix_sysctl_unregister(void) {}
2055 static int __init af_unix_init(void)
2057 struct sk_buff *dummy_skb;
2059 if (sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb)) {
2060 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
2063 /* allocate our sock slab cache */
2064 unix_sk_cachep = kmem_cache_create("unix_sock",
2065 sizeof(struct unix_sock), 0,
2066 SLAB_HWCACHE_ALIGN, NULL, NULL);
2067 if (!unix_sk_cachep)
2069 "af_unix_init: Cannot create unix_sock SLAB cache!\n");
2071 sock_register(&unix_family_ops);
2072 #ifdef CONFIG_PROC_FS
2073 proc_net_fops_create("unix", 0, &unix_seq_fops);
2075 unix_sysctl_register();
2079 static void __exit af_unix_exit(void)
2081 sock_unregister(PF_UNIX);
2082 unix_sysctl_unregister();
2083 proc_net_remove("unix");
2084 kmem_cache_destroy(unix_sk_cachep);
2087 module_init(af_unix_init);
2088 module_exit(af_unix_exit);
2090 MODULE_LICENSE("GPL");
2091 MODULE_ALIAS_NETPROTO(PF_UNIX);