diff --git a/include/linux/security.h b/include/linux/security.h index 0c88191..e1fcd9f 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -1563,7 +1563,10 @@ struct security_operations { #ifdef CONFIG_SECURITY_NETWORK int (*unix_stream_connect) (struct socket *sock, struct socket *other, struct sock *newsk); + int (*dbus_stream_connect) (struct socket *sock, + struct socket *other, struct sock *newsk); int (*unix_may_send) (struct socket *sock, struct socket *other); + int (*dbus_may_send) (struct socket *sock, struct socket *other); int (*socket_create) (int family, int type, int protocol, int kern); int (*socket_post_create) (struct socket *sock, int family, @@ -2518,7 +2521,10 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 int security_unix_stream_connect(struct socket *sock, struct socket *other, struct sock *newsk); +int security_dbus_stream_connect(struct socket *sock, struct socket *other, + struct sock *newsk); int security_unix_may_send(struct socket *sock, struct socket *other); +int security_dbus_may_send(struct socket *sock, struct socket *other); int security_socket_create(int family, int type, int protocol, int kern); int security_socket_post_create(struct socket *sock, int family, int type, int protocol, int kern); @@ -2562,12 +2568,25 @@ static inline int security_unix_stream_connect(struct socket *sock, return 0; } +static inline int security_dbus_stream_connect(struct socket *sock, + struct socket *other, + struct sock *newsk) +{ + return 0; +} + static inline int security_unix_may_send(struct socket *sock, struct socket *other) { return 0; } +static inline int security_dbus_may_send(struct socket *sock, + struct socket *other) +{ + return 0; +} + static inline int security_socket_create(int family, int type, int protocol, int kern) { diff --git a/include/linux/socket.h b/include/linux/socket.h index 032a19e..ae8e296 100644 --- a/include/linux/socket.h +++ b/include/linux/socket.h @@ -180,7 +180,8 @@ #define AF_ISDN 34 /* mISDN sockets */ #define AF_PHONET 35 /* Phonet sockets */ #define AF_IEEE802154 36 /* IEEE802154 sockets */ -#define AF_MAX 37 /* For now.. */ +#define AF_DBUS 37 /* DBUS sockets */ +#define AF_MAX 38 /* For now.. */ /* Protocol families, same as address families. */ #define PF_UNSPEC AF_UNSPEC @@ -220,6 +221,7 @@ #define PF_ISDN AF_ISDN #define PF_PHONET AF_PHONET #define PF_IEEE802154 AF_IEEE802154 +#define PF_DBUS AF_DBUS #define PF_MAX AF_MAX /* Maximum queue length specifiable by listen. */ diff --git a/include/net/af_dbus.h b/include/net/af_dbus.h new file mode 100644 index 0000000..0d05784 --- /dev/null +++ b/include/net/af_dbus.h @@ -0,0 +1,85 @@ +#ifndef __LINUX_NET_AFDBUS_H +#define __LINUX_NET_AFDBUS_H + +#include +#include +#include +#include + +extern void dbus_inflight(struct file *fp); +extern void dbus_notinflight(struct file *fp); +extern void dbus_gc(void); +extern void wait_for_dbus_gc(void); + +#define DBUS_HASH_SIZE 256 + +extern unsigned int dbus_tot_inflight; + +struct dbus_address { + atomic_t refcnt; + int len; + unsigned hash; + struct sockaddr_un name[0]; +}; + +struct dbus_skb_parms { + struct ucred creds; /* Skb credentials */ + struct scm_fp_list *fp; /* Passed files */ +#ifdef CONFIG_SECURITY_NETWORK + u32 secid; /* Security ID */ +#endif +}; + +#define DBUSCB(skb) (*(struct dbus_skb_parms *)&((skb)->cb)) +#define DBUSCREDS(skb) (&DBUSCB((skb)).creds) +#define DBUSSID(skb) (&DBUSCB((skb)).secid) + +#define dbus_state_lock(s) spin_lock(&dbus_sk(s)->lock) +#define dbus_state_unlock(s) spin_unlock(&dbus_sk(s)->lock) +#define dbus_state_lock_nested(s) \ + spin_lock_nested(&dbus_sk(s)->lock, \ + SINGLE_DEPTH_NESTING) + +#ifdef __KERNEL__ +/* The AF_DBUS socket */ +struct dbus_sock_priv; +struct DBusBus; +struct dbus_sock { + /* WARNING: sk has to be the first member */ + struct sock sk; + struct dbus_address *addr; + struct dentry *dentry; + struct vfsmount *mnt; + struct mutex readlock; + struct sock *peer; + struct sock *other; + struct list_head link; + atomic_long_t inflight; + spinlock_t lock; + unsigned int gc_candidate : 1; + unsigned int gc_maybe_cycle : 1; + struct socket_wq peer_wq; + + struct dbus_sock_priv *priv; + unsigned int daemon_side; + unsigned int authenticated; + char *uniq_name; +#define MAX_WELL_KNOWN_NAMES 10 + char *well_known_names[MAX_WELL_KNOWN_NAMES]; + struct DBusBus *bus; + struct hlist_node bus_link; + +}; +#define dbus_sk(__sk) ((struct dbus_sock *)__sk) + +#define peer_wait peer_wq.wait + +#ifdef CONFIG_SYSCTL +extern int dbus_sysctl_register(struct net *net); +extern void dbus_sysctl_unregister(struct net *net); +#else +static inline int dbus_sysctl_register(struct net *net) { return 0; } +static inline void dbus_sysctl_unregister(struct net *net) {} +#endif +#endif +#endif diff --git a/net/Kconfig b/net/Kconfig index 0d68b40..2a7f72d 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -47,6 +47,7 @@ menu "Networking options" source "net/packet/Kconfig" source "net/unix/Kconfig" +source "net/dbus/Kconfig" source "net/xfrm/Kconfig" source "net/iucv/Kconfig" diff --git a/net/Makefile b/net/Makefile index cb7bdc1..bb9fa24 100644 --- a/net/Makefile +++ b/net/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_NETFILTER) += netfilter/ obj-$(CONFIG_INET) += ipv4/ obj-$(CONFIG_XFRM) += xfrm/ obj-$(CONFIG_UNIX) += unix/ +obj-$(CONFIG_DBUS) += dbus/ ifneq ($(CONFIG_IPV6),) obj-y += ipv6/ endif diff --git a/net/core/sock.c b/net/core/sock.c index 2cf7f9f..cc96185 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -156,7 +156,7 @@ static const char *const af_family_key_strings[AF_MAX+1] = { "sk_lock-27" , "sk_lock-28" , "sk_lock-AF_CAN" , "sk_lock-AF_TIPC" , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV" , "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN" , "sk_lock-AF_PHONET" , - "sk_lock-AF_IEEE802154", + "sk_lock-AF_IEEE802154", "sk_lock-AF_DBUS", "sk_lock-AF_MAX" }; static const char *const af_family_slock_key_strings[AF_MAX+1] = { @@ -172,7 +172,7 @@ static const char *const af_family_slock_key_strings[AF_MAX+1] = { "slock-27" , "slock-28" , "slock-AF_CAN" , "slock-AF_TIPC" , "slock-AF_BLUETOOTH", "slock-AF_IUCV" , "slock-AF_RXRPC" , "slock-AF_ISDN" , "slock-AF_PHONET" , - "slock-AF_IEEE802154", + "slock-AF_IEEE802154", "slock-AF_DBUS", "slock-AF_MAX" }; static const char *const af_family_clock_key_strings[AF_MAX+1] = { @@ -188,7 +188,7 @@ static const char *const af_family_clock_key_strings[AF_MAX+1] = { "clock-27" , "clock-28" , "clock-AF_CAN" , "clock-AF_TIPC" , "clock-AF_BLUETOOTH", "clock-AF_IUCV" , "clock-AF_RXRPC" , "clock-AF_ISDN" , "clock-AF_PHONET" , - "clock-AF_IEEE802154", + "clock-AF_IEEE802154", "clock-AF_DBUS", "clock-AF_MAX" }; diff --git a/net/dbus/Kconfig b/net/dbus/Kconfig new file mode 100644 index 0000000..04a759a --- /dev/null +++ b/net/dbus/Kconfig @@ -0,0 +1,16 @@ +# +# D-Bus Domain Sockets +# + +config DBUS + tristate "DBUS domain sockets (EXPERIMENTAL)" + depends on EXPERIMENTAL + ---help--- + If you say Y here, you will include support for D-BUS domain sockets. + D-BUS sockets are similar to Unix sockets but with D-Bus routing in + order to provide better performances than the traditional + dbus-daemon. + + To compile this driver as a module, choose M here: the + module will be called dbus. If unsure, say N. + diff --git a/net/dbus/Makefile b/net/dbus/Makefile new file mode 100644 index 0000000..8df236c --- /dev/null +++ b/net/dbus/Makefile @@ -0,0 +1,8 @@ +# +# Makefile for the Linux D-Bus domain socket layer. +# + +obj-$(CONFIG_DBUS) += dbus.o + +dbus-y := af_dbus.o garbage.o matchrule.o message.o bus.o +dbus-$(CONFIG_SYSCTL) += sysctl_net_dbus.o diff --git a/net/dbus/af_dbus.c b/net/dbus/af_dbus.c new file mode 100644 index 0000000..3490223 --- /dev/null +++ b/net/dbus/af_dbus.c @@ -0,0 +1,2103 @@ +/* + * DBUS Implementation of D-Bus domain sockets. + * + * Authors: Alban Crequy + * Ian Molton + * + * Based on the BSD Unix domain sockets implementation. + * + * Authors: Alan Cox, + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Fixes: + * Linus Torvalds : Assorted bug cures. + * Niibe Yutaka : async I/O support. + * Carsten Paeth : PF_UNIX check, address fixes. + * Alan Cox : Limit size of allocated blocks. + * Alan Cox : Fixed the stupid socketpair bug. + * Alan Cox : BSD compatibility fine tuning. + * Alan Cox : Fixed a bug in connect when interrupted. + * Alan Cox : Sorted out a proper draft version of + * file descriptor passing hacked up from + * Mike Shaver's work. + * Marty Leisner : Fixes to fd passing + * Nick Nevin : recvmsg bugfix. + * Alan Cox : Started proper garbage collector + * Heiko EiBfeldt : Missing verify_area check + * Alan Cox : Started POSIXisms + * Andreas Schwab : Replace inode by dentry for proper + * reference counting + * Kirk Petersen : Made this a module + * Christoph Rohland : Elegant non-blocking accept/connect algorithm. + * Lots of bug fixes. + * Alexey Kuznetosv : Repaired (I hope) bugs introduces + * by above two patches. + * Andrea Arcangeli : If possible we block in connect(2) + * if the max backlog of the listen socket + * is been reached. This won't break + * old apps and it will avoid huge amount + * of socks hashed (this for unix_gc() + * performances reasons). + * Security fix that limits the max + * number of socks to 2*max_files and + * the number of skb queueable in the + * dgram receiver. + * Artur Skawina : Hash function optimizations + * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8) + * Malcolm Beattie : Set peercred for socketpair + * Michal Ostrowski : Module initialization cleanup. + * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT, + * the core infrastructure is doing that + * for all net proto families now (2.5.69+) + * + * + * Known differences from reference BSD that was tested: + * + * [TO FIX] + * ECONNREFUSED is not returned from one end of a connected() socket to the + * other the moment one end closes. + * fstat() doesn't return st_dev=0, and give the blksize as high water mark + * and a fake inode identifier (nor the BSD first socket fstat twice bug). + * [NOT TO FIX] + * accept() returns a path name even if the connecting socket has closed + * in the meantime (BSD loses the path and gives up). + * accept() returns 0 length path for an unbound connector. BSD returns 16 + * and a null first byte in the path (but not for gethost/peername - BSD bug ??) + * socketpair(...SOCK_RAW..) doesn't panic the kernel. + * BSD af_unix apparently has connect forgetting to block properly. + * (need to check this with the POSIX spec in detail) + * + * Differences from 2.0.0-11-... (ANK) + * Bug fixes and improvements. + * - client shutdown killed server socket. + * - removed all useless cli/sti pairs. + * + * Semantic changes/extensions. + * - generic control message passing. + * - SCM_CREDENTIALS control message. + * - "Abstract" (not FS based) socket bindings. + * Abstract names are sequences of bytes (not zero terminated) + * started by 0, so that this name space does not intersect + * with BSD names. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dbus-protocol.h" +#include "matchrule.h" +#include "message.h" +#include "bus.h" + +static struct hlist_head dbus_socket_table[DBUS_HASH_SIZE + 1]; +static DEFINE_SPINLOCK(dbus_table_lock); +static atomic_t dbus_nr_socks = ATOMIC_INIT(0); + +#define dbus_sockets_unbound (&dbus_socket_table[DBUS_HASH_SIZE]) + +#define DBUS_ABSTRACT(sk) (dbus_sk(sk)->addr->hash != DBUS_HASH_SIZE) + +#ifdef CONFIG_SECURITY_NETWORK +static inline void dbus_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) +{ + scm->secid = *DBUSSID(skb); +} +#else +static inline void dbus_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) +{ } +#endif /* CONFIG_SECURITY_NETWORK */ + +/* + * SMP locking strategy: + * hash table is protected with spinlock dbus_table_lock + * each socket state is protected by separate rwlock. + */ + +static inline unsigned dbus_hash_fold(__wsum n) +{ + unsigned hash = (__force unsigned)n; + hash ^= hash>>16; + hash ^= hash>>8; + return hash&(DBUS_HASH_SIZE-1); +} + +#define dbus_peer(sk) (dbus_sk(sk)->peer) + +static inline int dbus_our_peer(struct sock *sk, struct sock *osk) +{ + return dbus_peer(osk) == sk; +} + +static inline int dbus_may_send(struct sock *sk, struct sock *osk) +{ + return dbus_peer(osk) == NULL || dbus_our_peer(sk, osk); +} + +static inline int dbus_recvq_full(struct sock const *sk) +{ + return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog; +} + +static struct sock *dbus_peer_get(struct sock *s) +{ + struct sock *peer; + + dbus_state_lock(s); + peer = dbus_peer(s); + if (peer) + sock_hold(peer); + dbus_state_unlock(s); + return peer; +} + +static inline void dbus_release_addr(struct dbus_address *addr) +{ + if (atomic_dec_and_test(&addr->refcnt)) + kfree(addr); +} + +/* + * Check dbus socket name: + * - should be not zero length. + * - if started by not zero, should be NULL terminated (FS object) + * - if started by zero, it is abstract name. + */ + +static int dbus_mkname(struct sockaddr_un *sunaddr, int len, unsigned *hashp) +{ + if (len <= sizeof(short) || len > sizeof(*sunaddr)) + return -EINVAL; + if (!sunaddr || sunaddr->sun_family != AF_DBUS) + return -EINVAL; + if (sunaddr->sun_path[0]) { + /* + * This may look like an off by one error but it is a bit more + * subtle. 108 is the longest valid AF_DBUS path for a binding. + * sun_path[108] doesnt as such exist. However in kernel space + * we are guaranteed that it is a valid memory location in our + * kernel address buffer. + */ + ((char *)sunaddr)[len] = 0; + len = strlen(sunaddr->sun_path)+1+sizeof(short); + return len; + } + + *hashp = dbus_hash_fold(csum_partial(sunaddr, len, 0)); + return len; +} + +static void __dbus_remove_socket(struct sock *sk) +{ + sk_del_node_init(sk); +} + +static void __dbus_insert_socket(struct hlist_head *list, struct sock *sk) +{ + WARN_ON(!sk_unhashed(sk)); + sk_add_node(sk, list); +} + +static inline void dbus_remove_socket(struct sock *sk) +{ + spin_lock(&dbus_table_lock); + __dbus_remove_socket(sk); + spin_unlock(&dbus_table_lock); +} + +static inline void dbus_insert_socket(struct hlist_head *list, struct sock *sk) +{ + spin_lock(&dbus_table_lock); + __dbus_insert_socket(list, sk); + spin_unlock(&dbus_table_lock); +} + +static struct sock *__dbus_find_socket_byname(struct net *net, + struct sockaddr_un *sunname, + int len, int type, unsigned hash) +{ + struct sock *s; + struct hlist_node *node; + + sk_for_each(s, node, &dbus_socket_table[hash ^ type]) { + struct dbus_sock *u = dbus_sk(s); + + if (!net_eq(sock_net(s), net)) + continue; + + if (u->addr->len == len && + !memcmp(u->addr->name, sunname, len)) + goto found; + } + s = NULL; +found: + return s; +} + +static inline struct sock *dbus_find_socket_byname(struct net *net, + struct sockaddr_un *sunname, + int len, int type, + unsigned hash) +{ + struct sock *s; + + spin_lock(&dbus_table_lock); + s = __dbus_find_socket_byname(net, sunname, len, type, hash); + if (s) + sock_hold(s); + spin_unlock(&dbus_table_lock); + return s; +} + +static struct sock *dbus_find_socket_byinode(struct net *net, struct inode *i) +{ + struct sock *s; + struct hlist_node *node; + + spin_lock(&dbus_table_lock); + sk_for_each(s, node, + &dbus_socket_table[i->i_ino & (DBUS_HASH_SIZE - 1)]) { + struct dentry *dentry = dbus_sk(s)->dentry; + + if (!net_eq(sock_net(s), net)) + continue; + + if (dentry && dentry->d_inode == i) { + sock_hold(s); + goto found; + } + } + s = NULL; +found: + spin_unlock(&dbus_table_lock); + return s; +} + +static inline int dbus_writable(struct sock *sk) +{ + return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf; +} + +static void dbus_write_space(struct sock *sk) +{ + struct socket_wq *wq; + + rcu_read_lock(); + if (dbus_writable(sk)) { + wq = rcu_dereference(sk->sk_wq); + if (wq_has_sleeper(wq)) + wake_up_interruptible_sync(&wq->wait); + sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); + } + rcu_read_unlock(); +} + +static void dbus_sock_destructor(struct sock *sk) +{ + struct dbus_sock *u = dbus_sk(sk); + + skb_queue_purge(&sk->sk_receive_queue); + + WARN_ON(atomic_read(&sk->sk_wmem_alloc)); + WARN_ON(!sk_unhashed(sk)); + WARN_ON(sk->sk_socket); + if (!sock_flag(sk, SOCK_DEAD)) { + printk(KERN_INFO "Attempt to release alive dbus socket: %p\n", sk); + return; + } + + if (!u->daemon_side &&u->uniq_name && + u->authenticated) { + bus_matchmaker_disconnected(u->bus->matchmaker, u); + dbus_bus_disconnected(u->bus, u); + } + + if (u->addr) + dbus_release_addr(u->addr); + + if (u->priv) { + kfree(u->priv); + u->priv = NULL; + } + + if (u->bus) { + u->bus = NULL; + } + + atomic_dec(&dbus_nr_socks); + local_bh_disable(); + sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); + local_bh_enable(); +#ifdef DBUS_REFCNT_DEBUG + printk(KERN_DEBUG "DBUS %p is destroyed, %d are still alive.\n", sk, + atomic_read(&dbus_nr_socks)); +#endif +} + +static int dbus_release_sock(struct sock *sk, int embrion) +{ + struct dbus_sock *u = dbus_sk(sk); + struct dentry *dentry; + struct vfsmount *mnt; + struct sock *skpair; + struct sk_buff *skb; + int state; + + dbus_remove_socket(sk); + + /* Clear state */ + dbus_state_lock(sk); + sock_orphan(sk); + sk->sk_shutdown = SHUTDOWN_MASK; + dentry = u->dentry; + u->dentry = NULL; + mnt = u->mnt; + u->mnt = NULL; + state = sk->sk_state; + sk->sk_state = TCP_CLOSE; + dbus_state_unlock(sk); + + wake_up_interruptible_all(&u->peer_wait); + + skpair = dbus_peer(sk); + + if (skpair != NULL) { + if (sk->sk_type == SOCK_STREAM) { + dbus_state_lock(skpair); + /* No more writes */ + skpair->sk_shutdown = SHUTDOWN_MASK; + if (!skb_queue_empty(&sk->sk_receive_queue) || embrion) + skpair->sk_err = ECONNRESET; + dbus_state_unlock(skpair); + skpair->sk_state_change(skpair); + sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP); + } + sock_put(skpair); /* It may now die */ + dbus_peer(sk) = NULL; + } + + /* Try to flush out this socket. Throw out buffers at least */ + + while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { + if (state == TCP_LISTEN) + dbus_release_sock(skb->sk, 1); + /* passed fds are erased in the kfree_skb hook */ + kfree_skb(skb); + } + + if (dentry) { + dput(dentry); + mntput(mnt); + } + + sock_put(sk); + + /* ---- Socket is dead now and most probably destroyed ---- */ + + /* + * Fixme: BSD difference: In BSD all sockets connected to use get + * ECONNRESET and we die on the spot. In Linux we behave + * like files and pipes do and wait for the last + * dereference. + * + * Can't we simply set sock->err? + * + * What the above comment does talk about? --ANK(980817) + */ + + if (dbus_tot_inflight) + dbus_gc(); /* Garbage collect fds */ + + return 0; +} + +static int dbus_listen(struct socket *sock, int backlog) +{ + int err; + struct sock *sk = sock->sk; + struct dbus_sock *u = dbus_sk(sk); + + err = -EOPNOTSUPP; + if (sock->type != SOCK_STREAM) + goto out; /* Only stream sockets accept */ + err = -EINVAL; + if (!u->addr) + goto out; /* No listens on an unbound socket */ + + u->bus->matchmaker = bus_matchmaker_new(); + + dbus_state_lock(sk); + if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN) + goto out_unlock; + if (backlog > sk->sk_max_ack_backlog) + wake_up_interruptible_all(&u->peer_wait); + sk->sk_max_ack_backlog = backlog; + sk->sk_state = TCP_LISTEN; + /* set credentials so connect can copy them */ + sk->sk_peercred.pid = task_tgid_vnr(current); + current_euid_egid(&sk->sk_peercred.uid, &sk->sk_peercred.gid); + err = 0; + +out_unlock: + dbus_state_unlock(sk); +out: + return err; +} + +static int dbus_release(struct socket *); +static int dbus_bind(struct socket *, struct sockaddr *, int); +static int dbus_stream_connect(struct socket *, struct sockaddr *, + int addr_len, int flags); +static int dbus_socketpair(struct socket *, struct socket *); +static int dbus_accept(struct socket *, struct socket *, int); +static int dbus_getname(struct socket *, struct sockaddr *, int *, int); +static unsigned int dbus_poll(struct file *, struct socket *, poll_table *); +static int dbus_ioctl(struct socket *, unsigned int, unsigned long); +static int dbus_shutdown(struct socket *, int); +static int dbus_stream_sendmsg(struct kiocb *, struct socket *, + struct msghdr *, size_t); +static int dbus_stream_recvmsg(struct kiocb *, struct socket *, + struct msghdr *, size_t, int); + +static const struct proto_ops dbus_stream_ops = { + .family = PF_DBUS, + .owner = THIS_MODULE, + .release = dbus_release, + .bind = dbus_bind, + .connect = dbus_stream_connect, + .socketpair = dbus_socketpair, + .accept = dbus_accept, + .getname = dbus_getname, + .poll = dbus_poll, + .ioctl = dbus_ioctl, + .listen = dbus_listen, + .shutdown = dbus_shutdown, + .setsockopt = sock_no_setsockopt, + .getsockopt = sock_no_getsockopt, + .sendmsg = dbus_stream_sendmsg, + .recvmsg = dbus_stream_recvmsg, + .mmap = sock_no_mmap, + .sendpage = sock_no_sendpage, +}; + +static struct proto dbus_proto = { + .name = "DBUS", + .owner = THIS_MODULE, + .obj_size = sizeof(struct dbus_sock), +}; + +struct dbus_sock_priv { + int disabled; +}; + +/* + * AF_DBUS sockets do not interact with hardware, hence they + * dont trigger interrupts - so it's safe for them to have + * bh-unsafe locking for their sk_receive_queue.lock. Split off + * this special lock-class by reinitializing the spinlock key: + */ +static struct lock_class_key af_dbus_sk_receive_queue_lock_key; + +static struct sock *dbus_create1(struct net *net, struct socket *sock) +{ + struct sock *sk = NULL; + struct dbus_sock *u; + struct dbus_sock_priv *priv = NULL; + + atomic_inc(&dbus_nr_socks); + if (atomic_read(&dbus_nr_socks) > 2 * get_max_files()) + goto out; + + priv = kzalloc(sizeof(struct dbus_sock_priv), GFP_KERNEL); + if (!priv) + goto out; + + sk = sk_alloc(net, PF_DBUS, GFP_KERNEL, &dbus_proto); + if (!sk) { + kfree(priv); + goto out; + } + + sock_init_data(sock, sk); + lockdep_set_class(&sk->sk_receive_queue.lock, + &af_dbus_sk_receive_queue_lock_key); + + sk->sk_write_space = dbus_write_space; + sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen; + sk->sk_destruct = dbus_sock_destructor; + u = dbus_sk(sk); + u->dentry = NULL; + u->mnt = NULL; + spin_lock_init(&u->lock); + atomic_long_set(&u->inflight, 0); + INIT_LIST_HEAD(&u->link); + mutex_init(&u->readlock); /* single task reading lock */ + init_waitqueue_head(&u->peer_wait); + u->priv = priv; + u->daemon_side = 0; + u->authenticated = 0; + u->uniq_name = NULL; + u->bus = NULL; + INIT_HLIST_NODE(&u->bus_link); + dbus_insert_socket(dbus_sockets_unbound, sk); +out: + if (sk == NULL) + atomic_dec(&dbus_nr_socks); + else { + local_bh_disable(); + sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); + local_bh_enable(); + } + return sk; +} + +static int dbus_create(struct net *net, struct socket *sock, int protocol, + int kern) +{ + if (protocol && protocol != PF_DBUS) + return -EPROTONOSUPPORT; + + sock->state = SS_UNCONNECTED; + + switch (sock->type) { + case SOCK_STREAM: + sock->ops = &dbus_stream_ops; + break; + /* + * Believe it or not BSD has AF_UNIX, SOCK_RAW though + * nothing uses it. + */ + default: + return -ESOCKTNOSUPPORT; + } + + return dbus_create1(net, sock) ? 0 : -ENOMEM; +} + +static int dbus_release(struct socket *sock) +{ + struct sock *sk = sock->sk; + + if (!sk) + return 0; + + sock->sk = NULL; + + return dbus_release_sock(sk, 0); +} + +static int dbus_autobind(struct socket *sock) +{ + struct sock *sk = sock->sk; + struct net *net = sock_net(sk); + struct dbus_sock *u = dbus_sk(sk); + static u32 ordernum = 1; + struct dbus_address *addr; + int err; + + mutex_lock(&u->readlock); + + err = 0; + if (u->addr) + goto out; + + err = -ENOMEM; + addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL); + if (!addr) + goto out; + + addr->name->sun_family = AF_DBUS; + atomic_set(&addr->refcnt, 1); + +retry: + addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short); + addr->hash = dbus_hash_fold(csum_partial(addr->name, addr->len, 0)); + + spin_lock(&dbus_table_lock); + ordernum = (ordernum+1)&0xFFFFF; + + if (__dbus_find_socket_byname(net, addr->name, addr->len, sock->type, + addr->hash)) { + spin_unlock(&dbus_table_lock); + /* Sanity yield. It is unusual case, but yet... */ + if (!(ordernum&0xFF)) + yield(); + goto retry; + } + addr->hash ^= sk->sk_type; + + __dbus_remove_socket(sk); + u->addr = addr; + __dbus_insert_socket(&dbus_socket_table[addr->hash], sk); + spin_unlock(&dbus_table_lock); + err = 0; + +out: mutex_unlock(&u->readlock); + return err; +} + +static struct sock *dbus_find_other(struct net *net, + struct sockaddr_un *sunname, int len, + int type, unsigned hash, int *error) +{ + struct sock *u; + struct path path; + int err = 0; + + if (sunname->sun_path[0]) { + struct inode *inode; + err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path); + if (err) + goto fail; + inode = path.dentry->d_inode; + err = inode_permission(inode, MAY_WRITE); + if (err) + goto put_fail; + + err = -ECONNREFUSED; + if (!S_ISSOCK(inode->i_mode)) + goto put_fail; + u = dbus_find_socket_byinode(net, inode); + if (!u) + goto put_fail; + + if (u->sk_type == type) + touch_atime(path.mnt, path.dentry); + + path_put(&path); + + err = -EPROTOTYPE; + if (u->sk_type != type) { + sock_put(u); + goto fail; + } + } else { + err = -ECONNREFUSED; + u = dbus_find_socket_byname(net, sunname, len, type, hash); + if (u) { + struct dentry *dentry; + dentry = dbus_sk(u)->dentry; + if (dentry) + touch_atime(dbus_sk(u)->mnt, dentry); + } else + goto fail; + } + return u; + +put_fail: + path_put(&path); +fail: + *error = err; + return NULL; +} + + +static int dbus_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) +{ + struct sock *sk = sock->sk; + struct net *net = sock_net(sk); + struct dbus_sock *u = dbus_sk(sk); + struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; + struct dentry *dentry = NULL; + struct nameidata nd; + int err; + unsigned hash; + struct dbus_address *addr; + struct hlist_head *list; + + err = -EINVAL; + if (sunaddr->sun_family != AF_DBUS) + goto out; + + if (addr_len == sizeof(short)) { + err = dbus_autobind(sock); + goto out; + } + + err = dbus_mkname(sunaddr, addr_len, &hash); + if (err < 0) + goto out; + addr_len = err; + + mutex_lock(&u->readlock); + + err = -EINVAL; + if (u->addr) + goto out_up; + + err = -ENOMEM; + addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL); + if (!addr) + goto out_up; + + u->bus = dbus_bus_new(); + if (!u->bus) { + kfree(addr); + goto out; + } + + memcpy(addr->name, sunaddr, addr_len); + addr->len = addr_len; + addr->hash = hash ^ sk->sk_type; + atomic_set(&addr->refcnt, 1); + + if (sunaddr->sun_path[0]) { + unsigned int mode; + err = 0; + /* + * Get the parent directory, calculate the hash for last + * component. + */ + err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd); + if (err) + goto out_mknod_parent; + + dentry = lookup_create(&nd, 0); + err = PTR_ERR(dentry); + if (IS_ERR(dentry)) + goto out_mknod_unlock; + + /* + * All right, let's create it. + */ + mode = S_IFSOCK | + (SOCK_INODE(sock)->i_mode & ~current_umask()); + err = mnt_want_write(nd.path.mnt); + if (err) + goto out_mknod_dput; + err = security_path_mknod(&nd.path, dentry, mode, 0); + if (err) + goto out_mknod_drop_write; + err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0); +out_mknod_drop_write: + mnt_drop_write(nd.path.mnt); + if (err) + goto out_mknod_dput; + mutex_unlock(&nd.path.dentry->d_inode->i_mutex); + dput(nd.path.dentry); + nd.path.dentry = dentry; + + addr->hash = DBUS_HASH_SIZE; + } + + spin_lock(&dbus_table_lock); + + if (!sunaddr->sun_path[0]) { + err = -EADDRINUSE; + if (__dbus_find_socket_byname(net, sunaddr, addr_len, + sk->sk_type, hash)) { + dbus_release_addr(addr); + goto out_unlock; + } + + list = &dbus_socket_table[addr->hash]; + } else { + list = &dbus_socket_table[dentry->d_inode->i_ino & (DBUS_HASH_SIZE-1)]; + u->dentry = nd.path.dentry; + u->mnt = nd.path.mnt; + } + + err = 0; + __dbus_remove_socket(sk); + u->addr = addr; + __dbus_insert_socket(list, sk); + +out_unlock: + spin_unlock(&dbus_table_lock); +out_up: + mutex_unlock(&u->readlock); +out: + return err; + +out_mknod_dput: + dput(dentry); +out_mknod_unlock: + mutex_unlock(&nd.path.dentry->d_inode->i_mutex); + path_put(&nd.path); +out_mknod_parent: + if (err == -EEXIST) + err = -EADDRINUSE; + dbus_release_addr(addr); + goto out_up; +} + +static long dbus_wait_for_peer(struct sock *other, long timeo) +{ + struct dbus_sock *u = dbus_sk(other); + int sched; + DEFINE_WAIT(wait); + + prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE); + + sched = !sock_flag(other, SOCK_DEAD) && + !(other->sk_shutdown & RCV_SHUTDOWN) && + dbus_recvq_full(other); + + dbus_state_unlock(other); + + if (sched) + timeo = schedule_timeout(timeo); + + finish_wait(&u->peer_wait, &wait); + return timeo; +} + +static int dbus_stream_connect(struct socket *sock, struct sockaddr *uaddr, + int addr_len, int flags) +{ + struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; + struct sock *sk = sock->sk; + struct net *net = sock_net(sk); + struct dbus_sock *u = dbus_sk(sk), *newu, *otheru; + struct sock *newsk = NULL; + struct sock *other = NULL; + struct sk_buff *skb = NULL; + unsigned hash; + int st; + int err; + long timeo; + + err = dbus_mkname(sunaddr, addr_len, &hash); + if (err < 0) + goto out; + addr_len = err; + + if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr && + (err = dbus_autobind(sock)) != 0) + goto out; + + timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); + + /* First of all allocate resources. + If we will make it after state is locked, + we will have to recheck all again in any case. + */ + + err = -ENOMEM; + + /* create new sock for complete connection */ + newsk = dbus_create1(sock_net(sk), NULL); + if (newsk == NULL) + goto out; + + /* Allocate skb for sending to listening sock */ + skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL); + if (skb == NULL) + goto out; + +restart: + /* Find listening sock. */ + other = dbus_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err); + if (!other) + goto out; + + /* Latch state of peer */ + dbus_state_lock(other); + + /* Apparently VFS overslept socket death. Retry. */ + if (sock_flag(other, SOCK_DEAD)) { + dbus_state_unlock(other); + sock_put(other); + goto restart; + } + + err = -ECONNREFUSED; + if (other->sk_state != TCP_LISTEN) + goto out_unlock; + if (other->sk_shutdown & RCV_SHUTDOWN) + goto out_unlock; + + if (dbus_recvq_full(other)) { + err = -EAGAIN; + if (!timeo) + goto out_unlock; + + timeo = dbus_wait_for_peer(other, timeo); + + err = sock_intr_errno(timeo); + if (signal_pending(current)) + goto out; + sock_put(other); + goto restart; + } + + /* Latch our state. + + It is tricky place. We need to grab write lock and cannot + drop lock on peer. It is dangerous because deadlock is + possible. Connect to self case and simultaneous + attempt to connect are eliminated by checking socket + state. other is TCP_LISTEN, if sk is TCP_LISTEN we + check this before attempt to grab lock. + + Well, and we have to recheck the state after socket locked. + */ + st = sk->sk_state; + + switch (st) { + case TCP_CLOSE: + /* This is ok... continue with connect */ + break; + case TCP_ESTABLISHED: + /* Socket is already connected */ + err = -EISCONN; + goto out_unlock; + default: + err = -EINVAL; + goto out_unlock; + } + + dbus_state_lock_nested(sk); + + if (sk->sk_state != st) { + dbus_state_unlock(sk); + dbus_state_unlock(other); + sock_put(other); + goto restart; + } + + err = security_dbus_stream_connect(sock, other->sk_socket, newsk); + if (err) { + dbus_state_unlock(sk); + goto out_unlock; + } + + /* The way is open! Fastly set all the necessary fields... */ + + sock_hold(sk); + dbus_peer(newsk) = sk; + newsk->sk_state = TCP_ESTABLISHED; + newsk->sk_type = sk->sk_type; + newsk->sk_peercred.pid = task_tgid_vnr(current); + current_euid_egid(&newsk->sk_peercred.uid, &newsk->sk_peercred.gid); + newu = dbus_sk(newsk); + newu->daemon_side = 1; + newsk->sk_wq = &newu->peer_wq; + otheru = dbus_sk(other); + newu->bus = otheru->bus; + u->bus = otheru->bus; + + /* copy address information from listening to new sock*/ + if (otheru->addr) { + atomic_inc(&otheru->addr->refcnt); + newu->addr = otheru->addr; + } + if (otheru->dentry) { + newu->dentry = dget(otheru->dentry); + newu->mnt = mntget(otheru->mnt); + } + + /* Set credentials */ + sk->sk_peercred = other->sk_peercred; + + sock->state = SS_CONNECTED; + sk->sk_state = TCP_ESTABLISHED; + sock_hold(newsk); + + smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */ + dbus_peer(sk) = newsk; + + dbus_state_unlock(sk); + + /* take ten and and send info to listening sock */ + spin_lock(&other->sk_receive_queue.lock); + __skb_queue_tail(&other->sk_receive_queue, skb); + spin_unlock(&other->sk_receive_queue.lock); + dbus_state_unlock(other); + other->sk_data_ready(other, 0); + sock_put(other); + return 0; + +out_unlock: + if (other) + dbus_state_unlock(other); + +out: + kfree_skb(skb); + if (newsk) + dbus_release_sock(newsk, 0); + if (other) + sock_put(other); + return err; +} + +static int dbus_socketpair(struct socket *socka, struct socket *sockb) +{ + struct sock *ska = socka->sk, *skb = sockb->sk; + + /* Join our sockets back to back */ + sock_hold(ska); + sock_hold(skb); + dbus_peer(ska) = skb; + dbus_peer(skb) = ska; + ska->sk_peercred.pid = skb->sk_peercred.pid = task_tgid_vnr(current); + current_euid_egid(&skb->sk_peercred.uid, &skb->sk_peercred.gid); + ska->sk_peercred.uid = skb->sk_peercred.uid; + ska->sk_peercred.gid = skb->sk_peercred.gid; + + if (ska->sk_type != SOCK_DGRAM) { + ska->sk_state = TCP_ESTABLISHED; + skb->sk_state = TCP_ESTABLISHED; + socka->state = SS_CONNECTED; + sockb->state = SS_CONNECTED; + } + return 0; +} + +static int dbus_accept(struct socket *sock, struct socket *newsock, int flags) +{ + struct sock *sk = sock->sk; + struct sock *tsk; + struct sk_buff *skb; + int err; + + err = -EOPNOTSUPP; + if (sock->type != SOCK_STREAM && sock->type) + goto out; + + err = -EINVAL; + if (sk->sk_state != TCP_LISTEN) + goto out; + + /* If socket state is TCP_LISTEN it cannot change (for now...), + * so that no locks are necessary. + */ + + skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err); + if (!skb) { + /* This means receive shutdown. */ + if (err == 0) + err = -EINVAL; + goto out; + } + + tsk = skb->sk; + skb_free_datagram(sk, skb); + wake_up_interruptible(&dbus_sk(sk)->peer_wait); + + /* attach accepted sock to socket */ + dbus_state_lock(tsk); + newsock->state = SS_CONNECTED; + sock_graft(tsk, newsock); + dbus_state_unlock(tsk); + return 0; + +out: + return err; +} + + +static int dbus_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer) +{ + struct sock *sk = sock->sk; + struct dbus_sock *u; + DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr); + int err = 0; + + if (peer) { + sk = dbus_peer_get(sk); + + err = -ENOTCONN; + if (!sk) + goto out; + err = 0; + } else { + sock_hold(sk); + } + + u = dbus_sk(sk); + dbus_state_lock(sk); + if (!u->addr) { + sunaddr->sun_family = AF_DBUS; + sunaddr->sun_path[0] = 0; + *uaddr_len = sizeof(short); + } else { + struct dbus_address *addr = u->addr; + + *uaddr_len = addr->len; + memcpy(sunaddr, addr->name, *uaddr_len); + } + dbus_state_unlock(sk); + sock_put(sk); +out: + return err; +} + +static void dbus_detach_fds(struct scm_cookie *scm, struct sk_buff *skb) +{ + int i; + + scm->fp = DBUSCB(skb).fp; + skb->destructor = sock_wfree; + DBUSCB(skb).fp = NULL; + + for (i = scm->fp->count-1; i >= 0; i--) + dbus_notinflight(scm->fp->fp[i]); +} + +static void dbus_destruct_fds(struct sk_buff *skb) +{ + struct scm_cookie scm; + memset(&scm, 0, sizeof(scm)); + dbus_detach_fds(&scm, skb); + + /* Alas, it calls VFS */ + /* So fscking what? fput() had been SMP-safe since the last Summer */ + scm_destroy(&scm); + sock_wfree(skb); +} + +static int dbus_attach_fds(struct scm_cookie *scm, struct sk_buff *skb) +{ + int i; + + /* + * Need to duplicate file references for the sake of garbage + * collection. Otherwise a socket in the fps might become a + * candidate for GC while the skb is not yet queued. + */ + DBUSCB(skb).fp = scm_fp_dup(scm->fp); + if (!DBUSCB(skb).fp) + return -ENOMEM; + + for (i = scm->fp->count-1; i >= 0; i--) + dbus_inflight(scm->fp->fp[i]); + skb->destructor = dbus_destruct_fds; + return 0; +} + +/* + * Send AF_DBUS data. + */ + +struct delivery_data_t { + struct kiocb *kiocb; + struct msghdr *msg; + struct sk_buff *skb; + size_t len; +}; + +static void dbus_stream_deliver(struct sock *other, void *user_data) +{ + struct delivery_data_t *data = user_data; + struct kiocb *kiocb = data->kiocb; + struct msghdr *msg = data->msg; + size_t len = data->len; + + int err; + struct sock_iocb *siocb = kiocb_to_siocb(kiocb); + struct sk_buff *skb_cloned; + bool fds_sent = false; + + if (other->sk_state != TCP_ESTABLISHED) { + pr_warning("D-Bus message delivery failure: non established socket!\n"); + return; + } + + skb_cloned = skb_clone(data->skb, GFP_KERNEL); + if (!skb_cloned) { + pr_warning("D-Bus message delivery failure: skb not cloned!\n"); + err = -ENOMEM; + goto out_err; + } + + memcpy(DBUSCREDS(skb_cloned), &siocb->scm->creds, sizeof(struct ucred)); + /* Only send the fds in the first buffer */ + if (siocb->scm->fp && !fds_sent) { + err = dbus_attach_fds(siocb->scm, skb_cloned); + if (err) { + pr_warning("D-Bus message delivery failure: " + "fds not attached!\n"); + kfree_skb(skb_cloned); + goto out_err; + } + fds_sent = true; + } + + dbus_state_lock(other); + + if (sock_flag(other, SOCK_DEAD) || + (other->sk_shutdown & RCV_SHUTDOWN)) + goto pipe_err_free; + + skb_queue_tail(&other->sk_receive_queue, skb_cloned); + dbus_state_unlock(other); + other->sk_data_ready(other, len); + + return; + +pipe_err_free: + dbus_state_unlock(other); + kfree_skb(skb_cloned); + + if (!(msg->msg_flags&MSG_NOSIGNAL)) + send_sig(SIGPIPE, current, 0); + err = -EPIPE; +out_err: + scm_destroy(siocb->scm); + siocb->scm = NULL; + return; +} + +static int dbus_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, + struct msghdr *msg, size_t len) +{ + int sent = 0; + struct sock_iocb *siocb = kiocb_to_siocb(kiocb); + struct sock *sk = sock->sk; + struct sock *other = dbus_peer(sk); + struct dbus_sock *otheru = dbus_sk(other); + struct hlist_node *other_node; + struct sockaddr_un *sunaddr = msg->msg_name; + int err; + struct scm_cookie tmp_scm; + struct DBusMessage dbus_message = {0,}; + unsigned char *message = NULL; + int parsing_ret; + int main_recipient_found = 0; + struct sk_buff *skb; + + pr_debug("D-Bus message received: %zd bytes\n", len); + + if (NULL == siocb->scm) + siocb->scm = &tmp_scm; + wait_for_dbus_gc(); + err = scm_send(sock, msg, siocb->scm); + if (err < 0) + return err; + + err = -EOPNOTSUPP; + if (msg->msg_flags&MSG_OOB) + goto out_err; + + if (msg->msg_namelen) { + err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; + goto out_err; + } else { + sunaddr = NULL; + err = -ENOTCONN; + if (sk->sk_state != TCP_ESTABLISHED) + goto out_err; + } + + if (sk->sk_shutdown & SEND_SHUTDOWN) + goto pipe_err; + + if (len > (8192 - 256)) { + pr_warning("Message too big, truncate\n"); + len = 8192 - 256; + } + + message = kmalloc(len, sk->sk_allocation); + if (!message) { + err = -ENOMEM; + goto out_err; + } + err = memcpy_fromiovecend(message, msg->msg_iov, 0, len); + if (err) + goto out_err; + + /* We can parse the message*/ + dbus_message.len = len; + dbus_message.new_len = len; + parsing_ret = dbus_message_parse(message, len, &dbus_message); + + /* 'NameAcquired' signal for uniq name */ + if (dbus_sk(sk)->daemon_side && parsing_ret == 0 && + dbus_message.type == DBUS_MESSAGE_TYPE_SIGNAL && + dbus_message.sender && dbus_message.path && + dbus_message.interface && dbus_message.member && + dbus_message.arg0 && + !strcmp(dbus_message.sender, "org.freedesktop.DBus") && + !strcmp(dbus_message.path, "/org/freedesktop/DBus") && + !strcmp(dbus_message.interface, "org.freedesktop.DBus") && + !strcmp(dbus_message.member, "NameAcquired")) { + if (dbus_message.arg0[0] == ':') + dbus_bus_add_connection(otheru->bus, otheru, + dbus_message.arg0); + else + dbus_bus_add_well_known_name(otheru->bus, otheru, + dbus_message.arg0); + } + + /* 'NameLost' signal for well-known name */ + if (dbus_sk(sk)->daemon_side && parsing_ret == 0 && + dbus_message.type == DBUS_MESSAGE_TYPE_SIGNAL && + dbus_message.sender && dbus_message.path && + dbus_message.interface && dbus_message.member && + dbus_message.arg0 && dbus_message.arg0[0] != ':' && + !strcmp(dbus_message.sender, "org.freedesktop.DBus") && + !strcmp(dbus_message.path, "/org/freedesktop/DBus") && + !strcmp(dbus_message.interface, "org.freedesktop.DBus") && + !strcmp(dbus_message.member, "NameLost")) { + if (dbus_message.arg0[0] == ':') + dbus_bus_disconnected(otheru->bus, otheru); + else + dbus_bus_remove_well_known_name(otheru->bus, otheru, + dbus_message.arg0); + } + + /* 'AddMatch' method call */ + if (!dbus_sk(sk)->daemon_side && parsing_ret == 0 && + dbus_message.type == DBUS_MESSAGE_TYPE_METHOD_CALL && + dbus_message.destination && dbus_message.path && + dbus_message.interface && dbus_message.member && + !strcmp(dbus_message.destination, "org.freedesktop.DBus") && + !strcmp(dbus_message.path, "/org/freedesktop/DBus") && + !strcmp(dbus_message.interface, "org.freedesktop.DBus") && + !strcmp(dbus_message.member, "AddMatch")) { + BusMatchRule *rule; + pr_debug("Add match rule \"%s\"\n", dbus_message.arg0); + rule = bus_match_rule_parse(dbus_sk(sk), dbus_message.arg0); + if (rule) { + bus_matchmaker_add_rule(dbus_sk(sk)->bus->matchmaker, rule); + pr_debug("Match rule added\n"); + } else + pr_warning("Match rule parsing failed\n"); + + } + + /* Add a sender field */ + if (!dbus_sk(sk)->daemon_side && parsing_ret == 0 && + !dbus_message.sender && dbus_sk(sk)->uniq_name && + (!dbus_message.destination || + strcmp(dbus_message.destination, "org.freedesktop.DBus"))) { + dbus_message_add_sender(&dbus_message, dbus_sk(sk)->uniq_name, + sk->sk_allocation); + message = dbus_message.message; + } + + skb = sock_alloc_send_skb(sk, dbus_message.new_len, + msg->msg_flags&MSG_DONTWAIT, &err); + + if (skb == NULL) + goto out_err; + + if (skb_tailroom(skb) < dbus_message.new_len || + dbus_message.new_len > ((sk->sk_sndbuf >> 1) - 64) || + dbus_message.new_len > SKB_MAX_ALLOC) { + /* We don't split messages here */ + pr_warning("D-Bus message too big (%zd) " + "for sk_sndbuf=%d tail_room=%d SKB_MAX_ALLOC=%lu\n", + dbus_message.new_len, sk->sk_sndbuf, skb_tailroom(skb), SKB_MAX_ALLOC); + kfree_skb(skb); + goto out_err; + } + + memcpy(skb_put(skb, dbus_message.new_len), message, + dbus_message.new_len); + + /* Delivery to the peer */ + if (dbus_sk(sk)->daemon_side || parsing_ret != 0 || + (dbus_message.destination && + !strcmp(dbus_message.destination, "org.freedesktop.DBus"))) { + struct delivery_data_t data; + data.kiocb = kiocb; + data.msg = msg; + data.len = dbus_message.new_len; + data.skb = skb; + other = dbus_peer(sk); + + if (dbus_sk(sk)->daemon_side) + pr_debug("Message '%s' " + "header '%s' -> '%s', " + "routed [dbus-daemon] -> '%s'\n", + dbus_message.member ? (char *)dbus_message.member : "(null)", + dbus_message.sender ? (char *)dbus_message.sender : "(null)", + dbus_message.destination ? (char *)dbus_message.destination : "(null)", + (char *)(dbus_sk(other)->uniq_name) ? + (char *)(dbus_sk(other)->uniq_name) : "(null)"); + else + pr_debug("Message '%s' " + "header '%s' -> '%s', " + "routed '%s' -> [dbus-daemon]\n", + dbus_message.member ? (char *)dbus_message.member : "(null)", + dbus_message.sender ? (char *)dbus_message.sender : "(null)", + dbus_message.destination ? (char *)dbus_message.destination : "(null)", + (char *)(dbus_sk(sk)->uniq_name) ? + (char *)(dbus_sk(sk)->uniq_name) : "(null)"); + + dbus_stream_deliver(other, &data); + goto skip_kernel_routing; + } + + /* Routed to the unique name or well-known name */ + hlist_for_each_entry(otheru, other_node, &dbus_sk(sk)->bus->connections, + bus_link) { + struct delivery_data_t data; + int i, found = 0; + other = &otheru->sk; + + if (other->sk_state != TCP_ESTABLISHED) { + continue; + } + if (other == sk) { + continue; + } + if (otheru->daemon_side) { + continue; + } + if (!otheru->authenticated) { + continue; + } + if (otheru->uniq_name == NULL || + dbus_message.destination == NULL) { + continue; + } + if (!strcmp(otheru->uniq_name, + dbus_message.destination)) { + found = 1; + } else for (i = 0 ; i < MAX_WELL_KNOWN_NAMES ; i++) { + char **name = &otheru->well_known_names[i]; + if (*name && dbus_message.destination && + !strcmp(*name, dbus_message.destination)) { + found = 1; + break; + } + } + if (!found) + continue; + + pr_debug("Message '%s' " + "header '%s' -> '%s', " + "routed '%s' -> '%s'\n", + dbus_message.member ? (char *)dbus_message.member : "(null)", + dbus_message.sender ? (char *)dbus_message.sender : "(null)", + dbus_message.destination ? (char *)dbus_message.destination : "(null)", + (char *)(dbus_sk(sk)->uniq_name) ? + (char *)(dbus_sk(sk)->uniq_name) : "(null)", + (char *)(otheru->uniq_name) ? + (char *)(otheru->uniq_name) : "(null)"); + + main_recipient_found = 1; + + data.kiocb = kiocb; + data.msg = msg; + data.len = dbus_message.new_len; + data.skb = skb; + dbus_stream_deliver(other, &data); + break; + } + + /* If we didn't find the destination, it is a D-Bus activation. D-Bus + * activation is done in userspace by dbus-daemon. */ + if (!main_recipient_found && dbus_message.destination) { + struct delivery_data_t data; + data.kiocb = kiocb; + data.msg = msg; + data.len = dbus_message.new_len; + data.skb = skb; + other = dbus_peer(sk); + + pr_debug("Message '%s' " + "header '%s' -> '%s', " + "routed '%s' -> [D-Bus activation]\n", + dbus_message.member ? (char *)dbus_message.member : "(null)", + dbus_message.sender ? (char *)dbus_message.sender : "(null)", + dbus_message.destination ? (char *)dbus_message.destination : "(null)", + (char *)(dbus_sk(sk)->uniq_name) ? + (char *)(dbus_sk(sk)->uniq_name) : "(null)"); + + dbus_stream_deliver(other, &data); + goto skip_kernel_routing; + } + + /* Generic delivery by match rules */ + if (!dbus_sk(sk)->daemon_side && parsing_ret == 0) { + struct delivery_data_t data; + data.kiocb = kiocb; + data.msg = msg; + data.len = dbus_message.new_len; + data.skb = skb; + + pr_debug("Message '%s' " + "header '%s' -> '%s', " + "routed '%s' -> [match rules]\n", + dbus_message.member ? (char *)dbus_message.member : "(null)", + dbus_message.sender ? (char *)dbus_message.sender : "(null)", + dbus_message.destination ? (char *)dbus_message.destination : "(null)", + (char *)(dbus_sk(sk)->uniq_name) ? + (char *)(dbus_sk(sk)->uniq_name) : "(null)"); + + bus_matchmaker_get_recipients(dbus_sk(sk)->bus->matchmaker, + dbus_sk(sk), NULL, &dbus_message, dbus_stream_deliver, + &data); + } + +skip_kernel_routing: + kfree_skb(skb); + scm_destroy(siocb->scm); + siocb->scm = NULL; + kfree(message); + + /* sent may be bigger than len but don't confuse userspace + * applications */ + return len; + +pipe_err: + if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL)) + send_sig(SIGPIPE, current, 0); + err = -EPIPE; +out_err: + scm_destroy(siocb->scm); + siocb->scm = NULL; + if (message) { + kfree(message); + } + return sent ? : err; +} + +static void dbus_copy_addr(struct msghdr *msg, struct sock *sk) +{ + struct dbus_sock *u = dbus_sk(sk); + + msg->msg_namelen = 0; + if (u->addr) { + msg->msg_namelen = u->addr->len; + memcpy(msg->msg_name, u->addr->name, u->addr->len); + } +} + +/* + * Sleep until data has arrive. But check for races.. + */ + +static long dbus_stream_data_wait(struct sock *sk, long timeo) +{ + DEFINE_WAIT(wait); + + dbus_state_lock(sk); + + for (;;) { + prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); + + if (!skb_queue_empty(&sk->sk_receive_queue) || + sk->sk_err || + (sk->sk_shutdown & RCV_SHUTDOWN) || + signal_pending(current) || + !timeo) + break; + + set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); + dbus_state_unlock(sk); + timeo = schedule_timeout(timeo); + dbus_state_lock(sk); + clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); + } + + finish_wait(sk_sleep(sk), &wait); + dbus_state_unlock(sk); + return timeo; +} + + + +static int dbus_stream_recvmsg(struct kiocb *iocb, struct socket *sock, + struct msghdr *msg, size_t size, + int flags) +{ + struct sock_iocb *siocb = kiocb_to_siocb(iocb); + struct scm_cookie tmp_scm; + struct sock *sk = sock->sk; + struct dbus_sock *u = dbus_sk(sk); + struct sockaddr_un *sunaddr = msg->msg_name; + int copied = 0; + int check_creds = 0; + int target; + int err = 0; + long timeo; + + err = -EINVAL; + if (sk->sk_state != TCP_ESTABLISHED) + goto out; + + err = -EOPNOTSUPP; + if (flags&MSG_OOB) + goto out; + + target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); + timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT); + + msg->msg_namelen = 0; + + /* Lock the socket to prevent queue disordering + * while sleeps in memcpy_tomsg + */ + + if (!siocb->scm) { + siocb->scm = &tmp_scm; + memset(&tmp_scm, 0, sizeof(tmp_scm)); + } + + mutex_lock(&u->readlock); + + do { + int chunk; + struct sk_buff *skb; + + dbus_state_lock(sk); + skb = skb_dequeue(&sk->sk_receive_queue); + if (skb == NULL) { + if (copied >= target) + goto unlock; + + /* + * POSIX 1003.1g mandates this order. + */ + + err = sock_error(sk); + if (err) + goto unlock; + if (sk->sk_shutdown & RCV_SHUTDOWN) + goto unlock; + + dbus_state_unlock(sk); + err = -EAGAIN; + if (!timeo) + break; + mutex_unlock(&u->readlock); + + timeo = dbus_stream_data_wait(sk, timeo); + + if (signal_pending(current)) { + err = sock_intr_errno(timeo); + goto out; + } + mutex_lock(&u->readlock); + continue; + unlock: + dbus_state_unlock(sk); + break; + } + dbus_state_unlock(sk); + + if (check_creds) { + /* Never glue messages from different writers */ + if (memcmp(DBUSCREDS(skb), &siocb->scm->creds, + sizeof(siocb->scm->creds)) != 0) { + skb_queue_head(&sk->sk_receive_queue, skb); + break; + } + } else { + /* Copy credentials */ + siocb->scm->creds = *DBUSCREDS(skb); + check_creds = 1; + } + + /* Copy address just once */ + if (sunaddr) { + dbus_copy_addr(msg, skb->sk); + sunaddr = NULL; + } + + chunk = min_t(unsigned int, skb->len, size); + if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) { + skb_queue_head(&sk->sk_receive_queue, skb); + if (copied == 0) + copied = -EFAULT; + break; + } + copied += chunk; + size -= chunk; + + /* Mark read part of skb as used */ + if (!(flags & MSG_PEEK)) { + skb_pull(skb, chunk); + + if (DBUSCB(skb).fp) + dbus_detach_fds(siocb->scm, skb); + + /* put the skb back if we didn't use it up.. */ + if (skb->len) { + skb_queue_head(&sk->sk_receive_queue, skb); + break; + } + + kfree_skb(skb); + + if (siocb->scm->fp) + break; + } else { + /* It is questionable, see note in dbus_dgram_recvmsg. + * FIXMEIM - dgram needed? + */ + if (DBUSCB(skb).fp) + siocb->scm->fp = scm_fp_dup(DBUSCB(skb).fp); + + /* put message back and return */ + skb_queue_head(&sk->sk_receive_queue, skb); + break; + } + } while (size); + + mutex_unlock(&u->readlock); + scm_recv(sock, msg, siocb->scm, flags); +out: + return copied ? : err; +} + +static int dbus_shutdown(struct socket *sock, int mode) +{ + struct sock *sk = sock->sk; + struct sock *other; + + mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN); + + if (mode) { + dbus_state_lock(sk); + sk->sk_shutdown |= mode; + other = dbus_peer(sk); + if (other) + sock_hold(other); + dbus_state_unlock(sk); + sk->sk_state_change(sk); + + if (other && (sk->sk_type == SOCK_STREAM)) { + + int peer_mode = 0; + + if (mode&RCV_SHUTDOWN) + peer_mode |= SEND_SHUTDOWN; + if (mode&SEND_SHUTDOWN) + peer_mode |= RCV_SHUTDOWN; + dbus_state_lock(other); + other->sk_shutdown |= peer_mode; + dbus_state_unlock(other); + other->sk_state_change(other); + if (peer_mode == SHUTDOWN_MASK) + sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP); + else if (peer_mode & RCV_SHUTDOWN) + sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN); + } + if (other) + sock_put(other); + } + return 0; +} + +static int dbus_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) +{ + struct sock *sk = sock->sk; + long amount = 0; + int err; + + switch (cmd) { + case SIOCOUTQ: // TIOCOUTQ == 0x5411 + amount = sk_wmem_alloc_get(sk); + err = put_user(amount, (int __user *)arg); + break; + case SIOCINQ: // FIONREAD == 0x541B + { + struct sk_buff *skb; + + if (sk->sk_state == TCP_LISTEN) { + err = -EINVAL; + break; + } + + spin_lock(&sk->sk_receive_queue.lock); + if (sk->sk_type == SOCK_STREAM) { + skb_queue_walk(&sk->sk_receive_queue, skb) + amount += skb->len; + } + spin_unlock(&sk->sk_receive_queue.lock); + err = put_user(amount, (int __user *)arg); + break; + } + + case 0x5420: + pr_debug("D-Bus ioctl 0x5420 -> returns 42\n"); + amount = 42; + err = put_user(amount, (int __user *)arg); + break; + default: + err = -ENOIOCTLCMD; + break; + } + return err; +} + +static unsigned int dbus_poll(struct file *file, struct socket *sock, poll_table *wait) +{ + struct sock *sk = sock->sk; + unsigned int mask; + + sock_poll_wait(file, sk_sleep(sk), wait); + mask = 0; + + /* exceptional events? */ + if (sk->sk_err) + mask |= POLLERR; + if (sk->sk_shutdown == SHUTDOWN_MASK) + mask |= POLLHUP; + if (sk->sk_shutdown & RCV_SHUTDOWN) + mask |= POLLRDHUP; + + /* 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_STREAM) && + sk->sk_state == TCP_CLOSE) + mask |= POLLHUP; + + /* + * we set writable also when the other side has shut down the + * connection. This prevents stuck sockets. + */ + if (dbus_writable(sk)) + mask |= POLLOUT | POLLWRNORM | POLLWRBAND; + + return mask; +} + +#ifdef CONFIG_PROC_FS +static struct sock *first_dbus_socket(int *i) +{ + for (*i = 0; *i <= DBUS_HASH_SIZE; (*i)++) { + if (!hlist_empty(&dbus_socket_table[*i])) + return __sk_head(&dbus_socket_table[*i]); + } + return NULL; +} + +static struct sock *next_dbus_socket(int *i, struct sock *s) +{ + struct sock *next = sk_next(s); + /* More in this chain? */ + if (next) + return next; + /* Look for next non-empty chain. */ + for ((*i)++; *i <= DBUS_HASH_SIZE; (*i)++) { + if (!hlist_empty(&dbus_socket_table[*i])) + return __sk_head(&dbus_socket_table[*i]); + } + return NULL; +} + +struct dbus_iter_state { + struct seq_net_private p; + int i; +}; + +static struct sock *dbus_seq_idx(struct seq_file *seq, loff_t pos) +{ + struct dbus_iter_state *iter = seq->private; + loff_t off = 0; + struct sock *s; + + for (s = first_dbus_socket(&iter->i); s; s = next_dbus_socket(&iter->i, s)) { + if (sock_net(s) != seq_file_net(seq)) + continue; + if (off == pos) + return s; + ++off; + } + return NULL; +} + +static void *dbus_seq_start(struct seq_file *seq, loff_t *pos) + __acquires(dbus_table_lock) +{ + spin_lock(&dbus_table_lock); + return *pos ? dbus_seq_idx(seq, *pos - 1) : SEQ_START_TOKEN; +} + +static void *dbus_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct dbus_iter_state *iter = seq->private; + struct sock *sk = v; + ++*pos; + + if (v == SEQ_START_TOKEN) + sk = first_dbus_socket(&iter->i); + else + sk = next_dbus_socket(&iter->i, sk); + while (sk && (sock_net(sk) != seq_file_net(seq))) + sk = next_dbus_socket(&iter->i, sk); + return sk; +} + +static void dbus_seq_stop(struct seq_file *seq, void *v) + __releases(dbus_table_lock) +{ + spin_unlock(&dbus_table_lock); +} + +static int dbus_seq_show(struct seq_file *seq, void *v) +{ + + if (v == SEQ_START_TOKEN) + seq_puts(seq, "Num RefCount Protocol Flags Type St " + "Inode Path\n"); + else { + struct sock *s = v; + struct dbus_sock *u = dbus_sk(s); + dbus_state_lock(s); + + seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu", + s, + atomic_read(&s->sk_refcnt), + 0, + s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0, + s->sk_type, + s->sk_socket ? + (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) : + (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING), + sock_i_ino(s)); + + if (u->addr) { + int i, len; + seq_putc(seq, ' '); + + i = 0; + len = u->addr->len - sizeof(short); + if (!DBUS_ABSTRACT(s)) + len--; + else { + seq_putc(seq, '@'); + i++; + } + for ( ; i < len; i++) + seq_putc(seq, u->addr->name->sun_path[i]); + } + dbus_state_unlock(s); + seq_putc(seq, '\n'); + } + + return 0; +} + +static const struct seq_operations dbus_seq_ops = { + .start = dbus_seq_start, + .next = dbus_seq_next, + .stop = dbus_seq_stop, + .show = dbus_seq_show, +}; + +static int dbus_seq_open(struct inode *inode, struct file *file) +{ + return seq_open_net(inode, file, &dbus_seq_ops, + sizeof(struct dbus_iter_state)); +} + +static const struct file_operations dbus_seq_fops = { + .owner = THIS_MODULE, + .open = dbus_seq_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release_net, +}; + +#endif + +static const struct net_proto_family dbus_family_ops = { + .family = PF_DBUS, + .create = dbus_create, + .owner = THIS_MODULE, +}; + + +static int dbus_net_init(struct net *net) +{ + int error = -ENOMEM; + + net->unx.sysctl_max_dgram_qlen = 10; + if (dbus_sysctl_register(net)) + goto out; + +#ifdef CONFIG_PROC_FS + if (!proc_net_fops_create(net, "dbus", 0, &dbus_seq_fops)) { + dbus_sysctl_unregister(net); + goto out; + } +#endif + error = 0; +out: + return error; +} + +static void dbus_net_exit(struct net *net) +{ + dbus_sysctl_unregister(net); + proc_net_remove(net, "dbus"); +} + +static struct pernet_operations dbus_net_ops = { + .init = dbus_net_init, + .exit = dbus_net_exit, +}; + +static int __init af_dbus_init(void) +{ + int rc = -1; + struct sk_buff *dummy_skb; + + BUILD_BUG_ON(sizeof(struct dbus_skb_parms) > sizeof(dummy_skb->cb)); + + rc = proto_register(&dbus_proto, 1); + if (rc != 0) { + printk(KERN_CRIT "%s: Cannot create dbus_sock SLAB cache!\n", + __func__); + goto out; + } + + sock_register(&dbus_family_ops); + register_pernet_subsys(&dbus_net_ops); +out: + return rc; +} + +static void __exit af_dbus_exit(void) +{ + sock_unregister(PF_DBUS); + proto_unregister(&dbus_proto); + unregister_pernet_subsys(&dbus_net_ops); +} + +/* Earlier than device_initcall() so that other drivers invoking + request_module() don't end up in a loop when modprobe tries + to use a DBUS socket. But later than subsys_initcall() because + we depend on stuff initialised there */ +fs_initcall(af_dbus_init); +module_exit(af_dbus_exit); + +MODULE_LICENSE("GPL"); +MODULE_ALIAS_NETPROTO(PF_DBUS); diff --git a/net/dbus/bus.c b/net/dbus/bus.c new file mode 100644 index 0000000..1da98a6 --- /dev/null +++ b/net/dbus/bus.c @@ -0,0 +1,91 @@ +/* bus.c The D-Bus bus + * + * Copyright (C) 2010 Collabora Ltd + * Authors: Alban Crequy + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "bus.h" +#include "dbus-protocol.h" + +struct DBusBus * +dbus_bus_new (void) +{ + struct DBusBus *bus = kmalloc(sizeof(struct DBusBus), GFP_KERNEL); + INIT_HLIST_HEAD(&bus->connections); + return bus; +} + +void +dbus_bus_free (struct DBusBus *bus) +{ + kfree(bus); +} + +void +dbus_bus_add_connection (struct DBusBus *bus, struct dbus_sock *conn, + const char *unique_name) +{ + BUG_ON(!unique_name); + BUG_ON(unique_name[0] != ':'); + + conn->authenticated = 1; + conn->uniq_name = + kmalloc(strlen(unique_name)+1, GFP_KERNEL); + strcpy(conn->uniq_name, unique_name); + + hlist_add_head(&conn->bus_link, &bus->connections); +} + +void +dbus_bus_disconnected(struct DBusBus *bus, struct dbus_sock *conn) +{ + if (!bus) + return; + + hlist_del_init(&conn->bus_link); +} + +void +dbus_bus_add_well_known_name (struct DBusBus *bus, + struct dbus_sock *conn, const char *name) +{ + int i; + for (i = 0 ; i < MAX_WELL_KNOWN_NAMES ; i++) { + char **nameptr = &conn->well_known_names[i]; + if (!*nameptr) { + *nameptr = kstrdup(name, GFP_KERNEL); + break; + } + } +} + +void +dbus_bus_remove_well_known_name (struct DBusBus *bus, + struct dbus_sock *conn, const char *name) +{ + int i; + for (i = 0 ; i < MAX_WELL_KNOWN_NAMES ; i++) { + char **nameptr = &conn->well_known_names[i]; + if (*nameptr && name && + !strcmp(name, *nameptr)) { + kfree(*nameptr); + *nameptr = NULL; + break; + } + } +} diff --git a/net/dbus/bus.h b/net/dbus/bus.h new file mode 100644 index 0000000..88100f6 --- /dev/null +++ b/net/dbus/bus.h @@ -0,0 +1,45 @@ +/* bus.h The D-Bus Bus + * + * Copyright (C) 2010 Collabora Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef DBUS_BUS_H +#define DBUS_BUS_H + +#include +#include "matchrule.h" + +struct DBusBus { + struct hlist_head connections; + BusMatchmaker* matchmaker; +}; + + +struct DBusBus *dbus_bus_new (void); +void dbus_bus_free (struct DBusBus *bus); + +void dbus_bus_add_connection (struct DBusBus *bus, struct dbus_sock *conn, + const char *unique_name); +void dbus_bus_disconnected(struct DBusBus *bus, struct dbus_sock *conn); + +void dbus_bus_add_well_known_name (struct DBusBus *bus, + struct dbus_sock *conn, const char *name); +void dbus_bus_remove_well_known_name (struct DBusBus *bus, + struct dbus_sock *conn, const char *name); + +#endif /* DBUS_BUS_H */ diff --git a/net/dbus/dbus-protocol.h b/net/dbus/dbus-protocol.h new file mode 100644 index 0000000..17798e9 --- /dev/null +++ b/net/dbus/dbus-protocol.h @@ -0,0 +1,462 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-protocol.h D-Bus protocol constants + * + * Copyright (C) 2002, 2003 CodeFactory AB + * Copyright (C) 2004, 2005 Red Hat, Inc. + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef DBUS_PROTOCOL_H +#define DBUS_PROTOCOL_H + +/* Don't include anything in here from anywhere else. It's + * intended for use by any random library. + */ + +#ifdef __cplusplus +extern "C" { +#if 0 +} /* avoids confusing emacs indentation */ +#endif +#endif + +/* Normally docs are in .c files, but there isn't a .c file for this. */ +/** + * @defgroup DBusProtocol Protocol constants + * @ingroup DBus + * + * @brief Defines constants which are part of the D-Bus protocol + * + * This header is intended for use by any library, not only libdbus. + * + * @{ + */ + + +/* Message byte order */ +#define DBUS_LITTLE_ENDIAN ('l') /**< Code marking LSB-first byte order in the wire protocol. */ +#define DBUS_BIG_ENDIAN ('B') /**< Code marking MSB-first byte order in the wire protocol. */ + +/** Protocol version. */ +#define DBUS_MAJOR_PROTOCOL_VERSION 1 + +/** Type code that is never equal to a legitimate type code */ +#define DBUS_TYPE_INVALID ((int) '\0') +/** #DBUS_TYPE_INVALID as a string literal instead of a int literal */ +#define DBUS_TYPE_INVALID_AS_STRING "\0" + +/* Primitive types */ +/** Type code marking an 8-bit unsigned integer */ +#define DBUS_TYPE_BYTE ((int) 'y') +/** #DBUS_TYPE_BYTE as a string literal instead of a int literal */ +#define DBUS_TYPE_BYTE_AS_STRING "y" +/** Type code marking a boolean */ +#define DBUS_TYPE_BOOLEAN ((int) 'b') +/** #DBUS_TYPE_BOOLEAN as a string literal instead of a int literal */ +#define DBUS_TYPE_BOOLEAN_AS_STRING "b" +/** Type code marking a 16-bit signed integer */ +#define DBUS_TYPE_INT16 ((int) 'n') +/** #DBUS_TYPE_INT16 as a string literal instead of a int literal */ +#define DBUS_TYPE_INT16_AS_STRING "n" +/** Type code marking a 16-bit unsigned integer */ +#define DBUS_TYPE_UINT16 ((int) 'q') +/** #DBUS_TYPE_UINT16 as a string literal instead of a int literal */ +#define DBUS_TYPE_UINT16_AS_STRING "q" +/** Type code marking a 32-bit signed integer */ +#define DBUS_TYPE_INT32 ((int) 'i') +/** #DBUS_TYPE_INT32 as a string literal instead of a int literal */ +#define DBUS_TYPE_INT32_AS_STRING "i" +/** Type code marking a 32-bit unsigned integer */ +#define DBUS_TYPE_UINT32 ((int) 'u') +/** #DBUS_TYPE_UINT32 as a string literal instead of a int literal */ +#define DBUS_TYPE_UINT32_AS_STRING "u" +/** Type code marking a 64-bit signed integer */ +#define DBUS_TYPE_INT64 ((int) 'x') +/** #DBUS_TYPE_INT64 as a string literal instead of a int literal */ +#define DBUS_TYPE_INT64_AS_STRING "x" +/** Type code marking a 64-bit unsigned integer */ +#define DBUS_TYPE_UINT64 ((int) 't') +/** #DBUS_TYPE_UINT64 as a string literal instead of a int literal */ +#define DBUS_TYPE_UINT64_AS_STRING "t" +/** Type code marking an 8-byte double in IEEE 754 format */ +#define DBUS_TYPE_DOUBLE ((int) 'd') +/** #DBUS_TYPE_DOUBLE as a string literal instead of a int literal */ +#define DBUS_TYPE_DOUBLE_AS_STRING "d" +/** Type code marking a UTF-8 encoded, nul-terminated Unicode string */ +#define DBUS_TYPE_STRING ((int) 's') +/** #DBUS_TYPE_STRING as a string literal instead of a int literal */ +#define DBUS_TYPE_STRING_AS_STRING "s" +/** Type code marking a D-Bus object path */ +#define DBUS_TYPE_OBJECT_PATH ((int) 'o') +/** #DBUS_TYPE_OBJECT_PATH as a string literal instead of a int literal */ +#define DBUS_TYPE_OBJECT_PATH_AS_STRING "o" +/** Type code marking a D-Bus type signature */ +#define DBUS_TYPE_SIGNATURE ((int) 'g') +/** #DBUS_TYPE_SIGNATURE as a string literal instead of a int literal */ +#define DBUS_TYPE_SIGNATURE_AS_STRING "g" +/** Type code marking a unix file descriptor */ +#define DBUS_TYPE_UNIX_FD ((int) 'h') +/** #DBUS_TYPE_UNIX_FD as a string literal instead of a int literal */ +#define DBUS_TYPE_UNIX_FD_AS_STRING "h" + +/* Compound types */ +/** Type code marking a D-Bus array type */ +#define DBUS_TYPE_ARRAY ((int) 'a') +/** #DBUS_TYPE_ARRAY as a string literal instead of a int literal */ +#define DBUS_TYPE_ARRAY_AS_STRING "a" +/** Type code marking a D-Bus variant type */ +#define DBUS_TYPE_VARIANT ((int) 'v') +/** #DBUS_TYPE_VARIANT as a string literal instead of a int literal */ +#define DBUS_TYPE_VARIANT_AS_STRING "v" + +/** STRUCT and DICT_ENTRY are sort of special since their codes can't + * appear in a type string, instead + * DBUS_STRUCT_BEGIN_CHAR/DBUS_DICT_ENTRY_BEGIN_CHAR have to appear + */ +/** Type code used to represent a struct; however, this type code does not appear + * in type signatures, instead #DBUS_STRUCT_BEGIN_CHAR and #DBUS_STRUCT_END_CHAR will + * appear in a signature. + */ +#define DBUS_TYPE_STRUCT ((int) 'r') +/** #DBUS_TYPE_STRUCT as a string literal instead of a int literal */ +#define DBUS_TYPE_STRUCT_AS_STRING "r" +/** Type code used to represent a dict entry; however, this type code does not appear + * in type signatures, instead #DBUS_DICT_ENTRY_BEGIN_CHAR and #DBUS_DICT_ENTRY_END_CHAR will + * appear in a signature. + */ +#define DBUS_TYPE_DICT_ENTRY ((int) 'e') +/** #DBUS_TYPE_DICT_ENTRY as a string literal instead of a int literal */ +#define DBUS_TYPE_DICT_ENTRY_AS_STRING "e" + +/** Does not include #DBUS_TYPE_INVALID, #DBUS_STRUCT_BEGIN_CHAR, #DBUS_STRUCT_END_CHAR, + * #DBUS_DICT_ENTRY_BEGIN_CHAR, or #DBUS_DICT_ENTRY_END_CHAR - i.e. it is the number of + * valid types, not the number of distinct characters that may appear in a type signature. + */ +#define DBUS_NUMBER_OF_TYPES (16) + +/* characters other than typecodes that appear in type signatures */ + +/** Code marking the start of a struct type in a type signature */ +#define DBUS_STRUCT_BEGIN_CHAR ((int) '(') +/** #DBUS_STRUCT_BEGIN_CHAR as a string literal instead of a int literal */ +#define DBUS_STRUCT_BEGIN_CHAR_AS_STRING "(" +/** Code marking the end of a struct type in a type signature */ +#define DBUS_STRUCT_END_CHAR ((int) ')') +/** #DBUS_STRUCT_END_CHAR a string literal instead of a int literal */ +#define DBUS_STRUCT_END_CHAR_AS_STRING ")" +/** Code marking the start of a dict entry type in a type signature */ +#define DBUS_DICT_ENTRY_BEGIN_CHAR ((int) '{') +/** #DBUS_DICT_ENTRY_BEGIN_CHAR as a string literal instead of a int literal */ +#define DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING "{" +/** Code marking the end of a dict entry type in a type signature */ +#define DBUS_DICT_ENTRY_END_CHAR ((int) '}') +/** #DBUS_DICT_ENTRY_END_CHAR as a string literal instead of a int literal */ +#define DBUS_DICT_ENTRY_END_CHAR_AS_STRING "}" + +/** Max length in bytes of a bus name, interface, or member (not object + * path, paths are unlimited). This is limited because lots of stuff + * is O(n) in this number, plus it would be obnoxious to type in a + * paragraph-long method name so most likely something like that would + * be an exploit. + */ +#define DBUS_MAXIMUM_NAME_LENGTH 255 + +/** This one is 255 so it fits in a byte */ +#define DBUS_MAXIMUM_SIGNATURE_LENGTH 255 + +/** Max length of a match rule string; to keep people from hosing the + * daemon with some huge rule + */ +#define DBUS_MAXIMUM_MATCH_RULE_LENGTH 1024 + +/** Max arg number you can match on in a match rule, e.g. + * arg0='hello' is OK, arg3489720987='hello' is not + */ +#define DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER 63 + +/** Max length of a marshaled array in bytes (64M, 2^26) We use signed + * int for lengths so must be INT_MAX or less. We need something a + * bit smaller than INT_MAX because the array is inside a message with + * header info, etc. so an INT_MAX array wouldn't allow the message + * overhead. The 64M number is an attempt at a larger number than + * we'd reasonably ever use, but small enough that your bus would chew + * through it fairly quickly without locking up forever. If you have + * data that's likely to be larger than this, you should probably be + * sending it in multiple incremental messages anyhow. + */ +#define DBUS_MAXIMUM_ARRAY_LENGTH (67108864) +/** Number of bits you need in an unsigned to store the max array size */ +#define DBUS_MAXIMUM_ARRAY_LENGTH_BITS 26 + +/** The maximum total message size including header and body; similar + * rationale to max array size. + */ +#define DBUS_MAXIMUM_MESSAGE_LENGTH (DBUS_MAXIMUM_ARRAY_LENGTH * 2) +/** Number of bits you need in an unsigned to store the max message size */ +#define DBUS_MAXIMUM_MESSAGE_LENGTH_BITS 27 + +/** The maximum total number of unix fds in a message. Similar + * rationale as DBUS_MAXIMUM_MESSAGE_LENGTH. However we divide by four + * given that one fd is an int and hence at least 32 bits. + */ +#define DBUS_MAXIMUM_MESSAGE_UNIX_FDS (DBUS_MAXIMUM_MESSAGE_LENGTH/4) +/** Number of bits you need in an unsigned to store the max message unix fds */ +#define DBUS_MAXIMUM_MESSAGE_UNIX_FDS_BITS (DBUS_MAXIMUM_MESSAGE_LENGTH_BITS-2) + +/** Depth of recursion in the type tree. This is automatically limited + * to DBUS_MAXIMUM_SIGNATURE_LENGTH since you could only have an array + * of array of array of ... that fit in the max signature. But that's + * probably a bit too large. + */ +#define DBUS_MAXIMUM_TYPE_RECURSION_DEPTH 32 + +/* Types of message */ + +/** This value is never a valid message type, see dbus_message_get_type() */ +#define DBUS_MESSAGE_TYPE_INVALID 0 +/** Message type of a method call message, see dbus_message_get_type() */ +#define DBUS_MESSAGE_TYPE_METHOD_CALL 1 +/** Message type of a method return message, see dbus_message_get_type() */ +#define DBUS_MESSAGE_TYPE_METHOD_RETURN 2 +/** Message type of an error reply message, see dbus_message_get_type() */ +#define DBUS_MESSAGE_TYPE_ERROR 3 +/** Message type of a signal message, see dbus_message_get_type() */ +#define DBUS_MESSAGE_TYPE_SIGNAL 4 + +#define DBUS_NUM_MESSAGE_TYPES 5 + +/* Header flags */ + +/** If set, this flag means that the sender of a message does not care about getting + * a reply, so the recipient need not send one. See dbus_message_set_no_reply(). + */ +#define DBUS_HEADER_FLAG_NO_REPLY_EXPECTED 0x1 +/** + * If set, this flag means that even if the message bus knows how to start an owner for + * the destination bus name (see dbus_message_set_destination()), it should not + * do so. If this flag is not set, the bus may launch a program to process the + * message. + */ +#define DBUS_HEADER_FLAG_NO_AUTO_START 0x2 + +/* Header fields */ + +/** Not equal to any valid header field code */ +#define DBUS_HEADER_FIELD_INVALID 0 +/** Header field code for the path - the path is the object emitting a signal or the object receiving a method call. + * See dbus_message_set_path(). + */ +#define DBUS_HEADER_FIELD_PATH 1 +/** Header field code for the interface containing a member (method or signal). + * See dbus_message_set_interface(). + */ +#define DBUS_HEADER_FIELD_INTERFACE 2 +/** Header field code for a member (method or signal). See dbus_message_set_member(). */ +#define DBUS_HEADER_FIELD_MEMBER 3 +/** Header field code for an error name (found in #DBUS_MESSAGE_TYPE_ERROR messages). + * See dbus_message_set_error_name(). + */ +#define DBUS_HEADER_FIELD_ERROR_NAME 4 +/** Header field code for a reply serial, used to match a #DBUS_MESSAGE_TYPE_METHOD_RETURN message with the + * message that it's a reply to. See dbus_message_set_reply_serial(). + */ +#define DBUS_HEADER_FIELD_REPLY_SERIAL 5 +/** + * Header field code for the destination bus name of a message. See dbus_message_set_destination(). + */ +#define DBUS_HEADER_FIELD_DESTINATION 6 +/** + * Header field code for the sender of a message; usually initialized by the message bus. + * See dbus_message_set_sender(). + */ +#define DBUS_HEADER_FIELD_SENDER 7 +/** + * Header field code for the type signature of a message. + */ +#define DBUS_HEADER_FIELD_SIGNATURE 8 +/** + * Header field code for the number of unix file descriptors associated + * with this message. + */ +#define DBUS_HEADER_FIELD_UNIX_FDS 9 + + +/** + * Value of the highest-numbered header field code, can be used to determine + * the size of an array indexed by header field code. Remember though + * that unknown codes must be ignored, so check for that before + * indexing the array. + */ +#define DBUS_HEADER_FIELD_LAST DBUS_HEADER_FIELD_UNIX_FDS + +/** Header format is defined as a signature: + * byte byte order + * byte message type ID + * byte flags + * byte protocol version + * uint32 body length + * uint32 serial + * array of struct (byte,variant) (field name, value) + * + * The length of the header can be computed as the + * fixed size of the initial data, plus the length of + * the array at the end, plus padding to an 8-boundary. + */ +#define DBUS_HEADER_SIGNATURE \ + DBUS_TYPE_BYTE_AS_STRING \ + DBUS_TYPE_BYTE_AS_STRING \ + DBUS_TYPE_BYTE_AS_STRING \ + DBUS_TYPE_BYTE_AS_STRING \ + DBUS_TYPE_UINT32_AS_STRING \ + DBUS_TYPE_UINT32_AS_STRING \ + DBUS_TYPE_ARRAY_AS_STRING \ + DBUS_STRUCT_BEGIN_CHAR_AS_STRING \ + DBUS_TYPE_BYTE_AS_STRING \ + DBUS_TYPE_VARIANT_AS_STRING \ + DBUS_STRUCT_END_CHAR_AS_STRING + + +/** + * The smallest header size that can occur. (It won't be valid due to + * missing required header fields.) This is 4 bytes, two uint32, an + * array length. This isn't any kind of resource limit, just the + * necessary/logical outcome of the header signature. + */ +#define DBUS_MINIMUM_HEADER_SIZE 16 + +/* Errors */ +/* WARNING these get autoconverted to an enum in dbus-glib.h. Thus, + * if you change the order it breaks the ABI. Keep them in order. + * Also, don't change the formatting since that will break the sed + * script. + */ +/** A generic error; "something went wrong" - see the error message for more. */ +#define DBUS_ERROR_FAILED "org.freedesktop.DBus.Error.Failed" +/** There was not enough memory to complete an operation. */ +#define DBUS_ERROR_NO_MEMORY "org.freedesktop.DBus.Error.NoMemory" +/** The bus doesn't know how to launch a service to supply the bus name you wanted. */ +#define DBUS_ERROR_SERVICE_UNKNOWN "org.freedesktop.DBus.Error.ServiceUnknown" +/** The bus name you referenced doesn't exist (i.e. no application owns it). */ +#define DBUS_ERROR_NAME_HAS_NO_OWNER "org.freedesktop.DBus.Error.NameHasNoOwner" +/** No reply to a message expecting one, usually means a timeout occurred. */ +#define DBUS_ERROR_NO_REPLY "org.freedesktop.DBus.Error.NoReply" +/** Something went wrong reading or writing to a socket, for example. */ +#define DBUS_ERROR_IO_ERROR "org.freedesktop.DBus.Error.IOError" +/** A D-Bus bus address was malformed. */ +#define DBUS_ERROR_BAD_ADDRESS "org.freedesktop.DBus.Error.BadAddress" +/** Requested operation isn't supported (like ENOSYS on UNIX). */ +#define DBUS_ERROR_NOT_SUPPORTED "org.freedesktop.DBus.Error.NotSupported" +/** Some limited resource is exhausted. */ +#define DBUS_ERROR_LIMITS_EXCEEDED "org.freedesktop.DBus.Error.LimitsExceeded" +/** Security restrictions don't allow doing what you're trying to do. */ +#define DBUS_ERROR_ACCESS_DENIED "org.freedesktop.DBus.Error.AccessDenied" +/** Authentication didn't work. */ +#define DBUS_ERROR_AUTH_FAILED "org.freedesktop.DBus.Error.AuthFailed" +/** Unable to connect to server (probably caused by ECONNREFUSED on a socket). */ +#define DBUS_ERROR_NO_SERVER "org.freedesktop.DBus.Error.NoServer" +/** Certain timeout errors, possibly ETIMEDOUT on a socket. + * Note that #DBUS_ERROR_NO_REPLY is used for message reply timeouts. + * @warning this is confusingly-named given that #DBUS_ERROR_TIMED_OUT also exists. We can't fix + * it for compatibility reasons so just be careful. + */ +#define DBUS_ERROR_TIMEOUT "org.freedesktop.DBus.Error.Timeout" +/** No network access (probably ENETUNREACH on a socket). */ +#define DBUS_ERROR_NO_NETWORK "org.freedesktop.DBus.Error.NoNetwork" +/** Can't bind a socket since its address is in use (i.e. EADDRINUSE). */ +#define DBUS_ERROR_ADDRESS_IN_USE "org.freedesktop.DBus.Error.AddressInUse" +/** The connection is disconnected and you're trying to use it. */ +#define DBUS_ERROR_DISCONNECTED "org.freedesktop.DBus.Error.Disconnected" +/** Invalid arguments passed to a method call. */ +#define DBUS_ERROR_INVALID_ARGS "org.freedesktop.DBus.Error.InvalidArgs" +/** Missing file. */ +#define DBUS_ERROR_FILE_NOT_FOUND "org.freedesktop.DBus.Error.FileNotFound" +/** Existing file and the operation you're using does not silently overwrite. */ +#define DBUS_ERROR_FILE_EXISTS "org.freedesktop.DBus.Error.FileExists" +/** Method name you invoked isn't known by the object you invoked it on. */ +#define DBUS_ERROR_UNKNOWN_METHOD "org.freedesktop.DBus.Error.UnknownMethod" +/** Certain timeout errors, e.g. while starting a service. + * @warning this is confusingly-named given that #DBUS_ERROR_TIMEOUT also exists. We can't fix + * it for compatibility reasons so just be careful. + */ +#define DBUS_ERROR_TIMED_OUT "org.freedesktop.DBus.Error.TimedOut" +/** Tried to remove or modify a match rule that didn't exist. */ +#define DBUS_ERROR_MATCH_RULE_NOT_FOUND "org.freedesktop.DBus.Error.MatchRuleNotFound" +/** The match rule isn't syntactically valid. */ +#define DBUS_ERROR_MATCH_RULE_INVALID "org.freedesktop.DBus.Error.MatchRuleInvalid" +/** While starting a new process, the exec() call failed. */ +#define DBUS_ERROR_SPAWN_EXEC_FAILED "org.freedesktop.DBus.Error.Spawn.ExecFailed" +/** While starting a new process, the fork() call failed. */ +#define DBUS_ERROR_SPAWN_FORK_FAILED "org.freedesktop.DBus.Error.Spawn.ForkFailed" +/** While starting a new process, the child exited with a status code. */ +#define DBUS_ERROR_SPAWN_CHILD_EXITED "org.freedesktop.DBus.Error.Spawn.ChildExited" +/** While starting a new process, the child exited on a signal. */ +#define DBUS_ERROR_SPAWN_CHILD_SIGNALED "org.freedesktop.DBus.Error.Spawn.ChildSignaled" +/** While starting a new process, something went wrong. */ +#define DBUS_ERROR_SPAWN_FAILED "org.freedesktop.DBus.Error.Spawn.Failed" +/** We failed to setup the environment correctly. */ +#define DBUS_ERROR_SPAWN_SETUP_FAILED "org.freedesktop.DBus.Error.Spawn.FailedToSetup" +/** We failed to setup the config parser correctly. */ +#define DBUS_ERROR_SPAWN_CONFIG_INVALID "org.freedesktop.DBus.Error.Spawn.ConfigInvalid" +/** Bus name was not valid. */ +#define DBUS_ERROR_SPAWN_SERVICE_INVALID "org.freedesktop.DBus.Error.Spawn.ServiceNotValid" +/** Service file not found in system-services directory. */ +#define DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND "org.freedesktop.DBus.Error.Spawn.ServiceNotFound" +/** Permissions are incorrect on the setuid helper. */ +#define DBUS_ERROR_SPAWN_PERMISSIONS_INVALID "org.freedesktop.DBus.Error.Spawn.PermissionsInvalid" +/** Service file invalid (Name, User or Exec missing). */ +#define DBUS_ERROR_SPAWN_FILE_INVALID "org.freedesktop.DBus.Error.Spawn.FileInvalid" +/** Tried to get a UNIX process ID and it wasn't available. */ +#define DBUS_ERROR_SPAWN_NO_MEMORY "org.freedesktop.DBus.Error.Spawn.NoMemory" +/** Tried to get a UNIX process ID and it wasn't available. */ +#define DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN "org.freedesktop.DBus.Error.UnixProcessIdUnknown" +/** A type signature is not valid. */ +#define DBUS_ERROR_INVALID_SIGNATURE "org.freedesktop.DBus.Error.InvalidSignature" +/** A file contains invalid syntax or is otherwise broken. */ +#define DBUS_ERROR_INVALID_FILE_CONTENT "org.freedesktop.DBus.Error.InvalidFileContent" +/** Asked for SELinux security context and it wasn't available. */ +#define DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown" +/** Asked for ADT audit data and it wasn't available. */ +#define DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN "org.freedesktop.DBus.Error.AdtAuditDataUnknown" +/** There's already an object with the requested object path. */ +#define DBUS_ERROR_OBJECT_PATH_IN_USE "org.freedesktop.DBus.Error.ObjectPathInUse" +/** The message meta data does not match the payload. e.g. expected + number of file descriptors were not sent over the socket this message was received on. */ +#define DBUS_ERROR_INCONSISTENT_MESSAGE "org.freedesktop.DBus.Error.InconsistentMessage" + +/* XML introspection format */ + +/** XML namespace of the introspection format version 1.0 */ +#define DBUS_INTROSPECT_1_0_XML_NAMESPACE "http://www.freedesktop.org/standards/dbus" +/** XML public identifier of the introspection format version 1.0 */ +#define DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" +/** XML system identifier of the introspection format version 1.0 */ +#define DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd" +/** XML document type declaration of the introspection format version 1.0 */ +#define DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "\n" + +/** @} */ + +#ifdef __cplusplus +#if 0 +{ /* avoids confusing emacs indentation */ +#endif +} +#endif + +#endif /* DBUS_PROTOCOL_H */ diff --git a/net/dbus/garbage.c b/net/dbus/garbage.c new file mode 100644 index 0000000..dee0a41 --- /dev/null +++ b/net/dbus/garbage.c @@ -0,0 +1,389 @@ +/* + * NET3: Garbage Collector For AF_DBUS sockets + * + * Garbage Collector: + * Copyright (C) Barak A. Pearlmutter. + * Released under the GPL version 2 or later. + * + * Chopped about by Alan Cox 22/3/96 to make it fit the AF_UNIX socket problem. + * If it doesn't work blame me, it worked when Barak sent it. + * + * Assumptions: + * + * - object w/ a bit + * - free list + * + * Current optimizations: + * + * - explicit stack instead of recursion + * - tail recurse on first born instead of immediate push/pop + * - we gather the stuff that should not be killed into tree + * and stack is just a path from root to the current pointer. + * + * Future optimizations: + * + * - don't just push entire root set; process in place + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Fixes: + * Alan Cox 07 Sept 1997 Vmalloc internal stack as needed. + * Cope with changing max_files. + * Al Viro 11 Oct 1998 + * Graph may have cycles. That is, we can send the descriptor + * of foo to bar and vice versa. Current code chokes on that. + * Fix: move SCM_RIGHTS ones into the separate list and then + * skb_free() them all instead of doing explicit fput's. + * Another problem: since fput() may block somebody may + * create a new unix_socket when we are in the middle of sweep + * phase. Fix: revert the logic wrt MARKED. Mark everything + * upon the beginning and unmark non-junk ones. + * + * [12 Oct 1998] AAARGH! New code purges all SCM_RIGHTS + * sent to connect()'ed but still not accept()'ed sockets. + * Fixed. Old code had slightly different problem here: + * extra fput() in situation when we passed the descriptor via + * such socket and closed it (descriptor). That would happen on + * each unix_gc() until the accept(). Since the struct file in + * question would go to the free list and might be reused... + * That might be the reason of random oopses on filp_close() + * in unrelated processes. + * + * AV 28 Feb 1999 + * Kill the explicit allocation of stack. Now we keep the tree + * with root in dummy + pointer (gc_current) to one of the nodes. + * Stack is represented as path from gc_current to dummy. Unmark + * now means "add to tree". Push == "make it a son of gc_current". + * Pop == "move gc_current to parent". We keep only pointers to + * parents (->gc_tree). + * AV 1 Mar 1999 + * Damn. Added missing check for ->dead in listen queues scanning. + * + * Miklos Szeredi 25 Jun 2007 + * Reimplement with a cycle collecting algorithm. This should + * solve several problems with the previous code, like being racy + * wrt receive and holding up unrelated socket operations. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/* Internal data structures and random procedures: */ + +static LIST_HEAD(gc_inflight_list); +static LIST_HEAD(gc_candidates); +static DEFINE_SPINLOCK(dbus_gc_lock); +static DECLARE_WAIT_QUEUE_HEAD(dbus_gc_wait); + +unsigned int dbus_tot_inflight; + + +static struct sock *dbus_get_socket(struct file *filp) +{ + struct sock *u_sock = NULL; + struct inode *inode = filp->f_path.dentry->d_inode; + + /* + * Socket ? + */ + if (S_ISSOCK(inode->i_mode)) { + struct socket *sock = SOCKET_I(inode); + struct sock *s = sock->sk; + + /* + * PF_DBUS ? + */ + if (s && sock->ops && sock->ops->family == PF_DBUS) + u_sock = s; + } + return u_sock; +} + +/* + * Keep the number of times in flight count for the file + * descriptor if it is for an AF_DBUS socket. + */ + +void dbus_inflight(struct file *fp) +{ + struct sock *s = dbus_get_socket(fp); + if (s) { + struct dbus_sock *u = dbus_sk(s); + spin_lock(&dbus_gc_lock); + if (atomic_long_inc_return(&u->inflight) == 1) { + BUG_ON(!list_empty(&u->link)); + list_add_tail(&u->link, &gc_inflight_list); + } else { + BUG_ON(list_empty(&u->link)); + } + dbus_tot_inflight++; + spin_unlock(&dbus_gc_lock); + } +} + +void dbus_notinflight(struct file *fp) +{ + struct sock *s = dbus_get_socket(fp); + if (s) { + struct dbus_sock *u = dbus_sk(s); + spin_lock(&dbus_gc_lock); + BUG_ON(list_empty(&u->link)); + if (atomic_long_dec_and_test(&u->inflight)) + list_del_init(&u->link); + dbus_tot_inflight--; + spin_unlock(&dbus_gc_lock); + } +} + +static inline struct sk_buff *sock_queue_head(struct sock *sk) +{ + return (struct sk_buff *)&sk->sk_receive_queue; +} + +#define receive_queue_for_each_skb(sk, next, skb) \ + for (skb = sock_queue_head(sk)->next, next = skb->next; \ + skb != sock_queue_head(sk); skb = next, next = skb->next) + +static void scan_inflight(struct sock *x, void (*func)(struct dbus_sock *), + struct sk_buff_head *hitlist) +{ + struct sk_buff *skb; + struct sk_buff *next; + + spin_lock(&x->sk_receive_queue.lock); + receive_queue_for_each_skb(x, next, skb) { + /* + * Do we have file descriptors ? + */ + if (DBUSCB(skb).fp) { + bool hit = false; + /* + * Process the descriptors of this socket + */ + int nfd = DBUSCB(skb).fp->count; + struct file **fp = DBUSCB(skb).fp->fp; + while (nfd--) { + /* + * Get the socket the fd matches + * if it indeed does so + */ + struct sock *sk = dbus_get_socket(*fp++); + if (sk) { + struct dbus_sock *u = dbus_sk(sk); + + /* + * Ignore non-candidates, they could + * have been added to the queues after + * starting the garbage collection + */ + if (u->gc_candidate) { + hit = true; + func(u); + } + } + } + if (hit && hitlist != NULL) { + __skb_unlink(skb, &x->sk_receive_queue); + __skb_queue_tail(hitlist, skb); + } + } + } + spin_unlock(&x->sk_receive_queue.lock); +} + +static void scan_children(struct sock *x, void (*func)(struct dbus_sock *), + struct sk_buff_head *hitlist) +{ + if (x->sk_state != TCP_LISTEN) + scan_inflight(x, func, hitlist); + else { + struct sk_buff *skb; + struct sk_buff *next; + struct dbus_sock *u; + LIST_HEAD(embryos); + + /* + * For a listening socket collect the queued embryos + * and perform a scan on them as well. + */ + spin_lock(&x->sk_receive_queue.lock); + receive_queue_for_each_skb(x, next, skb) { + u = dbus_sk(skb->sk); + + /* + * An embryo cannot be in-flight, so it's safe + * to use the list link. + */ + BUG_ON(!list_empty(&u->link)); + list_add_tail(&u->link, &embryos); + } + spin_unlock(&x->sk_receive_queue.lock); + + while (!list_empty(&embryos)) { + u = list_entry(embryos.next, struct dbus_sock, link); + scan_inflight(&u->sk, func, hitlist); + list_del_init(&u->link); + } + } +} + +static void dec_inflight(struct dbus_sock *usk) +{ + atomic_long_dec(&usk->inflight); +} + +static void inc_inflight(struct dbus_sock *usk) +{ + atomic_long_inc(&usk->inflight); +} + +static void inc_inflight_move_tail(struct dbus_sock *u) +{ + atomic_long_inc(&u->inflight); + /* + * If this still might be part of a cycle, move it to the end + * of the list, so that it's checked even if it was already + * passed over + */ + if (u->gc_maybe_cycle) + list_move_tail(&u->link, &gc_candidates); +} + +static bool gc_in_progress = false; + +void wait_for_dbus_gc(void) +{ + wait_event(dbus_gc_wait, gc_in_progress == false); +} + +/* The external entry point: dbus_gc() */ +void dbus_gc(void) +{ + struct dbus_sock *u; + struct dbus_sock *next; + struct sk_buff_head hitlist; + struct list_head cursor; + LIST_HEAD(not_cycle_list); + + spin_lock(&dbus_gc_lock); + + /* Avoid a recursive GC. */ + if (gc_in_progress) + goto out; + + gc_in_progress = true; + /* + * First, select candidates for garbage collection. Only + * in-flight sockets are considered, and from those only ones + * which don't have any external reference. + * + * Holding dbus_gc_lock will protect these candidates from + * being detached, and hence from gaining an external + * reference. Since there are no possible receivers, all + * buffers currently on the candidates' queues stay there + * during the garbage collection. + * + * We also know that no new candidate can be added onto the + * receive queues. Other, non candidate sockets _can_ be + * added to queue, so we must make sure only to touch + * candidates. + */ + list_for_each_entry_safe(u, next, &gc_inflight_list, link) { + long total_refs; + long inflight_refs; + + total_refs = file_count(u->sk.sk_socket->file); + inflight_refs = atomic_long_read(&u->inflight); + + BUG_ON(inflight_refs < 1); + BUG_ON(total_refs < inflight_refs); + if (total_refs == inflight_refs) { + list_move_tail(&u->link, &gc_candidates); + u->gc_candidate = 1; + u->gc_maybe_cycle = 1; + } + } + + /* + * Now remove all internal in-flight reference to children of + * the candidates. + */ + list_for_each_entry(u, &gc_candidates, link) + scan_children(&u->sk, dec_inflight, NULL); + + /* + * Restore the references for children of all candidates, + * which have remaining references. Do this recursively, so + * only those remain, which form cyclic references. + * + * Use a "cursor" link, to make the list traversal safe, even + * though elements might be moved about. + */ + list_add(&cursor, &gc_candidates); + while (cursor.next != &gc_candidates) { + u = list_entry(cursor.next, struct dbus_sock, link); + + /* Move cursor to after the current position. */ + list_move(&cursor, &u->link); + + if (atomic_long_read(&u->inflight) > 0) { + list_move_tail(&u->link, ¬_cycle_list); + u->gc_maybe_cycle = 0; + scan_children(&u->sk, inc_inflight_move_tail, NULL); + } + } + list_del(&cursor); + + /* + * not_cycle_list contains those sockets which do not make up a + * cycle. Restore these to the inflight list. + */ + while (!list_empty(¬_cycle_list)) { + u = list_entry(not_cycle_list.next, struct dbus_sock, link); + u->gc_candidate = 0; + list_move_tail(&u->link, &gc_inflight_list); + } + + /* + * Now gc_candidates contains only garbage. Restore original + * inflight counters for these as well, and remove the skbuffs + * which are creating the cycle(s). + */ + skb_queue_head_init(&hitlist); + list_for_each_entry(u, &gc_candidates, link) + scan_children(&u->sk, inc_inflight, &hitlist); + + spin_unlock(&dbus_gc_lock); + + /* Here we are. Hitlist is filled. Die. */ + __skb_queue_purge(&hitlist); + + spin_lock(&dbus_gc_lock); + + /* All candidates should have been detached by now. */ + BUG_ON(!list_empty(&gc_candidates)); + gc_in_progress = false; + wake_up(&dbus_gc_wait); + + out: + spin_unlock(&dbus_gc_lock); +} diff --git a/net/dbus/matchrule.c b/net/dbus/matchrule.c new file mode 100644 index 0000000..52ebd6b --- /dev/null +++ b/net/dbus/matchrule.c @@ -0,0 +1,1651 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* matchrule.c D-Bus match rule implementation + * + * Based on signals.c from dbus + * + * Copyright (C) 2010 Collabora, Ltd. + * Copyright (C) 2003, 2005 Red Hat, Inc. + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "matchrule.h" + +#include + +#include "dbus-protocol.h" +#include "message.h" + +struct BusMatchRule +{ + int refcount; /**< reference count */ + + struct dbus_sock *matches_go_to; /**< Owner of the rule */ + + unsigned int flags; /**< BusMatchFlags */ + + int message_type; + char *interface; + char *member; + char *sender; + char *destination; + char *path; + + unsigned int *arg_lens; + char **args; + int args_len; + + /* BusMatchRule is attached to RulePool, either in a simple double-linked + * list if the rule does not have any interface, or in a red-black tree + * sorted by interface. If several rules can have the same interface, the + * first one is attached with struct rb_node and the next ones are in the + * list */ + + struct rb_node node; + /* Doubly-linked non-circular list. If the rule has an interface, it is in + * the rb tree and the single head is right here. Otherwise, the single head + * is in RulePool->rules_without_iface. With this datastructure, we don't + * need any allocation to insert or remove the rule. */ + struct hlist_head first; + struct hlist_node list; +}; + +#define BUS_MATCH_ARG_IS_PATH 0x8000000u + +#define DBUS_STRING_MAX_LENGTH 1024 + +/** Max length of a match rule string; to keep people from hosing the + * daemon with some huge rule + */ +#define DBUS_MAXIMUM_MATCH_RULE_LENGTH 1024 + +BusMatchRule* +bus_match_rule_new (struct dbus_sock *matches_go_to) +{ + BusMatchRule *rule; + + rule = kzalloc (sizeof (BusMatchRule), GFP_KERNEL); + if (rule == NULL) + return NULL; + + rule->refcount = 1; + rule->matches_go_to = matches_go_to; + + WARN_ON (!rule->matches_go_to); + + return rule; +} + +BusMatchRule * +bus_match_rule_ref (BusMatchRule *rule) +{ + WARN_ON (rule->refcount <= 0); + + rule->refcount += 1; + + return rule; +} + +void +bus_match_rule_unref (BusMatchRule *rule) +{ + WARN_ON (rule->refcount <= 0); + + rule->refcount -= 1; + if (rule->refcount == 0) + { + if (rule->interface) + kfree (rule->interface); + if (rule->member) + kfree (rule->member); + if (rule->sender) + kfree (rule->sender); + if (rule->destination) + kfree (rule->destination); + if (rule->path) + kfree (rule->path); + if (rule->arg_lens) + kfree (rule->arg_lens); + + /* can't use dbus_free_string_array() since there + * are embedded NULL + */ + if (rule->args) + { + int i; + + i = 0; + while (i < rule->args_len) + { + if (rule->args[i]) + kfree (rule->args[i]); + ++i; + } + + kfree (rule->args); + } + + kfree (rule); + } +} + +/* Note this function does not do escaping, so it's only + * good for debug spew at the moment + */ +static char* +match_rule_to_string (BusMatchRule *rule) +{ + char *str; + char *cur; + + cur = str = kmalloc (DBUS_STRING_MAX_LENGTH, GFP_KERNEL); + if (!str) + { + return NULL; + } + + if (rule->flags & BUS_MATCH_MESSAGE_TYPE) + { + /* FIXME make type readable */ + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "type='%d'", rule->message_type); + } + + if (rule->flags & BUS_MATCH_INTERFACE) + { + if (cur != str) + { + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + ","); + } + + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "interface='%s'", rule->interface); + } + + if (rule->flags & BUS_MATCH_MEMBER) + { + if (cur != str) + { + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + ","); + } + + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "member='%s'", rule->member); + } + + if (rule->flags & BUS_MATCH_PATH) + { + if (cur != str) + { + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + ","); + } + + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "path='%s'", rule->path); + } + + if (rule->flags & BUS_MATCH_SENDER) + { + if (cur != str) + { + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + ","); + } + + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "sender='%s'", rule->sender); + } + + if (rule->flags & BUS_MATCH_DESTINATION) + { + if (cur != str) + { + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + ","); + } + + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "destination='%s'", rule->destination); + } + + if (rule->flags & BUS_MATCH_ARGS) + { + int i; + + WARN_ON (!rule->args); + + i = 0; + while (i < rule->args_len) + { + if (rule->args[i] != NULL) + { + int is_path; + + if (cur != str) + { + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + ","); + } + + is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0; + + cur += snprintf(cur, str - cur + DBUS_STRING_MAX_LENGTH, + "arg%d%s='%s'", i, is_path ? "path" : "", + rule->args[i]); + } + + ++i; + } + } + + return str; +} + +int +bus_match_rule_set_message_type (BusMatchRule *rule, + int type) +{ + rule->flags |= BUS_MATCH_MESSAGE_TYPE; + + rule->message_type = type; + + return 1; +} + +int +bus_match_rule_set_interface (BusMatchRule *rule, + const char *interface) +{ + char *new; + + WARN_ON (!interface); + + new = kstrdup (interface, GFP_KERNEL); + if (new == NULL) + return 0; + + rule->flags |= BUS_MATCH_INTERFACE; + kfree (rule->interface); + rule->interface = new; + + return 1; +} + +int +bus_match_rule_set_member (BusMatchRule *rule, + const char *member) +{ + char *new; + + WARN_ON(!member); + + new = kstrdup (member, GFP_KERNEL); + if (new == NULL) + return 0; + + rule->flags |= BUS_MATCH_MEMBER; + kfree (rule->member); + rule->member = new; + + return 1; +} + +int +bus_match_rule_set_sender (BusMatchRule *rule, + const char *sender) +{ + char *new; + + WARN_ON (!sender); + + new = kstrdup (sender, GFP_KERNEL); + if (new == NULL) + return 0; + + rule->flags |= BUS_MATCH_SENDER; + kfree (rule->sender); + rule->sender = new; + + return 1; +} + +int +bus_match_rule_set_destination (BusMatchRule *rule, + const char *destination) +{ + char *new; + + WARN_ON (!destination); + + new = kstrdup (destination, GFP_KERNEL); + if (new == NULL) + return 0; + + rule->flags |= BUS_MATCH_DESTINATION; + kfree (rule->destination); + rule->destination = new; + + return 1; +} + +int +bus_match_rule_set_path (BusMatchRule *rule, + const char *path) +{ + char *new; + + WARN_ON (!path); + + new = kstrdup (path, GFP_KERNEL); + if (new == NULL) + return 0; + + rule->flags |= BUS_MATCH_PATH; + kfree (rule->path); + rule->path = new; + + return 1; +} + +int +bus_match_rule_set_arg (BusMatchRule *rule, + int arg, + const char *value, + int is_path) +{ + int length; + char *new; + + /* args_len is the number of args not including null termination + * in the char** + */ + if (arg >= rule->args_len) + { + unsigned int *new_arg_lens; + char **new_args; + int new_args_len; + int i; + + new_args_len = arg + 1; + + /* add another + 1 here for null termination */ + new_args = krealloc (rule->args, + sizeof (char *) * (new_args_len + 1), GFP_KERNEL); + if (new_args == NULL) + return 0; + + /* NULL the new slots */ + i = rule->args_len; + while (i <= new_args_len) /* <= for null termination */ + { + new_args[i] = NULL; + ++i; + } + + rule->args = new_args; + + /* and now add to the lengths */ + new_arg_lens = krealloc (rule->arg_lens, + sizeof (int) * (new_args_len + 1), GFP_KERNEL); + + if (new_arg_lens == NULL) + return 0; + + /* zero the new slots */ + i = rule->args_len; + while (i <= new_args_len) /* <= for null termination */ + { + new_arg_lens[i] = 0; + ++i; + } + + rule->arg_lens = new_arg_lens; + rule->args_len = new_args_len; + } + + length = strlen (value); + if (!(new = kstrdup (value, GFP_KERNEL))) + return 0; + + rule->flags |= BUS_MATCH_ARGS; + + kfree (rule->args[arg]); + rule->arg_lens[arg] = length; + rule->args[arg] = new; + + if (is_path) + rule->arg_lens[arg] |= BUS_MATCH_ARG_IS_PATH; + + return 1; +} + +#define ISWHITE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r')) + +static int +find_key (const char *str, + int start, + char *key, + int *value_pos) +{ + const char *p; + const char *s; + const char *key_start; + const char *key_end; + + s = str; + + p = s + start; + + while (*p && ISWHITE (*p)) + ++p; + + key_start = p; + + while (*p && *p != '=' && !ISWHITE (*p)) + ++p; + + key_end = p; + + while (*p && ISWHITE (*p)) + ++p; + + if (key_start == key_end) + { + /* Empty match rules or trailing whitespace are OK */ + *value_pos = p - s; + return 1; + } + + if (*p != '=') + { + pr_warning("Match rule has a key with no subsequent '=' character"); + return 0; + } + ++p; + + strncat (key, key_start, key_end - key_start); + + *value_pos = p - s; + + return 1; +} + +static int +find_value (const char *str, + int start, + const char *key, + char *value, + int *value_end) +{ + const char *p; + const char *s; + char quote_char; + int orig_len; + + orig_len = strlen (value); + + s = str; + + p = s + start; + + quote_char = '\0'; + + while (*p) + { + if (quote_char == '\0') + { + switch (*p) + { + case '\0': + goto done; + + case '\'': + quote_char = '\''; + goto next; + + case ',': + ++p; + goto done; + + case '\\': + quote_char = '\\'; + goto next; + + default: + strncat(value, p, 1); + } + } + else if (quote_char == '\\') + { + /* \ only counts as an escape if escaping a quote mark */ + if (*p != '\'') + { + strncat(value, "\\", 1); + } + + strncat(value, p, 1); + + quote_char = '\0'; + } + else + { + if (*p == '\'') + { + quote_char = '\0'; + } + else + { + strncat(value, p, 1); + } + } + + next: + ++p; + } + + done: + + if (quote_char == '\\') + { + strncat(value, "\\", 1); + } + else if (quote_char == '\'') + { + pr_warning ("Unbalanced quotation marks in match rule"); + return 0; + } + + /* Zero-length values are allowed */ + + *value_end = p - s; + + return 1; +} + +/* duplicates aren't allowed so the real legitimate max is only 6 or + * so. Leaving extra so we don't have to bother to update it. + * FIXME this is sort of busted now with arg matching, but we let + * you match on up to 10 args for now + */ +#define MAX_RULE_TOKENS 16 + +/* this is slightly too high level to be termed a "token" + * but let's not be pedantic. + */ +typedef struct +{ + char *key; + char *value; +} RuleToken; + +static int +tokenize_rule (const char *rule_text, + RuleToken tokens[MAX_RULE_TOKENS]) +{ + int i; + int pos; + int retval; + + retval = 0; + + i = 0; + pos = 0; + while (i < MAX_RULE_TOKENS && + pos < strlen (rule_text)) + { + char *key; + char *value; + + key = kzalloc (DBUS_STRING_MAX_LENGTH, GFP_KERNEL); + if (!key) + { + printk("Out of memory"); + BUG(); + return 0; + } + + value = kzalloc (DBUS_STRING_MAX_LENGTH, GFP_KERNEL); + if (!value) + { + kfree(key); + printk("Out of memory"); + BUG(); + return 0; + } + + if (!find_key (rule_text, pos, key, &pos)) + goto out; + + if (strlen (key) == 0) + goto next; + + tokens[i].key = key; + + if (!find_value (rule_text, pos, tokens[i].key, value, &pos)) + goto out; + + tokens[i].value = value; + + next: + ++i; + } + + retval = 1; + + out: + if (!retval) + { + i = 0; + while (tokens[i].key || tokens[i].value) + { + kfree (tokens[i].key); + kfree (tokens[i].value); + tokens[i].key = NULL; + tokens[i].value = NULL; + ++i; + } + } + + return retval; +} + +#if 0 +static int +bus_match_rule_parse_arg_match (BusMatchRule *rule, + const char *key, + const char *value) +{ + int is_path; + DBusString key_str; + unsigned long arg; + int length; + int end; + + /* For now, arg0='foo' always implies that 'foo' is a + * DBUS_TYPE_STRING. Someday we could add an arg0type='int32' thing + * if we wanted, which would specify another type, in which case + * arg0='5' would have the 5 parsed as an int rather than string. + */ + + /* First we need to parse arg0 = 0, arg27 = 27 */ + + _dbus_string_init_const (&key_str, key); + length = _dbus_string_get_length (&key_str); + + if (_dbus_string_get_length (&key_str) < 4) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Key '%s' in match rule starts with 'arg' but lacks an arg number. Should be 'arg0' or 'arg7' for example.\n", key); + goto failed; + } + + if (!_dbus_string_parse_uint (&key_str, 3, &arg, &end)) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Key '%s' in match rule starts with 'arg' but could not parse arg number. Should be 'arg0' or 'arg7' for example.\n", key); + goto failed; + } + + if (end != length && + ((end + 4) != length || + !_dbus_string_ends_with_c_str (&key_str, "path"))) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Key '%s' in match rule contains junk after argument number. Only 'path' is optionally valid ('arg0path' for example).\n", key); + goto failed; + } + + is_path = end != length; + + /* If we didn't check this we could allocate a huge amount of RAM */ + if (arg > DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER) + { + printk("Key '%s' in match rule has arg number %lu but the maximum is %d.\n", key, (unsigned long) arg, DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER); + dump_stack(); + return 0; + } + + if ((rule->flags & BUS_MATCH_ARGS) && + rule->args_len > (int) arg && + rule->args[arg] != NULL) + { + printk("Argument %d matched more than once in match rule\n", key); + dump_stack(); + return 0; + } + + if (!bus_match_rule_set_arg (rule, arg, value, is_path)) + { + printk("Out of memory"); + dump_stack(); + return 0; + } + + return 1; +} +#endif + +/* + * The format is comma-separated with strings quoted with single quotes + * as for the shell (to escape a literal single quote, use '\''). + * + * type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo', + * path='/bar/foo',destination=':452345.34' + * + */ +BusMatchRule* +bus_match_rule_parse (struct dbus_sock *matches_go_to, + const char *rule_text) +{ + BusMatchRule *rule; + RuleToken tokens[MAX_RULE_TOKENS+1]; /* NULL termination + 1 */ + int i; + + if (strlen (rule_text) > DBUS_MAXIMUM_MATCH_RULE_LENGTH) + { + pr_warning ("Match rule text is %d bytes, maximum is %d", + strlen (rule_text), + DBUS_MAXIMUM_MATCH_RULE_LENGTH); + return NULL; + } + + memset (tokens, '\0', sizeof (tokens)); + + rule = bus_match_rule_new (matches_go_to); + if (rule == NULL) + { + printk("Out of memory"); + BUG(); + goto failed; + } + + if (!tokenize_rule (rule_text, tokens)) + goto failed; + + i = 0; + while (tokens[i].key != NULL) + { +#if 0 + char *tmp_str; + int len; +#endif + const char *key = tokens[i].key; + const char *value = tokens[i].value; + +#if 0 + tmp_str = value; + len = strlen (tmp_str); +#endif + + if (strcmp (key, "type") == 0) + { + int t; + + if (rule->flags & BUS_MATCH_MESSAGE_TYPE) + { + pr_warning("Key %s specified twice in match rule\n", key); + goto failed; + } + + t = dbus_message_type_from_string (value); + + if (t == DBUS_MESSAGE_TYPE_INVALID) + { + pr_warning("Invalid message type (%s) in match rule\n", value); + goto failed; + } + + if (!bus_match_rule_set_message_type (rule, t)) + { + printk("Out of memeory"); + BUG(); + goto failed; + } + } + else if (strcmp (key, "sender") == 0) + { + if (rule->flags & BUS_MATCH_SENDER) + { + pr_warning("Key %s specified twice in match rule\n", key); + goto failed; + } + +#if 0 + if (!_dbus_validate_bus_name (tmp_str, 0, len)) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Sender name '%s' is invalid\n", value); + goto failed; + } +#endif + + if (!bus_match_rule_set_sender (rule, value)) + { + printk("Out of memeory"); + BUG(); + goto failed; + } + } + else if (strcmp (key, "interface") == 0) + { + if (rule->flags & BUS_MATCH_INTERFACE) + { + pr_warning("Key %s specified twice in match rule\n", key); + goto failed; + } + +#if 0 + if (!_dbus_validate_interface (tmp_str, 0, len)) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Interface name '%s' is invalid\n", value); + goto failed; + } +#endif + + if (!bus_match_rule_set_interface (rule, value)) + { + printk("Out of memeory"); + BUG(); + goto failed; + } + } + else if (strcmp (key, "member") == 0) + { + if (rule->flags & BUS_MATCH_MEMBER) + { + pr_warning("Key %s specified twice in match rule\n", key); + goto failed; + } + +#if 0 + if (!_dbus_validate_member (tmp_str, 0, len)) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Member name '%s' is invalid\n", value); + goto failed; + } +#endif + + if (!bus_match_rule_set_member (rule, value)) + { + printk("Out of memeory"); + BUG(); + goto failed; + } + } + else if (strcmp (key, "path") == 0) + { + if (rule->flags & BUS_MATCH_PATH) + { + pr_warning("Key %s specified twice in match rule\n", key); + goto failed; + } + +#if 0 + if (!_dbus_validate_path (tmp_str, 0, len)) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Path '%s' is invalid\n", value); + goto failed; + } +#endif + + if (!bus_match_rule_set_path (rule, value)) + { + printk("Out of memeory"); + BUG(); + goto failed; + } + } + else if (strcmp (key, "destination") == 0) + { + if (rule->flags & BUS_MATCH_DESTINATION) + { + pr_warning("Key %s specified twice in match rule\n", key); + goto failed; + } + +#if 0 + if (!_dbus_validate_bus_name (tmp_str, 0, len)) + { + dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID, + "Destination name '%s' is invalid\n", value); + goto failed; + } +#endif + + if (!bus_match_rule_set_destination (rule, value)) + { + printk("Out of memeory"); + BUG(); + goto failed; + } + } + else if (strncmp (key, "arg", 3) == 0) + { +#if 0 + if (!bus_match_rule_parse_arg_match (rule, key, &tmp_str, error)) + goto failed; +#endif + } + else + { + pr_warning("Unknown key \"%s\" in match rule\n", + key); + goto failed; + } + + ++i; + } + + + goto out; + + failed: + if (rule) + { + bus_match_rule_unref (rule); + rule = NULL; + } + + out: + + i = 0; + while (tokens[i].key || tokens[i].value) + { + WARN_ON (i >= MAX_RULE_TOKENS); + kfree (tokens[i].key); + kfree (tokens[i].value); + ++i; + } + + return rule; +} + +typedef struct RulePool RulePool; +struct RulePool +{ + /* Maps non-NULL interface names to a list of BusMatchRule */ + struct rb_root rules_by_iface; + + /* List of BusMatchRules which don't specify an interface */ + struct hlist_head rules_without_iface; +}; + +struct BusMatchmaker +{ + int refcount; + + /* Pools of rules, grouped by the type of message they match. 0 + * (DBUS_MESSAGE_TYPE_INVALID) represents rules that do not specify a message + * type. + */ + RulePool rules_by_type[DBUS_NUM_MESSAGE_TYPES]; +}; + +/* return the match rule containing the hlist_head. It may not be the first + * match rule in the list. */ +struct BusMatchRule *match_rule_search(struct rb_root *root, const char *interface) +{ + struct rb_node *node = root->rb_node; + + while (node) { + struct BusMatchRule *data = container_of(node, + struct BusMatchRule, node); + int result; + + result = strcmp(interface, data->interface); + + if (result < 0) + node = node->rb_left; + else if (result > 0) + node = node->rb_right; + else + return data; + } + return NULL; +} + +void match_rule_insert(struct rb_root *root, struct BusMatchRule *data) +{ + struct rb_node **new = &(root->rb_node), *parent = NULL; + + /* Figure out where to put new node */ + while (*new) { + struct BusMatchRule *this = container_of(*new, + struct BusMatchRule, node); + int result = strcmp(data->interface, this->interface); + + parent = *new; + if (result < 0) + new = &((*new)->rb_left); + else if (result > 0) + new = &((*new)->rb_right); + else { + /* the head is not used */ + INIT_HLIST_HEAD(&data->first); + /* Add it at the beginning of the list */ + hlist_add_head(&data->list, &this->first); + return; + } + } + + /* this rule is single in its list */ + INIT_HLIST_HEAD(&data->first); + hlist_add_head(&data->list, &data->first); + + /* Add new node and rebalance tree. */ + rb_link_node(&data->node, parent, new); + rb_insert_color(&data->node, root); +} + +BusMatchmaker* +bus_matchmaker_new (void) +{ + BusMatchmaker *matchmaker; + int i; + + matchmaker = kzalloc (sizeof (BusMatchmaker), GFP_KERNEL); + if (matchmaker == NULL) + return NULL; + + matchmaker->refcount = 1; + + for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++) + { + RulePool *p = matchmaker->rules_by_type + i; + + p->rules_by_iface = RB_ROOT; + } + + return matchmaker; +} + +BusMatchmaker * +bus_matchmaker_ref (BusMatchmaker *matchmaker) +{ + WARN_ON (matchmaker->refcount <= 0); + + matchmaker->refcount += 1; + + return matchmaker; +} + +void +bus_matchmaker_unref (BusMatchmaker *matchmaker) +{ + WARN_ON (matchmaker->refcount <= 0); + + matchmaker->refcount -= 1; + if (matchmaker->refcount == 0) + { + // FIXME: LEAK!! + kfree (matchmaker); + } +} + +/* The rule can't be modified after it's added. */ +int +bus_matchmaker_add_rule (BusMatchmaker *matchmaker, + BusMatchRule *rule) +{ + RulePool *pool; + + WARN_ON (rule->message_type < 0); + WARN_ON (rule->message_type >= DBUS_NUM_MESSAGE_TYPES); + + pool = matchmaker->rules_by_type + rule->message_type; + + if (rule->interface) { + match_rule_insert (&pool->rules_by_iface, rule); + } else { + hlist_add_head(&rule->list, &pool->rules_without_iface); + } + + return 1; +} + +static int +match_rule_equal (BusMatchRule *a, + BusMatchRule *b) +{ + if (a->flags != b->flags) + return 0; + + if (a->matches_go_to != b->matches_go_to) + return 0; + + if ((a->flags & BUS_MATCH_MESSAGE_TYPE) && + a->message_type != b->message_type) + return 0; + + if ((a->flags & BUS_MATCH_MEMBER) && + strcmp (a->member, b->member) != 0) + return 0; + + if ((a->flags & BUS_MATCH_PATH) && + strcmp (a->path, b->path) != 0) + return 0; + + if ((a->flags & BUS_MATCH_INTERFACE) && + strcmp (a->interface, b->interface) != 0) + return 0; + + if ((a->flags & BUS_MATCH_SENDER) && + strcmp (a->sender, b->sender) != 0) + return 0; + + if ((a->flags & BUS_MATCH_DESTINATION) && + strcmp (a->destination, b->destination) != 0) + return 0; + + if (a->flags & BUS_MATCH_ARGS) + { + int i; + + if (a->args_len != b->args_len) + return 0; + + i = 0; + while (i < a->args_len) + { + int length; + + if ((a->args[i] != NULL) != (b->args[i] != NULL)) + return 0; + + if (a->arg_lens[i] != b->arg_lens[i]) + return 0; + + length = a->arg_lens[i] & ~BUS_MATCH_ARG_IS_PATH; + + if (a->args[i] != NULL) + { + WARN_ON (!b->args[i]); + if (memcmp (a->args[i], b->args[i], length) != 0) + return 0; + } + + ++i; + } + } + + return 1; +} + +void +bus_matchmaker_remove_rule (BusMatchmaker *matchmaker, + BusMatchRule *rule) +{ + RulePool *pool; + + WARN_ON (rule->message_type < 0); + WARN_ON (rule->message_type >= DBUS_NUM_MESSAGE_TYPES); + + pool = matchmaker->rules_by_type + rule->message_type; + + if (rule->interface) { + BusMatchRule *head = match_rule_search(&pool->rules_by_iface, + rule->interface); + hlist_del(&rule->list); + if (!hlist_empty(&head->first) && head == rule) { + BusMatchRule *next = hlist_entry(head->first.first, + BusMatchRule, list); + hlist_move_list(&head->first, &next->first); + } + //XXX what _if we need to remove the rb_node entry: rb_erase + } else { + hlist_del(&rule->list); + } + //bus_match_rule_unref (rule); // cf XXX +} + +/* Remove a single rule which is equal to the given rule by value */ +void +bus_matchmaker_remove_rule_by_value (BusMatchmaker *matchmaker, + BusMatchRule *rule) +{ + RulePool *pool; + + WARN_ON (rule->message_type < 0); + WARN_ON (rule->message_type >= DBUS_NUM_MESSAGE_TYPES); + + pool = matchmaker->rules_by_type + rule->message_type; + + if (rule->interface) { + BusMatchRule *head = match_rule_search(&pool->rules_by_iface, + rule->interface); + hlist_del(&rule->list); + if (!hlist_empty(&head->first) && match_rule_equal(head, rule)) { + BusMatchRule *next = hlist_entry(head->first.first, + BusMatchRule, list); + hlist_move_list(&head->first, &next->first); + } + } else { + hlist_del(&rule->list); + } + //bus_match_rule_unref (rule); // cf XXX +} + +static void +rule_list_remove_by_connection (BusMatchmaker *matchmaker, + struct hlist_head *rules, + struct dbus_sock *connection) +{ + struct hlist_node *cur, *next; + BusMatchRule *rule; + + if (rules == NULL) + return; + + hlist_for_each_entry_safe(rule, cur, next, rules, list) + { + char *s = match_rule_to_string (rule); + + if (rule->matches_go_to == connection) { + pr_debug("Delete rule \"%s\" because it is the owner of the rule\n", s); + kfree(s); + bus_matchmaker_remove_rule(matchmaker, rule); + } else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') || + ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':')) { + /* The rule matches to/from a base service, see if it's the + * one being disconnected, since we know this service name + * will never be recycled. + */ + const char *name; + + name = connection->uniq_name; + if (!name) + { + kfree(s); + continue; + } + + if (((rule->flags & BUS_MATCH_SENDER) && + strcmp (rule->sender, name) == 0) || + ((rule->flags & BUS_MATCH_DESTINATION) && + strcmp (rule->destination, name) == 0)) + { + pr_debug("Delete rule \"%s\" because of sender or destination\n", s); + bus_matchmaker_remove_rule(matchmaker, rule); + } + kfree(s); + } + + } +} + +void +bus_matchmaker_disconnected (BusMatchmaker *matchmaker, + struct dbus_sock *connection) +{ + int i; + + /* FIXME + * + * This scans all match rules on the bus. We could avoid that + * for the rules belonging to the connection, since we keep + * a list of those; but for the rules that just refer to + * the connection we'd need to do something more elaborate. + */ + + pr_debug ("Removing all rules for connection '%s'\n", + connection->uniq_name ? connection->uniq_name : "(null)"); + for (i = 0; i < MAX_WELL_KNOWN_NAMES; i++) + if (connection->well_known_names[i]) + pr_debug(" - including well known name '%s'\n", + connection->well_known_names[i]); + + for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++) + { + RulePool *p = matchmaker->rules_by_type + i; + struct rb_node *node; + + rule_list_remove_by_connection (matchmaker, + &p->rules_without_iface, connection); + + for (node = rb_first(&p->rules_by_iface); node; node = rb_next(node)) { + pr_debug("interface=%s\n", + rb_entry(node, struct BusMatchRule, node)->interface); + rule_list_remove_by_connection (matchmaker, + &rb_entry(node, struct BusMatchRule, node)->first, connection); + } + } + + pr_debug("bus_matchmaker_disconnected: Finished :-)\n"); +} + +static int +connection_is_primary_owner (struct dbus_sock *connection, + const char *service_name) +{ +#if 0 + BusService *service; + DBusString str; + BusRegistry *registry; + + _dbus_assert (connection != NULL); + + registry = bus_connection_get_registry (connection); + + _dbus_string_init_const (&str, service_name); + service = bus_registry_lookup (registry, &str); + + if (service == NULL) + return 0; /* Service doesn't exist so connection can't own it. */ + + return bus_service_get_primary_owners_connection (service) == connection; +#endif + return 1; +} + +static int +match_rule_matches (BusMatchRule *rule, + struct dbus_sock *sender, + struct dbus_sock *addressed_recipient, + const struct DBusMessage *message, + BusMatchFlags already_matched) +{ + int flags; + + /* All features of the match rule are AND'd together, + * so 0 if any of them don't match. + */ + + /* sender/addressed_recipient of #NULL may mean bus driver, + * or for addressed_recipient may mean a message with no + * specific recipient (i.e. a signal) + */ + + /* Don't bother re-matching features we've already checked implicitly. */ + flags = rule->flags & (~already_matched); + + if (flags & BUS_MATCH_MESSAGE_TYPE) + { + WARN_ON (rule->message_type == DBUS_MESSAGE_TYPE_INVALID); + + if (rule->message_type != message->type) + return 0; + } + + if (flags & BUS_MATCH_INTERFACE) + { + const char *iface; + + WARN_ON (!rule->interface); + + iface = message->interface; + if (iface == NULL) + return 0; + + if (strcmp (iface, rule->interface) != 0) + return 0; + } + + if (flags & BUS_MATCH_MEMBER) + { + const char *member; + + WARN_ON (!rule->member); + + member = message->member; + if (member == NULL) + return 0; + + if (strcmp (member, rule->member) != 0) + return 0; + } + + if (flags & BUS_MATCH_SENDER) + { + WARN_ON (!rule->sender); + + if (sender == NULL) + { + if (strcmp (rule->sender, + "org.freedesktop.DBus") != 0) + return 0; + } + else + { + if (!connection_is_primary_owner (sender, rule->sender)) + return 0; + } + } + + if (flags & BUS_MATCH_DESTINATION) + { + const char *destination; + + WARN_ON (!rule->destination); + + destination = message->destination; + if (destination == NULL) + return 0; + + if (addressed_recipient == NULL) + { + if (strcmp (rule->destination, + "org.freedesktop.DBus") != 0) + return 0; + } + else + { + if (!connection_is_primary_owner (addressed_recipient, rule->destination)) + return 0; + } + } + + if (flags & BUS_MATCH_PATH) + { + const char *path; + + WARN_ON (!rule->path); + + path = message->path; + if (path == NULL) + return 0; + + if (strcmp (path, rule->path) != 0) + return 0; + } + +#if 0 + if (flags & BUS_MATCH_ARGS) + { + int i; + DBusMessageIter iter; + + _dbus_assert (rule->args != NULL); + + dbus_message_iter_init (message, &iter); + + i = 0; + while (i < rule->args_len) + { + int current_type; + const char *expected_arg; + int expected_length; + int is_path; + + expected_arg = rule->args[i]; + expected_length = rule->arg_lens[i] & ~BUS_MATCH_ARG_IS_PATH; + is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0; + + current_type = dbus_message_iter_get_arg_type (&iter); + + if (expected_arg != NULL) + { + const char *actual_arg; + int actual_length; + + if (current_type != DBUS_TYPE_STRING) + return 0; + + actual_arg = NULL; + dbus_message_iter_get_basic (&iter, &actual_arg); + _dbus_assert (actual_arg != NULL); + + actual_length = strlen (actual_arg); + + if (is_path) + { + if (actual_length < expected_length && + actual_arg[actual_length - 1] != '/') + return 0; + + if (expected_length < actual_length && + expected_arg[expected_length - 1] != '/') + return 0; + + if (memcmp (actual_arg, expected_arg, + MIN (actual_length, expected_length)) != 0) + return 0; + } + else + { + if (expected_length != actual_length || + memcmp (expected_arg, actual_arg, expected_length) != 0) + return 0; + } + + } + + if (current_type != DBUS_TYPE_INVALID) + dbus_message_iter_next (&iter); + + ++i; + } + } +#endif + + return 1; +} + +static void +get_recipients_from_list (struct hlist_head *rules, + struct dbus_sock *sender, + struct dbus_sock *addressed_recipient, + const struct DBusMessage *message, + deliver_func_t deliver_func, + void *user_data) +{ + struct hlist_node *cur; + BusMatchRule *rule; + + if (rules == NULL) + return; + + hlist_for_each_entry(rule, cur, rules, list) + { + char *s = match_rule_to_string (rule); + + + if (match_rule_matches (rule, + sender, addressed_recipient, message, + BUS_MATCH_MESSAGE_TYPE | BUS_MATCH_INTERFACE)) + { + pr_debug (" [YES] deliver to '%s' with match rule \"%s\"\n", + rule->matches_go_to->uniq_name ? + rule->matches_go_to->uniq_name : "(null)", s); + if (deliver_func) + deliver_func ((struct sock *)(rule->matches_go_to), user_data); + } + else + { + pr_debug (" [NO] deliver to '%s' with match rule \"%s\"\n", + rule->matches_go_to->uniq_name ? + rule->matches_go_to->uniq_name : "(null)", s); + } + kfree (s); + } +} + +static struct hlist_head * +bus_matchmaker_get_rules (BusMatchmaker *matchmaker, + int message_type, + const char *interface) +{ + static struct hlist_head empty = {0,}; + RulePool *p; + + WARN_ON (message_type < 0); + WARN_ON (message_type >= DBUS_NUM_MESSAGE_TYPES); + + p = matchmaker->rules_by_type + message_type; + + if (interface == NULL) + { + return &p->rules_without_iface; + } + else + { + BusMatchRule *rule = match_rule_search (&p->rules_by_iface, + interface); + if (rule) + return &rule->first; + else + return ∅ + } +} + +void +bus_matchmaker_get_recipients (BusMatchmaker *matchmaker, + struct dbus_sock *sender, + struct dbus_sock *addressed_recipient, + const struct DBusMessage *message, + deliver_func_t deliver_func, + void *user_data) +{ + int type; + const char *interface; + struct hlist_head *neither, *just_type, *just_iface, *both; + + type = message->type; + interface = message->interface; + + neither = bus_matchmaker_get_rules (matchmaker, DBUS_MESSAGE_TYPE_INVALID, + NULL); + just_type = just_iface = both = NULL; + + if (interface != NULL) + just_iface = bus_matchmaker_get_rules (matchmaker, + DBUS_MESSAGE_TYPE_INVALID, interface); + + if (type > DBUS_MESSAGE_TYPE_INVALID && type < DBUS_NUM_MESSAGE_TYPES) + { + just_type = bus_matchmaker_get_rules (matchmaker, type, NULL); + + if (interface != NULL) + both = bus_matchmaker_get_rules (matchmaker, type, interface); + } + + get_recipients_from_list (neither, sender, addressed_recipient, + message, deliver_func, user_data); + get_recipients_from_list (just_iface, sender, addressed_recipient, + message, deliver_func, user_data); + get_recipients_from_list (just_type, sender, addressed_recipient, + message, deliver_func, user_data); + get_recipients_from_list (both, sender, addressed_recipient, + message, deliver_func, user_data); +} + diff --git a/net/dbus/matchrule.h b/net/dbus/matchrule.h new file mode 100644 index 0000000..d8de5d6 --- /dev/null +++ b/net/dbus/matchrule.h @@ -0,0 +1,92 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* signals.h Bus signal connection implementation + * + * Copyright (C) 2003 Red Hat, Inc. + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef BUS_SIGNALS_H +#define BUS_SIGNALS_H + +#include +#include + +#include "message.h" + +typedef struct BusMatchRule BusMatchRule; +typedef struct BusMatchmaker BusMatchmaker; + +typedef enum +{ + BUS_MATCH_MESSAGE_TYPE = 1 << 0, + BUS_MATCH_INTERFACE = 1 << 1, + BUS_MATCH_MEMBER = 1 << 2, + BUS_MATCH_SENDER = 1 << 3, + BUS_MATCH_DESTINATION = 1 << 4, + BUS_MATCH_PATH = 1 << 5, + BUS_MATCH_ARGS = 1 << 6 +} BusMatchFlags; + +BusMatchRule* bus_match_rule_new (struct dbus_sock *matches_go_to); +BusMatchRule* bus_match_rule_ref (BusMatchRule *rule); +void bus_match_rule_unref (BusMatchRule *rule); + +int bus_match_rule_set_message_type (BusMatchRule *rule, + int type); +int bus_match_rule_set_interface (BusMatchRule *rule, + const char *interface); +int bus_match_rule_set_member (BusMatchRule *rule, + const char *member); +int bus_match_rule_set_sender (BusMatchRule *rule, + const char *sender); +int bus_match_rule_set_destination (BusMatchRule *rule, + const char *destination); +int bus_match_rule_set_path (BusMatchRule *rule, + const char *path); +int bus_match_rule_set_arg (BusMatchRule *rule, + int arg, + const char *value, + int is_path); + +BusMatchRule* bus_match_rule_parse (struct dbus_sock *matches_go_to, + const char *rule_text); + +BusMatchmaker* bus_matchmaker_new (void); +BusMatchmaker* bus_matchmaker_ref (BusMatchmaker *matchmaker); +void bus_matchmaker_unref (BusMatchmaker *matchmaker); + +int bus_matchmaker_add_rule (BusMatchmaker *matchmaker, + BusMatchRule *rule); +void bus_matchmaker_remove_rule_by_value (BusMatchmaker *matchmaker, + BusMatchRule *value); +void bus_matchmaker_remove_rule (BusMatchmaker *matchmaker, + BusMatchRule *rule); +void bus_matchmaker_disconnected (BusMatchmaker *matchmaker, + struct dbus_sock *connection); + +typedef void (*deliver_func_t) (struct sock *dest, void *user_data); + +void bus_matchmaker_get_recipients (BusMatchmaker *matchmaker, + struct dbus_sock *sender, + struct dbus_sock *addressed_recipient, + const struct DBusMessage *message, + deliver_func_t deliver_func, + void *user_data); + +#endif /* BUS_SIGNALS_H */ diff --git a/net/dbus/message.c b/net/dbus/message.c new file mode 100644 index 0000000..17233d3 --- /dev/null +++ b/net/dbus/message.c @@ -0,0 +1,274 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* message.c Basic D-Bus message parsing + * + * Copyright (C) 2010 Collabora Ltd + * Authors: Alban Crequy + * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc. + * Copyright (C) 2002, 2003 CodeFactory AB + * + * Licensed under the Academic Free License version 2.1 + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "message.h" +#include "dbus-protocol.h" + +/** + * Utility function to convert a machine-readable (not translated) + * string into a D-Bus message type. + * + * @code + * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL + * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN + * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL + * "error" -> DBUS_MESSAGE_TYPE_ERROR + * anything else -> DBUS_MESSAGE_TYPE_INVALID + * @endcode + * + */ +int +dbus_message_type_from_string (const char *type_str) +{ + if (strcmp (type_str, "method_call") == 0) + return DBUS_MESSAGE_TYPE_METHOD_CALL; + if (strcmp (type_str, "method_return") == 0) + return DBUS_MESSAGE_TYPE_METHOD_RETURN; + else if (strcmp (type_str, "signal") == 0) + return DBUS_MESSAGE_TYPE_SIGNAL; + else if (strcmp (type_str, "error") == 0) + return DBUS_MESSAGE_TYPE_ERROR; + else + return DBUS_MESSAGE_TYPE_INVALID; +} + +/** + * Utility function to convert a D-Bus message type into a + * machine-readable string (not translated). + * + * @code + * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call" + * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return" + * DBUS_MESSAGE_TYPE_SIGNAL -> "signal" + * DBUS_MESSAGE_TYPE_ERROR -> "error" + * DBUS_MESSAGE_TYPE_INVALID -> "invalid" + * @endcode + * + */ +const char * +dbus_message_type_to_string (int type) +{ + switch (type) + { + case DBUS_MESSAGE_TYPE_METHOD_CALL: + return "method_call"; + case DBUS_MESSAGE_TYPE_METHOD_RETURN: + return "method_return"; + case DBUS_MESSAGE_TYPE_SIGNAL: + return "signal"; + case DBUS_MESSAGE_TYPE_ERROR: + return "error"; + default: + return "invalid"; + } +} + +int dbus_message_parse(unsigned char *message, size_t len, + struct DBusMessage *dbus_message) +{ + unsigned char *cur; + int array_header_len; + + dbus_message->message = message; + + if (len < 4 + 4 + 4 + 4 || message[1] == 0 || message[1] > 4) { + return -EINVAL; + } + dbus_message->type = message[1]; + dbus_message->body_length = *((u32 *)(message + 4)); + cur = message + 12; + array_header_len = *(u32 *)cur; + dbus_message->len_offset = 12; + cur += 4; + while (cur < message + len + && cur < message + 12 + 4 + array_header_len) { + int header_code; + int signature_len; + unsigned char *signature; + int str_len; + unsigned char *str; + + /* D-Bus alignment craziness */ + if ((cur - message) % 8 != 0) + cur += 8 - (cur - message) % 8; + + header_code = *(char *)cur; + cur++; + signature_len = *(char *)cur; + /* All header fields of the current D-Bus spec have a simple + * type, either o, s, g, or u */ + if (signature_len != 1) + return -EINVAL; + cur++; + signature = cur; + cur += signature_len + 1; + if (signature[0] != 'o' && + signature[0] != 's' && + signature[0] != 'g' && + signature[0] != 'u') + return -EINVAL; + + if (signature[0] == 'u') { + cur += 4; + continue; + } + + if (signature[0] != 'g') { + str_len = *(u32 *)cur; + cur += 4; + } else { + str_len = *(char *)cur; + cur += 1; + } + + str = cur; + switch (header_code) { + case 1: dbus_message->path = str; + break; + case 2: dbus_message->interface = str; + break; + case 3: dbus_message->member = str; + break; + case 6: dbus_message->destination = str; + break; + case 7: dbus_message->sender = str; + break; + case 8: dbus_message->body_signature = str; + break; + } + cur += str_len + 1; + } + + dbus_message->padding_end = (8 - (cur - message) % 8) % 8; + + /* Jump to body D-Bus alignment craziness */ + if ((cur - message) % 8 != 0) + cur += 8 - (cur - message) % 8; + dbus_message->new_header_offset = cur - message; + + if (dbus_message->new_header_offset + + dbus_message->body_length != len) { + pr_warning("Message truncated? " + "Header %d + Body %d != Length %zd\n", + dbus_message->new_header_offset, + dbus_message->body_length, len); + return -EINVAL; + } + + if (dbus_message->body_signature && + dbus_message->body_signature[0] == 's') { + int str_len; + str_len = *(u32 *)cur; + cur += 4; + dbus_message->arg0 = cur; + cur += str_len + 1; + } + + if ((cur - message) % 4 != 0) + cur += 4 - (cur - message) % 4; + + if (dbus_message->body_signature && + dbus_message->body_signature[0] == 's' && + dbus_message->body_signature[1] == 's') { + int str_len; + str_len = *(u32 *)cur; + cur += 4; + dbus_message->arg1 = cur; + cur += str_len + 1; + } + + if ((cur - message) % 4 != 0) + cur += 4 - (cur - message) % 4; + + if (dbus_message->body_signature && + dbus_message->body_signature[0] == 's' && + dbus_message->body_signature[1] == 's' && + dbus_message->body_signature[2] == 's') { + int str_len; + str_len = *(u32 *)cur; + cur += 4; + dbus_message->arg2 = cur; + cur += str_len + 1; + } + + if ((cur - message) % 4 != 0) + cur += 4 - (cur - message) % 4; + + return 0; +} + + +int +dbus_message_add_sender(struct DBusMessage *dbus_message, + const char *sender, gfp_t gfp_flags) +{ + unsigned char *message = dbus_message->message; + unsigned char *new_message; + int added_size_not_aligned = 1 + 3 + 4 + + strlen(sender) + 1; + int added_size = added_size_not_aligned + + (8 - added_size_not_aligned % 8) % 8; + + dbus_message->new_len = dbus_message->len + added_size; + new_message = kmalloc(dbus_message->new_len, gfp_flags); + if (!new_message) + return -ENOMEM; + + memcpy(new_message, message, dbus_message->new_header_offset); + *(char*)(new_message + dbus_message->new_header_offset) + = 7; + *(char*)(new_message + dbus_message->new_header_offset + 1) + = 1; + *(char*)(new_message + dbus_message->new_header_offset + 2) + = 's'; + *(char*)(new_message + dbus_message->new_header_offset + 3) + = '\0'; + *(u32*)(new_message + dbus_message->new_header_offset + 4) + = strlen(sender); + + BUG_ON(dbus_message->sender != NULL); + dbus_message->sender = new_message + dbus_message->new_header_offset + + 1 + 3 + 4; + strcpy(dbus_message->sender, sender); + memset(new_message + dbus_message->new_header_offset + + added_size_not_aligned, 0, + added_size - added_size_not_aligned); + memcpy(new_message + dbus_message->new_header_offset + + added_size, + message + dbus_message->new_header_offset, + dbus_message->len - dbus_message->new_header_offset); + + *(u32*)(new_message + dbus_message->len_offset) = + *(u32*)(new_message + dbus_message->len_offset) + + dbus_message->padding_end + added_size_not_aligned; + + kfree(message); + dbus_message_parse(new_message, dbus_message->new_len, dbus_message); + + return 0; +} + diff --git a/net/dbus/message.h b/net/dbus/message.h new file mode 100644 index 0000000..263e5d5 --- /dev/null +++ b/net/dbus/message.h @@ -0,0 +1,64 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* message.h Basic D-Bus message parsing + * + * Copyright (C) 2010 Collabora Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef DBUS_MESSAGE_H +#define DBUS_MESSAGE_H + +#include +#include + +/* No need to implement a feature-complete parser. It only implement what is + * needed by the bus. */ +struct DBusMessage { + char *message; + size_t len; + size_t new_len; + + /* direct pointers to the fields */ + int type; + char *path; + char *interface; + char *member; + char *destination; + char *sender; + char *body_signature; + int body_length; + char *arg0; + char *arg1; + char *arg2; + + /* How to add the 'sender' field in the headers */ + int new_header_offset; + int len_offset; + int padding_end; +}; + +int dbus_message_type_from_string (const char *type_str); + +const char *dbus_message_type_to_string (int type); + +int dbus_message_parse(unsigned char *message, size_t len, + struct DBusMessage *dbus_message); + +int dbus_message_add_sender(struct DBusMessage *dbus_message, + const char *sender, gfp_t gfp_flags); + +#endif /* DBUS_MESSAGE_H */ diff --git a/net/dbus/sysctl_net_dbus.c b/net/dbus/sysctl_net_dbus.c new file mode 100644 index 0000000..1f5cd15 --- /dev/null +++ b/net/dbus/sysctl_net_dbus.c @@ -0,0 +1,62 @@ +/* + * NET4: Sysctl interface to net af_dbus subsystem. + * + * Authors: Mike Shaver. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + */ + +#include +#include + +#include + +static ctl_table dbus_table[] = { + { + .procname = "max_dgram_qlen", + .data = &init_net.unx.sysctl_max_dgram_qlen, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec + }, + { } +}; + +static struct ctl_path dbus_path[] = { + { .procname = "net", }, + { .procname = "dbus", }, + { }, +}; + +int dbus_sysctl_register(struct net *net) +{ + struct ctl_table *table; + + table = kmemdup(dbus_table, sizeof(dbus_table), GFP_KERNEL); + if (table == NULL) + goto err_alloc; + + table[0].data = &net->unx.sysctl_max_dgram_qlen; + net->unx.ctl = register_net_sysctl_table(net, dbus_path, table); + if (net->unx.ctl == NULL) + goto err_reg; + + return 0; + +err_reg: + kfree(table); +err_alloc: + return -ENOMEM; +} + +void dbus_sysctl_unregister(struct net *net) +{ + struct ctl_table *table; + + table = net->unx.ctl->ctl_table_arg; + unregister_sysctl_table(net->unx.ctl); + kfree(table); +} diff --git a/security/capability.c b/security/capability.c index 8168e3e..377d0b4 100644 --- a/security/capability.c +++ b/security/capability.c @@ -554,6 +554,17 @@ static int cap_unix_may_send(struct socket *sock, struct socket *other) return 0; } +static int cap_dbus_stream_connect(struct socket *sock, struct socket *other, + struct sock *newsk) +{ + return 0; +} + +static int cap_dbus_may_send(struct socket *sock, struct socket *other) +{ + return 0; +} + static int cap_socket_create(int family, int type, int protocol, int kern) { return 0; @@ -994,6 +1005,8 @@ void __init security_fixup_ops(struct security_operations *ops) #ifdef CONFIG_SECURITY_NETWORK set_to_cap_if_null(ops, unix_stream_connect); set_to_cap_if_null(ops, unix_may_send); + set_to_cap_if_null(ops, dbus_stream_connect); + set_to_cap_if_null(ops, dbus_may_send); set_to_cap_if_null(ops, socket_create); set_to_cap_if_null(ops, socket_post_create); set_to_cap_if_null(ops, socket_bind); diff --git a/security/security.c b/security/security.c index 351942a..5516f59 100644 --- a/security/security.c +++ b/security/security.c @@ -995,6 +995,19 @@ int security_unix_may_send(struct socket *sock, struct socket *other) } EXPORT_SYMBOL(security_unix_may_send); +int security_dbus_stream_connect(struct socket *sock, struct socket *other, + struct sock *newsk) +{ + return security_ops->dbus_stream_connect(sock, other, newsk); +} +EXPORT_SYMBOL(security_dbus_stream_connect); + +int security_dbus_may_send(struct socket *sock, struct socket *other) +{ + return security_ops->dbus_may_send(sock, other); +} +EXPORT_SYMBOL(security_dbus_may_send); + int security_socket_create(int family, int type, int protocol, int kern) { return security_ops->socket_create(family, type, protocol, kern); diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 5c9f25b..11dfb51 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -65,6 +65,7 @@ #include #include /* for Unix socket types */ #include /* for Unix socket types */ +#include /* for DBus socket types */ #include #include #include @@ -4032,6 +4033,64 @@ static int selinux_socket_unix_may_send(struct socket *sock, return 0; } +static int selinux_socket_dbus_stream_connect(struct socket *sock, + struct socket *other, + struct sock *newsk) +{ + struct sk_security_struct *ssec; + struct inode_security_struct *isec; + struct inode_security_struct *other_isec; + struct common_audit_data ad; + int err; + + isec = SOCK_INODE(sock)->i_security; + other_isec = SOCK_INODE(other)->i_security; + + COMMON_AUDIT_DATA_INIT(&ad, NET); + ad.u.net.sk = other->sk; + + err = avc_has_perm(isec->sid, other_isec->sid, + isec->sclass, + UNIX_STREAM_SOCKET__CONNECTTO, &ad); //FIXME!!! + if (err) + return err; + + /* connecting socket */ + ssec = sock->sk->sk_security; + ssec->peer_sid = other_isec->sid; + + /* server child socket */ + ssec = newsk->sk_security; + ssec->peer_sid = isec->sid; + err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid) +; + + return err; +} + + +static int selinux_socket_dbus_may_send(struct socket *sock, + struct socket *other) +{ + struct inode_security_struct *isec; + struct inode_security_struct *other_isec; + struct common_audit_data ad; + int err; + + isec = SOCK_INODE(sock)->i_security; + other_isec = SOCK_INODE(other)->i_security; + + COMMON_AUDIT_DATA_INIT(&ad, NET); + ad.u.net.sk = other->sk; + + err = avc_has_perm(isec->sid, other_isec->sid, + isec->sclass, SOCKET__SENDTO, &ad); + if (err) + return err; + + return 0; +} + static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, u32 peer_sid, struct common_audit_data *ad) @@ -5565,6 +5624,8 @@ static struct security_operations selinux_ops = { .unix_stream_connect = selinux_socket_unix_stream_connect, .unix_may_send = selinux_socket_unix_may_send, + .dbus_stream_connect = selinux_socket_dbus_stream_connect, + .dbus_may_send = selinux_socket_dbus_may_send, .socket_create = selinux_socket_create, .socket_post_create = selinux_socket_post_create, --- a/include/net/af_dbus.h +++ b/include/net/af_dbus.h @@ -58,7 +58,7 @@ spinlock_t lock; unsigned int gc_candidate : 1; unsigned int gc_maybe_cycle : 1; - struct socket_wq peer_wq; + wait_queue_head_t peer_wait; struct dbus_sock_priv *priv; unsigned int daemon_side; @@ -72,8 +72,6 @@ }; #define dbus_sk(__sk) ((struct dbus_sock *)__sk) -#define peer_wait peer_wq.wait - #ifdef CONFIG_SYSCTL extern int dbus_sysctl_register(struct net *net); extern void dbus_sysctl_unregister(struct net *net); --- a/net/dbus/af_dbus.c +++ b/net/dbus/af_dbus.c @@ -315,16 +315,13 @@ static void dbus_write_space(struct sock *sk) { - struct socket_wq *wq; - - rcu_read_lock(); + read_lock(&sk->sk_callback_lock); if (dbus_writable(sk)) { - wq = rcu_dereference(sk->sk_wq); - if (wq_has_sleeper(wq)) - wake_up_interruptible_sync(&wq->wait); + if (sk_has_sleeper(sk)) + wake_up_interruptible_sync(sk->sk_sleep); sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); } - rcu_read_unlock(); + read_unlock(&sk->sk_callback_lock); } static void dbus_sock_destructor(struct sock *sk) @@ -586,8 +583,7 @@ return sk; } -static int dbus_create(struct net *net, struct socket *sock, int protocol, - int kern) +static int dbus_create(struct net *net, struct socket *sock, int protocol) { if (protocol && protocol != PF_DBUS) return -EPROTONOSUPPORT; @@ -1007,7 +1003,7 @@ current_euid_egid(&newsk->sk_peercred.uid, &newsk->sk_peercred.gid); newu = dbus_sk(newsk); newu->daemon_side = 1; - newsk->sk_wq = &newu->peer_wq; + newsk->sk_sleep = &newu->peer_wait; otheru = dbus_sk(other); newu->bus = otheru->bus; u->bus = otheru->bus; @@ -1126,7 +1122,7 @@ { struct sock *sk = sock->sk; struct dbus_sock *u; - DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr); + struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; int err = 0; if (peer) { @@ -1604,7 +1600,7 @@ dbus_state_lock(sk); for (;;) { - prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); + prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); if (!skb_queue_empty(&sk->sk_receive_queue) || sk->sk_err || @@ -1620,7 +1616,7 @@ clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); } - finish_wait(sk_sleep(sk), &wait); + finish_wait(sk->sk_sleep, &wait); dbus_state_unlock(sk); return timeo; } @@ -1857,7 +1853,7 @@ struct sock *sk = sock->sk; unsigned int mask; - sock_poll_wait(file, sk_sleep(sk), wait); + sock_poll_wait(file, sk->sk_sleep, wait); mask = 0; /* exceptional events? */