diff options
author | Alban Crequy <alban.crequy@collabora.co.uk> | 2012-05-28 12:01:12 +0200 |
---|---|---|
committer | Rodrigo Moya <rodrigo@gnome-db.org> | 2012-05-28 12:08:09 +0200 |
commit | 9f6a163a328a92c0d73b2152bac8ea05f985bc85 (patch) | |
tree | 7e37603edbcc378f8bc845ec978ed09a42e97a4a | |
parent | f75cf852875dbe481a661c1b4147c6ba177b2fc7 (diff) | |
download | dbus-9f6a163a328a92c0d73b2152bac8ea05f985bc85.tar.gz |
bus driver: Move AF_BUS specific code in bus/driver-afbus.[ch]
-rw-r--r-- | bus/Makefile.am | 10 | ||||
-rw-r--r-- | bus/driver.c | 156 | ||||
-rw-r--r-- | driver-afbus.c | 179 | ||||
-rw-r--r-- | driver-afbus.h | 45 |
4 files changed, 236 insertions, 154 deletions
diff --git a/bus/Makefile.am b/bus/Makefile.am index 6cbc09a6..cf3a9627 100644 --- a/bus/Makefile.am +++ b/bus/Makefile.am @@ -63,6 +63,15 @@ endif endif endif +if HAVE_AFBUS +AFBUS_SOURCE = \ + driver-afbus.c \ + driver-afbus.h \ + $(NULL) +else +AFBUS_SOURCE = +endif + BUS_SOURCES= \ activation.c \ activation.h \ @@ -83,6 +92,7 @@ BUS_SOURCES= \ dispatch.h \ driver.c \ driver.h \ + $(AFBUS_SOURCE) \ expirelist.c \ expirelist.h \ policy.c \ diff --git a/bus/driver.c b/bus/driver.c index 444e56a4..b3f18da5 100644 --- a/bus/driver.c +++ b/bus/driver.c @@ -26,6 +26,7 @@ #include "activation.h" #include "connection.h" #include "driver.h" +#include "driver-afbus.h" #include "dispatch.h" #include "services.h" #include "selinux.h" @@ -38,36 +39,6 @@ #include <dbus/dbus-marshal-recursive.h> #include <string.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <asm/types.h> -#include <errno.h> -#include <linux/netlink.h> -#include <linux/rtnetlink.h> -#include <linux/connector.h> -#include <dbus/dbus-transport-afbus.h> - -/* FIXME: CN_IDX_NFDBUS should be allocated and moved in include/linux/connector.h */ -#define CN_IDX_NFDBUS (CN_NETLINK_USERS + 3) -#define CN_VAL_NFDBUS 0x01 - -#define NFDBUS_CMD_ADDMATCH 0x01 -#define NFDBUS_CMD_REMOVEMATCH 0x02 - -struct nfdbus_nl_cfg_req { - __u32 cmd; - __u32 len; - struct sockaddr_bus addr; - __u64 pad; - unsigned char data[0]; -}; - -struct nfdbus_nl_cfg_reply { - __u32 ret_code; -}; - - static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection, DBusMessage *hello_message, BusTransaction *transaction, @@ -976,120 +947,6 @@ bus_driver_handle_update_activation_environment (DBusConnection *connection, return retval; } -static int -ensure_nl_sock(DBusError *error) -{ - static int nlsock = 0; - - struct sockaddr_nl l_local; - int fd; - int ret; - - if (nlsock > 0) - return nlsock; - - fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); - if (fd == -1) { - dbus_set_error (error, DBUS_ERROR_NETLINK, - "Couldn't use the netlink socket: %s", - strerror(errno)); - return -1; - } - - l_local.nl_family = AF_NETLINK; - l_local.nl_groups = 0; - l_local.nl_pid = 0; - ret = bind(fd, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)); - if (ret == -1) { - close(fd); - dbus_set_error (error, DBUS_ERROR_NETLINK, - "Couldn't bind the netlink socket: %s", - strerror(errno)); - return -1; - } - - nlsock = fd; - return nlsock; -} - -static int netlink_send(int nlsock, struct cn_msg *msg, int seq) -{ - struct nlmsghdr *nlh; - unsigned int size; - char buf[4096]; - struct cn_msg *m; - - size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len); - - nlh = (struct nlmsghdr *)buf; - nlh->nlmsg_seq = seq; - nlh->nlmsg_pid = getpid(); - nlh->nlmsg_type = NLMSG_DONE; - nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh)); - nlh->nlmsg_flags = 0; - - m = NLMSG_DATA(nlh); - memcpy(m, msg, sizeof(*m) + msg->len); - - return send(nlsock, nlh, size, 0); -} - -static dbus_bool_t -upload_match_rule (struct sockaddr_bus *address, - const char *rule, - DBusError *error) -{ - static int seq; - - int nlsock; - int ret; - - char buf[sizeof(struct cn_msg) + sizeof(struct nfdbus_nl_cfg_req) + 1024]; - - struct cn_msg *data; - struct nlmsghdr *reply; - struct nfdbus_nl_cfg_req *req; - //struct nfdbus_nl_cfg_reply *req_reply; - - nlsock = ensure_nl_sock (error); - if (nlsock == -1) - return FALSE; - - memset(buf, 0, sizeof(buf)); - - data = (struct cn_msg *)buf; - - data->id.idx = CN_IDX_NFDBUS; - data->id.val = CN_VAL_NFDBUS; - data->seq = seq++; - data->ack = 0; - data->len = sizeof(struct nfdbus_nl_cfg_req) + strlen(rule) + 1; - req = (struct nfdbus_nl_cfg_req *) data->data; - - req->cmd = NFDBUS_CMD_ADDMATCH; - req->len = strlen(rule) + 1; - req->addr = *address; - strcpy((char *)req->data, rule); - - ret = netlink_send(nlsock, data, seq++); - if (ret <= 0) - { - } - - memset(buf, 0, sizeof(buf)); - ret = recv(nlsock, buf, sizeof(buf), 0); - if (ret <= 0) - { - } - - reply = (struct nlmsghdr *)buf; - if (reply->nlmsg_type != NLMSG_DONE) - { - } - - return TRUE; -} - static dbus_bool_t bus_driver_handle_add_match (DBusConnection *connection, BusTransaction *transaction, @@ -1100,8 +957,6 @@ bus_driver_handle_add_match (DBusConnection *connection, const char *text; DBusString str; BusMatchmaker *matchmaker; - struct sockaddr_bus address; - long len = sizeof(address); _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -1134,14 +989,7 @@ bus_driver_handle_add_match (DBusConnection *connection, if (rule == NULL) goto failed; - memset (&address, 0, sizeof (address)); - //address.sbus_family = AF_BUS; - //address.sbus_addr.s_addr= 0x0000000000000000; - //address.sbus_path = 0x00; - if (!dbus_connection_get_peer_address(connection, &address, &len)) - goto failed; - - if (!upload_match_rule (&address, text, error)) + if (!bus_driver_afbus_upload_match_rule (connection, text, error)) goto failed; matchmaker = bus_connection_get_matchmaker (connection); diff --git a/driver-afbus.c b/driver-afbus.c new file mode 100644 index 00000000..0d45c274 --- /dev/null +++ b/driver-afbus.c @@ -0,0 +1,179 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* driver.c Bus client, AF_BUS bits (driver) + * + * Copyright (C) 2012 Collabora Ltd + * + * 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 <config.h> + +#include "driver-afbus.h" + +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <asm/types.h> +#include <errno.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#include <linux/connector.h> +#include <dbus/dbus-transport-afbus.h> + +/* FIXME: CN_IDX_NFDBUS should be allocated and moved in include/linux/connector.h */ +#define CN_IDX_NFDBUS (CN_NETLINK_USERS + 3) +#define CN_VAL_NFDBUS 0x01 + +#define NFDBUS_CMD_ADDMATCH 0x01 +#define NFDBUS_CMD_REMOVEMATCH 0x02 + +struct nfdbus_nl_cfg_req { + __u32 cmd; + __u32 len; + struct sockaddr_bus addr; + __u64 pad; + unsigned char data[0]; +}; + +struct nfdbus_nl_cfg_reply { + __u32 ret_code; +}; + +static int +ensure_nl_sock(DBusError *error) +{ + static int nlsock = 0; + + struct sockaddr_nl l_local; + int fd; + int ret; + + if (nlsock > 0) + return nlsock; + + fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); + if (fd == -1) { + dbus_set_error (error, DBUS_ERROR_NETLINK, + "Couldn't use the netlink socket: %s", + strerror(errno)); + return -1; + } + + l_local.nl_family = AF_NETLINK; + l_local.nl_groups = 0; + l_local.nl_pid = 0; + ret = bind(fd, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)); + if (ret == -1) { + close(fd); + dbus_set_error (error, DBUS_ERROR_NETLINK, + "Couldn't bind the netlink socket: %s", + strerror(errno)); + return -1; + } + + nlsock = fd; + return nlsock; +} + +static int netlink_send(int nlsock, struct cn_msg *msg, int seq) +{ + struct nlmsghdr *nlh; + unsigned int size; + char buf[4096]; + struct cn_msg *m; + + size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len); + + nlh = (struct nlmsghdr *)buf; + nlh->nlmsg_seq = seq; + nlh->nlmsg_pid = getpid(); + nlh->nlmsg_type = NLMSG_DONE; + nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh)); + nlh->nlmsg_flags = 0; + + m = NLMSG_DATA(nlh); + memcpy(m, msg, sizeof(*m) + msg->len); + + return send(nlsock, nlh, size, 0); +} + +dbus_bool_t +bus_driver_afbus_upload_match_rule (DBusConnection *connection, + const char *rule, + DBusError *error) +{ + static int seq; + + int nlsock; + int ret; + + char buf[sizeof(struct cn_msg) + sizeof(struct nfdbus_nl_cfg_req) + 1024]; + + struct cn_msg *data; + struct nlmsghdr *reply; + struct nfdbus_nl_cfg_req *req; + //struct nfdbus_nl_cfg_reply *req_reply; + + struct sockaddr_bus address; + long len = sizeof(address); + + memset (&address, 0, sizeof (address)); + if (!dbus_connection_get_peer_address(connection, &address, &len)) + { + /* Don't return an error if it is not a AF_BUS socket */ + return TRUE; + } + + nlsock = ensure_nl_sock (error); + if (nlsock == -1) + return FALSE; + + memset(buf, 0, sizeof(buf)); + + data = (struct cn_msg *)buf; + + data->id.idx = CN_IDX_NFDBUS; + data->id.val = CN_VAL_NFDBUS; + data->seq = seq++; + data->ack = 0; + data->len = sizeof(struct nfdbus_nl_cfg_req) + strlen(rule) + 1; + req = (struct nfdbus_nl_cfg_req *) data->data; + + req->cmd = NFDBUS_CMD_ADDMATCH; + req->len = strlen(rule) + 1; + req->addr = address; + strcpy((char *)req->data, rule); + + ret = netlink_send(nlsock, data, seq++); + if (ret <= 0) + { + } + + memset(buf, 0, sizeof(buf)); + ret = recv(nlsock, buf, sizeof(buf), 0); + if (ret <= 0) + { + } + + reply = (struct nlmsghdr *)buf; + if (reply->nlmsg_type != NLMSG_DONE) + { + } + + return TRUE; +} diff --git a/driver-afbus.h b/driver-afbus.h new file mode 100644 index 00000000..f9b0d87e --- /dev/null +++ b/driver-afbus.h @@ -0,0 +1,45 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* driver-afbus.h Bus client, AF_BUS bits (driver) + * + * Copyright (C) 2012 Collabora Ltd + * + * 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_DRIVER_AFBUS_H +#define BUS_DRIVER_AFBUS_H + +#include <config.h> +#include <dbus/dbus.h> +#include "connection.h" + +#if HAVE_AFBUS +dbus_bool_t bus_driver_afbus_upload_match_rule (DBusConnection *connection, + const char *rule, + DBusError *error); +#else +inline +dbus_bool_t bus_driver_afbus_upload_match_rule (DBusConnection *connection, + const char *rule, + DBusError *error) +{ + return TRUE; +} +#endif + +#endif /* BUS_DRIVER_AFBUS_H */ |