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 static kmem_cache_t *unix_sk_cachep;
129 struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1];
130 DEFINE_RWLOCK(unix_table_lock);
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 */
402 /* ---- Socket is dead now and most probably destroyed ---- */
405 * Fixme: BSD difference: In BSD all sockets connected to use get
406 * ECONNRESET and we die on the spot. In Linux we behave
407 * like files and pipes do and wait for the last
410 * Can't we simply set sock->err?
412 * What the above comment does talk about? --ANK(980817)
415 if (atomic_read(&unix_tot_inflight))
416 unix_gc(); /* Garbage collect fds */
421 static int unix_listen(struct socket *sock, int backlog)
424 struct sock *sk = sock->sk;
425 struct unix_sock *u = unix_sk(sk);
428 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
429 goto out; /* Only stream/seqpacket sockets accept */
432 goto out; /* No listens on an unbound socket */
433 unix_state_wlock(sk);
434 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
436 if (backlog > sk->sk_max_ack_backlog)
437 wake_up_interruptible_all(&u->peer_wait);
438 sk->sk_max_ack_backlog = backlog;
439 sk->sk_state = TCP_LISTEN;
440 /* set credentials so connect can copy them */
441 sk->sk_peercred.pid = current->tgid;
442 sk->sk_peercred.uid = current->euid;
443 sk->sk_peercred.gid = current->egid;
447 unix_state_wunlock(sk);
452 static int unix_release(struct socket *);
453 static int unix_bind(struct socket *, struct sockaddr *, int);
454 static int unix_stream_connect(struct socket *, struct sockaddr *,
455 int addr_len, int flags);
456 static int unix_socketpair(struct socket *, struct socket *);
457 static int unix_accept(struct socket *, struct socket *, int);
458 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
459 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
460 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
461 static int unix_shutdown(struct socket *, int);
462 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
463 struct msghdr *, size_t);
464 static int unix_stream_recvmsg(struct kiocb *, struct socket *,
465 struct msghdr *, size_t, int);
466 static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
467 struct msghdr *, size_t);
468 static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
469 struct msghdr *, size_t, int);
470 static int unix_dgram_connect(struct socket *, struct sockaddr *,
472 static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
473 struct msghdr *, size_t);
475 static struct proto_ops unix_stream_ops = {
477 .owner = THIS_MODULE,
478 .release = unix_release,
480 .connect = unix_stream_connect,
481 .socketpair = unix_socketpair,
482 .accept = unix_accept,
483 .getname = unix_getname,
486 .listen = unix_listen,
487 .shutdown = unix_shutdown,
488 .setsockopt = sock_no_setsockopt,
489 .getsockopt = sock_no_getsockopt,
490 .sendmsg = unix_stream_sendmsg,
491 .recvmsg = unix_stream_recvmsg,
492 .mmap = sock_no_mmap,
493 .sendpage = sock_no_sendpage,
496 static struct proto_ops unix_dgram_ops = {
498 .owner = THIS_MODULE,
499 .release = unix_release,
501 .connect = unix_dgram_connect,
502 .socketpair = unix_socketpair,
503 .accept = sock_no_accept,
504 .getname = unix_getname,
505 .poll = datagram_poll,
507 .listen = sock_no_listen,
508 .shutdown = unix_shutdown,
509 .setsockopt = sock_no_setsockopt,
510 .getsockopt = sock_no_getsockopt,
511 .sendmsg = unix_dgram_sendmsg,
512 .recvmsg = unix_dgram_recvmsg,
513 .mmap = sock_no_mmap,
514 .sendpage = sock_no_sendpage,
517 static struct proto_ops unix_seqpacket_ops = {
519 .owner = THIS_MODULE,
520 .release = unix_release,
522 .connect = unix_stream_connect,
523 .socketpair = unix_socketpair,
524 .accept = unix_accept,
525 .getname = unix_getname,
526 .poll = datagram_poll,
528 .listen = unix_listen,
529 .shutdown = unix_shutdown,
530 .setsockopt = sock_no_setsockopt,
531 .getsockopt = sock_no_getsockopt,
532 .sendmsg = unix_seqpacket_sendmsg,
533 .recvmsg = unix_dgram_recvmsg,
534 .mmap = sock_no_mmap,
535 .sendpage = sock_no_sendpage,
538 static struct sock * unix_create1(struct socket *sock)
540 struct sock *sk = NULL;
543 if (atomic_read(&unix_nr_socks) >= 2*files_stat.max_files)
546 sk = sk_alloc(PF_UNIX, GFP_KERNEL, sizeof(struct unix_sock),
551 atomic_inc(&unix_nr_socks);
553 sock_init_data(sock,sk);
554 sk_set_owner(sk, THIS_MODULE);
556 sk->sk_write_space = unix_write_space;
557 sk->sk_max_ack_backlog = sysctl_unix_max_dgram_qlen;
558 sk->sk_destruct = unix_sock_destructor;
562 rwlock_init(&u->lock);
563 atomic_set(&u->inflight, sock ? 0 : -1);
564 init_MUTEX(&u->readsem); /* single task reading lock */
565 init_waitqueue_head(&u->peer_wait);
566 unix_insert_socket(unix_sockets_unbound, sk);
571 static int unix_create(struct socket *sock, int protocol)
573 if (protocol && protocol != PF_UNIX)
574 return -EPROTONOSUPPORT;
576 sock->state = SS_UNCONNECTED;
578 switch (sock->type) {
580 sock->ops = &unix_stream_ops;
583 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
587 sock->type=SOCK_DGRAM;
589 sock->ops = &unix_dgram_ops;
592 sock->ops = &unix_seqpacket_ops;
595 return -ESOCKTNOSUPPORT;
598 return unix_create1(sock) ? 0 : -ENOMEM;
601 static int unix_release(struct socket *sock)
603 struct sock *sk = sock->sk;
610 return unix_release_sock (sk, 0);
613 static int unix_autobind(struct socket *sock)
615 struct sock *sk = sock->sk;
616 struct unix_sock *u = unix_sk(sk);
617 static u32 ordernum = 1;
618 struct unix_address * addr;
628 addr = kmalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
632 memset(addr, 0, sizeof(*addr) + sizeof(short) + 16);
633 addr->name->sun_family = AF_UNIX;
634 atomic_set(&addr->refcnt, 1);
637 addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
638 addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
640 write_lock(&unix_table_lock);
641 ordernum = (ordernum+1)&0xFFFFF;
643 if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
645 write_unlock(&unix_table_lock);
646 /* Sanity yield. It is unusual case, but yet... */
647 if (!(ordernum&0xFF))
651 addr->hash ^= sk->sk_type;
653 __unix_remove_socket(sk);
655 __unix_insert_socket(&unix_socket_table[addr->hash], sk);
656 write_unlock(&unix_table_lock);
659 out: up(&u->readsem);
663 static struct sock *unix_find_other(struct sockaddr_un *sunname, int len,
664 int type, unsigned hash, int *error)
670 if (sunname->sun_path[0]) {
671 err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
674 err = permission(nd.dentry->d_inode,MAY_WRITE, &nd);
679 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
681 u=unix_find_socket_byinode(nd.dentry->d_inode);
685 if (u->sk_type == type)
686 touch_atime(nd.mnt, nd.dentry);
691 if (u->sk_type != type) {
697 u=unix_find_socket_byname(sunname, len, type, hash);
699 struct dentry *dentry;
700 dentry = unix_sk(u)->dentry;
702 touch_atime(unix_sk(u)->mnt, dentry);
716 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
718 struct sock *sk = sock->sk;
719 struct unix_sock *u = unix_sk(sk);
720 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
721 struct dentry * dentry = NULL;
725 struct unix_address *addr;
726 struct hlist_head *list;
729 if (sunaddr->sun_family != AF_UNIX)
732 if (addr_len==sizeof(short)) {
733 err = unix_autobind(sock);
737 err = unix_mkname(sunaddr, addr_len, &hash);
749 addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
753 memcpy(addr->name, sunaddr, addr_len);
754 addr->len = addr_len;
755 addr->hash = hash ^ sk->sk_type;
756 atomic_set(&addr->refcnt, 1);
758 if (sunaddr->sun_path[0]) {
762 * Get the parent directory, calculate the hash for last
765 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
767 goto out_mknod_parent;
769 * Yucky last component or no last component at all?
770 * (foo/., foo/.., /////)
773 if (nd.last_type != LAST_NORM)
776 * Lock the directory.
778 down(&nd.dentry->d_inode->i_sem);
780 * Do the final lookup.
782 dentry = lookup_hash(&nd.last, nd.dentry);
783 err = PTR_ERR(dentry);
785 goto out_mknod_unlock;
788 * Special case - lookup gave negative, but... we had foo/bar/
789 * From the vfs_mknod() POV we just have a negative dentry -
790 * all is fine. Let's be bastards - you had / on the end, you've
791 * been asking for (non-existent) directory. -ENOENT for you.
793 if (nd.last.name[nd.last.len] && !dentry->d_inode)
796 * All right, let's create it.
799 (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
800 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
803 up(&nd.dentry->d_inode->i_sem);
807 addr->hash = UNIX_HASH_SIZE;
810 write_lock(&unix_table_lock);
812 if (!sunaddr->sun_path[0]) {
814 if (__unix_find_socket_byname(sunaddr, addr_len,
815 sk->sk_type, hash)) {
816 unix_release_addr(addr);
820 list = &unix_socket_table[addr->hash];
822 list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
823 u->dentry = nd.dentry;
828 __unix_remove_socket(sk);
830 __unix_insert_socket(list, sk);
833 write_unlock(&unix_table_lock);
842 up(&nd.dentry->d_inode->i_sem);
848 unix_release_addr(addr);
852 static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
855 struct sock *sk = sock->sk;
856 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
861 if (addr->sa_family != AF_UNSPEC) {
862 err = unix_mkname(sunaddr, alen, &hash);
867 if (test_bit(SOCK_PASS_CRED, &sock->flags) && !unix_sk(sk)->addr &&
868 (err = unix_autobind(sock)) != 0)
871 other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
875 unix_state_wlock(sk);
878 if (!unix_may_send(sk, other))
881 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
887 * 1003.1g breaking connected state with AF_UNSPEC
890 unix_state_wlock(sk);
894 * If it was connected, reconnect.
897 struct sock *old_peer = unix_peer(sk);
899 unix_state_wunlock(sk);
901 if (other != old_peer)
902 unix_dgram_disconnected(sk, old_peer);
906 unix_state_wunlock(sk);
911 unix_state_wunlock(sk);
917 static long unix_wait_for_peer(struct sock *other, long timeo)
919 struct unix_sock *u = unix_sk(other);
923 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
925 sched = !sock_flag(other, SOCK_DEAD) &&
926 !(other->sk_shutdown & RCV_SHUTDOWN) &&
927 (skb_queue_len(&other->sk_receive_queue) >
928 other->sk_max_ack_backlog);
930 unix_state_runlock(other);
933 timeo = schedule_timeout(timeo);
935 finish_wait(&u->peer_wait, &wait);
939 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
940 int addr_len, int flags)
942 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
943 struct sock *sk = sock->sk;
944 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
945 struct sock *newsk = NULL;
946 struct sock *other = NULL;
947 struct sk_buff *skb = NULL;
953 err = unix_mkname(sunaddr, addr_len, &hash);
958 if (test_bit(SOCK_PASS_CRED, &sock->flags)
959 && !u->addr && (err = unix_autobind(sock)) != 0)
962 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
964 /* First of all allocate resources.
965 If we will make it after state is locked,
966 we will have to recheck all again in any case.
971 /* create new sock for complete connection */
972 newsk = unix_create1(NULL);
976 /* Allocate skb for sending to listening sock */
977 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
982 /* Find listening sock. */
983 other = unix_find_other(sunaddr, addr_len, sk->sk_type, hash, &err);
987 /* Latch state of peer */
988 unix_state_rlock(other);
990 /* Apparently VFS overslept socket death. Retry. */
991 if (sock_flag(other, SOCK_DEAD)) {
992 unix_state_runlock(other);
998 if (other->sk_state != TCP_LISTEN)
1001 if (skb_queue_len(&other->sk_receive_queue) >
1002 other->sk_max_ack_backlog) {
1007 timeo = unix_wait_for_peer(other, timeo);
1009 err = sock_intr_errno(timeo);
1010 if (signal_pending(current))
1018 It is tricky place. We need to grab write lock and cannot
1019 drop lock on peer. It is dangerous because deadlock is
1020 possible. Connect to self case and simultaneous
1021 attempt to connect are eliminated by checking socket
1022 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1023 check this before attempt to grab lock.
1025 Well, and we have to recheck the state after socket locked.
1031 /* This is ok... continue with connect */
1033 case TCP_ESTABLISHED:
1034 /* Socket is already connected */
1042 unix_state_wlock(sk);
1044 if (sk->sk_state != st) {
1045 unix_state_wunlock(sk);
1046 unix_state_runlock(other);
1051 err = security_unix_stream_connect(sock, other->sk_socket, newsk);
1053 unix_state_wunlock(sk);
1057 /* The way is open! Fastly set all the necessary fields... */
1060 unix_peer(newsk) = sk;
1061 newsk->sk_state = TCP_ESTABLISHED;
1062 newsk->sk_type = sk->sk_type;
1063 newsk->sk_peercred.pid = current->tgid;
1064 newsk->sk_peercred.uid = current->euid;
1065 newsk->sk_peercred.gid = current->egid;
1066 newu = unix_sk(newsk);
1067 newsk->sk_sleep = &newu->peer_wait;
1068 otheru = unix_sk(other);
1070 /* copy address information from listening to new sock*/
1072 atomic_inc(&otheru->addr->refcnt);
1073 newu->addr = otheru->addr;
1075 if (otheru->dentry) {
1076 newu->dentry = dget(otheru->dentry);
1077 newu->mnt = mntget(otheru->mnt);
1080 /* Set credentials */
1081 sk->sk_peercred = other->sk_peercred;
1084 unix_peer(sk) = newsk;
1085 sock->state = SS_CONNECTED;
1086 sk->sk_state = TCP_ESTABLISHED;
1088 unix_state_wunlock(sk);
1090 /* take ten and and send info to listening sock */
1091 spin_lock(&other->sk_receive_queue.lock);
1092 __skb_queue_tail(&other->sk_receive_queue, skb);
1093 /* Undo artificially decreased inflight after embrion
1094 * is installed to listening socket. */
1095 atomic_inc(&newu->inflight);
1096 spin_unlock(&other->sk_receive_queue.lock);
1097 unix_state_runlock(other);
1098 other->sk_data_ready(other, 0);
1104 unix_state_runlock(other);
1110 unix_release_sock(newsk, 0);
1116 static int unix_socketpair(struct socket *socka, struct socket *sockb)
1118 struct sock *ska=socka->sk, *skb = sockb->sk;
1120 /* Join our sockets back to back */
1125 ska->sk_peercred.pid = skb->sk_peercred.pid = current->tgid;
1126 ska->sk_peercred.uid = skb->sk_peercred.uid = current->euid;
1127 ska->sk_peercred.gid = skb->sk_peercred.gid = current->egid;
1129 if (ska->sk_type != SOCK_DGRAM) {
1130 ska->sk_state = TCP_ESTABLISHED;
1131 skb->sk_state = TCP_ESTABLISHED;
1132 socka->state = SS_CONNECTED;
1133 sockb->state = SS_CONNECTED;
1138 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1140 struct sock *sk = sock->sk;
1142 struct sk_buff *skb;
1146 if (sock->type!=SOCK_STREAM && sock->type!=SOCK_SEQPACKET)
1150 if (sk->sk_state != TCP_LISTEN)
1153 /* If socket state is TCP_LISTEN it cannot change (for now...),
1154 * so that no locks are necessary.
1157 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1159 /* This means receive shutdown. */
1166 skb_free_datagram(sk, skb);
1167 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1169 /* attach accepted sock to socket */
1170 unix_state_wlock(tsk);
1171 newsock->state = SS_CONNECTED;
1172 sock_graft(tsk, newsock);
1173 unix_state_wunlock(tsk);
1181 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1183 struct sock *sk = sock->sk;
1184 struct unix_sock *u;
1185 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1189 sk = unix_peer_get(sk);
1200 unix_state_rlock(sk);
1202 sunaddr->sun_family = AF_UNIX;
1203 sunaddr->sun_path[0] = 0;
1204 *uaddr_len = sizeof(short);
1206 struct unix_address *addr = u->addr;
1208 *uaddr_len = addr->len;
1209 memcpy(sunaddr, addr->name, *uaddr_len);
1211 unix_state_runlock(sk);
1217 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1221 scm->fp = UNIXCB(skb).fp;
1222 skb->destructor = sock_wfree;
1223 UNIXCB(skb).fp = NULL;
1225 for (i=scm->fp->count-1; i>=0; i--)
1226 unix_notinflight(scm->fp->fp[i]);
1229 static void unix_destruct_fds(struct sk_buff *skb)
1231 struct scm_cookie scm;
1232 memset(&scm, 0, sizeof(scm));
1233 unix_detach_fds(&scm, skb);
1235 /* Alas, it calls VFS */
1236 /* So fscking what? fput() had been SMP-safe since the last Summer */
1241 static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1244 for (i=scm->fp->count-1; i>=0; i--)
1245 unix_inflight(scm->fp->fp[i]);
1246 UNIXCB(skb).fp = scm->fp;
1247 skb->destructor = unix_destruct_fds;
1252 * Send AF_UNIX data.
1255 static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
1256 struct msghdr *msg, size_t len)
1258 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1259 struct sock *sk = sock->sk;
1260 struct unix_sock *u = unix_sk(sk);
1261 struct sockaddr_un *sunaddr=msg->msg_name;
1262 struct sock *other = NULL;
1263 int namelen = 0; /* fake GCC */
1266 struct sk_buff *skb;
1268 struct scm_cookie tmp_scm;
1270 if (NULL == siocb->scm)
1271 siocb->scm = &tmp_scm;
1272 err = scm_send(sock, msg, siocb->scm);
1277 if (msg->msg_flags&MSG_OOB)
1280 if (msg->msg_namelen) {
1281 err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1288 other = unix_peer_get(sk);
1293 if (test_bit(SOCK_PASS_CRED, &sock->flags)
1294 && !u->addr && (err = unix_autobind(sock)) != 0)
1298 if (len > sk->sk_sndbuf - 32)
1301 skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1305 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1307 unix_attach_fds(siocb->scm, skb);
1309 skb->h.raw = skb->data;
1310 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1314 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1319 if (sunaddr == NULL)
1322 other = unix_find_other(sunaddr, namelen, sk->sk_type,
1328 unix_state_rlock(other);
1330 if (!unix_may_send(sk, other))
1333 if (sock_flag(other, SOCK_DEAD)) {
1335 * Check with 1003.1g - what should
1338 unix_state_runlock(other);
1342 unix_state_wlock(sk);
1343 if (unix_peer(sk) == other) {
1345 unix_state_wunlock(sk);
1347 unix_dgram_disconnected(sk, other);
1349 err = -ECONNREFUSED;
1351 unix_state_wunlock(sk);
1361 if (other->sk_shutdown & RCV_SHUTDOWN)
1364 if (sk->sk_type != SOCK_SEQPACKET) {
1365 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1370 if (unix_peer(other) != sk &&
1371 (skb_queue_len(&other->sk_receive_queue) >
1372 other->sk_max_ack_backlog)) {
1378 timeo = unix_wait_for_peer(other, timeo);
1380 err = sock_intr_errno(timeo);
1381 if (signal_pending(current))
1387 skb_queue_tail(&other->sk_receive_queue, skb);
1388 unix_state_runlock(other);
1389 other->sk_data_ready(other, len);
1391 scm_destroy(siocb->scm);
1395 unix_state_runlock(other);
1401 scm_destroy(siocb->scm);
1406 static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
1407 struct msghdr *msg, size_t len)
1409 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1410 struct sock *sk = sock->sk;
1411 struct sock *other = NULL;
1412 struct sockaddr_un *sunaddr=msg->msg_name;
1414 struct sk_buff *skb;
1416 struct scm_cookie tmp_scm;
1418 if (NULL == siocb->scm)
1419 siocb->scm = &tmp_scm;
1420 err = scm_send(sock, msg, siocb->scm);
1425 if (msg->msg_flags&MSG_OOB)
1428 if (msg->msg_namelen) {
1429 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1434 other = unix_peer_get(sk);
1439 if (sk->sk_shutdown & SEND_SHUTDOWN)
1445 * Optimisation for the fact that under 0.01% of X messages typically
1451 /* Keep two messages in the pipe so it schedules better */
1452 if (size > sk->sk_sndbuf / 2 - 64)
1453 size = sk->sk_sndbuf / 2 - 64;
1455 if (size > SKB_MAX_ALLOC)
1456 size = SKB_MAX_ALLOC;
1462 skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1468 * If you pass two values to the sock_alloc_send_skb
1469 * it tries to grab the large buffer with GFP_NOFS
1470 * (which can fail easily), and if it fails grab the
1471 * fallback size buffer which is under a page and will
1474 size = min_t(int, size, skb_tailroom(skb));
1476 memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
1478 unix_attach_fds(siocb->scm, skb);
1480 if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1485 unix_state_rlock(other);
1487 if (sock_flag(other, SOCK_DEAD) ||
1488 (other->sk_shutdown & RCV_SHUTDOWN))
1491 skb_queue_tail(&other->sk_receive_queue, skb);
1492 unix_state_runlock(other);
1493 other->sk_data_ready(other, size);
1498 scm_destroy(siocb->scm);
1504 unix_state_runlock(other);
1507 if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1508 send_sig(SIGPIPE,current,0);
1513 scm_destroy(siocb->scm);
1515 return sent ? : err;
1518 static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1519 struct msghdr *msg, size_t len)
1522 struct sock *sk = sock->sk;
1524 err = sock_error(sk);
1528 if (sk->sk_state != TCP_ESTABLISHED)
1531 if (msg->msg_namelen)
1532 msg->msg_namelen = 0;
1534 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1537 static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1539 struct unix_sock *u = unix_sk(sk);
1541 msg->msg_namelen = 0;
1543 msg->msg_namelen = u->addr->len;
1544 memcpy(msg->msg_name, u->addr->name, u->addr->len);
1548 static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1549 struct msghdr *msg, size_t size,
1552 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1553 struct scm_cookie tmp_scm;
1554 struct sock *sk = sock->sk;
1555 struct unix_sock *u = unix_sk(sk);
1556 int noblock = flags & MSG_DONTWAIT;
1557 struct sk_buff *skb;
1564 msg->msg_namelen = 0;
1568 skb = skb_recv_datagram(sk, flags, noblock, &err);
1572 wake_up_interruptible(&u->peer_wait);
1575 unix_copy_addr(msg, skb->sk);
1577 if (size > skb->len)
1579 else if (size < skb->len)
1580 msg->msg_flags |= MSG_TRUNC;
1582 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1587 siocb->scm = &tmp_scm;
1588 memset(&tmp_scm, 0, sizeof(tmp_scm));
1590 siocb->scm->creds = *UNIXCREDS(skb);
1592 if (!(flags & MSG_PEEK))
1595 unix_detach_fds(siocb->scm, skb);
1599 /* It is questionable: on PEEK we could:
1600 - do not return fds - good, but too simple 8)
1601 - return fds, and do not return them on read (old strategy,
1603 - clone fds (I chose it for now, it is the most universal
1606 POSIX 1003.1g does not actually define this clearly
1607 at all. POSIX 1003.1g doesn't define a lot of things
1612 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1616 scm_recv(sock, msg, siocb->scm, flags);
1619 skb_free_datagram(sk,skb);
1627 * Sleep until data has arrive. But check for races..
1630 static long unix_stream_data_wait(struct sock * sk, long timeo)
1634 unix_state_rlock(sk);
1637 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1639 if (skb_queue_len(&sk->sk_receive_queue) ||
1641 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1642 signal_pending(current) ||
1646 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1647 unix_state_runlock(sk);
1648 timeo = schedule_timeout(timeo);
1649 unix_state_rlock(sk);
1650 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
1653 finish_wait(sk->sk_sleep, &wait);
1654 unix_state_runlock(sk);
1660 static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1661 struct msghdr *msg, size_t size,
1664 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1665 struct scm_cookie tmp_scm;
1666 struct sock *sk = sock->sk;
1667 struct unix_sock *u = unix_sk(sk);
1668 struct sockaddr_un *sunaddr=msg->msg_name;
1670 int check_creds = 0;
1676 if (sk->sk_state != TCP_ESTABLISHED)
1683 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1684 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1686 msg->msg_namelen = 0;
1688 /* Lock the socket to prevent queue disordering
1689 * while sleeps in memcpy_tomsg
1693 siocb->scm = &tmp_scm;
1694 memset(&tmp_scm, 0, sizeof(tmp_scm));
1702 struct sk_buff *skb;
1704 skb = skb_dequeue(&sk->sk_receive_queue);
1707 if (copied >= target)
1711 * POSIX 1003.1g mandates this order.
1714 if ((err = sock_error(sk)) != 0)
1716 if (sk->sk_shutdown & RCV_SHUTDOWN)
1723 timeo = unix_stream_data_wait(sk, timeo);
1725 if (signal_pending(current)) {
1726 err = sock_intr_errno(timeo);
1734 /* Never glue messages from different writers */
1735 if (memcmp(UNIXCREDS(skb), &siocb->scm->creds, sizeof(siocb->scm->creds)) != 0) {
1736 skb_queue_head(&sk->sk_receive_queue, skb);
1740 /* Copy credentials */
1741 siocb->scm->creds = *UNIXCREDS(skb);
1745 /* Copy address just once */
1748 unix_copy_addr(msg, skb->sk);
1752 chunk = min_t(unsigned int, skb->len, size);
1753 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1754 skb_queue_head(&sk->sk_receive_queue, skb);
1762 /* Mark read part of skb as used */
1763 if (!(flags & MSG_PEEK))
1765 skb_pull(skb, chunk);
1768 unix_detach_fds(siocb->scm, skb);
1770 /* put the skb back if we didn't use it up.. */
1773 skb_queue_head(&sk->sk_receive_queue, skb);
1784 /* It is questionable, see note in unix_dgram_recvmsg.
1787 siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1789 /* put message back and return */
1790 skb_queue_head(&sk->sk_receive_queue, skb);
1796 scm_recv(sock, msg, siocb->scm, flags);
1798 return copied ? : err;
1801 static int unix_shutdown(struct socket *sock, int mode)
1803 struct sock *sk = sock->sk;
1806 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1809 unix_state_wlock(sk);
1810 sk->sk_shutdown |= mode;
1811 other=unix_peer(sk);
1814 unix_state_wunlock(sk);
1815 sk->sk_state_change(sk);
1818 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
1822 if (mode&RCV_SHUTDOWN)
1823 peer_mode |= SEND_SHUTDOWN;
1824 if (mode&SEND_SHUTDOWN)
1825 peer_mode |= RCV_SHUTDOWN;
1826 unix_state_wlock(other);
1827 other->sk_shutdown |= peer_mode;
1828 unix_state_wunlock(other);
1829 other->sk_state_change(other);
1830 read_lock(&other->sk_callback_lock);
1831 if (peer_mode == SHUTDOWN_MASK)
1832 sk_wake_async(other,1,POLL_HUP);
1833 else if (peer_mode & RCV_SHUTDOWN)
1834 sk_wake_async(other,1,POLL_IN);
1835 read_unlock(&other->sk_callback_lock);
1843 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1845 struct sock *sk = sock->sk;
1852 amount = atomic_read(&sk->sk_wmem_alloc);
1853 err = put_user(amount, (int __user *)arg);
1857 struct sk_buff *skb;
1859 if (sk->sk_state == TCP_LISTEN) {
1864 spin_lock(&sk->sk_receive_queue.lock);
1865 if (sk->sk_type == SOCK_STREAM ||
1866 sk->sk_type == SOCK_SEQPACKET) {
1867 skb_queue_walk(&sk->sk_receive_queue, skb)
1870 skb = skb_peek(&sk->sk_receive_queue);
1874 spin_unlock(&sk->sk_receive_queue.lock);
1875 err = put_user(amount, (int __user *)arg);
1880 err = dev_ioctl(cmd, (void __user *)arg);
1886 static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1888 struct sock *sk = sock->sk;
1891 poll_wait(file, sk->sk_sleep, wait);
1894 /* exceptional events? */
1897 if (sk->sk_shutdown == SHUTDOWN_MASK)
1901 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1902 (sk->sk_shutdown & RCV_SHUTDOWN))
1903 mask |= POLLIN | POLLRDNORM;
1905 /* Connection-based need to check for termination and startup */
1906 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE)
1910 * we set writable also when the other side has shut down the
1911 * connection. This prevents stuck sockets.
1913 if (unix_writable(sk))
1914 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1920 #ifdef CONFIG_PROC_FS
1921 static struct sock *unix_seq_idx(int *iter, loff_t pos)
1926 for (s = first_unix_socket(iter); s; s = next_unix_socket(iter, s)) {
1935 static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
1937 read_lock(&unix_table_lock);
1938 return *pos ? unix_seq_idx(seq->private, *pos - 1) : ((void *) 1);
1941 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1946 return first_unix_socket(seq->private);
1947 return next_unix_socket(seq->private, v);
1950 static void unix_seq_stop(struct seq_file *seq, void *v)
1952 read_unlock(&unix_table_lock);
1955 static int unix_seq_show(struct seq_file *seq, void *v)
1959 seq_puts(seq, "Num RefCount Protocol Flags Type St "
1963 struct unix_sock *u = unix_sk(s);
1964 unix_state_rlock(s);
1966 seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu",
1968 atomic_read(&s->sk_refcnt),
1970 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
1973 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
1974 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
1982 len = u->addr->len - sizeof(short);
1983 if (!UNIX_ABSTRACT(s))
1989 for ( ; i < len; i++)
1990 seq_putc(seq, u->addr->name->sun_path[i]);
1992 unix_state_runlock(s);
1993 seq_putc(seq, '\n');
1999 static struct seq_operations unix_seq_ops = {
2000 .start = unix_seq_start,
2001 .next = unix_seq_next,
2002 .stop = unix_seq_stop,
2003 .show = unix_seq_show,
2007 static int unix_seq_open(struct inode *inode, struct file *file)
2009 struct seq_file *seq;
2011 int *iter = kmalloc(sizeof(int), GFP_KERNEL);
2016 rc = seq_open(file, &unix_seq_ops);
2020 seq = file->private_data;
2021 seq->private = iter;
2030 static struct file_operations unix_seq_fops = {
2031 .owner = THIS_MODULE,
2032 .open = unix_seq_open,
2034 .llseek = seq_lseek,
2035 .release = seq_release_private,
2040 static struct net_proto_family unix_family_ops = {
2042 .create = unix_create,
2043 .owner = THIS_MODULE,
2046 #ifdef CONFIG_SYSCTL
2047 extern void unix_sysctl_register(void);
2048 extern void unix_sysctl_unregister(void);
2050 static inline void unix_sysctl_register(void) {}
2051 static inline void unix_sysctl_unregister(void) {}
2054 static int __init af_unix_init(void)
2056 struct sk_buff *dummy_skb;
2058 if (sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb)) {
2059 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
2062 /* allocate our sock slab cache */
2063 unix_sk_cachep = kmem_cache_create("unix_sock",
2064 sizeof(struct unix_sock), 0,
2065 SLAB_HWCACHE_ALIGN, NULL, NULL);
2066 if (!unix_sk_cachep)
2068 "af_unix_init: Cannot create unix_sock SLAB cache!\n");
2070 sock_register(&unix_family_ops);
2071 #ifdef CONFIG_PROC_FS
2072 proc_net_fops_create("unix", 0, &unix_seq_fops);
2074 unix_sysctl_register();
2078 static void __exit af_unix_exit(void)
2080 sock_unregister(PF_UNIX);
2081 unix_sysctl_unregister();
2082 proc_net_remove("unix");
2083 kmem_cache_destroy(unix_sk_cachep);
2086 module_init(af_unix_init);
2087 module_exit(af_unix_exit);
2089 MODULE_LICENSE("GPL");
2090 MODULE_ALIAS_NETPROTO(PF_UNIX);