af_unix: fix 'poll for write'/connected DGRAM sockets
[pandora-kernel.git] / net / unix / af_unix.c
index 50bbf6b..783317d 100644 (file)
@@ -169,6 +169,11 @@ static inline int unix_may_send(struct sock *sk, struct sock *osk)
        return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
 }
 
+static inline int unix_recvq_full(struct sock const *sk)
+{
+       return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
+}
+
 static struct sock *unix_peer_get(struct sock *s)
 {
        struct sock *peer;
@@ -482,6 +487,8 @@ static int unix_socketpair(struct socket *, struct socket *);
 static int unix_accept(struct socket *, struct socket *, int);
 static int unix_getname(struct socket *, struct sockaddr *, int *, int);
 static unsigned int unix_poll(struct file *, struct socket *, poll_table *);
+static unsigned int unix_dgram_poll(struct file *, struct socket *,
+                                   poll_table *);
 static int unix_ioctl(struct socket *, unsigned int, unsigned long);
 static int unix_shutdown(struct socket *, int);
 static int unix_stream_sendmsg(struct kiocb *, struct socket *,
@@ -527,7 +534,7 @@ static const struct proto_ops unix_dgram_ops = {
        .socketpair =   unix_socketpair,
        .accept =       sock_no_accept,
        .getname =      unix_getname,
-       .poll =         datagram_poll,
+       .poll =         unix_dgram_poll,
        .ioctl =        unix_ioctl,
        .listen =       sock_no_listen,
        .shutdown =     unix_shutdown,
@@ -548,7 +555,7 @@ static const struct proto_ops unix_seqpacket_ops = {
        .socketpair =   unix_socketpair,
        .accept =       unix_accept,
        .getname =      unix_getname,
-       .poll =         datagram_poll,
+       .poll =         unix_dgram_poll,
        .ioctl =        unix_ioctl,
        .listen =       unix_listen,
        .shutdown =     unix_shutdown,
@@ -819,7 +826,11 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
                 */
                mode = S_IFSOCK |
                       (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
+               err = mnt_want_write(nd.path.mnt);
+               if (err)
+                       goto out_mknod_dput;
                err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
+               mnt_drop_write(nd.path.mnt);
                if (err)
                        goto out_mknod_dput;
                mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
@@ -979,8 +990,7 @@ static long unix_wait_for_peer(struct sock *other, long timeo)
 
        sched = !sock_flag(other, SOCK_DEAD) &&
                !(other->sk_shutdown & RCV_SHUTDOWN) &&
-               (skb_queue_len(&other->sk_receive_queue) >
-                other->sk_max_ack_backlog);
+               unix_recvq_full(other);
 
        unix_state_unlock(other);
 
@@ -1054,8 +1064,7 @@ restart:
        if (other->sk_state != TCP_LISTEN)
                goto out_unlock;
 
-       if (skb_queue_len(&other->sk_receive_queue) >
-           other->sk_max_ack_backlog) {
+       if (unix_recvq_full(other)) {
                err = -EAGAIN;
                if (!timeo)
                        goto out_unlock;
@@ -1424,9 +1433,7 @@ restart:
                        goto out_unlock;
        }
 
-       if (unix_peer(other) != sk &&
-           (skb_queue_len(&other->sk_receive_queue) >
-            other->sk_max_ack_backlog)) {
+       if (unix_peer(other) != sk && unix_recvq_full(other)) {
                if (!timeo) {
                        err = -EAGAIN;
                        goto out_unlock;
@@ -1987,6 +1994,60 @@ static unsigned int unix_poll(struct file * file, struct socket *sock, poll_tabl
        return mask;
 }
 
+static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
+                                   poll_table *wait)
+{
+       struct sock *sk = sock->sk, *other;
+       unsigned int mask, writable;
+
+       poll_wait(file, sk->sk_sleep, wait);
+       mask = 0;
+
+       /* exceptional events? */
+       if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
+               mask |= POLLERR;
+       if (sk->sk_shutdown & RCV_SHUTDOWN)
+               mask |= POLLRDHUP;
+       if (sk->sk_shutdown == SHUTDOWN_MASK)
+               mask |= POLLHUP;
+
+       /* readable? */
+       if (!skb_queue_empty(&sk->sk_receive_queue) ||
+           (sk->sk_shutdown & RCV_SHUTDOWN))
+               mask |= POLLIN | POLLRDNORM;
+
+       /* Connection-based need to check for termination and startup */
+       if (sk->sk_type == SOCK_SEQPACKET) {
+               if (sk->sk_state == TCP_CLOSE)
+                       mask |= POLLHUP;
+               /* connection hasn't started yet? */
+               if (sk->sk_state == TCP_SYN_SENT)
+                       return mask;
+       }
+
+       /* writable? */
+       writable = unix_writable(sk);
+       if (writable) {
+               other = unix_peer_get(sk);
+               if (other) {
+                       if (unix_peer(other) != sk) {
+                               poll_wait(file, &unix_sk(other)->peer_wait,
+                                         wait);
+                               if (unix_recvq_full(other))
+                                       writable = 0;
+                       }
+
+                       sock_put(other);
+               }
+       }
+
+       if (writable)
+               mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
+       else
+               set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
+
+       return mask;
+}
 
 #ifdef CONFIG_PROC_FS
 static struct sock *first_unix_socket(int *i)
@@ -2037,7 +2098,7 @@ static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
        __acquires(unix_table_lock)
 {
        spin_lock(&unix_table_lock);
-       return *pos ? unix_seq_idx(seq, *pos - 1) : ((void *) 1);
+       return *pos ? unix_seq_idx(seq, *pos - 1) : SEQ_START_TOKEN;
 }
 
 static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
@@ -2046,7 +2107,7 @@ static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
        struct sock *sk = v;
        ++*pos;
 
-       if (v == (void *)1)
+       if (v == SEQ_START_TOKEN)
                sk = first_unix_socket(&iter->i);
        else
                sk = next_unix_socket(&iter->i, sk);
@@ -2064,7 +2125,7 @@ static void unix_seq_stop(struct seq_file *seq, void *v)
 static int unix_seq_show(struct seq_file *seq, void *v)
 {
 
-       if (v == (void *)1)
+       if (v == SEQ_START_TOKEN)
                seq_puts(seq, "Num       RefCount Protocol Flags    Type St "
                         "Inode Path\n");
        else {
@@ -2193,7 +2254,11 @@ static void __exit af_unix_exit(void)
        unregister_pernet_subsys(&unix_net_ops);
 }
 
-module_init(af_unix_init);
+/* Earlier than device_initcall() so that other drivers invoking
+   request_module() don't end up in a loop when modprobe tries
+   to use a UNIX socket. But later than subsys_initcall() because
+   we depend on stuff initialised there */
+fs_initcall(af_unix_init);
 module_exit(af_unix_exit);
 
 MODULE_LICENSE("GPL");